<?php
header('content-type: application/json');
header("access-control-allow-origin: *");
require_once(dirname(__FILE__)."/../packages/require_api.php");

if(isset($_GET['action'])){

	require_once($global['root-model']."Crud.php");
	$obj_crud = new Crud();

	require_once($global['root-model']."Product.php");
	$obj_product = new Product();

	//===================================== Get Data ========================================
	if($_GET['action'] == 'get_report_inventory'){

		$R_message = array("code" => "400", "message" => "No Data");

		$N_page = isset($_REQUEST['page']) ? $_REQUEST['page'] : "";
		$itemperpage = isset($_REQUEST['item']) ? $_REQUEST['item'] : "";
		$brand = isset($_REQUEST['brand']) ? $_REQUEST['brand'] : "";
		$category = isset($_REQUEST['category']) ? $_REQUEST['category'] : "";
		$N_otype = isset($_REQUEST['otype']) ? $_REQUEST['otype'] : "";
		$N_start_date = isset($_REQUEST['start_date']) ? $_REQUEST['start_date'] : "";
		$N_end_date = isset($_REQUEST['end_date']) ? $_REQUEST['end_date'] : "";
		$N_shipment_type = isset($_REQUEST['ship_type']) ? $_REQUEST['ship_type'] : "";
		$N_keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : "";

		if($N_start_date != ""){
			$N_start_format = date("Y-m-d", strtotime($N_start_date));
		} else {
			$N_start_format = "";
		}
		if($N_end_date != ""){
			$N_end_format = date("Y-m-d", strtotime($N_end_date)).' 23:59:00';
		} else {
			$N_end_format = "";
		}

		$result = $obj_product->get_data_report($obj_crud, $N_page, $itemperpage, $N_shipment_type, $N_otype, $brand, $category, $N_start_format, $N_end_format, $N_keyword);
		if(is_array($result)){
			$total_data = $result[0]['total_data_all'];
			$total_page = $result[0]['total_page'];
			$remaining = $total_data - ((($N_page-1) * $itemperpage) + count($result));
			$R_message = array("code" => "200", "message" => "Fetch success", "num_data" => count($result), "remaining" => $remaining, "total_data" => $total_data, "total_page" => $total_page, "data" => $result);
		}

		echo json_encode($R_message); 
	} 
	else{
		$R_message = array("code" => "404", "message" => "Action Not Found");
		echo json_encode($R_message);
	}
}//end gate
else{
	$R_message = array("code" => "404", "message" => "Not Found");
	echo json_encode($R_message);
}
?>