<?php

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

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

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

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

$formname = "GSTR1_11";
$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"])) {

    $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 'Sales' 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);
//    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.= $get[descrip] . ",";
    $contents.= $get[minvouch] . ",";
    $contents.= $get[maxvouch] . ",";
    $contents.= $get[counvouch] . ",";
    $contents.= "" . "\n";

    $contents.= $get2[descrip2] . ",";
    $contents.= $get2[minvouch2] . ",";
    $contents.= $get2[maxvouch2] . ",";
    $contents.= $get2[counvouch2] . ",";
    $contents.= "" . "\n";

    $contents.= $get3[descrip3] . ",";
    $contents.= $get3[minvouch3] . ",";
    $contents.= $get3[maxvouch3] . ",";
    $contents.= $get3[counvouch3] . ",";
    $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;
    }
}
?>