<?php

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

$maindb = "purmain";
$trandb = "purtran";
$formname = "Purchase GST Report";
$file = "pugstrep.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");
    }
    $contents.=" , , , , , BENHUR PURCHASE, , , , , ,\n";

    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= "From" . ",";
    $contents.= $mfrmdate . ",";
    $contents.= "To" . ",";
    $contents.= $mtodate . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . "\n";

    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` a
                             WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'  
                             group by vouchno order by vouchdt, vouchno ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);

    $contents.="Sr No., Customer Name, Bill Date, Location, Invoice No., GSTIN., Before GST, Rate, IGST, CGST, SGST, Total Amount\n";

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

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $qrecpgst = mysqli_query($conn, "SELECT `name`, `gstno`, `state`, `statecode` from `accmast` where `accode`= '$pmain[party]' ")or die(mysqli_error($conn));
        $frecpgst = mysqli_fetch_array($qrecpgst);

        $ctr++;
        $name = $frecpgst[name];
        $invdt = $pmain[vouchdt];
        $stcode = $frecpgst[statecode];
        $state = $frecpgst[state];
        $invno = $pmain[vouchno];
        $gstno = $frecpgst[gstno];
        $taxval = $pmain[tottaxamt];
        if ($pmain[oms] == "N") {
            $rate = $pmain[cgstperc*2];
        } else {
            $rate = $pmain[igstperc];
        }
        $igst = $pmain[totigst];
        $cgst = $pmain[totcgst];
        $sgst = $pmain[totsgst];
        $invval = $pmain[netamt];
        $totalamount = $pmain[netamt];
        
        
        $contents.= $ctr . ",";
        $contents.= $name . ",";
        $contents.= $invdt . ",";
        $contents.= $stcode . '-' . $state . ",";
        $contents.= $invno . ",";
        $contents.= $gstno . ",";
        $contents.= $taxval . ",";
        $contents.= $rate . ",";
        $contents.= $igst . ",";
        $contents.= $cgst . ",";
        $contents.= $sgst . ",";
        $contents.= $totalamount . "\n";

        $tottaxval = $tottaxval + $taxval;
        $totcgst = $totcgst + $cgst;
        $totsgst = $totsgst + $sgst;
        $totigst = $totigst + $igst;
        $totamt = $totamt + $totalamount;
    }

    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= "Total" . ",";
    $contents.= " " . ",";
    $contents.= " " . ",";
    $contents.= $tottaxval . ",";
    $contents.= " " . ",";
    $contents.= $totigst . ",";
    $contents.= $totcgst . ",";
    $contents.= $totsgst . ",";
    $contents.= $totamt . "\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;
    }
}
?>