Edit file File name : _helpers.php Content :<?php // Core shared functions for OmniFlow Utilities function get_db_connection() { require 'db_config.php'; try { $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { throw new Exception("Connection failed: " . $conn->connect_error); } $conn->set_charset("utf8mb4"); return $conn; } catch (Exception $e) { header('Content-Type: application/json'); die(json_encode(['success' => false, 'message' => 'Database Connection Error'])); } } function get_stmt_result($stmt) { $result = []; $stmt->store_result(); $meta = $stmt->result_metadata(); if (!$meta) return $result; $fields = []; $row = []; while ($field = $meta->fetch_field()) { $fields[] = &$row[$field->name]; } call_user_func_array([$stmt, 'bind_result'], $fields); while ($stmt->fetch()) { $r = []; foreach ($row as $key => $val) { $r[$key] = $val; } $result[] = $r; } return $result; } function update_customer_status($conn, $customer_id) { $stmt = $conn->prepare("SELECT balance, status FROM customers WHERE id = ?"); $stmt->bind_param("s", $customer_id); $stmt->execute(); $res = get_stmt_result($stmt); if (empty($res)) return; $balance = (float)$res[0]['balance']; $current_status = $res[0]['status']; // Logic: Due date is end of month. // If today is past the 5th of the NEXT month and balance is > 0, Pending Disconnection. // If today is past the end of the month and balance is > 0, Overdue. $today = new DateTime(); $day_of_month = (int)$today->format('j'); // Check for arrears (balance from previous months) $stmt_arr = $conn->prepare("SELECT SUM(amount) as current_charges FROM readings WHERE customer_id = ? AND MONTH(reading_date) = MONTH(CURRENT_DATE) AND YEAR(reading_date) = YEAR(CURRENT_DATE)"); $stmt_arr->bind_param("s", $customer_id); $stmt_arr->execute(); $curr_res = get_stmt_result($stmt_arr); $current_charges = (float)($curr_res[0]['current_charges'] ?? 0); $arrears = $balance - $current_charges; $status = 'Active'; if ($balance > 0) { if ($arrears > 0) { if ($day_of_month > 5) { $status = 'Pending Disconnection'; } else { $status = 'Overdue'; } } else { // Balance exists but it's only from the current month $status = 'Active'; } } // Manual override preservation for disconnected if ($current_status === 'Disconnected' && $balance > 0) { $status = 'Disconnected'; } $stmt = $conn->prepare("UPDATE customers SET status = ? WHERE id = ?"); $stmt->bind_param("ss", $status, $customer_id); $stmt->execute(); } ?>Save