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

$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"])) {

    $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));
//    browse($get);

    $contents.="Sr No., UR Type, Note/Ref Vouch No., Note/Ref Vouch Date, Doc Type, Inv. No., Inv. Date., Reason, Place of Supply, Ref. Value, Rate, Taxable Value, Cess Amt, Pre GST\n";
    while ($pmain = mysqli_fetch_array($getpmain)) {
        $ctr++;
        $urtype = '';
        $vouchno = $pmain[vouchno];
        $vouchdt = $pmain[vouchdt];
        $type = $pmain[doctype];
        $invno = $pmain[invno];
        $invdt = $pmain[" "];
        $doctype = 'C';
        $gstrate = $pmain[gstperc];
        if ($pmain[credtype] == 'G') {
            $credtype = "Goods Return";
        } else {
            $credtype = "Other / Corrrection in Invoice";
        }
        $state = $pmain[state];
        $stcode = $pmain[statecode];
        $invval = $pmain[billamt];
        $rate = $gstrate;
        $taxval = $pmain[tamt];
        $cess = '';
        $pregst = 'N';

        $contents.= $ctr . ",";
        $contents.= $urtype . ",";
        $contents.= $vouchno . ",";
        $contents.= $vouchdt . ",";
        $contents.= $type . ",";
        $contents.= $invno . ",";
        $contents.= $invdt . ",";
        $contents.= $credtype . ",";
        $contents.= $stcode . ",";
        $contents.= $invval . ",";
        $contents.= $rate . ",";
        $contents.= $taxval . ",";
        $contents.= $cess . ",";
        $contents.= $pregst . "\n";

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

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