Auto commit: 2025-11-27T10:58:53.677Z

This commit is contained in:
Flatlogic Bot 2025-11-27 10:58:53 +00:00
parent 7a2b03ad4b
commit 3965ab4f7a
2 changed files with 24 additions and 15 deletions

View File

@ -81,13 +81,13 @@ if ($project_id) {
$fid = $latest_forecast['id']; $fid = $latest_forecast['id'];
// Base Monthly Cost // Base Monthly Cost
$cost_sql = "SELECT fa.month, SUM(fa.allocatedDays * (r.totalMonthlyCost / 20)) FROM forecastAllocation fa JOIN roster r ON fa.rosterId = r.id WHERE fa.forecastingId = :fid GROUP BY fa.month"; $cost_sql = "SELECT fa.month, SUM(fa.allocatedDays * (r.totalMonthlyCost / 20)) as cost FROM forecastAllocation fa JOIN roster r ON fa.rosterId = r.id WHERE fa.forecastingId = :fid GROUP BY fa.month";
$cost_stmt = $pdo->prepare($cost_sql); $cost_stmt = $pdo->prepare($cost_sql);
$cost_stmt->execute([':fid' => $fid]); $cost_stmt->execute([':fid' => $fid]);
$monthly_costs = $cost_stmt->fetchAll(PDO::FETCH_KEY_PAIR); $monthly_costs = $cost_stmt->fetchAll(PDO::FETCH_KEY_PAIR);
// Base Monthly WIP // Base Monthly WIP
$wip_sql = "SELECT fa.month, SUM(fa.allocatedDays * r.discountedRevenue) FROM forecastAllocation fa JOIN roster r ON fa.rosterId = r.id WHERE fa.forecastingId = :fid GROUP BY fa.month"; $wip_sql = "SELECT fa.month, SUM(fa.allocatedDays * r.discountedRevenue) as wip FROM forecastAllocation fa JOIN roster r ON fa.rosterId = r.id WHERE fa.forecastingId = :fid GROUP BY fa.month";
$wip_stmt = $pdo->prepare($wip_sql); $wip_stmt = $pdo->prepare($wip_sql);
$wip_stmt->execute([':fid' => $fid]); $wip_stmt->execute([':fid' => $fid]);
$monthly_wip = $wip_stmt->fetchAll(PDO::FETCH_KEY_PAIR); $monthly_wip = $wip_stmt->fetchAll(PDO::FETCH_KEY_PAIR);
@ -143,7 +143,7 @@ if ($project_id) {
} }
// 3. Fetch and apply overridden data // 3. Fetch and apply overridden data
$override_stmt = $pdo->prepare("SELECT month, wip, opening_balance, billing, expenses FROM projectFinanceMonthly WHERE projectId = :pid AND is_overridden = 1"); $override_stmt = $pdo->prepare("SELECT 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 FROM projectFinanceMonthly WHERE projectId = :pid AND is_overridden = 1 GROUP BY month");
$override_stmt->execute([':pid' => $project_id]); $override_stmt->execute([':pid' => $project_id]);
while ($row = $override_stmt->fetch(PDO::FETCH_ASSOC)) { while ($row = $override_stmt->fetch(PDO::FETCH_ASSOC)) {

View File

@ -19,20 +19,29 @@ 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, wip, opening_balance, billing, expenses, is_overridden) $sql = "INSERT INTO projectFinanceMonthly (projectId, month, metricName, value, is_overridden)
VALUES (:pid, :m, :wip, :ob, :bill, :exp, 1) VALUES (:pid, :m, :metric, :value, 1)
ON DUPLICATE KEY UPDATE wip = :wip, opening_balance = :ob, billing = :bill, expenses = :exp, is_overridden = 1"; ON DUPLICATE KEY UPDATE value = :value, is_overridden = 1";
$stmt = $pdo->prepare($sql); $stmt = $pdo->prepare($sql);
$metric_map = [
'wip' => 'wip',
'opening_balance' => 'opening_balance',
'billings' => 'billing',
'expenses' => 'expenses'
];
foreach ($metrics as $metricKey => $value) {
if (isset($metric_map[$metricKey])) {
$metricName = $metric_map[$metricKey];
$stmt->execute([ $stmt->execute([
':pid' => $project_id, ':pid' => $project_id,
':m' => $month, ':m' => $month,
':wip' => $metrics['wip'], ':metric' => $metricName,
':ob' => $metrics['opening_balance'], ':value' => $value
':bill' => $metrics['billings'],
':exp' => $metrics['expenses']
]); ]);
}
}
$response = ['success' => true]; $response = ['success' => true];