<?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->city = $data->city;
$sales->df = $data->df;
$sales->dt = $data->dt;
$sales->invoice_no = $data->invoice_no;
$sales->id_no = $data->id_no;
$sales->name = $data->name;
$sales->mbps = $data->mbps;
$sales->months = $data->months;
$sales->S_R = $data->S_R;
$sales->bal = $data->bal;

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

$arr=array();
$arr["records"]=array();
$taxF = $total_amountF = $refundF = $paymentF = $balanceF = $normal_discountF= $member_discountF= $promotion_discountF= $pccF= 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:"",
            "id_no" => ($id_no)?$id_no:"",
            "name" => ($name)?$name:"",
            "city" => ($city)?$city:"",
            // "phone" => ($phone)?$phone:"",
            "package" => ($package)?$package:"",	
            "months" => number_format($months),
			"price" => number_format($price),
            "fee" => number_format($fee),
            "tax_charges" => number_format($price*$tax_percent/100),
            "normal_discount" => number_format($normal_discount),
            "member_discount" => number_format($member_discount),
            "promotion_discount" => number_format($promotion_discount),
            "refund" => number_format($refund_amount),
            "pcc" => number_format($price_change_charges),
            "total_amount" => number_format($price*$months + $fee + ($price*$months*$tax_percent/100) - $normal_discount - $member_discount - $promotion_discount - $refund_amount + $price_change_charges),
            "payment" => number_format($tp),
            "balance" => number_format($price*$months + $fee + ($price*$months*$tax_percent/100) - $normal_discount - $member_discount - $promotion_discount - $refund_amount + $price_change_charges - $tp),
            "entry_by" => ($entry_by)?$entry_by:"",
        );  
        $taxF += ($price*$tax_percent/100) ;
        $normal_discountF += $normal_discount ;
        $member_discountF += $member_discount ;
        $promotion_discountF += $promotion_discount ;
        $refundF += $refund_amount ;
        $pccF += $price_change_charges ;
        $total_amountF += $price*$months + $fee + ($price*$months*$tax_percent/100) - $normal_discount - $member_discount - $promotion_discount - $refund_amount + $price_change_charges;
        $paymentF += $tp;
        $balanceF += $price*$months + $fee + ($price*$months*$tax_percent/100) - $normal_discount - $member_discount - $promotion_discount - $refund_amount + $price_change_charges - $tp;
        array_push($arr["records"], $detail);
    }
}
$arr["taxF"] = number_format($taxF);
$arr["normal_discountF"] = number_format($normal_discountF);
$arr["member_discountF"] = number_format($member_discountF);
$arr["promotion_discountF"] = number_format($promotion_discountF);
$arr["refundF"] = number_format($refundF);
$arr["pccF"] = number_format($pccF);
$arr["total_amountF"] = number_format($total_amountF);
$arr["paymentF"] = number_format($paymentF);
$arr["balanceF"] = number_format($balanceF);
echo json_encode($arr);
?>