<?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'] != '') {
for ($i = 1; $i <= 12; $i++) {

    if ($i < 10) {
        $j = $i + 3;
    } else {
        $j = $i - 9;
    }

    $monthName = date("F", strtotime('00-'.$j.'-01'));


    $zubysale = mysqli_query($conn, "SELECT  SUM(netamt) as snetamt from salemain WHERE month(`vouchdt`) = '$j' and series = 'ZB1'") or die(mysqli_error($conn));
//    print table($tradesale);
//    die();
    $fetzuby = mysqli_fetch_array($zubysale);
    $totslzb1 = mysqli_num_rows($fetzuby);
    $zuby = $fetzuby['snetamt'];


    $zubyxsale = mysqli_query($conn, "SELECT  SUM(netamt) as snetamt from salemain WHERE month(`vouchdt`) = '$j' and series = 'ZBX'") or die(mysqli_error($conn));
    $fetzubyx = mysqli_fetch_array($zubyxsale);
    $totslzbx = mysqli_num_rows($fetzubyx);
    $zubyx = $fetzubyx['snetamt'];
    
    

    $strsale = mysqli_query($conn, "SELECT  SUM(netamt) as snetamt from salemain WHERE month(`vouchdt`) = '$j' and series = 'STR' ") or die(mysqli_error($conn));
    $fetstr = mysqli_fetch_array($strsale);
    $totslstr = mysqli_num_rows($fetstr);
    $storeroom = $fetstr['snetamt'];

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

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

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

//    if($other == ''){
//        $other = 0;
//    }

    $rows[] = ['month' => $monthName,
        'zuby' => $zuby,
        'zubyx' => $zubyx,
        'storeroom' => $storeroom];
        
    $ctr++;
}


//}

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