diff --git a/assets/js/project_details.js b/assets/js/project_details.js index e4046f2..37e5272 100644 --- a/assets/js/project_details.js +++ b/assets/js/project_details.js @@ -1,37 +1,31 @@ document.addEventListener('DOMContentLoaded', function () { - const overrideBtn = document.getElementById('override-btn'); + const overrideBtns = document.querySelectorAll('.btn-override'); - if (overrideBtn) { - overrideBtn.addEventListener('click', function () { + overrideBtns.forEach(btn => { + btn.addEventListener('click', function () { const projectId = this.dataset.projectId; const month = this.dataset.month; if (this.textContent.trim() === 'Override') { this.textContent = 'Save'; - this.classList.remove('btn-warning'); + this.classList.remove('btn-outline-primary'); this.classList.add('btn-success'); makeEditable('WIP', month); - makeEditable('Opening-Balance', month); + makeEditable('Opening Balance', month); makeEditable('Billings', month); makeEditable('Expenses', month); makeEditable('Cost', month); } else if (this.textContent.trim() === 'Save') { - const wip = document.getElementById(`wip-${month}-input`).value; - const openingBalance = document.getElementById(`opening-balance-${month}-input`).value; - const billings = document.getElementById(`billings-${month}-input`).value; - const expenses = document.getElementById(`expenses-${month}-input`).value; - const cost = document.getElementById(`cost-${month}-input`).value; - const formData = new FormData(); formData.append('projectId', projectId); - formData.append('month', month); - formData.append('wip', wip); - formData.append('openingBalance', openingBalance); - formData.append('billings', billings); - formData.append('expenses', expenses); - formData.append('cost', cost); + formData.append('month', month + '-01'); + formData.append('wip', document.getElementById(`wip-${month}-input`).value); + formData.append('openingBalance', document.getElementById(`opening-balance-${month}-input`).value); + formData.append('billings', document.getElementById(`billings-${month}-input`).value); + formData.append('expenses', document.getElementById(`expenses-${month}-input`).value); + formData.append('cost', document.getElementById(`cost-${month}-input`).value); fetch('save_override.php', { method: 'POST', @@ -40,19 +34,26 @@ document.addEventListener('DOMContentLoaded', function () { .then(response => response.json()) .then(data => { if (data.success) { - this.textContent = 'Overridden'; - this.classList.remove('btn-success'); - this.classList.add('btn-secondary'); - this.disabled = true; + // Replace button with "Overridden" badge + this.parentElement.innerHTML = 'Overridden'; - updateMetricCell('WIP', month, wip); - updateMetricCell('Opening-Balance', month, openingBalance); - updateMetricCell('Billings', month, billings); - updateMetricCell('Expenses', month, expenses); - updateMetricCell('Cost', month, cost); + updateMetricCell('WIP', month, data.wip); + updateMetricCell('Opening Balance', month, data.openingBalance); + updateMetricCell('Billings', month, data.billings); + updateMetricCell('Expenses', month, data.expenses); + updateMetricCell('Cost', month, data.cost); updateMetricCell('NSR', month, data.nsr); updateMetricCell('Margin', month, data.margin); + // Show the next override button + const nextButtonCell = this.parentElement.nextElementSibling; + if (nextButtonCell) { + const nextButton = nextButtonCell.querySelector('.btn-override'); + if (nextButton) { + nextButton.style.display = 'block'; + } + } + } else { alert('Error saving override: ' + data.error); } @@ -63,7 +64,7 @@ document.addEventListener('DOMContentLoaded', function () { }); } }); - } + }); function makeEditable(metric, month) { const cellId = `${metric.toLowerCase().replace(/\s/g, '-')}-${month}`; diff --git a/project_details.php b/project_details.php index bb0103d..9606cce 100644 --- a/project_details.php +++ b/project_details.php @@ -268,9 +268,6 @@ if (!$project) { foreach ($months as $month): ?> - - - > @@ -302,10 +298,49 @@ if (!$project) { ?> + + Actions + prepare("SELECT month, is_overridden FROM projectFinanceMonthly WHERE projectId = :pid GROUP BY month, is_overridden"); + $override_check_stmt->execute([':pid' => $project_id]); + $statuses = $override_check_stmt->fetchAll(PDO::FETCH_ASSOC); + + $monthly_override_status = []; + foreach($months as $month){ + $monthly_override_status[$month] = 0; + } + foreach($statuses as $status){ + if($status['is_overridden']){ + $monthly_override_status[$status['month']] = 1; + } + } + + $first_unoverridden_month = null; + foreach($monthly_override_status as $month => $status){ + if(!$status){ + $first_unoverridden_month = $month; + break; + } + } + + foreach ($months as $month): + ?> + + Overridden'; + } else { + $display_style = ($month === $first_unoverridden_month) ? '' : 'style="display: none;"'; + echo ''; + } + ?> + + + diff --git a/save_override.php b/save_override.php index 8afb9e4..6291136 100644 --- a/save_override.php +++ b/save_override.php @@ -73,7 +73,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { ':value' => $margin ]); - $response = ['success' => true, 'nsr' => $nsr, 'margin' => $margin]; + $response = [ + 'success' => true, + 'wip' => $wip, + 'openingBalance' => $opening_balance, + 'billings' => $billings, + 'expenses' => $expenses, + 'cost' => $cost, + 'nsr' => $nsr, + 'margin' => $margin + ]; } catch (PDOException $e) { $response['error'] = 'Database error: ' . $e->getMessage(); @@ -84,4 +93,5 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { } } -echo json_encode($response); \ No newline at end of file +echo json_encode($response); +exit; \ No newline at end of file