setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { echo json_encode(["status" => "error", "message" => "Database Connection Failed: " . $e->getMessage()]); exit(); } $type = isset($_GET['type']) ? $_GET['type'] : ''; $method = $_SERVER['REQUEST_METHOD']; // ======================================================================= // REQUEST ROUTING CONTROLLER // ======================================================================= // PATH 1: READ / RETRIEVE ALL DATA BLOCKS ON USER INITIALIZATION LOGIN if ($method === 'GET' && $type === 'sales') { try { // 🚀 FIXED: We are querying the full columns and passing them exactly as keys $stmt = $conn->prepare("SELECT txn_id, timestamp_string, cashier, customer, summary, total_amount, payment_mode FROM sales_history ORDER BY id DESC LIMIT 500"); $stmt->execute(); $sales = $stmt->fetchAll(PDO::FETCH_ASSOC); // Format properties to clean JSON parameters $formattedSales = array_map(function($sale) { return [ "id" => $sale['txn_id'], "txnId" => $sale['txn_id'], "timestamp" => $sale['timestamp_string'], "cashier" => $sale['cashier'], "customer" => $sale['customer'], "summary" => $sale['summary'], "total" => floatval($sale['total_amount']), "mode" => $sale['payment_mode'] ]; }, $sales); echo json_encode($formattedSales); } catch(PDOException $e) { echo json_encode(["status" => "error", "message" => $e->getMessage()]); } exit(); } // PATH 2: COMITTING A COMPLETED POS TRANSACTION INVOICE TO CLOUD if ($method === 'POST' && $type === 'commit_sale') { $inputData = json_decode(file_get_contents("php://input"), true); if (!$inputData) { echo json_encode(["status" => "error", "message" => "Invalid incoming JSON packet payload stream."]); exit(); } try { $conn->beginTransaction(); // 1. Insert transaction details into the Sales History table $stmt = $conn->prepare("INSERT INTO sales_history (txn_id, timestamp_string, cashier, customer, summary, total_amount, payment_mode) VALUES (:txn_id, :ts, :cashier, :cust, :summary, :total, :mode)"); $stmt->execute([ ':txn_id' => $inputData['txnId'], ':ts' => $inputData['timestamp'], ':cashier' => $inputData['cashier'], ':cust' => $inputData['customer'], ':summary' => $inputData['summary'], ':total' => $inputData['total'], ':mode' => $inputData['mode'] ]); // 2. Adjust financial drawer metrics safely inside double-entry tables row cells $modeKey = 'ledger_cash'; if (strtolower($inputData['mode']) === 'mpesa') $modeKey = 'ledger_mpesa'; if (strtolower($inputData['mode']) === 'bank') $modeKey = 'ledger_bank'; $stmt = $conn->prepare("UPDATE system_ledgers SET ledger_value = ledger_value + :amt WHERE ledger_key = :mKey"); $stmt->execute([':amt' => $inputData['total'], ':mKey' => $modeKey]); // 3. Deduct volume from the central tank row if it is a fluid refill item if (isset($inputData['deductWaterVolume']) && floatval($inputData['deductWaterVolume']) > 0) { $stmt = $conn->prepare("UPDATE system_ledgers SET ledger_value = GREATEST(0, ledger_value - :vol) WHERE ledger_key = 'central_tank_litres'"); $stmt->execute([':vol' => $inputData['deductWaterVolume']]); } $conn->commit(); echo json_encode(["status" => "success", "message" => "Invoice committed successfully to central cloud database row records."]); } catch(Exception $e) { $conn->rollBack(); echo json_encode(["status" => "error", "message" => "Transaction Aborted: " . $e->getMessage()]); } exit(); } // PATH 3: RETRIEVE SYSTEM DRAWER BALANCES AND WATER RESERVOIRS if ($method === 'GET' && $type === 'get_balances') { try { $stmt = $conn->prepare("SELECT ledger_key, ledger_value FROM system_ledgers"); $stmt->execute(); $ledgers = $stmt->fetchAll(PDO::FETCH_KEY_PAIR); echo json_encode([ "status" => "success", "cash" => floatval($ledgers['ledger_cash'] ?? 0), "mpesa" => floatval($ledgers['ledger_mpesa'] ?? 0), "bank" => floatval($ledgers['ledger_bank'] ?? 0), "tank_litres" => floatval($ledgers['central_tank_litres'] ?? 3000) ]); } catch(PDOException $e) { echo json_encode(["status" => "error", "message" => $e->getMessage()]); } exit(); } ?>