<?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';

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);
$data = json_decode(file_get_contents("php://input"));

$sales->invoice_no = $data->invoice_no;
$sales->sales_date = $data->sales_date;
$sales->name = $data->name;
$sales->package = $data->package;
$sales->mbps = $data->mbps;
$sales->days= $data->days;
$sales->price= $data->price;
$sales->fee= $data->fee;
$sales->tax_percent= $data->tax_percent;
$sales->start_date= $data->start_date;
// $sales->extra_days= $data->extra_days;
// $sales->end_date= $data->end_date;
$sales->entry_by = isset($_SESSION['user'])?$_SESSION['user']:"";
$sales->entry_date_time = date("Y-m-d H:i:s");


$sales_detail->invoice_no = $data->invoice_no;
$sales_detail->mbps = $data->mbps;
$sales_detail->days= $data->days;
$sales_detail->editFromSales();


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

if($sales->edit()){
	$arr = array(
		"message" => "updated"
	);
}else{
	$arr = array(
		"message" => "error_r"
	);
}
echo json_encode($arr);	
?>