=== recordInvoicePayment === function recordInvoicePayment(int $invoiceId, float $amount, array $meta = []): array { if ($amount <= 0.0) { return [ 'success' => false, 'message' => 'Nominal pembayaran harus lebih besar dari 0.' ]; } $amount = round($amount, 2); $startedTransaction = false; if (!$this->db->inTransaction()) { $this->db->beginTransaction(); $startedTransaction = true; } try { // Row lock on invoice to prevent concurrent overpayments $stmt = $this->db->prepare("SELECT * FROM billing_invoices WHERE id = :id FOR UPDATE"); $stmt->execute([':id' => $invoiceId]); $invoice = $stmt->fetch(PDO::FETCH_ASSOC); if (!$invoice) { if ($startedTransaction) $this->db->rollBack(); return ['success' => false, 'message' => "Invoice #{$invoiceId} tidak ditemukan."]; } === getFinancialSummary === function getFinancialSummary(?string $startDate = null, ?string $endDate = null, ?string $routerId = null, ?string $routerSession = null): array { $hasRouterId = $this->hasColumn('billing_payments', 'router_id'); $hasRouterSession = $this->hasColumn('billing_payments', 'router_session'); $hasVoid = $this->hasColumn('billing_payments', 'is_voided'); // 1. Cash Revenue metrics from billing_payments $sql = "SELECT COUNT(*) AS total_transactions, COALESCE(SUM(amount), 0) AS period_cash_revenue, COALESCE(SUM(CASE WHEN DATE(payment_date) = CURDATE() THEN amount ELSE 0 END), 0) AS today_cash_revenue, COALESCE(SUM(CASE WHEN payment_date >= DATE_FORMAT(NOW(), '%Y-%m-01 00:00:00') THEN amount ELSE 0 END), 0) AS month_cash_revenue FROM billing_payments WHERE 1=1"; if ($hasVoid) { $sql .= " AND is_voided = 0"; } === generateMonthlyInvoices === function generateMonthlyInvoices(string $period, array $filters = [], bool $dryRun = false): array { $hasRouterId = $this->hasColumn('billing_customers', 'router_id'); $hasRouterSession = $this->hasColumn('billing_customers', 'router_session'); $sql = "SELECT bc.id, bc.name, bc.status, bc.profile_id, bc.billing_day, bc.is_isolated"; if ($hasRouterId) $sql .= ", bc.router_id"; if ($hasRouterSession) $sql .= ", bc.router_session"; $sql .= " FROM billing_customers bc WHERE bc.status = 'active'"; $params = []; if (!empty($filters['customer_id'])) { $sql .= " AND bc.id = :cust_id"; $params[':cust_id'] = (int)$filters['customer_id']; } $routerId = !empty($filters['router_id']) ? (string)$filters['router_id'] : null; $routerSession = !empty($filters['router_session']) ? (string)$filters['router_session'] : null; if ($routerId !== null && $routerId !== 'all') {