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

$database = new Database();
$db = $database->getConnection();
 
$extra_days = new ExtraDays($db);
$sales_detail = new SalesDetail($db);
$data = json_decode(file_get_contents("php://input"));
	
$extra_days->city = $data->city;
$extra_days->date = $data->date;
$extra_days->id_no = $data->id_no;
$extra_days->customer_id = $data->customer_id;
$extra_days->invoice_no = $data->invoice_no;
$extra_days->reason = $data->reason;
$extra_days->days = $data->days;
$extra_days->entry_by = isset($_SESSION['user'])?$_SESSION['user']:"";
$extra_days->entry_date_time = date("Y-m-d H:i:s");	

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

	if($extra_days->create()){
		$sales_detail->extra_days_id = $extra_days->id;
		if($sales_detail->create()){
			$arr = array(
				"message" => "created"
			);
		}else{
			$arr = array(
				"message" => "error"
			);
		}
	}else{
		$arr = array(
			"message" => "error"
		);
	}
// }
echo json_encode($arr);
?>