From 7a2b03ad4bbd3555ee5b616d0aece8f00a5ed1ce Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Wed, 26 Nov 2025 17:17:29 +0000 Subject: [PATCH] over5 --- ...11_restructure_project_finance_monthly.sql | 34 +++++++++++++++++++ project_details.php | 22 ++++-------- project_finance_details.php | 18 ++++------ save_override.php | 23 ++++++------- 4 files changed, 57 insertions(+), 40 deletions(-) create mode 100644 db/migrations/011_restructure_project_finance_monthly.sql diff --git a/db/migrations/011_restructure_project_finance_monthly.sql b/db/migrations/011_restructure_project_finance_monthly.sql new file mode 100644 index 0000000..b4319ed --- /dev/null +++ b/db/migrations/011_restructure_project_finance_monthly.sql @@ -0,0 +1,34 @@ +-- Create a temporary table with the new structure +CREATE TABLE `projectFinanceMonthly_new` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `projectId` INT NOT NULL, + `month` DATE NOT NULL, + `wip` DECIMAL(15, 2) DEFAULT 0.00, + `opening_balance` DECIMAL(15, 2) DEFAULT 0.00, + `billing` DECIMAL(15, 2) DEFAULT 0.00, + `expenses` DECIMAL(15, 2) DEFAULT 0.00, + `is_overridden` BOOLEAN NOT NULL DEFAULT FALSE, + `createdAt` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + `updatedAt` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + FOREIGN KEY (`projectId`) REFERENCES `projects`(`id`) ON DELETE CASCADE, + UNIQUE KEY `project_month` (`projectId`, `month`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +-- Migrate the data +INSERT INTO `projectFinanceMonthly_new` (projectId, month, wip, opening_balance, billing, expenses, is_overridden) +SELECT + projectId, + 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, + MAX(is_overridden) as is_overridden +FROM projectFinanceMonthly +GROUP BY projectId, month; + +-- Drop the old table +DROP TABLE projectFinanceMonthly; + +-- Rename the new table +RENAME TABLE projectFinanceMonthly_new TO projectFinanceMonthly; diff --git a/project_details.php b/project_details.php index d6fa67f..1d4231a 100644 --- a/project_details.php +++ b/project_details.php @@ -143,26 +143,16 @@ if ($project_id) { } // 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 = $pdo->prepare("SELECT month, wip, opening_balance, billing, expenses 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; - } + if (in_array($month, $months)) { + $financial_data['WIP'][$month] = $row['wip']; + $financial_data['Opening Balance'][$month] = $row['opening_balance']; + $financial_data['Billings'][$month] = $row['billing']; + $financial_data['Expenses'][$month] = $row['expenses']; } } diff --git a/project_finance_details.php b/project_finance_details.php index 7555ee3..3116b09 100644 --- a/project_finance_details.php +++ b/project_finance_details.php @@ -15,8 +15,8 @@ if ($project_id) { $project_name = $stmt->fetchColumn(); // Fetch financial data - $stmt = $pdo->prepare("SELECT * FROM projectFinanceMonthly"); - $stmt->execute(); + $stmt = $pdo->prepare("SELECT * FROM projectFinanceMonthly WHERE projectId = :pid ORDER BY month"); + $stmt->execute([':pid' => $project_id]); $financial_data = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { @@ -60,7 +60,7 @@ $page_title = $project_name ? "Financial Data for " . htmlspecialchars($project_ -
Debug Mode: Showing all data from projectFinanceMonthly table.
+
@@ -78,20 +78,14 @@ $page_title = $project_name ? "Financial Data for " . htmlspecialchars($project_ 'Metric', 'month' => 'Month', - 'opening_balance' => 'Opening Balance', - 'payment' => 'Payment', 'wip' => 'WIP', + 'opening_balance' => 'Opening Balance', + 'billing' => 'Billing', 'expenses' => 'Expenses', - 'cost' => 'Cost', - 'nsr' => 'NSR', - 'margin' => 'Margin', - 'is_confirmed' => 'Confirmed', + 'is_overridden' => 'Overridden', 'createdAt' => 'Created At', 'updatedAt' => 'Updated At', - 'value' => 'Value', - 'is_overridden' => 'Overridden', 'id' => 'ID', ]; diff --git a/save_override.php b/save_override.php index 3cb3c72..ca0ef68 100644 --- a/save_override.php +++ b/save_override.php @@ -19,21 +19,20 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') { 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"; + $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); - foreach ($metrics as $metricName => $value) { - $processedValue = ($value === '' || $value === null) ? 0.00 : $value; - $stmt->execute([ - ':pid' => $project_id, - ':m' => $month, - ':metric' => $metricName, - ':val' => $processedValue - ]); - } + $stmt->execute([ + ':pid' => $project_id, + ':m' => $month, + ':wip' => $metrics['wip'], + ':ob' => $metrics['opening_balance'], + ':bill' => $metrics['billings'], + ':exp' => $metrics['expenses'] + ]); $response = ['success' => true];