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

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

$getseries = mysqli_query($conn, "SELECT `code`, `name` FROM `cepheisy_pki_root`.`series` WHERE `code` !=''") or die(mysqli_error($conn));
// print table($getseries);
$ctr = 0;
$rows = array();
if ($_REQUEST['weeklyfrmdt'] != '') {
    $weeklyfrmdt = $_REQUEST['weeklyfrmdt'];
    $weeklytodt = $_REQUEST['weeklytodt'];
    $month = $_REQUEST['month'];
    $todaydate = $_REQUEST['todaydate'];
    // echo $todaydate;
    // die();
    if ($month != '') {
        $vdatecond = "and month(`vouchdt`)='$month'";
    }
    // else if ($todaydate !='') {
    //     $vdatecond = "and vouchdt='$todaydate'";
    // }
    else {
        $vdatecond = "and `vouchdt`>='$weeklyfrmdt' and `vouchdt`<='$weeklytodt'";
    }

    while ($fsereis = mysqli_fetch_array($getseries)) {
        $series = $fsereis['code'];
        $seriesname = $fsereis['name'];
        // echo $vdatecond;
        // die();

        $getsale = mysqli_query($conn, "SELECT SUM(netamt) as snetamt from salemain  where series='$series' $vdatecond ") or die(mysqli_error($conn));
        $sale = mysqli_fetch_array($getsale);
        $saleamt = $sale['snetamt'];

        if($saleamt == ''){
            $saleamt = 0;
        }

        $rows[] = ['series' => $series,
            'seriescode' => $seriesname,
            'saleamt' => $saleamt,];
        $ctr++;
    }
}

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