<?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());
//$zcomp = mysqli_fetch_assoc($comp);

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

$fseries = $_SESSION["series_" . $dbname];

$keycond = " AND `series`='$fseries' ";

$maindb = 'salemain';
$trandb = 'saletran';

$zroot = $_SESSION["zdbroot"];
$formname = 'Invoice Print';
$root = $_SESSION['zdir'];
$vouchno = $_GET['vouchno'];

##############################Print Start Here##################################
$mainqry = mysqli_query("SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as mydt FROM $maindb WHERE vouchno='$vouchno' $keycond") or die(mysqli_error());
$tranqry = mysqli_query("SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as mydt FROM $trandb WHERE vouchno='$vouchno' $keycond order by srno") or die(mysqli_error());
$fmain = mysqli_fetch_object($mainqry);
$compqry = mysqli_query("SELECT * FROM $zroot.series WHERE code='$fseries'") or die(mysqli_error());
$fcomp = mysqli_fetch_object($compqry);


$billno = $fmain->vouchno;

if ($fmain->cashchk == 1) {
    $paytype = "CASH";
    
    if ($fmain->cardchk == 1) {
       $paytype = $paytype + " / CARD";
    }
} else {
    if ($fmain->cardchk == 1) {
       $paytype = "CARD";
    }
}

include("includes/linkshead.php");
//browse($fcomp->name);
$print_page = '';

//for ($i = 0; $i <= 1; $i++) {
//$tranqry = mysqli_query( "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as mydt FROM $trandb WHERE vouchno='$vouchno' and cdiscperc != '100.00' order by subvouch, srno") or die(mysqli_error());

if ($i == 0) {
    $print_page = "style='page-break-after: always;'";
}
?>

<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;
    }
    .f8{
        font-size: 8px;
    }
    .f9{
        font-size: 9px;
    }
    .f10{
        font-size: 10px;
    }
   
    .f11{
        font-size: 11px;
    }
    .f12{
        font-size: 12px;
    }
    .f13{
        font-size: 13px;
    }
    .f14 {
        font-size: 14px;
    }
    .f15 {
        font-size: 15px;
    }
    .f16 {
        font-size: 16px;
    }
    .f17 {
        font-size: 17px;
    }
    .f19 {
        font-size: 19px;
    }
    .f20 {
        font-size: 20px;
    }
    .f23 {
        font-size: 23px;
    }
</style>
<!--<script>



</script>-->
</head>
<body onload="window.print();">    
    <!--<body >-->    
    <div class="subpage" <?= $print_page ?>>
        <table>
            <tr><td class="text-center thicker f20" colspan="3" ><?= $fcomp->compname; ?></td></tr>
            <tr><td class="text-center bold f12" colspan="3"><?= $fcomp->address; ?></td></tr>
            <?php if ($fcomp->tel != '' || $fcomp->mobile) { ?>
                <tr><td class="text-center bold f12" colspan="3">PH : <?= $fcomp->tel . " / " . $fcomp->mobile; ?></td></tr>
            <?php } ?>
            <tr><td class="text-center bold f12" colspan="3">GST No. : <?= $fcomp->gstno; ?></td></tr>
            <tr>
                <td class="bold f13" style='width:30%;'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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%;">&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 f11">Bill No.</td>
                <td style="width:30%" class="bold f11">: <?= $billno; ?></td>
                <td style="width:20%" class="lpadd text-right f11">Date :</td>
                <td style="width:30%;" class='lpadd thicker f11' colspan=""><?= $fmain->mydt; ?> </td>
            </tr>
            <tr>
                <td class="lpadd f11" colspan=''>Time </td>
                <td class="bold f11">: <?= $fmain->enttime; ?></td>

                <td></td>
                <td></td>
            </tr>
            <?
            $prname = '';
            if ($fmain->nameprint != "") {
                $prname = $fmain->nameprint;
            } else {
                $getname = mysqli_query("SELECT accode,name,concat(add1,add2,add3) as address FROM accmast WHERE accode='$fmain->party'") or die(mysqli_error());
                $fetname = mysqli_fetch_object($getname);
                $prname = $fetname->name;
            }
            ?>
            <tr>
                <td colspan="" class='lpadd f11'>Name</td>
                <td colspan="3" class='bold f12'>: <?= $prname; ?></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:5%' class='bold lpadd f13'>Sr.</td>
                <td style='width:20%' class='bold lpadd f13'>Item</td>
                <td style='width:9%' class='bold text-center lpadd f13'>Qty.</td>
                <td style='width:9%' class='bold text-center lpadd f13'>Unit</td>
                <td style='width:11%' class='bold lpadd f13'>Rate</td>
                <td style='width:16%' class='bold text-right lpadd f13'>Amt.</td>
            </tr>
            <?php
            $tsub = '';
            $a = 0;
            $ctr = 0;
            $tqty = 0;
            $itemamt5 = $itemamt12 = $itemamt18 = $itemamt28 = 0;
            while ($ftran = mysqli_fetch_object($tranqry)) {
                
                $gstp = round($ftran->cgstrate + $ftran->sgstrate + $ftran->igstrate) ;

                switch ($gstp) {
                    case 5:
                        $itemamt5 = $itemamt5 + $ftran->subtot ;
                        break;
                    case 12:
                        $itemamt12 = $itemamt12 + $ftran->subtot ;
                        break;
                    case 18:
                        $itemamt18 = $itemamt18 + $ftran->subtot ;
                        break;
                    case 28:
                        $itemamt28 = $itemamt28 + $ftran->subtot ;
                        break;
                    default:
                }
                $ctr++;
                $itemqry = mysqli_query("SELECT `code`,`name` FROM `item` WHERE `code` = '$ftran->item'") or die(mysqli_error());
                $fitem = mysqli_fetch_array($itemqry);
                ?>
                <tr style="">
                    <td class='lpadd f13'><?= $ctr; ?></td>
                    <td class='lpadd f13' colspan="5"><?= $fitem['name']; ?></td>
                </tr>   
                <tr style="">
                    <td class='lpadd' colspan="1"></td>
                    <td class='text-center f10 lpadd'><?= number_format($gstp,2) ; ?>%</td>
                    <td class='text-center f10 bold lpadd'><?= $ftran->qty; ?></td>
                    <td class='text-center f10 bold lpadd'><?= $ftran->unit; ?></td>
                    <td  class='text-center f10 bold'><?= $ftran->ratebfdisc; ?></td>
                    <td  class='text-right f10 bold'><?= $ftran->total; ?></td>
                </tr>   

                <? if ($ftran->cdiscamt > 0) { ?>
                    <tr style="">
                        <td class='lpadd f15 ' colspan="2"></td>
                        <td class='text-left f13 rpadd'><i>Discount</i></td>
                        <td  class='f13'><i><?= $ftran->discperc; ?>%</i></td>
                        <td  class='text-right f13'><i>-<?= $ftran->discount; ?></i></td>
                    </tr> 
                <? } ?>
                <?
                $tqty = $tqty + $ftran->qty;
            }
            ?>

        </table>
        <table>
            <tr style='border-top: 2px solid black'>
                <td colspan='' class='text-right  f13 rpadd ' style="width:33%">Total Qty.</td>
                <td colspan='' class='text-right  f13 rpadd ' style="width:40%"><span class="bold"> <?= $tqty ?> </span>&nbsp;&nbsp;Subtotal</td>
                <td colspan='' class='text-right f14 ' style="width:28%" ><?= round($fmain->subtotal); ?></td>
            </tr>
            <? if ($fmain->litemdisc > 0) { ?>
                <tr>
                    <td colspan='' class='text-right f14 ' style="width:33%"></td>
                    <td colspan='' class='text-right f13 rpadd' style="width:40%">Disc.</td>
                    <td colspan='' class='text-right f14 ' style="width:28%"><?= $fmain->litemdisc; ?></td>
                </tr>
            <? } if ($fmain->tottaxamt > 0) { ?>
                <tr>
                    <td colspan='2' class='text-right  f13  rpadd'>Taxable Amt.</td>
                    <td colspan='' class='text-right f14 '><?= round($fmain->tottaxamt); ?></td>
                </tr>
            <? } ?>
            <tr>
                <td colspan="5" style="border:none;border-collapse: collapse;">
                    <table  border="solid 1px" style="border-collapse: collapse; width:100%">
                        <tbody style="border-collapse: collapse;">

                            <tr>

                                        <!--<td style="width:57px;">&nbsp;</td>-->
                                <td style="width:32px;text-align: right">%</td>
                                <td style="width:89px;text-align: right" >Item Amt.</td>
                                <td style="width:89px;text-align: right" >CGST</td>
                                <td style="width:89px;text-align: right">SGST</td>
                                <!--<td style="width:89px;text-align: right">IGST</td>-->
                                <td style="width:99px;">&nbsp;</td>
                            </tr>
                            <?php
                            if ($fmain->totgst5 > 0) {
                                Echo "<tr style='height: 21px'>";
//                                    Echo "<td class='text-right' style='text-align: right;'><b>&nbsp;</b></td>";
                                Echo "<td class='text-right' style='text-align: right;'><b>5%</b></td>";
                                Echo "<td class='text-right' style='text-align: right;'>" . number_format($itemamt5,2) . "</td>";
                                Echo "<td class='text-right' style='text-align: right;'>$fmain->cgst5</td>";
                                Echo "<td class='text-right' style='text-align: right;'>$fmain->sgst5</td>";
//                                    Echo "<td class='text-right' style='text-align: right;'>$fmain->igst5</td>";
                                Echo "<td class='text-right' style='text-align: right;'>$fmain->totgst5</td>";
                                Echo "</tr>";
                            }
                            if ($fmain->totgst12 > 0) {

                                Echo "<tr style='height: 21px'>";
//                                    Echo "<td class='text-right'><b>&nbsp;</b></td>";
                                Echo "<td class='text-right' style='text-align: right;'><b>12%</b></td>";
                                Echo "<td class='text-right' style='text-align: right;'>" . number_format($itemamt12,2) . "</td>";
                                Echo "<td class='text-right' style='text-align: right;'>$fmain->cgst12</td>";
                                Echo "<td class='text-right' style='text-align: right;'>$fmain->sgst12</td>";
//                                    Echo "<td class='text-right' style='text-align: right;'>$fmain->igst12</td>";
                                Echo "<td class='text-right' style='text-align: right;'>$fmain->totgst12</td>";
                                Echo "</tr>";
                            }
                            if ($fmain->totgst18 > 0) {

                                Echo "<tr style='height: 21px'>";
//                                    Echo "<td class='text-right'><b>&nbsp;</b></td>";
                                Echo "<td class='text-right' style='text-align: right;'><b>18%</b></td>";
                                Echo "<td class='text-right' style='text-align: right;'>" . number_format($itemamt18,2) . "</td>";
                                Echo "<td class='text-right' style='text-align: right;'>$fmain->cgst18</td>";
                                Echo "<td class='text-right' style='text-align: right;'>$fmain->sgst18</td>";
//                                    Echo "<td class='text-right' style='text-align: right;'>$fmain->igst18</td>";
                                Echo "<td class='text-right' style='text-align: right;'>$fmain->totgst18</td>";
                                Echo "</tr>";
                            }
                            if ($fmain->totgst28 > 0) {

                                Echo "<tr style='height: 21px'>";
//                                    Echo "<td class='text-right'><b>&nbsp;</b></td>";
                                Echo "<td class='text-right' style='text-align: right;'><b>28%</b></td>";
                                Echo "<td class='text-right' style='text-align: right;'>" . number_format($itemamt28,2) . "</td>";
                                Echo "<td class='text-right' style='text-align: right;'>$fmain->cgst28</td>";
                                Echo "<td class='text-right' style='text-align: right;'>$fmain->sgst28</td>";
//                                    Echo "<td class='text-right' style='text-align: right;'>$fmain->igst28</td>";
                                Echo "<td class='text-right' style='text-align: right;'>$fmain->totgst28</td>";
                                Echo "</tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </td>
            </tr>
            <tr  style='border-top: 2px solid black;border-bottom: 2px solid black;'>
                <td colspan='' class='bold' ><label class="f9">(Inclusive Of Taxes)</label></td>
                <td colspan='' class='text-center bold f16'>Bill Amt.</td>
                <td colspan='' class='text-right bold f17 bold'><i class="fa fa-inr"></i>&nbsp;<?= round($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='' class='lpadd bold' ><label class="f10">Paid By <?php echo $paytype ; ?></label></td>
            </tr>
                
            <tr  style=''>
                <td colspan='3' class='lpadd bold f8'>* Buy online at www.urbangreensindia.com</td>
            </tr>
            <tr  style=''>
                <td colspan='3' class='lpadd bold f8'>* Visit us at www.urbangreenindia.com for offers and schemes.</td>
            </tr>
            <tr  style=''>
                <td colspan='3' class='lpadd bold f8'>* Opt for paperless billing. Go Green Go Healthy</td>
            </tr>
            <tr>
                <td colspan='3' class='lpadd bold f10'>Mumbai Jurisdiction</td>
            </tr>
        </table>
        <br>
        &nbsp;
    </div>
    <?php
    $print_page = '';
    $tranqry = '';
    $ftran = '';
//    }
    ?>