<?php

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


$formname = "GSTR1_1";
$file = "gstr1_1.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"])) {
    
    include('includes/head_excel_rep.php');

    $para = $_POST["para"];

    if ($para == "P") {
        $maindb = "gstmain";
        $trandb = "gsttran";
    } else {
        $maindb = "rawmain";
        $trandb = "rawtran";
    }

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


    $contents .= "GST No., Receiver Name, Inv. No, Inv. Date, Inv. Value,  Place of Supply, Reverse Charge, Applicable % of Tax Rate, Inv. Type, E-Comm GST, Rate, Taxable Value, Integrated Tax, Central Tax,State/UT Tax,Cess Amount,IRN, IRN Date\n";


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


    mysqli_query($conn, "drop temporary table if exists main1") or die(mysqli_error($conn));
    mysqli_query($conn, "create temporary table main1
                               SELECT a.irn, date_format(a.uploaddate,'%d-%m-%Y') as uploaddate,a.vouchno, a.vouchdt, 
                               a.party, a.invno, a.packchgs, a.packgtperc, a.netamt as billamt, sum(b.subtot) as tamt, a.oms, 
                               b.gstperc  FROM `$maindb` a, `$trandb` b 
                               WHERE a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' and a.vouchno = b.vouchno and a.canc_flag='' 
                               and a.party in (select `accode` from accmast where `gstno` != '') 
                               group by b.vouchno, b.gstperc ") or die(mysqli_error($conn));

    $getm1 = mysqli_query($conn, "SELECT * FROM `main1` where packchgs > 0  group by vouchno order by vouchno") or die(mysqli_error($conn));

    if (mysqli_num_rows($getm1) > 0) {
        while ($p1 = mysqli_fetch_array($getm1)) {
            $vouchno = $p1['vouchno'];

            $getin = mysqli_query($conn, "SELECT * FROM `main1` where packchgs > 0 and packgtperc > 0 and vouchno = '$vouchno' ") or die(mysqli_error($conn));
            if (mysqli_num_rows($getin) <= 0) {

//                browse($vouchno) ;

                $vouchdt = $p1['vouchdt'];
                $invno = $p1['invno'];
                $billamt = $p1['billamt'];
                $post = $p1['packchgs'];
                $gstperc = $pl['packgtperc'];
                $party = $p1['party'];
                $oms = $p1['oms'];

//                $tamt = $p1['tamt'];

                mysqli_query($conn, "insert into main1 (gstperc, vouchno, vouchdt, party, invno, billamt, packchgs, tamt, oms ) values 
                                            ('$gstperc','$vouchno', '$vouchdt', '$party', '$invno', '$billamt', '$post', 0, '$oms' )") or die(mysqli_error($conn));
            }
        }
    }
    mysqli_query($conn, "update main1 set tamt = (tamt + packchgs) where gstperc > 0") or die(mysqli_error($conn));
//    mysqli_query($conn, "update main1 set packchgs = 0 where gstperc != 18") or die(mysqli_error($conn));

    $getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt, b.gstno, b.state, b.statecode, name as rcvrname
                                                FROM `main1` a, accmast b where a.party = b.accode order by a.vouchno ") or die(mysqli_error($conn));
    $ctr = 0;
    $totamt = 0;
    $totdisc = 0;
    $totbill = 0;
    $gstamt = 0;

    $tot27 = $tot00 = 0;
    $gst27 = $gst00 = 0;

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

        $ctr++;
        $gstno = $pmain['gstno'];
        $oms = $pmain['oms'];
        $state = $pmain['state'];
        $stcode = $pmain['statecode'];
        $rcvr = $pmain['rcvrname'];

        $postamt = $pmain['packchgs'];
        $invno = $pmain['invno'];
        $invdt = $pmain['mydt'];
        $invval = $pmain['billamt'];


        $revchrg = "N";

        $ecommgst = '';
        $invtype = "Regular B2B";

        $pnetamt = $pmain['billamt'];
        $rate = $pmain['gstperc'];
        $taxval = $pmain['tamt'];
        $cess = '';
        $gstamt = ($taxval * $rate) / 100;
        $tgstamt1 = $tgstamt1 + $gstamt;

        if ($oms == '1') {
            $igstamt = $gstamt;
            $cgstamt = 0;
        } else {
            $igstamt = 0;
            $cgstamt = round($gstamt / 2, 2);
        }
        $cname = str_replace(",", " " , $rcvr);

        $contents .= $gstno . ",";
        $contents .= $cname . ",";
        $contents .= $invno . ",";
        $contents .= $invdt . ",";
        $contents .= $invval . ",";
        $contents .= $stcode . '-' . $state . ",";
        $contents .= $revchrg . ",";
        $contents .= "" . ",";
        $contents .= $invtype . ",";
        $contents .= $ecommgst . ",";
        $contents .= $rate . ",";
        $contents .= $taxval . ",";
        $contents .= "$igstamt" . ",";
        $contents .= "$cgstamt" . ",";
        $contents .= "$cgstamt" . ",";
        $contents .= "" . ",";
        $contents .= "$pmain[irn]" . ",";
        $contents .= "$pmain[uploaddate]" . "\n";

        $totinvval = $totinvval + $invval;
        $tottaxval = $tottaxval + $taxval;

//        if ($stcode == 27) {
//            $tot27 + $tot27 + $taxval;
//            $gst27 + $gst27 + $gstamt;
//        } else {
//            $tot00 + $tot00 + $taxval;
//            $gst00 + $sgt00 + $gstamt;
//        }
    }

    $grtotinvval = $totinvval;
    $grtottaxval = $tottaxval;
    $grtotgst = $tgstamt1;

//    $contents .= " " . ",";
//    $contents .= " " . ",";
//    $contents .= " " . ",";
//    $contents .= "Total" . ",";
//    $contents .= $grtotinvval . ",";
//    // $contents .= " " . ",";
//    $contents .= " " . ",";
//    $contents .= " " . ",";
//    $contents .= " " . ",";
//    $contents .= " " . ",";
//    $contents .= " " . ",";
//    $contents .= " " . ",";
//    $contents .= $grtottaxval . ",";
//    $contents .= $grtotgst . ",";
//    $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;
    }
}
?>