<?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 = 'deexmain';
$trandb = 'deextran';
$zroot = $_SESSION["zdbroot"];

$formname = 'Deemed Sale Register';
$file = 'tisalereg_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' ";
    }
    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'];

    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")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")or die(mysqli_error($conn));
    }
    $get = mysqli_num_rows($getpmain);

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th{
            /*background: #26A69A;*/
        }
        
        .colr{
            /*background: #26A69A;*/
            font-size: 17px;
        }
        table tr td{
            font-size: 14px;
        }

    </style>
    </head>
    <body>


        <div class="container-fluid">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">

                    <div class="col-lg-12 pad">
                        <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-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:4%;">Sr</th>
                                    <th style="width:6%;">Invoice No.</th>
                                    <th style="width:7%;">Date</th>
                                    <th style="width:6%;">Godown</th>
                                    <?php if (empty($party)) { ?>
                                        <th style="width:19%;">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: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 = $totbillamt = 0;

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

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

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

                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name`,gstno FROM `accmast` where `accode` = '$customer'") 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['invno'];
                                    $mydt = $pmain['mydt'];
                                    $gcode = $sgodw['gcode'];
                                    $partynm = $sparty['name'];
                                    $staff = $pmain['username'];
                                    
                                    $gstno = $sparty['gstno'];
                                    $gstperc = $pmain['igst1'];
                                    $totgst = $pmain['totgst1'];
                                    $billamt = $pmain['billamt'];
                                    $subtot = $pmain['grossamt'];
                                    $totdisc = $pmain['discamt'];
                                    $taxable = $pmain['taxableamt'];

                                    $ctr++;
                                    Echo"<tr>";
                                    Echo"<td>$ctr</td>";
                                    Echo"<td>$voucher</td>";
                                    Echo"<td>$mydt</td>";
                                    Echo"<td>$gcode</td>";
                                    if (empty($party)) {

                                        Echo"<td>$partynm</td>";
                                    }
                                    Echo"<td>$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'>$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>";



                                    $grsubtot = $grsubtot + $subtot;
                                    $grtotdis = $grtotdis + $totdisc;
                                    $grtottaxable = $grtottaxable + $taxable;
                                    $grtotgst1 = $grtotgst1 + $totgst;
                                    $grtotbillamt = $grtotbillamt + $billamt;

                                    $totpackamt = $totpackamt + $pmain['packamt'];
                                    $totstamt = $totstamt + $pmain['stamt'];
                                    $totothamt = $totothamt + $pmain['otheramt'];
                                    $totbillamt = $totbillamt + $pmain['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 "<thead>";
                                        Echo "<tr class='mySearchTr2'>";
                                        Echo "<th style='width:5%;' class='dth'>Sr</th>";
                                        Echo "<th style='width:5%;' class='dth'>Barcode</th>";
                                        Echo "<th style='width:6%;' class='dth'>Product Code</th>";
                                        Echo "<th style='width:10%;' class='dth'>Product Name</th>";
                                        Echo "<th style='width:5%;' 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'>Mrp</th>";
                                        Echo "<th style='width:5%;' class='dth'>Batch</th>";
                                        Echo "<th style='width:5%;' class='dth'>Qty.</th>";
                                        Echo "<th style='width:5%;' class='dth'>Rate</th>";
                                        Echo "<th style='width:5%;' class='dth'>Amount</th>";
                                        Echo "<th style='width:5%;' class='dth'>Disc%</th>";
                                        Echo "<th style='width:5%;' class='dth'>Disc. Amt.</th>";
                                        Echo "<th style='width:5%;' class='dth'>Taxable Amount</th>";
                                        Echo "<th style='width:5%;' class='dth'>IGST Rate</th>";
                                        Echo "<th style='width:5%;' class='dth'>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 `$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'];
                                            $amount = $tran['amount'];



                                            $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>";
                                            Echo "<td class='text-right'>$igstrate</td>";
                                            Echo "<td class='text-right'>$igstamt</td>";
                                            Echo "<td class='text-right'>$amount</td>";
                                            Echo "</tr>";

                                            $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'];
                                        }

                                        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>";
                                            Echo "<td class='text-right dth'></td>";
                                            Echo "<td class='text-right dth'>$totigstamt2</td>";
                                        Echo "<td class='text-right dth'>$totamount</td>";
                                        Echo "</tr>";
                                        Echo "</table>";
                                    }
                                }
                                Echo "<tr>";
                                Echo "<td class='text-right colr' colspan='7'><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></b></td>";
                                Echo "<td class='numeric text-right colr '><b>$grtotgst1</b></td>";
                                Echo "<td class='numeric text-right colr '><b>$grtotbillamt</b></td>";
                                Echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div> 
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
    $party = $_POST["party"];
    $godown = $_POST["godown"];
    $staff = $_POST["staff"];
    $ptype = $_POST["ptype"];
    $dtype = $_POST["dtype"];
    $gsttype = $_POST["gsttype"];

    if (!empty($party)) {
        $partycond = " AND `party` = '$party' ";
    }
    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 (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");
    }

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

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



    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 ")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 ")or die(mysqli_error($conn));
    }
    $get = mysqli_num_rows($getpmain);


    $contents .= "Sr,Voucher,Party Name, GST No., Staff, Amount, Disc Amt., Taxable, CGST 2.5%, SGST 2.5%, IGST 5%, CGST 6%, SGST 6%, IGST 12%, CGST 9%, SGST 9%, IGST 18%, CGST 14%, SGST 14%, IGST 28%, Bill Amount\n";

    $ctr = 0;
    $totamt = 0;
    $totdiscamt = 0;
    $tottaxable = 0;
    $totgst5 = 0;
    $totgst12 = 0;
    $totgst18 = 0;
    $totgst28 = 0;
    $totpackamt = $totstamt = $totothamt = $totbillamt = 0;

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

        $gstaf = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where `accode` = '$pmain[staff]'") or die(mysqli_error($conn));
        $sstaf = mysqli_fetch_array($gstaf);

        $gparty = mysqli_query($conn, "SELECT `accode`,`name`, `gstno` FROM `accmast` where `accode` = '$pmain[party]'") or die(mysqli_error($conn));
        $sparty = mysqli_fetch_array($gparty);
        $pname = str_replace(",", " ", $sparty['name']);
        $gstno = $sparty['gstno'];

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

        $cgst2_5 = $pmain['cgst5'];
        $sgst2_5 = $pmain['sgst5'];
        $igst5 = $pmain['igst5'];
        $cgst6 = $pmain['cgst12'];
        $sgst6 = $pmain['sgst12'];
        $igst12 = $pmain['igst12'];
        $cgst9 = $pmain['cgst18'];
        $sgst9 = $pmain['sgst18'];
        $igst18 = $pmain['igst18'];
        $cgst14 = $pmain['cgst28'];
        $sgst14 = $pmain['sgst28'];
        $igst28 = $pmain['igst28'];
        $billamt = $pmain['billamt'];
        $subtot = $pmain['grossamt'];
        $totdisc = $pmain['discamt'];
        $taxable = $pmain['taxableamt'];


        $ctr++;
        $contents .= $ctr . ",";
        $contents .= $pmain['vouchno'] . ",";
        $contents .= $pname . ",";
        $contents .= $gstno . ",";
        $contents .= $pmain['username'] . ",";
        $contents .= $subtot . ",";
        $contents .= $totdisc . ",";
        $contents .= $taxable . ",";
        $contents .= $cgst2_5 . ",";
        $contents .= $sgst2_5 . ",";
        $contents .= $igst5 . ",";
        $contents .= $cgst6 . ",";
        $contents .= $sgst6 . ",";
        $contents .= $igst12 . ",";
        $contents .= $cgst9 . ",";
        $contents .= $sgst9 . ",";
        $contents .= $igst18 . ",";
        $contents .= $cgst14 . ",";
        $contents .= $sgst14 . ",";
        $contents .= $igst28 . ",";
        $contents .= $billamt . "\n";




        $grsubtot = $grsubtot + $subtot;
        $grtotdis = $grtotdis + $totdisc;
        $grtottaxable = $grtottaxable + $taxable;
        $grtotcgst2_5 = $grtotcgst2_5 + $cgst2_5;
        $grtotsgst2_5 = $grtotsgst2_5 + $sgst2_5;
        $grtotgst5 = $grtotgst5 + $igst5;
        $grtotcgst6 = $grtotcgst6 + $cgst6;
        $grtotsgst6 = $grtotsgst6 + $sgst6;
        $grtotgst12 = $grtotgst12 + $igst12;
        $grtotcgst9 = $grtotcgst9 + $cgst9;
        $grtotsgst9 = $grtotsgst9 + $sgst9;
        $grtotgst18 = $grtotgst18 + $igst18;
        $grtotcgst14 = $grtotcgst14 + $cgst14;
        $grtotsgst14 = $grtotsgst14 + $sgst14;
        $grtotgst28 = $grtotgst28 + $igst28;
        $grtotbillamt = $grtotbillamt + $billamt;

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


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

            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";
            } elseif ($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";
            }

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

                $ctr2++;




                $contents .= $ctr2 . ",";
                $contents .= $tran['barcode'] . ",";
                $contents .= $tran['itemcode'] . ",";
                $contents .= $tran['itemname'] . ",";
                $contents .= $tran['hsncode'] . ",";
                $contents .= $tran['gram'] . ",";
                $contents .= $tran['unit'] . ",";
                $contents .= $tran['mrp'] . ",";
                $contents .= $tran['batch'] . ",";
                $contents .= $tran['qty'] . ",";
                $contents .= $tran['rate'] . ",";
                $contents .= $tran['amount'] . ",";
                $contents .= $tran['discperc'] . ",";
                $contents .= $tran['discamt'] . ",";
                $contents .= $tran['subtot'] . ",";
                if ($gsttype == 1) {
                    $contents .= $tran['igstrate'] . ",";
                    $contents .= $tran['igstamt'] . ",";
                } elseif ($gsttype == 2) {
                    $contents .= $tran['cgstrate'] . ",";
                    $contents .= $tran['cgstamt'] . ",";
                    $contents .= $tran['sgstrate'] . ",";
                    $contents .= $tran['sgstamt'] . ",";
                }
                $contents .= $tran['amount'] . "\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 . ",";
            }
            $contents .= $totamount . "\n";
        }
    }

    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= "Total" . ",";
    $contents .= $grsubtot . ",";
    $contents .= $grtotdis . ",";
    $contents .= $grtottaxable . ",";
    $contents .= $grtotcgst2_5 . ",";
    $contents .= $grtotsgst2_5 . ",";
    $contents .= $grtotgst5 . ",";
    $contents .= $grtotcgst6 . ",";
    $contents .= $grtotsgst6 . ",";
    $contents .= $grtotgst12 . ",";
    $contents .= $grtotcgst9 . ",";
    $contents .= $grtotsgst9 . ",";
    $contents .= $grtotgst18 . ",";
    $contents .= $grtotcgst14 . ",";
    $contents .= $grtotsgst14 . ",";
    $contents .= $grtotgst28 . ",";
    $contents .= $grtotbillamt . "\n";

    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1) {
        header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
        print $contents;
    } else {
        header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
?>