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

//$comp = mysqli_query($conn, "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);



$keycond = "";

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

$zroot = $_SESSION["zdbroot"];
$formname = 'Invoice Print';
$root = $_SESSION['zdir'];
$vouchno = $_GET['vouchno'];
$para = $_GET["para"];
if ($para != '') {
    $fname = 'Sale Order';
    $lbbill = 'Order No.';
    $maindb = 'sordmain';
    $trandb = 'sordtran';
} else {
    $fname = 'Order Estimate';
    $lbbill = 'Bill No.';

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

##############################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 srno") or die(mysqli_error($conn));
$fmain = mysqli_fetch_object($mainqry);


$billno = $fmain->vouchno . '-' . $fmain->invno;

if ($fmain->cashchk == 1) {
    $paytype = "CASH";

    if ($fmain->cardchk == 1) {
        $paytype = $paytype + " / CARD";
    }
} else {
    if ($fmain->cardchk == 1) {
        $paytype = "CARD";
    }
    if ($fmain->cashchk == 1) {
        $paytype = "CASH";
    }
    if ($fmain->upichk == 1) {
        $getupn = mysqli_query($conn, "SELECT name FROM paytype WHERE code='$fmain->upiname'") or die(mysqli_error($conn));
        $fetupn = mysqli_fetch_object($getupn);
        $paytype = "UPI - " . $fetupn->name;
    }
    if ($fmain->credchk == 1) {
        $paytype = "CREDIT";
    }
}

$zcomp = $_SESSION["comp_" . $dbname];

$cmpname = mysqli_query($conn, "SELECT * From $zroot.`company` where `code`='$zcomp'")or die(mysqli_error($conn));
$fcomp = mysqli_fetch_object($cmpname);

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

//for ($i = 0; $i <= 1; $i++) {
//$tranqry = mysqli_query($conn,  "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($conn));

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->name; ?></td></tr>
            <tr><td class="text-center bold f12" colspan="3"><?= $fcomp->add1 . $fcomp->add2 . $fcomp->add3; ?></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'><?= $fname ?></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"><?= $lbbill ?></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 class="lpadd f11 text-right">Mobile </td>
                <td class="bold f11">: <?= $fmain->printmobile; ?></td>

            </tr>-->
            <?
            $prname = '';
            if ($fmain->nameprint != "") {
                $prname = $fmain->nameprint;
            } else {
                $getname = mysqli_query($conn, "SELECT accode,name,concat(add1,add2,add3) as address FROM accmast WHERE accode='$fmain->party'") or die(mysqli_error($conn));
                $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>
            <?php
            if ($fmain->address) {
                ?>
                <tr>
                    <td class="lpadd f13" colspan=''>Address  </td>
                    <td class="lpadd f13" colspan='4'><?= $fmain->add1 .  $fmain->add2 . $fmain->add3 ?></td>
                </tr>
            <?php } ?>

        </table>
        <!--</div>-->
        <table>
            <tr style='border-top: 1px solid black;border-bottom: 1px solid black'>
                <td style='width:5%' class='bold lpadd f12'>Sr.</td>
                <td style='width:40%' class='bold lpadd f12'>Item</td>
                <td style='width:9%' class='bold text-center lpadd f12'>Qty.</td>
                <!--<td style='width:9%' class='bold text-center lpadd f13'>Unit</td>-->
                <td style='width:11%' class='bold lpadd f12'>Rate</td>
                <td style='width:15%' class='bold text-right lpadd f12'>Amt.</td>
            </tr>
            <?php
            $tsub = '';
            $a = 0;
            $ctr = 0;
            $tqty = 0;
            $tamt = 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($conn, "SELECT `code`,`name` FROM `item` WHERE `code` = '$ftran->item'") or die(mysqli_error($conn));
                $fitem = mysqli_fetch_array($itemqry);
                ?>
                <tr style="">
                    <td class='lpadd f12'><?= $ctr; ?></td>
                    <td class='lpadd f12' ><?= $fitem['code'] .  ' - '  .$fitem['name']; ?></td>
<!--                </tr>   
                <tr style="">-->
                    <!--<td class='lpadd' colspan="1"></td>-->
                    <!-- <?= number_format($gstp, 2); ?>% -->
                    <!--<td class='text-center f10 lpadd'></td>-->
                    <td class='text-center f12 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-center f12 bold'><?= $ftran->rate; ?></td>
                    <?php if ($ftran->discount > 0) { ?>
                        <td  class='text-right f12 bold'><?= $ftran->amount; ?></td>

                        <?php
                        $tamt = $tamt + $ftran->total;
                    } else {
                        ?>
                        <td  class='text-right f10 bold'><?= $ftran->amount; ?></td>
                        <?php
                        $tamt = $tamt + $ftran->total;
                    }
                    ?>

                </tr>   

                <? if ($ftran->discount > 0 || $ftran->freeqty > 0) { ?>
                    <tr style="">
                        <td class='f11 text-right' colspan="2"><i>Free Qty.</i> </td>
                        <td class='f11 text-right' colspan=""><?= $ftran->freeqty; ?></i></td>

                        <td class='text-right f11 rpadd'><i>Discount</i></td>
                        <td  class='f11 text-right'><i><?= $ftran->discperc; ?>%</i></td>
                        <td  class='text-right f11'><i>-<?= $ftran->discount; ?></i></td>
                    </tr> 
                <? } ?>
                <?
                $tqty = $tqty + $ftran->qty;
                // browse($ftran->sellqty);
            }
            ?>

        </table>
        <table>
            <tr style='border-top: 2px solid black'>
                <td colspan='' class='text-right  f13 rpadd ' style="width:43%">Total Qty.&nbsp;&nbsp;<span class="bold"> <?= $tqty ?> </span></td>
                <td colspan='' class='text-center f14'>Sub Total <i class="fa fa-inr"></i></td>
                <td colspan='' class='text-right bold f12 bold'><?= round($fmain->subtotal); ?></td>
                <? // if ($fmain->litemdisc > 0) { ?>
<!--                    <td colspan='' class='text-right f13 rpadd' style="width:40%">Sub Total</td>
                    <td colspan='' class='text-right f14 ' style="width:28%" ><?= round($tamt); ?></td>-->
                <? // } else { ?>
<!--                    <td colspan='' class='text-right  f13 rpadd ' style="width:40%">&nbsp;&nbsp;</td>
                    <td colspan='' class='text-right f14 ' style="width:28%" >&nbsp;</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 f14'>Bill Amt. <i class="fa fa-inr"></i></td>
                <td colspan='' class='text-right bold f17 bold'><?= round($fmain->netamt); ?></td>
            </tr>-->

            <tr>
                <td colspan='3' class='lpadd bold text-right f12 bold'><span style='padding-right: 50px;'>Packing  </span><?= round($fmain->packamt); ?></td>
            </tr>
            <tr>
                <td colspan='3' class='lpadd bold text-right f12 bold'><span style='padding-right: 65px;'>Transport </span>  <?= round($fmain->paidamt); ?></td>
            </tr>
            <tr>
                <td colspan='3' class='lpadd bold text-right f12 bold'><span style='padding-right: 30px;'>Bill Amount</span>  <?= round($fmain->netamt); ?></td>
            </tr>
            <tr   style='border-top: 2px solid black;'>
                <td colspan='3' class='lpadd bold text-right f12 bold'>Rupees<?= rsword($fmain->netamt); ?></td>
            </tr>
            <?php if ($para == '') { ?>
<!--                <tr  style='border-top: 2px solid black;'>
                    <td colspan='' class='lpadd bold'><label class="f10">Paid By <?php echo $paytype; ?></label></td>
                    <td colspan='' class='lpadd bold text-right'><label class="f10">(Inclusive Of Taxes)</label></td>
                </tr>-->
            <?php } ?>

            
        </table>
        <br>
        &nbsp;
    </div>
    <?php
    $print_page = '';
    $tranqry = '';
    $ftran = '';
//    }
    ?>