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

$database = new Database();
$db = $database->getConnection();
 
$sales = new Sales($db);
$sales_detail = new SalesDetail($db);
$payment = new Payment($db);
$ont = new ONT($db);
$message = new Message($db);
$data = json_decode(file_get_contents("php://input"));

$sales->city = $data->city;
$sales->sales_date = $data->sales_date;
$sales->invoice_no = $sales->getGenerateInvoiceNo(date("y")."_".date("m")."_");
$sales->customer_id = $data->customer_id;
$sales->name = $data->name;
$sales->id_no = $data->id_no;
$sales->package = $data->package;
$sales->mbps = $data->mbps;
$sales->months = $data->months;
$sales->days = $data->days;
$sales->price= $data->package_price;
$sales->fee= $data->installation_fee;
$sales->tax_percent= $data->tax_percent;
$sales->normal_discount= $data->normal_discount;
$sales->member_discount= $data->member_discount;
$sales->promotion_discount= $data->promotion_discount;

if($data->S_R=='Sales'){
	$sales->start_date= "";
	$sales->end_date = "";
}else{
	$sales->start_date= $data->start_date;
	$sales->end_date = "";
}

$sales->refund_amount= $data->refund_amount;
$sales->price_change_charges= $data->price_change_charges;
$sales->installation_address= $data->installation_address;
$sales->S_R= $data->S_R;
$sales->entry_by = isset($_SESSION['user'])?$_SESSION['user']:"";
$sales->entry_date_time = date("Y-m-d H:i:s");

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

$payment->p_customer_id = $sales->customer_id;
$payment->invoice_no = $sales->invoice_no;
$payment->date = $sales->sales_date;
$payment->payment_type = $data->payment_type;
$payment->amount = $data->payment;
$payment->city = $sales->city;
$payment->entry_by = isset($_SESSION['user'])?$_SESSION['user']:"";
$payment->entry_date_time = date("Y-m-d H:i:s");
$payment->create();

$ont->id_no = $sales->id_no;
$ont->status = 1;
$ont->updateStatus();

foreach($data->details as $s_detail){
	$sales_detail->package_percent = $s_detail->package_percent;
	$sales_detail->fee_percent = $s_detail->fee_percent;
	$sales_detail->mbps = $s_detail->mbps;
	$sales_detail->days = $s_detail->days;
	$sales_detail->gift = $s_detail->gift;
	$sales_detail->promotion_id = $s_detail->pid;
	$sales_detail->status= 'Promotion';
	$sales_detail->create();
}

if($sales->create()){

	if($data->S_R=="Renew"){
		// $msg_detail = $message->getMsg("renew");
		// $token = "";
		// $stmt = $sales->getSMSByID($sales->id);
		// $num = $stmt->rowCount();
		// if($num>0){
		// 	while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
		// 		extract($row);
	
		// 		$healthy = ["[ID]", "[Plan]", "[Start_Date]", "[End_Date]", "[Amount]", "[Invoice_No]", "[Hotline]"];
		// 		$yummy   = [$id_no, $package, $start_date, $end_dates, number_format($payment), $invoice_no, $ph_no];
	
		// 		$data = [
		// 			"to"        =>      $phone,
		// 			"message"   =>      str_replace($healthy, $yummy, $msg_detail),
		// 		];
	
		// 		$ch = curl_init("https://smspoh.com/api/v2/send");
		// 		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		// 		curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
		// 		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		// 		curl_setopt($ch, CURLOPT_HTTPHEADER, [
		// 				'Authorization: Bearer RCZwPzftQwmUfuYfOeBjH2WICaTffqJIH82-_EBT6OXZzut4-tcxn5jkP2PUFfdV',
		// 				'Content-Type: application/json'
		// 			]);
			
		// 			curl_exec($ch);
	
					$sales->updateSMS("renew");
	
		// 	}
		// }
	}


	$arr = array(
		"message" => "created",
		"vno" => $sales->id
	);
}else{
	$arr = array(
		"message" => "error_r"
	);
}
echo json_encode($arr);	
?>