<?php

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

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

$maindb4 = "deexmain";
$trandb4 = "deextran";

$maindb2 = "credmain";
$trandb2 = "credtran";

$formname = "Document Summary";

$maindb3 = "salemainexp";
$trandb3 = "saletranexp";

$maindb5 = "slotmain";
$trandb5 = "slottran";

$maindb6 = "slonlmain";
$maindb6 = "slonlmain";

$file = "gstr1_docs.php";

$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");
    }

    $getpmain = mysqli_query($conn, "SELECT 'Invoices for outward supply' as descrip, min(invno) as minvouch, max(invno) as maxvouch, count(invno) as counvouch FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                             AND `vouchdt` <= '$mtodate' order by invno ")or die(mysqli_error($conn));
    $get = mysqli_fetch_array($getpmain);

    $getpmain2 = mysqli_query($conn, "SELECT 'Sales Return/ Credit Note' as descrip2, min(vouchno) as minvouch2, max(vouchno) as maxvouch2, count(vouchno) as counvouch2 FROM `$maindb2` WHERE `vouchdt` >= '$mfrmdate' 
                             AND `vouchdt` <= '$mtodate' order by invno ")or die(mysqli_error($conn));
    $get2 = mysqli_fetch_array($getpmain2);

    $getpmain3 = mysqli_query($conn, "SELECT 'Export' as descrip3, min(vouchno) as minvouch3, max(vouchno) as maxvouch3, count(vouchno) as counvouch3 FROM `$maindb3` WHERE `invdate` >= '$mfrmdate' 
                             AND `invdate` <= '$mtodate' order by vouchno")or die(mysqli_error($conn));
    $get3 = mysqli_fetch_array($getpmain3);

    $getpmain4 = mysqli_query($conn, "SELECT 'Deemed Export Sales' as descrip4, min(invno) as minvouch4, max(invno) as maxvouch4, count(invno) as counvouch4 FROM `$maindb4` WHERE `vouchdt` >= '$mfrmdate'
                             AND `vouchdt` <= '$mtodate' order by invno ")or die(mysqli_error($conn));
    $get4 = mysqli_fetch_array($getpmain4);

    $getpmain5 = mysqli_query($conn, "SELECT 'Other Sales' as descrip5, min(invno) as minvouch5, max(invno) as maxvouch5, count(invno) as counvouch5 FROM `$maindb5` WHERE `vouchdt` >= '$mfrmdate' 
                             AND `vouchdt` <= '$mtodate' order by invno ")or die(mysqli_error($conn));
    $get5 = mysqli_fetch_array($getpmain5);

    $getpmain6 = mysqli_query($conn, "SELECT 'Online Sales' as descrip6, min(invno) as minvouch6, max(invno) as maxvouch6, count(invno) as counvouch6 FROM `$maindb6` WHERE `vouchdt` >= '$mfrmdate' 
                             AND `vouchdt` <= '$mtodate' order by invno ")or die(mysqli_error($conn));
    $get6 = mysqli_fetch_array($getpmain6);
    
//    print table($getpmain);
//    $getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.*, sum(b.subtot) as tamt , sum(b.cgstamt) as tcgstamt,
//                             sum(b.sgstamt) as tsgstamt, sum(b.igstamt) as tigstamt FROM `$maindb` a, `$trandb` b WHERE a.`vouchdt` >= '$mfrmdate' 
//                             AND a.`vouchdt` <= '$mtodate' and a.party in (select accode from accmast where gstno != '') and a.vouchno = b.vouchno 
//                             group by b.vouchno,b.gstperc order by a.vouchdt, a.vouchno ")or die(mysqli_error($conn));
//    $get = mysqli_num_rows($getpmain);
//    browse($get);

    $contents .= "Nature of Doc, Sr No. From, Sr No. To, Total No., Cancelled\n";


    $contents .= $get4[descrip4] . ",";
    $contents .= 'DE' . $get4[minvouch4] . ",";
    $contents .= 'DE' . $get4[maxvouch4] . ",";
    $contents .= $get4[counvouch4] . ",";
    $contents .= "" . "\n";

    $contents .= $get[descrip] . ",";
    $contents .= 'L' . $get[minvouch] . ",";
    $contents .= 'L' . $get[maxvouch] . ",";
    $contents .= $get[counvouch] . ",";
    $contents .= "" . "\n";

    $contents .= $get3[descrip3] . ",";
    $contents .= 'EX' . $get3[minvouch3] . ",";
    $contents .= 'EX'  . $get3[maxvouch3] . ",";
    $contents .= $get3[counvouch3] . ",";
    $contents .= "" . "\n";

    $contents .= $get5[descrip5] . ",";
    $contents .= 'O' . $get5[minvouch5] . ",";
    $contents .= 'O' . $get5[maxvouch5] . ",";
    $contents .= $get5[counvouch5] . ",";
    $contents .= "" . "\n";

    $contents .= $get6[descrip6] . ",";
    $contents .= 'OL' . $get6[minvouch6] . ",";
    $contents .= 'OL' . $get6[maxvouch6] . ",";
    $contents .= $get6[counvouch6] . ",";
    $contents .= "" . "\n";

    $contents .= $get2[descrip2] . ",";
    $contents .= $get2[minvouch2] . ",";
    $contents .= $get2[maxvouch2] . ",";
    $contents .= $get2[counvouch2] . ",";
    $contents .= "" . "\n";
    
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $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;
    }
}
?>