<?php

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

$maindb = "purmain";
$maindb2 = 'debmain';
$maindb3 = 'salemain';
$maindb4 = 'credmain';

$formname = "GST Summary";
$file = "gstsum.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");
    }

    $getsaletype = mysqli_query($conn, "SELECT code, name FROM `saletype` WHERE name LIKE '%GST%' order by per ")or die(mysqli_error($conn));
    $getsaletype2 = mysqli_query($conn, "SELECT code, name FROM `saletype` WHERE name LIKE '%GST%' order by per ")or die(mysqli_error($conn));
    $getsaletype3 = mysqli_query($conn, "SELECT code, name FROM `saletype` WHERE name LIKE '%GST%' order by per ")or die(mysqli_error($conn));
    $getsaletype4 = mysqli_query($conn, "SELECT code, name FROM `saletype` WHERE name LIKE '%GST%' order by per ")or die(mysqli_error($conn));

    $contents.="Purchase\n";
    $contents.="Description, CGST ,, SGST,, IGST\n";
    $contents.=",Amount,Tax,Amount,Tax,Amount,Tax\n";


    while ($fetsaletype = mysqli_fetch_array($getsaletype)) {
        $gpurcgst = mysqli_query($conn, "SELECT SUM(subtotal) as camt, SUM(totcgst) as ctax FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' AND gstperc='$fetsaletype[code]'  ")or die(mysqli_error($conn));
        $fpurcgst = mysqli_fetch_array($gpurcgst);

        $gpurigst = mysqli_query($conn, "SELECT SUM(subtotal) as iamt, SUM(totigst) as itax FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' AND gstperc='$fetsaletype[code]' AND `totigst` > '0'")or die(mysqli_error($conn));
        $fpurigst = mysqli_fetch_array($gpurigst);

        $contents.= $fetsaletype[name] . ",";
        $contents.= $fpurcgst[camt] . ",";
        $contents.= $fpurcgst[ctax] . ",";
        $contents.= $fpurcgst[camt] . ",";
        $contents.= $fpurcgst[ctax] . ",";
        $contents.= $fpurigst[iamt] . ",";
        $contents.= $fpurigst[itax] . "\n";
    }
    $contents.="\n\n\n\n";
    $contents.="Purchase Return\n";
    $contents.="Description, CGST ,, SGST,, IGST\n";
    $contents.=",Amount,Tax,Amount,Tax,Amount,Tax\n";


    while ($fetsaletype2 = mysqli_fetch_array($getsaletype2)) {
        $gpurcgst2 = mysqli_query($conn, "SELECT SUM(subtotal) as camt, SUM(totcgst) as ctax FROM `$maindb2` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' AND gstperc='$fetsaletype2[code]'  ")or die(mysqli_error($conn));
        $fpurcgst2 = mysqli_fetch_array($gpurcgst2);

        $gpurigst2 = mysqli_query($conn, "SELECT SUM(subtotal) as iamt, SUM(totigst) as itax FROM `$maindb2` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' AND gstperc='$fetsaletype2[code]' AND `totigst` > '0'")or die(mysqli_error($conn));
        $fpurigst2 = mysqli_fetch_array($gpurigst2);

        $contents.= $fetsaletype2[name] . ",";
        $contents.= $fpurcgst2[camt] . ",";
        $contents.= $fpurcgst2[ctax] . ",";
        $contents.= $fpurcgst2[camt] . ",";
        $contents.= $fpurcgst2[ctax] . ",";
        $contents.= $fpurigst2[iamt] . ",";
        $contents.= $fpurigst2[itax] . "\n";
    }
    $contents.="\n\n\n\n";
    $contents.="Sale\n";
    $contents.="Description, CGST ,, SGST,, IGST\n";
    $contents.=",Amount,Tax,Amount,Tax,Amount,Tax\n";


    while ($fetsaletype3 = mysqli_fetch_array($getsaletype3)) {
        $gpurcgst3 = mysqli_query($conn, "SELECT SUM(subtotal) as camt, SUM(totcgst) as ctax FROM `$maindb3` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' AND gstperc='$fetsaletype3[code]'  ")or die(mysqli_error($conn));
        $fpurcgst3 = mysqli_fetch_array($gpurcgst3);

        $gpurigst3 = mysqli_query($conn, "SELECT SUM(subtotal) as iamt, SUM(totigst) as itax FROM `$maindb3` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' AND gstperc='$fetsaletype3[code]' AND `totigst` > '0'")or die(mysqli_error($conn));
        $fpurigst3 = mysqli_fetch_array($gpurigst3);

        $contents.= $fetsaletype3[name] . ",";
        $contents.= $fpurcgst3[camt] . ",";
        $contents.= $fpurcgst3[ctax] . ",";
        $contents.= $fpurcgst3[camt] . ",";
        $contents.= $fpurcgst3[ctax] . ",";
        $contents.= $fpurigst3[iamt] . ",";
        $contents.= $fpurigst3[itax] . "\n";
    }
    $contents.="\n\n\n\n";
    $contents.="Sale Return\n";
    $contents.="Description, CGST ,, SGST,, IGST\n";
    $contents.=",Amount,Tax,Amount,Tax,Amount,Tax\n";


    while ($fetsaletype4 = mysqli_fetch_array($getsaletype4)) {
        $gpurcgst4 = mysqli_query($conn, "SELECT SUM(subtotal) as camt, SUM(totcgst) as ctax FROM `$maindb4` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' AND gstperc='$fetsaletype4[code]'  ")or die(mysqli_error($conn));
        $fpurcgst4 = mysqli_fetch_array($gpurcgst4);

        $gpurigst4 = mysqli_query($conn, "SELECT SUM(subtotal) as iamt, SUM(totigst) as itax FROM `$maindb4` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' AND gstperc='$fetsaletype4[code]' AND `totigst` > '0'")or die(mysqli_error($conn));
        $fpurigst4 = mysqli_fetch_array($gpurigst4);

        $contents.= $fetsaletype4[name] . ",";
        $contents.= $fpurcgst4[camt] . ",";
        $contents.= $fpurcgst4[ctax] . ",";
        $contents.= $fpurcgst4[camt] . ",";
        $contents.= $fpurcgst4[ctax] . ",";
        $contents.= $fpurigst4[iamt] . ",";
        $contents.= $fpurigst4[itax] . "\n";
    }


    $contents = strip_tags($contents);


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