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

$maindb1 = "purmain";
$trandb1 = "purtran";

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

$formname = "GSTR1_4";
$file = "gstr1_4.php";

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

    $getpmain = mysqli_query($conn, "SELECT 'Sale - GST Registered - Local' as descrip, sum(taxableamt) as nettot, sum(cgst5+cgst12+cgst18+cgst28) as cgst, 
                         sum(sgst5+sgst12+sgst18+sgst28) as sgst, sum(igst5+igst12+igst18+igst28) as igst from $maindb where vouchdt >= '$mfrmdate'
                         AND vouchdt <= '$mtodate' and party in (select accode from accmast where gstno != '')") or die(mysqli_error($conn));
    $pmain = mysqli_fetch_array($getpmain);

    $contents.="Sr No., Description, Net Total, CGST Amount, SGST Amount, IGST Amount\n";
    $ctr = 0;
    $totamt = 0;
    $totdisc = 0;
    $totbill = 0;

    $ctr++;
    $descrip = $pmain[descrip];
    $taxableamt = $pmain[nettot];
    $cgstamt = $pmain[cgst];
    $sgstamt = $pmain[sgst];
    $igstamt = $pmain[igst];

    $contents.= $ctr . ",";
    $contents.= $descrip . ",";
    $contents.= $taxableamt . ",";
    $contents.= $cgstamt . ",";
    $contents.= $sgstamt . ",";
    $contents.= $igstamt . "\n";

    $getpmain1 = mysqli_query($conn, "SELECT 'Sale - Non GST  - Local' as descrip, sum(tottaxamt) as nettot, sum(cgst5+cgst12+cgst18+cgst28) as cgst, 
                         sum(sgst5+sgst12+sgst18+sgst28) as sgst, sum(igst5+igst12+igst18+igst28) as igst from $maindb where vouchdt >= '$mfrmdate'
                         AND vouchdt <= '$mtodate' and party in (select accode from accmast where gstno = '')") or die(mysqli_error($conn));
    $pmain1 = mysqli_fetch_array($getpmain1);
    $ctr = 0;
    $totamt = 0;
    $totdisc = 0;
    $totbill = 0;

    $ctr++;
    $descrip1 = $pmain1[descrip];
    $taxableamt1 = $pmain1[nettot];
    $cgstamt1 = $pmain1[cgst];
    $sgstamt1 = $pmain1[sgst];
    $igstamt1 = $pmain1[igst];

    $contents.= $ctr . ",";
    $contents.= $descrip1 . ",";
    $contents.= $taxableamt1 . ",";
    $contents.= $cgstamt1 . ",";
    $contents.= $sgstamt1 . ",";
    $contents.= $igstamt1 . "\n";

    $getpmain2 = mysqli_query($conn, "SELECT 'Purchase - GST Registered' as descrip, sum(tottaxamt) as nettot, sum(totcgst) as cgst, 
                              sum(totsgst) as sgst, sum(totigst) as igst from $maindb1 where vouchdt >= '$mfrmdate'
                              AND vouchdt <= '$mtodate' and party in (select accode from accmast where gstno != '')") or die(mysqli_error($conn));
    $pmain2 = mysqli_fetch_array($getpmain2);
    $ctr = 0;
    $totamt = 0;
    $totdisc = 0;
    $totbill = 0;

    $ctr++;
    $descrip2 = $pmain2[descrip];
    $taxableamt2 = $pmain2[nettot];
    $cgstamt2 = $pmain2[cgst];
    $sgstamt2 = $pmain2[sgst];
    $igstamt2 = $pmain2[igst];

    $contents.= $ctr . ",";
    $contents.= $descrip2 . ",";
    $contents.= $taxableamt2 . ",";
    $contents.= $cgstamt2 . ",";
    $contents.= $sgstamt2 . ",";
    $contents.= $igstamt2 . "\n";

    $getpmain3 = mysqli_query($conn, "SELECT 'Purchase - Non GST' as descrip, sum(tottaxamt) as nettot, sum(totcgst) as cgst, 
                              sum(totsgst) as sgst, sum(totigst) as igst from $maindb1 where vouchdt >= '$mfrmdate'
                              AND vouchdt <= '$mtodate' and party in (select accode from accmast where gstno = '')") or die(mysqli_error($conn));
    $pmain3 = mysqli_fetch_array($getpmain3);
    $ctr = 0;
    $totamt = 0;
    $totdisc = 0;
    $totbill = 0;

    $ctr++;
    $descrip3 = $pmain3[descrip];
    $taxableamt3 = $pmain3[nettot];
    $cgstamt3 = $pmain3[cgst];
    $sgstamt3 = $pmain3[sgst];
    $igstamt3 = $pmain3[igst];

    $contents.= $ctr . ",";
    $contents.= $descrip3 . ",";
    $contents.= $taxableamt3 . ",";
    $contents.= $cgstamt3 . ",";
    $contents.= $sgstamt3 . ",";
    $contents.= $igstamt3 . "\n";
    
    $contents = strip_tags($contents);

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