<?php

ob_end_clean();
ini_set("memory_limit", "200M");
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 = 'inwdmain';
    $trandb = 'inwdtran';
    $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;
}
$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: 'Arial Black';
        }
    </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;
                    } else {
//                        $num_qty = $ctmp->qty;
//                        $num_qty = $ctmp->nolabel;
                        $num_qty = $ctmp->nosets;
                        $itemname = $ctmp->item;
                    }
                    $barcode = $ctmp->barcode;
                    $compcode = $ctmp->compcode;
                    $itemqty = $ctmp->itemqty;
                    $sizecode = $ctmp->sizecode;
                    $colrcode = $ctmp->colrcode;
                    
                    $compqry = mysqli_query($conn, "SELECT * FROM `compmast` WHERE compcode='$compcode' ")or die(mysqli_error($conn));
                    $compget = mysqli_fetch_array($compqry);
                    $company = $compget["name"];
                    $itemqry = mysqli_query($conn, "SELECT * FROM `item` WHERE barcode='$barcode' ")or die(mysqli_error($conn));
                    $itemget = mysqli_fetch_array($itemqry);
                    $article = $itemget["article"];
                    $colqry = mysqli_query($conn, "SELECT * FROM `colour` WHERE colrcode='$colrcode' ")or die(mysqli_error($conn));
                    $colget = mysqli_fetch_array($colqry);
                    $colour = $colget["name"];
                    $sizeqry = mysqli_query($conn, "SELECT * FROM `size` WHERE sizecode='$sizecode' ")or die(mysqli_error($conn));
                    $sizeget = mysqli_fetch_array($sizeqry);
                    $size = $sizeget["name"];
                    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='1px' style='margin-top:15px;'>" . $barcode . "</font></center>";
                        echo "<img alt='$itemname' src='barcode.php?text=$barcode' style='width:$barcode_width;height:35px;margin:auto;' />";
                        echo "</td>";
                        echo "</tr>";
                        
                        echo "<tr>";
                        echo "<td align='center' valign='top' >";
                        echo "<font  style='font-size: 14px;' ><b>" . $itemname . '  ' . $year2 . $monn . $year1. "</b></font>";
                        echo "</td>";
                        echo "</tr>";
                       
                        echo "<td>";
                        echo "<font  style='font-size: 16px;' ><b>" . strtoupper($company) . "</b></font>";
                        echo "</td>";
                        echo "</tr>";
                        
                        echo "<tr>";
                        echo "<td>";
                        echo "<font style='font-size: 24px;' ><b>" . strtoupper($article) . "</b></font>";
                        echo "</td>";
                        echo "</tr>";
                        
                        echo "<tr>";
                        echo "<td>";
                        echo "<font style='font-size: 24px;' ><b>" . strtoupper($colour) . "</b></font>";
                        echo "</td>";
                        echo "</tr>";
                        
                        echo "<tr>";
                        echo "<td valign='bottom'>";
                        echo "<font style='font-size: 16px;' ><b>" . strtoupper($size) . "</b></font>";
                        echo "<br/>";
                        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>
