<?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");

date_default_timezone_set('Asia/Bangkok');
session_start() ;

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

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

$other_service_charges = new OtherServiceCharges($db);

$data = json_decode(file_get_contents("php://input"));
$date_time = date("Y-m-d H:i:s");

foreach($data->items as $item){

	$other_service_charges->date = date("Y-m-d");//$item->date;
	$other_service_charges->checkin_id = $data->check_in_id;
	$other_service_charges->room_no = $data->room_no;
	$other_service_charges->customer_name = $data->customer_name;
	$other_service_charges->payment_type = $data->payment_type;
	$other_service_charges->payment_date_time = ($data->payment_type=="Charge to Room")?"":$date_time;

	// $other_service_charges->source = $data->source;

	$other_service_charges->category = $item->category;
	$other_service_charges->type = $item->type;
	$other_service_charges->item_code = $item->item_code;
	$other_service_charges->item_name = $item->item_name;
	$other_service_charges->quantity = $item->quantity;
	$other_service_charges->price = $item->price;
	$other_service_charges->foc = $item->foc;
	$other_service_charges->entry_by = isset($_SESSION['userid']) ? $_SESSION['userid'] : "";

	// if($item->service_category=="F&B"){
		if(!$other_service_charges->create()){
			$arr = array(
				"message" => "error"
			);
			echo json_encode($arr);
			die();
		}
	// }else{
	// 	if($stock_balance->checkItemForStockOut()){
	// 	    if($stock_out->create()){
	// 	        if($stock_balance->updateFromStockOut()){
	// 	            if(!$other_service_charges->create()){
	// 					$arr = array(
	// 						"message" => "error"
	// 					);
	// 					echo json_encode($arr);
	// 					die();
	// 				}
	// 	        }else{
	// 	            $stock_out->delete();
	// 	            echo 'error1'; 
	// 	            die();
	// 	        }    
	// 	    }else{
	//             echo 'error2'; 
	//             die(); 
	//     	}
	// 	}else{
	// 	    $arr = array(
	// 			"message" => "no_data"
	// 		);
	// 		echo json_encode($arr);
	// 	    die();
	// 	}
	// }
	
}

$arr = array(
	"message" => "created",
	"date_time" => strtotime($date_time)
);
echo json_encode($arr);
?>