This commit is contained in:
Flatlogic Bot 2025-11-26 17:17:29 +00:00
parent c76337f9a8
commit 7a2b03ad4b
4 changed files with 57 additions and 40 deletions

View File

@ -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;

View File

@ -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'];
}
}

View File

@ -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_
</div>
</div>
<div class="alert alert-warning"><strong>Debug Mode:</strong> Showing all data from <code>projectFinanceMonthly</code> table.</div>
<?php if (isset($db_error)): ?>
<div class="alert alert-danger"><?php echo $db_error; ?></div>
@ -78,20 +78,14 @@ $page_title = $project_name ? "Financial Data for " . htmlspecialchars($project_
<?php
// Define user-friendly headers
$header_map = [
'metricName' => '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',
];

View File

@ -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];