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

$maindb = "credmain";
$trandb = "credtran";
$othdb = "credoth";

$maindb1 = "debmain";
$trandb1 = "debtran";
$othdb1 = "deboth";

$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 a.invno, a.vouchno, a.vouchdt, a.billamt, a.taxableamt, 'C' as doctype, DATE_FORMAT(a.vouchdt, '%d-%m-%Y') as mydt, b.gstperc FROM `$maindb` a, `$trandb` b
                             WHERE a.vouchno = b.vouchno AND a.vouchdt >= '$mfrmdate' AND a.vouchdt <= '$mtodate' 
                             AND a.party in (select accode from accmast where gstno != '') group by a.vouchno, b.gstperc order by a.vouchdt, a.vouchno)
                             union ALL
                             (SELECT a.invno, a.vouchno, a.vouchdt, a.billamt, a.taxableamt, 'D' as doctype, DATE_FORMAT(a.vouchdt, '%d-%m-%Y') as mydt, b.gstperc FROM `$maindb1` a, `$trandb1` b
                             WHERE a.vouchno = b.vouchno AND a.vouchdt >= '$mfrmdate' AND a.vouchdt <= '$mtodate' 
                             AND a.party in (select accode from accmast where gstno != '') group by a.vouchno, b.gstperc  order by a.vouchdt, a.vouchno)") or die(mysqli_error($conn));

    $contents.="Sr No., GST No., Inv./Advance Receipt No., Inv./Advance Receipt Date., Note/Ref Vouch No., Note/Ref Vouch Date,Doc Type,Reason for Issuing Doc, Place of Supply, Note/Ref Vouch Value, Rate, Taxable Value, Cess Amt,Pre GST\n";

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

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $qrecpgst = mysqli_query($conn, "SELECT `gstno`, `state`, `statecode` from `accmast` where `accode`= '$pmain[party]' ")or die(mysqli_error($conn));
        $frecpgst = mysqli_fetch_array($qrecpgst);

        $ctr++;
        $gstno = $frecpgst[gstno];
        $invno = $pmain[invno];
        $invdt = $pmain[" "];
        $vouchno = $pmain[vouchno];
        $vouchdt = $pmain[vouchdt];
        $doctype = $pmain[doctype];
        $gstrate = $pmain[gstperc];
        if ($pmain[credtype] == 'G') {
            $credtype = "Goods Return";
        } else {
            $credtype = "Other / Corrrection in Invoice";
        }
        $state = $frecpgst[state];
        $stcode = $frecpgst[statecode];
        $invval = $pmain[billamt];
        $taxval = $pmain[taxableamt];
        $cess = '';
        $pregst = 'N';


        $contents.= $ctr . ",";
        $contents.= $gstno . ",";
        $contents.= $invno . ",";
        $contents.= $invdt . ",";
        $contents.= $vouchno . ",";
        $contents.= $vouchdt . ",";
        $contents.= $doctype . ",";
        $contents.= $credtype . ",";
        $contents.= $stcode . '-' . $state . ",";
        $contents.= $invval . ",";
        $contents.= $gstrate . ",";
        $contents.= $taxval . ",";
        $contents.= $cess . ",";
        $contents.= $pregst . "\n";

        $totinvval = $totinvval + $invval;
        $tottaxval = $tottaxval + $taxval;
    }

    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= "Total" . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= $totinvval . ",";
    $contents.= " " . ",";
    $contents.= "$tottaxval" . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $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;
//    }
}
?>