<?php

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

$formname = "Input / Output GST";
$file = "gstr1_7.php";

$compcd = $_SESSION["comp_" . $dbname];
$zroot = $_SESSION["zdir"];
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
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");
    }

    $iotype = $_POST['iotype'];

    if ($iotype == "1" || $iotype == "2") {
        $getpmain = mysqli_query($conn, "SELECT sum(cgst3) as pucgst3, sum(sgst3) as pusgst3, sum(igst3)  as puigst3, 
                                 sum(cgst5) as pucgst5, sum(sgst5) as pusgst5, sum(igst5)  as puigst5, 
                                 sum(cgst12) as pucgst12, sum(sgst12) as pusgst12, sum(igst12) as puigst12, 
                                 sum(cgst18) as pucgst18, sum(sgst18)  as pusgst18, sum(igst18) as puigst18, 
                                 sum(cgst28) as pucgst28, sum(sgst28) as pusgst28, sum(igst28) as puigst28 FROM `rawmain` 
                                 WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $pmain = mysqli_fetch_array($getpmain);

        $getdmain = mysqli_query($conn, "SELECT sum(cgst3) as prcgst3, sum(sgst3) as prsgst3, sum(igst3)  as prigst3, 
                                 sum(cgst5) as prcgst5, sum(sgst5) as prsgst5, sum(igst5)  as prigst5, 
                                 sum(cgst12) as prcgst12, sum(sgst12) as prsgst12, sum(igst12) as prigst12, 
                                 sum(cgst18) as prcgst18, sum(sgst18)  as prsgst18, sum(igst18) as prigst18, 
                                 sum(cgst28) as prcgst28, sum(sgst28) as prsgst28, sum(igst28) as prigst28 FROM `debmain` 
                                 WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $dmain = mysqli_fetch_array($getdmain);


        $getpuoth3 = mysqli_query($conn, "SELECT sum(cgstamt) as cgst3, sum(sgstamt) as sgst3, sum(igstamt) as igst3 FROM `puotmain` 
                                 WHERE gsttype = 3 and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'")or die(mysqli_error($conn));

        $getpuoth5 = mysqli_query($conn, "SELECT sum(cgstamt) as cgst5, sum(sgstamt) as sgst5, sum(igstamt) as igst5 FROM `puotmain` 
                                 WHERE gsttype = 5 and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'")or die(mysqli_error($conn));

        $getpuoth12 = mysqli_query($conn, "SELECT sum(cgstamt) as cgst12, sum(sgstamt) as sgst12, sum(igstamt) as igst12 FROM `puotmain` 
                                 WHERE gsttype = 12 and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'")or die(mysqli_error($conn));

        $getpuoth18 = mysqli_query($conn, "SELECT sum(cgstamt) as cgst18, sum(sgstamt) as sgst18, sum(igstamt) as igst18 FROM `puotmain` 
                                 WHERE gsttype = 18 and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'")or die(mysqli_error($conn));

        $getpuoth28 = mysqli_query($conn, "SELECT sum(cgstamt) as cgst28, sum(sgstamt) as sgst28, sum(igstamt) as igst28 FROM `puotmain` 
                                 WHERE gsttype = 28 and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'")or die(mysqli_error($conn));


//        for cgst

        $taxbl_3 = mysqli_query($conn, "SELECT SUM(subtot) as subtot3  from rawtran where vouchno != '' and cgstrate = '1.50' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable3 = mysqli_fetch_array($taxbl_3);
        //purchase other
        $taxbl_oth_3 = mysqli_query($conn, "SELECT SUM(subtot) as subtot3  from puottran where vouchno != '' and cgstrate = '1.50' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_3 = mysqli_fetch_array($taxbl_oth_3);

        $taxble3 = $taxable_oth_3['subtot3'] + $taxable3['subtot3'];


        $taxbl_5 = mysqli_query($conn, "SELECT SUM(subtot) as subtot5 from rawtran where vouchno != '' and cgstrate = '2.50' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable5 = mysqli_fetch_array($taxbl_5);
        //purchase other
        $taxbl_oth_5 = mysqli_query($conn, "SELECT SUM(subtot) as subtot5 from puottran where vouchno != '' and cgstrate = '2.50' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_5 = mysqli_fetch_array($taxbl_oth_5);

        $taxble5 = $taxable_oth_5['subtot5'] + $taxable5['subtot5'];


        $taxbl_6 = mysqli_query($conn, "SELECT SUM(subtot) as subtot6 from rawtran where vouchno != '' and cgstrate = '6.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable6 = mysqli_fetch_array($taxbl_6);
        //purchase other        
        $taxbl_oth_6 = mysqli_query($conn, "SELECT SUM(subtot) as subtot6 from puottran where vouchno != '' and cgstrate = '6.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_6 = mysqli_fetch_array($taxbl_oth_6);
        $taxble6 = $taxable_oth_6['subtot'] + $taxable6['subtot6'];

        $taxbl_9 = mysqli_query($conn, "SELECT SUM(subtot) as subtot9 from rawtran where vouchno != '' and cgstrate = '9.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable9 = mysqli_fetch_array($taxbl_9);
        //purchase other        
        $taxbl_oth_9 = mysqli_query($conn, "SELECT SUM(subtot) as subtot9 from puottran where vouchno != '' and cgstrate = '9.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_9 = mysqli_fetch_array($taxbl_oth_9);
        $taxble9 = $taxable_oth_9['subtot9'] + $taxable9['subtot9'];

        $taxbl_14 = mysqli_query($conn, "SELECT SUM(subtot) as subtot14 from rawtran where vouchno != '' and cgstrate = '14.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable14 = mysqli_fetch_array($taxbl_14);
        //purchase other        
        $taxbl_oth_14 = mysqli_query($conn, "SELECT SUM(subtot) as subtot14 from puottran where vouchno != '' and cgstrate = '14.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_14 = mysqli_fetch_array($taxbl_oth_14);
        $taxble14 = $taxable_oth_14['subtot14'] + $taxable14['subtot14'];

//         for igst
        $taxbl_i3 = mysqli_query($conn, "SELECT SUM(subtot) as subtot3 from rawtran where vouchno != '' and igstrate = '3.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_i3 = mysqli_fetch_array($taxbl_i3);
//        purchase other
        $taxbl_oth_i3 = mysqli_query($conn, "SELECT SUM(subtot) as subtot3 from puottran where vouchno != '' and igstrate = '3.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_i3 = mysqli_fetch_array($taxbl_oth_i3);

        $taxble_i3 = $taxable_oth_i3['subtot3'] + $taxable_i3['subtot3'];

        $taxbl_i5 = mysqli_query($conn, "SELECT SUM(subtot) as subtot5 from rawtran where vouchno != '' and igstrate = '5.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_i5 = mysqli_fetch_array($taxbl_i5);
//        purchase other        
        $taxbl_oth_i5 = mysqli_query($conn, "SELECT SUM(subtot) as subtot5 from puottran where vouchno != '' and igstrate = '5.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_i5 = mysqli_fetch_array($taxbl_oth_i5);

        $taxble_i5 = $taxable_oth_i5['subtot5'] + $taxable_i5['subtot5'];

        $taxbl_i12 = mysqli_query($conn, "SELECT SUM(subtot) as subtot12 from rawtran where vouchno != '' and igstrate = '12.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_i12 = mysqli_fetch_array($taxbl_i12);
        //        purchase other        
        $taxbl_oth_i12 = mysqli_query($conn, "SELECT SUM(subtot) as subtot12 from puottran where vouchno != '' and igstrate = '12.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_i12 = mysqli_fetch_array($taxbl_oth_i12);

        $taxble_i12 = $taxable_oth_i12['subtot12'] + $taxable_i12['subtot12'];


        $taxbl_i18 = mysqli_query($conn, "SELECT SUM(subtot) as subtot18 from rawtran where vouchno != '' and igstrate = '18.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_i18 = mysqli_fetch_array($taxbl_i18);
        //        purchase other        
        $taxbl_oth_i18 = mysqli_query($conn, "SELECT SUM(subtot) as subtot18 from puottran where vouchno != '' and igstrate = '18.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_i18 = mysqli_fetch_array($taxbl_oth_i18);

        $taxble_i18 = $taxable_oth_i18['subtot18'] + $taxable_i18['subtot18'];


        $taxbl_i28 = mysqli_query($conn, "SELECT SUM(subtot) as subtot28  from rawtran where vouchno != '' and igstrate = '28.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_i28 = mysqli_fetch_array($taxbl_i28);
        //        purchase other        
        $taxbl_oth_i28 = mysqli_query($conn, "SELECT SUM(subtot) as subtot28  from puottran where vouchno != '' and igstrate = '28.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_i28 = mysqli_fetch_array($taxbl_oth_i28);

        $taxble_i28 = $taxable_oth_i28['subtot28'] + $taxable_i28['subtot28'];

        //taxble_end


        $puotmain3 = mysqli_fetch_array($getpuoth3);
        $puotmain5 = mysqli_fetch_array($getpuoth5);
        $puotmain12 = mysqli_fetch_array($getpuoth12);
        $puotmain18 = mysqli_fetch_array($getpuoth18);
        $puotmain28 = mysqli_fetch_array($getpuoth28);

        $pudbcbal3 = $pmain['pcgst3'] + $puotmain3['cgst3'] - $dmain['prcgst3'];
        $pudbsbal3 = $pmain['pusgst3'] + $puotmain3['sgst3'] - $dmain['prsgst3'];
        $pudbibal3 = $pmain['puigst3'] + $puotmain3['igst3'] - $dmain['prigst3'];

        $pudbcbal5 = $pmain['pucgst5'] + $puotmain5['cgst5'] - $dmain['prcgst5'];
        $pudbsbal5 = $pmain['pusgst5'] + $puotmain5['sgst5'] - $dmain['prsgst5'];
        $pudbibal5 = $pmain['puigst5'] + $puotmain5['igst5'] - $dmain['prigst5'];

        $pudbcbal12 = $pmain['pucgst12'] + $puotmain12['cgst12'] - $dmain['prcgst12'];
        $pudbsbal12 = $pmain['pusgst12'] + $puotmain12['sgst12'] - $dmain['prsgst12'];
        $pudbibal12 = $pmain['puigst12'] + $puotmain12['igst12'] - $dmain['prigst12'];

        $pudbcbal18 = $pmain['pucgst18'] + $puotmain18['cgst18'] - $dmain['prcgst18'];
        $pudbsbal18 = $pmain['pusgst18'] + $puotmain18['sgst18'] - $dmain['prsgst18'];
        $pudbibal18 = $pmain['puigst18'] + $puotmain18['igst18'] - $dmain['prigst18'];

        $pudbcbal28 = $pmain['pucgst28'] + $puotmain28['cgst28'] - $dmain['prcgst28'];
        $pudbsbal28 = $pmain['pusgst28'] + $puotmain28['sgst28'] - $dmain['prsgst28'];
        $pudbibal28 = $pmain['puigst28'] + $puotmain28['igst28'] - $dmain['prigst28'];

        $intotal = $pudbcbal3 + $pudbsbal3 + $pudbibal3 + $pudbcbal5 + $pudbsbal5 + $pudbibal5 +
                $pudbcbal12 + $pudbsbal12 + $pudbibal12 + $pudbcbal18 + $pudbsbal18 + $pudbibal18 +
                $pudbcbal28 + $pudbsbal28 + $pudbibal28;

        $tottaxble = $taxble3 + $taxble_i3 +
                $taxble5 + $taxble_i5 +
                $taxble6 + $taxble_i12 +
                $taxble9 + $taxble_i18 +
                $taxble14 + $taxble_i28;

        $taxble_1_5 = $taxble3 / 2;
        $taxble_2_5 = $taxble5 / 2;
        $taxble_6 = $taxble6 / 2;
        $taxble_9 = $taxble9 / 2;
        $taxble_14 = $taxble14 / 2;


        $contents .= ",INPUT GST\nSr No., GST Type, Amount,Taxable Amt.\n";

        $contents .= "1, CGST 1.5%, $pudbcbal3,$taxble_1_5\n";
        $contents .= ",  SGST 1.5%, $pudbsbal3,$taxble_1_5\n";
        $contents .= ",  IGST 3%, $pudbibal3,$taxble_i3\n\n";

        $contents .= "2, CGST 2.5%, $pudbcbal5,$taxble_2_5\n";
        $contents .= ",  SGST 2.5%, $pudbsbal5,$taxble_2_5\n";
        $contents .= ",  IGST 5%, $pudbibal5,$taxble_i5\n\n";

        $contents .= "3, CGST 6%, $pudbcbal12,$taxble_6\n";
        $contents .= ",  SGST 6%, $pudbsbal12,$taxble_6\n";
        $contents .= ",  IGST 12%, $pudbibal12,$taxble_i12\n\n";

        $contents .= "4, CGST 9%, $pudbcbal18,$taxble_9\n";
        $contents .= ",  SGST 9%, $pudbsbal18,$taxble_9\n";
        $contents .= ",  IGST 18%, $pudbibal18,$taxble_i18\n\n";

        $contents .= "5, CGST 14%, $pudbcbal28,$taxble_14\n";
        $contents .= ",  SGST 14%, $pudbsbal28,$taxble_14\n";
        $contents .= ",  IGST 28%, $pudbibal28,$taxble_i28\n\n";
        $contents .= ",Total,$intotal,$tottaxble\n\n\n\n";
    }

    if ($iotype == "1" || $iotype == "3") {
        $getslmain = mysqli_query($conn, "SELECT sum(cgst1) as slcgst1, sum(sgst1) as slsgst1, sum(igst1)  as sligst1, 
                                 sum(cgst5) as slcgst5, sum(sgst5) as slsgst5, sum(igst5)  as sligst5, 
                                 sum(cgst12) as slcgst12, sum(sgst12) as slsgst12, sum(igst12) as sligst12, 
                                 sum(cgst18) as slcgst18, sum(sgst18)  as slsgst18, sum(igst18) as sligst18, 
                                 sum(cgst28) as slcgst28, sum(sgst28) as slsgst28, sum(igst28) as sligst28 FROM `salemain` 
                                 WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $slmain = mysqli_fetch_array($getslmain);

        $getdemain = mysqli_query($conn, "SELECT sum(cgst1) as decgst1, sum(sgst1) as desgst1, sum(igst1)  as deigst1, 
                                 sum(cgst5) as decgst5, sum(sgst5) as desgst5, sum(igst5)  as deigst5, 
                                 sum(cgst12) as decgst12, sum(sgst12) as desgst12, sum(igst12) as deigst12, 
                                 sum(cgst18) as decgst18, sum(sgst18)  as desgst18, sum(igst18) as deigst18, 
                                 sum(cgst28) as decgst28, sum(sgst28) as desgst28, sum(igst28) as deigst28 FROM `deexmain` 
                                 WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $demain = mysqli_fetch_array($getdemain);

        $getcrmain = mysqli_query($conn, "SELECT sum(cgst5) as crcgst5, sum(sgst5) as crsgst5, sum(igst5)  as crigst5, 
                                 sum(cgst12) as crcgst12, sum(sgst12) as crsgst12, sum(igst12) as crigst12, 
                                 sum(cgst18) as crcgst18, sum(sgst18)  as crsgst18, sum(igst18) as crigst18, 
                                 sum(cgst28) as crcgst28, sum(sgst28) as crsgst28, sum(igst28) as crigst28 FROM `credmain` 
                                 WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $crmain = mysqli_fetch_array($getcrmain);

        $getcredoth1 = mysqli_query($conn, "SELECT sum(cgstamt) as cgst1, sum(sgstamt) as sgst1, sum(igstamt) as igst1 FROM `credmain` 
                                 WHERE gsttype < 3 and credtype = 'O' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));

        $getcredoth5 = mysqli_query($conn, "SELECT sum(cgstamt) as cgst5, sum(sgstamt) as sgst5, sum(igstamt) as igst5 FROM `credmain` 
                                 WHERE gsttype = 5 and credtype = 'O' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));

        $getcredoth12 = mysqli_query($conn, "SELECT sum(cgstamt) as cgst12, sum(sgstamt) as sgst12, sum(igstamt) as igst12 FROM `credmain` 
                                 WHERE gsttype = 12 and credtype = 'O' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));

        $getcredoth18 = mysqli_query($conn, "SELECT sum(cgstamt) as cgst18, sum(sgstamt) as sgst18, sum(igstamt) as igst18 FROM `credmain` 
                                 WHERE gsttype = 18 and credtype = 'O' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));

        $getcredoth28 = mysqli_query($conn, "SELECT sum(cgstamt) as cgst28, sum(sgstamt) as sgst28, sum(igstamt) as igst28 FROM `credmain` 
                                 WHERE gsttype = 28 and credtype = 'O' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));

//taxable start
        $taxbl_1 = mysqli_query($conn, "SELECT SUM(subtot) as subtot1  from saletran where vouchno != '' and cgstrate = '0.05' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable1 = mysqli_fetch_array($taxbl_1);

        $taxbl_de_1 = mysqli_query($conn, "SELECT SUM(subtot) as subtot1  from deextran where vouchno != '' and cgstrate = '0.05' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_de1 = mysqli_fetch_array($taxbl_de_1);

        $taxble1 =  $taxable1['subtot1'] + $taxable_de1['subtot1'];


        $taxbl_5 = mysqli_query($conn, "SELECT SUM(subtot) as subtot5 from saletran where vouchno != '' and cgstrate = '2.50' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable5 = mysqli_fetch_array($taxbl_5);
        //sale other
        $taxbl_oth_5 = mysqli_query($conn, "SELECT SUM(taxableamt) as subtot5 from slotmain where vouchno != '' and cgstrate = '2.50' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_5 = mysqli_fetch_array($taxbl_oth_5);

        $taxble5 = $taxable_oth_5['subtot5'] + $taxable5['subtot5'];


        $taxbl_6 = mysqli_query($conn, "SELECT SUM(subtot) as subtot6 from saletran where vouchno != '' and cgstrate = '6.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable6 = mysqli_fetch_array($taxbl_6);
        //sale other        
        $taxbl_oth_6 = mysqli_query($conn, "SELECT SUM(taxableamt) as subtot6 from slotmain where vouchno != '' and cgstrate = '6.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_6 = mysqli_fetch_array($taxbl_oth_6);
       
        $taxble6 = $taxable_oth_6['subtot'] + $taxable6['subtot6'];

        $taxbl_9 = mysqli_query($conn, "SELECT SUM(subtot) as subtot9 from saletran where vouchno != '' and cgstrate = '9.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable9 = mysqli_fetch_array($taxbl_9);
        //sale other        
        $taxbl_oth_9 = mysqli_query($conn, "SELECT SUM(taxableamt) as subtot9 from slotmain where vouchno != '' and cgstrate = '9.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_9 = mysqli_fetch_array($taxbl_oth_9);
        $taxble9 = $taxable_oth_9['subtot9'] + $taxable9['subtot9'];

        $taxbl_14 = mysqli_query($conn, "SELECT SUM(subtot) as subtot14 from saletran where vouchno != '' and cgstrate = '14.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable14 = mysqli_fetch_array($taxbl_14);
        //sale other        
        $taxbl_oth_14 = mysqli_query($conn, "SELECT SUM(taxableamt) as subtot14 from slotmain where vouchno != '' and cgstrate = '14.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_14 = mysqli_fetch_array($taxbl_oth_14);
        $taxble14 = $taxable_oth_14['subtot14'] + $taxable14['subtot14'];

//         for igst
        $taxbl_i1 = mysqli_query($conn, "SELECT SUM(subtot) as subtot1 from saletran where vouchno != '' and igstrate = '0.10' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_i1 = mysqli_fetch_array($taxbl_i1);

        $taxbl_de_i1 = mysqli_query($conn, "SELECT SUM(subtot) as subtot1 from deextran where vouchno != '' and igstrate = '0.10' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_de_i1 = mysqli_fetch_array($taxbl_de_i1);
//        sale other
        $taxbl_oth_i1 = mysqli_query($conn, "SELECT SUM(taxableamt) as subtot1 from slotmain where vouchno != '' and igstrate = '0.10' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_i1 = mysqli_fetch_array($taxbl_oth_i1);

        $taxble_i1 = $taxable_oth_i1['subtot1'] + $taxable_i1['subtot1'] + $taxable_de_i1['subtot1'];

        $taxbl_i5 = mysqli_query($conn, "SELECT SUM(subtot) as subtot5 from saletran where vouchno != '' and igstrate = '5.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_i5 = mysqli_fetch_array($taxbl_i5);
//        sale other        
        $taxbl_oth_i5 = mysqli_query($conn, "SELECT SUM(taxableamt) as subtot5 from slotmain where vouchno != '' and igstrate = '5.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_i5 = mysqli_fetch_array($taxbl_oth_i5);

        $taxble_i5 = $taxable_oth_i5['subtot5'] + $taxable_i5['subtot5'];

        $taxbl_i12 = mysqli_query($conn, "SELECT SUM(subtot) as subtot12 from saletran where vouchno != '' and igstrate = '12.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_i12 = mysqli_fetch_array($taxbl_i12);
        //        sale other        
        $taxbl_oth_i12 = mysqli_query($conn, "SELECT SUM(taxableamt) as subtot12 from slotmain where vouchno != '' and igstrate = '12.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_i12 = mysqli_fetch_array($taxbl_oth_i12);

        $taxble_i12 = $taxable_oth_i12['subtot12'] + $taxable_i12['subtot12'];


        $taxbl_i18 = mysqli_query($conn, "SELECT SUM(subtot) as subtot18 from saletran where vouchno != '' and igstrate = '18.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_i18 = mysqli_fetch_array($taxbl_i18);
        //        sale other        
        $taxbl_oth_i18 = mysqli_query($conn, "SELECT SUM(taxableamt) as subtot18 from slotmain where vouchno != '' and igstrate = '18.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_i18 = mysqli_fetch_array($taxbl_oth_i18);

        $taxble_i18 = $taxable_oth_i18['subtot18'] + $taxable_i18['subtot18'];


        $taxbl_i28 = mysqli_query($conn, "SELECT SUM(subtot) as subtot28  from saletran where vouchno != '' and igstrate = '28.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_i28 = mysqli_fetch_array($taxbl_i28);
        //        sale other        
        $taxbl_oth_i28 = mysqli_query($conn, "SELECT SUM(taxableamt) as subtot28  from slotmain where vouchno != '' and igstrate = '28.00' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable_oth_i28 = mysqli_fetch_array($taxbl_oth_i28);

        $taxble_i28 = $taxable_oth_i28['subtot28'] + $taxable_i28['subtot28'];

        //taxble_end


        $cromain1 = mysqli_fetch_array($getcredoth1);
        $cromain5 = mysqli_fetch_array($getcredoth5);
        $cromain12 = mysqli_fetch_array($getcredoth12);
        $cromain18 = mysqli_fetch_array($getcredoth18);
        $cromain28 = mysqli_fetch_array($getcredoth28);

        $sldbcbal1 = $slmain['slcgst1'] + $demain['decgst1'] - $cromain1['cgst1'];
        $sldbsbal1 = $slmain['slsgst1'] + $demain['desgst1'] - $cromain1['sgst1'];
        $sldbibal1 = $slmain['sligst1'] + $demain['deigst1'] - $cromain1['igst1'];

        $sldbcbal5 = $slmain['slcgst5'] + $demain['decgst5'] - $crmain['crcgst5'] - $cromain5['cgst5'];
        $sldbsbal5 = $slmain['slsgst5'] + $demain['desgst5'] - $crmain['crsgst5'] - $cromain5['sgst5'];
        $sldbibal5 = $slmain['sligst5'] + $demain['deigst5'] - $crmain['crigst5'] - $cromain5['igst5'];

        $sldbcbal12 = $slmain['slcgst12'] + $demain['decgst5'] - $crmain['crcgst12'] - $cromain12['cgst12'];
        $sldbsbal12 = $slmain['slsgst12'] + $demain['desgst12'] - $crmain['crsgst12'] - $cromain12['sgst12'];
        $sldbibal12 = $slmain['sligst12'] + $demain['deigst12'] - $crmain['crigst12'] - $cromain12['igst12'];

        $sldbcbal18 = $slmain['slcgst18'] + $demain['decgst18'] - $crmain['crcgst18'] - $cromain18['cgst18'];
        $sldbsbal18 = $slmain['slsgst18'] + $demain['desgst18'] - $crmain['crsgst18'] - $cromain18['sgst18'];
        $sldbibal18 = $slmain['sligst18'] + $demain['deigst18'] - $crmain['crigst18'] - $cromain18['igst18'];

        $sldbcbal28 = $slmain['slcgst28'] + $demain['decgst28'] - $crmain['crcgst28'] - $cromain28['cgst28'];
        $sldbsbal28 = $slmain['slsgst28'] + $demain['desgst28'] - $crmain['crsgst28'] - $cromain28['sgst28'];
        $sldbibal28 = $slmain['sligst28'] + $demain['deigst28'] - $crmain['crigst28'] - $cromain28['igst28'];


        $taxbl_1_de = mysqli_query($conn, "SELECT SUM(subtot) as subtot1  from deextran where vouchno != '' and cgstrate = '0.05' 
                                and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable1_de = mysqli_fetch_array($taxbl_1_de);
        $taxble1_de = $taxable1_de['subtot1'];

        $taxbl_i1_de = mysqli_query($conn, "SELECT SUM(subtot) as subtot1  from deextran where vouchno != '' and igstrate = '0.10' 
                                and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxablei1_de = mysqli_fetch_array($taxbl_i1_de);
        $taxblei1_de = $taxablei1_de['subtot1'];


        $taxbl_3_sl = mysqli_query($conn, "SELECT SUM(subtot) as subtot3  from saletran where vouchno != '' and cgstrate = '1.50' 
                                and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable3_sl = mysqli_fetch_array($taxbl_3_sl);
        $taxble3_sl = $taxable3['subtot3'];

        $taxbl_5_sl = mysqli_query($conn, "SELECT SUM(subtot) as subtot5  from saletran where vouchno != '' and cgstrate = '2.50' 
                                and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable5_sl = mysqli_fetch_array($taxbl_5_sl);
        $taxble5_sl = $taxable3['subtot5'];

        $taxbl_12_sl = mysqli_query($conn, "SELECT SUM(subtot) as subtot12  from saletran where vouchno != '' and cgstrate = '6.00' 
                                and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable12_sl = mysqli_fetch_array($taxbl_12_sl);
        $taxble12_sl = $taxable3['subtot12'];

        $taxbl_18_sl = mysqli_query($conn, "SELECT SUM(subtot) as subtot18  from saletran where vouchno != '' and cgstrate = '9.00' 
                                and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable18_sl = mysqli_fetch_array($taxbl_12_sl);
        $taxble18_sl = $taxable3['subtot18'];

        $taxbl_28_sl = mysqli_query($conn, "SELECT SUM(subtot) as subtot28  from saletran where vouchno != '' and cgstrate = '14.00' 
                                and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
        $taxable28_sl = mysqli_fetch_array($taxbl_28_sl);
        $taxble28_sl = $taxable3['subtot28'];


        $outotal = $sldbcbal1 + $sldbsbal1 + $sldbibal1 + $sldbcbal5 + $sldbsbal5 + $sldbibal5 +
                $sldbcbal12 + $sldbsbal12 + $sldbibal12 + $sldbcbal18 + $sldbsbal18 + $sldbibal18 +
                $sldbcbal28 + $sldbsbal28 + $sldbibal28;


//        $tottaxble = $taxble1 + $taxble_i1 + $taxble3 + $taxble_i3 +
        $tottaxble = $taxble1 + $taxble_i1 +
                     $taxble5 + $taxble_i5 +
                     $taxble6 + $taxble_i12 +
                     $taxble9 + $taxble_i18 +
                     $taxble14 + $taxble_i28;

        $taxble_0_5 = $taxble1 / 2;
        $taxble_2_5 = $taxble5 / 2;
        $taxble_6 = $taxble6 / 2;
        $taxble_9 = $taxble9 / 2;
        $taxble_14 = $taxble14 / 2;



        $contents .= ",OUTPUT GST\nSr No., GST Type, Amount,Taxable Amt.\n";

        $contents .= "1, CGST 0.5%, $sldbcbal1,$taxble_0_5\n";
        $contents .= ",  SGST 0.5%, $sldbsbal1,$taxble_0_5\n";
        $contents .= ",  IGST 1%, $sldbibal1,$taxble_i1\n\n";

        $contents .= "2, CGST 2.5%, $sldbcbal5,$taxble_2_5\n";
        $contents .= ",  SGST 2.5%, $sldbsbal5,$taxble_2_5\n";
        $contents .= ",  IGST 5%, $sldbibal5,$taxble_i5\n\n";

        $contents .= "3, CGST 6%, $sldbcbal12,$taxble_6\n";
        $contents .= ",  SGST 6%, $sldbsbal12,$taxble_6\n";
        $contents .= ",  IGST 12%, $sldbibal12,$taxble_i12\n\n";

        $contents .= "4, CGST 9%, $sldbcbal18,$taxble_9\n";
        $contents .= ",  SGST 9%, $sldbsbal18,$taxble_9\n";
        $contents .= ",  IGST 18%, $sldbibal18,$taxble_i18\n\n";

        $contents .= "5, CGST 14%, $sldbcbal28,$taxble_14\n";
        $contents .= ",  SGST 14%, $sldbsbal28,$taxble_14\n";
        $contents .= ",  IGST 28%, $sldbibal28,$taxble_i28\n\n";
        $contents .= ",Total,$outotal,$tottaxble\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;
    }
}
?>