<?php
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/check_in.php';
include_once '../objects/other_service_charges.php';

$database = new Database();
$db = $database->getConnection();

// $check_in = new CheckIn($db);
$other_service_charges = new OtherServiceCharges($db);

$data = json_decode(file_get_contents("php://input"));
// $check_in->id = $data->checkin_id;
// $check_in->getOneRowForCheckOut();

$other_service_charges->checkin_id = $data->checkin_id;

$check_in_items = array();

$stmt = $other_service_charges->getAllItemByCheckInId();
$num = $stmt->rowCount();

if($num > 0) {
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        extract($row);

        $item = array(
            "category" => $category,
            "type" => $type,
            "item_code" => $item_code,
            "item_name" => $item_name,
            "quantity" => $quantity,
            "price" => $price,
            "amount" => $quantity* (($foc==0)?$price:0),

        );
        array_push($check_in_items, $item);
    }
}

$total = array_sum(array_column($check_in_items, 'amount'));
// $check_in_charges = array_sum(array_column($check_in_items, 'charges'));
// $tax_charges = array_sum(array_column($check_in_items, 'tax'));

// $net_amount = $check_in_charges + $tax_charges - $deposit_charges;

$check_in = array(
    // "reservation_no" => $check_in->reservation_no,
    // "room_no" => $check_in->room_no,
    // "start_date" => $check_in->start_date,
    // "end_date" => $check_in->end_date,
    // "guest_names" => $check_in->guest_names,
    "check_in_items" => $check_in_items,
    "total" => $total,

);

echo json_encode($check_in);
?>
