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

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

$maindb = 'salemain';
$trandb = 'saletran';
$zroot = $_SESSION["zdbroot"];

$formname = 'Sale Register';
$file = 'tisalereg2_fr.php';
$regtype = $_POST["regtype"];

if (isset($_POST["view"]) || ($_GET["view"])) {
    $party = $_POST["party"];
    $p = $_GET["p"];

    $godown = $_POST["godown"];
    $staff = $_POST["staff"];
    $ptype = $_POST["ptype"];
    $dtype = $_POST["dtype"];
    $gsttype = $_POST["gsttype"];

    if (!empty($party)) {
        $partycond = " AND `party` = '$party' ";
        $partycond2 = " AND `partyname` = '$party' ";
    }
    if (!empty($p)) {
        $pcond = " AND `party` = '$p' ";
    }
    if (!empty($godown)) {
        $godowncond = " AND `godown` = '$godown' ";
    }
    if (!empty($staff)) {
        $staffcond = " AND `staff` = '$staff '";
    }
    if (!empty($ptype)) {
        $ptypecond = " AND `paidby`= '$ptype'";
    }
    if (!empty($dtype)) {
        $dtypecond = " AND `dtype`= '$dtype' ";
    }

    if (!empty($gsttype)) {
        if ($gsttype == 1) {
            $gstcond = "AND `oms` = '1'";
        } else {
            $gstcond = "AND `oms` = ''";
        }
    }

    if (($_GET["fdt"]) != '') {
        $mfdt = $_GET["fdt"];
        $mtdt = $_GET["tdt"];
        $mfrmdate = date("y-m-d", strtotime($mfdt));
        $mtodate = date("y-m-d", strtotime($mtdt));
        $dtype = $_GET["rpt"];
    } else {

        $mfrmdate = trim($_POST["frmdt"]);
        $mtodate = trim($_POST["todt"]);

        if (empty($_POST["frmdt"])) {
            browse("From Date Cannot Be Empty");
            header("refresh:1; saleitemgrp.php");
        }
        if (empty($_POST["todt"])) {
            browse("To Date Cannot Be Empty");
            header("refresh:1; saleitemgrp.php");
        }
        $mfdt = date("d-m-Y", strtotime($mfrmdate));
        $mtdt = date("d-m-Y", strtotime($mtodate));
    }

    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$party' AND grpcode='010'  ") or die(mysqli_error($conn));
    $sparty = mysqli_fetch_array($gparty);
    $partyname = $sparty['name'];

    //    $get = mysqli_num_rows($getpmain);

    include("includes/linkshead.php");
    ?>
    <script>
        $(document).ready(function () {
            $('#create_excel').click(function () {
                var excel_data = $('.tablerep').html();
                var page = "excellib.php?data=" + excel_data.trim();
                console.log(page);
                var data = {excel_data: excel_data};
                window.open('data:application/vnd.ms-excel,' + encodeURIComponent(excel_data));
            });
        });
    </script>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }

        th {
            background: #26A69A;
        }

        .dth {
            background: #BABFC2;
        }

        .colr {
            background: #26A69A;
            font-size: 17px;
        }

        table tr td {
            font-size: 14px;
        }        

        .bold > td{
            font-weight:bold ;
            font-size: 17px;
        }
    </style>
    </head>

    <body>
        <div class="tablerep">
            <?
            if ($dtype == 3) {
                $tradesale = mysqli_query($conn, "SELECT SUM(grossamt)as amt,SUM(discamt)AS discount,SUM(taxableamt) as taxable, 
                                      SUM(totgst5+totgst12+totgst18+totgst28) AS totgst, SUM(billamt) AS netamt, sum(postamt) as post
                                      from salemain WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and canc_flag != 'C'  ") or die(mysqli_error($conn));
                $ftradesale = mysqli_fetch_array($tradesale);

                
                $saleexmp = mysqli_query($conn, "SELECT SUM(amount)as amt, SUM(discamt)AS discount,SUM(subtot) as taxable, 
                                      SUM(cgstamt+sgstamt+igstamt) AS totgst ,SUM(total) AS netamt
                                      from saletran WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and canc != 'C' and 
                                      gstperc = 0 ") or die(mysqli_error($conn));
                $fsaleexmp = mysqli_fetch_array($saleexmp);

                
                $nonexmp = mysqli_query($conn, "SELECT SUM(amount)as amt, SUM(discamt)AS discount,SUM(subtot) as taxable, 
                                      SUM(cgstamt+sgstamt+igstamt) AS totgst ,SUM(total) AS netamt
                                      from saletran WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and canc != 'C' and 
                                      gstperc > 0 ") or die(mysqli_error($conn));
                $fnonexmp = mysqli_fetch_array($nonexmp);

                
                $deemsale = mysqli_query($conn, "SELECT SUM(grossamt)as amt,SUM(discamt)AS discount,SUM(taxableamt) as taxable, 
                                     SUM(totgst1) AS totgst ,SUM(billamt) AS netamt,  sum(postamt) as post
                                     from deexmain WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and canc_flag != 'C' ") or die(mysqli_error($conn));
                $fdeemsale = mysqli_fetch_array($deemsale);

                
                $exportsale = mysqli_query($conn, "SELECT SUM(grossamt * roe) as amt, SUM(taxableamt * roe) as taxable, SUM(inramt) AS netamt
                                     from salemainexp WHERE `invdate` >= '$mfrmdate' AND `invdate` <= '$mtodate' and canc_flag != 'C' ") or die(mysqli_error($conn));
                $fexportsale = mysqli_fetch_array($exportsale);

                
                $saleother = mysqli_query($conn, "SELECT SUM(grossamt) as amt, SUM(discount) AS discount, SUM(taxableamt) as taxable,
                                      SUM(cgstamt+sgstamt+igstamt) AS totgst ,SUM(billamt) AS netamt 
                                       from slotmain WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
                $fsaleother = mysqli_fetch_array($saleother);

                
                $creditnoteg = mysqli_query($conn, "SELECT SUM(grossamt)as amt,SUM(discamt)AS discount, SUM(taxableamt) as taxable, 
                                      SUM(totgst5+totgst12+totgst18+totgst28) AS totgst ,SUM(billamt) AS netamt  
                                      from credmain WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and credtype = 'G' ") or die(mysqli_error($conn));
                $fcreditnoteg = mysqli_fetch_array($creditnoteg);
                
                
                $creditnoteo = mysqli_query($conn, "SELECT SUM(grossamt)as amt,SUM(discamt)AS discount,SUM(taxableamt) as taxable, 
                                        SUM(cgstamt+sgstamt+igstamt) AS totgst ,SUM(billamt) AS netamt from credmain WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' and credtype = 'O' ") or die(mysqli_error($conn));
                $fcreditnoteo = mysqli_fetch_array($creditnoteo);


                $totamt = $ftradesale['amt'] + $fdeemsale['amt'] + $fexportsale['amt'] + $fsaleother['amt'];
                $totdiscamt = $ftradesale['discount'] + $fdeemsale['discount'] + $fsaleother['discount'];
                $totpostamt = $ftradesale['post'] + $fdeemsale['post'];
                $tottaxble = $ftradesale['taxable'] + $fdeemsale['taxable'] + $fexportsale['taxable'] + $fsaleother['taxable'];
                $totgst = $ftradesale['totgst'] + $fdeemsale['totgst'] + $fsaleother['totgst'];
                $totnetamt = $ftradesale['netamt'] + $fdeemsale['netamt'] + $fexportsale['netamt'] + $fsaleother['netamt'];

                $totretamt = $fcreditnoteg['amt'] + $fcreditnoteo['amt'];
                $totretdiscamt = $fcreditnoteg['discount'] + $fcreditnoteo['discoubt'];
                $totrettaxableamt = $fcreditnoteg['taxable'] + $fcreditnoteo['taxable'];
                $totretgst = $fcreditnoteg['totgst'] + $fcreditnoteo['totgst'];
                $totretnetamt = $fcreditnoteg['netamt'] + $fcreditnoteo['netamt'];

                $grtotamt = $totamt - $totretamt;
                $grtotdisc = $totdiscamt - $totretdiscamt;
                $grtottaxable = $tottaxble - $totrettaxableamt;
                $grtotgst = $totgst - $totretgst;
                $grtotnetamt = $totnetamt - $totretnetamt;
                ?>
                <div class="container" style="width:60%">
                    <?php
                    if ($dtype == 3) {
                        ?>
                        <div class="col-lg-12 pad text-right">
                            <button id="create_excel" name="create_excel" style="color: #26A69A;background: white; border: 1px black solid" type="button" class="btn btn-danger"><span style="text-align: right;">&nbsp;&nbsp;Excel&nbsp;&nbsp;</span></button>
                        </div>
                        
                        <?
                    }
                        ?>
                        <?php include("includes/head_rep2.php"); ?>
                        <div class="row">
                            <table style="background: white;width: 100%" class="table table-condensed table-striped table-bordered">
                                <tr>
                                    <th width="15%" class='text-right'></th>
                                    <th width="10%" class='text-right'>Amount</th>
                                    <th width="10%" class='text-right'>Discount Amt.</th>
                                    <th width="10%" class='text-right'>Postage</th>
                                    <th width="15%" class='text-right'>Taxable Amt.</th>
                                    <th width="15%" class='text-right'>GST</th>
                                    <th width="15%" class='text-right'>Net Amt.</th>
                                </tr>
                                <tr>
                                    <td>Local Sale</td>
                                    <td class='text-right'><?= $ftradesale['amt']; ?></td>
                                    <td class='text-right'><?= $ftradesale['discount'] ?></td>
                                    <td class='text-right'><?= $ftradesale['post'] ?></td>
                                    <td class='text-right'><?= $ftradesale['taxable'] ?></td>
                                    <td class='text-right'><?= $ftradesale['totgst'] ?></td>
                                    <td class='text-right'><?= $ftradesale['netamt'] ?></td>
                                </tr>
                                <tr>
                                    <td style='font-size:11px; font-style: italic; color: blue ' >Local Sale - Exempted</td>
                                    <td class='text-right'></td>
                                    <td class='text-right'></td>
                                    <td class='text-right'></td>
                                    <td class='text-right' style='font-size:11px; font-style: italic; color: blue '><?= $fsaleexmp['taxable'] ?></td>
                                    <td class='text-right' style='font-size:11px; font-style: italic; color: blue '><?= $fsaleexmp['totgst'] ?></td>
                                    <td class='text-right' style='font-size:11px; font-style: italic; color: blue '><?= $fsaleexmp['netamt'] ?></td>
                                </tr>
                                <tr>
                                    <td style='font-size:11px; font-style: italic; color: blue'>Local Sale - Non Exempted</td>
                                    <td class='text-right'></td>
                                    <td class='text-right'></td>
                                    <td class='text-right'></td>
                                    <td class='text-right' style='font-size:11px; font-style: italic; color: blue '><?= $fnonexmp['taxable'] ?></td>
                                    <td class='text-right' style='font-size:11px; font-style: italic; color: blue '><?= $fnonexmp['totgst'] ?></td>
                                    <td class='text-right' style='font-size:11px; font-style: italic; color: blue '><?= $fnonexmp['netamt'] ?></td>
                                </tr>
                                <tr>
                                    <td>Deemed Sale</td>
                                    <td class='text-right'><?= $fdeemsale['amt'] ?></td>
                                    <td class='text-right'><?= $fdeemsale['discount']; ?></td>
                                    <td class='text-right'><?= $fdeemsale['post']; ?></td>
                                    <td class='text-right'><?= $fdeemsale['taxable']; ?></td>
                                    <td class='text-right'><?= $fdeemsale['totgst']; ?></td>
                                    <td class='text-right'><?= $fdeemsale['netamt']; ?></td>
                                </tr>
                                <tr>
                                <tr>
                                    <td>Export Sale</td>
                                    <td class='text-right'><?= $fexportsale['amt']; ?></td>
                                    <td class='text-right'><? ?></td>
                                    <td class='text-right'><? ?></td>
                                    <td class='text-right'><?= $fexportsale['taxable']; ?></td>
                                    <td class='text-right'><? ?></td>
                                    <td class='text-right'><?= $fexportsale['netamt']; ?></td>
                                </tr>
                                <tr>
                                    <td>Sale Other</td>
                                    <td class='text-right'><?= $fsaleother['amt']; ?></td>
                                    <td class='text-right'><?= $fsaleother['discamt'] ?></td>
                                    <td class='text-right'></td>
                                    <td class='text-right'><?= $fsaleother['taxable']; ?></td>
                                    <td class='text-right'><?= $fsaleother['totgst'] ?></td>
                                    <td class='text-right'><?= $fsaleother['netamt']; ?></td>
                                </tr>
                                <tr class="bold" style="color:#26A69A">
                                    <td><b>Total Sale</b></td>
                                    <td class='text-right'><b><?= $totamt; ?></b></td>
                                    <td class='text-right'><b><?= $totdiscamt ?></b></b></td>
                                    <td class='text-right'><b><?= $totpostamt ?></b></b></td>
                                    <td class='text-right'><b><?= $tottaxble; ?></b></td>
                                    <td class='text-right'><b><?= $totgst ?></b></td>
                                    <td class='text-right'><b><?= $totnetamt; ?></b></td>
                                </tr>
                                <tr>
                                    <td colspan='7'><h4>RETURNS</h4></td>
                                </tr>
                                <tr style="background-color:#26A69A">
                                    <th class='text-right'></th>
                                    <th class='text-right'>Amount</th>
                                    <th class='text-right' >Discount</th>
                                    <th class='text-right' >Postage</th>
                                    <th class='text-right' >Taxable Amount</th>
                                    <th class='text-right'>GST</th>
                                    <th class='text-right'>Bill Amt.</th>
                                </tr>
                                <tr>
                                    <td>Goods Return</td>
                                    <td class='text-right'><?= $fcreditnoteg['amt'] ?></td>
                                    <td class='text-right' ><?= $fcreditnoteg['discount'] ?></td>
                                    <td class='text-right' ></td>
                                    <td class='text-right' ><?= $fcreditnoteg['taxable'] ?></td>
                                    <td class='text-right'><?= $fcreditnoteg['totgst'] ?></td>
                                    <td class='text-right'><?= $fcreditnoteg['netamt'] ?></td>
                                </tr>
                                <tr>
                                    <td>Others</td>
                                    <td class='text-right'><?= $fcreditnoteo['amt'] ?></td>
                                    <td class='text-right' ><?= $fcreditnoteo['discount'] ?></td>
                                    <td class='text-right'></td>
                                    <td class='text-right'><?= $fcreditnoteo['taxable'] ?></td>
                                    <td class='text-right'><?= $fcreditnoteo['totgst'] ?></td>
                                    <td class='text-right'><?= $fcreditnoteo['netamt'] ?></td>
                                </tr>
                                <tr class="bold" style="color:red">
                                    <td class="f17">Total Return Sale</td>
                                    <td class='text-right'><?= $totretamt; ?></td>
                                    <td class='text-right' ><?= $totretdiscamt; ?></td>
                                    <td class='text-right'></td>
                                    <td class='text-right'><?= $totrettaxableamt; ?></td>
                                    <td class='text-right'><?= $totretgst; ?></td>
                                    <td class='text-right'><?= $totretnetamt; ?></td>
                                </tr>
                            </table>
                            <br>
                            <table class="table table-bordered">
                                <tr style="color:blue" class="bold">
                                    <td style="width:15%">Total</td>
                                    <td style="width:10%" class='text-right'><?= $grtotamt; ?></td>
                                    <td style="width:10%" class='text-right'><?= $grtotdisc; ?></td>
                                    <td style="width:10%" class='text-right'><?= $totpostamt; ?></td>
                                    <td style="width:15%;color: #ff00cc;" class='text-right' ><?= $grtottaxable; ?></td>
                                    <td style="width:15%" class='text-right'><?= $grtotgst; ?></td>
                                    <td style="width:15%" class='text-right'><?= $grtotnetamt; ?></td>
                                </tr>
                            </table>
                        </div>
                    </div>


                <? } else {
                    ?>
                    <div class="container-fluid">
                        <?php include("includes/head_rep2.php"); ?>
                        <div class="row">

                            <?
                            if ($regtype == '' || $regtype == 'T') {
                                $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                                         AND `vouchdt` <= '$mtodate' $partycond $godowncond $staffcond $ptypecond $gstcond $pcond") or die(mysqli_error($conn));

                                $getno = mysqli_num_rows($getpmain);
                                if ($getno > 0) {
                                    ?>

                                    <table class="table table-condesed table-bordered">
                                        <tr>
                                            <th width="100%" class="h4" style="color:white"><b>Trading Inventory - Sale Register</b></th>
                                        </tr>
                                        <tr>
                                            <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                                Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno; ?></b>
                                            </td>
                                        </tr>
                                    </table>
                                    <table class="table table-condesed table-bordered">
                                        <?php if (!empty($party)) { ?>
                                            <tr>
                                                <th width="4%" class="h4" style="color:white"><b>Party</b></th>
                                                <th width="96%" class="h4" style="color:white"><b><?php echo $partyname; ?></b></th>
                                            </tr>
                                        <? } ?>

                                    </table>


                                    <table style="background: white;width: 100%" class="table table-condensed table-striped table-bordered">
                                        <thead class="mySearchTr">
                                            <tr>
                                                <!--<th style="width:4%;">Sr</th>-->
                                                <th style="width:6%;">Voucher</th>
                                                <th style="width:6%;">Invoice No.</th>
                                                <th style="width:8%;">Date</th>
                                                <!--<th style="width:6%;">Godown</th>-->
                                                <?php if (empty($party)) { ?>
                                                    <th style="width:25%;">Party Name</th>
                                                <? } ?>
                                                <th style="width:6%;">GST No.</th>
                                                <!--<th style="width:6%;">Staff</th>-->
                                                <th style="width:6%" class="text-right">Amount</th>
                                                <th style="width:5%" class="text-right">Disc.</th>
                                                <th style="width:5%" class="text-right">Postage</th>
                                                <th style="width:6%" class="text-right">Taxable</th>
                                                <th style="text-align:right; width:5%;">GST 5%</th>
                                                <th style="text-align:right; width:5%;">GST 12%</th>
                                                <th style="text-align:right; width:5%;">GST 18%</th>
                                                <th style="text-align:right; width:5%;">GST 28%</th>
                                                <th style="width:8%" class="text-right">Bill Amount</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $ctr = 0;
                                            $totamt = 0;
                                            $totdiscamt = 0;
                                            $grtotpost = 0;
                                            $tottaxable = 0;
                                            $totgst5 = 0;
                                            $totgst12 = 0;
                                            $totgst18 = 0;
                                            $totgst28 = 0;
                                            $totpackamt = $totstamt = $totothamt = $totbillamt = 0;

                                            if ($gsttype == '') {
                                                $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' $partycond $godowncond $staffcond $ptypecond $pcond order by invno") or die(mysqli_error($conn));
                                            } else {
                                                $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                                         AND `vouchdt` <= '$mtodate' $partycond $godowncond $staffcond $ptypecond $gstcond $pcond order by invno") or die(mysqli_error($conn));
                                            }


                                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                                if ($pmain['paidby'] == "C") {
                                                    $ptype = "CASH";
                                                } else {
                                                    $ptype = "CREDIT";
                                                }

                                                if ($pmain['canc_flag'] == "C") {
                                                    $partynm = "CANCELLED";
                                                    $color = "red";
                                                    $subtot = $totdisc = $post = $taxable = $gst5 = $gst12 = $gst18 = $gst28 = $billamt = 0;
                                                    $gstno = "";
                                                } else if ($pmain['canc_flag'] != "C") {

                                                    $godown = $pmain['godown'];
                                                    $customer = $pmain['party'];
                                                    $staf = $pmain['staff'];

                                                    $gstaf = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$staf' AND grpcode='040'  ") or die(mysqli_error($conn));
                                                    $sstaf = mysqli_fetch_array($gstaf);

                                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$customer' AND grpcode='010'  ") or die(mysqli_error($conn));
                                                    $sparty = mysqli_fetch_array($gparty);

                                                    $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$godown'") or die(mysqli_error($conn));
                                                    $sgodw = mysqli_fetch_array($ggodw);

                                                    $gst0 = 0;

                                                    $voucher = $pmain['voucher'];
                                                    $lslvouchno = $pmain['vouchno'];
                                                    $invno = $pmain['invno'];
                                                    $mydt = $pmain['mydt'];
                                                    $gcode = $sgodw['gcode'];
                                                    $partynm = $sparty['name'];
                                                    $staff = $sstaf['name'];
                                                    $gstno = $pmain['gstno'];

                                                    $gst5 = $pmain['totgst5'];
                                                    $gst12 = $pmain['totgst12'];
                                                    $gst18 = $pmain['totgst18'];
                                                    $gst28 = $pmain['totgst28'];
                                                    $billamt = $pmain['billamt'];
                                                    $subtot = $pmain['grossamt'];
                                                    $totdisc = $pmain['discamt'];
                                                    $post = $pmain['postamt'];
                                                    $taxable = $pmain['taxableamt'];

//                                                if (round($taxable) == round($billamt)) {
//                                                   $gst0 = round($taxable) ;
//                                                }
                                                }

                                                $ctr++;
                                                echo "<tr data-href='sale_ti.php?mode=edit&vouchno=$lslvouchno' class='clickable-row2' [target]='_blank'>";
                                                echo "<td>$voucher</td>";
                                                echo "<td>L$invno </td>";
                                                echo "<td>$mydt</td>";
                                                if (empty($party)) {
                                                    echo "<td>$partynm</td>";
                                                }
                                                echo "<td style='font-size:12px'>$gstno</td>";
//                                    echo "<td>$staff</td>";
                                                echo "<td class='text-right'>$subtot</td>";
                                                echo "<td class='text-right'>$totdisc</td>";
                                                echo "<td class='text-right'>$post</td>";
                                                echo "<td class='text-right'>$taxable</td>";
//                                            echo "<td class='text-right'>$gst0</td>";
                                                echo "<td class='text-right'>$gst5</td>";
                                                echo "<td class='text-right'>$gst12</td>";
                                                echo "<td class='text-right'>$gst18</td>";
                                                echo "<td class='text-right'>$gst28</td>";
                                                echo "<td class='text-right'>$billamt</td>";
                                                echo "</tr>";


                                                $grsubtot = $grsubtot + $subtot;
                                                $grtotdis = $grtotdis + $totdisc;
                                                $grtotpost = $grtotpost + $post;
                                                $grtottaxable = $grtottaxable + $taxable;
//                                            $grtotgst0 = $grtotgst0 + $gst0;
                                                $grtotgst5 = $grtotgst5 + $gst5;
                                                $grtotgst12 = $grtotgst12 + $gst12;
                                                $grtotgst18 = $grtotgst18 + $gst18;
                                                $grtotgst28 = $grtotgst28 + $gst28;
                                                $grtotbillamt = $grtotbillamt + $billamt;

                                                $totpackamt = $totpackamt + $pmain['packamt'];
                                                $totstamt = $totstamt + $pmain['stamt'];
                                                $totothamt = $totothamt + $pmain['otheramt'];
                                                $totbillamt = $totbillamt + $pmain['billamt'];

                                                if ($pmain['canc_flag'] == 1) {
                                                    
                                                } else {


                                                    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 "<tr class='mySearchTr2'>";
                                                        echo "<th style='width:5%;' class='dth'>Sr</th>";
                                                        echo "<th style='width:7%;' class='dth'>Barcode</th>";
//                                            echo "<th style='width:7%;' class='dth'>Product Code</th>";
                                                        echo "<th style='width:17%;' class='dth'>Product Name</th>";
                                                        echo "<th style='width:6%;' class='dth'>HSN Code</th>";
                                                        echo "<th style='width:5%;' class='dth'>Gram</th>";
                                                        echo "<th style='width:5%;' class='dth'>Unit</th>";
                                                        echo "<th style='width:5%;' class='dth text-right'>Mrp</th>";
                                                        echo "<th style='width:4%;' class='dth'>Batch</th>";
                                                        echo "<th style='width:5%;' class='dth text-right'>Qty.</th>";
                                                        echo "<th style='width:5%;' class='dth text-right'>Rate</th>";
                                                        echo "<th style='width:5%;' class='dth text-right'>Amount</th>";
                                                        echo "<th style='width:5%;' class='dth text-right'>Disc%</th>";
                                                        echo "<th style='width:6%;' class='dth text-right'>Disc. Amt.</th>";
                                                        echo "<th style='width:5%;' class='dth text-right'>Taxable Amount</th>";
                                                        if ($gsttype == 1) {
                                                            echo "<th style='width:4%;' class='dth'>IGST Rate</th>";
                                                            echo "<th style='width:5%;' class='dth'>IGST Amount</th>";
                                                        } elseif ($gsttype == 2) {
                                                            echo "<th style='width:4%;' class='dth'>CGST Rate</th>";
                                                            echo "<th style='width:5%;' class='dth'>CGST Amount</th>";
                                                            echo "<th style='width:4%;' class='dth'>SGST Rate</th>";
                                                            echo "<th style='width:5%;' class='dth'>SGST Amount</th>";
                                                        } else {
                                                            echo "<th style='width:5%;' class='dth'>IGST Rate</th>";
                                                            echo "<th style='width:5%;' class='dth'>IGST Amount</th>";
                                                            echo "<th style='width:5%;' class='dth'>CGST Rate</th>";
                                                            echo "<th style='width:5%;' class='dth'>CGST Amount</th>";
                                                            echo "<th style='width:5%;' class='dth'>SGST Rate</th>";
                                                            echo "<th style='width:5%;' class='dth'>SGST Amount</th>";
                                                        }
                                                        echo "<th style='width:7%;' class='text-right dth'>Total</th>";
                                                        echo "</tr>";

                                                        echo "<tr>";

                                                        $ctr2 = 0;
                                                        $totqty = 0;
                                                        $totrate = 0;
                                                        $trtotal = 0;
                                                        $totamount = $totdiscount = $totsubtot = $totcgstamt = $totsgstamt = $totigstamt2 = 0;

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

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



                                                            $barcode = $tran['barcode'];
                                                            $itemcode = $tran['itemcode'];
//                                                $itemname = $tran['itemname'];
                                                            $hsncode = $tran['hsncode'];
                                                            $gram = $tran['gram'];
                                                            $unit = $tran['unit'];
                                                            $mrp = $tran['mrp'];
                                                            $batch = $tran['batch'];
                                                            $qty = $tran['qty'];
                                                            $rate = $tran['rate'];
                                                            $amount = $tran['amount'];
                                                            $discperc = $tran['discperc'];
                                                            $discamt = $tran['discamt'];
                                                            $subtot = $tran['subtot'];
                                                            $igstrate = $tran['igstrate'];
                                                            $igstamt = $tran['igstamt'];
                                                            $sgstrate = $tran['sgstrate'];
                                                            $sgstamt = $tran['sgstamt'];
                                                            $cgstrate = $tran['cgstrate'];
                                                            $cgstamt = $tran['cgstamt'];
                                                            $total = $tran['total'];


                                                            $gitem = mysqli_query($conn, "SELECT  name FROM product WHERE prodcode = '$itemcode' ")or die(mysqli_error($conn));
                                                            $fitem = mysqli_fetch_array($gitem);
                                                            $itemname = $fitem['name'];

                                                            $ctr2++;
                                                            echo "<tr>";
                                                            echo "<td>$ctr2</td>";
//                                                echo "<td>$barcode</td>";
                                                            echo "<td>$itemcode</td>";
                                                            echo "<td style='font-size:11px'>$itemname</td>";
                                                            echo "<td>$hsncode</td>";
                                                            echo "<td>$gram</td>";
                                                            echo "<td>$unit</td>";
                                                            echo "<td class='text-right'>$mrp</td>";
                                                            echo "<td>$batch</td>";
                                                            echo "<td class='text-right'>$qty</td>";
                                                            echo "<td class='text-right'>$rate</td>";
                                                            echo "<td class='text-right'>$amount</td>";
                                                            echo "<td class='text-right'>$discperc</td>";
                                                            echo "<td class='text-right'>$discamt</td>";
                                                            echo "<td class='text-right'>$subtot</td>";
                                                            if ($gsttype == 1) {
                                                                echo "<td class='text-right'>$igstrate</td>";
                                                                echo "<td class='text-right'>$igstamt</td>";
                                                            } elseif ($gsttype == 2) {
                                                                echo "<td class='text-right'>$cgstrate</td>";
                                                                echo "<td class='text-right'>$cgstamt</td>";
                                                                echo "<td class='text-right'>$sgstrate</td>";
                                                                echo "<td class='text-right'>$sgstamt</td>";
                                                            } else {
                                                                echo "<td class='text-right'>$igstrate</td>";
                                                                echo "<td class='text-right'>$igstamt</td>";
                                                                echo "<td class='text-right'>$cgstrate</td>";
                                                                echo "<td class='text-right'>$cgstamt</td>";
                                                                echo "<td class='text-right'>$sgstrate</td>";
                                                                echo "<td class='text-right'>$sgstamt</td>";
                                                            }
                                                            echo "<td class='text-right'>$total</td>";
                                                            echo "</tr>";

                                                            $totqty = $totqty + $tran['qty'];
                                                            $trtotal = $trtotal + $total;
                                                            $totamount = $totamount + $tran['amount'];
                                                            $totdiscount = $totdiscount + $tran['discamt'];
                                                            $totsubtot = $totsubtot + $tran['subtot'];
                                                            $totcgstamt = $totcgstamt + $tran['cgstamt'];
                                                            $totsgstamt = $totsgstamt + $tran['sgstamt'];
                                                            $totigstamt2 = $totigstamt2 + $tran['igstamt'];
                                                        }

                                                        echo "<tr>";
                                                        echo "<td class='dth'></td>";
//                                            echo "<td class='dth'></td>";
                                                        echo "<td class='dth'></td>";
                                                        echo "<td class='dth'></td>";
                                                        echo "<td class='dth'></td>";
                                                        echo "<td class='dth'></td>";
                                                        echo "<td class='dth'></td>";
                                                        echo "<td class='dth'></td>";
                                                        echo "<td class='text-right dth'>Total</td>";
                                                        echo "<td class='text-right dth'>$totqty</td>";
                                                        echo "<td class='text-right dth'></td>";
                                                        echo "<td class='text-right dth'>$totamount</td>";
                                                        echo "<td class='text-right dth'></td>";
                                                        echo "<td class='text-right dth'>$totdiscount</td>";
                                                        echo "<td class='text-right dth'>$totsubtot</td>";
                                                        if ($gsttype == 1) {
                                                            echo "<td class='text-right dth'></td>";
                                                            echo "<td class='text-right dth'>$totigstamt2</td>";
                                                        } elseif ($gsttype == 2) {
                                                            echo "<td class='text-right dth'></td>";
                                                            echo "<td class='text-right dth'>$totcgstamt</td>";
                                                            echo "<td class='text-right dth'></td>";
                                                            echo "<td class='text-right dth'>$totsgstamt</td>";
                                                        } else {
                                                            echo "<td class='text-right dth'></td>";
                                                            echo "<td class='text-right dth'>$totigstamt2</td>";
                                                            echo "<td class='text-right dth'></td>";
                                                            echo "<td class='text-right dth'>$totcgstamt</td>";
                                                            echo "<td class='text-right dth'></td>";
                                                            echo "<td class='text-right dth'>$totsgstamt</td>";
                                                        }
                                                        echo "<td class='text-right dth'>$trtotal</td>";
                                                        echo "</tr>";
                                                        echo "</table>";
                                                    }
                                                }
                                            }
                                            echo "<tr>";
                                            if (empty($party)) {
                                                echo "<td class='text-right colr' colspan=''><b></b></td>";
                                            }
                                            echo "<td class='text-right colr' colspan='4'><b>Total</b></td>";
                                            echo "<td class='numeric text-right colr '><b>$grsubtot</b></td>";
                                            echo "<td class='numeric text-right colr '><b>$grtotdis</b></td>";
                                            echo "<td class='numeric text-right colr '><b>$grtotpost</b></td>";
                                            echo "<td class='numeric text-right colr '><b>$grtottaxable</b></td>";
//                                        echo "<td class='numeric text-right colr '><b>$grtotgst0</b></td>";
                                            echo "<td class='numeric text-right colr '><b>$grtotgst5</b></td>";
                                            echo "<td class='numeric text-right colr '><b>$grtotgst12</b></td>";
                                            echo "<td class='numeric text-right colr '><b>$grtotgst18</b></td>";
                                            echo "<td class='numeric text-right colr '><b>$grtotgst28</b></td>";
                                            echo "<td class='numeric text-right colr '><b>$grtotbillamt</b></td>";
                                            echo "</tr>";
                                            ?>
                                        </tbody>
                                    </table>

                                    <?php
                                }
                            }
                            if ($regtype == '' || $regtype == 'D') {

                                $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `deexmain` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' $partycond $godowncond $staffcond $ptypecond $gstcond $pcond") or die(mysqli_error($conn));

                                $getno2 = mysqli_num_rows($getpmain);
                                if ($getno2 > 0) {
                                    ?>

                                    <!--deemsalereg-->


                                    <table class="table table-condesed table-bordered">
                                        <tr>
                                            <th width="100%" class="h4" style="color:white"><b>DeemSale Register</b></th>
                                        </tr>
                                        <tr>
                                            <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                                Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno2; ?></b>
                                            </td>
                                        </tr>
                                    </table>
                                    <table class="table table-condesed table-bordered">
                                        <?php if (!empty($party)) { ?>
                                            <tr>
                                                <th width="4%" class="h4" style="color:white"><b>Party</b></th>
                                                <th width="96%" class="h4" style="color:white"><b><?php echo $partyname; ?></b></th>
                                            </tr>
                                        <? } ?>

                                    </table>


                                    <table style="background: white;width: 100%" class="table table-condensed table-striped  table-bordered">
                                        <thead class="mySearchTr">
                                            <tr>
                                                <!--<th style="width:4%;">Sr</th>-->
                                                <th style="width:5%;">Voucher</th>
                                                <th style="width:5%;">Invoice</th>
                                                <th style="width:9%;">Date</th>
                                                <!--<th style="width:6%;">Godown</th>-->
                                                <?php if (empty($party)) { ?>
                                                    <th style="width:23%;">Party Name</th>
                                                <? } ?>
                                                <th style="width:12%;">GST No.</th>
                                                <!--<th style="width:6%;">Staff</th>-->
                                                <th style="width:7%" class="text-right">Amount</th>
                                                <th style="width:7%" class="text-right">Disc Amt.</th>
                                                <th style="width:7%" class="text-right">Taxable</th>
                                                <!--<th style="text-align:right; width:6%;">IGST Rate</th>-->
                                                <th style="text-align:right; width:6%;">IGST Amt.</th>
                                                <th style="width:9%" class="text-right">Bill Amount</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $ctr = 0;
                                            $totamt = 0;
                                            $totdiscamt = 0;
                                            $tottaxable = 0;
                                            $totgst1 = 0;
                                            $totpackamt = $totstamt = $totothamt = $totbillamt2 = 0;
                                            $grtotbillamt2 = 0;
                                            $grsubtot2 = 0;
                                            $grtottaxable2 = 0;
                                            if ($gsttype == '') {
                                                $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `deexmain` WHERE `vouchdt` >= '$mfrmdate' 
                                         AND `vouchdt` <= '$mtodate' $partycond $godowncond $staffcond $ptypecond $pcond") or die(mysqli_error($conn));
                                            } else {
                                                $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `deexmain` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' $partycond $godowncond $staffcond $ptypecond $gstcond $pcond") or die(mysqli_error($conn));
                                            }
                                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                                if ($pmain['paidby'] == "C") {
                                                    $ptype = "CASH";
                                                } else {
                                                    $ptype = "CREDIT";
                                                }


                                                if ($pmain['canc_flag'] == "C") {

                                                    $voucher = $pmain['voucher'];
                                                    $deslvouchno = $pmain['vouchno'];
                                                    $invno = $pmain['invno'];
                                                    $mydt = $pmain['mydt'];

                                                    $partynm = "CANCELLED";
                                                    $gstno = "";
                                                    $billamt = $subtot = $totdisc = $taxable = 0;
                                                } else if ($pmain['canc_flag'] != "C") {

                                                    $godown = $pmain['godown'];
                                                    $customer = $pmain['party'];
                                                    $staf = $pmain['staff'];

                                                    $gstaf = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$staf' AND grpcode='040'  ") or die(mysqli_error($conn));
                                                    $sstaf = mysqli_fetch_array($gstaf);

                                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$customer' AND grpcode='010'  ") or die(mysqli_error($conn));
                                                    $sparty = mysqli_fetch_array($gparty);

                                                    $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$godown'") or die(mysqli_error($conn));
                                                    $sgodw = mysqli_fetch_array($ggodw);


                                                    $voucher = $pmain['voucher'];
                                                    $deslvouchno = $pmain['vouchno'];
                                                    $invno = $pmain['invno'];
                                                    $mydt = $pmain['mydt'];
                                                    $gcode = $sgodw['gcode'];
                                                    $partynm = $sparty['name'];
                                                    $staff = $sstaf['name'];
                                                    $gstno = $pmain['gstno'];
                                                    $gstperc = $pmain['igst1'];
                                                    $totgst = $pmain['totgst1'];
                                                    $billamt = $pmain['billamt'];
                                                    $subtot = $pmain['grossamt'];
                                                    $totdisc = $pmain['discamt'];
                                                    $taxable = $pmain['taxableamt'];
                                                }
                                                $ctr++;
                                                echo "<tr data-href='sale_de.php?mode=edit&vouchno=$deslvouchno' class='clickable-row2' [target]='_blank'>";
                                                echo "<td>$voucher</td>";
                                                echo "<td>DE$invno</td>";
                                                echo "<td>$mydt</td>";
                                                if (empty($party)) {

                                                    echo "<td>$partynm</td>";
                                                }
                                                echo "<td>$gstno</td>";
                                                echo "<td class='text-right'>$subtot</td>";
                                                echo "<td class='text-right'>$totdisc</td>";
                                                echo "<td class='text-right'>$taxable</td>";
//                                        echo "<td class='text-right'>$gstperc</td>";
                                                echo "<td class='text-right'>$totgst</td>";
                                                echo "<td class='text-right'>$billamt</td>";
                                                echo "</tr>";



                                                $grsubtot2 = $grsubtot2 + $subtot;
                                                $grtotdis2 = $grtotdis2 + $totdisc;
                                                $grtottaxable2 = $grtottaxable2 + $taxable;
                                                $grtotgst1 = $grtotgst1 + $totgst;
                                                $grtotbillamt2 = $grtotbillamt2 + $billamt;

                                                $totpackamt = $totpackamt + $pmain['packamt'];
                                                $totstamt = $totstamt + $pmain['stamt'];
                                                $totothamt = $totothamt + $pmain['otheramt'];
                                                $totbillamt = $totbillamt + $pmain['billamt'];


                                                if ($pmain['canc_flag'] == "C") {
                                                    
                                                } else {
                                                    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 "<thead>";
                                                        echo "<tr class='mySearchTr2'>";
                                                        echo "<th style='width:2%;' class='dth'>Sr</th>";
                                                        echo "<th style='width:5%;' class='dth'>Barcode</th>";
                                                        echo "<th style='width:7%;' class='dth'>Product Code</th>";
                                                        echo "<th style='width:15%;' class='dth'>Product Name</th>";
                                                        echo "<th style='width:6%;' class='dth'>HSN Code</th>";
                                                        echo "<th style='width:5%;' class='dth'>Gram</th>";
                                                        echo "<th style='width:5%;' class='dth'>Unit</th>";
                                                        echo "<th style='width:4%;' class='dth text-right'>Mrp</th>";
                                                        echo "<th style='width:5%;' class='dth '>Batch</th>";
                                                        echo "<th style='width:5%;' class='dth text-right'>Qty.</th>";
                                                        echo "<th style='width:5%;' class='dth text-right'>Rate</th>";
                                                        echo "<th style='width:5%;' class='dth text-right'>Amount</th>";
                                                        echo "<th style='width:5%;' class='dth text-right'>Disc%</th>";
                                                        echo "<th style='width:6%;' class='dth text-right'>Disc. Amt.</th>";
                                                        echo "<th style='width:5%;' class='dth text-right'>Taxable Amount</th>";
                                                        if ($gsttype == 2) {
                                                            echo "<th style='width:5%;' class='dth text-right'>CGST Rate</th>";
                                                            echo "<th style='width:5%;' class='dth text-right'>CGST Amount</th>";
                                                            echo "<th style='width:5%;' class='dth text-right'>SGST Rate</th>";
                                                            echo "<th style='width:5%;' class='dth text-right'>SGST Amount</th>";
                                                        } else if ($gsttype == 1) {
                                                            echo "<th style='width:5%;' class='dth text-right'>IGST Rate</th>";
                                                            echo "<th style='width:5%;' class='dth text-right'>IGST Amount</th>";
                                                        } else {
                                                            echo "<th style='width:5%;' class='dth text-right'>CGST Rate</th>";
                                                            echo "<th style='width:5%;' class='dth text-right'>CGST Amount</th>";
                                                            echo "<th style='width:5%;' class='dth text-right'>SGST Rate</th>";
                                                            echo "<th style='width:5%;' class='dth text-right'>SGST Amount</th>";
                                                            echo "<th style='width:5%;' class='dth text-right'>IGST Rate</th>";
                                                            echo "<th style='width:5%;' class='dth text-right'>IGST Amount</th>";
                                                        }
                                                        echo "<th style='width:7%;' class='text-right dth'>Total</th>";
                                                        echo "</tr>";
                                                        echo "<thead>";
                                                        echo "<tr>";


                                                        $ctr2 = 0;
                                                        $totqty = 0;
                                                        $totrate = 0;

                                                        $totamount = $totdiscount = $totsubtot = $totcgstamt = $totsgstamt = $totigstamt = 0;

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

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


                                                            $barcode = $tran['barcode'];
                                                            $itemcode = $tran['itemcode'];
//                                                $itemname = $tran['itemname'];
                                                            $hsncode = $tran['hsncode'];
                                                            $gram = $tran['gram'];
                                                            $unit = $tran['unit'];
                                                            $mrp = $tran['mrp'];
                                                            $batch = $tran['batch'];
                                                            $qty = $tran['qty'];
                                                            $rate = $tran['rate'];
                                                            $amount = $tran['amount'];
                                                            $discperc = $tran['discperc'];
                                                            $discamt = $tran['discamt'];
                                                            $subtot = $tran['subtot'];
                                                            $cgstrate = $tran['cgstrate'];
                                                            $cgstamt = $tran['cgstamt'];
                                                            $sgstrate = $tran['sgstrate'];
                                                            $sgstamt = $tran['sgstamt'];
                                                            $igstrate = $tran['igstrate'];
                                                            $igstamt = $tran['igstamt'];
                                                            $total = $tran['total'];


                                                            $gitem = mysqli_query($conn, "SELECT  name FROM product WHERE prodcode = '$itemcode' ")or die(mysqli_error($conn));
                                                            $fitem = mysqli_fetch_array($gitem);
                                                            $itemname = $fitem['name'];

                                                            $ctr2++;
                                                            echo "<tr>";
                                                            echo "<td>$ctr2</td>";
                                                            echo "<td>$barcode</td>";
                                                            echo "<td>$itemcode</td>";
                                                            echo "<td>$itemname</td>";
                                                            echo "<td>$hsncode</td>";
                                                            echo "<td>$gram</td>";
                                                            echo "<td>$unit</td>";
                                                            echo "<td class='text-right'>$mrp</td>";
                                                            echo "<td>$batch</td>";
                                                            echo "<td class='text-right'>$qty</td>";
                                                            echo "<td class='text-right'>$rate</td>";
                                                            echo "<td class='text-right'>$amount</td>";
                                                            echo "<td class='text-right'>$discperc</td>";
                                                            echo "<td class='text-right'>$discamt</td>";
                                                            echo "<td class='text-right'>$subtot</td>";
                                                            if ($gsttype == 2) {
                                                                echo "<td class='text-right'>$cgstrate</td>";
                                                                echo "<td class='text-right'>$cgstamt</td>";
                                                                echo "<td class='text-right'>$sgstrate</td>";
                                                                echo "<td class='text-right'>$sgstamt</td>";
                                                            } else if ($gsttype == 1) {
                                                                echo "<td class='text-right'>$igstrate</td>";
                                                                echo "<td class='text-right'>$igstamt</td>";
                                                            } else {
                                                                echo "<td class='text-right'>$cgstrate</td>";
                                                                echo "<td class='text-right'>$cgstamt</td>";
                                                                echo "<td class='text-right'>$sgstrate</td>";
                                                                echo "<td class='text-right'>$sgstamt</td>";
                                                                echo "<td class='text-right'>$igstrate</td>";
                                                                echo "<td class='text-right'>$igstamt</td>";
                                                            }
                                                            echo "<td class='text-right'>$total</td>";
                                                            echo "</tr>";

                                                            $totqty = $totqty + $tran['qty'];
                                                            $totamount = $totamount + $tran['amount'];
                                                            $totdiscount = $totdiscount + $tran['discamt'];
                                                            $totsubtot = $totsubtot + $tran['subtot'];
                                                            $totcgstamt = $totcgstamt + $cgstamt;
                                                            $totsgstamt = $totsgstamt + $sgstamt;
                                                            $totigstamt = $totigstamt + $igstamt;
                                                        }

                                                        echo "<tr>";
                                                        echo "<td class='dth'></td>";
                                                        echo "<td class='dth'></td>";
                                                        echo "<td class='dth'></td>";
                                                        echo "<td class='dth'></td>";
                                                        echo "<td class='dth'></td>";
                                                        echo "<td class='dth'></td>";
                                                        echo "<td class='dth'></td>";
                                                        echo "<td class='dth'></td>";
                                                        echo "<td class='text-right dth'>Total</td>";
                                                        echo "<td class='text-right dth'>$totqty</td>";
                                                        echo "<td class='text-right dth'></td>";
                                                        echo "<td class='text-right dth'>$totamount</td>";
                                                        echo "<td class='text-right dth'></td>";
                                                        echo "<td class='text-right dth'>$totdiscount</td>";
                                                        echo "<td class='text-right dth'>$totsubtot</td>";
                                                        if ($gsttype == 2) {
                                                            echo "<td class='text-right dth'></td>";
                                                            echo "<td class='text-right dth'>$totcgstamt</td>";
                                                            echo "<td class='text-right dth'></td>";
                                                            echo "<td class='text-right dth'>$totsgstamt</td>";
                                                        } else if ($gsttype == 1) {
                                                            echo "<td class='text-right dth'></td>";
                                                            echo "<td class='text-right dth'>$totigstamt</td>";
                                                        } else {
                                                            echo "<td class='text-right dth'></td>";
                                                            echo "<td class='text-right dth'>$totcgstamt</td>";
                                                            echo "<td class='text-right dth'></td>";
                                                            echo "<td class='text-right dth'>$totsgstamt</td>";
                                                            echo "<td class='text-right dth'></td>";
                                                            echo "<td class='text-right dth'>$totigstamt</td>";
                                                        }
                                                        echo "<td class='text-right dth'>$totamount</td>";
                                                        echo "</tr>";
                                                        echo "</table>";
                                                    }
                                                }
                                            }
                                            echo "<tr>";
                                            if (empty($party)) {
                                                echo "<td class='text-right colr' colspan=''><b></b></td>";
                                            }
                                            echo "<td class='text-right colr' colspan='4'><b>Total</b></td>";
                                            echo "<td class='numeric text-right colr '><b>$grsubtot2</b></td>";
                                            echo "<td class='numeric text-right colr '><b>$grtotdis2</b></td>";
                                            echo "<td class='numeric text-right colr '><b>$grtottaxable2</b></td>";
//                                    echo "<td class='numeric text-right colr '><b></b></td>";
                                            echo "<td class='numeric text-right colr '><b>$grtotgst1</b></td>";
                                            echo "<td class='numeric text-right colr '><b>$grtotbillamt2</b></td>";
                                            echo "</tr>";
                                            ?>
                                        </tbody>
                                    </table>
                                    <!-- </div>
                                                                                                                                                                                                                            </div> -->
                                    <?php
                                }
                            }
                            if ($regtype == '' || $regtype == 'E') {

                                $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`invdate`, '%d-%m-%Y') as mydt FROM `salemainexp` WHERE `invdate` >= '$mfrmdate' 
                                     AND `invdate` <= '$mtodate' $partycond2 $consigcond $consigcond $godowncond ") or die(mysqli_error($conn));


                                $getno3 = mysqli_num_rows($getpmain);
                                if ($getno3 > 0) {
                                    ?>

                                    <!--end deemsalereg-->
                                    <!-- export register-->
                                    <table class="table table-condesed table-bordered">
                                        <tr>
                                            <th width="100%" class="h4" style="color:white"><b>Export - Invoice Register</b></th>
                                        </tr>
                                        <tr>
                                            <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                                Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno3; ?></b>
                                            </td>
                                        </tr>
                                    </table>
                                    <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                                        <thead class="mySearchTr">
                                            <tr>
                                                <!--<th style="width:4%;">Sr</th>-->
                                                <th style="width:5%;">Voucher</th>
                                                <th style="width:10%;">Invoice</th>
                                                <th style="width:8%;">Date</th>
                                                <th style="width:25%;">Party Name</th>
                                                <th style="width:24%;">Consignee</th>
                                                <!--<th style="width:9%;">Godown</th>-->
                                                <th style="width:10%" class="text-right">Subtotal</th>
                                                <th style="width:10%" class="text-right">Disc. Amt.</th>
                                                <th style="width:14%" class="text-right">Net Amt.</th>
                                                <th style="width:14%" class="text-right">INR Amt.</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $ctr = 0;
                                            $totbillamt3 = 0;
                                            $totinramt = 0;
                                            if (!empty($party)) {
                                                $partycond2 = " AND `partyname` = '$party' ";
                                            }
                                            $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`invdate`, '%d-%m-%Y') as mydt FROM `salemainexp` WHERE `invdate` >= '$mfrmdate' 
                                     AND `invdate` <= '$mtodate' $partycond2 $consigcond $consigcond $godowncond ") or die(mysqli_error($conn));


                                            while ($pmain = mysqli_fetch_array($getpmain)) {

                                                $customer = $pmain['partyname'];
                                                $consignee = $pmain['consignee'];
                                                $gcode = $pmain['godown'];

                                                $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$customer' AND grpcode='010'  ") or die(mysqli_error($conn));
                                                $sparty = mysqli_fetch_array($gparty);

                                                $gconsig = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$consignee' AND grpcode='010'  ") or die(mysqli_error($conn));
                                                $consig = mysqli_fetch_array($gconsig);

                                                $ggodwn = mysqli_query($conn, "SELECT `code`,`name`,`gcode` FROM `godown` where `code` = '$gcode' ") or die(mysqli_error($conn));
                                                $godwn = mysqli_fetch_array($ggodwn);

                                                $voucher = $pmain['voucher'];
                                                $expvouchno = $pmain['vouchno'];
                                                $invno = $pmain['invoice'];
                                                $voucher = $pmain['voucher'];
                                                $vouchdt = $pmain['mydt'];

                                                if ($pmain['canc_flag'] == "C") {
                                                    $party = "CANCELLED";
                                                    $consigne = "";

                                                    $godown = $grossamt = $discount = $billamt = $inramt = 0;
                                                } else {

                                                    $party = $sparty['name'];
                                                    $consigne = $consig['name'];
                                                    $godown = $godwn['gcode'];
                                                    $grossamt = $pmain['grossamt'];
                                                    $discount = $pmain['discount'];
                                                    $billamt = $pmain['billamt'];
                                                    $inramt = $pmain['inramt'];
                                                }
                                                $ctr++;
                                                echo "<tr data-href='invoexp.php?mode=edit&vouchno=$expvouchno' class='clickable-row2' [target]='_blank'>";
//                                    echo "<td>$ctr</td>";
                                                echo "<td>$voucher</td>";
                                                echo "<td>EXP$invno</td>";
                                                echo "<td>$vouchdt</td>";
                                                echo "<td>$party</td>";
                                                echo "<td>$consigne</td>";
//                                    echo "<td>$godown</td>";
                                                echo "<td class='text-right'>$grossamt</td>";
                                                echo "<td class='text-right'>$discount</td>";
                                                echo "<td class='text-right'>$billamt</td>";
                                                echo "<td class='text-right'>$inramt</td>";
                                                echo "</tr>";

                                                $totsubtot = $totsubtot + $grossamt;
                                                $totdiscount = $totdiscount + $discount;
                                                $totbillamt3 = $totbillamt3 + $billamt;
                                                $totinramt = $totinramt + $inramt;

                                                if ($pmain['canc_flag'] == "C") {
                                                    
                                                } else {


                                                    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-striped table-bordered'>";
                                                        echo "<thead>";
                                                        echo "<tr class='mySearchTr2' >";
                                                        echo "<th style='width:3%;'>Sr</th>";
                                                        echo "<th style='width:5%;'>Barcode</th>";
                                                        echo "<th style='width:7%;' class=''>Product Code</th>";
                                                        echo "<th style='width:30%;' class=''>Product Name</th>";
                                                        echo "<th style='width:7%;' class=''>HSN Code</th>";
                                                        echo "<th style='width:5%;' class=''>Gram</th>";
                                                        echo "<th style='width:5%;' class=''>Unit</th>";
                                                        echo "<th style='width:6%;' class='text-right'>Mrp</th>";
                                                        echo "<th style='width:5%;' class=''>Batch</th>";
                                                        echo "<th style='width:5%;' class='text-right'>Qty.</th>";
                                                        echo "<th style='width:5%;' class='text-right'>Rate</th>";
                                                        echo "<th style='width:5%;' class='text-right'>Amount</th>";
                                                        echo "</tr>";
                                                        echo "<thead>";
                                                        echo "<tr>";

                                                        $ctr2 = 0;
                                                        $trtotqty = 0;
                                                        $trtotamount = 0;

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

                                                            $barcode = $tran['barcode'];
                                                            $itemcode = $tran['itemcode'];
//                                                $itemname = $tran['itemname'];
                                                            $hsncode = $tran['hsncode'];
                                                            $gram = $tran['gram'];
                                                            $unit = $tran['unit'];
                                                            $mrp = $tran['mrp'];
                                                            $batch = $tran['batch'];
                                                            $qty = $tran['qty'];
                                                            $rate = $tran['rate'];
                                                            $amount = $tran['amount'];

                                                            $gitem = mysqli_query($conn, "SELECT  name FROM product WHERE prodcode = '$itemcode' ")or die(mysqli_error($conn));
                                                            $fitem = mysqli_fetch_array($gitem);
                                                            $itemname = $fitem['name'];



                                                            $ctr2++;
                                                            echo "<tr>";
                                                            echo "<td>$ctr2</td>";
                                                            echo "<td>$barcode</td>";
                                                            echo "<td>$itemcode</td>";
                                                            echo "<td>$itemname</td>";
                                                            echo "<td>$hsncode</td>";
                                                            echo "<td>$gram</td>";
                                                            echo "<td>$unit</td>";
                                                            echo "<td class='text-right'>$mrp</td>";
                                                            echo "<td>$batch</td>";
                                                            echo "<td class='numeric text-right'>$qty</td>";
                                                            echo "<td class='numeric text-right'>$rate</td>";
                                                            echo "<td class='numeric text-right'>$amount</td>";
                                                            echo "</tr>";

                                                            $trtotqty = $trtotqty + $qty;
                                                            $trtotamount = $trtotamount + $amount;
                                                        }

                                                        echo "<tr class='mySearchTr2'>";
                                                        echo "<td class='text-right' colspan='9'>Total</td>";
                                                        echo "<td class='text-right'>$trtotqty</td>";
                                                        echo "<td class='text-right'></td>";
                                                        echo "<td class='text-right'>$trtotamount </td>";
                                                        echo "</tr>";
                                                        echo "</table>";
                                                    }
                                                }
                                            }
                                            echo "<tr class='mySearchTr'>";
                                            echo "<td class='text-right' colspan='5'><b>Total</b></td>";
                                            echo "<td class='numeric text-right'><b>$totsubtot</b></td>";
                                            echo "<td class='numeric text-right'><b>$totdiscount</b></td>";
                                            echo "<td class='numeric text-right'><b>$totbillamt3</b></td>";
                                            echo "<td class='numeric text-right'><b>$totinramt</b></td>";
                                            echo "</tr>";
                                            ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            <!--end export register-->
                            <?php
                        }
                    }
                    if ($regtype == '' || $regtype == 'O') {


                        $getosmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `slotmain` WHERE `vouchdt` >= '$mfrmdate' 
                                     AND `vouchdt` <= '$mtodate' $partycond $consigcond $consigcond $godowncond ") or die(mysqli_error($conn));


                        $getno4 = mysqli_num_rows($getosmain);
                        if ($getno4 > 0) {
                            ?>
                            <table class="table table-condesed table-bordered">
                                <tr>
                                    <th width="100%" class="h4" style="color:white"><b>Other - Sales Register</b></th>
                                </tr>
                                <tr>
                                    <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                        Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno4; ?></b>
                                    </td>
                                </tr>
                            </table>
                            <table style="background: white;width: 100%" class="table table-condensed table-striped table-bordered">
                                <thead class="mySearchTr">
                                    <tr>
                                        <!--<th style="width:4%;">Sr</th>-->
                                        <th style="width:6%;">Voucher</th>
                                        <th style="width:6%;">Invoice No.</th>
                                        <th style="width:10%;">Date</th>
                                        <th style="width:6%;">Godown</th>
                                        <?php if (empty($party)) { ?>
                                            <th style="width:25%;">Party Name</th>
                                        <? } ?>
                                        <th style="width:6%;">GST No.</th>
                                        <th style="width:5%" class="text-right">Amount</th>
                                        <th style="width:8%" class="text-right">Disc Amt.</th>
                                        <th style="width:7%" class="text-right">Taxable</th>
                                        <th style="text-align:right; width:6%;">CGST Amt.</th>
                                        <th style="text-align:right; width:6%;">SGST Amt.</th>
                                        <th style="text-align:right; width:6%;">IGST Amt.</th>
                                        <th style="width:8%" class="text-right">Bill Amount</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $ctr = 0;
                                    $totamt = 0;
                                    $totdiscamt = 0;
                                    $tottaxable = 0;
                                    $totgst5 = 0;
                                    $totgst12 = 0;
                                    $totgst18 = 0;
                                    $totgst28 = 0;
                                    $totpackamt = $totstamt = $totothamt = $totbillamt = 0;
                                    $grsubtot = $grtotdis = $grtottaxable = $grtotgst = $grtotbillamt = 0;



                                    if ($gsttype == '') {
                                        $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' $partycond $godowncond $staffcond $ptypecond $pcond order by invno") or die(mysqli_error($conn));
                                    } else {
                                        $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                                         AND `vouchdt` <= '$mtodate' $partycond $godowncond $staffcond $ptypecond $gstcond $pcond order by invno") or die(mysqli_error($conn));
                                    }


                                    while ($pmain2 = mysqli_fetch_array($getosmain)) {
                                        if ($pmain2['paidby'] == "C") {
                                            $ptype = "CASH";
                                        } else {
                                            $ptype = "CREDIT";
                                        }

                                        $godown = $pmain2['godown'];
                                        $customer = $pmain2['party'];
                                        $staf = $pmain2['staff'];

                                        $gstaf = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$staf' AND grpcode='040'  ") or die(mysqli_error($conn));
                                        $sstaf = mysqli_fetch_array($gstaf);

                                        $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$customer' AND grpcode='010'  ") or die(mysqli_error($conn));
                                        $sparty = mysqli_fetch_array($gparty);

                                        $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$godown'") or die(mysqli_error($conn));
                                        $sgodw = mysqli_fetch_array($ggodw);


                                        $voucher = $pmain2['voucher'];
                                        $lslvouchno = $pmain2['vouchno'];
                                        $invno = $pmain2['invno'];
                                        $mydt = $pmain2['mydt'];
                                        $gcode = $sgodw['gcode'];
                                        $partynm = $sparty['name'];
                                        $staff = $sstaf['name'];
                                        $gstno = $pmain2['gstno'];
                                        $cgst = $pmain2['cgstamt'];
                                        $sgst = $pmain2['sgstamt'];
                                        $igst = $pmain2['igstamt'];
                                        $billamt = $pmain2['billamt'];
                                        $subtot = $pmain2['grossamt'];
                                        $totdisc = $pmain2['discamt'];
                                        $taxable = $pmain2['taxableamt'];


                                        $ctr++;
                                        echo "<tr data-href='sale_ti.php?mode=edit&vouchno=$lslvouchno' class='clickable-row2' [target]='_blank'>";
                                        echo "<td>$voucher</td>";
                                        echo "<td>$invno </td>";
                                        echo "<td>$mydt</td>";
                                        echo "<td>$gcode</td>";
                                        if (empty($party)) {

                                            echo "<td style='font-size:12px'>$partynm</td>";
                                        }
                                        echo "<td style='font-size:12px'>$gstno</td>";
                                        echo "<td class='text-right'>$subtot</td>";
                                        echo "<td class='text-right'>$totdisc</td>";
                                        echo "<td class='text-right'>$taxable</td>";
                                        echo "<td class='text-right'>$cgst</td>";
                                        echo "<td class='text-right'>$sgst</td>";
                                        echo "<td class='text-right'>$igst</td>";
                                        echo "<td class='text-right'>$billamt</td>";
                                        echo "</tr>";



                                        $grsubtot = $grsubtot + $subtot;
                                        $grtotdis = $grtotdis + $totdisc;
                                        $grtottaxable = $grtottaxable + $taxable;
                                        $grtotcgst = $grtotcgst + $cgst;
                                        $grtotsgst = $grtotsgst + $sgst;
                                        $grtotigst = $grtotigst + $igst;
                                        $grtotbillamt = $grtotbillamt + $billamt;

                                        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 "<tr class='mySearchTr2'>";
                                            echo "<th style='width:5%;' class='dth'>Sr</th>";
                                            echo "<th style='width:80%;' class='dth'>Decription</th>";
                                            echo "<th style='width:10%;' class='dth'>Amount</th>";
                                            echo "</tr>";
                                            echo "<tr>";

                                            $ctr2 = 0;
                                            $totqty = 0;
                                            $totrate = 0;
                                            $trtotal = 0;
                                            $totamount = $totdiscount = $totsubtot = $totcgstamt = $totsgstamt = $totigstamt2 = 0;

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

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


                                                $descrip = $tran['descrip'];
                                                $amount = $tran['amount'];


                                                $gitem = mysqli_query($conn, "SELECT  name FROM product WHERE prodcode = '$itemcode' ")or die(mysqli_error($conn));
                                                $fitem = mysqli_fetch_array($gitem);
                                                $itemname = $fitem['name'];

                                                $ctr2++;
                                                echo "<tr>";
                                                echo "<td>$ctr2</td>";
                                                echo "<td>$descrip</td>";
                                                echo "<td class='text-right'>$amount</td>";
                                                echo "</tr>";

                                                $totamount = $totamount + $tran['amount'];
                                            }

                                            echo "<tr>";
                                            echo "<td class='dth'></td>";
                                            echo "<td class='text-right dth'>Total</td>";
                                            echo "<td class='text-right dth'>$totamount</td>";
                                            echo "</tr>";
                                            echo "</table>";
                                        }
                                    }
                                    echo "<tr>";
                                    if (empty($party)) {
                                        echo "<td class='text-right colr' colspan=''><b></b></td>";
                                    }
                                    echo "<td class='text-right colr' colspan='5'><b>Total</b></td>";
                                    echo "<td class='numeric text-right colr '><b>$grsubtot</b></td>";
                                    echo "<td class='numeric text-right colr '><b>$grtotdis</b></td>";
                                    echo "<td class='numeric text-right colr '><b>$grtottaxable</b></td>";
                                    echo "<td class='numeric text-right colr '><b>$grtotcgst</b></td>";
                                    echo "<td class='numeric text-right colr '><b>$grtotsgst</b></td>";
                                    echo "<td class='numeric text-right colr '><b>$grtotigst</b></td>";
                                    echo "<td class='numeric text-right colr '><b>$grtotbillamt</b></td>";
                                    echo "</tr>";
                                    ?>
                                </tbody>
                            </table>

                            <?
                        }
                    }
                }
                ?>
            </div>
        </div>
        <script>
            $(document).ready(function () {
                $('#create_excel').click(function () {
                    var excel_data = $('.tablerep').html(),
                            page = "excellib.php?data=" + excel_data.trim();
                    var data = {excel_data: excel_data};
                    window.open('data:application/vnd.ms-excel,' + encodeURIComponent(excel_data));
                });
            });
        </script>
        <?php include("includes/linksfoot.php");
        ?>
        </body>

        </html>
        <?php
    }


    if (isset($_POST["excel"])) {
        $party = $_POST["party"];
        $p = $_GET["p"];

        $godown = $_POST["godown"];
        $staff = $_POST["staff"];
        $ptype = $_POST["ptype"];
        $dtype = $_POST["dtype"];
        $gsttype = $_POST["gsttype"];

        if (!empty($party)) {
            $partycond = " AND `party` = '$party' ";
        }
        if (!empty($p)) {
            $pcond = " AND `party` = '$p' ";
        }
        if (!empty($godown)) {
            $godowncond = " AND `godown` = '$godown' ";
        }
        if (!empty($staff)) {
            $staffcond = " AND `staff` = '$staff '";
        }
        if (!empty($ptype)) {
            $ptypecond = " AND `paidby`= '$ptype'";
        }
        if (!empty($dtype)) {
            $dtypecond = " AND `dtype`= '$dtype' ";
        }

        $gstcond = " ";
        if (!empty($gsttype)) {
            if ($gsttype == 1) {
                $gstcond = "AND `oms` = '1'";
            } else {
                $gstcond = "AND `oms` = ''";
            }
        }

        if (($_GET["fdt"]) != '') {
            $mfdt = $_GET["fdt"];
            $mtdt = $_GET["tdt"];
            $mfrmdate = date("y-m-d", strtotime($mfdt));
            $mtodate = date("y-m-d", strtotime($mtdt));
            $dtype = $_GET["rpt"];
        } else {

            $mfrmdate = trim($_POST["frmdt"]);
            $mtodate = trim($_POST["todt"]);

            if (empty($_POST["frmdt"])) {
                browse("From Date Cannot Be Empty");
                header("refresh:1; saleitemgrp.php");
            }
            if (empty($_POST["todt"])) {
                browse("To Date Cannot Be Empty");
                header("refresh:1; saleitemgrp.php");
            }
            $mfdt = date("d-m-Y", strtotime($mfrmdate));
            $mtdt = date("d-m-Y", strtotime($mtodate));
        }

        $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$party' AND grpcode='010'  ") or die(mysqli_error($conn));
        $sparty = mysqli_fetch_array($gparty);
        $partyname = $sparty['name'];

        if ($dtype == 3) {
            $tradesale = mysqli_query($conn, "SELECT SUM(grossamt)as amt,SUM(discamt)AS discount,SUM(taxableamt) as taxable, 
                                      SUM(totgst5+totgst12+totgst18+totgst28) AS totgst, SUM(billamt) AS netamt
                                      from salemain WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and canc_flag != 'C'  ") or die(mysqli_error($conn));
            $ftradesale = mysqli_fetch_array($tradesale);

            $deemsale = mysqli_query($conn, "SELECT SUM(grossamt)as amt,SUM(discamt)AS discount,SUM(taxableamt) as taxable, 
                                     SUM(totgst1) AS totgst ,SUM(billamt) AS netamt
                                     from deexmain WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and canc_flag != 'C' ") or die(mysqli_error($conn));
            $fdeemsale = mysqli_fetch_array($deemsale);
            
            $exportsale = mysqli_query($conn, "SELECT SUM(grossamt * roe)as amt, SUM(taxableamt * roe) as taxable, SUM(inramt) AS netamt
                                     from salemainexp WHERE `invdate` >= '$mfrmdate' AND `invdate` <= '$mtodate' and canc_flag != 'C' ") or die(mysqli_error($conn));
            $fexportsale = mysqli_fetch_array($exportsale);

            $saleother = mysqli_query($conn, "SELECT SUM(grossamt) as amt, SUM(discount) AS discamt, SUM(taxableamt) as taxable,
                                      SUM(cgstamt+sgstamt+igstamt) AS totgst ,SUM(billamt) AS netamt 
                                       from slotmain WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
            $fsaleother = mysqli_fetch_array($saleother);
            
            
            $creditnoteg = mysqli_query($conn, "SELECT SUM(grossamt)as amt,SUM(discamt)AS discount,SUM(taxableamt) as taxable, 
                                      SUM(cgstamt+sgstamt+igstamt) AS totgst ,SUM(billamt) AS netamt
                                      from credmain WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and credtype = 'G' ") or die(mysqli_error($conn));
            $fcreditnoteg = mysqli_fetch_array($creditnoteg);

            $creditnoteo = mysqli_query($conn, "SELECT SUM(grossamt)as amt,SUM(discamt)AS discount,SUM(taxableamt) as taxable, 
                                        SUM(cgstamt+sgstamt+igstamt) AS totgst ,SUM(billamt) AS netamt from credmain WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' and credtype = 'O' ") or die(mysqli_error($conn));
            $fcreditnoteo = mysqli_fetch_array($creditnoteo);


            $totamt = $ftradesale['amt'] + $fdeemsale['amt'] + $fexportsale['amt'];
            $totdiscamt = $ftradesale['discount'] + $fdeemsale['discount'];
            $tottaxble = $ftradesale['taxable'] + $fdeemsale['taxable'] + $fexportsale['taxable'];
            $totgst = $ftradesale['totgst'] + $fdeemsale['totgst'];
            $totnetamt = $ftradesale['netamt'] + $fdeemsale['netamt'] + $fexportsale['netamt'];

            $totretamt = $fcreditnoteg['amt'] + $fcreditnoteo['amt'];
            $totretdiscamt = $fcreditnoteg['discount'] + $fcreditnoteo['discoubt'];
            $totrettaxableamt = $fcreditnoteg['taxable'] + $fcreditnoteo['taxable'];
            $totretgst = $fcreditnoteg['totgst'] + $fcreditnoteo['totgst'];
            $totretnetamt = $fcreditnoteg['netamt'] + $fcreditnoteo['netamt'];


            $grtotamt = $totamt - $totretamt;
            $grtotdisc = $totdiscamt - $totretdiscamt;
            $grtottaxable = $tottaxble - $totrettaxableamt;
            $grtotgst = $totgst - $totretgst;
            $grtotnetamt = $totnetamt - $totretnetamt;


            $contents .= ",Amount,Disc. Amt.,Taxable Amt.,GST,Net Amt.,\n";

            $contents .= "Local Sale" . ",";
            $contents .= $ftradesale['amt'] . ",";
            $contents .= $ftradesale['discount'] . ",";
            $contents .= $ftradesale['taxable'] . ",";
            $contents .= $ftradesale['totgst'] . ",";
            $contents .= $ftradesale['netamt'] . "\n";


            $contents .= "Deemed Sale" . ",";
            $contents .= $fdeemsale['amt'] . ",";
            $contents .= $fdeemsale['discount'] . ",";
            $contents .= $fdeemsale['taxable'] . ",";
            $contents .= $fdeemsale['totgst'] . ",";
            $contents .= $fdeemsale['netamt'] . "\n";


            $contents .= "Export Sale" . ",";
            $contents .= $fexportsale['amt'] . ",";
            $contents .= "" . ",";
            $contents .= $fexportsale['taxable'] . ",";
            $contents .= "" . ",";
            $contents .= $fexportsale['netamt'] . "\n";


            $contents .= "Sale Other" . ",";
            $contents .= $fsaleother['amt'] . ",";
            $contents .= $fsaleother['discamt'] . ",";
            $contents .= $fsaleother['taxable'] . ",";
            $contents .= $fsaleother['totgst'] . ",";
            $contents .= $fsaleother['netamt'] . "\n";

            $contents .= "Total Sale" . ",";
            $contents .= $totamt . ",";
            $contents .= $totdiscamt . ",";
            $contents .= $tottaxble . ",";
            $contents .= $totgst . ",";
            $contents .= $totnetamt . "\n";

            $contents .= ",\n";
            $contents .= "RETURNS" . "\n";


            $contents .= ",Amount,Discount,Taxable Amount,GST,Bill Amt.\n";

            $contents .= "GOODS RETURNS" . ",";
            $contents .= $fcreditnoteg['amt'] . ",";
            $contents .= $fcreditnoteg['discount'] . ",";
            $contents .= $fcreditnoteg['taxable'] . ",";
            $contents .= $fcreditnoteg['totgst'] . ",";
            $contents .= $creditnoteg['netamt'] . "\n";

            $contents .= "OTHERS" . ",";
            $contents .= $fcreditnoteo['amt'] . ",";
            $contents .= $fcreditnoteo['discount'] . ",";
            $contents .= $fcreditnoteo['taxable'] . ",";
            $contents .= $fcreditnoteo['totgst'] . ",";
            $contents .= $fcreditnoteo['netamt'] . "\n";


            $contents .= "Total Return Sale" . ",";
            $contents .= $totretamt . ",";
            $contents .= $totretdiscamt . ",";
            $contents .= $totrettaxableamt . ",";
            $contents .= $totretgst . ",";
            $contents .= $totretnetamt . "\n";

            $contents .= "\n";
            $contents .= "Grand Total" . ",";
            $contents .= $grtotamt . ",";
            $contents .= $grtotdisc . ",";
            $contents .= $grtottaxable . ",";
            $contents .= $grtotgst . ",";
            $contents .= $grtotnetamt . "\n";
        } else {
            if ($regtype == '' || $regtype == 'T') {
                $contents .= ",,,,,Trading Inventory - Sale Register\n";

                if (!empty($party)) {
                    $contents .= ",,,,,Party, $partyname\n";
                }

                $contents .= "Sr., Voucher, Invoice No., Date, Godown, Party Name, GST No., Staff, Amount, Disc Amt., Taxable, ";
                $contents .= "Taxable On GST 0%, Taxable on 0.05%, GST 0.05%, Taxable On GST 5%, GST 5%, Taxable On GST 12%,GST 12%, ";
                $contents .= "Taxable On GST 18%,GST 18%, Taxable On GST 28%,GST 28%, ";
                $contents .= "Taxable On IGST 0%, Taxable on 0.1%, IGST 0.1%, Taxable On IGST 5%, IGST 5%, Taxable On IGST 12%, IGST 12%, ";
                $contents .= "Taxable On IGST 18%, IGST 18%, Taxable On IGST 28%, IGST 28%, Bill Amount\n";


                $ctr = 0;
                $totamt = 0;
                $taxable = 0;
                $totdiscamt = 0;
                $tottaxable = 0;

                $totgst0 = 0;
                $totgst1 = 0;
                $totgst5 = 0;
                $totgst12 = 0;
                $totgst18 = 0;
                $totgst28 = 0;

                $totgst0i = 0;
                $totgst1i = 0;
                $totgst5i = 0;
                $totgst12i = 0;
                $totgst18i = 0;
                $totgst28i = 0;

                $totpackamt = $totstamt = $totothamt = $totbillamt = 0;
                $grsubtot = $grtotdis = $grtottaxable = $grtotgst = $grtotbillamt = 0;

                $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                                         AND `vouchdt` <= '$mtodate' $partycond $godowncond $staffcond $ptypecond $gstcond $pcond") or die(mysqli_error($conn));

                while ($pmain = mysqli_fetch_array($getpmain)) {
                    if ($pmain['paidby'] == "C") {
                        $ptype = "CASH";
                    } else {
                        $ptype = "CREDIT";
                    }

                    $oms = $pmain["oms"];
                    $godown = $pmain['godown'];
                    $customer = $pmain['party'];
                    $staf = $pmain['staff'];

                    $gstaf = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$staf' AND grpcode='040'  ") or die(mysqli_error($conn));
                    $sstaf = mysqli_fetch_array($gstaf);

                    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$customer' AND grpcode='010'  ") or die(mysqli_error($conn));
                    $sparty = mysqli_fetch_array($gparty);

                    $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$godown'") or die(mysqli_error($conn));
                    $sgodw = mysqli_fetch_array($ggodw);


                    $voucher = $pmain['voucher'];
                    $vouchno = $pmain['vouchno'];


                    if ($oms == 1) {
                        $taxbl0 = mysqli_query($conn, "SELECT sum(subtot) as subtot0, igstrate from  saletran  
                                           where vouchno = '$vouchno' and igstrate = 0.00")or die(mysqli_error($conn));

                        $taxbl5 = mysqli_query($conn, "SELECT sum(subtot) as subtot5, igstrate from  saletran  
                                           where vouchno = '$vouchno' and igstrate=5.00")or die(mysqli_error($conn));

                        $taxbl12 = mysqli_query($conn, "SELECT sum(subtot) as subtot12, igstrate from  saletran 
                                            where vouchno = '$vouchno' and igstrate = 12.00 ")or die(mysqli_error($conn));

                        $taxbl18 = mysqli_query($conn, "SELECT sum(subtot) as subtot18, igstrate from  saletran 
                                            where vouchno = '$vouchno' and igstrate=18.00")or die(mysqli_error($conn));

                        $taxbl28 = mysqli_query($conn, "SELECT sum(subtot) as subtot28, igstrate from  saletran 
                                            where vouchno = '$vouchno' and igstrate=28.00")or die(mysqli_error($conn));
                    } else {
                        $taxbl0 = mysqli_query($conn, "SELECT sum(subtot) as subtot0, cgstrate from  saletran  
                                           where vouchno = '$vouchno' and cgstrate = 0.00 group by cgstrate")or die(mysqli_error($conn));

                        $taxbl5 = mysqli_query($conn, "SELECT sum(subtot) as subtot5, cgstrate from  saletran  
                                           where vouchno = '$vouchno' and cgstrate = 2.50 group by cgstrate")or die(mysqli_error($conn));

                        $taxbl12 = mysqli_query($conn, "SELECT sum(subtot) as subtot12, cgstrate from  saletran 
                                            where vouchno = '$vouchno' and cgstrate = 6.00 ")or die(mysqli_error($conn));

                        $taxbl18 = mysqli_query($conn, "SELECT sum(subtot) as subtot18, cgstrate from  saletran 
                                            where vouchno = '$vouchno' and cgstrate=9.00 group by cgstrate")or die(mysqli_error($conn));

                        $taxbl28 = mysqli_query($conn, "SELECT sum(subtot) as subtot28, cgstrate from  saletran 
                                            where vouchno = '$vouchno' and cgstrate=14.00 group by cgstrate")or die(mysqli_error($conn));
                    }

                    $taxable0 = mysqli_fetch_array($taxbl0);
                    $taxable5 = mysqli_fetch_array($taxbl5);
                    $taxable12 = mysqli_fetch_array($taxbl12);
                    $taxable18 = mysqli_fetch_array($taxbl18);
                    $taxable28 = mysqli_fetch_array($taxbl28);

                    $invno = $pmain['invno'];
                    $mydt = $pmain['mydt'];
                    $gcode = $sgodw['gcode'];

                    if ($pmain['canc_flag'] == "C") {
                        $partynm = "CANCELLED";
                        $gst0 = $gst5 = $gst12 = $gst18 = $gst28 = $billamt = $taxable = $subtot = $totdisc = 0;
                        $tottaxble0 = $tottaxble1 = $tottaxble5 = $tottaxble12 = $tottaxble18 = $tottaxble28 = 0;
                        $tottaxble0i = $tottaxble1i = $tottaxble5i = $tottaxble12i = $tottaxble18i = $tottaxble28i = 0;
                    } else {
                        $partynm = $sparty['name'];
                        $staff = $sstaf['name'];
                        $gstno = $pmain['gstno'];

                        $gst0 = $pmain['totgst0'];
                        $gst1 = 0;
                        $gst5 = $pmain['totgst5'];
                        $gst12 = $pmain['totgst12'];
                        $gst18 = $pmain['totgst18'];
                        $gst28 = $pmain['totgst28'];

                        $billamt = $pmain['billamt'];
                        $subtot = $pmain['grossamt'];
                        $totdisc = $pmain['discamt'];
                        $taxable = $pmain['taxableamt'];

//                    $tottaxble0 = $pmain['tottaxble0'];
                        $tottaxble0 = $taxable0['subtot0'];
                        $tottaxble1 = 0;
                        $tottaxble5 = $pmain['tottaxble5'];
                        $tottaxble12 = $pmain['tottaxble12'];
                        $tottaxble18 = $pmain['tottaxble18'];
                        $tottaxble28 = $pmain['tottaxble28'];
                    }

                    $ctr++;
                    $contents .= $ctr . ",";
                    $contents .= $voucher . ",";
                    $contents .= "L" . $invno . ",";
                    $contents .= $mydt . ",";
                    $contents .= $gcode . ",";
                    $contents .= $partynm . ",";
                    $contents .= $gstno . ",";
                    $contents .= $staff . ",";
                    $contents .= $subtot . ",";
                    $contents .= $totdisc . ",";
                    $contents .= $taxable . ",";

                    if ($oms == 1) {
                        $contents .= ",,,,,,,,,,,";
                    }
                    $contents .= $tottaxble0 . ",";
                    $contents .= $tottaxble1 . ",";
                    $contents .= $gst1 . ",";
                    $contents .= $tottaxble5 . ",";
                    $contents .= $gst5 . ",";
                    $contents .= $tottaxble12 . ",";
                    $contents .= $gst12 . ",";
                    $contents .= $tottaxble18 . ",";
                    $contents .= $gst18 . ",";
                    $contents .= $tottaxble28 . ",";
                    $contents .= $gst28 . ",";

                    if ($oms == 1) {
                        
                    } else {
                        $contents .= ",,,,,,,,,,,";
                    }
                    $contents .= $billamt . "\n";

                    $grsubtot = $grsubtot + $subtot;
                    $grtotdis = $grtotdis + $totdisc;
                    $grtottaxable = $grtottaxable + $taxable;

                    if ($oms == 1) {
                        $grtottaxbl0i = $grtottaxbl0i + $tottaxble0;
                        $grtottaxbl1i = $grtottaxbl1i + $tottaxble1;
                        $grtottaxbl5i = $grtottaxbl5i + $tottaxble5;
                        $grtottaxbl12i = $grtottaxbl12i + $tottaxble12;
                        $grtottaxbl18i = $grtottaxbl18i + $tottaxble18;
                        $grtottaxbl28i = $grtottaxbl28i + $tottaxble28;

                        $grtotgst0i = $grtotgst0i + $gst0;
                        $grtotgst1i = $grtotgst1i + $gst1;
                        $grtotgst5i = $grtotgst5i + $gst5;
                        $grtotgst12i = $grtotgst12i + $gst12;
                        $grtotgst18i = $grtotgst18i + $gst18;
                        $grtotgst28i = $grtotgst28i + $gst28;
                    } else {
                        $grtottaxbl0 = $grtottaxbl0 + $tottaxble0;
                        $grtottaxbl1 = $grtottaxbl1 + $tottaxble1;
                        $grtottaxbl5 = $grtottaxbl5 + $tottaxble5;
                        $grtottaxbl12 = $grtottaxbl12 + $tottaxble12;
                        $grtottaxbl18 = $grtottaxbl18 + $tottaxble18;
                        $grtottaxbl28 = $grtottaxbl28 + $tottaxble28;

                        $grtotgst0 = $grtotgst0 + $gst0;
                        $grtotgst1 = $grtotgst1 + $gst1;
                        $grtotgst5 = $grtotgst5 + $gst5;
                        $grtotgst12 = $grtotgst12 + $gst12;
                        $grtotgst18 = $grtotgst18 + $gst18;
                        $grtotgst28 = $grtotgst28 + $gst28;
                    }

                    $grtotbillamt = $grtotbillamt + $billamt;

                    $totpackamt = $totpackamt + $pmain['packamt'];
                    $totstamt = $totstamt + $pmain['stamt'];
                    $totothamt = $totothamt + $pmain['otheramt'];
                    $totbillamt = $totbillamt + $pmain['billamt'];

                    if ($pmain['canc_flag'] == 'C') {
                        
                    } else {
                        if ($_POST["dtype"] == 2) {

                            if ($gsttype == 1) {
                                $contents .= "Sr,Barcode,Product Code,Product Name,HSN Code,Gram,Unit,Mrp,Batch,Qty.,Rate,Amount,Disc%,Disc. Amt.,Taxable Amount,IGST Rate,IGST Amount,Total\n";
                            } elseif ($gsttype == 2) {
                                $contents .= "Sr,Barcode,Product Code,Product Name,HSN Code,Gram,Unit,Mrp,Batch,Qty.,Rate,Amount,Disc%,Disc. Amt.,Taxable Amount,CGST Rate, CGST Amount, SGST Rate, SGST Amount, Total\n";
                            } else {
                                $contents .= "Sr,Barcode,Product Code,Product Name,HSN Code,Gram,Unit,Mrp,Batch,Qty.,Rate,Amount,Disc%,Disc. Amt.,Taxable Amount,CGST Rate, CGST Amount, SGST Rate, SGST Amount, IGST Rate,IGST Amount,Total\n";
                            }

                            $ctr2 = 0;
                            $totqty = 0;
                            $totrate = 0;
                            $totamount = $totdiscount = $totsubtot = $totcgstamt = $totsgstamt = $totigstamt = 0;

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

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


                                $barcode = $tran['barcode'];
                                $itemcode = $tran['itemcode'];
//                        $itemname = $tran['itemname'];
                                $hsncode = $tran['hsncode'];
                                $gram = $tran['gram'];
                                $unit = $tran['unit'];
                                $mrp = $tran['mrp'];
                                $batch = $tran['batch'];
                                $qty = $tran['qty'];
                                $rate = $tran['rate'];
                                $amount = $tran['amount'];
                                $discperc = $tran['discperc'];
                                $discamt = $tran['discamt'];
                                $subtot = $tran['subtot'];
                                $igstrate = $tran['igstrate'];
                                $igstamt = $tran['igstamt'];
                                $sgstrate = $tran['sgstrate'];
                                $sgstamt = $tran['sgstamt'];
                                $cgstrate = $tran['cgstrate'];
                                $cgstamt = $tran['cgstamt'];
                                $total = $tran['total'];

                                $gitem = mysqli_query($conn, "SELECT  name FROM product WHERE prodcode = '$itemcode' ")or die(mysqli_error($conn));
                                $fitem = mysqli_fetch_array($gitem);
                                $itemname = $fitem['name'];


                                $ctr2++;
                                $contents .= $ctr2 . ",";
                                $contents .= $barcode . ",";
                                $contents .= $itemcode . ",";
                                $contents .= $itemname . ",";
                                $contents .= $hsncode . ",";
                                $contents .= $gram . ",";
                                $contents .= $unit . ",";
                                $contents .= $mrp . ",";
                                $contents .= $batch . ",";
                                $contents .= $qty . ",";
                                $contents .= $rate . ",";
                                $contents .= $amount . ",";
                                $contents .= $discperc . ",";
                                $contents .= $discamt . ",";
                                $contents .= $subtot . ",";
                                if ($gsttype == 1) {
                                    $contents .= $igstrate . ",";
                                    $contents .= $igstamt . ",";
                                } elseif ($gsttype == 2) {
                                    $contents .= $cgstrate . ",";
                                    $contents .= $cgstamt . ",";
                                    $contents .= $sgstrate . ",";
                                    $contents .= $sgstamt . ",";
                                } else {
                                    $contents .= $igstrate . ",";
                                    $contents .= $igstamt . ",";
                                    $contents .= $cgstrate . ",";
                                    $contents .= $cgstamt . ",";
                                    $contents .= $sgstrate . ",";
                                    $contents .= $sgstamt . ",";
                                }
                                $contents .= $total . "\n";

                                $totqty = $totqty + $tran['qty'];
                                $totamount = $totamount + $tran['amount'];
                                $totdiscount = $totdiscount + $tran['discamt'];
                                $totsubtot = $totsubtot + $tran['subtot'];
                                $totcgstamt = $totcgstamt + $tran['cgstamt'];
                                $totsgstamt = $totsgstamt + $tran['sgstamt'];
                                $totigstamt = $totigstamt + $tran['igstamt'];
                            }


                            $contents .= " " . ",";
                            $contents .= " " . ",";
                            $contents .= " " . ",";
                            $contents .= " " . ",";
                            $contents .= " " . ",";
                            $contents .= " " . ",";
                            $contents .= " " . ",";
                            $contents .= " " . ",";
                            $contents .= "Total" . ",";
                            $contents .= $totqty . ",";
                            $contents .= "" . ",";
                            $contents .= $totamount . ",";
                            $contents .= "" . ",";
                            $contents .= $totdiscount . ",";
                            $contents .= $totsubtot . ",";
                            if ($gsttype == 1) {
                                $contents .= "" . ",";
                                $contents .= $totigstamt2 . ",";
                            } elseif ($gsttype == 2) {
                                $contents .= "" . ",";
                                $contents .= $totcgstamt . ",";
                                $contents .= "" . ",";
                                $contents .= $totsgstamt . ",";
                            } else {
                                $contents .= "" . ",";
                                $contents .= $totigstamt2 . ",";
                                $contents .= "" . ",";
                                $contents .= $totcgstamt . ",";
                                $contents .= "" . ",";
                                $contents .= $totsgstamt . ",";
                            }
                            $contents .= $totamount . "\n";
                        }
                    }
                }

                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "Total" . ",";
                $contents .= $grsubtot . ",";
                $contents .= $grtotdis . ",";
                $contents .= $grtottaxable . ",";

                $contents .= $grtottaxbl0 . ",";

                $contents .= $grtottaxbl1 . ",";
                $contents .= $grtotgst1 . ",";

                $contents .= $grtottaxbl5 . ",";
                $contents .= $grtotgst5 . ",";

                $contents .= $grtottaxbl12 . ",";
                $contents .= $grtotgst12 . ",";

                $contents .= $grtottaxbl18 . ",";
                $contents .= $grtotgst18 . ",";

                $contents .= $grtottaxbl28 . ",";
                $contents .= $grtotgst28 . ",";

                ////////////

                $contents .= $grtottaxbl0i . ",";

                $contents .= $grtottaxbl1i . ",";
                $contents .= $grtotgst1i . ",";

                $contents .= $grtottaxbl5i . ",";
                $contents .= $grtotgst5i . ",";

                $contents .= $grtottaxbl12i . ",";
                $contents .= $grtotgst12i . ",";

                $contents .= $grtottaxbl18i . ",";
                $contents .= $grtotgst18i . ",";

                $contents .= $grtottaxbl28i . ",";
                $contents .= $grtotgst28i . ",";

                $contents .= $grtotbillamt . "\n";
            }



            if ($regtype == '' || $regtype == 'D') {

//            <!--deemsalereg-->


                $contents .= "\n";
                $contents .= ",,,,,Deemed Sale - Register\n";

                if (!empty($party)) {
                    $contents .= ",,,,,Party, $partyname\n";
                }

                $contents .= "Sr., Voucher, Invoice No., Date, Godown, Party Name, GST No., Staff, Amount, Disc Amt., Taxable, ";
                $contents .= "Taxable On GST 0%, Taxable on 0.05%, GST 0.05%, Taxable On GST 5%, GST 5%, Taxable On GST 12%,GST 12%, ";
                $contents .= "Taxable On GST 18%,GST 18%, Taxable On GST 28%,GST 28%, ";
                $contents .= "Taxable On IGST 0%, Taxable on 0.1%, IGST 0.10%, Taxable On IGST 5%, IGST 5%, Taxable On IGST 12%, IGST 12%, Taxable On IGST 18%, IGST 18%, ";
                $contents .= "Taxable On IGST 28%, IGST 28%, Bill Amount\n";

                $grsubtot = $grtotdis = $grtottaxable = $grtottaxbl0 = $grtottaxbl1 = $grtotgst1 = $grtottaxbl5 = $grtotgst5 = 0;
                $grtottaxbl12 = $grtotgst12 = $grtottaxbl18 = $grtotgst18 = $grtottaxbl28 = $grtotgst28 = 0;

                $grtottaxbl0i = $grtottaxbl1i = $grtotgst1i = $grtottaxbl5i = $grtotgst5i = 0;
                $grtottaxbl12i = $grtotgst12i = $grtottaxbl18i = $grtotgst18i = $grtottaxbl28i = $grtotgst28i = 0;
                $grtotbillamt = 0;


                $ctr = 0;
                $totamt = 0;
                $totdiscamt = 0;
                $tottaxable = 0;
                $totgst1 = 0;
                $totpackamt = $totstamt = $totothamt = $totbillamt2 = 0;
                $grtotbillamt2 = 0;

                if ($gsttype == '') {
                    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `deexmain` WHERE `vouchdt` >= '$mfrmdate'
                                         AND `vouchdt` <= '$mtodate' $partycond $godowncond $staffcond $ptypecond $pcond") or die(mysqli_error($conn));
                } else {
                    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `deexmain` WHERE `vouchdt` >= '$mfrmdate'
                                         AND `vouchdt` <= '$mtodate' $partycond $godowncond $staffcond $ptypecond $gstcond $pcond") or die(mysqli_error($conn));
                }
                while ($pmain = mysqli_fetch_array($getpmain)) {
                    if ($pmain['paidby'] == "C") {
                        $ptype = "CASH";
                    } else {
                        $ptype = "CREDIT";
                    }

                    $oms = $pmain["oms"];
                    $godown = $pmain['godown'];
                    $customer = $pmain['party'];
                    $staf = $pmain['staff'];

                    $gstaf = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` where `accode` = '$staf' AND grpcode = '040' ") or die(mysqli_error($conn));
                    $sstaf = mysqli_fetch_array($gstaf);

                    $gparty = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` where `accode` = '$customer' AND grpcode = '010' ") or die(mysqli_error($conn));
                    $sparty = mysqli_fetch_array($gparty);

                    $ggodw = mysqli_query($conn, "SELECT `code`, `gcode` FROM `godown` where `code` = '$godown'") or die(mysqli_error($conn));
                    $sgodw = mysqli_fetch_array($ggodw);

                    $voucher = $pmain['voucher'];
                    $invno = $pmain['invno'];
                    $mydt = $pmain['mydt'];
                    $gcode = $sgodw['gcode'];

                    if ($pmain['canc_flag'] == 'C') {
                        $partynm = "CANCELLED";
                        $gstperc = $totgst = $billamt = $subtot = $totdisc = $taxable = 0;
                    } else {

                        $taxable0 = 0;
                        $taxable01 = 0;

                        $partynm = $sparty['name'];
                        $staff = $sstaf['name'];
                        $gstno = $pmain['gstno'];
                        $gstperc = $pmain['igst1'];
                        $totgst = $pmain['totgst1'];
                        $billamt = $pmain['billamt'];
                        $subtot = $pmain['grossamt'];
                        $totdisc = $pmain['discamt'];
                        $taxable = $pmain['taxableamt'];

                        if (round($taxable) == round($billamt)) {
                            $gst0 = $totgst;
                            $gst1 = 0;

                            $tottaxble0 = $taxable;
                            $tottaxble1 = 0;
                        } else {
                            $gst0 = 0;
                            $gst1 = $totgst;

                            $tottaxble0 = 0;
                            $tottaxble1 = $taxable;
                        }

                        $gst5 = 0;
                        $gst12 = 0;
                        $gst18 = 0;
                        $gst28 = 0;

                        $tottaxble5 = 0;
                        $tottaxble12 = 0;
                        $tottaxble18 = 0;
                        $tottaxble28 = 0;
                    }

                    $ctr++;

                    $contents .= $ctr . ",";
                    $contents .= $voucher . ",";
                    $contents .= "DE" . $invno . ",";
                    $contents .= $mydt . ",";
                    $contents .= $gcode . ",";
                    $contents .= $partynm . ",";
                    $contents .= $gstno . ",";
                    $contents .= $staff . ",";
                    $contents .= $subtot . ",";
                    $contents .= $totdisc . ",";
                    $contents .= $taxable . ",";

                    if ($oms == 1) {
                        $contents .= ",,,,,,,,,,,";
                    }

                    $contents .= $tottaxble0 . ",";
                    $contents .= $tottaxble1 . ",";
                    $contents .= $gst1 . ",";
                    $contents .= $tottaxble5 . ",";
                    $contents .= $gst5 . ",";
                    $contents .= $tottaxble12 . ",";
                    $contents .= $gst12 . ",";
                    $contents .= $tottaxble18 . ",";
                    $contents .= $gst18 . ",";
                    $contents .= $tottaxble28 . ",";
                    $contents .= $gst28 . ",";

                    if ($oms == 1) {
                        
                    } else {
                        $contents .= ",,,,,,,,,,,";
                    }
                    $contents .= $billamt . "\n";

                    ////////////

                    $grsubtot2 = $grsubtot2 + $subtot;
                    $grtotdis2 = $grtotdis2 + $totdisc;
                    $grtottaxable2 = $grtottaxable2 + $taxable;

                    $totpackamt = $totpackamt + $pmain['packamt'];
                    $totstamt = $totstamt + $pmain['stamt'];
                    $totothamt = $totothamt + $pmain['otheramt'];
                    $totbillamt = $totbillamt + $pmain['billamt'];

                    if ($oms == 1) {
                        $grtottaxbl0i = $grtottaxbl0i + $tottaxble0;
                        $grtottaxbl1i = $grtottaxbl1i + $tottaxble1;
                        $grtottaxbl5i = $grtottaxbl5i + $tottaxble5;
                        $grtottaxbl12i = $grtottaxbl12i + $tottaxble12;
                        $grtottaxbl18i = $grtottaxbl18i + $tottaxble18;
                        $grtottaxbl28i = $grtottaxbl28i + $tottaxble28;

                        $grtotgst0i = $grtotgst0i + $gst0;
                        $grtotgst1i = $grtotgst1i + $gst1;
                        $grtotgst5i = $grtotgst5i + $gst5;
                        $grtotgst12i = $grtotgst12i + $gst12;
                        $grtotgst18i = $grtotgst18i + $gst18;
                        $grtotgst28i = $grtotgst28i + $gst28;
                    } else {
                        $grtottaxbl0 = $grtottaxbl0 + $tottaxble0;
                        $grtottaxbl1 = $grtottaxbl1 + $tottaxble1;
                        $grtottaxbl5 = $grtottaxbl5 + $tottaxble5;
                        $grtottaxbl12 = $grtottaxbl12 + $tottaxble12;
                        $grtottaxbl18 = $grtottaxbl18 + $tottaxble18;
                        $grtottaxbl28 = $grtottaxbl28 + $tottaxble28;

                        $grtotgst0 = $grtotgst0 + $gst0;
                        $grtotgst1 = $grtotgst1 + $gst1;
                        $grtotgst5 = $grtotgst5 + $gst5;
                        $grtotgst12 = $grtotgst12 + $gst12;
                        $grtotgst18 = $grtotgst18 + $gst18;
                        $grtotgst28 = $grtotgst28 + $gst28;
                    }

                    $grtotbillamt = $grtotbillamt + $billamt;


                    if ($pmain['canc_flag'] == 'C') {
                        
                    } else {

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

                            if ($gsttype == 2) {
                                $contents .= "Sr,Barcode,Item Code,Item Name,HSN Code,Gram,Unit,Mrp,Batch,Qty.,Rate,Amount,Disc%,Disc. Amt.,Taxable Amount,CGST Rate,CGST Amount,SGST Rate,SGST Amount,Total\n";
                            } else if ($gsttype == 1) {
                                $contents .= "Sr,Barcode,Item Code,Item Name,HSN Code,Gram,Unit,Mrp,Batch,Qty.,Rate,Amount,Disc%,Disc. Amt.,Taxable Amount,IGST Rate,IGST Amount,Total\n";
                            } else {
                                $contents .= "Sr,Barcode,Item Code,Item Name,HSN Code,Gram,Unit,Mrp,Batch,Qty.,Rate,Amount,Disc%,Disc. Amt.,Taxable Amount,CGST Rate,CGST Amount,SGST Rate,SGST Amount,IGST Rate,IGST Amount,Total\n";
                            }

                            $ctr2 = 0;
                            $totqty = 0;
                            $totrate = 0;
                            $totamount = $totdiscount = $totsubtot = $totcgstamt = $totsgstamt = $totigstamt = 0;

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

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


                                $barcode = $tran['barcode'];
                                $itemcode = $tran['itemcode'];
                                $hsncode = $tran['hsncode'];
                                $gram = $tran['gram'];
                                $unit = $tran['unit'];
                                $mrp = $tran['mrp'];
                                $batch = $tran['batch'];
                                $qty = $tran['qty'];
                                $rate = $tran['rate'];
                                $amount = $tran['amount'];
                                $discperc = $tran['discperc'];
                                $discamt = $tran['discamt'];
                                $subtot = $tran['subtot'];
                                $cgstrate = $tran['cgstrate'];
                                $cgstamt = $tran['cgstamt'];
                                $sgstrate = $tran['sgstrate'];
                                $sgstamt = $tran['sgstamt'];
                                $igstrate = $tran['igstrate'];
                                $igstamt = $tran['igstamt'];
                                $total = $tran['total'];

                                $gitem = mysqli_query($conn, "SELECT  name FROM product WHERE prodcode = '$itemcode' ")or die(mysqli_error($conn));
                                $fitem = mysqli_fetch_array($gitem);
                                $itemname = $fitem['name'];

                                $ctr2++;
                                $contents .= $ctr2 . ",";
                                $contents .= $barcode . ",";
                                $contents .= $itemcode . ",";
                                $contents .= $itemname . ",";
                                $contents .= $hsncode . ",";
                                $contents .= $gram . ",";
                                $contents .= $unit . ",";
                                $contents .= $mrp . ",";
                                $contents .= $batch . ",";
                                $contents .= $qty . ",";
                                $contents .= $rate . ",";
                                $contents .= $amount . ",";
                                $contents .= $discperc . ",";
                                $contents .= $discamt . ",";
                                $contents .= $subtot . ",";
                                if ($gsttype == 2) {
                                    $contents .= $cgstrate . ",";
                                    $contents .= $cgstamt . ",";
                                    $contents .= $sgstrate . ",";
                                    $contents .= $sgstamt . ",";
                                } else if ($gsttype == 1) {
                                    $contents .= $igstrate . ",";
                                    $contents .= $igstamt . ",";
                                } else {
                                    $contents .= $cgstrate . ",";
                                    $contents .= $cgstamt . ",";
                                    $contents .= $sgstrate . ",";
                                    $contents .= $sgstamt . ",";
                                    $contents .= $igstrate . ",";
                                    $contents .= $igstamt . ",";
                                }
                                $contents .= $total . "\n";

                                $totqty = $totqty + $tran['qty'];
                                $totamount = $totamount + $tran['amount'];
                                $totdiscount = $totdiscount + $tran['discamt'];
                                $totsubtot = $totsubtot + $tran['subtot'];
                                $totcgstamt = $totcgstamt + $tran['cgstamt'];
                                $totsgstamt = $totsgstamt + $tran['sgstamt'];
                                $totigstamt = $totigstamt + $tran['igstamt'];
                            }


                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "" . ",";
                            $contents .= "Total" . ",";
                            $contents .= $totqty . ",";
                            $contents .= "" . ",";
                            $contents .= $totamount . ",";
                            $contents .= "" . ",";
                            $contents .= $totdiscount . ",";
                            $contents .= $totsubtot . ",";
                            if ($gsttype == 2) {
                                $contents .= ",";
                                $contents .= $totcgstamt . ",";
                                $contents .= "" . ",";
                                $contents .= $totsgstamt . ",";
                                $contents .= "" . ",";
                            } else if ($gsttype == 1) {
                                $contents .= "" . ",";
                                $contents .= $totigstamt . ",";
                            } else {
                                $contents .= ",";
                                $contents .= $totcgstamt . ",";
                                $contents .= "" . ",";
                                $contents .= $totsgstamt . ",";
                                $contents .= "" . ",";
                                $contents .= $totigstamt . ",";
                            }
                            $contents .= $totamount . "\n";
                        }
                    }
                }

                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "Total" . ",";
                $contents .= $grsubtot2 . ",";
                $contents .= $grtotdis2 . ",";
                $contents .= $grtottaxable2 . ",";

                $contents .= $grtottaxbl0 . ",";

                $contents .= $grtottaxbl1 . ",";
                $contents .= $grtotgst1 . ",";

                $contents .= $grtottaxbl5 . ",";
                $contents .= $grtotgst5 . ",";

                $contents .= $grtottaxbl12 . ",";
                $contents .= $grtotgst12 . ",";

                $contents .= $grtottaxbl18 . ",";
                $contents .= $grtotgst18 . ",";

                $contents .= $grtottaxbl28 . ",";
                $contents .= $grtotgst28 . ",";

                ////////////

                $contents .= $grtottaxbl0i . ",";

                $contents .= $grtottaxbl1i . ",";
                $contents .= $grtotgst1i . ",";

                $contents .= $grtottaxbl5i . ",";
                $contents .= $grtotgst5i . ",";

                $contents .= $grtottaxbl12i . ",";
                $contents .= $grtotgst12i . ",";

                $contents .= $grtottaxbl18i . ",";
                $contents .= $grtotgst18i . ",";

                $contents .= $grtottaxbl28i . ",";
                $contents .= $grtotgst28i . ",";

                $contents .= $grtotbillamt . "\n";
            }
//            <!--end deemsalereg-->

            if ($regtype == '' || $regtype == 'O') {

                $getosmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `slotmain` WHERE `vouchdt` >= '$mfrmdate' 
                                     AND `vouchdt` <= '$mtodate' $partycond $consigcond $consigcond $godowncond ") or die(mysqli_error($conn));


                $getno4 = mysqli_num_rows($getosmain);
                if ($getno4 > 0) {

                    $contents .= ",,,,,Other Sale Register\n";
                    $contents .= ",,,,,Total No. Record - $getno4\n";
                    $contents .= "Sr.,Voucher,Invoice No.,Date,Godown,Party Name,GST No.,Amount,Disc Amt.,Taxable,CGST Amt.,SGST Amt.,IGST Amt.,Bill Amount,\n";

                    $ctr = 0;
                    $totamt = 0;
                    $totdiscamt = 0;
                    $tottaxable = 0;
                    $totgst5 = 0;
                    $totgst12 = 0;
                    $totgst18 = 0;
                    $totgst28 = 0;

                    $grsubtot = $grtotdis = $grtottaxable = $grtottaxbl0 = $grtottaxbl1 = $grtotgst1 = $grtottaxbl5 = $grtotgst5 = 0;
                    $grtottaxbl12 = $grtotgst12 = $grtottaxbl18 = $grtotgst18 = $grtottaxbl28 = $grtotgst28 = 0;

                    $grtottaxbl0i = $grtottaxbl1i = $grtotgst1i = $grtottaxbl5i = $grtotgst5i = 0;
                    $grtottaxbl12i = $grtotgst12i = $grtottaxbl18i = $grtotgst18i = $grtottaxbl28i = $grtotgst28i = 0;
                    $grtotbillamt = 0;


                    $totpackamt = $totstamt = $totothamt = $totbillamt = 0;
                    $grsubtot = $grtotdis = $grtottaxable = $grtotgst = $grtotbillamt = 0;


                    if ($gsttype == '') {
                        $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                    AND `vouchdt` <= '$mtodate' $partycond $godowncond $staffcond $ptypecond $pcond order by invno") or die(mysqli_error($conn));
                    } else {
                        $getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                    AND `vouchdt` <= '$mtodate' $partycond $godowncond $staffcond $ptypecond $gstcond $pcond order by invno") or die(mysqli_error($conn));
                    }


                    while ($pmain2 = mysqli_fetch_array($getosmain)) {
                        if ($pmain2['paidby'] == "C") {
                            $ptype = "CASH";
                        } else {
                            $ptype = "CREDIT";
                        }

                        $godown = $pmain2['godown'];
                        $customer = $pmain2['party'];
                        $staf = $pmain2['staff'];

                        $gstaf = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$staf' AND grpcode='040'  ") or die(mysqli_error($conn));
                        $sstaf = mysqli_fetch_array($gstaf);

                        $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$customer' AND grpcode='010'  ") or die(mysqli_error($conn));
                        $sparty = mysqli_fetch_array($gparty);

                        $ggodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` where `code` = '$godown'") or die(mysqli_error($conn));
                        $sgodw = mysqli_fetch_array($ggodw);


                        $voucher = $pmain2['voucher'];
                        $lslvouchno = $pmain2['vouchno'];
                        $invno = $pmain2['invno'];
                        $mydt = $pmain2['mydt'];
                        $gcode = $sgodw['gcode'];
                        $partynm = $sparty['name'];
                        $staff = $sstaf['name'];
                        $gstno = $pmain2['gstno'];
                        $cgst = $pmain2['cgstamt'];
                        $sgst = $pmain2['sgstamt'];
                        $igst = $pmain2['igstamt'];
                        $billamt = $pmain2['billamt'];
                        $subtot = $pmain2['grossamt'];
                        $totdisc = $pmain2['discamt'];
                        $taxable = $pmain2['taxableamt'];


                        $ctr++;
                        $contents .= $ctr . ",";
                        $contents .= $voucher . ",";
                        $contents .= $invno . ",";
                        $contents .= $mydt . ",";
                        $contents .= $gcode . ",";
                        $contents .= $partynm . ",";
                        $contents .= $gstno . ",";
                        $contents .= $subtot . ",";
                        $contents .= $totdisc . ",";
                        $contents .= $taxable . ",";
                        $contents .= $cgst . ",";
                        $contents .= $sgst . ",";
                        $contents .= $igst . ",";
                        $contents .= $billamt . "\n";


                        $grsubtot = $grsubtot + $subtot;
                        $grtotdis = $grtotdis + $totdisc;
                        $grtottaxable = $grtottaxable + $taxable;
                        $grtotcgst = $grtotcgst + $cgst;
                        $grtotsgst = $grtotsgst + $sgst;
                        $grtotigst = $grtotigst + $igst;
                        $grtotbillamt = $grtotbillamt + $billamt;

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

                            $contents .= "Sr., Descrition, Amount\n";


                            $ctr2 = 0;
                            $totqty = 0;
                            $totrate = 0;
                            $trtotal = 0;
                            $totamount = $totdiscount = $totsubtot = $totcgstamt = $totsgstamt = $totigstamt2 = 0;

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

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


                                $descrip = $tran['descrip'];
                                $amount = $tran['amount'];


                                $gitem = mysqli_query($conn, "SELECT  name FROM product WHERE prodcode = '$itemcode' ")or die(mysqli_error($conn));
                                $fitem = mysqli_fetch_array($gitem);
                                $itemname = $fitem['name'];

                                $ctr2++;
                                $contents .= $ctr2 . ",";
                                $contents .= $descrip . ",";
                                $contents .= $amount . "\n";

                                $totamount = $totamount + $tran['amount'];
                            }

                            $contents .= ",Total" . ",";
                            $contents .= $totamount . "\n";
                        }
                    }
                    $contents .= ",";
                    $contents .= ",,,,Total" . ",,";
                    $contents .= $grsubtot . ",";
                    $contents .= $grtotdis . ",";
                    $contents .= $grtottaxable . ",";
                    $contents .= $grtotcgst . ",";
                    $contents .= $grtotsgst . ",";
                    $contents .= $grtotigst . ",";
                    $contents .= $grtotbillamt . "\n";
                }
            }


            if ($regtype == '' || $regtype == 'E') {
//            <!-- export register-->
                $contents .= "\n";

                $contents .= ",,,,,Export - Invoice Register\n";
                $contents .= "Sr.,Voucher, Invoice No., Date, Godown, Party Name, Consignee,Subtotal,Disc. Amt.,Net Amt.,INR Amt.,\n";

                $ctr = 0;
                $totbillamt3 = 0;
                $totinramt = 0;
                if (!empty($party)) {
                    $partycond2 = " AND `partyname` = '$party' ";
                }
                $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`invdate`, '%d-%m-%Y') as mydt FROM `salemainexp` WHERE `invdate` >= '$mfrmdate'
                                     AND `invdate` <= '$mtodate' $partycond2 $consigcond $consigcond $godowncond ") or die(mysqli_error($conn));


                while ($pmain = mysqli_fetch_array($getpmain)) {

                    $customer = $pmain['partyname'];
                    $consignee = $pmain['consignee'];
                    $gcode = $pmain['godown'];

                    $gparty = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` where `accode` = '$customer' AND grpcode = '010' ") or die(mysqli_error($conn));
                    $sparty = mysqli_fetch_array($gparty);

                    $gconsig = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` where `accode` = '$consignee' AND grpcode = '010' ") or die(mysqli_error($conn));
                    $consig = mysqli_fetch_array($gconsig);

                    $ggodwn = mysqli_query($conn, "SELECT `code`, `name`, `gcode` FROM `godown` where `code` = '$gcode' ") or die(mysqli_error($conn));
                    $godwn = mysqli_fetch_array($ggodwn);

                    $voucher = $pmain['voucher'];
                    $invno = $pmain['invoice'];
                    $vouchdt = $pmain['mydt'];
                    $godown = $godwn['gcode'];

                    if ($pmain['canc_flag'] == "C") {

                        $party = "CANCELLED";
                        $consigne = "";
                        $grossamt = $discount = $billamt = $inramt = 0;
                    } else {


                        $party = $sparty['name'];
                        $consigne = $consig['name'];
                        $godown = $godwn['gcode'];
                        $grossamt = $pmain['grossamt'];
                        $discount = $pmain['discount'];
                        $billamt = $pmain['billamt'];
                        $inramt = $pmain['inramt'];
                    }

                    $ctr++;
                    $contents .= $ctr . ",";
                    $contents .= $voucher . ",";
                    $contents .= $invno . ",";
                    $contents .= $vouchdt . ",";
                    $contents .= $godown . ",";
                    $contents .= $party . ",";
                    $contents .= $consigne . ",";
                    $contents .= $grossamt . ",";
                    $contents .= $discount . ",";
                    $contents .= $billamt . ",";
                    $contents .= $inramt . "\n";

                    $totsubtot = $totsubtot + $grossamt;
                    $totdiscount = $totdiscount + $discount;
                    $totbillamt3 = $totbillamt3 + $billamt;
                    $totinramt = $totinramt + $inramt;

                    if ($pmain['canc_flag'] == 'C') {
                        
                    } else {


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


                            $contents .= "Sr,Barcode,Product Code,Product Name,HSN Code,Gram,Unit,Mrp,Batch,Qty.,Rate,Amount,\n";

                            $ctr2 = 0;
                            $trtotqty = 0;
                            $trtotamount = 0;

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

                                $barcode = $tran['barcode'];
                                $itemcode = $tran['itemcode'];
//                        $itemname = $tran['itemname'];
                                $hsncode = $tran['hsncode'];
                                $gram = $tran['gram'];
                                $unit = $tran['unit'];
                                $mrp = $tran['mrp'];
                                $batch = $tran['batch'];
                                $qty = $tran['qty'];
                                $rate = $tran['rate'];
                                $amount = $tran['amount'];

                                $gitem = mysqli_query($conn, "SELECT  name FROM product WHERE prodcode = '$itemcode' ")or die(mysqli_error($conn));
                                $fitem = mysqli_fetch_array($gitem);
                                $itemname = $fitem['name'];
                                $ctr2++;

                                $contents .= $ctr2 . ",";
                                $contents .= $barcode . ",";
                                $contents .= $itemcode . ",";
                                $contents .= $itemname . ",";
                                $contents .= $hsncode . ",";
                                $contents .= $gram . ",";
                                $contents .= $unit . ",";
                                $contents .= $mrp . ",";
                                $contents .= $batch . ",";
                                $contents .= $qty . ",";
                                $contents .= $rate . ",";
                                $contents .= $amount . "\n";

                                $trtotqty = $trtotqty + $qty;
                                $trtotamount = $trtotamount + $amount;
                            }

                            $contents .= ",,,,,,,,Total" . ",";
                            $contents .= $trtotqty . ",";
                            $contents .= " " . ",";
                            $contents .= $trtotamoun . "\n";
                        }
                    }
                }
                $contents .= ",,,,,,Total" . ",";
                $contents .= $totsubtot . ",";
                $contents .= $totdiscount . ",";
                $contents .= $totbillamt3 . ",";
                $contents .= $totinramt . "\n";
//<!--end export register-->  
            }
        }
        $contents = strip_tags($contents);

        if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
            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;
        }
    }
    ?>