<?php

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

$maindb = "salemain";
$trandb = "saletran";
$formname = "B2C Large (Unregistered)";
$file = "gstr1_b2bl.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");
    }

//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.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.*, sum(b.subtot) as tamt  FROM `$maindb` a, `$trandb` b WHERE a.`vouchdt` >= '$mfrmdate'
                            AND a.`vouchdt` <= '$mtodate' and `billamt` > 250000 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 .= "Sr No., Inv. No, Inv. Date, Inv. Value, Place of Supply, Rate, Taxable Value, Cess Amt,E-Comm. 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++;
        $invno = $pmain['invno'];
        $invdt = $pmain['mydt'];
        $invval = $pmain['billamt'];
        $state = $frecpgst['state'];
        $stcode = $frecpgst['statecode'];
        $gstrate = $pmain['gstperc'];

//        if ($pmain[oms] == '') {
//            $gstrate = $pmain[cgstperc];
//        } else {
//            $gstrate = $pmain[igstperc];
//        }
        $taxval = $pmain['taxableamt'];
        $cess = '';
        $ecommgst = '';


        $contents .= $ctr . ",";
        $contents .= $invno . ",";
        $contents .= $invdt . ",";
        $contents .= $invval . ",";
        $contents .= $stcode . '-' . $state . ",";
        $contents .= $gstrate . ",";
        $contents .= $taxval . ",";
        $contents .= $cess . ",";
        $contents .= $ecommgst . "\n";

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