<?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';
date_default_timezone_set('Asia/Rangoon');
session_start();
$database = new Database();
$db = $database->getConnection();
 
$sales = new Sales($db);
$data = json_decode(file_get_contents("php://input"));

// $ont->expire_date = $data->expire_date;   
$sales->df = $data->df;
$sales->dt = $data->dt;
$sales->start_df = $data->start_df;
$sales->start_dt = $data->start_dt;
$sales->id_no = $data->id_no;
$sales->city = $data->city;
$sales->assign = $data->assign;
$sales->S_R = $data->S_R;

$stmt = $sales->searchDateInterval();
$num = $stmt->rowCount();

$arr=array();
$arr["records"]=array();
$total_amountF = $refundF = $paymentF = $balanceF = 0;
if($num>0){
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
        $detail=array( 
            "id" => $id,
            "sales_date" => ($name)?$sales_date:"",
            "invoice_no" => ($invoice_no)?$invoice_no:"",
            "city" => ($city)?$city:"",
            "customer_id" => ($customer_id)?$customer_id:"",
            "id_no" => ($id_no)?$id_no:"",
            "name" => ($name)?$name:"",
            "package" => ($package)?$package:"",	
            "months" => ($months)?$months:"",
            "extra_days" => number_format($extra_days),
            "start_date" => ($start_date)?$start_date:"",	
            "end_date" => ($end_date)?$end_date:"",	
			"price" => number_format($price),
            "fee" => number_format($fee),
            "tax_charges" => number_format($price*$tax_percent/100),
            "total_amount" => number_format($price + $fee +($price*$tax_percent/100)),
            // "normal_discount" => number_format($normal_discount),
            // "member_discount" => number_format($member_discount),
            // "promotion_discount" => number_format($promotion_discount),
            // "pcc" => number_format($price_change_charges),
            // "refund" => number_format($refund_amount),
            // "payment" => number_format($tp),
            // "balance" => number_format($price + $fee -$refund_amount - $tp - $normal_discount - $member_discount - $promotion_discount + $price_change_charges)
        );  
        $total_amountF += $price + $fee +($price*$tax_percent/100);
        // $refundF += $refund_amount ;
        // $normal_discountF += $normal_discount ;
        // $member_discountF += $member_discount ;
        // $promotion_discountF += $promotion_discount ;
        // $pccF += $pcc ;
        // $paymentF += $tp;
        // $balanceF += $price + $fee - $refund_amount - $tp - $normal_discount - $member_discount - $promotion_discount + $price_change_charges;
        array_push($arr["records"], $detail);
    }
}
$arr["total_amountF"] = number_format($total_amountF);
// $arr["normal_discountF"] = number_format($normal_discountF);
// $arr["member_discountF"] = number_format($member_discountF);
// $arr["promotion_discountF"] = number_format($promotion_discountF);
// $arr["pccF"] = number_format($pccF);
// $arr["refundF"] = number_format($refundF);
// $arr["paymentF"] = number_format($paymentF);
// $arr["balanceF"] = number_format($balanceF);
echo json_encode($arr);
?>