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

$maindb = 'credmain';
$trandb = 'credtran';
$formname = "Credit Note Register";
$file = "ticredreg_fr.php";
$keycond = "AND `credtype`='G'";


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


$type = $_POST["type"];

if ($type == 1) {
    $keycond = " ";
} else if ($type == 2) {
    $keycond = " and `credtype` = 'G'";
} else if ($type == 3) {
    $keycond = " and `credtype` = 'O'";
}


$party = $_POST["party"];
$dtype = $_POST["dtype"];
$gsttype = $_POST["gsttype"];

$dtypecond = "";
$mscopecond = "";

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

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

if (!empty($party)) {
    $partycond = " AND `party`='$party' ";
}

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

if (!empty($dtype)) {
    $dtypecond = " AND `dtype`='$dtype' ";
}

if (!empty($scope)) {
    $mscopecond = " AND `type`='$scope' ";
}

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

$getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `credmain` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate'  $keycond $partycond $gstcond ")or die(mysqli_error($conn));
$getno = mysqli_num_rows($getpmain);

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

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        table tr td{
            font-size: 14px;
        }
    </style>
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-bordered table-condensed">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:3%">Sr</th>
                                <th style="width:4%">Voucher</th>
                                <th style="width:9%">Date</th>
                                <th style="width:23%">Party</th>
                                <th style="width:8%">GST No.</th>
                                <th style="width:4%">Godown</th>
                                <th style="width:7%" class="text-right">Amount</th>
                                <th style="width:6%" class="text-right">Discount</th>
                                <th style="width:8%" class="text-right">Taxable</th>
                                <th style="width:5%" class="text-right">GST 5%</th>
                                <th style="width:5%" class="text-right">GST 12%</th>
                                <th style="width:5%" class="text-right">GST 18%</th>
                                <th style="width:5%" class="text-right">GST 28%</th>
                                <th style="width:8%" class="text-right">Bill Amt.</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = 0;
                            $totdisc = 0;
                            $totbill = 0;
                            $gst5 = $gst12 = $gst18 = $gst18 = $gst28 = 0;
                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $getsupp = mysqli_query($conn, "SELECT `accode`,`name`,`gstno` FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
                                $supp = mysqli_fetch_array($getsupp);
                                
                                $getgodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` WHERE `code`='$pmain[godown]'")or die(mysqli_error($conn));
                                $godw = mysqli_fetch_array($getgodw);

                                $vouchno = $pmain['vouchno'];
                                $mydt = $pmain['mydt'];
                                $supplier = $supp['name'];
                                $suppgst = $supp['gstno'];
                                
                                $godown = $godw['gcode'];
                                $gst5 = $gst12 = $gst18 = $gst18 = $gst28 = 0;

                                if ($pmain["credtype"] == "O") {
                                    if ($pmain['cgstperc'] == "2.5") {
                                        $gst5 = $pmain['cgstamt'] + $pmain["sgstamt"];
                                    }
                                    if ($pmain['cgstperc'] == "6") {
                                        $gst12 = $pmain['cgstamt'] + $pmain["sgstamt"];
                                    }
                                    if ($pmain['cgstperc'] == "9") {
                                        $gst18 = $pmain['cgstamt'] + $pmain["sgstamt"];
                                    }
                                    if ($pmain['cgstperc'] == "14") {
                                        $gst28 = $pmain['cgstamt'] + $pmain["sgstamt"];
                                    }
                                } else {
                                    $gst5 = $pmain['totgst5'];
                                    $gst12 = $pmain['totgst12'];
                                    $gst18 = $pmain['totgst18'];
                                    $gst28 = $pmain['totgst28'];
                                }
                                $billamt = $pmain['billamt'];
                                $subtot = $pmain['grossamt'];
                                $totdisc = $pmain['discamt'];
                                $taxable = $pmain['taxableamt'];
                                $credtype = $pmain['credtype'];

                                if ($pmain["credtype"] == "O") {
                                    $post_file = 'credoth.php';
                                } else {
                                    $post_file = 'crednote.php';
                                }


                                $ctr++;
                                Echo "<tr id='boldfont' data-href='$post_file?mode=edit&vouchno=$vouchno' class='clickable-row2' [target]='_blank'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$vouchno</td>";
                                Echo "<td>$mydt</td>";
                                Echo "<td style='font-size:12px'>$supplier</td>";
                                Echo "<td style='font-size:11px'>$suppgst</td>";
                                Echo "<td>$godown</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'>$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;
                                $grtotdisc = $grtotdisc + $totdisc;
                                $grtaxable = $grtaxable + $taxable;
                                $grgst5 = $grgst5 + $gst5;
                                $grgst12 = $grgst12 + $gst12;
                                $grgst18 = $grgst18 + $gst18;
                                $grgst28 = $grgst28 + $gst28;
                                $grtotbillamt = $grtotbillamt + $billamt;

                                
                                $totbill = $totbill + $pmain['netamt'];
                                $totamt = $totamt + $pmain['subtotal'];
                                $totdisc = $totdisc + $pmain['discount'];

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

                                        Echo "<tr style='height:10px;'>";
                                        Echo "<td colspan='25'>";
                                        Echo "<table style='width:100%;'  class='table table-condensed table-hover table-bordered'>";

                                        Echo "<tr>";
                                        Echo "<td style='background-color:  white;width:5%;'>Sr</td>";
                                        Echo "<td style='background-color:  white;width:10%; class='numeric''>Barcode</td>";
                                        Echo "<td style='background-color:  white';width:6%; >Product Code</td>";
                                        Echo "<td style='background-color:  white';width:6%; >Product Name</td>";
                                        Echo "<td style='background-color:  white';width:6%; >HSN Code</td>";
                                        Echo "<td style='background-color:  white';width:6%; >Gram</td>";
                                        Echo "<td style='background-color:  white;width:6%;' >Unit</td>";
                                        Echo "<td style='background-color:  white;width:6%;' class='numeric'>Mrp</td>";
                                        Echo "<td style='background-color:  white;width:6%;' class='numeric'>Batch</td>";
                                        Echo "<td style='background-color:  white;width:6%;' class='numeric'>Qty.</td>";
                                        Echo "<td style='background-color:  white;width:6%;' class='numeric'>Rate</td>";
                                        Echo "<td style='background-color:  white;width:6%;' class='numeric'>Amount</td>";
                                        Echo "<td style='background-color:  white;width:6%;' class='numeric'>Discperc</td>";
                                        Echo "<td style='background-color:  white;width:6%;' class='numeric'>Discount</td>";
                                        Echo "<td style='background-color:  white;width:6%;' class='numeric'>Taxable Amount</td>";
                                        if ($gsttype == 2) {
                                            Echo "<td style='background-color:  white;width:6%;' class='numeric'>CGST Rate</td>";
                                            Echo "<td style='background-color:  white;width:6%;' class='numeric'>CGST Amount</td>";
                                            Echo "<td style='background-color:  white;width:6%;' class='numeric'>SGST Rate</td>";
                                            Echo "<td style='background-color:  white;width:6%;' class='numeric'>SGST Amount</td>";
                                        } elseif ($gsttype == 1) {
                                            Echo "<td style='background-color:  white;width:6%;' class='numeric'>IGST Rate</td>";
                                            Echo "<td style='background-color:  white;width:6%;' class='numeric'>IGST Amount</td>";
                                        }
                                        Echo "<td style='background-color:  white;width:6%;' class='numeric'>Total</td>";
                                        Echo "</tr>";


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

                                            if ($totcgst != '0.00') {
                                                $discount = $totdisc;
                                                $discperc = '10.714';
                                                $cgstamt = $totcgst;
                                                $sgstamt = $totsgst;
                                                $cgstrate = 6;
                                                $sgstrate = 6;
                                                $trantaxable = $pmain[total];
                                                $trantotal = $tran[amount];
                                            } else {
                                                $discount = $tran[discamt];
                                                $discperc = $tran[discperc];
                                                $cgstamt = $tran[cgstamt];
                                                $sgstamt = $tran[sgstamt];
                                                $cgstrate = $tran[cgstrate];
                                                $sgstrate = $tran[sgstrate];
                                                $trantaxable = $tran[subtot];
                                                $trantotal = $tran[total];
                                            }

                                            $ctr2++;
                                            Echo "<tr>";
                                            Echo "<td>$ctr2</td>";
                                            Echo "<td>$tran[barcode]</td>";
                                            Echo "<td>$tran[itemcode]</td>";
                                            Echo "<td>$tran[itemname]</td>";
                                            Echo "<td>$tran[hsncode]</td>";
                                            Echo "<td>$tran[gram]</td>";
                                            Echo "<td>$tran[unit]</td>";
                                            Echo "<td>$tran[mrp]</td>";
                                            Echo "<td>$tran[batch]</td>";
                                            Echo "<td class='numeric'>$tran[qty]</td>";
                                            Echo "<td class='numeric'>$tran[rate]</td>";
                                            Echo "<td class='numeric'>$tran[amount]</td>";
                                            Echo "<td class='numeric'>$discperc</td>";
                                            Echo "<td class='numeric'>$discount</td>";
                                            Echo "<td class='numeric'>$trantaxable</td>";
                                            if ($gsttype == 1) {
                                                Echo "<td class='numeric'>$tran[igstrate]</td>";
                                                Echo "<td class='numeric'>$tran[igstamt]</td>";
                                            } elseif ($gsttype == 2) {
                                                Echo "<td class='numeric'>$cgstrate</td>";
                                                Echo "<td class='numeric'>$cgstamt</td>";
                                                Echo "<td class='numeric'>$sgstrate</td>";
                                                Echo "<td class='numeric'>$sgstamt</td>";
                                            }
                                            Echo "<td class='numeric'>$trantotal</td>";
                                            Echo "</tr>";

                                            $totqty = $totqty + $tran[qty];
                                            $totrate = $totrate + $tran[rate];
                                            $totamount = $totamount + $tran[amount];
                                            $totdisc2 = $totdisc2 + $discount;
                                            $totsubtot = $totsubtot + $trantaxable;
                                            $totcgstamt2 = $totcgstamt2 + $cgstamt;
                                            $totsgstamt2 = $totsgstamt2 + $sgstamt;
                                            $totigstamt2 = $totigstamt2 + $tran[igstamt];
                                            $totamt = $totamt + $trantotal;
                                            $ftot = $ftot + $tran[subtot];
                                        }
                                        Echo "<tr>";
                                        Echo "<td></td>";
                                        Echo "<td></td>";
                                        Echo "<td></td>";
                                        Echo "<td></td>";
                                        Echo "<td></td>";
                                        Echo "<td></td>";
                                        Echo "<td></td>";
                                        Echo "<td></td>";
                                        Echo "<td>Total</td>";
                                        Echo "<td>$totqty</td>";
                                        Echo "<td></td>";
                                        Echo "<td>$totamount</td>";
                                        Echo "<td></td>";
                                        Echo "<td>$totdisc2</td>";
                                        Echo "<td>$totsubtot</td>";
                                        if ($gsttype == 1) {
                                            Echo "<td></td>";
                                            Echo "<td>$totigstamt2</td>";
                                        } elseif ($gsttype == 2) {
                                            Echo "<td></td>";
                                            Echo "<td>$totcgstamt2</td>";
                                            Echo "<td></td>";
                                            Echo "<td>$totsgstamt2</td>";
                                        }
                                        Echo "<td>$totamt</td>";
                                        Echo "</tr>";

                                        Echo "</table>";
                                        Echo "</td>";
                                        Echo "</tr>";
                                    }
                                } elseif ($credtype == "O") {
                                    if ($_POST["dtype"] == 2) {

                                        Echo "<tr style='height:10px;'>";
                                        Echo "<td colspan='25'>";
                                        Echo "<table style='width:100%;'  class='table table-condensed table-bordered'>";
                                        Echo "<tr class='mySearchTr2'>";
                                        Echo "<td style='width:5%;'>Sr</td>";
                                        Echo "<td style='width:85%;'>Description</td>";
                                        Echo "<td style='width:10%;' class='text-right'>Amount</td>";
                                        Echo "</tr>";


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

                                            $ctr2++;
                                            Echo "<tr>";
                                            Echo "<td>$ctr2</td>";
                                            Echo "<td>$tran[descrip]</td>";
                                            Echo "<td class='text-right'>$tran[amount]</td>";
                                            Echo "</tr>";

                                            $totamount2 = $totamount2 + $tran['amount'];
                                        }
                                        Echo "<tr>";
                                        Echo "<td></td>";
                                        Echo "<td class='text-right'>Total</td>";
                                        Echo "<td class='text-right'>$totamount2</td>";
                                        Echo "</tr>";

                                        Echo "</table>";
                                        Echo "</td>";
                                        Echo "</tr>";
                                    }
                                }
                            }
                            echo "<tr class='mySearchTr'>";
                            echo "<td align='right' colspan='6'><h4><b>Grand Total</b></h4></td>";
                            echo "<td align='right' ><h4><b>$grsubtot</b></h4></td>";
                            echo "<td align='right' ><h4><b>$grtotdisc</b></h4></td>";
                            echo "<td align='right' ><h4><b>$grtaxable</b></h4></td>";
                            echo "<td align='right' ><h4><b>$grgst5</b></h4></td>";
                            echo "<td align='right' ><h4><b>$grgst12</b></h4></td>";
                            echo "<td align='right' ><h4><b>$grgst18</b></h4></td>";
                            echo "<td align='right' ><h4><b>$grgst28</b></h4></td>";
                            echo "<td align='right' ><h4><b>$grtotbillamt</b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}

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


    $contents .= "Sr,Voucher,Date,Party,GST No., Godown,Amount,Discount, Total Taxable,  ";
    $contents .= "Taxable 5%, GST 5%, Taxable 12%, GST 12%, Taxable 18%,  GST 18%, Taxable 28%, GST 28% ,Bill Amt.\n";
    $ctr = 0;
    $totamt = 0;
    $totdisc = 0;
    $totbill = 0;
    $gst0 = $gst5 = $gst12 = $gst18 = $gst18 = $gst28 = 0;
    while ($pmain = mysqli_fetch_array($getpmain)) {
        $getsupp = mysqli_query($conn, "SELECT `accode`,`name`,`gstno` FROM `accmast` WHERE `accode`='$pmain[party]'")or die(mysqli_error($conn));
        $supp = mysqli_fetch_array($getsupp);
        
        $getgodw = mysqli_query($conn, "SELECT `code`,`gcode` FROM `godown` WHERE `code`='$pmain[godown]'")or die(mysqli_error($conn));
        $godw = mysqli_fetch_array($getgodw);

        $vouchno = $pmain['vouchno'];
        $mydt = $pmain['mydt'];
        $supplier = $supp['name'];
        $suppgst = $supp['gstno'];
        
        $godown = $godw['gcode'];
        $gst0 = $gst5 = $gst12 = $gst18 = $gst18 = $gst28 = 0;

        if ($pmain["credtype"] == "O") {
            if ($pmain['cgstperc'] == "2.5") {
                $gst5 = $pmain['cgstamt'] + $pmain["sgstamt"];
            }
            if ($pmain['cgstperc'] == "6") {
                $gst12 = $pmain['cgstamt'] + $pmain["sgstamt"];
            }
            if ($pmain['cgstperc'] == "9") {
                $gst18 = $pmain['cgstamt'] + $pmain["sgstamt"];
            }
            if ($pmain['cgstperc'] == "14") {
                $gst28 = $pmain['cgstamt'] + $pmain["sgstamt"];
            }
        } else {
            $gst5 = $pmain['totgst5'];
            $gst12 = $pmain['totgst12'];
            $gst18 = $pmain['totgst18'];
            $gst28 = $pmain['totgst28'];
        }
        $billamt = $pmain['billamt'];
        $subtot = $pmain['grossamt'];
        $totdisc = $pmain['discamt'];
        $taxable = $pmain['taxableamt'];

        $taxable5  = $pmain['tottaxble5'];
        $taxable12 = $pmain['tottaxble12'];
        $taxable18 = $pmain['tottaxble18'];
        $taxable28 = $pmain['tottaxble28'];
        
        $credtype = $pmain['credtype'];

        $ctr++;

        $contents .= $ctr . ",";
        $contents .= $vouchno . ",";
        $contents .= $mydt . ",";
        $contents .= $supplier . ",";
        $contents .= $suppgst . ",";
        $contents .= $godown . ",";
        
        $contents .= $subtot . ",";
        $contents .= $totdisc . ",";
        $contents .= $taxable . ",";
        
        $contents .= $taxable5 . ",";
        $contents .= $gst5 . ",";
        
        $contents .= $taxable12 . ",";
        $contents .= $gst12 . ",";
        
        $contents .= $taxable18 . ",";
        $contents .= $gst18 . ",";
        
        $contents .= $taxable28 . ",";
        $contents .= $gst28 . ",";
        
        $contents .= $billamt . "\n";


        $grsubtot = $grsubtot + $subtot;
        $grtotdisc = $grtotdisc + $totdisc;
        $grtaxable = $grtaxable + $taxable;
        
        $grtaxable5  = $grtaxable5 + $taxable5 ;
        $grtaxable12 = $grtaxable12 + $taxable12 ;
        $grtaxable18 = $grtaxable18 + $taxable18 ;
        $grtaxable28 = $grtaxable28 + $taxable28 ;
        
        $grgst5  = $grgst5 + $gst5;
        $grgst12 = $grgst12 + $gst12;
        $grgst18 = $grgst18 + $gst18;
        $grgst28 = $grgst28 + $gst28;
        $grtotbillamt = $grtotbillamt + $billamt;


        $totbill = $totbill + $pmain['netamt'];
        $totamt = $totamt + $pmain['subtotal'];
        $totdisc = $totdisc + $pmain['discount'];

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

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

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

                    $discount = $tran[discamt];
                    $discperc = $tran[discperc];
                    $cgstamt = $tran[cgstamt];
                    $sgstamt = $tran[sgstamt];
                    $cgstrate = $tran[cgstrate];
                    $sgstrate = $tran[sgstrate];
                    $trantaxable = $tran[subtot];
                    $trantotal = $tran[total];

                    $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 .= $discperc . ",";
                    $contents .= $discount . ",";
                    $contents .= $trantaxable . ",";
                    if ($gsttype == 1) {
                        $contents .= $tran[igstrate] . ",";
                        $contents .= $tran[igstamt] . ",";
                    } elseif ($gsttype == 2) {
                        $contents .= $cgstrate . ",";
                        $contents .= $cgstamt . ",";
                        $contents .= $sgstrate . ",";
                        $contents .= $sgstamt . ",";
                    }
                    $contents .= $trantotal . "\n";

                    $totqty = $totqty + $tran[qty];
                    $totrate = $totrate + $tran[rate];
                    $totamount = $totamount + $tran[amount];
                    $totdisc2 = $totdisc2 + $discount;
                    $totsubtot = $totsubtot + $trantaxable;
                    $totcgstamt2 = $totcgstamt2 + $cgstamt;
                    $totsgstamt2 = $totsgstamt2 + $sgstamt;
                    $totigstamt2 = $totigstamt2 + $tran[igstamt];
                    $totamt = $totamt + $trantotal;
                    $ftot = $ftot + $tran[subtot];
                }

                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "" . ",";
                $contents .= "Total" . ",";
                $contents .= $totqty . ",";
                $contents .= "" . ",";
                $contents .= $totamount . ",";
                $contents .= "" . ",";
                $contents .= $totdisc2 . ",";
                $contents .= $totsubtot . ",";
                if ($gsttype == 1) {
                    $contents .= "" . ",";
                    $contents .= $totigstamt2 . ",";
                } elseif ($gsttype == 2) {
                    $contents .= "" . ",";
                    $contents .= $totcgstamt2 . ",";
                    $contents .= "" . ",";
                    $contents .= $totsgstamt2 . ",";
                }
                $contents .= $totamt . "\n";
            }
        } elseif ($credtype == "O") {
            if ($_POST["dtype"] == 2) {


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


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

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

                    $totamount = $totamount + $tran['amount'];
                }
                $contents .= "" . ",";
                $contents .= "Total" . ",";
                $contents .= $totamount . "\n";
            }
        }
    }
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Grand Total" . ",";
    $contents .= "" . ",";
    $contents .= $grsubtot . ",";
    $contents .= $grtotdisc . ",";
    $contents .= $grtaxable . ",";

    $contents .= $grtaxable5 . ",";
    $contents .= $grgst5 . ",";
    
    $contents .= $grtaxable12 . ",";
    $contents .= $grgst12 . ",";
    
    $contents .= $grtaxable12 . ",";
    $contents .= $grgst18 . ",";
    
    $contents .= $grtaxable28 . ",";
    $contents .= $grgst28 . ",";
    
    $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;
    }
}
?>