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

$database = new Database();
$db = $database->getConnection();
 
$stock_transfer = new StockTransfer($db);

$data = json_decode(file_get_contents("php://input"));
$stock_transfer->stock_transfer_date = isset($data->stock_transfer_date)?$data->stock_transfer_date:"";
$stock_transfer->df = isset($data->df)?$data->df:"";
$stock_transfer->dt = isset($data->dt)?$data->dt:"";
$stock_transfer->store_from = isset($data->store_from)?$data->store_from:"";
$stock_transfer->store_to = isset($data->store_to)?$data->store_to:"";
$stock_transfer->item_code = isset($data->item_code)?$data->item_code:"";
$stock_transfer->item_name = isset($data->item_name)?$data->item_name:"";


$stmt = $stock_transfer->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,
            "stock_transfer_date" => $stock_transfer_date,
            "store_from" => $store_from,
			"store_to" => $store_to,
            "stock_transfer_by" => $stock_transfer_by,
            "item_code" => $item_code,
            "item_name" => $item_name,
            "quantity" => number_format($quantity),
            "remark" => ($remark)?$remark:""
        );
        $total += $quantity ;
        array_push($arr["records"], $item);
    }
    $arr["total"] = number_format($total);
}
echo json_encode($arr);
?>
