over5
This commit is contained in:
parent
c76337f9a8
commit
7a2b03ad4b
34
db/migrations/011_restructure_project_finance_monthly.sql
Normal file
34
db/migrations/011_restructure_project_finance_monthly.sql
Normal 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;
|
||||||
@ -143,26 +143,16 @@ if ($project_id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 3. Fetch and apply overridden data
|
// 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]);
|
$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)) {
|
while ($row = $override_stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||||
$month = $row['month'];
|
$month = $row['month'];
|
||||||
$metricName = $row['metricName'];
|
if (in_array($month, $months)) {
|
||||||
$value = $row['value'];
|
$financial_data['WIP'][$month] = $row['wip'];
|
||||||
|
$financial_data['Opening Balance'][$month] = $row['opening_balance'];
|
||||||
if (isset($metric_map[$metricName])) {
|
$financial_data['Billings'][$month] = $row['billing'];
|
||||||
$display_metric = $metric_map[$metricName];
|
$financial_data['Expenses'][$month] = $row['expenses'];
|
||||||
if (isset($financial_data[$display_metric][$month])) {
|
|
||||||
$financial_data[$display_metric][$month] = $value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -15,8 +15,8 @@ if ($project_id) {
|
|||||||
$project_name = $stmt->fetchColumn();
|
$project_name = $stmt->fetchColumn();
|
||||||
|
|
||||||
// Fetch financial data
|
// Fetch financial data
|
||||||
$stmt = $pdo->prepare("SELECT * FROM projectFinanceMonthly");
|
$stmt = $pdo->prepare("SELECT * FROM projectFinanceMonthly WHERE projectId = :pid ORDER BY month");
|
||||||
$stmt->execute();
|
$stmt->execute([':pid' => $project_id]);
|
||||||
$financial_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
$financial_data = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
} catch (PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
@ -60,7 +60,7 @@ $page_title = $project_name ? "Financial Data for " . htmlspecialchars($project_
|
|||||||
</div>
|
</div>
|
||||||
</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)): ?>
|
<?php if (isset($db_error)): ?>
|
||||||
<div class="alert alert-danger"><?php echo $db_error; ?></div>
|
<div class="alert alert-danger"><?php echo $db_error; ?></div>
|
||||||
@ -78,20 +78,14 @@ $page_title = $project_name ? "Financial Data for " . htmlspecialchars($project_
|
|||||||
<?php
|
<?php
|
||||||
// Define user-friendly headers
|
// Define user-friendly headers
|
||||||
$header_map = [
|
$header_map = [
|
||||||
'metricName' => 'Metric',
|
|
||||||
'month' => 'Month',
|
'month' => 'Month',
|
||||||
'opening_balance' => 'Opening Balance',
|
|
||||||
'payment' => 'Payment',
|
|
||||||
'wip' => 'WIP',
|
'wip' => 'WIP',
|
||||||
|
'opening_balance' => 'Opening Balance',
|
||||||
|
'billing' => 'Billing',
|
||||||
'expenses' => 'Expenses',
|
'expenses' => 'Expenses',
|
||||||
'cost' => 'Cost',
|
'is_overridden' => 'Overridden',
|
||||||
'nsr' => 'NSR',
|
|
||||||
'margin' => 'Margin',
|
|
||||||
'is_confirmed' => 'Confirmed',
|
|
||||||
'createdAt' => 'Created At',
|
'createdAt' => 'Created At',
|
||||||
'updatedAt' => 'Updated At',
|
'updatedAt' => 'Updated At',
|
||||||
'value' => 'Value',
|
|
||||||
'is_overridden' => 'Overridden',
|
|
||||||
'id' => 'ID',
|
'id' => 'ID',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@ -19,21 +19,20 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||||||
if ($project_id && $month && !in_array(null, $metrics, true)) {
|
if ($project_id && $month && !in_array(null, $metrics, true)) {
|
||||||
try {
|
try {
|
||||||
$pdo = db();
|
$pdo = db();
|
||||||
$sql = "INSERT INTO projectFinanceMonthly (projectId, month, metricName, value, is_overridden)
|
$sql = "INSERT INTO projectFinanceMonthly (projectId, month, wip, opening_balance, billing, expenses, is_overridden)
|
||||||
VALUES (:pid, :m, :metric, :val, 1)
|
VALUES (:pid, :m, :wip, :ob, :bill, :exp, 1)
|
||||||
ON DUPLICATE KEY UPDATE value = :val, is_overridden = 1";
|
ON DUPLICATE KEY UPDATE wip = :wip, opening_balance = :ob, billing = :bill, expenses = :exp, is_overridden = 1";
|
||||||
|
|
||||||
$stmt = $pdo->prepare($sql);
|
$stmt = $pdo->prepare($sql);
|
||||||
|
|
||||||
foreach ($metrics as $metricName => $value) {
|
|
||||||
$processedValue = ($value === '' || $value === null) ? 0.00 : $value;
|
|
||||||
$stmt->execute([
|
$stmt->execute([
|
||||||
':pid' => $project_id,
|
':pid' => $project_id,
|
||||||
':m' => $month,
|
':m' => $month,
|
||||||
':metric' => $metricName,
|
':wip' => $metrics['wip'],
|
||||||
':val' => $processedValue
|
':ob' => $metrics['opening_balance'],
|
||||||
|
':bill' => $metrics['billings'],
|
||||||
|
':exp' => $metrics['expenses']
|
||||||
]);
|
]);
|
||||||
}
|
|
||||||
|
|
||||||
$response = ['success' => true];
|
$response = ['success' => true];
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user