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_ -
projectFinanceMonthly table.