<?php

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

$maindb1 = "rawmain";
$trandb1 = "rawtran";

$maindb = "salemain";
$trandb = "saletran";

$mainret = "credmain";
$tranret = "credtran";

$maindb2 = "salemainexp";
$trandb2 = "saletranexp";

$maindb3 = "deexmain";
$trandb3 = "deextran";

$formname = "GSTR-3B";
$file = "gstr3b_pre.php";

$postperc = 0.18 ;

$compcd = $_SESSION["comp_" . $dbname];
$zroot = $_SESSION["zdir"];
$admin = $_SESSION["admin_" . $dbname];
$myyear = $_SESSION[$myyear];

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

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

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

    if (empty($_POST["fdate"])) {
        browse("From Date Cannot Be Empty");
    }

    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
    }

    $ctr = 0;

    $tnetigst = 0;
    $tpostagei = 0;
    $tdeemexpi = 0;
    $tigsttax = 0;
    $texports = 0;
    $texempted = 0;

    $tnetgst = 0;
    $tpostage = 0;
    $tdeemexp = 0;
    $tcgsttax = 0;
    $tsgsttax = 0;

    $ttotal = 0;

    $contents.="Sr., Description, GST Net Taxable, Postage 18%, Deemed Exp, IGST Tax, Exports, Exempted, GST Net, Postage 18%, Deemed Exp., CGST Tax, SGST Tax, Total\n";

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

    ///////////  reg - 0 

    $getr = mysqli_query($conn, "SELECT '0%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt 
                              FROM `$maindb` a, `$trandb` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 0 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr = mysqli_fetch_array($getr);
    

    $getrexp = mysqli_query($conn, "SELECT '0%' as descrip, sum(b.amtcurr) as tamt
                              FROM `$maindb2` a, `$trandb2` b WHERE a.`invdate` >= '$mfrmdate'  AND a.`invdate` <= '$mtodate'  
                              and a.vouchno = b.vouchno and a.canc_flag = '' and 
                              a.partyname in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetrexp = mysqli_fetch_array($getrexp);
    
    $getrpos = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms = 1 and vouchno in (select vouchno from saletran where gstperc = 0) 
                                        and party in (select accode from accmast where gstno != '')  ") or die(mysqli_error($conn));
    $fetrpos = mysqli_fetch_array($getrpos);
    

    $getrposc = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms != 1 and vouchno in (select vouchno from saletran where gstperc = 0) 
                                        and party in (select accode from accmast where gstno != '')  ") or die(mysqli_error($conn));
    $fetrposc = mysqli_fetch_array($getrposc);
    
    $ctr++;
    $descrip = $fetr['descrip'];

    $netigst = "";
    $postagei = $fetrpos['post'];;
    $deemexpi = "";
    $igsttax = round(($fetrposc['post'] * $postperc),2);
    $exports = $fetrexp['tamt'];
    $exempted = $fetr['tamt'];

    $netgst  = "";
    $postage = $fetrposc['post'];
    $deemexp = "";
    $cgsttax = round(($fetrposc['post'] * $postperc),2) / 2 ;
    $sgsttax = round(($fetrposc['post'] * $postperc),2) / 2 ;

    $total = $exports + $exempted + $postagei + $igsttax + $postage + $cgsttax + $sgsttax ;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $texports  = $texports + $exports ;
    $texempted = $texempted + $exempted;
    $tpostagei = $tpostagei + $postagei;
    $tigsttax  = $tigsttax + $igsttax;
    $tpostage  = $tpostage + $postage;
    $tcgsttax  = $tcgsttax + $cgsttax;
    $tsgsttax  = $tsgsttax + $sgsttax;
    $ttotal = $ttotal + $total;

    ///////////  reg - deemed - 0 

    $getr1 = mysqli_query($conn, "SELECT 'Deemed Export 0%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt  
                              FROM `deexmain` a, `deextran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 0 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr1 = mysqli_fetch_array($getr1);

    
    $getrpos1 = mysqli_query($conn, "SELECT sum(postamt) as post from `deexmain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms = 1 and vouchno in (select vouchno from deextran where gstperc = 0) 
                                        and party in (select accode from accmast where gstno != '')  ") or die(mysqli_error($conn));
    $fetrpos1 = mysqli_fetch_array($getrpos1);
    

    $getrposc1 = mysqli_query($conn, "SELECT sum(postamt) as post from `deexmain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms != 1 and vouchno in (select vouchno from deextran where gstperc = 0) 
                                        and party in (select accode from accmast where gstno != '')  ") or die(mysqli_error($conn));
    $fetrposc1 = mysqli_fetch_array($getrposc1);

    
    $ctr++;
    $descrip = $fetr1['descrip'];

    $netigst = "";
    $postagei = $fetrpos1['post'];
    $deemexpi = "";
    $igsttax = round(($fetrpos1['post'] * $postperc),2);
    $exports = "";
    $exempted = $fetr1['tamt'];

    $netgst = "";
    $postage = $fetrposc1['post'];
    $deemexp = "";
    $cgsttax = round(($fetrposc1['post'] * $postperc),2) / 2 ;
    $sgsttax = round(($fetrposc1['post'] * $postperc),2) / 2 ;

    $total = $exempted + $postagei + $igsttax + $postage + $cgsttax + $sgsttax ;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $texempted = $texempted + $exempted;
    $tpostagei = $tpostagei + $postagei;
    $tigsttax  = $tigsttax + $igsttax;
    $tpostage  = $tpostage + $postage;
    $tcgsttax  = $tcgsttax + $cgsttax;
    $tsgsttax  = $tsgsttax + $sgsttax;
    $ttotal = $ttotal + $total;


    ///////////  reg - deemed - 0.1  

    $getr3 = mysqli_query($conn, "SELECT '0.1%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.igstamt) as igsttax  
                              FROM `deexmain` a, `deextran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 0.1 and a.oms = 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr3 = mysqli_fetch_array($getr3);

    $getr3c = mysqli_query($conn, "SELECT '0.1%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.cgstamt) as cgsttax  
                              FROM `deexmain` a, `deextran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 0.1 and a.oms != 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr3c = mysqli_fetch_array($getr3c);

    
    $getrpos3 = mysqli_query($conn, "SELECT sum(postamt) as post from `deexmain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms = 1 and vouchno in (select vouchno from deextran where gstperc = 0.1) 
                                        and party in (select accode from accmast where gstno != '')  ") or die(mysqli_error($conn));
    $fetrpos3 = mysqli_fetch_array($getrpos3);
    

    $getrposc3 = mysqli_query($conn, "SELECT sum(postamt) as post from `deexmain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms != 1 and vouchno in (select vouchno from deextran where gstperc = 0.1) 
                                        and party in (select accode from accmast where gstno != '')  ") or die(mysqli_error($conn));
    $fetrposc3 = mysqli_fetch_array($getrposc3);

    
    $ctr++;
    $descrip = $fetr3['descrip'];

    $netigst = "";
    $postagei = $fetrpos3['post'];
    $deemexpi = $fetr3['tamt'];
    $igsttax  = $fetr3['igsttax'] + (round(($fetrpos3['post'] * $postperc),2) / 2);
    $exports = "";
    $exempted = "";

    $netgst = "";
    $postage = $fetrposc3['post'];
    $deemexp = $fetr3c['tamt'];
    $cgsttax = $fetr3c['cgsttax'] + (round(($fetrposc3['post'] * $postperc),2) / 2);
    $sgsttax = $fetr3c['cgsttax'] + (round(($fetrposc3['post'] * $postperc),2) / 2);

    $total = $deemexpi + $postagei + $igsttax  + $postage + $deemexp + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tdeemexpi = tdeemexpi + $deemexpi;
    $tigsttax = $tigsttax + $igsttax;

    $tpostagei = $tpostagei + $postagei;
    $tpostage  = $tpostage + $postage;
    
    $tdeemexp = tdeemexp + $deemexp;
    $tcgsttax = $tcgsttax + $cgsttax;
    $tsgsttax = $tsgsttax + $sgsttax;

    $ttotal = $ttotal + $total;

    ///////////  reg - 5  

    $getr5 = mysqli_query($conn, "SELECT '5%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.igstamt) as igsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 5 and a.oms = 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr5 = mysqli_fetch_array($getr5);

    $getr5c = mysqli_query($conn, "SELECT '5%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.cgstamt) as cgsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 5 and a.oms != 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr5c = mysqli_fetch_array($getr5c);

    
    $getrpos5 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms = 1 and vouchno in (select vouchno from saletran where gstperc = 5) 
                                        and party in (select accode from accmast where gstno != '')  ") or die(mysqli_error($conn));
    $fetrpos5 = mysqli_fetch_array($getrpos5);
    

    $getrposc5 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms != 1 and vouchno in (select vouchno from saletran where gstperc = 5) 
                                        and party in (select accode from accmast where gstno != '')  ") or die(mysqli_error($conn));
    $fetrposc5 = mysqli_fetch_array($getrposc5);
    
    
    $ctr++;
    $descrip = $fetr5['descrip'];

    $netigst = $fetr5['tamt'];
    $postagei = $fetrpos5['post'];
    $deemexpi = "";
    $igsttax = $fetr5['igsttax'] + (round(($fetrpos5['post'] * $postperc),2) / 2);
    $exports = "";
    $exempted = "";

    $netgst = $fetr5c['tamt'] + (round(($fetrposc5['post'] * $postperc),2) / 2);;
    $postage = $fetrposc5['post'];
    $deemexp = "";
    $cgsttax = $fetr5c['cgsttax'] + (round(($fetrposc5['post'] * $postperc),2) / 2);
    $sgsttax = $fetr5c['cgsttax'] + (round(($fetrposc5['post'] * $postperc),2) / 2);

    $total = $netigst  + $postagei + $igsttax + $netgst  + $postage + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tnetigst = $tnetigst + $netigst;
    $tigsttax = $tigsttax + $igsttax;

    $tpostagei = $tpostagei + $postagei;
    $tpostage  = $tpostage + $postage;
    
    $tnetgst = $tnetgst + $netgst;
    $tcgsttax = $tcgsttax + $cgsttax;
    $tsgsttax = $tsgsttax + $sgsttax;

    $ttotal = $ttotal + $total;

  ///////////  reg - 12

    $getr12 = mysqli_query($conn, "SELECT '12%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.igstamt) as igsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 12 and a.oms = 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr12 = mysqli_fetch_array($getr12);

    $getr12c = mysqli_query($conn, "SELECT '12%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.cgstamt) as cgsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 12 and a.oms != 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr12c = mysqli_fetch_array($getr12c);

    
    $getrpos12 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms = 1 and vouchno in (select vouchno from saletran where gstperc = 12) 
                                        and party in (select accode from accmast where gstno != '')  ") or die(mysqli_error($conn));
    $fetrpos12 = mysqli_fetch_array($getrpos12);
    

    $getrposc12 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms != 1 and vouchno in (select vouchno from saletran where gstperc = 12) 
                                        and party in (select accode from accmast where gstno != '')  ") or die(mysqli_error($conn));
    $fetrposc12 = mysqli_fetch_array($getrposc12);
    
    $ctr++;
    $descrip = $fetr12['descrip'];

    $netigst = $fetr12['tamt'] ;
    $postagei = $fetrpos12['post'];
    $deemexpi = "";
    $igsttax = $fetr12['igsttax'] + (round(($fetrpos12['post'] * $postperc),2) / 2);
    $exports = "";
    $exempted = "";

    $netgst = $fetr12c['tamt'] ;
    $postage = $fetrposc12['post'];
    $deemexp = "";
    $cgsttax = $fetr12c['cgsttax'] + (round(($fetrposc12['post'] * $postperc),2) / 2);;
    $sgsttax = $fetr12c['cgsttax'] + (round(($fetrposc12['post'] * $postperc),2) / 2);;

    $total = $netigst  + $postagei + $igsttax + $netgst  + $postage + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tnetigst = $tnetigst + $netigst;
    $tigsttax = $tigsttax + $igsttax;

    $tpostagei = $tpostagei + $postagei;
    $tpostage  = $tpostage + $postage;
    
    $tnetgst = $tnetgst + $netgst;
    $tcgsttax = $tcgsttax + $cgsttax;
    $tsgsttax = $tsgsttax + $sgsttax;

    $ttotal = $ttotal + $total;

///////////  reg - 18

    $getr18 = mysqli_query($conn, "SELECT '18%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.igstamt) as igsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 18 and a.oms = 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr18 = mysqli_fetch_array($getr18);

    $getr18c = mysqli_query($conn, "SELECT '18%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.cgstamt) as cgsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 18 and a.oms != 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr18c = mysqli_fetch_array($getr18c);

    $getrpos18 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms = 1 and vouchno in (select vouchno from saletran where gstperc = 18) 
                                        and party in (select accode from accmast where gstno != '')  ") or die(mysqli_error($conn));
    $fetrpos18 = mysqli_fetch_array($getrpos18);
    

    $getrposc18 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms != 1 and vouchno in (select vouchno from saletran where gstperc = 18) 
                                        and party in (select accode from accmast where gstno != '')  ") or die(mysqli_error($conn));
    $fetrposc18 = mysqli_fetch_array($getrposc18);
    
    $ctr++;
    $descrip = $fetr18['descrip'];

    $netigst = $fetr18['tamt'];
    $postagei = $fetrpos18['post'];
    $deemexpi = "";
    $igsttax = $fetr18['igsttax'] + (round(($fetrpos18['post'] * $postperc),2) / 2);
    $exports = "";
    $exempted = "";

    $netgst = $fetr18c['tamt'];
    $postage = $fetrposc18['post'];
    $deemexp = "";
    $cgsttax = $fetr18c['cgsttax'] + (round(($fetrposc18['post'] * $postperc),2) / 2);
    $sgsttax = $fetr18c['cgsttax'] + (round(($fetrposc18['post'] * $postperc),2) / 2);

    $total = $netigst  + $postagei + $igsttax + $netgst  + $postage + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tnetigst = $tnetigst + $netigst;
    $tigsttax = $tigsttax + $igsttax;

    $tpostagei = $tpostagei + $postagei;
    $tpostage  = $tpostage + $postage;

    $tnetgst = $tnetgst + $netgst;
    $tcgsttax = $tcgsttax + $cgsttax;
    $tsgsttax = $tsgsttax + $sgsttax;

    $ttotal = $ttotal + $total;

///////////  reg - 28  

    $getr28 = mysqli_query($conn, "SELECT '28%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.igstamt) as igsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 28 and a.oms = 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr28 = mysqli_fetch_array($getr28);

    $getr28c = mysqli_query($conn, "SELECT '28%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.cgstamt) as cgsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 28 and a.oms != 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr28c = mysqli_fetch_array($getr28c);

    $getrpos28 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms = 1 and vouchno in (select vouchno from saletran where gstperc = 28) 
                                        and party in (select accode from accmast where gstno != '')  ") or die(mysqli_error($conn));
    $fetrpos28 = mysqli_fetch_array($getrpos28);
    

    $getrposc28 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms != 1 and vouchno in (select vouchno from saletran where gstperc = 28) 
                                        and party in (select accode from accmast where gstno != '')  ") or die(mysqli_error($conn));
    $fetrposc28 = mysqli_fetch_array($getrposc28);
    
    $ctr++;
    $descrip = $fetr28['descrip'];

    $netigst = $fetr28['tamt'];
    $postagei = $fetrpos28['post'];
    $deemexpi = "";
    $igsttax = $fetr28['igsttax'] + (round(($fetrpos28['post'] * $postperc),2) / 2);
    $exports = "";
    $exempted = "";

    $netgst = $fetr28c['tamt'];
    $postage = $fetrposc28['post'];
    $deemexp = "";
    $cgsttax = $fetr28c['cgsttax'] + (round(($fetrposc28['post'] * $postperc),2) / 2);
    $sgsttax = $fetr28c['cgsttax'] + (round(($fetrposc28['post'] * $postperc),2) / 2);

    $total = $netigst + $postagei + $igsttax + $netgst  + $postage + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tnetigst = $tnetigst + $netigst;
    $tigsttax = $tigsttax + $igsttax;

    $tpostagei = $tpostagei + $postagei;
    $tpostage  = $tpostage + $postage;

    $tnetgst = $tnetgst + $netgst;
    $tcgsttax = $tcgsttax + $cgsttax;
    $tsgsttax = $tsgsttax + $sgsttax;

    $ttotal = $ttotal + $total;

    $contents.= ",";
    $contents.= "Total" . ",";

    $contents.= $tnetigst . ",";
    $contents.= $tpostagei . ",";
    $contents.= $tdeemexpi . ",";
    $contents.= $tigsttax . ",";
    $contents.= $texports . ",";
    $contents.= $texempted . ",";

    $contents.= $tnetgst . ",";
    $contents.= $tpostage . ",";
    $contents.= $tdeemexp . ",";
    $contents.= $tcgsttax . ",";
    $contents.= $tsgsttax . ",";

    $contents.= $ttotal . "\n";

    $contents.= "\n";

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

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


    ///////////  unreg - 0 

    $getu = mysqli_query($conn, "SELECT '0%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt 
                              FROM `$maindb` a, `$trandb` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 0 and a.canc_flag = '' and a.oms = 1 and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu = mysqli_fetch_array($getu);

    $getuexp = mysqli_query($conn, "SELECT '0%' as descrip, sum(b.amtcurr) as tamt
                              FROM `$maindb2` a, `$trandb2` b WHERE a.`invdate` >= '$mfrmdate'  AND a.`invdate` <= '$mtodate'  
                              and a.vouchno = b.vouchno and a.canc_flag = '' and 
                              a.partyname in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetuexp = mysqli_fetch_array($getuexp);

    
    $getupos = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms = 1 and vouchno in (select vouchno from saletran where gstperc = 0) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetupos = mysqli_fetch_array($getupos);
    

    $getuposc = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms != 1 and vouchno in (select vouchno from saletran where gstperc = 0) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetuposc = mysqli_fetch_array($getuposc);
    
    $ctr++;
    $descrip = $fetu['descrip'];
    
   
    $netigst = "";
    $postagei = $fetupos['post'];;
    $deemexpi = "";
    $igsttax = round(($fetuposc['post'] * $postperc),2);
    $exports = $fetuexp['tamt'];
    $exempted = $fetu['tamt'];

    $netgst  = "";
    $postage = $fetuposc['post'];
    $deemexp = "";
    $cgsttax = round(($fetuposc['post'] * $postperc),2) / 2 ;
    $sgsttax = round(($fetuposc['post'] * $postperc),2) / 2 ;

    $total = $exports + $exempted + $postagei + $igsttax + $postage + $cgsttax + $sgsttax ;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $texempted = $texempted + $exempted;
    $texports = $texports + $exports;
    $tigsttax = $tigsttax + $igsttax;

    $tpostagei = $tpostagei + $postagei;
    $tpostage  = $tpostage + $postage;

    $tnetgst = $tnetgst + $netgst;
    $tcgsttax = $tcgsttax + $cgsttax;
    $tsgsttax = $tsgsttax + $sgsttax;

    $ttotal = $ttotal + $total;

   ///////////  unreg - 5  

    $getu5 = mysqli_query($conn, "SELECT '5%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.igstamt) as igsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 5 and a.oms = 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu5 = mysqli_fetch_array($getu5);

    $getu5o = mysqli_query($conn, "SELECT '5%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.igstamt) as igsttax  
                              FROM `slonlmain` a, `slonltran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 5 and a.oms = 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu5o = mysqli_fetch_array($getu5o);

    
    $getu5c = mysqli_query($conn, "SELECT '5%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.cgstamt) as cgsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 5 and a.oms != 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu5c = mysqli_fetch_array($getu5c);

    $getu5co = mysqli_query($conn, "SELECT '5%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.cgstamt) as cgsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 5 and a.oms != 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu5co = mysqli_fetch_array($getu5co);
    
    /////////// postage 
    
    $getupos5 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms = 1 and vouchno in (select vouchno from saletran where gstperc = 5) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetupos5 = mysqli_fetch_array($getupos5);
    

    $getuposc5 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms != 1 and vouchno in (select vouchno from saletran where gstperc = 5) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetuposc5 = mysqli_fetch_array($getuposc5);

    
    $getuposo5 = mysqli_query($conn, "SELECT sum(postamt) as post from `slonlmain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms = 1 and vouchno in (select vouchno from slonltran where gstperc = 5) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetuposo5 = mysqli_fetch_array($getuposo5);
    

    $getuposco5 = mysqli_query($conn, "SELECT sum(postamt) as post from `slonlmain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms != 1 and vouchno in (select vouchno from slonltran where gstperc = 5) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetuposco5 = mysqli_fetch_array($getuposco5);
    
    
    $ctr++;
    $descrip = $fetu5['descrip'];

    $netigst = $fetu5['tamt'] + $fetu5o['tamt'] ;
    $postagei = $fetupos5['post'] + $fetuposo5['post'];
    $deemexpi = "";
    $igsttax = $fetu5['igsttax'] + $fetu5o['igsttax'] + round(($fetupos5['post'] * $postperc),2) + round(($fetuposo5['post'] * $postperc),2); 
    $exports = "";
    $exempted = "";

    $netgst = $fetu5c['tamt'] + $fetu5co['tamt'];
    $postage = $fetuposc5['post'] + $fetuposco5['post'];
    $deemexp = "";
    $cgsttax = $fetu5c['cgsttax'] + $fetu5co['cgsttax'] + round(($fetuposc5['post'] * $postperc),2) + round(($fetuposco5['post'] * $postperc),2); 
    $sgsttax = $fetu5c['cgsttax'] + $fetu5co['cgsttax'] + round(($fetuposc5['post'] * $postperc),2) + round(($fetuposco5['post'] * $postperc),2); 

    $total = $netigst  + $postagei + $igsttax + $netgst  + $postage + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tnetigst = $tnetigst + $netigst;
    $tigsttax = $tigsttax + $igsttax;

    $tpostagei = $tpostagei + $postagei;
    $tpostage  = $tpostage + $postage;
    
    $tnetgst = $tnetgst + $netgst;
    $tcgsttax = $tcgsttax + $cgsttax;
    $tsgsttax = $tsgsttax + $sgsttax;

    $ttotal = $ttotal + $total;

///////////  unreg - 12

    $getu12 = mysqli_query($conn, "SELECT '12%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.igstamt) as igsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 12 and a.oms = 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu12 = mysqli_fetch_array($getu12);

    $getu12o = mysqli_query($conn, "SELECT '12%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.igstamt) as igsttax  
                              FROM `slonlmain` a, `slonltran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 12 and a.oms = 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu12o = mysqli_fetch_array($getu12o);


    $getu12c = mysqli_query($conn, "SELECT '12%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.cgstamt) as cgsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 12 and a.oms != 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu12c = mysqli_fetch_array($getu12c);

    $getu12co = mysqli_query($conn, "SELECT '12%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.cgstamt) as cgsttax  
                              FROM `slonlmain` a, `slonltran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 12 and a.oms != 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu12co = mysqli_fetch_array($getu12co);


    /////////// postage 
    
    $getupos12 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms = 1 and vouchno in (select vouchno from saletran where gstperc = 12) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetupos12 = mysqli_fetch_array($getupos12);
    

    $getuposc12 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms != 1 and vouchno in (select vouchno from saletran where gstperc = 12) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetuposc12 = mysqli_fetch_array($getuposc12);

    
    $getuposo12 = mysqli_query($conn, "SELECT sum(postamt) as post from `slonlmain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms = 1 and vouchno in (select vouchno from slonltran where gstperc = 12) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetuposo12 = mysqli_fetch_array($getuposo12);
    

    $getuposco12 = mysqli_query($conn, "SELECT sum(postamt) as post from `slonlmain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms != 1 and vouchno in (select vouchno from slonltran where gstperc = 12) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetuposco12 = mysqli_fetch_array($getuposco12);
    
    
    $ctr++;
    $descrip = $fetu12['descrip'];

    $netigst  = $fetu12['tamt'] + $fetu12o['tamt'];
    $postagei = $fetupos12['post'] + $fetuposo12['post'];
    $deemexpi = "";
    $igsttax  = $fetu12['igsttax'] + $fetu12o['igsttax'] + round(($fetupos12['post'] * $postperc),2) + round(($fetuposo12['post'] * $postperc),2); 
    $exports  = "";
    $exempted = "";
    
    $netgst  = $fetu12c['tamt'] + $fetu12co['tamt'];
    $postage = $fetuposc12['post'] + $fetuposco12['post'];;
    $deemexp = "";
    $cgsttax = $fetu12c['cgsttax'] + $fetu12co['cgsttax'] + round(($fetuposc12['post'] * $postperc),2) + round(($fetuposco12['post'] * $postperc),2); 
    $sgsttax = $fetu12c['cgsttax'] + $fetu12co['cgsttax'] + round(($fetuposc12['post'] * $postperc),2) + round(($fetuposco12['post'] * $postperc),2); 

    $total = $netigst  + $postagei + $igsttax + $netgst  + $postage + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tnetigst = $tnetigst + $netigst;
    $tigsttax = $tigsttax + $igsttax;

    $tpostagei = $tpostagei + $postagei;
    $tpostage  = $tpostage + $postage;

    $tnetgst = $tnetgst + $netgst;
    $tcgsttax = $tcgsttax + $cgsttax;
    $tsgsttax = $tsgsttax + $sgsttax;

    $ttotal = $ttotal + $total;

///////////  unreg - 18

    $getu18 = mysqli_query($conn, "SELECT '18%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.igstamt) as igsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 18 and a.oms = 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu18 = mysqli_fetch_array($getu18);

    $getu18o = mysqli_query($conn, "SELECT '18%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.igstamt) as igsttax  
                              FROM `slonlmain` a, `slonltran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 18 and a.oms = 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu18o = mysqli_fetch_array($getu18o);


    $getu18c = mysqli_query($conn, "SELECT '18%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.cgstamt) as cgsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 18 and a.oms != 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu18c = mysqli_fetch_array($getu18c);

    $getu18co = mysqli_query($conn, "SELECT '18%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.cgstamt) as cgsttax  
                              FROM `slonlmain` a, `slonltran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 18 and a.oms != 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu18co = mysqli_fetch_array($getu18co);

    /////////// postage 
    
    $getupos18 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms = 1 and vouchno in (select vouchno from saletran where gstperc = 18) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetupos18 = mysqli_fetch_array($getupos18);
    

    $getuposc18 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms != 1 and vouchno in (select vouchno from saletran where gstperc = 18) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetuposc18 = mysqli_fetch_array($getuposc18);

    
    $getuposo18 = mysqli_query($conn, "SELECT sum(postamt) as post from `slonlmain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms = 1 and vouchno in (select vouchno from slonltran where gstperc = 18) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetuposo18 = mysqli_fetch_array($getuposo18);
    

    $getuposco18 = mysqli_query($conn, "SELECT sum(postamt) as post from `slonlmain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms != 1 and vouchno in (select vouchno from slonltran where gstperc = 18) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetuposco18 = mysqli_fetch_array($getuposco18);
    
    
    
    $ctr++;
    $descrip = $fetu18['descrip'];

    $netigst = $fetu18['tamt'] + $fetu18o['tamt'];
    $postagei = $fetupos18['post'] + $fetuposo18['post'];
    $deemexpi = "";
    $igsttax = $fetu18['igsttax'] + $fetu18o['igsttax'] + round(($fetupos18['post'] * $postperc),2) + round(($fetuposo18['post'] * $postperc),2); 
    $exports = "";
    $exempted = "";
    
    $netgst = $fetu18c['tamt'] + $fetu18co['tamt'];
    $postage = $fetuposc18['post'] + $fetuposco18['post'];;
    $deemexp = "";
    $cgsttax = $fetu18c['cgsttax'] + $fetu18co['cgsttax'] + round(($fetuposc18['post'] * $postperc),2) + round(($fetuposco18['post'] * $postperc),2); 
    $sgsttax = $fetu18c['cgsttax'] + $fetu18co['cgsttax'] + round(($fetuposc18['post'] * $postperc),2) + round(($fetuposco18['post'] * $postperc),2); 
//
//    print $fetu18c['tamt'] ;
//    echo "<br>";
//    print $fetu18co['tamt'];
//    echo "<br>";
//    
//    print $fetu18c['cgsttax'] ;
//    echo "<br>";
//    print $fetu18co['cgsttax']    ;
//    echo "<br>";
//
//    print $fetuposc18['post'] ;
//    echo "<br>";
//    print $fetuposco18['post'];        
//    echo "<br>";
//    print $postage ;
//    echo "<br>";
//    
//    print $postperc ;
//    echo "<br>";
//    print round(($fetuposc18['post'] * $postperc),2) ;
//    echo "<br>";
//    print round(($fetuposco18['post'] * $postperc),2) ;
//    
//    die();
//    

    
    $total = $netigst + $postagei + $igsttax + $netgst + $postage + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tnetigst = $tnetigst + $netigst;
    $tigsttax = $tigsttax + $igsttax;

    $tpostagei = $tpostagei + $postagei;
    $tpostage  = $tpostage + $postage;

    $tnetgst = $tnetgst + $netgst;
    $tcgsttax = $tcgsttax + $cgsttax;
    $tsgsttax = $tsgsttax + $sgsttax;

    $ttotal = $ttotal + $total;


///////////  unreg - 28  

    $getu28 = mysqli_query($conn, "SELECT '28%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.igstamt) as igsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 28 and a.oms = 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu28 = mysqli_fetch_array($getu28);

    $getu28o = mysqli_query($conn, "SELECT '28%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.igstamt) as igsttax  
                              FROM `slonlmain` a, `slonltran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 28 and a.oms = 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu28o = mysqli_fetch_array($getu28o);
    
    $getu28c = mysqli_query($conn, "SELECT '28%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.cgstamt) as cgsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 28 and a.oms != 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu28c = mysqli_fetch_array($getu28c);

    $getu28co = mysqli_query($conn, "SELECT '28%' as descrip, b.gstperc, sum(b.subtot) as tamt, a.postamt, sum(b.cgstamt) as cgsttax  
                              FROM `slonlmain` a, `slonltran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 28 and a.oms != 1 and a.canc_flag = '' and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu28co = mysqli_fetch_array($getu28co);

    
/////////// postage 
    
    $getupos28 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms = 1 and vouchno in (select vouchno from saletran where gstperc = 28) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetupos28 = mysqli_fetch_array($getupos28);
    

    $getuposc28 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms != 1 and vouchno in (select vouchno from saletran where gstperc = 28) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetuposc28 = mysqli_fetch_array($getuposc28);

    
    $getuposo28 = mysqli_query($conn, "SELECT sum(postamt) as post from `slonlmain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms = 1 and vouchno in (select vouchno from slonltran where gstperc = 28) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetuposo28 = mysqli_fetch_array($getuposo28);
    

    $getuposco28 = mysqli_query($conn, "SELECT sum(postamt) as post from `slonlmain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                        and canc_flag = '' and oms != 1 and vouchno in (select vouchno from slonltran where gstperc = 28) 
                                        and party in (select accode from accmast where gstno = '')  ") or die(mysqli_error($conn));
    $fetuposco28 = mysqli_fetch_array($getuposco28);
    
    
    $ctr++;
    $descrip = $fetu28['descrip'];

    $netigst = $fetu28['tamt'] + $fetu28o['tamt'];
    $postagei = $fetupos28['post'] + $fetuposo28['post'];
    $deemexpi = "";
    $igsttax = $fetu28['igsttax'] + $fetu28o['igsttax'] + round(($fetupos28['post'] * $postperc),2) + round(($fetuposo28['post'] * $postperc),2); 
    $exports = "";
    $exempted = "";

    $netgst = $fetu28c['tamt'] + $fetu28co['tamt'];
    $postage = $fetuposc28['post'] + $fetuposco28['post'];;
    $deemexp = "";
    $cgsttax = $fetu28c['cgsttax'] + $fetu28co['cgsttax'] + round(($fetuposc28['post'] * $postperc),2) + round(($fetuposco28['post'] * $postperc),2); 
    $sgsttax = $fetu28c['cgsttax'] + $fetu28co['cgsttax'] + round(($fetuposc28['post'] * $postperc),2) + round(($fetuposco28['post'] * $postperc),2); 

    $total = $netigst  + $postagei + $igsttax + $netgst  + $postage + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tnetigst = $tnetigst + $netigst;
    $tigsttax = $tigsttax + $igsttax;

    $tpostagei = $tpostagei + $postagei;
    $tpostage  = $tpostage + $postage;

    $tnetgst = $tnetgst + $netgst;
    $tcgsttax = $tcgsttax + $cgsttax;
    $tsgsttax = $tsgsttax + $sgsttax;

    $ttotal = $ttotal + $total;

    $contents.= ",";
    $contents.= "Total" . ",";

    $contents.= $tnetigst . ",";
    $contents.= $tpostagei . ",";
    $contents.= $tdeemexpi . ",";
    $contents.= $tigsttax . ",";
    $contents.= $texports . ",";
    $contents.= $texempted . ",";

    $contents.= $tnetgst . ",";
    $contents.= $tpostage . ",";
    $contents.= $tdeemexp . ",";
    $contents.= $tcgsttax . ",";
    $contents.= $tsgsttax . ",";

    $contents.= $ttotal . "\n";

    $contents.= "\n";

///////////////////////////////////////////////////////////////////////////////////
/////////////// sale return
///////////////////////////////////////////////////////////////////////////////////


    $contents.= ", SALE RETURN,  " . "\n";

    ///////////  SALE RETURN    reg - 0 

    $getr = mysqli_query($conn, "SELECT '0%' as descrip, b.gstperc, sum(b.subtot) as tamt
                              FROM `$mainret` a, `$tranret` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 0  and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr = mysqli_fetch_array($getr);

    if (mysqli_num_rows($getr) > 0) {
        $ctr++;
        $descrip = $fetr['descrip'];

        $netigst = "";
        $postagei = "";
        $deemexpi = "";
        $igsttax = "";
        $exports = "";
        $exempted = $fetr['tamt'];

        $netgst = "";
        $postage = "";
        $deemexp = "";
        $cgsttax = "";
        $sgsttax = "";

        $total = $exempted;

        $contents.= $ctr . ",";
        $contents.= $descrip . ",";

        $contents.= $netigst . ",";
        $contents.= $postagei . ",";
        $contents.= $deemexpi . ",";
        $contents.= $igsttax . ",";
        $contents.= $exports . ",";
        $contents.= $exempted . ",";

        $contents.= $netgst . ",";
        $contents.= $postage . ",";
        $contents.= $deemexp . ",";
        $contents.= $cgsttax . ",";
        $contents.= $sgsttax . ",";

        $contents.= $total . "\n";

        $texempted = $texempted - $exempted;
        $ttotal = $ttotal - $total;
    }
    
    ///////////  SALE RETURN    reg - deemed - 0     not required

    ///////////  SALE RETURN    reg - deemed - 0.1   not required
    

    ///////////  SALE RETURN    reg - 5  

    $getr5 = mysqli_query($conn, "SELECT '5%' as descrip, b.gstperc, sum(b.subtot) as tamt, sum(b.igstamt) as igsttax  
                              FROM `$mainret` a, `$tranret` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 5 and a.oms = 1  and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr5 = mysqli_fetch_array($getr5);

    $getr5c = mysqli_query($conn, "SELECT '5%' as descrip, b.gstperc, sum(b.subtot) as tamt, sum(b.cgstamt) as cgsttax  
                              FROM `$mainret` a, `$tranret` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 5 and a.oms != 1  and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr5c = mysqli_fetch_array($getr5c);

    $ctr++;
    $descrip = $fetr5['descrip'];

    $netigst = $fetr5['tamt'];
    $postagei = "";
    $deemexpi = "";
    $igsttax = $fetr5['igsttax'];
    $exports = "";
    $exempted = "";

    $netgst = $fetr5c['tamt'];
    $postage = "";
    $deemexp = "";
    $cgsttax = $fetr5c['cgsttax'];
    $sgsttax = $fetr5c['cgsttax'];

    $total = $netigst + $igsttax + $netgst + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tnetigst = $tnetigst - $netigst;
    $tigsttax = $tigsttax - $igsttax;

    $tnetgst = $tnetgst - $netgst;
    $tcgsttax = $tcgsttax - $cgsttax;
    $tsgsttax = $tsgsttax - $sgsttax;

    $ttotal = $ttotal - $total;

///////////  SALE RETURN    reg - 12

    $getr12 = mysqli_query($conn, "SELECT '12%' as descrip, b.gstperc, sum(b.subtot) as tamt, sum(b.igstamt) as igsttax  
                              FROM `$mainret` a, `$tranret` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 12 and a.oms = 1  and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr12 = mysqli_fetch_array($getr12);

    $getr12c = mysqli_query($conn, "SELECT '12%' as descrip, b.gstperc, sum(b.subtot) as tamt, sum(b.cgstamt) as cgsttax  
                              FROM `$mainret` a, `$tranret` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 12 and a.oms != 1  and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr12c = mysqli_fetch_array($getr12c);

    $ctr++;
    $descrip = $fetr12['descrip'];

    $netigst = $fetr12['tamt'];
    $postagei = "";
    $deemexpi = "";
    $igsttax = $fetr12['igsttax'];
    $exports = "";
    $exempted = "";

    $netgst = $fetr12c['tamt'];
    $postage = "";
    $deemexp = "";
    $cgsttax = $fetr12c['cgsttax'];
    $sgsttax = $fetr12c['cgsttax'];

    $total = $netigst + $igsttax + $netgst + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tnetigst = $tnetigst - $netigst;
    $tigsttax = $tigsttax - $igsttax;

    $tnetgst = $tnetgst - $netgst;
    $tcgsttax = $tcgsttax - $cgsttax;
    $tsgsttax = $tsgsttax - $sgsttax;

    $ttotal = $ttotal - $total;

///////////  SALE RETURN    reg - 18

    $getr18 = mysqli_query($conn, "SELECT '18%' as descrip, b.gstperc, sum(b.subtot) as tamt,  sum(b.igstamt) as igsttax  
                              FROM `$mainret` a, `$tranret` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 18 and a.oms = 1  and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr18 = mysqli_fetch_array($getr18);

    $getr18c = mysqli_query($conn, "SELECT '18%' as descrip, b.gstperc, sum(b.subtot) as tamt,  sum(b.cgstamt) as cgsttax  
                              FROM `$mainret` a, `$tranret` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 18 and a.oms != 1  and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr18c = mysqli_fetch_array($getr18c);

    $ctr++;
    $descrip = $fetr18['descrip'];

    $netigst = $fetr18['tamt'] ;
    $postagei = "";
    $deemexpi = "";
    $igsttax = $fetr18['igsttax'] ;
    $exports = "";
    $exempted = "";

    $netgst = $fetr18c['tamt'];
    $postage = "";
    $deemexp = "";
    $cgsttax = $fetr18c['cgsttax'];
    $sgsttax = $fetr18c['cgsttax'];

    $total = $netigst + $igsttax + $netgst + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tnetigst = $tnetigst + $netigst;
    $tigsttax = $tigsttax + $igsttax;

    $tnetgst = $tnetgst + $netgst;
    $tcgsttax = $tcgsttax + $cgsttax;
    $tsgsttax = $tsgsttax + $sgsttax;

    $ttotal = $ttotal + $total;

///////////  SALE RETURN    reg - 28  

    $getr28 = mysqli_query($conn, "SELECT '28%' as descrip, b.gstperc, sum(b.subtot) as tamt,  sum(b.igstamt) as igsttax  
                              FROM `$mainret` a, `$tranret` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 28 and a.oms = 1  and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr28 = mysqli_fetch_array($getr28);

    $getr28c = mysqli_query($conn, "SELECT '28%' as descrip, b.gstperc, sum(b.subtot) as tamt,  sum(b.cgstamt) as cgsttax  
                              FROM `$mainret` a, `$tranret` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 28 and a.oms != 1  and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fetr28c = mysqli_fetch_array($getr28c);

    $ctr++;
    $descrip = $fetr28['descrip'];

    $netigst = $fetr28['tamt'];
    $postagei = "";
    $deemexpi = "";
    $igsttax = $fetr28['igsttax'];
    $exports = "";
    $exempted = "";

    $netgst = $fetr28c['tamt'];
    $postage = "";
    $deemexp = "";
    $cgsttax = $fetr28c['cgsttax'];
    $sgsttax = $fetr28c['cgsttax'];

    $total = $netigst + $igsttax + $netgst + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tnetigst = $tnetigst - $netigst;
    $tigsttax = $tigsttax - $igsttax;

    $tnetgst = $tnetgst - $netgst;
    $tcgsttax = $tcgsttax - $cgsttax;
    $tsgsttax = $tsgsttax - $sgsttax;

    $ttotal = $ttotal - $total;

    $contents.=  ",";
    $contents.= "Total" . ",";

    $contents.= $tnetigst . ",";
    $contents.= $tpostagei . ",";
    $contents.= $tdeemexpi . ",";
    $contents.= $tigsttax . ",";
    $contents.= $texports . ",";
    $contents.= $texempted . ",";

    $contents.= $tnetgst . ",";
    $contents.= $tpostage . ",";
    $contents.= $tdeemexp . ",";
    $contents.= $tcgsttax . ",";
    $contents.= $tsgsttax . ",";

    $contents.= $ttotal . "\n";

    $contents.= "\n";

////////////////////////    sale return

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

    ///////////  SALE RETURN    unreg - 0 

    $getu = mysqli_query($conn, "SELECT '0%' as descrip, b.gstperc, sum(b.subtot) as tamt 
                              FROM `$mainret` a, `$tranret` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 0  and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu = mysqli_fetch_array($getu);

    if (mysqli_num_rows($getu) > 0) {

        $ctr++;
        $descrip = $fetu['descrip'];

        $netigst = "";
        $postagei = "";
        $deemexpi = "";
        $igsttax = "";
        $exports = "";
        $exempted = $fetu['tamt'];

        $netgst = "";
        $postage = "";
        $deemexp = "";
        $cgsttax = "";
        $sgsttax = "";

        $total = $exempted;

        $contents.= $ctr . ",";
        $contents.= $descrip . ",";

        $contents.= $netigst . ",";
        $contents.= $postagei . ",";
        $contents.= $deemexpi . ",";
        $contents.= $igsttax . ",";
        $contents.= $exports . ",";
        $contents.= $exempted . ",";

        $contents.= $netgst . ",";
        $contents.= $postage . ",";
        $contents.= $deemexp . ",";
        $contents.= $cgsttax . ",";
        $contents.= $sgsttax . ",";

        $contents.= $total . "\n";

        $texempted = $texempted + $exempted;
        $ttotal = $ttotal + $total;
    }


    ///////////  SALE RETURN    unreg - 5  

    $getu5 = mysqli_query($conn, "SELECT '5%' as descrip, b.gstperc, sum(b.subtot) as tamt,  sum(b.igstamt) as igsttax  
                              FROM `$mainret` a, `$tranret` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 5 and a.oms = 1  and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu5 = mysqli_fetch_array($getu5);

    $getu5c = mysqli_query($conn, "SELECT '5%' as descrip, b.gstperc, sum(b.subtot) as tamt,  sum(b.cgstamt) as cgsttax  
                              FROM `$mainret` a, `$tranret` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 5 and a.oms != 1   and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu5c = mysqli_fetch_array($getu5c);

    
    $ctr++;
    $descrip = $fetu5['descrip'];

    $netigst = $fetu5['tamt'];
    $postagei = "";
    $deemexpi = "";
    $igsttax = $fetu5['igsttax'];
    $exports = "";
    $exempted = "";

    $netgst = $fetu5c['tamt'];
    $postage = "";
    $deemexp = "";
    $cgsttax = $fetu5c['cgsttax'];
    $sgsttax = $fetu5c['cgsttax'];

    $total = $netigst + $igsttax + $netgst + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tnetigst = $tnetigst + $netigst;
    $tigsttax = $tigsttax + $igsttax;

    $tnetgst = $tnetgst + $netgst;
    $tcgsttax = $tcgsttax + $cgsttax;
    $tsgsttax = $tsgsttax + $sgsttax;

    $ttotal = $ttotal + $total;

///////////  SALE RETURN    unreg - 12

    $getu12 = mysqli_query($conn, "SELECT '12%' as descrip, b.gstperc, sum(b.subtot) as tamt,  sum(b.igstamt) as igsttax  
                              FROM `$mainret` a, `$tranret` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 12 and a.oms = 1  and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu12 = mysqli_fetch_array($getu12);


    $getu12c = mysqli_query($conn, "SELECT '12%' as descrip, b.gstperc, sum(b.subtot) as tamt,  sum(b.cgstamt) as cgsttax  
                              FROM `$mainret` a, `$tranret` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 12 and a.oms != 1  and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu12c = mysqli_fetch_array($getu12c);


    $ctr++;
    $descrip = $fetu12['descrip'];

    $netigst = $fetu12['tamt']  ;
    $postagei = "";
    $deemexpi = "";
    $igsttax = $fetu12['igsttax']  ;
    $exports = "";
    $exempted = "";

    $netgst = $fetu12c['tamt'] ;
    $postage = "";
    $deemexp = "";
    $cgsttax = $fetu12c['cgsttax'] ;
    $sgsttax = $fetu12c['cgsttax'] ;

    $total = $netigst + $igsttax + $netgst + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tnetigst = $tnetigst + $netigst;
    $tigsttax = $tigsttax + $igsttax;

    $tnetgst = $tnetgst + $netgst;
    $tcgsttax = $tcgsttax + $cgsttax;
    $tsgsttax = $tsgsttax + $sgsttax;

    $ttotal = $ttotal + $total;

///////////  SALE RETURN    unreg - 18

    $getu18 = mysqli_query($conn, "SELECT '18%' as descrip, b.gstperc, sum(b.subtot) as tamt,  sum(b.igstamt) as igsttax  
                              FROM `$mainret` a, `$tranret` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 18 and a.oms = 1  and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu18 = mysqli_fetch_array($getu18);

    $getu18c = mysqli_query($conn, "SELECT '18%' as descrip, b.gstperc, sum(b.subtot) as tamt,  sum(b.cgstamt) as cgsttax  
                              FROM `$mainret` a, `$tranret` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 18 and a.oms != 1  and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu18c = mysqli_fetch_array($getu18c);


    $ctr++;
    $descrip = $fetu18['descrip'];

    $netigst = $fetu18['tamt'] ;
    $postagei = "";
    $deemexpi = "";
    $igsttax = $fetu18['igsttax'] ;
    $exports = "";
    $exempted = "";

    $netgst = $fetu18c['tamt'] ;
    $postage = "";
    $deemexp = "";
    $cgsttax = $fetu18c['cgsttax'] ;
    $sgsttax = $fetu18c['cgsttax'] ;

    $total = $netigst + $igsttax + $netgst + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tnetigst = $tnetigst + $netigst;
    $tigsttax = $tigsttax + $igsttax;

    $tnetgst = $tnetgst + $netgst;
    $tcgsttax = $tcgsttax + $cgsttax;
    $tsgsttax = $tsgsttax + $sgsttax;

    $ttotal = $ttotal + $total;


///////////  unreg - 28  

    $getu28 = mysqli_query($conn, "SELECT '28%' as descrip, b.gstperc, sum(b.subtot) as tamt, sum(b.igstamt) as igsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 28 and a.oms = 1  and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu28 = mysqli_fetch_array($getu28);

    $getu28c = mysqli_query($conn, "SELECT '28%' as descrip, b.gstperc, sum(b.subtot) as tamt, sum(b.cgstamt) as cgsttax  
                              FROM `salemain` a, `saletran` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and b.gstperc = 28 and a.oms != 1  and 
                              a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fetu28c = mysqli_fetch_array($getu28c);

    $ctr++;
    $descrip = $fetu28['descrip'];

    $netigst = $fetu28['tamt'];
    $postagei = "";
    $deemexpi = "";
    $igsttax = $fetu28['igsttax'];
    $exports = "";
    $exempted = "";

    $netgst = $fetu28c['tamt'];
    $postage = "";
    $deemexp = "";
    $cgsttax = $fetu28c['cgsttax'];
    $sgsttax = $fetu28c['cgsttax'];

    $total = $netigst + $igsttax + $netgst + $cgsttax + $sgsttax;

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";

    $contents.= $netigst . ",";
    $contents.= $postagei . ",";
    $contents.= $deemexpi . ",";
    $contents.= $igsttax . ",";
    $contents.= $exports . ",";
    $contents.= $exempted . ",";

    $contents.= $netgst . ",";
    $contents.= $postage . ",";
    $contents.= $deemexp . ",";
    $contents.= $cgsttax . ",";
    $contents.= $sgsttax . ",";

    $contents.= $total . "\n";

    $tnetigst = $tnetigst + $netigst;
    $tigsttax = $tigsttax + $igsttax;

    $tnetgst = $tnetgst + $netgst;
    $tcgsttax = $tcgsttax + $cgsttax;
    $tsgsttax = $tsgsttax + $sgsttax;

    $ttotal = $ttotal + $total;

    $contents.=  ",";
    $contents.= "Total" . ",";

    $contents.= $tnetigst . ",";
    $contents.= $tpostagei . ",";
    $contents.= $tdeemexpi . ",";
    $contents.= $tigsttax . ",";
    $contents.= $texports . ",";
    $contents.= $texempted . ",";

    $contents.= $tnetgst . ",";
    $contents.= $tpostage . ",";
    $contents.= $tdeemexp . ",";
    $contents.= $tcgsttax . ",";
    $contents.= $tsgsttax . ",";

    $contents.= $ttotal . "\n";

    $contents.= "\n";


    $contents = strip_tags($contents);

    header("Content-Disposition: attachment; filename=$formname" . "_Summary" . date('d-m-Y') . ".csv");
    print $contents;
}
?>