<?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 = "Purchase-Oil";

$maindb = 'purmain';
$trandb = 'purtran';
$accdb = 'actran';
$stkdb = 'transacn';

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

$depa = mysqli_query($conn, "SELECT `code`,`name`,`dptcode` FROM `dept` WHERE `dptcode`= '$getdata[dept]'") or die(mysqli_error($conn));
$getdept = mysqli_fetch_array($depa);

$cmpname = mysqli_query($conn, "SELECT * From $zroot.`company` where `code`='1'")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">


    <style>
        @media print {
            .noprint {
                display: none;
            }
        }
        body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            /*background-color: #FAFAFA;*/
            font: 12pt "Tahoma";
        }
        * {
            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; 
        }
        .text-right{
            text-align:right;
        }
        .text-center{
            text-align:center;
        }
    </style>
    <title>PUMP WEB</title>
</head>


<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: #26A69A ;color:white;">Go Back</button>
        <div class="page">
            <!--<div class="subpage">-->
            <!--   ************Header************  -->
            <div style="border:solid 1px;" class="col-md-12" >
                <h2 style="text-align:center"><? Echo $getcmpname['compname']; ?></h2>
                <?php // echo $getcmpname['hoadd1'] . " " . $getcmpname['hoadd2'] . " " . $getcmpname['hoadd3']; ?>
                <?php if ($getcmpname['htel'] != '') { ?>
                    <h4 style="text-align:center"> <?php echo "Contact No :" . $getcmpname['htel']; ?></h4>
                <?php } ?>
                <!--<h5><center><b><?php // echo "GST No:" . " " . $getcmpname['gstno'];   ?></b></center></h5>-->
            </div>
            <div style="border:solid 1px;" class="col-md-12" style="padding-left: 1px ; padding-right: 1px">
                <h3 style="text-align:center"><b><?php echo $formname; ?></b></h3>
                <?php
                $gcust = mysqli_query($conn, "SELECT * FROM `accmast` where accode = '$getdata[party]' ") or die(mysqli_error($conn));
                $cust = mysqli_fetch_array($gcust);
                ?>
                <table class="table table-border table-hover" style="width:100%">
                    <tbody>
                        <tr>
                            <td style="text-align: left; width:5%"><b>No.</b></td>
                            <td style="width:1%">:</td>
                            <td style="text-align: left; width:58%"><?php echo $getdata['vouchno']; ?></td>
                            <td style="text-align: right ; width:20%"><b>Date</b></td>
                            <td style="width:1%">:</td>
                            <td style="text-align: left; width:15%"><?php echo $getdata['vouchdt']; ?></td>
                        </tr>
                        <tr>
                            <td style="text-align: left; width:5%"><b>Name</b></td>
                            <td style="width:1%">:</td>
                            <td style="text-align: left; width:58%"><?php echo $showparty['name']; ?></td>
                            <td style="text-align: right ; width:20%"><b></b></td>
                            <td style="width:1%"></td>
                            <td style="text-align: left; width:15%; margin-right: 15px;"></td>
                        </tr>
                        <tr>
                            <td style="text-align: left; width:5%"><b>Address</b></td>
                            <td style="width:1%">:</td>
                            <td style="text-align: left; width:58%"><?php echo $cust['add1'] . $cust['add2']; ?></td>
                            <td style="text-align: right ; width:20%"><b></b></td>
                            <td style="width:1%"></td>
                            <td style="text-align: left; width:15%"><?php // echo $getdata['slipno']  ?></td>
                        </tr>
                        <?php if ($cust['add3'] != '') { ?>
                            <tr>
                                <td style="text-align: left; width:5"><b></b></td>
                                <td style="width:1%">:</td>
                                <td style="text-align: left; width:58%"><?php echo $cust['add3']; ?></td>
                                <td style="text-align: right ; width:20%"><b></b></td>
                                <td style="width:1%"></td>
                                <td style="text-align: left; width:15%"><?php // echo $getdata['shift']  ?></td>
                            </tr>
                        <?php } ?>
                        <!--<tr>
                            <td style="text-align: left; width:5%"><b>Vehicle No: </b></td>
                            <td style="width:1%">:</td>
                            <td style="text-align: left; width:10%"><?php echo $getdata['vehicleno']; ?></td>
                            <td style="text-align: right ; width:30%"></td>
                            <td style="width:1%"></td>
                            <td style="text-align: left; width:40%"></td>
                        </tr>-->
                    </tbody>
                </table>
            </div>
            <div>
                <table  border="solid 1px" style="border-collapse: collapse; width:100%">
                    <tr style="background-color: #4EB3C7">
                        <th style="width:3%; color:black;"><label>Sr.</label></th>
                        <th style="width:47%; color:black;"><label>Product Name</label></th>
                        <th style="width:11%; color:black; text-align: right;"><label>HSN Code</label></th>
                        <!--<th style="width:12%; color:black; text-align: left;"><label>Free Qty.</label></th>-->
                        <th style="width:9%; color:black;text-align: right;"><label>Qty.</label></th>
                        <th style="width:9%; 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 = $totamt = 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)) {

                        $gitemname = mysqli_query($conn, "SELECT `code`,`name` FROM `product` WHERE `code`='$showtran[item]'")or die(mysqli_error($conn));
                        $fitemname = mysqli_fetch_array($gitemname);

                        $item = $fitemname["name"];
                        $hsncode = $showtran['hsncode'];
                        $qty = $showtran['qty'];
                        $rate = $showtran['rate'];
                        $amount = $showtran['total'];

                        $ctr++;

                        Echo"<tr>";
                        Echo"<td style='text-align:center'>$ctr</td>";
                        Echo"<td style=''>$item</td>";
                        Echo"<td style='text-align:'>$hsncode</td>";
                        Echo"<td style=' text-align: right;'>$qty</td>";
                        Echo"<td style=' text-align:right;'>$rate</td>";
                        Echo"<td style=' text-align:right;'>$amount</td>";
                        Echo"</tr>";

                        $totqty = $totqty + $qty;
                        $totamt = $totamt + $amount;
                        $ctab = $ctab + 1;
                    }
                    if ($ctab < 12) {
                        for ($i = $ctab; $i <= 12; $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
                        }
                    }


                    $ptotqty = number_format($totqty, 2);
                    $ptotamt = number_format($totamt, 2);
                    ?>  

                    <tr style="height:26px;">
                        <td class="text-right border"  colspan="3"><b>Total</b></td>
                        <td class="text-right border"><b><?php echo $ptotqty; ?></b></td>
                        <td class="text-right border"><?php //  echo ;    ?></td>
                        <td class="text-right border"><b><?php echo $ptotamt; ?></b></td>
                    </tr>
                    <? if ($getdata['discondies'] > 0) { ?>
                        <tr style="height:26px;">
                            <td class="text-right border"  colspan="5">Discount on diesel</td>
                            <td class="text-right border"><?php echo $getdata['discondies']; ?></td>
                        </tr>
                    <? } ?>
                    <? if ($getdata['tcsamt']) { ?>
                        <tr style="height:26px;">
                            <td class="text-right border"  colspan="4">TCS</td>
                            <td class="text-right border"  colspan=""><?php echo $getdata['tcsperc']; ?>%</td>
                            <td class="text-right border"><?php echo $getdata['tcsamt']; ?></td>
                        </tr>
                    <? } ?>

                    <tr style="background-color: #4EB3C7">
                        <td  style="text-align: right;" class="" colspan="2"><b></b></td>
                        <td  style="text-align: right;" class=""><b>5%</b></td>
                        <td  style="text-align: right;" class=""><b>12%</b></td>
                        <td  style="text-align: right;" class=""><b>18%</b></td>
                        <td  style="text-align: right;" class=""><b>28%</b></td>
                    </tr>
                    <?php if ($getdata[oms] != 1) { ?>                  
                        <tr >
                            <td  style="text-align: right;" class="" colspan="2"><b>CGST</b></td>
                            <td  style="text-align: right;" class=""><b><?php echo $getdata['cgst5'] ?></b></td>
                            <td  style="text-align: right;" class=""><b><?php echo $getdata['cgst12'] ?></b></td>
                            <td  style="text-align: right;" class=""><b><?php echo $getdata['cgst18'] ?></b></td>
                            <td  style="text-align: right;" class=""><b><?php echo $getdata['cgst28'] ?></b></td>
                        </tr>
                        <tr>
                            <td  style="text-align: right;" class="" colspan="2"><b>SGST</b></td>
                            <td  style="text-align: right;" class=""><b><?php echo $getdata['sgst5'] ?></b></td>
                            <td  style="text-align: right;" class=""><b><?php echo $getdata['sgst12'] ?></b></td>
                            <td  style="text-align: right;" class=""><b><?php echo $getdata['sgst18'] ?></b></td>
                            <td  style="text-align: right;" class=""><b><?php echo $getdata['sgst28'] ?></b></td>
                        </tr>
                    <?php } ?>
                    <?php if ($getdata[oms] = 1) { ?>
                        <tr>
                            <td  style="text-align: right;" class="" colspan="2"><b>IGST</b></td>
                            <td  style="text-align: right;" class=""><b><?php echo $getdata['igst5'] ?></b></td>
                            <td  style="text-align: right;" class=""><b><?php echo $getdata['igst12'] ?></b></td>
                            <td  style="text-align: right;" class=""><b><?php echo $getdata['igst18'] ?></b></td>
                            <td  style="text-align: right;" class=""><b><?php echo $getdata['igst28'] ?></b></td>
                        </tr>
                    <?php } ?>
<?php 
$tot5 = $getdata[cgst5] + $getdata[sgst5] + $getdata[igst5];
$tot12 = $getdata[cgst12] + $getdata[sgst12] + $getdata[igst12];
$tot18 = $getdata[cgst18] + $getdata[sgst18] + $getdata[igst18];
$tot28 = $getdata[cgst28] + $getdata[sgst28] + $getdata[igst28];
?>
                    <tr>
                        <td  style="text-align: right;" class="" colspan="2"><b>Total</b></td>
                        <td  style="text-align: right;" class=""><b><?php echo $tot5 ?></b></td>
                        <td  style="text-align: right;" class=""><b><?php echo $tot12 ?></b></td>
                        <td  style="text-align: right;" class=""><b><?php echo $tot18 ?></b></td>
                        <td  style="text-align: right;" class=""><b><?php echo $tot28 ?></b></td>
                    </tr>

                    <tr>
                        <td colspan="5" style="text-align: right; "class=""><b>Bill Amount</b></td>
                        <td style="text-align: right; "class="lnone" colspan="2"><b><? Echo $getdata['totalamount']; ?></b></td>
                    </tr>

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

                    </tr>
                </table>
            </div>

            <div>
                <table class="border">
                    <tr>
                        <td class="border" style="width: 541px; font-size: 14px;">
                        </td>
                        <td class="border" style="width: 216px">
                            <?php
                            require("includes/dbconnect.php");
                            ?>
                            <div style=" text-align:right; font-size: 14px; font-weight: 900"><? echo "For $getcmpname[compname]"; ?></div>
                            <br>
                            <br>
                            <br>
                            <div style=" margin-left: 86px; font-size: 11px">Mechanical Manager</div>
                        </td>
                    </tr>
                </table>
            </div>
        </div>    
    </div>
    <?php
    include("includes/linksfoot.php");
    ?>
    <script>
        $(function () {
            formGeneral();
        });
    </script>
</body>
</html>
