<?php

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

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

$deexmain = "deexmain";
$deextran = "deextran";

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

    $getmain = mysqli_query($conn, "SELECT a.vouchno, a.vouchdt, a.`billamt`, b.`itemcode`, b.`hsncode`, sum(b.`qty`) as itemqty, 
                             sum(b.subtot) as tamt, sum(b.cgstamt) as tcgstamt, sum(b.sgstamt) as tsgstamt, sum(b.igstamt) as tigstamt, 
                             sum(b.`total`) as totval, b.`gstperc` FROM `$maindb` a, `$trandb` b WHERE a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' 
                             and a.vouchno = b.vouchno and a.canc_flag = '' group by b.hsncode, b.gstperc
                             UNION ALL
                             SELECT a.vouchno, a.vouchdt, a.`billamt`, b.`itemcode`, b.`hsncode`, sum(b.`qty`) as itemqty, 
                             sum(b.subtot) as tamt , sum(b.cgstamt) as tcgstamt, sum(b.sgstamt) as tsgstamt, sum(b.igstamt) as tigstamt, 
                             sum(b.`total`) as totval, b.`gstperc` FROM `$deexmain` a, `$deextran` b WHERE a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' 
                             and a.vouchno = b.vouchno and a.canc_flag = '' group by b.hsncode, b.gstperc
                             UNION ALL
                             SELECT a.vouchno, a.invdate as vouchdt, a.`billamt`, b.`itemcode`, b.`hsncode`, sum(b.`qty`) as itemqty, 
                             sum(b.amtcurr) as tamt, 0 as tcgstamt, 0 as tsgstamt, 0 as tigstamt, 
                             sum(b.amtcurr) as totval, 0 as `gstperc` FROM `salemainexp` a, `saletranexp` b WHERE a.`invdate` >= '$mfrmdate' AND a.`invdate` <= '$mtodate' 
                             and a.vouchno = b.vouchno and a.canc_flag = '' group by b.hsncode
                             UNION ALL
                             SELECT a.vouchno, a.vouchdt, a.`billamt`, b.`itemcode`, b.`hsncode`, sum(b.`qty`) as itemqty, 
                             sum(b.subtot) as tamt, sum(b.cgstamt) as tcgstamt, sum(b.sgstamt) as tsgstamt, sum(b.igstamt) as tigstamt, 
                             sum(b.`total`) as totval, b.`gstperc` FROM `slonlmain` a, `slonltran` b WHERE a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' 
                             and a.vouchno = b.vouchno and a.canc_flag = '' group by b.hsncode, b.gstperc") or die(mysqli_error($conn));

//     $getmain = mysqli_query($conn, "SELECT a.vouchno, a.vouchdt, a.`billamt`, b.`itemcode`, b.`hsncode`, sum(b.`qty`) as itemqty, 
//                             sum(b.subtot) as tamt, sum(b.cgstamt) as tcgstamt, sum(b.sgstamt) as tsgstamt, sum(b.igstamt) as tigstamt, 
//                             sum(b.`total`) as totval, b.`gstperc` FROM `slonlmain` a, `slonltran` b WHERE a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' 
//                             and a.vouchno = b.vouchno and a.canc_flag = ''  group by b.hsncode, b.gstperc") or die(mysqli_error($conn));
    
//    $getmain = mysqli_query($conn, "SELECT a.vouchno, a.vouchdt, a.`billamt`, b.`itemcode`, b.`hsncode`, sum(b.`qty`) as itemqty, 
//                             sum(b.subtot) as tamt, sum(b.cgstamt) as tcgstamt, sum(b.sgstamt) as tsgstamt, sum(b.igstamt) as tigstamt, 
//                             sum(b.`total`) 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));

    
    $get1 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and oms != 1 and canc_flag = '' ") or die(mysqli_error($conn));
    $get2 = mysqli_query($conn, "SELECT sum(postamt) as post from `deexmain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and oms != 1 and canc_flag = '' ") or die(mysqli_error($conn));
    $get3 = mysqli_query($conn, "SELECT sum(postamt) as post from `slonlmain` WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and oms != 1 and canc_flag = '' ") or die(mysqli_error($conn));

    $p1 = mysqli_fetch_array($get1);
    $p2 = mysqli_fetch_array($get2);
    $p3 = mysqli_fetch_array($get3);

    $c1 = $p1['post'];
    $c2 = $p2['post'];
    $c3 = $p3['post'];

    $cpost = $c1 + $c2 + $c3;
    $spost = $cpost / 2;

    $spgst = $spost * 0.18;

    $got1 = mysqli_query($conn, "SELECT sum(postamt) as post from `salemain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and oms = 1 and canc_flag = ''") or die(mysqli_error($conn));
    $got2 = mysqli_query($conn, "SELECT sum(postamt) as post from `deexmain`  WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and oms = 1 and canc_flag = ''") or die(mysqli_error($conn));
    $got3 = mysqli_query($conn, "SELECT sum(postamt) as post from `slonlmain` WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and oms = 1 and canc_flag = ''") or die(mysqli_error($conn));

    $o1 = mysqli_fetch_array($got1);
    $o2 = mysqli_fetch_array($got2);
    $o3 = mysqli_fetch_array($got3);

    $i1 = $o1['post'];
    $i2 = $o2['post'];
    $i3 = $o3['post'];

    $ipost = $i1 + $i2 + $i3;
    $ipgst = $ipost * 0.18;
    
    
//    browse($c1) ;
//    browse($i1) ;
//
//    browse($c2) ;
//    browse($i2) ;
//
//    browse($c3) ;
//    browse($i3) ;
    
    $contents.="HSN, Description, UQC, GST %, 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($getmain)) {

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

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


//        $contents.=$ctr . ",";
        $contents.=$hsn . ",";
        $contents.=$item . ",";
        $contents.=$uqc . ",";
        $contents.=$gstperc . ",";
        $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;
    }
    
    $totpost = round($cpost + $ipost,2) + round($ipgst,2) + round($spgst,2) + round($spgst,2) ;

    $contents.="9968" . ",";
    $contents.="COURIER" . ",";
    $contents.= "" . ",";
    $contents.= "18" . ",";
    $contents.= " " . ",";
    $contents.= $totpost . ",";
    $contents.= round($cpost + $ipost,2) . ",";
    $contents.= round($ipgst,2) . ",";
    $contents.= round($spgst,2) . ",";
    $contents.= round($spgst,2) . ",";
    $contents.=" " . "\n";

    $totinvval = $totinvval + $totpost;
    $tottaxval = $tottaxval + $cpost + $ipost;
    $ttotigst = $ttotigst + $ipgst;
    
    $ttotcgst = $ttotcgst + $spgst;
    $ttotsgst = $ttotsgst + $spgst;


    $contents.= " " . ",";
    $contents.= "Total" . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= round($totinvval,2) . ",";
    $contents.= round($tottaxval,2) . ",";
    $contents.= round($ttotigst,2)  . ",";
    $contents.= round($ttotcgst,2)  . ",";
    $contents.= round($ttotsgst,2)  . ",";
    $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;
    }
}
?>