<?php

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

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

$formname = "HSN Summary";
$file = "gstr1_hsn.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 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.vouchno = b.vouchno group by b.hsncode,b.gstperc 
//                             order by a.vouchdt, a.vouchno ")or die(mysqli_error($conn));
//    $get = mysqli_num_rows($getpmain);

    $getpmain = mysqli_query($conn, "SELECT a.vouchno, a.vouchdt, a.`netamt` as billamt, b.`itemcode`, b.`hsncode`, sum(b.`totqty`) as itemqty, 
                             sum(a.tottaxamt) as tamt, sum(a.totcgst) as tcgstamt, sum(a.totsgst) as tsgstamt, sum(a.totigst) as tigstamt, 
                             sum(a.`subtotal`) as totval, b.`gstperc` FROM `$maindb` a, `$trandb` b WHERE a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' 
                             and a.vouchno = b.vouchno group by b.hsncode, b.gstperc")or die(mysqli_error($conn));

//    $getpmain = mysqli_query($conn, "SELECT hsncode, itemname, itemqty, billamt, tamt, tcgstamt, tsgstamt, tigstamt FROM  
//                             gsthsn group by hsncode, gstperc order by vouchdt, vouchno")or die(mysqli_error($conn));

    $contents.="HSN, Description, UQC, Total Quantity, Tot Value, Taxable Value, Integrated Tax Amt, Central Tax Amt, State/UT Tax Amt, Cess Amt\n";

    $ctr = 0;
    $totamt = 0;
    $totdisc = 0;
    $totbill = 0;
    $ttotigst = 0;
    $ttotcgst = 0;
    $ttotsgst = 0;

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

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

        $getitem = mysqli_query($conn, "SELECT `name` from `item` WHERE `code`='$pmain[itemcode]'")or die(mysqli_error($conn));
        $fitem = mysqli_fetch_array($getitem);


        $ctr++;
        $hsn = $pmain['hsncode'];
        $item = $fitem['name'];
        $uqc = " ";
        $qty = $pmain['itemqty'];
        $invval = $pmain['totval'];
        $taxval = $pmain['tamt'];
        $totcgst = $pmain['tcgstamt'];
        $totsgst = $pmain['tsgstamt'];
        $totigst = $pmain['tigstamt'];
        $cess = " ";



//        $contents.=$ctr . ",";
        $contents.=$hsn . ",";
        $contents.="FABRIC" . ",";
        $contents.="MTRS." . ",";
        $contents.=$qty . ",";
        $contents.=$invval . ",";
        $contents.=$taxval . ",";
        $contents.=$totigst . ",";
        $contents.=$totcgst . ",";
        $contents.=$totsgst . ",";
//        $contents.=$totigst . ",";
        $contents.=$cess . "\n";

        $totinvval = $totinvval + $invval;
        $tottaxval = $tottaxval + $taxval;
        $ttotigst = $ttotigst + $totigst;
        $ttotcgst = $ttotcgst + $totcgst;
        $ttotsgst = $ttotsgst + $totsgst;
    }
    $contents.= " " . ",";
    $contents.= "Total" . ",";
//    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= "$totinvval" . ",";
    $contents.= "$tottaxval" . ",";
    $contents.= "$ttotigst" . ",";
    $contents.= "$ttotcgst" . ",";
    $contents.= "$ttotsgst" . ",";
    $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;
    }
}
?>