From 3965ab4f7a8280d899f943e34861d6544a55a6ab Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Thu, 27 Nov 2025 10:58:53 +0000 Subject: [PATCH] Auto commit: 2025-11-27T10:58:53.677Z --- project_details.php | 6 +++--- save_override.php | 33 +++++++++++++++++++++------------ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/project_details.php b/project_details.php index 1d4231a..5f7dd80 100644 --- a/project_details.php +++ b/project_details.php @@ -81,13 +81,13 @@ if ($project_id) { $fid = $latest_forecast['id']; // Base Monthly Cost - $cost_sql = "SELECT fa.month, SUM(fa.allocatedDays * (r.totalMonthlyCost / 20)) FROM forecastAllocation fa JOIN roster r ON fa.rosterId = r.id WHERE fa.forecastingId = :fid GROUP BY fa.month"; + $cost_sql = "SELECT fa.month, SUM(fa.allocatedDays * (r.totalMonthlyCost / 20)) as cost FROM forecastAllocation fa JOIN roster r ON fa.rosterId = r.id WHERE fa.forecastingId = :fid GROUP BY fa.month"; $cost_stmt = $pdo->prepare($cost_sql); $cost_stmt->execute([':fid' => $fid]); $monthly_costs = $cost_stmt->fetchAll(PDO::FETCH_KEY_PAIR); // Base Monthly WIP - $wip_sql = "SELECT fa.month, SUM(fa.allocatedDays * r.discountedRevenue) FROM forecastAllocation fa JOIN roster r ON fa.rosterId = r.id WHERE fa.forecastingId = :fid GROUP BY fa.month"; + $wip_sql = "SELECT fa.month, SUM(fa.allocatedDays * r.discountedRevenue) as wip FROM forecastAllocation fa JOIN roster r ON fa.rosterId = r.id WHERE fa.forecastingId = :fid GROUP BY fa.month"; $wip_stmt = $pdo->prepare($wip_sql); $wip_stmt->execute([':fid' => $fid]); $monthly_wip = $wip_stmt->fetchAll(PDO::FETCH_KEY_PAIR); @@ -143,7 +143,7 @@ if ($project_id) { } // 3. Fetch and apply overridden data - $override_stmt = $pdo->prepare("SELECT month, wip, opening_balance, billing, expenses FROM projectFinanceMonthly WHERE projectId = :pid AND is_overridden = 1"); + $override_stmt = $pdo->prepare("SELECT month, MAX(CASE WHEN metricName = 'wip' THEN value ELSE 0 END) as wip, MAX(CASE WHEN metricName = 'opening_balance' THEN value ELSE 0 END) as opening_balance, MAX(CASE WHEN metricName = 'billing' THEN value ELSE 0 END) as billing, MAX(CASE WHEN metricName = 'expenses' THEN value ELSE 0 END) as expenses FROM projectFinanceMonthly WHERE projectId = :pid AND is_overridden = 1 GROUP BY month"); $override_stmt->execute([':pid' => $project_id]); while ($row = $override_stmt->fetch(PDO::FETCH_ASSOC)) { diff --git a/save_override.php b/save_override.php index ca0ef68..e83d5fc 100644 --- a/save_override.php +++ b/save_override.php @@ -19,20 +19,29 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { 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"; - + $sql = "INSERT INTO projectFinanceMonthly (projectId, month, metricName, value, is_overridden) + VALUES (:pid, :m, :metric, :value, 1) + ON DUPLICATE KEY UPDATE value = :value, 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'] - ]); + $metric_map = [ + 'wip' => 'wip', + 'opening_balance' => 'opening_balance', + 'billings' => 'billing', + 'expenses' => 'expenses' + ]; + + foreach ($metrics as $metricKey => $value) { + if (isset($metric_map[$metricKey])) { + $metricName = $metric_map[$metricKey]; + $stmt->execute([ + ':pid' => $project_id, + ':m' => $month, + ':metric' => $metricName, + ':value' => $value + ]); + } + } $response = ['success' => true];