<?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/item_sales.php';
include_once '../objects/item_sales_detail.php';
// session_start();
 
$database = new Database();
$db = $database->getConnection();

$item_sales = new ItemSales($db);
$item_sales_detail = new ItemSalesDetail($db);
$data = json_decode(file_get_contents("php://input"));

$item_sales->voucher_no = $data->voucher_no;
$item_sales->getOnesales();

$s_details = array();
$item_sales_detail->voucher_no = $data->voucher_no;
$stmt = $item_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,
			"item_code" => $item_code,
            "item_name" => $item_name,
			"description" => $item_name,
            "store" => $store,
            "quantity" => $quantity,		
            "price" => $price
        );
        array_push($s_details, $s_detail);
    }
}

$sales = array(
    "id" => (int)$item_sales->id,
	"sales_date" => $item_sales->sales_date,
	"voucher_no" => $item_sales->voucher_no,
	"name" => $item_sales->name,
    "store" => $item_sales->store,
    "total_amount" => $item_sales->total_amount,
    "discount" => $item_sales->discount,
	"payment" => $item_sales->payment,
    "id_no" => $item_sales->id_no,
    "phone" =>$item_sales->phone,
	"s_details" => $s_details
);
echo json_encode($sales);

?>