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

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

$vouch = $_GET['vouchno'];
$type = $_GET['type'];

$cond = '';
$lcond = '';
$formname = "TAX INVOICE";
$maindb = 'salemain';
$trandb = 'saletran';
$root = $_SESSION["zdbroot"];

$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));

$getcomp2 = mysqli_query($conn, "SELECT * FROM $root.`company`")or die(mysqli_error($conn));
$comp2 = mysqli_fetch_array($getcomp2);



if (mysqli_num_rows($getqry) <= 0) {
    browse("Record Not Found");
}
include("includes/linkshead.php");
?>
<link href="styles/inv_format.css" rel="stylesheet">
<style>
    body{
        font-size: 13px;
    }
    .table > tbody > tr > td{
        padding: 0px;
    }

    .bor{
        border-left: 1px solid black;
        border-right:  1px solid black;

    }

    .numeric{
        text-align: right

    }
    
    .fontnew{
        font-family: 'Verdana';
        font-size : 11px;
        font : 'bold';

    }

</style>
</head>
<body onload="">

    <?php
    $tdhg = "24px";
    $qry = mysqli_fetch_array($getqry);
    $saltype = $qry[paidby];
    $oms = $qry[oms];
    browse($oms);
    $gstslform = $qry[gstno];
    $mobile = $qry[mobile];
    if ($saltype == "C") {
        $nametype = "Cash";
        $codetype = "C";
    } else {
        $nametype = "Credit";
        $codetype = "R";
    }

    $stafcode = $qry[staff];
    $getstaf = mysqli_query($conn, "SELECT accode,name FROM `accmast` where accode = '$stafcode' ")or die(mysqli_error($conn));
    $staff = mysqli_fetch_array($getstaf);
    ?>
    <div class="page">
        <div class="subpage" style="padding-top: 16px">
            <div class="border" style="border-bottom: none">
                <table style="width:100%;height: 7%">
                    <tr>
                        <td style="width:25%">
                            <table style="width:100%">
                                <tr>
                                    <td><img src="images/logo/khlogo.jpg" style="width: 100px;height: 60px"></td> 
                                </tr>
                            </table>
                        </td>
                        <td style="width:50%">
                            <table style="width:100%">
                                <tr>
                                    <td style="text-align: center;width: 100%;font-size: 35px;font-family: 'Times New Roman';"><b><? Echo $comp2[compname] ?></b></td> 

                                </tr>
                            </table> 
                        </td>
                        <td style="width:25%">
                            <table style="width:100%">
                                <tr>
                                    <td style="text-align: right"><img src="images/logo/KH150.jpg" style="width: 70px;height: 60px"></td> 
                                </tr>
                            </table> 
                        </td>
                    </tr>
                </table>
                <table style="width:100%">
                    <tr>
                        <td style="width:12%">
                            <b>
                                Head Office :
                            </b>
                        </td>

                        <td style="font-size: 12px;">
                            <?php echo $comp2[hoadd1] . ' ' . $comp2[hoadd2] . ' ' . $comp2[hoadd3]; ?>   
                        </td>
                    </tr>
                </table>
                <table style="width:100%">
                    <tr>
                        <td style="width:12%">
                            <b>
                                Sales :
                            </b>
                        </td>

                        <td style="font-size: 12px;">
                            <?php echo $comp2[sadd1] . ' ' . $comp2[sadd2] . ' ' . $comp2[sadd3]; ?>   
                        </td>
                    </tr>
                </table>
                <table style="width:100%">
                    <tr>
                        <td style="width:12%">
                            <b>
                                Factory :
                            </b>
                        </td>

                        <td style="font-size: 12px;">
                            <?php echo $comp2[fadd1] . ' ' . $comp2[fadd2] . ' ' . $comp2[fadd3]; ?>   
                        </td>
                    </tr>
                </table>
                <br>
                <table style="width:100%">
                    <tr>
                        <td style="width:10%;text-align: right;font-size: 15px;">
                            <b>
                                GST NO :&nbsp;
                            </b>
                        </td>

                        <td style="width:30%;font-size: 15px;">
                            <b>
                                <?php echo $comp2[gstno] ?>   
                            </b>
                        </td>
                        <td style="width:60%;font-size: 15px;">
                            <b>
                                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tax Invoice   
                            </b>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="border">
                <div class="row">
                    <?
                    $getpartyname = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$qry[party]' AND `grpcode`='010'")or die(mysqli_error($conn));
                    $mpartyname = mysqli_fetch_array($getpartyname);
                    $partyname = $mpartyname[name];
                    $add1 = $mpartyname[add1];
                    $add2 = $mpartyname[add2];
                    $add3 = $mpartyname[add3];
                    $city = $mpartyname[city];
                    $pin = $mpartyname[pin];
                    $panno = $mpartyname[vattin];
                    $gstno = $mpartyname[gstno];
                    $gettrans = mysqli_query($conn, "SELECT code,name FROM `transport` WHERE `code`='$qry[transporter]'")or die(mysqli_error($conn));
                    $mtrans = mysqli_fetch_array($gettrans);
                    ?>
                    <table>
                        <tr>
                            <td style="width:10%" class="text-right">
                                <label class="fontnew" style="text-align: right;">M/s.&nbsp;&nbsp;&nbsp;</label>
                            </td>
                            <td  class="" style="width:45%;border-right: 1px solid black">
                                <label class="fontnew"><b><? Echo $partyname; ?></b></label>
                            </td>
                            <td style="width:15%" class="text-right">
                                <label class="fontnew" style="text-align: right;"><b>Invoice No. :&nbsp;&nbsp; </b></label>
                            </td>
                            <td  class="" style="width:15%">
                                <label style="font-size: 12px;font-family: 'verdana'"><b><? Echo $qry[invno]; ?></b></label>
                            </td>
                        </tr>
                        <tr>
                            <td style="width:10%" class="text-right">
                                <label class="fontnew" ></label>
                            </td>
                            <td  class="" style="width:45%;border-right: 1px solid black">
                                <?php
                                if ($add1 != '') {
                                    ?>
                                    <label class="fontnew"><b><? Echo $add1 . '' . $add2; ?></b></label>
                                <?php }
                                ?>
                            </td>
                            <td style="width:15%" class="text-right">
                                <label class="fontnew" style="text-align: right;" >Date :&nbsp;&nbsp;</label>
                            </td>
                            <td  class="" style="width:15%">
                                <label class="fontnew"><? Echo $qry[mydt]; ?></label>
                            </td>
                        </tr>
                        <tr>
                            <td style="width:10%" class="text-right">
                                <label class="fontnew" ></label>
                            </td>
                            <td  class="" style="width:45%;border-right: 1px solid black">
                                <?php
                                if ($add3 != '') {
                                    ?>
                                    <label class="fontnew"><b><? Echo $add3 ?></b></label>
                                <?php }
                                ?>
                            </td>
                            <td style="width:15%" class="text-right">
                                <label class="fontnew" style="text-align: right;" >Transport :&nbsp;&nbsp;</label>
                            </td>
                            <td  class="" style="width:15%">
                                <label class="fontnew"><? Echo $mtrans[name]; ?></label>
                            </td>
                        </tr>
                        <tr>
                            <td style="width:10%" class="text-right">
                                <?php
                                if ($mobile != '') {
                                    ?>
                                    <!--<label class="fontnew" style="text-align: right;" >Mobile :&nbsp;&nbsp; </label>-->
                                <?php } ?>
                            </td>
                            <td  class="" style="width:45%;border-right: 1px solid black">
                                <?php
                                if ($mobile != '') {
                                    ?>
                                    <label class="fontnew"><b><? // Echo $mobile;    ?></b></label>
                                <?php }
                                ?>
                            </td>
                            <td style="width:15%" class="text-right">
                                <label class="fontnew" style="text-align: right;" >L.R. No. :&nbsp;&nbsp; </label>
                            </td>
                            <td  class="" style="width:15%">
                                <label class="fontnew"><? Echo $qry[lrno]; ?></label>
                            </td>
                        </tr>
                        <tr>
                            <td style="width:10%" class="text-right">
                                <?php
                                if ($gstslform != '') {
                                    ?>
                                    <label style="font-size: 13px;font-family: 'verdana';text-align: right;" >GST NO :&nbsp;&nbsp; </label>
                                <?php } ?>
                            </td>
                            <td  class="" style="width:45%;border-right: 1px solid black">
                                <?php
                                if ($gstno != '' && $gstno != '0') {
                                    ?>
                                    <label style="font-size: 13px;font-family: 'verdana';"><? Echo $gstno; ?></label>
                                <?php }
                                ?>
                            </td>
                            <td style="width:15%" class="text-right">
                                <label class="fontnew" style="text-align: right;" >L.R. Date :&nbsp;&nbsp; </label>
                            </td>
                            <td  class="" style="width:15%">
                                <label style="font-size: 12px;font-family: 'verdana'"><? Echo $qry[lrdate]; ?></label>
                            </td>
                        </tr>
                    </table>

                </div>
            </div>
            <div style="border-right: 1px solid black;border-left: 1px solid black;">
                <div class="row">
                    <div class="col-sm-12 pad">
                        <table class="table-condensed table-hover" style="width: 100%">
                            <tr style="background: #0098DA;color: white;border: 1px solid black; height: <?= $tdhg; ?>">
                                <th class="numeric bor" style="font-size:12px;width:2%">Sr</th>
                                <th class="bor" style="font-size:12px;width:25%">Description</th>
                                <th class="bor" style="font-size:12px;width:10%">Hsncode</th>
                                <th class="bor numeric " style="font-size:12px;width:10%">B.NO.</th>
                                <th class="bor" style="font-size:12px;width:8%">N.WT.</th>
                                <th class="bor numeric " style="font-size:12px;width:6%">Qty.</th>
                                <th class="bor numeric " style="font-size:12px;width:7%">Rate</th>
                                <th class="bor numeric " style="font-size:12px;width:10%">Amount</th>
                                <th class="bor numeric " style="font-size:12px;width:6%">Taxable Value</th>
                                <th class="bor numeric " style="font-size:12px;width:4%">Gst %</th>
                                <th class="bor numeric" style="font-size:12px;width:7%">Total</th>
                            </tr>
                            <?
                            $ctab = 0;
                            $gettran = mysqli_query($conn, "SELECT * FROM `saletran` WHERE `vouchno`='$qry[vouchno]' ORDER BY `srno`")or die(mysqli_error($conn));
                            while ($tran = mysqli_fetch_array($gettran)) {
                                $getitem = mysqli_query($conn, "SELECT `nametoprint` FROM `product` WHERE `barcode`='$tran[barcode]'")or die(mysqli_error($conn));
                                $item = mysqli_fetch_array($getitem);
                                $descrip = $item[nametoprint];
                                $gstperc = $tran[cgstrate] * 2;
                                ?>
                                <tr style="height: <?= $tdhg; ?>;">
                                    <td class="bor"><? Echo $tran[srno]; ?></td>
                                    <td class="bor"><? Echo $descrip; ?></td>
                                    <td class="bor"><? Echo $tran[hsncode]; ?></td>
                                    <td class="bor numeric "><? Echo $tran[batch]; ?></td>
                                    <td class="bor"><? Echo $tran[gram]; ?></td>
                                    <td class="bor text-right"><? Echo round($tran[qty], 0); ?></td>
                                    <td class="bor text-right"><? Echo $tran[rate]; ?></td>
                                    <td class="bor text-right"><? Echo round($tran[amount]); ?></td>
                                    <td class="bor text-right"><? Echo round($tran[subtot]); ?></td>
                                    <?php if ($oms == '') { ?>
                                        <td class="text-right bor"><? Echo $gstperc ?></td>
                                    <?php } else { ?>
                                        <td class="text-right bor"><? Echo $tran[igstrate]; ?></td>
                                    <?php } ?>
                                    <td class="text-right bor"><? Echo round($tran[total]); ?></td>
                                </tr>
                                <?
                                $ctab = $ctab + 3;
                            }
                            if ($ctab < 24) {
                                for ($i = $ctab; $i < 24; $i++) {
                                    ?>
                                    <tr style="height: <?= $tdhg; ?>">
                                        <td class="bor"></td>
                                        <td class="bor"></td>
                                        <td class="bor"></td>
                                        <td class="bor"></td>
                                        <td class="bor"></td>
                                        <td class="bor"></td>
                                        <td class="bor"></td>
                                        <td class="bor"></td>
                                        <td class="bor"></td>
                                        <td class="bor"></td>

                                        <td class="bor"></td>
                                    </tr>
                                    <?php
                                }
                            }
                            ?> 
                        </table>
                    </div>
                </div>
            </div>
            <div class="border">
                <div class="row">
                    <div class="col-sm-12" style="padding: 0px">
                        <table class="table-hover pad" style="float: left;width:50%">
                            <tr>
                                <td><label class="pad"><? Echo $qry[rem1] ?></label></td>
                            </tr>
                            <tr>
                                <td><label class="pad"><? Echo $qry[rem2]; ?></label></td>
                            </tr>
                        </table>
                        <table class="table-hover pad" style="float:left;width:50%">
                            <tr>
                                <td style="text-align: right"><label class="pad">Sub Total</label></td>
                                <td class="numeric" style="text-align:left "><label class="pad"></label></td>
                                <td class="numeric"><label class="pad" style="text-align:right"><? echo $qry[grossamt]; ?></label></td>
                            </tr>
                            <tr>
                                <td style="text-align: right"><label class="pad">Discount</label></td>
                                <td><label class="pad" style="text-align:left "><? $qry[discperc]; ?></label></td>
                                <td class="numeric"><label class="pad"><? Echo $qry[discamt]; ?></label></td>
                            </tr>
                            <tr>
                                <td style="text-align: right"><label class="pad">Taxable Value</label></td>
                                <td class="numeric" style="text-align:left "><label class="pad"></label></td>
                                <td class="numeric"><label class="pad"><? Echo $qry[taxableamt]; ?></label></td>
                            </tr>
                        </table>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12" style="padding: 0px">
<!--                        <table class="table-hover " style="float: left;width:50%">
                            <tr>
                                <td><label class="pad">Staff : </label> <? Echo $staff[name]; ?></td>
                            </tr>
                            <tr>
                                <td><label class="pad">GST NO. : </label> <? Echo $comp2[gstno]; ?></td>
                            </tr>
                            <tr>
                                <td><label class="pad">Bank Name : </label> <? Echo $comp2[bankname]; ?></td>
                            </tr>
                            <tr>
                                <td><label class="pad">Branch Name : </label> <? Echo $comp2[branchname]; ?></td>
                            </tr>
                            <tr>
                                <td><label class="pad">Account No. : </label> <? Echo $comp2[acno]; ?></td>
                            </tr>
                            <tr>
                                <td><label class="pad">IFC Code : </label> <? Echo $comp2[ifccode]; ?></td>
                            </tr>
                        </table>-->
                        <table class="table-hover " style="float: left;width:50%">
                            <tr>
                                <td>&nbsp;</td>
                            </tr>
                        </table>
                        <table class="table-hover table-bordered" style="float: left;width:50%">
                            <tr style="height: 21px">
                                <td width="10%" class='text-right'>%</td>
                                <td width="18%" class='text-right'>CGST</td>
                                <td width="18%" class='text-right'>SGST</td>
                                <td width="18%" class='text-right'>IGST</td>
                                <td width="35%"></td>
                            </tr>
                            <?php
                            Echo "<tr style='height: 21px'>";
                            Echo "<td class='text-right'><b>5%</b></td>";
                            Echo "<td class='text-right'>$qry[cgst5]</td>";
                            Echo "<td class='text-right'>$qry[sgst5]</td>";
                            Echo "<td class='text-right'>$qry[igst5]</td>";
                            Echo "<td class='text-right'>$qry[totgst5]</td>";
                            Echo "</tr>";
                            Echo "<tr style='height: 21px'>";
                            Echo "<td class='text-right'><b>12%</b></td>";
                            Echo "<td class='text-right'>$qry[cgst12]</td>";
                            Echo "<td class='text-right'>$qry[sgst12]</td>";
                            Echo "<td class='text-right'>$qry[igst12]</td>";
                            Echo "<td class='text-right'>$qry[totgst12]</td>";
                            Echo "</tr>";
                            Echo "<tr style='height: 21px'>";
                            Echo "<td class='text-right'><b>18%</b></td>";
                            Echo "<td class='text-right'>$qry[cgst18]</td>";
                            Echo "<td class='text-right'>$qry[sgst18]</td>";
                            Echo "<td class='text-right'>$qry[igst18]</td>";
                            Echo "<td class='text-right'>$qry[totgst18]</td>";
                            Echo "</tr>";
                            Echo "<tr style='height: 21px'>";
                            Echo "<td class='text-right'><b>28%</b></td>";
                            Echo "<td class='text-right'>$qry[cgst28]</td>";
                            Echo "<td class='text-right'>$qry[sgst28]</td>";
                            Echo "<td class='text-right'>$qry[igst28]</td>";
                            Echo "<td class='text-right'>$qry[totgst28]</td>";
                            Echo "</tr>";
                            ?>
                        </table>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12" style="padding: 0px">
                        <table class="table-hover " style="float: left;width:50%">
                            <tr>
                                <td><label class="pad"> </label> <? ?></td>
                            </tr>
                        </table>
                        <table class="table-hover" style="float: right;width:35%">
                            <tr>
                                <td><label class="pad" style="font-family: 'verdana';font-size: 15px">Bill Amount</label></td>
                                <td class="numeric"><label class="pad"><i style="font-size:15px" class="fa fa-rupee"></i></label></td>
                                <td class="numeric"><label class="pad" style="font-family: 'verdana';font-size: 16px"><b><? Echo number_format(round($qry[billamt]), 2); ?></b></label></td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
            <?php
            $mwords = round($qry[billamt]);
//            $words = convert_number_to_words($mwords);
            $words = rsword($mwords);
            ?>
            <div class="border" style="">
                <div class="row">
                    <div class="col-sm-6" style="padding: 0px">
                        <label class="pad"><?php Echo $words . " Only"; ?></label>
                    </div>
                </div>
            </div>
            <?php $content = "Certified that the particulars given above are true and correct." ?>
            <div class="" style="padding-top: 10px;">
                <div class="row">
                    <div class="col-sm-12" style="padding:0px">

                        <table width='100%'>
                            <tr>
                                <td style="width: 50%">
                                    <label style="font-family: 'verdana';font-size: 16px">E.O.& E.</label>
                                </td>
                                <td style="width: 50%" class="text-right">
                                    <label><b style="font-family: 'verdana';font-size: 16px"><?php Echo "For " . $comp2[compname] ?></b></label>
                                </td>
                            </tr>
                        </table>
                        <table width='50%'>
                            <tr>
                                <td>
                                    <label style="font-size:10px">
                                        " I / We hereby certify that  my / our registration  certificate  under  the  
                                        Maharashtra Value Added Tax Act  2002  is in force  on  the  date on which 
                                        the sale of the goods specified in this tax invoice is made by me/us  and 
                                        that the transaction of sale covered  by  this  tax invoice has been 
                                        affected by me/us and its shall be accounted for in the turnover of sales 
                                        while filling of return and the due tax, if any, payable on the sale has 
                                        been paid or shall be paid. "
                                    </label>
                                </td>
                            </tr>
                        </table>
                        <table width='100%'>
                            <tr>
                                <td class="text-right">
                                    <label><b style="font-family: 'verdana';font-size: 16px">Authorized Signatory</b></label><br>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>

            </div>
        </div>
    </div>
    <?php include('includes/linksfoot.php'); ?>
