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

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

$maindb = 'kotmain';
$trandb = 'kottran';

$zroot = $_SESSION["zdbroot"];
$formname = 'Demand Form Print';
$root = $_SESSION['zdir'];
$vouchno = $_GET['vouchno'];
$usertype = $_SESSION["usertype_" . $dbname];

$menuaction = "";
if ($usertype != "W") {
    $menuaction = "menu.php";
} else {
    $menuaction = "menu2.php";
}
##############################Print Start Here##################################
$mainqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d/%m/%Y')as mydt FROM $maindb WHERE vouchno='$vouchno' ") or die(mysqli_error($conn));
$tranqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d/%m/%Y')as mydt FROM $trandb WHERE vouchno='$vouchno' order by vouchno") or die(mysqli_error($conn));
$fmain = mysqli_fetch_object($mainqry);
$totqty = $fmain->totqty;

$compqry = mysqli_query($conn, "SELECT * FROM $zroot.company WHERE code='1'") or die(mysqli_error($conn));
$fcomp = mysqli_fetch_object($compqry);
$gettable = mysqli_query($conn, "SELECT name from tablemast where code = '$fmain->tableno' ")or die(mysqli_error($conn));
$ftable = mysqli_fetch_object($gettable);

$getsubvouch = mysqli_query($conn, "SELECT MAX(subvouch) as maxsvouch from `$trandb` where `vouchno`='$vouchno' ")or die(mysqli_error($conn));
$fsubvouch = mysqli_fetch_object($getsubvouch);


if ($fmain->parcel == "Y") {
    $parcel = "PARCEL";
} else {
    $inhouse = "IN HOUSE";
}
include("includes/linkshead.php");
?>
<!--<link href="MyStyle/rep_format.css" rel="stylesheet">-->
<style>
    table{
        width:100%;
    }
    .itembd{
        border-top: 0px;
        border-right: 1px;
        border-left: 1px;
        border-bottom: 1px;
        border-width: 1px;
        border-style:solid;
        border-color: black;
    }
    .bold{
        font-weight: bold;
    }
    .thicker {
        font-weight: 900;
    }
    .f14 {
        font-size: 12px;
    }
    .f15 {
        font-size: 21px;
    }
    .f17 {
        font-size: 17px;
    }
    .f19 {
        font-size: 25px;
    }
    table td {
        font-size: 11px;
    }
    .lpadd{
        padding-left:5px;   
    }
    .rpadd{
        padding-right:5px;   
    }


    @media print
    {    
        .noprint
        {
            display: none !important;
        }
    }
    .subpage {
        width: 80mm;
        min-height: auto;
        padding: 1mm;
        background-color: white;
    }

    body{
        font-family: 'Sans-serif', Arial Verdana;
    }
    .black{
        color:black;
    }
</style>
</head>
<!--<body onload="window.print();">-->
<body>
    <a class="noprint" href="<?= $menuaction; ?>">
        <button type="button" class="btn btn-warning btn-md noprint" style="background-color: #f7b64e">Go Back</button>
    </a>
    <div class="subpage">
        <br>

        <table style="padding-bottom: 2px">
            <tr><td class="text-center thicker" colspan="3" style='font-size: 17px'><?= $fcomp->pname; ?></td></tr>

            <tr>
                <? if ($fsubvouch->maxsvouch == 0) { ?>
                    <td class="text-center thicker" colspan="3" style='font-size: 19px'>TOKEN NO. : <?= $fmain->vouchno; ?></td></tr>
            <? } else { ?>
                <td class="text-center thicker" colspan="3" style='font-size: 19px'>TOKEN NO. : <?= $fmain->vouchno . "(" . $fsubvouch->maxsvouch . ")"; ?></td></tr>
            <? } ?>
            <tr ><td class="bold text-center" style='width:45%;font-size:14px;'><?= $inhouse; ?></td>
                <td class="bold" style="width:15%;"><div  style="border:1px solid black; border-radius:5px;padding: 2px;font-size: 17px" class='text-center'>&nbsp;&nbsp;KOT&nbsp;&nbsp;</div></td>
                <td class="bold text-center" style="width:40%;font-size:14px;"></td>
            </tr>
            <tr><td colspan="3">&nbsp;</td></tr>
        </table>
        <table>
            <tr>
                <td style="width:19%;" class="lpadd f15"><b><?= $fmain->mydt; ?></b></td>
                <td style="width:30%" class="bold"></td>
                <td style="width:50%" class="f15"><b>Time <?= $fmain->vouchtime; ?></b></td>
            </tr>

            <tr>
                <? if ($ftable->name != '') { ?>
                    <td class="lpadd f15" colspan='' >Table # <b><label class="f15"><?= $ftable->name; ?></label></b></td>
                <? } else { ?>
                    <td class="lpadd f15" colspan='' ><b><?= $parcel; ?></b></td>    
                <? }
                ?>
                <td class="bold"></td>
                <td class="" style="font-size:19px"><b><?= $fmain->user; ?></b></td>
            </tr>
<!--            <tr style='border-top: 1px solid black;border-bottom: 1px solid black'>
                <td class='bold lpadd f15' colspan=''>Sr.</td>
                <td class='bold lpadd f15' colspan='3'>Particulars</td>
                <td class='bold text-right lpadd f15'>Qty.</td>
            </tr>-->
        </table>
        <table>
            <tr style='border-top: 1px solid black;border-bottom: 1px solid black'>
                <!--<th class='bold lpadd f15 black' colspan='' style='width:11%'>Sr.</th>-->
                <th class='bold text-center lpadd f15 black' style='width:20%'>Qty.</th>
                <th class='bold lpadd f15 black' style='width:79%'>Particulars</th>
            </tr>
            <?php
            $ctr = 0;
            $tqty = 0;
            while ($ftran = mysqli_fetch_object($tranqry)) {
                $itemqry = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `code` = '$ftran->item'") or die(mysqli_error($conn));
                $fitem = mysqli_fetch_array($itemqry);

                $ctr++;
                ?>
                <tr style="">
                    <!--<td  class='lpadd f17'><?= $ctr; ?></td>-->
                    <td  class='text-center lpadd  f19'><?= $ftran->qty; ?></td>
                    <td  class='lpadd f19' ><?= $fitem['name']; ?></td>
                </tr>   
                <tr>
                    <td  class='lpadd f17 bold' colspan="2"><i><u><?= $ftran->remkot ; ?></u></i></td>
                </tr> 
                
            <?php
            $tqty = $tqty + $ftran->qty;
            } ?>
            <tr style="border-top:1px solid black;border-bottom:1px solid black;padding-bottom: 15px"> 
                <!--<td></td>-->
                <td class="text-center rpadd bold f15" ><?= round($tqty); ?></td>
                <td class="text-left bold f15" colspan="">Total Qty.</td>
            </tr>
        </table>
        <table>
            <tr>
                <th>&nbsp;</th>
            </tr>
        </table>
    </div>
</body>
