<?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_cdnr.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.invdate, 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.invdate, 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.="GST No., Inv./Advance Payment Voucher No., Inv./Advance Payment Voucher Date.,";
    $contents.="Note/Ref Voucher No., Note/Ref Voucher Date,Pre GST,";
    $contents.="Document Type, Supply Type, Note/Refund Voucher Value, Rate,";
    $contents.="Taxable Value, Integrated Tax Paid, Central Tax Paid, State/UT Tax,";
    $contents.="Cess Paid, Eligibility for ITC, Availed ITC Integrated Tax,";
    $contents.="Availed ITC Central Tax, Availed ITC State/UT Tax, Availed ITC Cess\n";

    $ctr = 0;
    $totinvval = 0;
    $tottaxval = 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);

        $rcvrname = mysqli_query($conn, "SELECT `accode`, `name` from `accmast` where `accode`= '$pmain[party]' ")or die(mysqli_error($conn));
        $rcvr = mysqli_fetch_array($rcvrname);

        $ctr++;
        if ($pmain[credtype] == 'G') {
            $credtype = "Goods Return";
        } else {
            $credtype = "Other / Corrrection in Invoice";
        }
        if ($pmain[oms] == '') {
            $supptype = "Inter State";
        } else {
            $supptype = "Outer State";
        }

        $contents.= $ctr . ",";
        $contents.= $frecpgst[gstno] . ",";
        $contents.= $pmain[invno] . ",";
        $contents.= $pmain[invdate] . ",";
        $contents.= $pmain[vouchno] . ",";
        $contents.= $pmain[mydt] . ",";
        $contents.= 'N' . ",";
        $contents.= $pmain[doctype] . ",";
        $contents.= $supptype . ",";
        $contents.= $pmain[billamt] . ",";
        $contents.= $pmain[gstperc] . ",";
        $contents.= $pmain[taxableamt] . ",";
        $contents.= $pmain[igstamt] . ",";
        $contents.= $pmain[cgstamt] . ",";
        $contents.= $pmain[cgstamt] . ",";
        $contents.= '' . ",";
        $contents.= '' . ",";
        $contents.= '' . ",";
        $contents.= '' . ",";
        $contents.= '' . "\n";

        $totinvval = $totinvval + $pmain[billamt];
        $tottaxval = $tottaxval + $pmain[taxableamt];
        $totigst = $totigst + $pmain[igstamt];
        $totcgst = $totcgst + $pmain[cgstamt];
    }

    $contents.= " " . ",";
    $contents.= "Total" . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= $totinvval . ",";
    $contents.= " " . ",";
    $contents.= "$tottaxval" . ",";
    $contents.= $totigst . ",";
    $contents.= $totcgst . ",";
    $contents.= $totcgst . ",";
    $contents.= "" . ",";
    $contents.= "" . ",";
    $contents.= "" . ",";
    $contents.= "" . ",";
    $contents.= " " . "\n";
//            $state = $frecpgst[state];
//        $stcode = $frecpgst[statecode];
//        $invval = $pmain[billamt];
//        $taxval = $pmain[taxableamt];
//        $cess = '';
//        $pregst = 'N';
//            $gstno = $frecpgst[gstno];
//        $invno = $pmain[invno];
//        $invdt = $pmain[invdate];
//        $vouchno = $pmain[vouchno];
//        $vouchdt = $pmain[mydt];
//        $doctype = $pmain[doctype];
//        $gstrate = $pmain[gstperc];
    //        if ($pmain[pregst] == ''){
//            $pregst = "N";
//        }else {
//            $pregst = "Y";
//        }
//
    $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;
//    }
}
?>