<?php
require_once 'BillingEngine.php';
// Assuming $db is your established PDO connection
$billing = new BillingEngine($db);

// Incoming POST data from any department (Pharmacy, Lab, etc.)
$patientId = $_POST['patient_id'];
$department = $_POST['department']; // e.g., 'laboratory'
$itemCode = $_POST['item_code']; // e.g., 'LAB-MAL-01'
$description = $_POST['description']; // e.g., 'Malaria Rapid Test'
$qty = $_POST['quantity'];
$price = $_POST['unit_price'];

// Dynamic metadata based on department
$metadata = [];
if ($department === 'laboratory') {
    $metadata['lab_req_id'] = $_POST['lab_req_id'];
} elseif ($department === 'pharmacy') {
    $metadata['prescription_id'] = $_POST['prescription_id'];
}

$response = $billing->addCharge($patientId, null, $department, $itemCode, $description, $qty, $price, $metadata);

header('Content-Type: application/json');
echo json_encode($response);
?>