Auto commit: 2025-11-27T10:58:53.677Z
This commit is contained in:
parent
7a2b03ad4b
commit
3965ab4f7a
@ -81,13 +81,13 @@ if ($project_id) {
|
||||
$fid = $latest_forecast['id'];
|
||||
|
||||
// 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->execute([':fid' => $fid]);
|
||||
$monthly_costs = $cost_stmt->fetchAll(PDO::FETCH_KEY_PAIR);
|
||||
|
||||
// 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->execute([':fid' => $fid]);
|
||||
$monthly_wip = $wip_stmt->fetchAll(PDO::FETCH_KEY_PAIR);
|
||||
@ -143,7 +143,7 @@ if ($project_id) {
|
||||
}
|
||||
|
||||
// 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]);
|
||||
|
||||
while ($row = $override_stmt->fetch(PDO::FETCH_ASSOC)) {
|
||||
|
||||
@ -19,20 +19,29 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
if ($project_id && $month && !in_array(null, $metrics, true)) {
|
||||
try {
|
||||
$pdo = db();
|
||||
$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";
|
||||
|
||||
$sql = "INSERT INTO projectFinanceMonthly (projectId, month, metricName, value, is_overridden)
|
||||
VALUES (:pid, :m, :metric, :value, 1)
|
||||
ON DUPLICATE KEY UPDATE value = :value, is_overridden = 1";
|
||||
$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([
|
||||
':pid' => $project_id,
|
||||
':m' => $month,
|
||||
':wip' => $metrics['wip'],
|
||||
':ob' => $metrics['opening_balance'],
|
||||
':bill' => $metrics['billings'],
|
||||
':exp' => $metrics['expenses']
|
||||
':metric' => $metricName,
|
||||
':value' => $value
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$response = ['success' => true];
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user