<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");

include_once '../config/database.php';
include_once '../objects/sales.php';
include_once '../objects/sales_detail.php';
// session_start();
 
$database = new Database();
$db = $database->getConnection();

$sales = new Sales($db);
$sales_detail = new SalesDetail($db);
$data = json_decode(file_get_contents("php://input"));

$sales->voucher_no = $data->voucher_no;
$sales->getOnePrint();

$s_details = array();
$sales_detail->voucher_no = $data->voucher_no;
$stmt = $sales_detail->getSalesDetail();
$num = $stmt->rowCount();
if($num>0){
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        extract($row);
		$s_detail=array(
            "id" => $id,
            "voucher_no" => $voucher_no,
            "brand" => $brand,
			"item_code" => $item_code,
			"item_name" => $brand ." ".$item_name,
            "store" => $store,
            "quantity" => $quantity,		
            "price" => $price
        );
        array_push($s_details, $s_detail);
    }
}

$sales = array(
    "id" => (int)$sales->id,
	"sales_date" => $sales->sales_date,
	"voucher_no" => $sales->voucher_no,
	"car_no" => $sales->car_no,
	"name" => $sales->name,
    "phone" => $sales->phone,
    "address" => $sales->address,
    "total_amount" => number_format($sales->total_amount),
    "payment" => number_format($sales->payment),
	"balance" => number_format($sales->total_amount - $sales->payment),
	"s_details" => $s_details
);
echo json_encode($sales);

?>