<?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/suspend.php';
include_once '../objects/sales_detail.php';
date_default_timezone_set('Asia/Rangoon'); 
session_start();

$database = new Database();
$db = $database->getConnection();
 
$suspend = new Suspend($db);
$sales_detail = new SalesDetail($db);
$data = json_decode(file_get_contents("php://input"));
	
$suspend->id = $data->id;
$suspend->invoice_no = $data->invoice_no;
$suspend->unsuspend_date = $data->unsuspend_date;
$diff=date_diff(date_create($data->suspend_date),date_create($data->unsuspend_date));
$suspend->days = $diff->format("%a");

$sales_detail->invoice_no = $data->invoice_no;
$sales_detail->promotion_id = 0;
$sales_detail->mbps = 0;
$sales_detail->days = $suspend->days;
$sales_detail->package_percent = 0;
$sales_detail->fee_percent = 0;
$sales_detail->gift = "";
$sales_detail->status = "Suspend";
$sales_detail->entry_by = isset($_SESSION['user'])?$_SESSION['user']:"";
$sales_detail->entry_date_time = date("Y-m-d H:i:s");	

	if($suspend->update()){
		$sales_detail->suspend_id = $data->id;

		if($sales_detail->chksuspend()){
			if($sales_detail->updateSuspend()){
				$arr = array(
					"message" => "updated"
				);
			}else{
				$arr = array(
					"message" => "error"
				);
			}
		}else{
			if($sales_detail->createSuspend()){
				$arr = array(
					"message" => "updated"
				);
			}else{
				$arr = array(
					"message" => "error"
				);
			}
		}


	}else{
		$arr = array(
			"message" => "error"
		);
	}
// }
echo json_encode($arr);
?>