<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

include_once '../../config/database.php';
include_once '../../objects/stock_balance.php';

$database = new Database();
$db = $database->getConnection();
 
$stock_balance = new StockBalance($db);
$data = json_decode(file_get_contents("php://input"));
$stock_balance->stock_balance_date = $data->stock_balance_date;
$stock_balance->store = $data->store;
$stock_balance->item_code = $data->item_code;
$stock_balance->item_name = $data->item_name;

$stock_balance->chk_qty = $data->chk_qty;
if($data->chk_qty==1){
    $stock_balance->qtyfrom = $data->qtyfrom;
    $stock_balance->qtyto = $data->qtyto;
    $stock_balance->sign = $data->sign;
}
$stmt = $stock_balance->search();

$num = $stmt->rowCount();
$arr=array();
$arr["records"]=array();
$total_qty = 0;
if($num>0){
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
        $detail=array(
            "stock_balance_date" => isset($stock_balance_date)?$stock_balance_date:"",
            "store" => $store,
            "item_code" => $item_code,
            "item_name" => $item_name,
            "minimum_stock_level" => (int)$min_stock_level,
            "quantity" => (int)$quantity
        );
        $total_qty += $quantity;
        array_push($arr["records"], $detail);
    }   
    $arr["total_qty"] = number_format($total_qty);
}
echo json_encode($arr);
?>