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

$maindb = "salemain";
$trandb = "saletran";
$formname = "Sale GST Register";
$file = "salegstreg_fr.php";

$fseries = $_SESSION["series_" . $dbname];
$keycond = " and `series`='$fseries'";

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

$vouchdt = trim($_POST["vouchdt"]);
$voucher = trim($_POST["voucher"]);
$mono = $_POST["monno"];
$invno = $_POST["invno"];
$godown = $_POST["godown"];
$addgst = $_POST["addgst"];
$addpack = $_POST["addpack"];
//    $debitac = $_POST["debitac"];
$paidby = trim($_POST["paidby"]);
$party = trim($_POST["party"]);
$taxableamt = trim($_POST["taxableamt"]);
$totbillamt = trim($_POST["totbillamt"]);
$grossamt = trim($_POST["grossamt"]);
$gstno = trim($_POST["gstno"]);
$totdisc = trim($_POST["totdisc"]);
$totvat = trim($_POST["totvat"]);
$totalqty = trim($_POST["totalqty"]);
$packby = trim($_POST["packby"]);
$mobile = trim($_POST["mobile"]);
$rem1 = trim($_POST["remark1"]);
$rem2 = trim($_POST["remark2"]);
$tvatperc = trim($_POST["tvatperc"]);
$ldiscperc = trim($_POST["ldiscperc"]);

$lrno = $_POST["lrno"];
$lrdate = $_POST["lrdate"];
$transporter = $_POST["transporter"];
$area = $_POST["area"];

$totadv = $_POST["totadv"];
$totbal = $_POST["totbal"];

$dtypecond = "";
$mscopecond = "";
$itemcond = "";
//    $gstcond = "";
$prodtypecond = "";

$mfrmdate = trim($_POST["fdate"]);
$mtodate = trim($_POST["tdate"]);

if (!empty($party)) {
    $partycond = " AND `party`='$party' ";
}
if (!empty($salesman)) {
    $salemcond = " AND `salesman`='$salesman' ";
}
//    if (!empty($gstperc)) {
//        $gstcond = " AND `gstperc`='$gstperc' ";
//    }
if (!empty($dtype)) {
    $dtypecond = " AND `dtype`='$dtype' ";
}

if (!empty($scope)) {
    $mscopecond = " AND `type`='$scope' ";
}
$prncond = "";
if ($prnflag == 1) {
    $prn = '1';
} else {
    $prn = '';
}
if (!empty($prnflag)) {
    $prncond = " AND `prnflag`='$prn' ";
}

if (empty($_POST["fdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; slgstreg.php");
}
if (empty($_POST["tdate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; slgstreg.php");
}

$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` where
                           `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' AND series = '$fseries' $prncond $partycond $salemcond $keycond order by series,vouchno")or die(mysqli_error($conn));

if (isset($_POST["view"])) {

    include("includes/linkshead.php");
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        .dtype{
            background-color: #cccccc;
        }
    </style>
    </head>
    <body>
        <div class="container-fluid">
            <?php include("includes/head_rep.php"); ?>

            <div class="row">
                <div class="col-lg-12 pad">
                    <?php
                    if ($namenum == 1) {
                        ?>
                        <table style="background: white;width: 50%" align="center" class="table table-striped table-condensed table-bordered table-hover">
                            <thead class="mySearchTr">
                                <tr>
                                    <th width='3%'>Sr</th>
        <!--                                    <th width='3%'>Inv.</th>
                                    <th width='7%'>Date</th>-->
                                    <th width='19%'>Party</th>
                                    <th width='4%'>Mobile</th>
                                    <th width='7%'>Series</th>
                                    <!--<th width='5%' class="text-right">Bill.Amt</th>-->
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $ttotamt = 0;
                                $totpack = 0;
                                $totbill = 0;

                                while ($pmain = mysqli_fetch_array($getpmain)) {
                                    $getsupp = mysqli_query($conn, "SELECT `accode`,`name`,mobile FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
                                    $supp = mysqli_fetch_array($getsupp);

                                    if ($pmain['nameprint'] != '') {
                                        $namep = $pmain['nameprint'];
                                    } else {
                                        $namep = $supp['name'];
                                    }

                                    if ($pmain['printmobile'] != '') {
                                        $mobp = $pmain['printmobile'];
                                    } else {
                                        $mobp = $supp['mobile'];
                                    }

                                    $ctr++;
                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>$ctr</td>";
//                                    Echo "<td>$pmain[invno]</td>";
//                                    Echo "<td>$pmain[mydt]</td>";
                                    Echo "<td>$namep</td>";
                                    Echo "<td>$mobp</td>";
                                    Echo "<td>$pmain[series]</td>";
//                                    Echo "<td class='text-right'>$pmain[netamt]</td>";
                                    Echo "</tr>";
                                    $totbill = $totbill + $pmain['netamt'];
                                }
                                ?>
                            </tbody>
                        </table>
                    <?php } else {
                        ?>
                        <table style="background: white;width: 100%" class="table table-striped table-condensed table-bordered table-hover">
                            <thead class="mySearchTr">
                                <tr>
                                    <th width='3%'>Sr</th>
                                    <th width='3%'>Voucher</th>
                                    <th width='5%'>Date</th>
                                    <th width='2%'>Inv.</th>
                                    <th width='14%'>Party</th>
                                    <th width='6%'>Paidby </th>
                                    <th width='6%'>LR. No. </th>
                                    <th width='6%'>LR Date </th>
                                    <th width='6%'>Transporter</th>
                                    <th width='6%'>Area</th>
                                    <th width='5%' class="text-right">Total Qty.</th>
                                    <th width='5%' class="text-right">Sub Total</th>
                                    <th width='5%' class="text-right">Less Item Discount</th>
                                    <th width='5%' class="text-right">Taxable Value</th>
                                    <th width='5%' class="text-right">Total Gst</th>
                                    <th width='5%' class="text-right">Total Amount</th>
                                    <th width='13%' >Remarks</th>

                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $ttotamt = 0;
                                $totpack = 0;
                                $totbill = 0;
                                $totitmpr = 0;
                                $totdisc = 0;
                                $ttottaxamt = 0;
                                $ttotgst5 = 0;
                                $ttotgst12 = 0;
                                $ttotgst18 = 0;
                                $ttotgst28 = 0;
                                $tcashamt = 0;
                                $tccamt = 0;
                                $tcredamt = 0;

                                while ($pmain = mysqli_fetch_array($getpmain)) {
                                    $getsupp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
                                    $supp = mysqli_fetch_array($getsupp);
                                    $getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
                                    $party = mysqli_fetch_array($getparty);
                                    $partyname = $party[name];
                                    $getport = mysqli_query($conn, "SELECT `code`,`name` FROM `transport` WHERE `code`='$pmain[transporter]'")or die(mysqli_error($conn));
                                    $transport = mysqli_fetch_array($getport);
                                    $transporter = $transport[name];
                                    $getsale = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[salesman]'")or die(mysqli_error($conn));
                                    $sale = mysqli_fetch_array($getsale);
                                    $getpay = mysqli_query($conn, "SELECT `code`,`name` FROM `paytype` WHERE `code`='$pmain[upiname]'")or die(mysqli_error($conn));
                                    $upay = mysqli_fetch_array($getpay);
                                    $upiname = $upay['name'];
                                    $prnf = '';

                                    if ($pmain['prnflag'] == '1') {
                                        $prnf = 'P';
                                    }

                                    $ctr++;
                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>$ctr</td>";
                                    Echo "<td><a href='salewhole.php?mode=edit&vouchno=$pmain[vouchno]&series=$pmain[series]' target='_blank'>$pmain[invno]</a></td>";

                                    Echo "<td>$pmain[invdt]</td>";
                                    Echo "<td>$pmain[invno]</td>";
                                    Echo "<td>$partyname</td>";
                                    Echo "<td>$pmain[cashcred]</td>";
                                    Echo "<td>$pmain[lrno]</td>";
                                    Echo "<td>$pmain[lrdate]</td>";
                                    Echo "<td>$transporter</td>";
                                    Echo "<td>$pmain[area]</td>";
                                    Echo "<td class='text-right'>$pmain[totqty]</td>";
                                    Echo "<td class='text-right'>$pmain[subtotal]</td>";
                                    Echo "<td class='text-right'>$pmain[litemdisc]</td>";
                                    Echo "<td class='text-right'>$pmain[tottaxamt]</td>";
                                    Echo "<td class='text-right'>$pmain[totalgst]</td>";
                                    Echo "<td class='text-right'>$pmain[totalamount]</td>";
                                    Echo "<td>$pmain[remark1] $pmain[remark1] </td>";
                                    Echo "</tr>";

                                    $ttotamt = $ttotamt + $pmain['subtotal'];
                                    $ttotqty = $ttotqty + $pmain['totqty'];
                                    $ttotalgst = $ttotalgst + $pmain['totalgst'];
                                    $totitmpr = $totitmpr + $pmain['litemdisc'];
                                    $totdisc = $totdisc + $pmain['discount'];
                                    $totpack = $totpack + $pmain['couamt'];
                                    $ttottaxamt = $ttottaxamt + $pmain['tottaxamt'];
                                    $ttotalamt = $ttotalamt + $pmain['totalamount'];
                                    $ttotgst5 = $ttotgst5 + $pmain['totgst5'];
                                    $ttotgst12 = $ttotgst12 + $pmain['totgst12'];
                                    $ttotgst18 = $ttotgst18 + $pmain['totgst18'];
                                    $ttotgst28 = $ttotgst28 + $pmain['totgst28'];
                                    $totbill = $totbill + $pmain['netamt'];
                                    $tcashamt = $tcashamt + $pmain['cashamt'];
                                    $tccamt = $tccamt + $pmain['ccamt'];
                                    $tcredamt = $tcredamt + $pmain['upiamt'];

                                    if ($_POST["dtype"] == 2) {

                                        Echo "<tr style='height:10px;'>";
                                        Echo "<td colspan='25'>";
                                        Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                        Echo "<th width='3%;'class='dtype'>Sr</th>";
                                        Echo "<th width='20%' class='dtype'>Itemname</th>";
                                        Echo "<th width='5%' class='dtype'>HSN Code</th>";
                                        Echo "<th width='5%' class='numeric dtype'>Qty</th>";
                                        Echo "<th width='6%' class='numeric dtype'>Rate</th>";
                                        Echo "<th width='6%' class='numeric dtype'>Amount</th>";
                                        Echo "<th width='6%' class='numeric dtype'>Disc%</th>";
                                        Echo "<th width='6%' class='numeric dtype'>Disc. Amt.</th>";
                                        Echo "<th width='6%' class='numeric dtype'>Taxable Value</th>";
                                        Echo "<th width='6%' class='numeric dtype'>CGST Rate</th>";
                                        Echo "<th width='6%' class='numeric dtype'>CGST Amt.</th>";
                                        Echo "<th width='6%' class='numeric dtype'>SGST Rate</th>";
                                        Echo "<th width='6%' class='numeric dtype'>SGST Amt.</th>";
                                        Echo "<th width='6%' class='numeric dtype'>IGST Rate</th>";
                                        Echo "<th width='6%' class='numeric dtype'>IGST Amt.</th>";
                                        Echo "<th width='4%' class='numeric dtype'>Total</th>";
                                        Echo "<tr>";

                                        $ctr2 = 0;
                                        $totqty = $totdisc = $totdiscamt = $totcgst = $totsgst = $totigst = $totamt = $tottaxable = $total = 0;


                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' $keycond")or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {

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

                                            $ctr2++;
                                            Echo "<tr>";
                                            Echo "<td>$ctr2</td>";
                                            Echo "<td>$item[name]</td>";
                                            Echo "<td>$tran[hsncode]</td>";
                                            Echo "<td class='numeric'>$tran[qty]</td>";
                                            Echo "<td class='numeric'>$tran[rate]</td>";
                                            Echo "<td class='numeric'>$tran[amount]</td>";
                                            Echo "<td class='numeric'>$tran[discperc]</td>";
                                            Echo "<td class='numeric'>$tran[discount]</td>";
                                            Echo "<td class='numeric'>$tran[subtot]</td>";
                                            Echo "<td class='numeric'>$tran[cgstrate]</td>";
                                            Echo "<td class='numeric'>$tran[cgstamt]</td>";
                                            Echo "<td class='numeric'>$tran[sgstrate]</td>";
                                            Echo "<td class='numeric'>$tran[sgstamt]</td>";
                                            Echo "<td class='numeric'>$tran[igstrate]</td>";
                                            Echo "<td class='numeric'>$tran[igstamt]</td>";
                                            Echo "<td class='numeric'>$tran[total]</td>";
                                            Echo "</tr>";

                                            $totamt = $totamt + $tran['amount'];
                                            $totrate = $totrate + $tran['rate'];
                                            $tottaxable = $tottaxable + $tran['subtot'];
                                            $totqty = $totqty + $tran['qty'];
                                            $totdisc = $totdisc + $tran['discperc'];
                                            $totdiscamt = $totdiscamt + $tran['discount'];
                                            $totcgst = $totcgst + $tran['cgstamt'];
                                            $totsgst = $totsgst + $tran['sgstamt'];
                                            $totigst = $totigst + $tran['igstamt'];
                                            $dtotcgst = $dtotcgst + $tran['cgstamt'];
                                            $dtotsgst = $dtotsgst + $tran['sgstamt'];
                                            $dtotigst = $dtotigst + $tran['igstamt'];
                                            $total = $total + $tran['total'];
                                        }
                                        echo "<tr> ";
                                        $colr1 = "blue";

                                        echo "<td colspan='3' style='color:$colr1' id='font1' class='text-right'><h5><b>Total</b></h5></td>";
                                        echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>$totqty</b></h5></td>";
                                        echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>$totrate </b></h5></td>";
                                        echo "<td style='color:$colr1' id='font1' class='numeric'><h5><b>$totamt</b></h5></td>";
                                        echo "<td style='color:$colr1' id='font1' class='numeric'><h5><b>$totdisc</b></h5></td>";
                                        echo "<td style='color:$colr1' id='font1' class='numeric'><h5><b>$totdiscamt</b></h5></td>";
                                        echo "<td style='color:$colr1' id='font1' class='numeric'><h5><b>$tottaxable</b></h5></td>";
                                        echo "<td style='color:$colr1' id='font1' class='numeric'><h5><b></b></h5></td>";
                                        echo "<td style='color:$colr1' id='font1' class='numeric'><h5><b>$totcgst</b></h5></td>";
                                        echo "<td style='color:$colr1' id='font1' class='numeric'><h5><b></b></h5></td>";
                                        echo "<td style='color:$colr1' id='font1' class='numeric'><h5><b>$totsgst</b></h5></td>";
                                        echo "<td style='color:$colr1' id='font1' class='numeric'><h5><b></b></h5></td>";
                                        echo "<td style='color:$colr1' id='font1' class='numeric'><h5><b>$totigst</b></h5></td>";
                                        echo "<td style='color:$colr1' id='font1' class='numeric'><h5><b>$total</b></h5></td>";
                                        echo "</tr>";

                                        Echo "</table>";
                                        Echo "</td>";
                                    }
                                }


                                $colr1 = "orange";
                                echo "<tr> ";

                                echo "<td colspan='10' style='color:$colr1' id='font1' class='text-right'><h5><b>Detail Total</b></h5></td>";
                                echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>$ttotqty</b></h5></td>";
                                echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>$ttotamt</b></h5></td>";
                                echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>$totitmpr</b></h5></td>";
                                echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>$ttottaxamt</b></h5></td>";
                                echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>$ttotalgst</b></h5></td>";
                                echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>$ttotalamt</b></h5></td>";
                                echo "<td style='color:$colr1' id='font1' class='numeric'><h5><b></b></h5></td>";

                                echo "</tr>";

                                $colr = "green";

                                echo "<tr> ";
                                echo "<td align='center' colspan='10' style='color:$colr;'><h5><b>Grand Total</b></h5></td>";
                                echo "<td align='right' style='color:$colr;' id='font'><h5><b>$ttotqty</b></h5></td>";
                                echo "<td align='right' style='color:$colr;' id='font'><h5><b>$ttotamt</b></h5></td>";
                                echo "<td align='right' style='color:$colr;' id='font'><h5><b>$totitmpr</b></h5></td>";
                                echo "<td align='right' style='color:$colr;' id='font'><h5><b>$ttottaxamt</b></h5></td>";
                                echo "<td align='right' style='color:$colr;' id='font'><h5><b>$ttotalgst</b></h5></td>";
                                echo "<td align='right' style='color:$colr;' id='font'><h5><b>$ttotalamt</b></h5></td>";
                                echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    <?php } ?>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}



if (isset($_POST["excel"])) {

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    if ($namenum == 1) {

        $contents .= "Sr., Invoice, Date, Party, Mobile, Series, Bill Amt.\n";

        $ctr = 0;
        $ttotamt = 0;
        $totpack = 0;
        $totbill = 0;

        while ($pmain = mysqli_fetch_array($getpmain)) {
            $getsupp = mysqli_query($conn, "SELECT `accode`,`name`,mobile FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
            $supp = mysqli_fetch_array($getsupp);

            if ($pmain['nameprint'] != '') {
                $namep = $pmain['nameprint'];
            } else {
                $namep = $supp['name'];
            }

            if ($pmain['printmobile'] != '') {
                $mobp = $pmain['printmobile'];
            } else {
                $mobp = $supp['mobile'];
            }

            $ctr++;
            $contents .= "$ctr" . ",";
            $contents .= "$namep" . ",";
            $contents .= "$mobp" . ",";
            $contents .= "$pmain[series]" . "\n";

            $totbill = $totbill + $pmain['netamt'];
        }
    } else {


        $contents .= "Sr., Voucher, Date, Inv., Party, Paidby, LR. No., LR Date, Transporter, Area, Total Qty., Sub Total, Less Item Discount, Taxable Value, Total GST, Total Amount, Remarks\n";

        $ctr = 0;
        $ttotamt = 0;
        $totpack = 0;
        $totbill = 0;
        $totitmpr = 0;
        $totdisc = 0;
        $ttottaxamt = 0;
        $ttotgst5 = 0;
        $ttotgst12 = 0;
        $ttotgst18 = 0;
        $ttotgst28 = 0;
        $tcashamt = 0;
        $tccamt = 0;
        $tcredamt = 0;

        while ($pmain = mysqli_fetch_array($getpmain)) {
            $getsupp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
            $supp = mysqli_fetch_array($getsupp);
            $getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
            $party = mysqli_fetch_array($getparty);
            $partyname = $party[name];
            $getport = mysqli_query($conn, "SELECT `code`,`name` FROM `transport` WHERE `code`='$pmain[transporter]'")or die(mysqli_error($conn));
            $transport = mysqli_fetch_array($getport);
            $transporter = $transport[name];
            $getsale = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[salesman]'")or die(mysqli_error($conn));
            $sale = mysqli_fetch_array($getsale);
            $getpay = mysqli_query($conn, "SELECT `code`,`name` FROM `paytype` WHERE `code`='$pmain[upiname]'")or die(mysqli_error($conn));
            $upay = mysqli_fetch_array($getpay);
            $upiname = $upay['name'];
            $prnf = '';

            if ($pmain['prnflag'] == '1') {
                $prnf = 'P';
            }

            $ctr++;
            $contents .= "$ctr" . ",";
            $contents .= "$pmain[invno]" . ",";
            $contents .= "$pmain[invdt]" . ",";
            $contents .= "$pmain[invno]" . ",";
            $contents .= "$partyname" . ",";
            $contents .= "$pmain[cashcred]" . ",";
            $contents .= "$pmain[lrno]" . ",";
            $contents .= "$pmain[lrdate]" . ",";
            $contents .= "$transporter" . ",";
            $contents .= "$pmain[area]" . ",";
            $contents .= "$pmain[totqty]" . ",";
            $contents .= "$pmain[subtotal]" . ",";
            $contents .= "$pmain[litemdisc]" . ",";
            $contents .= "$pmain[tottaxamt]" . ",";
            $contents .= "$pmain[totalgst]" . ",";
            $contents .= "$pmain[totalamount]" . ",";
            $contents .= "$pmain[remark1] $pmain[remark1] " . "\n";
            

            $ttotamt = $ttotamt + $pmain['subtotal'];
            $ttotqty = $ttotqty + $pmain['totqty'];
            $ttotalgst = $ttotalgst + $pmain['totalgst'];
            $totitmpr = $totitmpr + $pmain['litemdisc'];
            $totdisc = $totdisc + $pmain['discount'];
            $totpack = $totpack + $pmain['couamt'];
            $ttottaxamt = $ttottaxamt + $pmain['tottaxamt'];
            $ttotalamt = $ttotalamt + $pmain['totalamount'];
            $ttotgst5 = $ttotgst5 + $pmain['totgst5'];
            $ttotgst12 = $ttotgst12 + $pmain['totgst12'];
            $ttotgst18 = $ttotgst18 + $pmain['totgst18'];
            $ttotgst28 = $ttotgst28 + $pmain['totgst28'];
            $totbill = $totbill + $pmain['netamt'];
            $tcashamt = $tcashamt + $pmain['cashamt'];
            $tccamt = $tccamt + $pmain['ccamt'];
            $tcredamt = $tcredamt + $pmain['upiamt'];

            if ($_POST["dtype"] == 2) {

                
                $contents .= "Sr., Itemname, HSN Code, Qty, Rate, Amount, Disc %, Disc. Amt., Taxable Value, CGST Rate, CGST Amt., SGST Rate, SGST Amt., IGST Rate, IGSt Amt., Total\n";

                $ctr2 = 0;
                $totqty = $totdisc = $totdiscamt = $totcgst = $totsgst = $totigst = $totamt = $tottaxable = $total = 0;


                $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' $keycond")or die(mysqli_error($conn));
                while ($tran = mysqli_fetch_array($gettran)) {

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

                    $ctr2++;
                    $contents .= "$ctr2" . ",";
                    $contents .= "$item[name]" . ",";
                    $contents .= "$tran[hsncode]" . ",";
                    $contents .= "$tran[qty]" . ",";
                    $contents .= "$tran[rate]" . ",";
                    $contents .= "$tran[amount]" . ",";
                    $contents .= "$tran[discperc]" . ",";
                    $contents .= "$tran[discount]" . ",";
                    $contents .= "$tran[subtot]" . ",";
                    $contents .= "$tran[cgstrate]" . ",";
                    $contents .= "$tran[cgstamt]" . ",";
                    $contents .= "$tran[sgstrate]" . ",";
                    $contents .= "$tran[sgstamt]" . ",";
                    $contents .= "$tran[igstrate]" . ",";
                    $contents .= "$tran[igstamt]" . ",";
                    $contents .= "$tran[total]" . "\n";
                   

                    $totamt = $totamt + $tran['amount'];
                    $totrate = $totrate + $tran['rate'];
                    $tottaxable = $tottaxable + $tran['subtot'];
                    $totqty = $totqty + $tran['qty'];
                    $totdisc = $totdisc + $tran['discperc'];
                    $totdiscamt = $totdiscamt + $tran['discount'];
                    $totcgst = $totcgst + $tran['cgstamt'];
                    $totsgst = $totsgst + $tran['sgstamt'];
                    $totigst = $totigst + $tran['igstamt'];
                    $dtotcgst = $dtotcgst + $tran['cgstamt'];
                    $dtotsgst = $dtotsgst + $tran['sgstamt'];
                    $dtotigst = $dtotigst + $tran['igstamt'];
                    $total = $total + $tran['total'];
                }
                
                $colr1 = "blue";
                
                $contents .= "Total" . ",";
                $contents .= "$totqty" . ",";
                $contents .= "$totrate" . ",";
                $contents .= "$totamt" . ",";
                $contents .= "$totdisc" . ",";
                $contents .= "$totdiscamt" . ",";
                $contents .= "$tottaxable" . ",";
                $contents .= "" . ",";
                $contents .= "$totcgst" . ",";
                $contents .= "" . ",";
                $contents .= "$totsgst" . ",";
                $contents .= "" . ",";
                $contents .= "$totigst" . ",";
                $contents .= "$total" . "\n";
              
            }
        }


        $colr1 = "orange";

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Detail Total" . ",";
        $contents .= "$ttotqty" . ",";
        $contents .= "$ttotamt" . ",";
        $contents .= "$totitmpr" . ",";
        $contents .= "$ttottaxamt" . ",";
        $contents .= "$ttotalgst" . ",";
        $contents .= "$ttotalamt" . ",";
        $contents .= "" . "\n";

        $colr = "green";


        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= "$ttotqty" . ",";
        $contents .= "$ttotamt" . ",";
        $contents .= "$totitmpr" . ",";
        $contents .= "$ttottaxamt" . ",";
        $contents .= "$ttotalgst" . ",";
        $contents .= "$ttotalamt" . "\n";
     
}
    $contents = strip_tags($contents);
    if ($_POST["dtype"] == 1) {
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        print $contents;
    } else {
        header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
        print $contents;
    }
}

if (isset($_POST["subpdf"])) {
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));

    require("library/fpdf/fpdf.php");

    class pdf extends fpdf {

        function Footer() {



//Position at 1.5 cm from bottom

            $this->SetY(-15);

//Arial italic 8

            $this->SetFont('Arial', 'I', 8);

//Page number

            $this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
        }

    }

    $pdf = new pdf('L', 'mm', 'A4');

    $pdf->AliasNbPages();

    $pdf->AddPage();
    include("includes/head_rep_pdf.php");
    if ($namenum == 1) {
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->SetFillColor(64, 37, 91);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->Cell(8, 7, "Sr.", 1, 0, '', 1);
//        $pdf->Cell(10, 7, "Inv.", 1, 0, '', 1);
//        $pdf->Cell(22, 7, "Date", 1, 0, '', 1);
        $pdf->Cell(70, 7, "Party", 1, 0, '', 1);
        $pdf->Cell(30, 7, "Mobile", 1, 0, '', 1);
//        $pdf->Cell(25, 7, "Bill.Amt", 1, 0, 'R', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();
        $ctr = 0;


        while ($pmain = mysqli_fetch_array($getpmain)) {
            $getsupp = mysqli_query($conn, "SELECT `accode`,`name`,mobile FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
            $supp = mysqli_fetch_array($getsupp);

            if ($pmain['nameprint'] != '') {
                $namep = $pmain['nameprint'];
            } else {
                $namep = $supp['name'];
            }

            if ($pmain['printmobile'] != '') {
                $mobp = $pmain['printmobile'];
            } else {
                $mobp = $supp['mobile'];
            }

            $ctr++;
            $pdf->SetFont('Arial', '', 8);
            $pdf->Cell(8, 7, $ctr, 1);
//            $pdf->Cell(10, 7, $pmain[invno], 1);
//            $pdf->Cell(22, 7, $pmain[mydt], 1);
            $pdf->Cell(70, 7, $namep, 1);
            $pdf->Cell(30, 7, $mobp, 1);
//            $pdf->Cell(25, 7, $pmain[netamt], 1, 0, 'R');
            $pdf->Ln();

            $totbill = $totbill + $pmain['netamt'];
        }
    } else {
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->SetFillColor(64, 37, 91);
        $pdf->SetTextColor(255, 255, 255);
        $pdf->Cell(8, 7, "Sr.", 1, 0, '', 1);
        $pdf->Cell(8, 7, "Inv.", 1, 0, '', 1);
        $pdf->Cell(18, 7, "Date", 1, 0, '', 1);
        $pdf->Cell(30, 7, "Party", 1, 0, '', 1);
        $pdf->Cell(10, 7, "Print", 1, 0, '', 1);
        $pdf->Cell(25, 7, "Salesman", 1, 0, '', 1);
        $pdf->Cell(15, 7, "Amount", 1, 0, 'R', 1);
        $pdf->Cell(19, 7, "Item Disc", 1, 0, 'R', 1);
        $pdf->Cell(15, 7, "Bill Disc", 1, 0, 'R', 1);
        $pdf->Cell(15, 7, "Packing", 1, 0, 'R', 1);
        $pdf->Cell(15, 7, "Taxable", 1, 0, 'R', 1);
        $pdf->Cell(15, 7, "GST 5%", 1, 0, 'R', 1);
        $pdf->Cell(15, 7, "GST 12%", 1, 0, 'R', 1);
        $pdf->Cell(15, 7, "GST 18%", 1, 0, 'R', 1);
        $pdf->Cell(15, 7, "GST 28%", 1, 0, 'R', 1);
        $pdf->Cell(15, 7, "Bill.Amt", 1, 0, 'R', 1);
        $pdf->Cell(10, 7, "Cash", 1, 0, 'R', 1);
        $pdf->Cell(11, 7, "Cards", 1, 0, 'R', 1);
        $pdf->Cell(11, 7, "Credit", 1, 0, 'R', 1);
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
        $pdf->Ln();

        $ctr = 0;
        $ttotamt = 0;
        $totpack = 0;
        $totbill = 0;
        $totitmpr = 0;
        $totdisc = 0;
        $tottaxamt = 0;
        $totcgst = 0;
        $totsgst = 0;
        $totigst = 0;
        $tcashamt = 0;
        $tccamt = 0;
        $tcredamt = 0;


        while ($pmain = mysqli_fetch_array($getpmain)) {
            $getsupp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
            $supp = mysqli_fetch_array($getsupp);
            $getsale = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[salesman]'")or die(mysqli_error($conn));
            $sale = mysqli_fetch_array($getsale);
            $prnf = '';

            if ($pmain['prnflag'] == '1') {
                $prnf = 'P';
            }
            $ctr++;
            $pdf->SetFont('Arial', '', 8);
            $pdf->Cell(8, 7, $ctr, 1);
            $pdf->Cell(8, 7, $pmain['invno'], 1);
            $pdf->Cell(18, 7, $pmain['mydt'], 1);
            $pdf->Cell(30, 7, $supp['name'], 1);
            $pdf->Cell(10, 7, $prnf, 1);
            $pdf->Cell(25, 7, $sale['name'], 1);
            $pdf->Cell(15, 7, $pmain['subtotal'], 1, 0, 'R');
            $pdf->Cell(19, 7, $pmain['litemdisc'], 1, 0, 'R');
            $pdf->Cell(15, 7, $pmain['discount'], 1, 0, 'R');
            $pdf->Cell(15, 7, $pmain['couamt'], 1, 0, 'R');
            $pdf->Cell(15, 7, $pmain['tottaxamt'], 1, 0, 'R');
            $pdf->Cell(15, 7, $pmain['totgst5'], 1, 0, 'R');
            $pdf->Cell(15, 7, $pmain['totgst12'], 1, 0, 'R');
            $pdf->Cell(15, 7, $pmain['totgst18'], 1, 0, 'R');
            $pdf->Cell(15, 7, $pmain['totgst28'], 1, 0, 'R');
            $pdf->Cell(15, 7, $pmain['netamt'], 1, 0, 'R');
            $pdf->Cell(10, 7, $pmain['cashamt'], 1, 0, 'R');
            $pdf->Cell(11, 7, $pmain['ccamt'], 1, 0, 'R');
            $pdf->Cell(11, 7, $pmain['credamt'], 1, 0, 'R');
            $pdf->Ln();

            $ttotamt = $ttotamt + $pmain['subtotal'];
            $totitmpr = $totitmpr + $pmain['litemdisc'];
            $totdisc = $totdisc + $pmain['discount'];
            $totpack = $totpack + $pmain['couamt'];
            $ttottaxamt = $ttottaxamt + $pmain['tottaxamt'];
            $totgst5 = $totsgst + $pmain['totgst5'];
            $totgst12 = $totcgst + $pmain['totgst12'];
            $totgst18 = $totigst + $pmain['totgst18'];
            $totgst28 = $totigst + $pmain['totgst28'];
            $totbill = $totbill + $pmain['netamt'];
            $tcashamt = $tcashamt + $pmain['cashamt'];
            $tccamt = $tccamt + $pmain['ccamt'];
            $tcredamt = $tcredamt + $pmain['credamt'];

            if ($_POST["dtype"] == 2) {

                $pdf->SetFont('Arial', 'B', 10);
                $pdf->SetFillColor(204, 204, 204);
                $pdf->SetTextColor(255, 255, 255);
                $pdf->Cell(10, 7, "Sr.", 1, 0, '', 1);
                $pdf->Cell(15, 7, "Barcode", 1, 0, '', 1);
                $pdf->Cell(42, 7, "Itemname", 1, 0, '', 1);
                $pdf->Cell(21, 7, "HSN Code", 1, 0, '', 1);
                $pdf->Cell(10, 7, "Qty.", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, "Unit", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, "Mrp", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, "Rate", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, "Amount", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, "Disc%", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, "Disc.Amt.", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, "Taxable", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, "CGST %", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, "CGST Amt.", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, "SGST %", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, "SGST Amt.", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, "Total", 1, 0, 'R', 1);
                $pdf->SetFont('Arial', '', 1, 0);

                $pdf->SetTextColor(0, 0, 0);
                $pdf->Ln();

                $ctr2 = 0;
                $totqty = $totdisc = $totdiscamt = $totcgst = $totsgst = $totigst = $totamt = 0;

                $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' $keycond")or die(mysqli_error($conn));
                while ($tran = mysqli_fetch_array($gettran)) {

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

                    $ctr2++;
                    $pdf->SetFont('Arial', '', 8);
                    $pdf->Cell(10, 7, $ctr2, 1);
                    $pdf->Cell(15, 7, $tran['barcode'], 1);
                    $pdf->Cell(42, 7, $item['name'], 1);
                    $pdf->Cell(21, 7, $tran['hsncode'], 1);
                    $pdf->Cell(10, 7, $tran['qty'], 1, 0, 'R');
                    $pdf->Cell(15, 7, $tran['unit'], 1, 0, 'R');
                    $pdf->Cell(15, 7, $tran['mrp'], 1, 0, 'R');
                    $pdf->Cell(15, 7, $tran['rate'], 1, 0, 'R');
                    $pdf->Cell(15, 7, $tran['amount'], 1, 0, 'R');
                    $pdf->Cell(15, 7, $tran['discperc'], 1, 0, 'R');
                    $pdf->Cell(15, 7, $tran['discount'], 1, 0, 'R');
                    $pdf->Cell(15, 7, $tran['subtot'], 1, 0, 'R');
                    $pdf->Cell(15, 7, $tran['cgstrate'], 1, 0, 'R');
                    $pdf->Cell(15, 7, $tran['cgstamt'], 1, 0, 'R');
                    $pdf->Cell(15, 7, $tran['sgstrate'], 1, 0, 'R');
                    $pdf->Cell(15, 7, $tran['sgstamt'], 1, 0, 'R');
                    $pdf->Cell(15, 7, $tran['total'], 1, 0, 'R');
                    $pdf->Ln();

                    $totamt = $totamt + $tran['amount'];
                    $tottaxable = $tottaxable + $tran['subtot'];
                    $totqty = $totqty + $tran['qty'];
                    $totdisc = $totdisc + $tran['discperc'];
                    $totdiscamt = $totdiscamt + $tran['discount'];
                    $totcgst = $totcgst + $tran['cgstamt'];
                    $totsgst = $totsgst + $tran['sgstamt'];
                    $totigst = $totigst + $tran['igstamt'];
                    $total = $total + $tran['total'];
                }
                $pdf->SetFont('Arial', 'B', 10);
                $pdf->SetFillColor(255, 255, 255);
                $pdf->SetTextColor(0, 128, 0);
                $pdf->Cell(88, 7, "Total", 1, 0, 'R', 1);
                $pdf->Cell(10, 7, $totqty, 1, 0, '', 1);
                $pdf->Cell(15, 7, " ", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, " ", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, " ", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, $totamt, 1, 0, 'R', 1);
                $pdf->Cell(15, 7, $totdisc, 1, 0, 'R', 1);
                $pdf->Cell(15, 7, $totdiscamt, 1, 0, 'R', 1);
                $pdf->Cell(15, 7, $tottaxable, 1, 0, 'R', 1);
                $pdf->Cell(15, 7, " ", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, $totcgst, 1, 0, 'R', 1);
                $pdf->Cell(15, 7, " ", 1, 0, 'R', 1);
                $pdf->Cell(15, 7, $totsgst, 1, 0, 'R', 1);
                $pdf->Cell(15, 7, $total, 1, 0, 'R', 1);
                $pdf->SetFont('Arial', '', 1, 0);

                $pdf->SetTextColor(0, 0, 0);
                $pdf->Ln();
            }
        }
        $pdf->SetFont('Arial', 'B', 10);
        $pdf->SetFillColor(255, 255, 255);
        $pdf->SetTextColor(0, 0, 255);
        $pdf->Cell(99, 7, "Grand Total", 1, 0, 'R', 1);
        $pdf->Cell(15, 7, $ttotamt, 1, 0, 'R', 1);
        $pdf->Cell(19, 7, $totitmpr, 1, 0, 'R', 1);
        $pdf->Cell(15, 7, $totdisc, 1, 0, 'R', 1);
        $pdf->Cell(15, 7, $totpack, 1, 0, 'R', 1);
        $pdf->Cell(15, 7, $ttottaxamt, 1, 0, 'R', 1);
        $pdf->Cell(15, 7, $totgst5, 1, 0, 'R', 1);
        $pdf->Cell(15, 7, $totgst12, 1, 0, 'R', 1);
        $pdf->Cell(15, 7, $totgst18, 1, 0, 'R', 1);
        $pdf->Cell(15, 7, $totgst28, 1, 0, 'R', 1);
        $pdf->Cell(15, 7, $totbill, 1, 0, 'R', 1);
        $pdf->Cell(10, 7, $tcashamt, 1, 0, 'R', 1);
        $pdf->Cell(11, 7, $tccamt, 1, 0, 'R', 1);
        $pdf->Cell(11, 7, $tcredamt, 1, 0, 'R', 1);
        $pdf->Ln();
        $pdf->SetFont('Arial', '', 1, 0);

        $pdf->SetTextColor(0, 0, 0);
    }
    $pdf->Output("Sale.pdf", "D");
}
?>