<?php

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

$maindb = "salemainexp";
$trandb = "saletranexp";
$formname = "Export";
$file = "gstr1_exp.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.`invdate`, '%d-%m-%Y') as mydt,  DATE_FORMAT(a.`orddate`, '%d-%m-%Y') as orddate,
                             b.vouchno FROM `$maindb` a, `$trandb` b WHERE a.`invdate` >= '$mfrmdate' AND a.`invdate` <= '$mtodate' and 
                             a.vouchno =  b.vouchno group by b.vouchno order by a.vouchno ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
//    browse($get);

    $contents.="Export Type, Inv. No, Inv. Date, Inv. Value, Port Code, Ship Billing No.,Ship Billing Date, Rate, Taxable Value\n";

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

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

        $ctr++;

        if ($pmain['exptype'] == 'de') {
            $export = "WPAY";
        } else {
            $export = "WOPAY";
        }
        $invno = $pmain['invoice'];
        $invdt = $pmain['invdate'];
        $invval = $pmain['inramt'];
        $ptcode = $pmain['portcode'];
        $sbno = $pmain['sbno'];
        $sbdt = $pmain['sbdate'];
        $gstrate = $pmain['igstrate'];
//        $rate =  ;
        $taxval = $pmain['taxableamt'] * $pmain['roe'];


        $contents.= $export . ",";
        $contents.= $invno . ",";
        $contents.= $invdt . ",";
        $contents.= $invval . ",";
        $contents.= $ptcode . ",";
        $contents.= $sbno . ",";
        $contents.= $sbdt . ",";
        $contents.= $gstrate . ",";
        $contents.= $taxval . "\n";

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

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