<?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 = "TAX INVOICE";

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

$getcust = mysqli_query($conn, "SELECT * from accmast where accode = '$accode' ") or die(mysqli_error($conn));
$cust = mysqli_fetch_array($getcust);
$custname = $cust['name'];
$billtype = $cust['billtype'];

if ($billtype == 'S') {
    $typeord = ' order by vouchdt, vouchno ';
} else {
    $typeord = ' order by vehicleno, vouchdt, vouchno ';
}

$getdata = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as myvouchdt 
                                       from ctran WHERE party = '$accode' $typeord ") or die(mysqli_error($conn));


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

    <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;
        }

        .sktablecss {
            height: 22px;
            border-bottom: 1px solid grey;
            border-bottom-width: thin;
        }

        .table>thead>tr>th,
        .table>tbody>tr>th,
        .table>tfoot>tr>th,
        .table>thead>tr>td,
        .table>tbody>tr>td,
        .table>tfoot>tr>td {
            padding: 2px;
        }

        .table>thead>tr>td,
        .table>tbody>tr>td,
        .table>tfoot>tr>td {
            padding: 3px;
        }
    </style>
</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 style="border:solid 1px;height:120px;" class="col-md-12">
                <h5 style="height:0px;">
                    <center><? echo $formname; ?></center>
                </h5>
                <h3 style="height:20px;">
                    <center><b><?php echo $getcmpname['compname']; ?></b></center>
                </h3>
                <center style="height:18px;font-size : 14px;"><?php echo $getcmpname['hoadd1'] . " " . $getcmpname['hoadd2'] . " " . $getcmpname['hoadd3']; ?></center>
                <center style="height:14px;"><?php echo "Contact No :" . $getcmpname['htel']; ?></center>
                <h5 style="height:7px;">
                    <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">
                <table class=" table-border table-hover" style="width:100%;font-family:Arial, Helvetica, sans-serif">
                    <tr class="sktablecss">
                        <td style="text-align: left; width:5%"><b>Month</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:40%"><?php echo $period; ?></td>
                        <td style="text-align: right ; width:30%"><b></b></td>
                        <td style="width:1%"></td>
                        <td style="text-align: left; width:20%"></td>
                    </tr>
                    <tr class="sktablecss">
                        <td style="text-align: left; width:5%"><b>Invoice no.</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:40%"><?php echo $dinvno; ?></td>
                        <td style="text-align: right ; width:30%"><b>Date</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:20%; margin-right: 15px;"><?php echo $mtdt; ?></td>
                    </tr>
                    <tr class="sktablecss">
                        <td style="text-align: left; width:5%"><b>Account No.</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:40%"><?php echo $custcode; ?></td>
                        <td style="text-align: right ; width:30%"><b></b></td>
                        <td style="width:1%"></td>
                        <td style="text-align: left; width:20%"></td>

                    </tr>
                    <tr class="sktablecss">
                        <td style="text-align: left; width:15%"><b>Name</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:40%"><?php echo $custname; ?></td>
                        <td style="text-align: right ; width:30%"></td>
                        <td style="width:1%"></td>
                        <td style="text-align: left; width:15%"></td>
                    </tr>
                    <tr class="sktablecss">
                        <td style="text-align: left; width:5%"><b>Address</b></td>
                        <td style="width:1%">:</td>
                        <td style="text-align: left; width:40%"><?php echo $cust['add1']; ?></td>
                        <td style="text-align: right ; width:30%"><b></b></td>
                        <td style="width:1%"></td>
                        <td style="text-align: left; width:20%"></td>
                    </tr>
                    <tr style="height: 20px;">
                        <td style="text-align: left; width:5%"></td>
                        <td style="width:1%"></td>
                        <td style="text-align: left; width:40%"><?php echo $cust['add2'] . $cust['add3']; ?></td>
                        <td style="text-align: right ; width:30%"><b></b></td>
                        <td style="width:1%"></td>
                        <td style="text-align: left; width:20%"></td>
                    </tr>
                </table>
            </div>
            <div>
                <table class="table table-bordered" style="border-collapse: collapse; width:100%">
                    <thead>
                    <th style="width:3%; "><label>Sr.</label></th>
                    <th style="width:10%;  text-align: left;"><label>Date</label></th>
                    <th style="width:8%;  text-align: left;"><label>Slip No.</label></th>
                    <th style="width:12%;  text-align: left;"><label>Vehicle</label></th>
                    <th style="width:30%; text-align: left;"><label>Product</label></th>
                    <th style="width:10%; text-align: right;"><label>Qty.</label></th>
                    <th style="width:5%; text-align: left;"><label>Unit</label></th>
                    <th style="width:10%; text-align: right;"><label>Rate</label></th>
                    <th style="width:10%;  text-align: right;"><label>Amount</label></th>
                    </thead>

                    <?php
                    $ctr = 0;
                    $ctab = $totqty = $totamt = 0;
                    $pqty = $dqty = $cqty = $oqty = 0;
                    while ($showtran = mysqli_fetch_array($getdata)) {

                        $prodcode = $showtran['prodcode'];

                        $getitem = mysqli_query($conn, "SELECT prodcode, name FROM `product` WHERE prodcode = '$showtran[prodcode]'") or die(mysqli_error($conn));
                        $geti = mysqli_fetch_array($getitem);
                        $prodname = $geti['name'];


                        $mydt = $showtran['myvouchdt'];
                        $slipno = $showtran['slipno'];
                        $vehno = $showtran['vehno'];

                        $icode = $showtran['product'];

                        $amt = $showtran['amt'];

                        $unit = $showtran['unit'];
                        $qty = $showtran['qty'];
                        $rate = $showtran['rate'];

                        $ctr++;

                        echo "<tr style='height:26px;' id='bold-font'>";
                        echo "<td style='text-align:center'>$ctr</td>";
                        echo "<td>$mydt</td>";
                        echo "<td style='text-align:center'>$slipno</td>";
                        echo "<td>$vehno</td>";
                        echo "<td>$prodname</td>";
                        echo "<td style=' text-align: right;'>$qty</td>";
                        echo "<td>$unit</td>";
                        echo "<td style=' text-align:right;'>$rate</td>";
                        echo "<td style=' text-align:right;'>$amt</td>";
                        echo "</tr>";

                        if ($prodcode == 'P') {
                            $pqty = $pqty + $qty;
                        } elseif ($prodcode == 'D') {
                            $dqty = $dqty + $qty;
                        } elseif ($prodcode == 'C') {
                            $cqty = $cqty + $qty;
                        } else {
                            $oqty = $oqty + $qty;
                        }

                        $totqty = $totqty + $qty;
                        $totamt = $totamt + $amt;
                        $ctab = $ctab + 1;
                    }
                    if ($ctab < 16) {
                        for ($i = $ctab; $i <= 16; $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>
                                <td class="border"></td>
                                <td class="border"></td>
                            </tr>
                            <?php
                        }
                    }
                    ?>


                    <tr style="height:26px;">
                        <td colspan="9">
                            <b style="padding-left: 80px">Petrol</b>&nbsp;&nbsp;&nbsp;<?php echo $pqty; ?>
                            <b style="padding-left:80px">Diesel</b>&nbsp;&nbsp;&nbsp;<?php echo $dqty; ?>
                            <b style="padding-left:80px">CNG</b>&nbsp;&nbsp;&nbsp;<?php echo $cqty; ?>
                            <b style="padding-left:80px">Oil/Other</b>&nbsp;&nbsp;&nbsp;<?php echo $oqty; ?>
                        </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><center><b>RTGS DETAIL</b></center></tr>  -->
                                    <tr style="height:26px;">
                                        <td style="width:35%;"><b>Bank Name</b></td>
                                        <td style="width:75%;"><?= $getcmpname['bankname'] ?></td>
                                    </tr>

                                    <tr style="height:26px;">
                                        <td style="width:25%;"><b>Branch</b></td>
                                        <td style="width:75%;"><?= $getcmpname['branchname'] ?></td>
                                    </tr>
                                    <tr style="height:26px;">
                                        <td style="width:25%;"><b>Account No.</b></td>
                                        <td style="width:75%;"><?= $getcmpname['acno'] ?></td>
                                    </tr>
                                    <tr style="height:26px;">
                                        <td style="width:25%;"><b>IFSC Code</b></td>
                                        <td style="width:75%;"><?= $getcmpname['ifccode'] ?></td>
                                    </tr>
                                </tbody>
                            </table>
                        </td>

                        <td colspan="4" style="border:none;border-collapse: collapse; ">
                            <table class="table-bordered" style="border-collapse: collapse; width:100%" align="right">
                                <tbody>
                                    <tr style="height:26px;">
                                        <td style="width:50%;text-align: right">Subtotal </td>
                                        <td style="width:50%;text-align: right"><?= number_format($totamt, 2); ?></td>
                                    </tr>

                                    <?php
                                    if ($discdies > 0) {
                                        ?>
                                        <tr style="height:26px;">
                                            <td style="width:50%;text-align: right">Discount @<b><?= $diesmast ?></b> per litres</td>
                                            <td style="width:50%;text-align: right"><?= $discdies ?></td>
                                        </tr>
                                    <?php } else {
                                        ?>

                                        <tr style="height:26px;">
                                            <td style="width:50%;text-align: right"></td>
                                            <td style="width:50%;text-align: right"></td>
                                        </tr>
                                    <?php } ?>

                                    <?php
                                    if ($tcsamt > 0) {
                                        ?>
                                        <tr style="height:26px;">
                                            <td style="width:50%;text-align: right">TCS <?= $tcsperc ?>% Total TCS</td>
                                            <td style="width:50%;text-align: right"><?= $tcsamt ?></td>
                                        </tr>
                                    <?php } else { ?>

                                        <tr style="height:26px;">
                                            <td style="width:50%;text-align: right"></td>
                                            <td style="width:50%;text-align: right"></td>
                                        </tr>
                                    <?php } ?>



                                    <?php
                                    if ($oper > 0) {
                                        ?>
                                        <tr style="height:26px;">
                                            <td style="width:50%;text-align: right"><?php echo $opchg ?>  A/c. Operating Charges</td>
                                            <td style="width:50%;text-align: right"><?= $oper ?></td>
                                        </tr>
                                    <?php } else { ?>
                                        <tr style="height:26px;">
                                            <td style="width:50%;text-align: right"></td>
                                            <td style="width:50%;text-align: right"></td>
                                        </tr>
                                    <?php } ?>


                                    <tr style="height:26px;">
                                        <td style="width:50%;text-align: right">Net Amount</td>
                                        <td style="width:50%;text-align: right;font-weight:bolder;"><?= number_format($sot, 2); ?></td>
                                    </tr>

                                    <tr style="height:26px;">
                                        <td style="width:50%;text-align: right">(Rounded Off)</td>
                                        <td style="width:50%;text-align: right;font-weight:bolder;"><?= number_format($rsot, 2); ?></td>
                                    </tr>

                                </tbody>
                            </table>
                        </td>
                    <tr>
                    <tr style="height:26px;">
                        <td colspan="9">
                            Rupees
                            <?php echo rsword(round($rsot));
                            ?>
                        </td>

                    </tr>

                </table>
            </div>

            <div>
                <table class="border">
                    <tr>
                        <td class="border" style="width: 511px; font-size: 10px;">

                            I / We hereby certify that my/our registration certificate under Rule 1 of GST invoice
                            rules 2017 is in force 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
                            effected by me/us and it shall be accounted of 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.

                        </td>
                        <td class="border" style="width: 246px">

                            <div style=" text-align:right; font-size: 14px; font-weight: 900"><? echo "For $getcmpname[compname]"; ?></div>
                            <div style="padding-left: 25px;"><img src="images/viveklogo2.png" width="35%"></div>

                            <div style=" margin-left: 86px; font-size: 11px">AUTHORIZED SIGNATORY</div>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>

    <?php
////////////    invoice    /// 

    if ($hpara == 'Y') {

        $mhalf2 = $mperiod - 1;
        mysqli_query($conn, "INSERT INTO `invoice` (`month`, `half`, `year`,  `period`,  `invno`, `invdate`, `custcode`, `accode`, `vehno`,
                                                `subtotal`, `opchg`, `operchgs`, `discdies`, `tcsamt`, `tdsperc`, `tdsamt`, 
                                                `stonopperc`, `stonopchgs`, `sototal`, `roff`, `billamt`, `arrears`, `oparr`, `grdtot`)
                                        VALUES ('$mmonth', '$mhalf2', '$myear', '$mperiod', '$dinvno', '$mbilldate', '$custcode', '$accode', '$vehno',
                                                '$totamt', '$opchg', '$oper', '$discdies', '$tcsamt', '$tdsperc', '$tdsamt', 
                                                '$stopperc', '$stopchgs', '$sot', '$roff', '$rsot', '$arrears', '$oparr', '$grd') ") or die(mysqli_error($conn));

////////////    actran   /// accounts part

        $paccode = $accode;

        $trnno = 12 + $mperiod;
        $minoper = $oper * - 1;
        if ($oper > 0) {
            mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`)
                                        VALUES ('OC', '$paccode', '$ddate', '$trnno', '$paccode', '010', '$minoper') ") or die(mysqli_error($conn));

            mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`)
                                        VALUES ('OC', '$paccode', '$ddate', '$trnno', '000027', '011', '$oper') ") or die(mysqli_error($conn));
        }

        if ($stopchgs > 0) {
            $minstopchgs = $stopchgs * - 1;
            mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`)
                                        VALUES ('STOC', '$paccode', '$ddate', '$trnno', '$paccode', '010', '$minstopchgs') ") or die(mysqli_error($conn));

            mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`)
                                        VALUES ('STOC', '$paccode', '$ddate', '$trnno', '000006', '020', '$stopchgs') ") or die(mysqli_error($conn));
        }

        if ($oparr > 0) {
            $minoparr = $oparr * - 1;
            mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`)
                                        VALUES ('AC', '$paccode', '$ddate', '$trnno', '$paccode', '010', '$minoparr') ") or die(mysqli_error($conn));

            mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`)
                                        VALUES ('AC', '$paccode', '$ddate', '$trnno', '000039', '011', '$oparr') ") or die(mysqli_error($conn));
        }

        IF ($mperiod != 8) {
            $mintcs = $tcsamt * -1;
            mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`, `staffcode`)
                                        VALUES ('TCS', '$paccode', '$ddate', '$trnno', '$paccode', '010', '$mintcs', '000032') ") or die(mysqli_error($conn));

            mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`, `staffcode`)
                                        VALUES ('TCS', '$paccode', '$ddate', '$trnno', '000032', '020', '$tcsamt', '$paccode') ") or die(mysqli_error($conn));
        }

        if ($rof != 0) {
            $minrof = $rof * - 1;
            mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`)
                                        VALUES ('RO', '$paccode', '$ddate', '$trnno', '$paccode', '010', '$rof') ") or die(mysqli_error($conn));

            mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`)
                                        VALUES ('RO', '$paccode', '$ddate', '$trnno', '000028', '012', '$minrof') ") or die(mysqli_error($conn));
        }
        IF ($tdsamt > 0) {
            $mintds = $tdsamt * -1;
            mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`, `staffcode`)
                                        VALUES ('TDS', '$paccode', '$ddate', '$trnno', '$paccode', '010', '$tdsamt', '000034') ") or die(mysqli_error($conn));

            mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`, `staffcode`)
                                        VALUES ('TDS', '$paccode', '$ddate', '$trnno', '000034', '020', '$mintds', '$paccode') ") or die(mysqli_error($conn));
        }
    }
    include("includes/linksfoot.php");
    ?>
    <script>
        $(function () {
            formGeneral();
        });
    </script>
</body>

</html>