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

$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);
$mfrdate = getzdates($zyear, 'S');
$mtodate = getzdates($zyear, 'E');
mysqli_select_db($conn, $zdb);

$admin = $_SESSION["admin_" . $dbname];
$zroot = $_SESSION["zdbroot"];
$tdate = date('Y-m-d');

$ctr = 0;
$rows = array();
//if ($_REQUEST['weeklyfrmdt'] != '') {
    
                $tradesale = mysqli_query($conn, "SELECT SUM(grossamt)as amt, SUM(discamt)AS discount,SUM(taxableamt) as taxable, 
                                      SUM(totgst5+totgst12+totgst18+totgst28) AS totgst ,SUM(billamt) AS netamt, sum(postamt) as post
                                      from salemain WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and canc_flag != 'C'  ") or die(mysqli_error($conn));
                $fetlocal = mysqli_fetch_array($tradesale);
                $totlocal = mysqli_num_rows($tradesale);
                $local = $fetlocal['netamt'];
                

//                $saleexmp = mysqli_query($conn, "SELECT SUM(amount)as amt, SUM(discamt)AS discount,SUM(subtot) as taxable, 
//                                      SUM(cgstamt+sgstamt+igstamt) AS totgst ,SUM(total) AS netamt
//                                      from saletran WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and canc != 'C' and 
//                                      gstperc = 0 ") or die(mysqli_error($conn));
//                $fsaleexmp = mysqli_fetch_array($saleexmp);
//
//                $nonexmp = mysqli_query($conn, "SELECT SUM(amount)as amt, SUM(discamt)AS discount,SUM(subtot) as taxable, 
//                                      SUM(cgstamt+sgstamt+igstamt) AS totgst ,SUM(total) AS netamt
//                                      from saletran WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and canc != 'C' and 
//                                      gstperc > 0 ") or die(mysqli_error($conn));
//                $fnonexmp = mysqli_fetch_array($nonexmp);

                $deemsale = mysqli_query($conn, "SELECT SUM(grossamt)as amt,SUM(discamt)AS discount,SUM(taxableamt) as taxable, 
                                     SUM(totgst1) AS totgst ,SUM(billamt) AS netamt,  sum(postamt) as post
                                     from deexmain WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and canc_flag != 'C' ") or die(mysqli_error($conn));
                $fetdeem = mysqli_fetch_array($deemsale);
                $totdeem = mysqli_num_rows($deemsale);
                $deem = $fetdeem['netamt'];

                $exportsale = mysqli_query($conn, "SELECT SUM(grossamt * roe) as amt, SUM(taxableamt * roe) as taxable, SUM(inramt) AS netamt
                                     from salemainexp WHERE `invdate` >= '$mfrmdate' AND `invdate` <= '$mtodate' and canc_flag != 'C' ") or die(mysqli_error($conn));
                $fetexport = mysqli_fetch_array($exportsale);
                $totexpo = mysqli_num_rows($exportsale);
                $expo = $fetexport['netamt'];

                $saleother = mysqli_query($conn, "SELECT SUM(grossamt) as amt, SUM(discount) AS discount, SUM(taxableamt) as taxable,
                                      SUM(cgstamt+sgstamt+igstamt) AS totgst ,SUM(billamt) AS netamt 
                                       from slotmain WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
                $fetother = mysqli_fetch_array($saleother);
                $totother = mysqli_num_rows($saleother);
                $other = $fetother['netamt'];
                
        $rows[] = ['local' => $local,
            'deem' => $deem,
            'expo' => $expo,
            'other' => $other];
        $ctr++;                

//}

header("Content-type: application\json");
echo json_encode($rows);
