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

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdir"];
$zcomp = $_SESSION["comp_" . $dbname];

$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);

$vouch = $_GET['vouchno'];

$cond = '';
$lcond = '';
$formname = "Invoice Print";

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

$getqry = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as mydt   FROM `$maindb` WHERE vouchno='$vouch' ORDER BY `vouchno`")or die(mysqli_error($conn));
$getdata = mysqli_fetch_array($getqry);
$vouchdt = $getdata['vouchdt'] ;

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

$getparty = mysqli_query($conn, "SELECT `accode`,`name` From accmast WHERE `accode` = '$getdata[party]'")or die(mysqli_error($conn));
$showparty = mysqli_fetch_array($getparty);
?>

<!DOCTYPE HTML>
<html lang="en-US">


    <?php
//        include("includes/files.php");
//        include("includes/tabsuf.php");
    ?>
    <style>
        body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            /*background-color: #FAFAFA;*/
            font: 12pt "Tahoma";
        }
        @media print {
            .noprint {
                display: none;
            }
        }
        * {
            box-sizing: border-box;
            -moz-box-sizing: border-box;
        }
        .page {
            width: 210mm;
            min-height: 297mm;
            padding: 4mm;
            margin: 10mm auto;
            border: 1px #D3D3D3 solid;
            border-radius: 5px;
            /*background: white;*/

        }
        .subpage {
            padding: 0cm;
            /*        border: 1px solid;*/
            height: 288mm;
            outline: 2cm #ffffff solid;
        }

        @page {
            size: A4;
            margin: 0;
        }
        @media print {
            html, body {
                width: 210mm;
                height: 297mm;        
            }
            .page {
                margin: 0;
                border: initial;
                border-radius: initial;
                width: initial;
                min-height: initial;
                box-shadow: initial;
                background: initial;
                page-break-after: always;
            }
        }
        .numeric{
            text-align: right;
        }
        .rnone{
            border-right:none; 
        }
        .lnone{
            border-left:none; 
        }
    </style>
</head>

<!--<link href="MyStyle/rep_format.css" rel="stylesheet">-->

<body id="main_container">
    <div class="book">
        <button class="btn btn-primary goBack noprint" onclick="window.close()"  style="float: right;width: 150px;height:35px;background-color: white;color:#A0B1BA;">Go Back</button>
        <div class="page">
            <div style="border:solid 1px;" class="col-md-12" >

                <!--<div class="subpage">-->
                <!--   ************Header************  -->
                <?php //include("includes/head_print.php"); ?>
            </div>
            <div style="border:solid 1px;" class="col-md-12" >
                <h3><center><? Echo "NHC"; ?></center></h3>
                <h5><center><b>Estimate/Approval</b></center></h5>
            </div>
            <div style="border:solid 1px;" class="col-md-12" style="padding-left: 1px ; padding-right: 1px">
                <?php
                mysqli_select_db($conn, $zdb);

                $gcust = mysqli_query($conn, "SELECT * FROM `accmast` where accode = '$getdata[party]' ") or die(mysqli_error($conn));
                $cust = mysqli_fetch_array($gcust);
                $popen = $cust['openbal'];
                $pdc = $cust['dc'];
                if ($pdc == "D") {
                    $mopen = $popen * -1;
                } else {
                    $mopen = $popen;
                }
                
                
                $getbal1 = mysqli_query($conn, "SELECT sum(amount) as tamt FROM `actran` where accode = '$getdata[party]' and date <= '$vouchdt'  ") or die(mysqli_error($conn));
                $fetbal1 = mysqli_fetch_array($getbal1);
                $tamt = $fetbal1['tamt'];
                
                
                $getdt = mysqli_query($conn, "SELECT * FROM `$maindb` where party = '$getdata[party]' and vouchdt < '$vouchdt' order by vouchdt desc limit 0,1 ") or die(mysqli_error($conn));
                if (mysqli_num_rows($getdt) > 0 ) {
                   $fetdt = mysqli_fetch_array($getdt);
                   $tdate = $fetdt['vouchdt'];
                   
                   $getbal = mysqli_query($conn, "SELECT sum(amount) as pamt FROM `actran` where accode = '$getdata[party]' and date <= '$tdate'  ") or die(mysqli_error($conn));
                   $fetbal = mysqli_fetch_array($getbal);
                   $prevamt = $fetbal['pamt'] * -1;
                   
                   $getamt = mysqli_query($conn, "SELECT sum(amount) as nxamt FROM `actran` where accode = '$getdata[party]' and date > '$tdate' and date <= '$vouchdt' and amount > 0 ") or die(mysqli_error($conn));
                   $fetamt = mysqli_fetch_array($getamt);
                   $nxamt  = $fetamt['nxamt'] * -1 ;
                   browse($nxamt);
                } else {
                   $prevamt = 0 ;
                   $nxamt = $tamt ;
                }
                
                $getbal2 = mysqli_query($conn, "SELECT totalamount as tamt2 FROM `gstmain` where autovouch = '$getdata[vouchno]' ") or die(mysqli_error($conn));
                $fetbal2 = mysqli_fetch_array($getbal2);
                $tamt2 = $fetbal2['tamt2'];

                if (abs($tamt) > 0) {
                    $net = $getdata['netamt'];
                } else {
                    $net = 0;
                }
                $gstpick = $getdata['gstpick'];

                $tbal = $mopen + $tamt + $net + $getdata['gstpick'];

                if ($tbal < 0) {
                    $tdc = "D";
                } else {
                    
                } $tdc = "C";
                ?>
                <table class="table table-border table-hover" style="width:100%">
                    <tr>
                        <td style="text-align: left; width:5%"><b>M/s.</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:44%"><?php echo "$showparty[name]"; ?></td>
                        <td style="text-align: right ; width:10%"><b>Voucher</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:39%"><?php echo" $getdata[vouchno]"; ?></td>
                    </tr>
                    <tr>
                        <td style="text-align: left; width:5%"><b>Shop.</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:44%"><?php echo "$cust[add1]"; ?></td>
                        <td style="text-align: right ; width:10%"><b>Date</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:39%"><?php echo "$getdata[mydt]"; ?></td>
                    </tr>
                    <tr>
                        <td style="text-align: left; width:5%"></td>
                        <td style="width:1%"></td>
                        <td style="text-align: left; width:44%"><?php echo "$cust[add2]"; ?></td>
                        <td style="text-align: right ; width:10%"></td>
                        <td style="width:1%"></td>
                        <td style="text-align: left; width:39%"></td>
                    </tr>
                    <tr>
                        <td style="text-align: left; width:5%"></td>
                        <td style="width:1%"></td>
                        <td style="text-align: left; width:44%"><?php echo "$cust[city] - $cust[pin]"; ?></td>
                        <td style="text-align: right ; width:10%">Transporter</td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:39%"><?= $getdata['transname']; ?></td>
                    </tr>
                    <tr>
                        <td style="text-align: left; width:5%"><b>Mobile</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:44%"><?php echo "$cust[mobile]  /  " . $cust['mobile2']; ?></td>
                        <td style="text-align: right ; width:10%;font-weight:  bolder;">Marka</td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:39%"><?php echo $cust['vattin']?></td>
                    </tr>
                </table>
            </div>
            <div>
                <table  border="solid 1px" style="border-collapse: collapse; width:100%">
                    <tr>
                        <th style="width:5%; color:black;"><label>Sr.</label></th>
                        <th style="width:25%; color:black;"><label>Itemname.</label></th>
                        <th style="width:15%; color:black;"><label>Color</label></th>
                        <th style="width:10%; color:black; text-align: right;"><label>Qty.</label></th>
                        <!--<th style="width:10%; color:black; text-align: right;"><label>Mrp</label></th>-->
                        <th style="width:10%; color:black; text-align: right;"><label>Rate</label></th>
                        <th style="width:10%; color:black; text-align: right;"><label>Amount</label></th>
                    </tr>

                    <?php
                    $ctr = 0;
                    $ctab = $totqty = 0;
                    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb`WHERE `vouchno`='$vouch' order by srno")or die(mysqli_error($conn));
                    while ($showtran = mysqli_fetch_array($gettran)) {
                        $getitem = mysqli_query($conn, "SELECT `name`,`code`  From `item` WHERE `code` = $showtran[item] ")or die(mysqli_error($conn));
                        $showitem = mysqli_fetch_array($getitem);

                        $getuint = mysqli_query($conn, "SELECT `name`,`code`  From `color` WHERE `code` = '$showtran[color]' ")or die(mysqli_error($conn));
                        $showunit = mysqli_fetch_array($getuint);


                        Echo"<tr>";
                        Echo"<td style=''>$showtran[srno]</td>";
                        Echo"<td style=''>$showitem[name]</td>";
                        Echo"<td style=''>$showunit[name]</td>";
                        Echo"<td style='text-align: right;'>$showtran[qty]</td>";
//                        Echo"<td style=' text-align:right;'>$showtran[mrp]</td>";
                        Echo"<td style=' text-align:right;'>$showtran[rate]</td>";
                        Echo"<td style=' text-align:right;'>$showtran[amount]</td>";
                        Echo"</tr>";

                        $ctr++;
                        $ctab = $ctab + 1;
                        $totqty = $totqty + $showtran['qty'];
                    }
                    if ($ctab < 19) {
                        for ($i = $ctab; $i <= 19; $i++) {
                            $ctab = $ctab + 1;
                            ?>
                            <tr style="height:26px;">
                                <td class="border"></td>
                                <td class="border"></td>
                                <td class="border"></td>
                                <td class="border"></td>
                                <!--<td class="border"></td>-->
                                <td class="border"></td>
                                <td class="border"></td>
                            </tr>      
                            <?php
                        }
                    }
                    ?>  

                    <tr style="">
                        <td colspan="2" style="border:none" ><? echo "$getdata[remark1]" . " &nbsp;" . "$getdata[remark2]"; ?></td>
                        <td colspan="" style="text-align: right;border:none" > Subtotal  </td>
                        <td style="text-align: right;border:none; padding-right: 5px;"><b> <? echo $totqty; ?>  </b></td>
                        <td style="border:none"></td>
                        <td style="text-align: right;border-left: none;"> <? echo $getdata['subtotal']; ?>  </td>

                    </tr>
                    <?php
                    if ($getdata['litemdisc'] > 0) {
                        ?> 
                        <tr>
                            <td colspan="3" style="text-align: right; " class="lnone rnone"> Less Item Discount  </td>
                            <td style="text-align: right; " class="lnone rnone">   </td>
                            <td class="lnone rnone"></td>
                            <td style="text-align: right; " class="lnone "> <? echo $getdata['litemdisc']; ?>  </td>
                        </tr>
                        <?php
                    }
                    if ($getdata['cdiscamt'] > 0) {
                        ?> 
                        <tr>
                            <td colspan="3" style="text-align: right;" class=" rnone" > Less Other  </td>
                            <td style="text-align: right;" class="lnone rnone"> <? Echo $getdata['cdiscperc']; ?>  </td>
                            <td class="lnone rnone">%</td>
                            <td style="text-align: right; " class="lnone "> <? Echo $getdata['cdiscamt']; ?>  </td>
                        </tr>
                        <?php
                    }

                    if ($getdata['lesother'] > 0) {
                        ?> 
                        <tr>
                            <td colspan="3" style="text-align: right;" class="rnone"> Less Other  </td>
                            <td style="text-align: right; " class="lnone rnone">   </td>
                            <td class="lnone rnone"></td>
                            <td style="text-align: right;" class="lnone "> <? echo $getdata['lesother']; ?>  </td>
                        </tr>
                        <?php
                    }
                    if ($getdata['repairamt'] != 0) {
                        ?> 
                        <tr>
                            <td colspan="3" style="text-align: right;" class="rnone">Repair</td>
                            <td style="text-align: right; " class="lnone rnone">   </td>
                            <td class="lnone rnone"></td>
                            <td style="text-align: right;" class="lnone "> <? echo $getdata['repairamt']; ?>  </td>
                        </tr>
                        <?php
                    }

                    if ($getdata['vatamt'] > 0) {
                        ?> 
                        <tr>
                            <td colspan="3" style="text-align: right;" class=" rnone"> Add: V.A.T  </td>
                            <td style="text-align: right;" class="lnone rnone"> <? Echo $getdata['vatperc']; ?>  </td>
                            <td class="lnone rnone">%</td>
                            <td style="text-align: right;" class="lnone "> <? Echo $getdata['vatamt']; ?>  </td>
                        </tr>
                        <?php
                    }
                    
                    if ($getdata['packing'] > 0) {
                        ?> 
                        <tr>
                            <td colspan="3" style="text-align: right; " class=" rnone"> Add: Packing  </td>
                            <td style="text-align: right;"class="lnone rnone">   </td>
                            <td class="lnone rnone"></td>
                            <td style="text-align: right; "class="lnone "> <? Echo $getdata['packing']; ?>  </td>
                        </tr>
                        <?php
                    }
                    if ($getdata['packamt'] > 0) {
                        ?> 
                        <tr>
                            <td colspan="3" style="text-align: right; "class=" rnone">Add Packing <? Echo $getdata['packkg']; ?></td>
                            <td style="text-align: right; "class="lnone rnone">Rate&nbsp;<? Echo $getdata['packrate']; ?></td>
                            <td style="text-align: center;"class="lnone rnone "></td>
                            <td style="text-align: right; "class="lnone "><? Echo $getdata['packamt']; ?></td>
                        </tr>

                        <?php
                    }
                    if ($getdata['paidamt'] > 0) {
                        ?> 
                        <tr>
                            <td colspan="3" style="text-align: right; "class=" rnone">Add Paid</td>
                            <td style="text-align: right; "class="lnone rnone">Rate&nbsp;<? Echo $getdata['paidrate']; ?></td>
                            <td style="text-align: center;"class="lnone rnone "></td>
                            <td style="text-align: right; "class="lnone "><? Echo $getdata['paidamt']; ?></td>
                        </tr>
                        <?php
                    }
                    if ($gstpick > 0) {
                        ?> 
                        <tr>
                            <td colspan="3" style="text-align: right; "class=" rnone"><b>Add GST Amount</b></td>
                            <td style="text-align: right; "class="lnone rnone"><b></b></td>
                            <td class="lnone rnone"></td>
                            <td style="text-align: right; "class="lnone "><b><? Echo $gstpick; ?></b></td>
                        </tr>
                        <?php
                    }

                    if ($getdata['netamt'] > 0) {
                        $netamt = $getdata['netamt'] + $gstpick ;
                        ?> 
                        <tr>
                            <td colspan="3" style="text-align: right; "class=" rnone"><b>Total Amount</b></td>
                            <td style="text-align: right; "class="lnone rnone"><b></b></td>
                            <td class="lnone rnone"></td>
                            <td style="text-align: right; "class="lnone "><b><? Echo number_format($netamt, 2); ?></b></td>
                        </tr>
                        <?php
                    }
                    if ($getdata['ladvance'] > 0) {
                        ?> 
                        <tr>
                            <td colspan="3" style="text-align: right; "class=" rnone">Less Advance</td>
                            <td style="text-align: right; "class="lnone rnone"></td>
                            <td class="lnone rnone"></td>
                            <td style="text-align: right; "class="lnone "><? Echo $getdata['ladvance']; ?></td>
                        </tr>
                        <?php
                    }
                    ?>

                    <tr>
                        <td colspan="8">
                            Rupees 
                            <?php echo rsword(round($getdata['netamt'])); ?>
                        </td>

                    </tr>

                    <?php
                    $balamt = $netamt;
                    if ($tbal != 0) {
                        $abal = $tbal * -1;
                        $balamt = $netamt + $abal;
                        
                        ?> 
                        <tr>
                            <td colspan="3" style="text-align: right; "class=" rnone">Previous Balance</td>
                            <td style="text-align: right; "class="lnone rnone"></td>
                            <td class="lnone rnone"></td>
                            <td style="text-align: right; "class="lnone "><? Echo number_format($prevamt, 2); ?></td>
                        </tr>
                        <tr>
                            <td colspan="3" style="text-align: right; "class=" rnone">Payment Recd.</td>
                            <td style="text-align: right; "class="lnone rnone"></td>
                            <td class="lnone rnone"></td>
                            <td style="text-align: right; "class="lnone "><? Echo number_format($nxamt, 2); ?></td>
                        </tr>
                        
                    <?php }
                    ?>
                    <tr>
                        <td colspan="3" style="text-align: right; "class=" rnone">Total Balance</td>
                        <td style="text-align: right; "class="lnone rnone"></td>
                        <td class="lnone rnone"></td>
                        <td style="text-align: right; "class="lnone "><? Echo number_format($balamt, 2); ?></td>
                    </tr>
                </table>
            </div>

            <div>
                <table class="border">
                    <tr>
                        <td class="border" style="width: 541px; font-size: 14px;">
                            <!--<img src="images/taximg.png">-->
                        </td>
                        <td class="border" style="width: 216px">
                            <div style=" text-align:right; font-size: 14px; font-weight: 900"><? echo "For NHC"; ?></div>
                            <br>
                            <br>
                            <br>
                            <div style=" margin-left: 51px; font-size: 11px">AUTHORISED SIGNATORY</div>
                        </td>
                    </tr>
                </table>
            </div>
        </div>    
        <!--</div>-->
    </div>
    <?php
    include("includes/linksfoot.php");
    ?>
    <script>
        $(function () {
            formGeneral();
        });
    </script>
</body>
</html>
