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

//$comp = mysqli_query("SELECT * FROM `company`") or die(mysqli_error($conn));
//$zcomp = mysqli_fetch_assoc($comp);

$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'];

##############################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' "
                             . " and total != 0 order by subvouch, srno") or die(mysqli_error($conn));
$fmain = mysqli_fetch_object($mainqry);
$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 * FROM tablemast WHERE code='$fmain->tableno'") or die(mysqli_error($conn));
$fettable = mysqli_fetch_object($gettable);


$billno = $fmain->vouchno;

if ($fmain->cashchk == 1) {
    $cash = "CASH";
}
if ($fmain->cardchk == 1) {
    $card = "CARD";

    $paymode = mysqli_query($conn, "SELECT name from paytype where code = '$fmain->paymode'")or die(mysql_error($conn));
    $fpaymode = mysqli_fetch_object($paymode);
    $paytype = $fpaymode->name;
}



if ($fmain->parcel == "Y") {

//    if ($fmain->delvcomp == '') {
    $parcel = "PARCEL";
//    }

    $prclcomp = mysqli_query($conn, "SELECT name from delvcomp where code = '$fmain->delvcomp'")or die(mysql_error($conn));
    $fprclcomp = mysqli_fetch_object($prclcomp);
    $delvcomp = ($fprclcomp->name);
} else {
    $inhouse = "IN HOUSE";
}

include("includes/linkshead.php");
?>

<style>
    table{
        width:100%;
    }
    /*    .subpage{
            background-color: white;
            width:37%;
            padding-left:12px;
            padding-right:10px;
            padding-top:10px;
        }*/
    .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;
    }
    table td {
        font-size: 10px;
    }
    .lpadd{
        padding-left:5px;   
    }
    .rpadd{
        padding-right:10px;   
    }
    .border{
        border: 1px solid black ;
    }
    .subpage {
        width: 90mm;
        min-height: auto;
        padding: 1mm;
        background-color: white;
        /*margin:15mm auto ;*/
    }
    body{
        font-family: Verdana, Geneva, sans-serif;
    }
    .f11{
        font-size: 11px;
    }
    .f12{
        font-size: 12px;
    }
    .f13{
        font-size: 13px;
    }
    .f14 {
        font-size: 14px;
    }
    .f15 {
        font-size: 15px;
    }
    .f17 {
        font-size: 17px;
    }
    .f19 {
        font-size: 19px;
    }
    .f23 {
        font-size: 23px;
    }
</style>
<!--<script>



</script>-->
</head>
<!--<body onload="window.print();">-->    
<body >    
    <div class="subpage" >
        <table>
            <tr><td class="text-center thicker f23" colspan="3" ><?= $fcomp->name; ?></td></tr>
            <tr><td class="text-center bold f14" colspan="3"><?= $fcomp->add1; ?></td></tr>
            <tr><td class="text-center bold f14" colspan="3"><?= $fcomp->add2; ?></td></tr>
            <tr><td class="text-center bold f14" colspan="3"><?= $fcomp->add3 ?></td></tr>
            <tr><td class="text-center bold f14" colspan="3"><?= $fcomp->city . " - " . $fcomp->pin; ?></td></tr>
            <tr><td class="text-center bold f14" colspan="3">PH : <?= $fcomp->tel . "" . $fcomp->mobile; ?></td></tr>
            <tr>
                <td class="bold f13" style='width:30%;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?= $inhouse; ?></td>
                <td class="bold f15" style="width:40%;"><div  style="border:1px solid black; border-radius:5px;padding: 2px;" class='text-center f15'>BILL</div></td>
                <td class="text-center bold f12" style="width:30%;"><?= $delvcomp; ?>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
            </tr>
        </table>
        <!--<div style="border:1px solid black;border-radius: 5px">-->
        <table>
            <tr>
                <td style="width:20%" class="lpadd f14">Bill No.</td>
                <td style="width:30%" class="bold f14">: <?= $billno; ?></td>
                <td style="width:20%" class="lpadd text-right f14">Date :</td>
                <td style="width:30%;" class='lpadd thicker f14' colspan=""><?= $fmain->mydt; ?> </td>
            </tr>
            <tr>
                <td class="lpadd f14" colspan=''>Time </td>
                <td class="bold f14">: <?= $fmain->vouchtime; ?></td>
                <? if ($fmain->parcel == "Y") { ?>
                    <td class="lpadd f14" ></td>
                    <td class="bold f14" ><?= $parcel ?></td>
                <? } else { ?>
                    <td class="text-right f14">Table :</td>
                    <td class="lpadd bold f14"><?= $fettable->name; ?></td>
                <? } ?>

                <td></td>
            </tr>
            <? if ($fmain->parcel == "Y") { ?>
                <tr>
                    <td colspan="" class='lpadd f13'>Name</td>
                    <td colspan="" class='f13'><?= $fmain->custname; ?></td>
                </tr>
                <tr>
                    <td class="lpadd f13" colspan=''>Address : </td>
                    <td class="lpadd f13" colspan='4'><?= $fmain->custaddress ?></td>
                </tr>
            <? } ?>

        </table>
        <!--</div>-->
        <table>
            <tr style='border-top: 1px solid black;border-bottom: 1px solid black'>
                <td style='width:60%' class='bold lpadd f13' colspan='2'>Description</td>
                <td style='width:12%' class='bold text-center lpadd f13'>Qty.</td>
                <td style='width:12%' class='bold lpadd f13'>Rate</td>
                <td style='width:16%' class='bold text-right lpadd f13'>Amt.</td>
            </tr>
            <?php
            $tsub = '';
            $a = 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);

                $msub = $ftran->subvouch;
                
                if ($msub != $tsub) {
                    $tsub = $msub ;
                    $a = 1;
                }

                if ($a == 1) {
                    ?>
                    <tr style="">
                        <td class='lpadd bold' style="font-size:14px;" colspan="4"><span style="font-weight: 600;"><u>KOT NO. : <?= $fmain->vouchno . "(" . $msub . ")"; ?></u></span></td>
                    </tr> 
                <?   $a = 0 ;
                
                } ?>
                <tr style="">
                    <td class='lpadd f19' colspan="2"><?= $fitem['name']; ?></td>
                    <td class='text-center f13 lpadd'><?= $ftran->qty; ?></td>
                    <td  class='f13'><?= round($ftran->rate); ?></td>
                    <td  class='text-right f13'><?= $ftran->amount ?></td>
                </tr>   
                        
                    <? if($ftran->cdiscamt >0 ){?>
                <tr style="">
                    <td class='lpadd f15 ' colspan="2"><i><?= $ftran->remtran ?></i></td>
                    <td class='text-left f13 rpadd'><i>Discount</i></td>
                    <td  class='f13'><i><?= $ftran->cdiscperc ; ?>%</i></td>
                    <td  class='text-right f13'><i>-<?= $ftran->amount - $ftran->total ;?></i></td>
                </tr> 
                    <?} ?>
            <? }
            ?>

        </table>
        <table>
            <tr style='border-top: 2px solid black'>
                <td colspan='' class='text-right  f13 rpadd' >Subtotal</td>
                <td colspan='2' class='text-right f14 bold' ><?= $fmain->subtotal ?></td>
            </tr>
            <? if ($fmain->discamt > 0) { ?>
                <tr>

                    <td colspan='' class='text-right  f13 rpadd' style="width:55%">Disc.</td>
                    <td colspan='' class='text-right f14 bold' style="width:19%"><?= $fmain->discperc; ?>%</td>
                    <td colspan='' class='text-right f14 bold' style="width:26%"><?= $fmain->discamt; ?></td>
                </tr>
            <? } if ($fmain->taxable > 0) { ?>
                <tr>
                    <td colspan='' class='text-right  f13 rpadd'>Taxable Amt.</td>
                    <td colspan='2' class='text-right f14 bold'><?= $fmain->taxable; ?></td>
                </tr>
            <? }if ($fmain->cgstperc > 0) { ?>
                <tr>
                    <td colspan='' class='text-right  f13 rpadd'>CGST</td>
                    <td colspan='' class='text-right f14 bold'><?= $fmain->cgstperc; ?>%</td>
                    <td colspan='' class='text-right f14 bold'><?= $fmain->cgstamt; ?></td>
                </tr>
            <? } if ($fmain->sgstperc > 0) { ?>
                <tr>
                    <td colspan='' class='text-right  f13 rpadd'>SGST</td>
                    <td colspan='' class='text-right f14 bold'><?= $fmain->sgstperc; ?>%</td>
                    <td colspan='' class='text-right f14 bold'><?= $fmain->sgstamt; ?></td>
                </tr>
            <? } ?>

            <tr  style='border-top: 2px solid black;border-bottom: 2px solid black;'>
                <td colspan='' class='bold' ><label class="f12"></label><label class="f13"><?php echo $paytype . $cash; ?></label></td>
                <td colspan='' class='text-right bold f13'>Bill Amt.</td>
                <td colspan='' class='text-right bold f15 bold'><i class='fa fa-inr'></i>&nbsp;<?= $fmain->netamt; ?></td>
            </tr>
            <tr  style='border-top: 2px solid black;'>
                <td colspan='3' class='lpadd bold f12 bold'>Rupees <?= rsword($fmain->netamt); ?>&nbsp;Only</td>
            </tr>
            <tr  style='border-top: 2px solid black;'>
                <td colspan='3' class='lpadd bold f12'>GSTIN : <?= $fcomp->gst; ?></td>
            </tr>
            <tr>
                <td colspan='3' class='lpadd bold f12'>Mumbai Jurisdiction</td>
            </tr>
        </table>
        <br>
        &nbsp;
    </div>
