<?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";

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

$purmain = "purmain";

$formname = "GSTR-3B";
$file = "gstr3b.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 'Outward Taxable' as descrip, b.gstperc, sum(b.subtot) as tamt, sum(b.cgstamt) as cgst, 
                            sum(b.igstamt) as igst FROM `$maindb` a, `$trandb` 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));
    $pmain = mysqli_fetch_array($getpmain);

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

    $ctr++;
    $descrip = $pmain['descrip'];
    $taxableamt = $pmain['tamt'];
    $cgstamt = $pmain['cgst'];
    $sgstamt = $pmain['cgst'];
    $igstamt = $pmain['igst'];

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

    $getpmain1  = mysqli_query($conn, "SELECT 'Outward Taxable (Zero Rated)' as descrip, b.gstperc, sum(b.subtot) as tamt,  
                              sum(b.cgstamt) as cgst, sum(b.igstamt) as igst FROM `$maindb` a, `$trandb` b WHERE a.`vouchdt` >= '$mfrmdate'  
                              AND a.`vouchdt` <= '$mtodate'  and a.vouchno = b.vouchno and b.gstperc > 0 and b.gstperc < 1 and 
                              a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $pmain1 = mysqli_fetch_array($getpmain1);

    $ctr++;
    $descrip1 = $pmain1['descrip'];
    $taxableamt = $pmain1['tamt'];
    $cgstamt1 = $pmain1['cgst'];
    $sgstamt1 = $pmain1['cgst'];
    $igstamt1 = $pmain1['igst'];

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

    $getpmain2  = mysqli_query($conn, "SELECT 'Other Outward Supplies (Nil rated and Exempted)' as descrip, b.gstperc, sum(b.subtot) as tamt  
                              FROM `$maindb` a, `$trandb` 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));
    
    $pmain2 = mysqli_fetch_array($getpmain2);

    $ctr++;
    $descrip2 = $pmain2[descrip];
    $taxableamt2 = $pmain2['tamt'];
    $cgstamt2 = 0;
    $sgstamt2 = 0;
    $igstamt2 = 0;

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

    //  inward supply liabel to reverse charge
    
    $descrip3 = 'Inward Supply Liabel To Reverse Charge' ;
    $taxableamt3 = 0;
    $cgstamt3 = 0;
    $sgstamt3 = 0;
    $igstamt3 = 0;
            
    $contents.= $ctr . ",";
    $contents.= $descrip3 . ",";
    $contents.= $taxableamt3 . ",";
    $contents.= $igstamt3 . ",";
    $contents.= $cgstamt3 . ",";
    $contents.= $sgstamt3 . ",";
    $contents.= "" . "\n";
    
    
    $getpmain3  = mysqli_query($conn, "SELECT 'Non GST Outward Supplies' as descrip, b.gstperc, sum(b.subtot) as tamt  
                              FROM `$maindb` a, `$trandb` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate'  
                              and a.vouchno = b.vouchno and a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    
    $pmain3 = mysqli_fetch_array($getpmain3);

    $ctr++;
    $descrip4 = $pmain3[descrip];
    $taxableamt4 = $pmain3['tamt'];
    $cgstamt4 = 0;
    $sgstamt4 = 0;
    $igstamt4 = 0;

    $contents.= $ctr . ",";
    $contents.= $descrip4 . ",";
    $contents.= $taxableamt4 . ",";
    $contents.= $igstamt4 . ",";
    $contents.= $cgstamt4 . ",";
    $contents.= $sgstamt4 . ",";
    $contents.= "" . "\n";

    $contents = strip_tags($contents);

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