<?php

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

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

$maindb2 = "slonlmain";
$trandb2 = "slonltran";

//$formname = "GSTR1_9";
$file = "gstr1_exemp.php";
$formname = "Nil / Exempted / Non GST";

$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"]) || isset($_GET["excel"])) {
    if (isset($_GET['fdt'])) {
        $mfrmdate = trim($_GET["fdt"]);
    } else {
        $mfrmdate = trim($_POST["fdate"]);
    }

    if (isset($_GET['tdt'])) {
        $mtodate = trim($_GET["tdt"]);
    } else {
        $mtodate = trim($_POST["tdate"]);
    }

    if (empty($mfrmdate)) {
        browse("From Date Cannot Be Empty");
    }
    if (empty($mtodate)) {
        browse("To Date Cannot Be Empty");
    }
    
    $getp1 = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.*, 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.oms = 1 and 
                                     a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fet1 = mysqli_fetch_array($getp1) ;

    $getpo1 = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.*, sum(b.subtot) as tamt 
                                     FROM `$maindb2` a, `$trandb2` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate' 
                                     and a.vouchno = b.vouchno and b.gstperc = 0 and a.oms = 1 and 
                                     a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $feto1 = mysqli_fetch_array($getpo1) ;

    $amt1 = $fet1['tamt'] + $feto1['tamt'];


    $getp2 = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.*, 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.oms != 1 and 
                                     a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $fet2 = mysqli_fetch_array($getp2) ;

    $getpo2 = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.*, sum(b.subtot) as tamt 
                                     FROM `$maindb2` a, `$trandb2` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate' 
                                     and a.vouchno = b.vouchno and b.gstperc = 0 and a.oms != 1 and 
                                     a.party in (select accode from accmast where gstno != '') ") or die(mysqli_error($conn));
    $feto2 = mysqli_fetch_array($getpo2) ;
    $amt2 = $fet2['tamt'] + $feto2['tamt'];

    
    $getp3 = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.*, 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.oms = 1 and 
                                     a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fet3 = mysqli_fetch_array($getp3) ;

    $getpo3 = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.*, sum(b.subtot) as tamt 
                                     FROM `$maindb2` a, `$trandb2` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate' 
                                     and a.vouchno = b.vouchno and b.gstperc = 0 and a.oms = 1 and 
                                     a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $feto3 = mysqli_fetch_array($getpo3) ;
    $amt3 = $fet3['tamt'] + $feto3['tamt'] ;

    $getp4 = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.*, 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.oms != 1 and 
                                     a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $fet4 = mysqli_fetch_array($getp4) ;

    $getpo4 = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.*, sum(b.subtot) as tamt 
                                     FROM `$maindb2` a, `$trandb2` b WHERE a.`vouchdt` >= '$mfrmdate'  AND a.`vouchdt` <= '$mtodate' 
                                     and a.vouchno = b.vouchno and b.gstperc = 0 and a.oms != 1 and 
                                     a.party in (select accode from accmast where gstno = '') ") or die(mysqli_error($conn));
    $feto4 = mysqli_fetch_array($getpo4) ;
    $amt4 = $fet4['tamt'] + $feto4['tamt'];

   
    $contents.="Sr No., Description, Nil Rated, Exempted (Other Than Nil, Non-GST\n";
    $contents.="      ,            , Supplies , rated/Non-GST Supplies,  Supplies\n";
    
    $desc1 = 'Inter-State supplies to registered persons' ;
    $desc2 = 'Intra-State supplies to registered persons' ;
    $desc3 = 'Inter-State supplies to unregistered persons' ;
    $desc4 = 'Inter-State supplies to unregistered persons' ;

    $ctr = 0;
    $totamt = 0;
    $totdisc = 0;
    $totbill = 0;

    $ctr++ ;   
    $contents.= $ctr . ",";
    $contents.= $desc1 . ",";
    $contents.= " " . ",";
    $contents.= $amt1 . ",";
    $contents.= " " . "\n";

    $ctr++ ;   
    $contents.= $ctr . ",";
    $contents.= $desc2 . ",";
    $contents.= " " . ",";
    $contents.= $amt2 . ",";
    $contents.= " " . "\n";
    
    $ctr++ ;   
    $contents.= $ctr . ",";
    $contents.= $desc3 . ",";
    $contents.= " " . ",";
    $contents.= $amt3 . ",";
    $contents.= " " . "\n";

    $ctr++ ;   
    $contents.= $ctr . ",";
    $contents.= $desc4 . ",";
    $contents.= " " . ",";
    $contents.= $amt4 . ",";
    $contents.= " " . "\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;
    }
}
?>