<?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_in.php';

$database = new Database();
$db = $database->getConnection();
 
$stock_in = new StockIn($db);

$data = json_decode(file_get_contents("php://input"));
$stock_in->stock_in_date = isset($data->stock_in_date)?$data->stock_in_date:"";
$stock_in->store = $data->store;
$stock_in->df = $data->df;
$stock_in->dt = $data->dt;
$stock_in->supplier = $data->supplier;
$stock_in->item_code = $data->item_code;
$stock_in->item_name = $data->item_name;

$stmt = $stock_in->search();
$num = $stmt->rowCount();

$arr=array();
$arr["records"]=array();
if($num>0){

    $total_amount = $total_qty=0;
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
        $item=array(
            "id" => (int) $id,
            "date" => $stock_in_date,
			"store" => $store,
            "supplier" => $supplier,
			"item_code" => $item_code,
			"item_name" => $item_name,
            "remark" => ($remark)?$remark:"",
            "quantity" => number_format($quantity),
            "price" => number_format($price),
            "amount" => number_format($quantity * $price)
        );
        $total_qty += $quantity;
        $total_amount += $quantity * $price;
        array_push($arr["records"], $item);
    }
    $arr["total_qty"] = number_format($total_qty);
    $arr["total_amount"] = number_format($total_amount);
}
echo json_encode($arr);
?>