<?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"));

$sales->df = $data->df;
$sales->dt = $data->dt;
$sales->id_no = $data->id_no;
$sales->city = $data->city;
$sales->bal = $data->bal;

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

$arr=array();
$arr["records"]=array();
$total_amountF = $paymentF = $balanceF = 0;
if($num>0){
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
        $detail=array( 
            // "id" => $id,
            "payment_date" => ($date)?$date:"",
            "invoice_no" => ($invoice_no)?$invoice_no:"",
            "id_no" => ($id_no)?$id_no:"",
            "name" => ($name)?$name:"",
            "phone" => ($phone)?$phone:"",
            "address" => ($address)?$address:"",
            "package" => ($package)?$package:"",	
            "total" => number_format($total),
			"payment" => number_format($amount),
            "balance" => number_format( $total-$amount)
        );  
        $total_amountF += $total ;
        $paymentF += $amount ;
        $balanceF += $total-$amount ;
        array_push($arr["records"], $detail);
    }
}
$arr["total_amountF"] = number_format($total_amountF);
$arr["paymentF"] = number_format($paymentF);
$arr["balanceF"] = number_format($balanceF);
echo json_encode($arr);
?>