<?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/installation.php';
include_once '../objects/installation_detail.php';
include_once '../objects/ont.php';

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

$database = new Database();
$db = $database->getConnection();
 
$installation = new Installation($db);
$installation_detail = new InstallationDetail($db);
$ont = new ONT($db);
$data = json_decode(file_get_contents("php://input"));
	
$installation->order_no = $data->order_no;
$installation->customer_id = $data->customer_id;
$installation->customer_name = $data->customer_name;
$installation->customer_phone = $data->customer_phone;
$installation->install_type = $data->install_type;
$installation->install_date = $data->install_date;
$installation->city = $data->city;
$installation->id_no = $data->id_no;
$installation->geolocation = $data->geolocation;
$installation->install_address = $data->install_address;
$installation->slot_id = $data->slot_id;
$installation->pon_port = $data->pon_port;
$installation->odb = $data->odb;
$installation->service_port = $data->service_port;
$installation->technician1 = $data->technician1;
$installation->technician2 = $data->technician2;
$installation->technician3 = $data->technician3;
$installation->remark = $data->remark;
$installation->entry_by = (isset($_SESSION['user'])?$_SESSION['user']:"");
$installation->entry_date_time = date("Y-m-d H:i:s");	

$installation_detail->install_type_ = $data->install_type;
$installation_detail->install_date_ = $data->install_date;
$installation_detail->city_ = $data->city;
$installation_detail->id_no_ = $data->id_no;

$ont->id_no = $data->id_no;
$ont->city = $data->city;
$ont->entry_by = $installation->entry_by;
$ont->entry_date_time = $installation->entry_date_time;
if($installation->install_type=="Cable"){

	if($ont->exist()){
		$msg_arr = array(
			"message" => "duplicate"
		);
		echo json_encode($msg_arr);
		die();
	}else{
		if(!$ont->exist1()){
			$ont->createFromInstall();
		}
	}
	
}


	if($installation->create()){
		$installation_detail->installation_id = $installation->id;
		foreach($data->item_details as $p_detail){
	
			$installation_detail->item_code = $p_detail->item_code;
			$installation_detail->item_name = $p_detail->item_name;
			$installation_detail->quantity = $p_detail->quantity;
			$installation_detail->unit = $p_detail->unit;
			$installation_detail->entry_by = (isset($_SESSION['user'])?$_SESSION['user']:"");
			$installation_detail->entry_date_time = date("Y-m-d H:i:s");	
		
			if(!$installation_detail->create()){
				$msg_arr = array(
					"message" => "error2",
					"table" => "installation_detail"
				);
				echo json_encode($msg_arr);
				die();
			}
		}

		$arr = array(
			"message" => "created",
			"last_id" => $installation->id
		);
	}else{
		$arr = array(
			"message" => "error1"
		);
	}

echo json_encode($arr);
?>
