<?php
ob_end_clean();
ini_set('memory_limit', '1024M');
session_start();

//error_reporting(E_ALL);
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
ini_set("error_reporting", E_ALL & ~E_DEPRECATED);
include ('includes/function.php');

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$maindb = '';
$trandb = '';
$mvouch = $_REQUEST["vouchno"];

$formname = 'Barcode Label';
if ($_GET["para"] == 1) {
    $maindb = 'purmain';
    $trandb = 'purtran';
    $mvouchdt = $_REQUEST["vouchdt"];
} elseif ($_GET["para"] == 2) {
    $maindb = 'openmain';
    $trandb = 'opentran';
    $mvouchdt = date("Y-m-d");
} elseif ($_GET["para"] == 3) {
    $mvouchdt = date("Y-m-d");
    $maindb = 'item';
    $trandb = '';
}
$d = date_parse_from_format("Y-m-d", $mvouchdt);
$month = $d["month"];

$year = $d["year"];
$tyear = substr($year, 2);
$year1 = substr($tyear, 1);
$year2 = substr($tyear, 0, 1);

switch ($month) {
    case '1':
        $monn = 'A';
        break;
    case '2':
        $monn = 'B';
        break;
    case '3':
        $monn = 'C';
        break;
    case '4':
        $monn = 'D';
        break;
    case '5':
        $monn = 'E';
        break;
    case '6':
        $monn = 'F';
        break;
    case '7':
        $monn = 'G';
        break;
    case '8':
        $monn = 'H';
        break;
    case '9':
        $monn = 'I';
        break;
    case '10':
        $monn = 'J';
        break;
    case '11':
        $monn = 'K';
        break;
    case '12':
        $monn = 'L';
        break;

    default:
        break;
}
//browse($monn);

$append_file = 'inw_add.php';
$file = 'inward.php';
$grid = 'inwdmain.php';
//$font_size = "22px";
$barcode_width = "80%";
if ($_GET["para"] == 3) {
//    require("includes/dbconnect.php");
    $cmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE code='$mvouch'") or die(mysqli_error($conn));
    $num = $_GET["number"];
    $xnum = $num - 1;
} else {
    mysqli_select_db($conn, $zdb);
    $cmain = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE vouchno='$mvouch' ORDER BY srno") or die(mysqli_error($conn));
    $num = mysqli_num_rows($cmain);
    $xnum = $num - 1;
}
//browse($num);
$border = "0px";
?>
<html>    
    <head>        
        <title>
            <?= $formname ?>
        </title>    
    </head>    
    <style type="text/css">     
        table {
            page-break-inside:auto }
        tr    {
            page-break-inside:avoid;
            page-break-after:auto;
        }
        td    {
            border:0px solid black;
        }
        thead {
            display:table-header-group }
        tfoot {
            display:table-footer-group }
        table {
            table-layout:fixed;
            width:100%;
        }
        td.one{
            width:100%;
            border:<?= $border ?> solid black;
        }
        body{
            font-family: 'Verdana';
        }
    </style>        
    <script src="js/jquery.js" type="text/javascript">
    </script>        
    <script>
        $(document).ready(function () {

        });
    </script>        
    <body>        
        <table width="100%" align="center" border="0" cellspacing='0' cellpadding='0' style='height:100%;border: <?= $border ?> solid red;margin-top:-5px;'>            
            <tbody>                
                <?php
//browse($num);                
                $ctr = 0;
                $breaker = 1;
//                require("includes/dbconnect.php");
                while ($ctmp = mysqli_fetch_object($cmain)) {

                    $ctr++;
                    if ($_GET["para"] == 3) {
                        $num_qty = $_GET["number"];
                        $itemname = $ctmp->name;
                        $mrp = $ctmp->mrp;
                        $barcode = $ctmp->code;
                    } else {
//                        $num_qty = $ctmp->qty;
                        $num_qty = $ctmp->qty;
                        $itemcode = $ctmp->itemcode;
                        $citemname = mysqli_query($conn, "SELECT * FROM `item` WHERE code='$itemcode' ") or die(mysqli_error($conn));
                        $fitemname = mysqli_fetch_array($citemname);
                        $itemname = $fitemname[name];
                        $barcode = $fitemname[code];
                        $mrp = $ctmp->mrp;
                        
                    }
                    

                    for ($i = 1; $i <= $num_qty; $i++) {
                        echo "<td width='100%' class='one'>";
                        echo "<table width='100%' style='border:$border solid red;height:100%;/*margin-top:10px;*/padding-left:10px;'>";

                        echo "<tr>";
                        echo "<td align='center' valign='top' style='height:20px;'>";
                        echo "<br/>";
                        echo "<center><font size='3px' style='margin-top:10px;'>" . $barcode . "</font></center>";
                        echo "<img alt='$itemname' src='barcode.php?text=$barcode' style='width:$barcode_width;height:33px;margin:auto;' />";
                        echo "</td>";
                        echo "</tr>";

                        echo "<tr>";
                        echo "<td align='center' valign='top' >";
                        echo "<font  style='font-size: 11px;' ><b>" . $itemname . "</b></font>";
                        echo "</td>";
                        echo "</tr>";

                        echo "<td style='text-align: center'>";
//                        echo "<font  style='font-size: 16px;' ><b>Qty. : 1 PCS.</b></font>";
                        echo "<font  style='font-size: 13px;' ><b>BENHUR</b></font>";
                        echo "</td>";
                        echo "</tr>";

                        echo "<tr>";
                        echo "<td>";
                        echo "<font style='font-size: 13px;' ><b>MRP : " . $mrp . "</b></font>";
                        echo "</td>";
                        echo "</tr>";


                        echo "</table>";
                        echo "</td>";
                        echo "</tr>";
                        echo "</table>";
                        echo "</td>";
                        echo "</tr>";
                        echo "</tr>";
                        echo "</table>";
                        if ($i >= $num_qty && $ctr >= $num) {
                            exit;
                        } else {
                            echo "<div  style='page-break-before:always;'>";
                            echo "<table width='100%' align='center' border='1' cellspacing='0' cellpadding='0' style='height:100%;border:$border solid green;page-break-before:always;margin-top:10px;'>";
                        }
                    }
                }
                ?>            
            </tbody>        
        </table>    
    </body>
</html>
