<?php

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

$maindb = "salemain";
$trandb = "saletran";
$formname = "GSTR1_10";
$file = "gstr1_10.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 *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
//    browse($get);

//    $getptran = mysqli_query($conn, "SELECT hsncode, item, qty, item, amount, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$trandb` WHERE `vouchdt` >= '$mfrmdate' 
//                            AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
//    $get = mysqli_num_rows($getptran);

    $contents.="Sr No., HSN, Description, UQC, Tot Qty,Tot Value, Integrated Tax Amt, Central Tax Amt, State/UT Tax Amt, Cess Amt\n";

    $ctr = 0;
    $totamt = 0;
    $totdisc = 0;
    $totbill = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {


        $qrecpgst = mysqli_query($conn, "SELECT `gstno` from `accmast` where `accode`= '$pmain[accode]'")or die(mysqli_error($conn));
        $frecpgst = mysqli_fetch_array($qrecpgst);

//        $getcomgst = mysqli_query($conn, "SELECT `gst` from $zdir.`company` where `code` = '$compcd' ")or die(mysqli_error($conn));
//        $fetcompgst = mysqli_fetch_array($getcomgst);


//        $getrate = mysqli_query($conn, "SELECT `rate` from `$trandb'  WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
//        $trate = mysqli_fetch_array($getrate);


        $ctr++;
        $hsn = $ptran[hsncode];
        $item = $ptran[item];
        $uqc = " ";
        $qty = $ptran[qty];
        $amount = $ptran[amount];
        $taxval = $pmain[tottaxamt];
        $totcgst = $pmain[totcgst];
        $totsgst = $pmain[totsgst];
        $totigst = $pmain[totigst];
        $cess = " ";



        $contents.=$ctr . ",";
        $contents.=$hsn . ",";
        $contents.=$item . ",";
        $contents.=$uqc . ",";
        $contents.=$qty . ",";
        $contents.=$amount . ",";
        $contents.=$taxval . ",";
        $contents.=$totcgst . ",";
        $contents.=$totsgst . ",";
        $contents.=$totigst . ",";
        $contents.=$cess . "\n";

        $totinvval = $totinvval + $invval;
        $tottaxval = $tottaxval + $taxval;
    }
    $contents.= " " . ",";
    $contents.= "Total" . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= "$totinvval" . ",";
    $contents.= "$tottaxval" . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";

    
    $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;
    }
}
?>