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


$maindb = 'inwdmain';
$trandb = 'inwdtran';



$formname = 'Barcode Label';

if ($_GET["para"] == 2) {
//    $maindb = 'openmain';
//    $trandb = 'opentran';
//} else {
    $maindb = 'inwdmain';
    $trandb = 'inwdtran';
}

$append_file = 'inw_add.php';
$file = 'inward.php';
$grid = 'inwdmain.php';
$font_size = "2px";
$barcode_width = "170px";
$barcode_height = "25px";

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

$mvouch = $_REQUEST["vouchno"];

$cmain = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE vouchno='$mvouch' ORDER BY srno");

$num = mysqli_num_rows($cmain);
?>
<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: 3px;
                    margin-top: 2px;
            }
            td.one{
                width:50%;
                border:0px solid black;                
            }


        </style>    
    </head>

    
    <body>
        <table width="100%" border="0" cellspacing='0' cellpadding='1px' style='height:100%;border: 0px solid red; margin-top: 0px; margin-bottom: 0px'>
            <tbody>
                <?php
                $ctr = 1;
                $breaker = 1;
                while ($ctmp = mysqli_fetch_object($cmain)) {
                    
                    $num_qty  = $ctmp->qty;
                    $barcode  = $ctmp->barcode;
                    $itemcode =$ctmp->itemcode;
                    $costcode = $ctmp->costcode;
                    $location_code = $ctmp->location;


                    $getitemname = mysqli_query($conn, "SELECT `name` FROM `item` WHERE code='$itemcode'")or die(mysqli_error($conn));
                    $mitemname = mysqli_fetch_object($getitemname);
                    $itemname = $mitemname->name;

                    $getlocname = mysqli_query($conn, "SELECT `name` FROM `location` WHERE code='$location_code'")or die(mysqli_error($conn));
                    $mlocname = mysqli_fetch_object($getlocname);
                    $locationname = $mlocname->name;
                    
                    
                    for ($i = 1; $i <= $num_qty; $i++) {
                        
                        
                        echo "<td width='50%' class='one'>";
                        echo "<table width='100%' style='border:0px solid red;'>";
                        
                        echo "<tr>";
                        echo "<td align='center' class=''>";
                        echo "<center><font size='1px'>".$barcode."</font></center>";
                        
                        echo "<img alt='$itemcode' src='barcode.php?text=$barcode' style='width:$barcode_width;height:$barcode_height;margin:auto;' height='50px'/>";
                        echo "</td>";
                        echo "</tr>";
                        echo "<tr>";
                        echo "<td>";
                        echo "<table width='50%' style='border:0px solid blue;'>";
                        echo "<tr>";
//                        echo "<td align='right'>";
//                        echo "<font size='$font_size'><b> Item Name : </b></font>";
                        echo "<center><font size='1px'>".strtoupper($itemname)."</font></center>";
//                        echo "</td>";
//                        echo "<td>";
//                        echo "<font size='$font_size'><b>" . strtoupper($itemname) . "</b></font>";
//                        echo "</td>";
//                        echo "</tr>";
                        echo "<tr>";
                        echo "<td align='right'>";
                        echo "<font size='$font_size'><b> Cost Code : </b></font>";
                        echo "</td>";
                        echo "<td>";
                        echo "<font size='$font_size'><b>" . strtoupper($costcode) . "</b></font>";
                        echo "</td>";
                        echo "</tr>";
                        echo "<tr>";
                        echo "<td align='right'>";
                        echo "<font size='$font_size'><b> Location : </b></font>";
                        echo "</td>";
                        echo "<td>";
                        echo "<font size='$font_size'><b>" . $locationname . "</b></font>";
                        echo "</td>";
                        echo "</tr>";

                        echo "</td>";
                        echo "</tr>";
                        echo "<tr>";
                        
                        echo "</tr>";
                        echo "</td>";
                        echo "</tr>";
                        echo "<tr>";
                        echo "</tr>";
                        echo "</table>";
                        echo "</td>";
                        echo "</tr>";
                        echo "</table>";
                         //echo "</br>";
                        echo "</td>";
                        
                        

                        if ($ctr == 3) {
                            if($breaker == 24){                               
                            }else{
                               echo "<tr>";
                            }
                            $ctr = 1;
                        } else {
                            $ctr++;
                        }

                        if ($breaker == 24) {
                                 echo "</tbody></table>"; 
                                 echo "<div  style='page-break-before:always;'>";
                                 //echo "<br/>";
                                 echo "<table width='230px' border='0' cellspacing='0' cellpadding='0' style='height:20px;'>";
                                 echo "</br>";
                                 echo "</br>";
                                 $breaker = 1;
                            
                        }else{
                            $breaker++;
                        }

                        
                    }
                }
                ?>
            </tbody>
        </table>


    </body>
</html>