<?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');

$month = $_REQUEST['month'];

$ctr = 0;
$rows = array();
if ($_REQUEST['month'] != '') {

    mysqli_query($conn, "create temporary table sal_ret_data SELECT sum(abs(qty)) as iqty, barcode from transacn WHERE month(`vouchdt`) = '$month'  
                                      and (source = 'SL' or source = 'SLDE' or source = 'SLEX' or source = 'SLOT'  or source = 'SR') group by barcode ") or die(mysqli_error($conn));
    
    $gettopitem = mysqli_query($conn, "SELECT sum(a.iqty) as sqty, a.barcode, b.`name` as `itemname` FROM `sal_ret_data` a, `product` b 
                                              WHERE a.barcode = b.barcode group by b.`barcode` order by sum(a.iqty) asc LIMIT 0,10") or die(mysqli_error($conn));
    
    while ($ftopitem = mysqli_fetch_array($gettopitem)) {
        $itemgrp = $ftopitem['barcode'] . ' - ' . $ftopitem['itemname'];;
        $qty = $ftopitem['sqty'];

        $rows[] = ['itemgrp' => $itemgrp,
            'qty' => $qty,];
        $ctr++;
    }
}

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