<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include ('includes/function.php');

$formname = 'Barcode Label';
$font_size = "2px";
$barcode_width = "155px";
$barcode_height = "20px";

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);

$zroot = $_SESSION["zdbroot"];

$costcode = '';
$mvouch = $_REQUEST["vouchno"];

$maindb = 'purmain';
$trandb = 'purtran';
$mvouchdt = $_REQUEST["vouchdt"];
$cmain = mysqli_query($conn, "SELECT a.*,b.name as itemname, b.mrp as mmrp, b.msp as mmsp FROM `$trandb` a, `item` b  
                     WHERE a.vouchno='$mvouch' and a.itemcode =b.code ORDER BY a.srno") or die(mysqli_error($conn));
$num = mysqli_num_rows($cmain);
$xnum = $num - 1;
//browse($xnum);
?>

<html>
    <head>
        <title><?= $formname ?></title>
        <style type="text/css">
            table { page-break-inside:auto }
            tr    { page-break-inside:avoid; page-break-after:auto }
            thead { display:table-header-group }
            tfoot { display:table-footer-group }
            table {
                table-layout:fixed;
                width:100%;
                margin-left: 1px;
                margin-bottom: 1px;
                margin-top: 3px;
            }
            td.one{
                width:20%;
                border:0px solid black;                
            }

            .page {

                width: 210mm;
                min-height: 290mm;
                padding: 1mm;
                margin:2mm auto;
                /*border: 1px #D3D3D3 solid;*/
                /*border-radius: 5px;*/
                background: white;

            }
        </style>

    </head>
    <body >
<!--        <table width='100%' class="page" border='0' cellspacing='0' cellpadding='0' style=''>
        -->
<!--<tbody>-->
        <?php
//                browse(fmod(3,5));
        $ctr = 0;
        $breaker = 1;
        $location = '';
        while ($ctmp = mysqli_fetch_object($cmain)) {
            $num_qty = $ctmp->qty;
            $barcode = $ctmp->itemcode;
            $itemname = $ctmp->itemname;
            $msp1 = $ctmp->mmsp;
            $mrp = $ctmp->mmrp;
            $msp = round($msp1, 0);
            $convmsp = (string) $msp;
            $clen = strlen($convmsp);

            $dcode = "";
            for ($i = 0; $i < $clen; $i++) {
                $c1 = substr($convmsp, $i, 1);
                $getmsp = mysqli_query($conn, "Select * from $zroot.msp where no = '$c1'") or die(mysqli_error($conn));
                $fetmsp = mysqli_fetch_array($getmsp);
                $codmsp = $fetmsp[code];
                $dcode = $dcode . $codmsp;
            }


            if ($num_qty < 5) {
                $bal5 = 5 - $num_qty;
            } else {
                $bal5 = fmod($num_qty, 5) + 1;
            }
            for ($i = 1; $i <= $num_qty; $i++) {


                $ctr++;
                $lblctr++;
                if ($ctr == 1) {
//                browse($ctr);
                    echo "<table border=\"0\" align=\"left\" style =\"width: 45mm;height:36mm\">";

                    echo "\n <tr> \n ";
                    echo "\n <td style=\"width: 40mm; max-width: 40mm; height:35mm; padding-left:2mm;padding-top:1mm;\" valign=\"top\"> \n";
                } else {
//                browse($ctr . '  2');
                    echo "\n <td style=\"width: 40mm; max-width: 40mm; height:35mm; padding-left:1mm;\" valign=\"top\"> \n";
                }

                echo "<span>Barcode $barcode ";
                echo "<br /> <img alt='$itemcode' src='barcode.php?text=$barcode' style=''/>";
                echo '<br />' . strtoupper($itemname);
                echo '<br />  Qty. : 1 PCS.';
                echo '<br />' . "<b>MRP : " . $mrp . "</b>";
                echo '<br />' . $dcode;

                echo "</span> ";
                echo "\n";
                echo "</td>";
                if ($ctr == 5) {
                    echo "\n </tr> \n ";
                    $ctr = 0;
                    echo "</table>";
                }
                if ($lblctr >= 40) {
                    $lblctr = 0;
                }
            }
        }
        $bstart = $num_qty + 1;
        if ($bal5 > 0) {
//                    browse('gya');
//                    browse($bal5);
            for ($i = 1; $i <= $bal5; $i++) {

//                        browse('aaya');
                $ctr++;
                $lblctr++;
                if ($ctr == 1) {
//                    browse($ctr . ' 3');
                    echo "<table border=\"0\" align=\"left\" style =\"width: 45mm;height:36mm\">";
                    echo "\n <tr> \n ";
                    echo "\n <td style=\"width: 40mm; max-width: 40mm; height:35mm; padding-left:1mm;\" valign=\"top\"> \n";
                } else {
//                    browse($ctr . '  4');
                    echo "\n <td style=\"width: 40mm; max-width: 40mm; height:35mm; padding-left:1mm;\" valign=\"top\"> \n";
                }

                echo "<span><b>&nbsp;&nbsp;</b>";
                echo "<br />&nbsp;&nbsp;";

                echo '<br />&nbsp;&nbsp;';
                echo '<br />&nbsp;&nbsp;';
                echo '<br />&nbsp;&nbsp;';
                echo '<br />&nbsp;&nbsp;';

                echo "</span> ";
                echo "\n";
                echo "</td>";
                if ($ctr == 5) {
                    echo "\n </tr> \n ";
                    $ctr = 0;
                    echo "</table>";
                }
            }
        }
###################################
        ?>

    </body>
</html>