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

$database = new Database();
$db = $database->getConnection();
 
$stock_out = new StockOut($db);

$data = json_decode(file_get_contents("php://input"));
// $stock_out->stock_out_date = $data->stock_out_date;
$stock_out->df = isset($data->df)?$data->df:"";
$stock_out->dt = isset($data->dt)?$data->dt:"";
$stock_out->category = isset($data->category)?$data->category:"";
$stock_out->brand = isset($data->brand)?$data->brand:"";
$stock_out->item_code = isset($data->item_code)?$data->item_code:"";
$stock_out->item_name = isset($data->item_name)?$data->item_name:"";
$stock_out->status = isset($data->status)?$data->status:"";
$stock_out->store = isset($data->store)?$data->store:"";

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

$arr=array();
$arr["records"]=array();
$total = 0;
if($num>0){
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
        $item=array(
            "id" => (int) $id,
            "date" => $stock_out_date,
			"store" => $store,
            "stock_out_by" => $stock_out_by,
            "category" => "",
            "brand" => "",
			"item_code" => $item_code,
			"item_name" => $item_name,
            "quantity" => number_format($quantity),
            "status" => $status,
            "remark" => ($remark)?$remark:""
        );
        $total += $quantity;
        array_push($arr["records"], $item);
    }
    $arr["total"] = number_format($total);
}
echo json_encode($arr);
?>
