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

$maindb = "credmain";
$trandb = "credtran";
$othdb = "credoth";

$maindb1 = "debmain";
$trandb1 = "debtran";
$othdb1 = "deboth";

$formname = "Credit / Debit Note for URD";

$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.invno, a.invdate,a.vouchno,a.vouchdt, a.billamt, sum(b.subtot) as tamt, a.taxableamt, 'C' as doctype, DATE_FORMAT(a.vouchdt, '%d-%m-%Y') as mydt, b.gstperc FROM `$maindb` a, `$trandb` b
//                              WHERE a.vouchno = b.vouchno AND a.vouchdt >= '$mfrmdate' AND a.vouchdt <= '$mtodate' 
//                              AND a.party in (select accode from accmast where gstno != '') group by a.vouchno, b.gstperc order by a.vouchdt, a.vouchno)
//                              union ALL
//                              (SELECT a.invno, a.vouchno, a.invdate, a.vouchdt, a.billamt, sum(b.subtot) as tamt, a.taxableamt, 'D' as doctype, DATE_FORMAT(a.vouchdt, '%d-%m-%Y') as mydt, b.gstperc FROM `$maindb1` a, `$trandb1` b
//                              WHERE a.vouchno = b.vouchno AND a.vouchdt >= '$mfrmdate' AND a.vouchdt <= '$mtodate' 
//                              AND a.party in (select accode from accmast where gstno != '') group by a.vouchno, b.gstperc  order by a.vouchdt, a.vouchno)") or die(mysqli_error($conn));

    $getpmain = mysqli_query($conn, " (SELECT a.invno, a.invdate,a.vouchno,a.vouchdt, a.billamt, sum(b.subtot) as tamt, a.taxableamt, 'C' as doctype, DATE_FORMAT(a.vouchdt, '%d-%m-%Y') as mydt, b.gstperc FROM `$maindb` a, `$trandb` b
                              WHERE a.vouchno = b.vouchno AND a.vouchdt >= '$mfrmdate' AND a.vouchdt <= '$mtodate' 
                              AND a.party in (select accode from accmast where gstno = '') 
                              group by a.vouchno, b.gstperc order by a.vouchdt, a.vouchno)") or die(mysqli_error($conn));

    $getpmain2 = mysqli_query($conn, "SELECT a.invno,a.invdate, a.vouchno, a.vouchdt, a.party, a.billamt, a.taxableamt, 'C' as doctype, DATE_FORMAT(a.vouchdt, '%d-%m-%Y') as mydt,
                              a.`gsttype`, b.amount as othamt FROM `$maindb` a, `$othdb` b WHERE a.vouchno = b.vouchno AND a.vouchdt >= '$mfrmdate' AND a.vouchdt <= '$mtodate' 
                              AND a.party in (select accode from accmast where gstno = '') 
                              group by a.vouchno order by a.vouchdt, a.vouchno") or die(mysqli_error($conn));


    $contents.="UR Type, Note/Refund Voucher No., Note/Refund Voucher Date,  Invoice/Advance Receipt Number, Invoice/Advance Receipt Date,Revised Note/Refund Voucher No., Revised Note/Refund Voucher Date,Document Type, Supply Type, Refund Vouhcer Value ,Applicable % of Tax Rate, Rate, Taxable Value, Cess Amt, Pre GST\n";
    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);

        $rcvrname = mysqli_query($conn, "SELECT `accode`, `name` from `accmast` where `accode`= '$pmain[party]' ")or die(mysqli_error($conn));
        $rcvr = mysqli_fetch_array($rcvrname);


        $ctr++;
        $urtype = '';
        $vouchno = $pmain[vouchno];
        $vouchdt = $pmain[vouchdt];
        $type = $pmain[doctype];
        $invno = $pmain[invno];
        $invdt = $pmain[invdate];
        $doctype = 'C';
        $gstrate = $pmain[gstperc];
        if ($pmain[credtype] == 'G') {
            $credtype = "Goods Return";
        } else {
            $credtype = "Other / Corrrection in Invoice";
        }
        
        if ($pmain[oms] == '1') {
            $oms = "Intra State";
        } else {
            $oms = "Inter State";
        }
        $state = $frecpgst[state];
        $stcode = $frecpgst[statecode];
        $invval = $pmain[billamt];
        $rate = $gstrate;
        $taxval = $pmain[tamt];
        $cess = '';
        $pregst = 'N';

//        $contents.= $ctr . ",";
        $contents.= $urtype . ",";
        $contents.= $vouchno . ",";
        $contents.= $vouchdt . ",";
        $contents.= $invno . ",";
        $contents.= $invdt . ",";
        $contents.= $vouchno . ",";
        $contents.= $vouchdt . ",";
        $contents.= $type . ",";
        $contents.= $oms . ",";

//        $contents.= $credtype . ",";
//        $contents.= $stcode . ",";
        $contents.= $invval . ",";
        $contents.= "" . ",";
        $contents.= $rate . ",";
        $contents.= $taxval . ",";
        $contents.= $cess . ",";
        $contents.= $pregst . "\n";

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

    while ($pmain2 = mysqli_fetch_array($getpmain2)) {

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

        $rcvrname = mysqli_query($conn, "SELECT `accode`, `name` from `accmast` where `accode`= '$pmain2[party]' ")or die(mysqli_error($conn));
        $rcvr = mysqli_fetch_array($rcvrname);


        $ctr++;
        $urtype = '';
        $vouchno = $pmain2[vouchno];
        $vouchdt = $pmain2[vouchdt];
        $type = $pmain2[doctype];
        $invno = $pmain2[invno];
        $invdt = $pmain2[invdate];
        $doctype = 'C';
        $gstrate = $pmain2[gstperc];
        if ($pmain2[credtype] == 'G') {
            $credtype = "Goods Return";
        } else {
            $credtype = "Other / Corrrection in Invoice";
        }
        
        if ($pmain2[oms] == '1') {
            $oms = "Intra State";
        } else {
            $oms = "Inter State";
        }
        $state = $frecpgst[state];
        $stcode = $frecpgst[statecode];
        $invval = $pmain2[billamt];
        $rate = $gstrate;
        $taxval = $pmain2[tamt];
        $cess = '';
        $pregst = 'N';

//        $contents.= $ctr . ",";
        $contents.= $urtype . ",";
        $contents.= $vouchno . ",";
        $contents.= $vouchdt . ",";
        $contents.= $invno . ",";
        $contents.= $invdt . ",";
        $contents.= $vouchno . ",";
        $contents.= $vouchdt . ",";
        $contents.= $type . ",";
        $contents.= $oms . ",";

//        $contents.= $credtype . ",";
//        $contents.= $stcode . ",";
        $contents.= $invval . ",";
        $contents.= "" . ",";
        $contents.= $rate . ",";
        $contents.= $taxval . ",";
        $contents.= $cess . ",";
        $contents.= $pregst . "\n";

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

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