false, 'error' => 'Invalid request']; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $project_id = $_POST['projectId'] ?? null; $month = $_POST['month'] ?? null; $metrics = [ 'wip' => $_POST['wip'] ?? null, 'opening_balance' => $_POST['openingBalance'] ?? null, 'billings' => $_POST['billings'] ?? null, 'expenses' => $_POST['expenses'] ?? null, ]; if ($project_id && $month && !in_array(null, $metrics, true)) { try { $pdo = db(); $sql = "INSERT INTO projectFinanceMonthly (projectId, month, wip, opening_balance, billing, expenses, is_overridden) VALUES (:pid, :m, :wip, :ob, :bill, :exp, 1) ON DUPLICATE KEY UPDATE wip = :wip, opening_balance = :ob, billing = :bill, expenses = :exp, is_overridden = 1"; $stmt = $pdo->prepare($sql); $stmt->execute([ ':pid' => $project_id, ':m' => $month, ':wip' => $metrics['wip'], ':ob' => $metrics['opening_balance'], ':bill' => $metrics['billings'], ':exp' => $metrics['expenses'] ]); $response = ['success' => true]; } catch (PDOException $e) { $response['error'] = 'Database error: ' . $e->getMessage(); error_log($e->getMessage()); } } else { $response['error'] = 'Missing required fields.'; } } echo json_encode($response);