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

$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 Voucher";

$maindb = 'purmain';
$trandb = 'purtran';

$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));
$getmdata = mysqli_fetch_array($getqry);


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

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


    <?php
//        include("includes/files.php");
//        include("includes/tabsuf.php");
    ?>
    <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: 290mm;
            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: 290mm;        
            }
            .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 class="subpage">-->
            <!--   ************Header************  -->
            <div style="border:solid 1px;" class="col-md-12" >
                <h3><center><? Echo $getcmpname['name']; ?></center></h3>
                <center><?php echo $getcmpname['add1'] . " " . $getcmpname['add2'] . " " . $getcmpname['add3']; ?></center>
                <center><?php echo "Email :" . $getcmpname['email'] . " &nbsp " . "Contact No :" . $getcmpname['tel'] . "/" . $getcmpname['mobile']; ?></center>
                <h5><center><b><?php echo $formname; ?></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`=$getmdata[party]") or die(mysqli_error($conn));
                $cust = mysqli_fetch_array($gcust);
                ?>

                <table class="table table-border table-hover" style="width:100%">
                    <tr>
                        <td style="text-align: left;">
                            <b>M/s.</b><font size="3px" weight="bold"><?php echo "$cust[name]"; ?></font>
                        </td>
                        <td style="text-align: center;">
                            <b>Voucher :</b>
                            <?php echo "$getmdata[vouchno]"; ?> 
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: left;"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><?php echo "$cust[add1]"; ?></td>
                        <td style="padding-left: 74px;text-align: center;">
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b style="text-align:right;">Date :</b>
                            <?php echo "$getmdata[vouchdt]"; ?> 
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: left;"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b><?php echo "$cust[add2] $cust[add3]"; ?></td>
                        <td style="text-align: center;">
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: left;"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b>GST NO. <?php echo "$cust[gstno] "; ?></td>
                        <td style="text-align: center;">
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: left;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Mobile :- <?php echo "$cust[mobile]"; ?></td>
                    </tr>
                </table>
            </div>
            <div>
                <table  border="solid 1px" style="border-collapse: collapse; width:100%">
                    <tr>
                        <th style="width:10%">Sr.</th>
                        <th style="width:30%">Itemname</th>
                        <th style="width:10%">Hsn Code</th>
                        <th class="numeric" style="width:10%">Qty.</th>
                        <th style="width:10%">Unit</th>  
                        <th class="numeric" style="width:10%">Rate</th>
                        <th class="numeric" style="width:17%">Amount</th>
                    </tr>

                    <?php
                    $tranpart = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`=$getmdata[vouchno] order by srno") or die(mysqli_error($conn));
                    $mht = '20px';
                    $ctab = 0;

                    while ($row = mysqli_fetch_array($tranpart)) {
                        $getitem = mysqli_query($conn, "SELECT code,name FROM `item` WHERE `code`=$row[itemcode]") or die(mysqli_error($conn));
                        $gtit = mysqli_fetch_array($getitem);
                        ?>
                        <tr class="" style="height: 27px;">
                            <td class="" style="text-align: center;"><?php echo "$row[srno]"; ?></td>
                            <td><? Echo $gtit[name]; ?></td>
                            <td class="numeric"><? Echo $row[hsncode]; ?></td>
                            <td class="numeric"><? Echo $row[qty]; ?></td>
                            <td><? Echo $row[unit]; ?></td>
                            <td class="numeric"><? Echo $row[rate]; ?></td>
                            <td class="numeric"><? Echo $row[amount]; ?></td>
                        </tr>
                        <?
                        $ctab = $ctab + 1;
                        $totqty = $row[qty] + $totqty;
                    }
                    if ($ctab < 20) {
                        for ($i = $ctab; $i <= 20; $i++) {
                            $ctab = $ctab + 1;
                            ?>
                                                                            <!--<tr style="height:25px;"></tr>-->
                            <tr style="height: 25px;">
                                <td></td> 
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>      
                            <?php
                        }
                    }
                    ?>  
                    <!--                </table>
                                    <table class="" style="border: 1px solid black;" width="100%">-->
                    <tr style="">
                        <td colspan="3" style="border:none" ><? echo "$getmdata[remark1] $getmdata[remark2]"; ?></td>
                        <td colspan="" style="text-align: right;border:none" > Subtotal  </td>
                        <td style="text-align: right;border:none "> <? // echo $totqty; ?>  </td>
                        <td style="border:none"></td>
                        <td style="text-align: right;border-left: none;"> <? echo $getmdata[subtotal]; ?>  </td>

                    </tr>
                    <?php if ($getmdata[litemdisc] > 0) { ?> 
                        <tr>
                            <td colspan="4" 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 $getmdata[litemdisc]; ?>  </td>
                        </tr>
                        <?php
                    }

                    if ($getmdata[cdiscamt] > 0) {
                        ?> 
                        <tr>
                            <td colspan="4" style="text-align: right;" class=" rnone" > Less Cash Discount  </td>
                            <td style="text-align: right;" class="lnone rnone"> <? Echo $getmdata[cdiscperc]; ?>  </td>
                            <td class="lnone rnone">%</td>
                            <td style="text-align: right; " class="lnone "> <? Echo $getmdata[cdiscamt]; ?>  </td>
                        </tr>
                        <?php
                    }

                    if ($getmdata[lesother] > 0) {
                        ?> 
                        <tr>
                            <td colspan="4" style="text-align: right;" class="rnone"> Less Discount  </td>
                            <td style="text-align: right; " class="lnone rnone">   </td>
                            <td class="lnone rnone"></td>
                            <td style="text-align: right;" class="lnone "> <? echo $getmdata[lesother]; ?>  </td>
                        </tr>
                        <?php
                    }

                    if ($getmdata[tottaxamt] > 0) {
                        ?> 
                        <tr>
                            <td colspan="4" style="text-align: right;" class=" rnone"> Taxable Value  </td>
                            <td style="text-align: right;" class="lnone rnone">  </td>
                            <td class="lnone rnone"></td>
                            <td style="text-align: right;" class="lnone "> <? Echo $getmdata[tottaxamt]; ?>  </td>
                        </tr>
                        <?php
                    }
                    if ($getmdata[packing] > 0) {
                        ?> 
                        <tr>
                            <td colspan="4" 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 $getmdata[packing]; ?>  </td>
                        </tr>
                        <?php
                    }

                    if ($getmdata[totcgst] > 0) {
                        ?> 
                        <tr>
                            <td colspan="4" style="text-align: right;" class=" rnone" > Add CGST  </td>
                            <td style="text-align: right;" class="lnone rnone"> <? Echo $getmdata[cgstperc]; ?>  </td>
                            <td class="lnone rnone">%</td>
                            <td style="text-align: right; " class="lnone "> <? Echo $getmdata[totcgst]; ?>  </td>
                        </tr>
                        <?php
                    }
                    if ($getmdata[totsgst] > 0) {
                        ?> 
                        <tr>
                            <td colspan="4" style="text-align: right;" class=" rnone" > Add SGST  </td>
                            <td style="text-align: right;" class="lnone rnone"> <? Echo $getmdata[sgstperc]; ?>  </td>
                            <td class="lnone rnone">%</td>
                            <td style="text-align: right; " class="lnone "> <? Echo $getmdata[totsgst]; ?>  </td>
                        </tr>
                        <?php
                    }
                    if ($getmdata[totigst] > 0) {
                        ?> 
                        <tr>
                            <td colspan="4" style="text-align: right;" class=" rnone" > Add IGST  </td>
                            <td style="text-align: right;" class="lnone rnone"> <? Echo $getmdata[igstperc]; ?>  </td>
                            <td class="lnone rnone">%</td>
                            <td style="text-align: right; " class="lnone "> <? Echo $getmdata[totigst]; ?>  </td>
                        </tr>
                        <?php
                    }
                    if ($getmdata[netamt] > 0) {
                        ?> 
                        <tr>
                            <td colspan="3" style="text-align:  "class=" rnone">GST No:<?php Echo $getcmpname['gst']?></td>
                            <td colspan="2" style="text-align:  "class=" rnone"><b>Total Amount</b></td>
                            <td style="text-align: right; "class="lnone rnone"><b></b></td>
                            <td style="text-align: right; "class="lnone "><b><? Echo $getmdata[netamt]; ?></b></td>
                        </tr>
                    <?php } ?>
                    <tr>
                        <td colspan="9">
                            Rupees 
                            <?php echo rsword(round($getmdata[netamt])); ?>
                        </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">
                            <?php
                            require("includes/dbconnect.php");
                            ?>
                            <div style=" text-align:right; font-size: 14px; font-weight: 900"><? echo "For $getcmpname[name]"; ?></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>
