diff --git a/assets/js/project_details.js b/assets/js/project_details.js index 2046209..2b50d12 100644 --- a/assets/js/project_details.js +++ b/assets/js/project_details.js @@ -70,7 +70,8 @@ document.addEventListener('DOMContentLoaded', function () { function updateCell(metric, month, value) { const cell = document.getElementById(`${metric.toLowerCase().replace(/\s/g, '-')}-${month}`); if (cell) { - cell.innerHTML = `€${parseFloat(value).toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; + const numericValue = value === '' ? 0 : parseFloat(value); + cell.innerHTML = `€${numericValue.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 2 })}`; } } }); diff --git a/project_details.php b/project_details.php index 3249c0e..3c64bfd 100644 --- a/project_details.php +++ b/project_details.php @@ -141,6 +141,39 @@ if ($project_id) { // Update previous month's WIP for the next iteration $previous_month_wip = $current_wip; } + + // 3. Fetch and apply overridden data + $override_stmt = $pdo->prepare("SELECT month, metricName, value FROM projectFinanceMonthly WHERE projectId = :pid AND is_overridden = 1"); + $override_stmt->execute([':pid' => $project_id]); + + $metric_map = [ + 'wip' => 'WIP', + 'opening_balance' => 'Opening Balance', + 'billings' => 'Billings', + 'expenses' => 'Expenses' + ]; + + while ($row = $override_stmt->fetch(PDO::FETCH_ASSOC)) { + $month = $row['month']; + $metricName = $row['metricName']; + $value = $row['value']; + + if (isset($metric_map[$metricName])) { + $display_metric = $metric_map[$metricName]; + if (isset($financial_data[$display_metric][$month])) { + $financial_data[$display_metric][$month] = $value; + } + } + } + + // 4. Recalculate dependent metrics (NSR and Margin) after override + foreach ($months as $month) { + $nsr = $financial_data['WIP'][$month] + $financial_data['Billings'][$month] - $financial_data['Opening Balance'][$month] - $financial_data['Expenses'][$month]; + $financial_data['NSR'][$month] = $nsr; + + $margin = ($nsr != 0) ? (($nsr - $financial_data['Cost'][$month]) / $nsr) : 0; + $financial_data['Margin'][$month] = $margin; + } } }