From 2b9925f4fcf4984b67f488f790cabc523517ee25 Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Wed, 26 Nov 2025 16:27:58 +0000 Subject: [PATCH] overrride - works --- assets/js/project_details.js | 76 +++++++++++++++++++ ...5_create_project_finance_monthly_table.sql | 1 - ..._overridden_to_project_finance_monthly.sql | 4 + .../010_fix_project_finance_monthly_table.sql | 3 + project_details.php | 35 +++++++-- save_override.php | 49 ++++++++++++ 6 files changed, 160 insertions(+), 8 deletions(-) create mode 100644 assets/js/project_details.js create mode 100644 db/migrations/009_add_value_and_overridden_to_project_finance_monthly.sql create mode 100644 db/migrations/010_fix_project_finance_monthly_table.sql create mode 100644 save_override.php diff --git a/assets/js/project_details.js b/assets/js/project_details.js new file mode 100644 index 0000000..2046209 --- /dev/null +++ b/assets/js/project_details.js @@ -0,0 +1,76 @@ + +document.addEventListener('DOMContentLoaded', function () { + const overrideBtn = document.getElementById('override-btn'); + + if (overrideBtn) { + overrideBtn.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.add('btn-success'); + + makeEditable('WIP', month); + makeEditable('Opening-Balance', month); + makeEditable('Billings', month); + makeEditable('Expenses', 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 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); + + fetch('save_override.php', { + method: 'POST', + body: formData + }) + .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; + + updateCell('WIP', month, wip); + updateCell('Opening-Balance', month, openingBalance); + updateCell('Billings', month, billings); + updateCell('Expenses', month, expenses); + } else { + alert('Error saving override: ' + data.error); + } + }) + .catch(error => { + console.error('Error:', error); + alert('An unexpected error occurred.'); + }); + } + }); + } + + function makeEditable(metric, month) { + const cell = document.getElementById(`${metric.toLowerCase().replace(/\s/g, '-')}-${month}`); + if (cell) { + const value = cell.textContent.replace(/€/g, '').replace(/,/g, ''); + cell.innerHTML = ``; + } + } + + 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 })}`; + } + } +}); diff --git a/db/migrations/005_create_project_finance_monthly_table.sql b/db/migrations/005_create_project_finance_monthly_table.sql index e0d9ee7..10ea925 100644 --- a/db/migrations/005_create_project_finance_monthly_table.sql +++ b/db/migrations/005_create_project_finance_monthly_table.sql @@ -3,7 +3,6 @@ CREATE TABLE IF NOT EXISTS `projectFinanceMonthly` ( `projectId` INT NOT NULL, `metricName` VARCHAR(255) NOT NULL, `month` DATE NOT NULL, - `amount` DECIMAL(15, 2) NOT NULL, `createdAt` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, `updatedAt` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY (`projectId`) REFERENCES `projects`(`id`) ON DELETE CASCADE, diff --git a/db/migrations/009_add_value_and_overridden_to_project_finance_monthly.sql b/db/migrations/009_add_value_and_overridden_to_project_finance_monthly.sql new file mode 100644 index 0000000..2d86e84 --- /dev/null +++ b/db/migrations/009_add_value_and_overridden_to_project_finance_monthly.sql @@ -0,0 +1,4 @@ + +ALTER TABLE `projectFinanceMonthly` +ADD COLUMN `value` DECIMAL(15, 2) NOT NULL DEFAULT 0.00, +ADD COLUMN `is_overridden` BOOLEAN NOT NULL DEFAULT FALSE; diff --git a/db/migrations/010_fix_project_finance_monthly_table.sql b/db/migrations/010_fix_project_finance_monthly_table.sql new file mode 100644 index 0000000..2918ad7 --- /dev/null +++ b/db/migrations/010_fix_project_finance_monthly_table.sql @@ -0,0 +1,3 @@ +ALTER TABLE `projectFinanceMonthly` ADD COLUMN `metricName` VARCHAR(255) NOT NULL AFTER `projectId`; +ALTER TABLE `projectFinanceMonthly` ADD COLUMN `value` DECIMAL(15, 2) NOT NULL DEFAULT 0.00; +ALTER TABLE `projectFinanceMonthly` ADD COLUMN `is_overridden` BOOLEAN NOT NULL DEFAULT FALSE; \ No newline at end of file diff --git a/project_details.php b/project_details.php index 08fc482..3249c0e 100644 --- a/project_details.php +++ b/project_details.php @@ -236,17 +236,35 @@ if (!$project) { Metric - - - + + + + + + + + - + - - + + > - + @@ -268,5 +288,6 @@ if (!$project) { + \ No newline at end of file diff --git a/save_override.php b/save_override.php new file mode 100644 index 0000000..3cb3c72 --- /dev/null +++ b/save_override.php @@ -0,0 +1,49 @@ + 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, metricName, value, is_overridden) + VALUES (:pid, :m, :metric, :val, 1) + ON DUPLICATE KEY UPDATE value = :val, is_overridden = 1"; + + $stmt = $pdo->prepare($sql); + + foreach ($metrics as $metricName => $value) { + $processedValue = ($value === '' || $value === null) ? 0.00 : $value; + $stmt->execute([ + ':pid' => $project_id, + ':m' => $month, + ':metric' => $metricName, + ':val' => $processedValue + ]); + } + + $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); \ No newline at end of file