This commit is contained in:
Flatlogic Bot 2025-11-26 10:17:48 +00:00
parent 8cc108c2d6
commit 8be23f0e45
5 changed files with 50 additions and 24 deletions

View File

@ -67,6 +67,7 @@ document.addEventListener('DOMContentLoaded', function () {
document.getElementById('view-totalAnnualCost').value = '€' + parseFloat(data.totalAnnualCost).toFixed(2); document.getElementById('view-totalAnnualCost').value = '€' + parseFloat(data.totalAnnualCost).toFixed(2);
document.getElementById('view-grossRevenue').value = '€' + parseFloat(data.grossRevenue).toFixed(2); document.getElementById('view-grossRevenue').value = '€' + parseFloat(data.grossRevenue).toFixed(2);
document.getElementById('view-discountedRevenue').value = '€' + parseFloat(data.discountedRevenue).toFixed(2); document.getElementById('view-discountedRevenue').value = '€' + parseFloat(data.discountedRevenue).toFixed(2);
document.getElementById('view-dailyCost').value = '€' + (parseFloat(data.totalMonthlyCost) / 20).toFixed(2);
viewResourceModal.show(); viewResourceModal.show();
}); });

View File

@ -21,12 +21,21 @@ $output = fopen('php://output', 'w');
// Add headers // Add headers
if (!empty($roster_data)) { if (!empty($roster_data)) {
fputcsv($output, array_keys($roster_data[0])); $roster_data_with_daily_cost = [];
} foreach ($roster_data as $row) {
$row['dailyCost'] = $row['totalMonthlyCost'] / 20;
// Add data $roster_data_with_daily_cost[] = $row;
foreach ($roster_data as $row) { }
fputcsv($output, $row); fputcsv($output, array_keys($roster_data_with_daily_cost[0]));
foreach ($roster_data_with_daily_cost as $row) {
fputcsv($output, $row);
}
} else {
// Add headers even if there is no data
$stmt = $pdo->query("SHOW COLUMNS FROM roster");
$columns = $stmt->fetchAll(PDO::FETCH_COLUMN);
$columns[] = 'dailyCost';
fputcsv($output, $columns);
} }
fclose($output); fclose($output);

View File

@ -50,7 +50,7 @@ try {
$fid = $latest_forecast['id']; $fid = $latest_forecast['id'];
// Base Monthly Cost // Base Monthly Cost
$cost_sql = "SELECT fa.month, SUM(fa.allocatedDays * r.totalMonthlyCost) 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)) 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);
@ -69,27 +69,29 @@ try {
// 2. Calculate cumulative values month by month // 2. Calculate cumulative values month by month
$cumulative_billing = 0; $cumulative_billing = 0;
$cumulative_wip = 0;
$cumulative_cost = 0; $cumulative_cost = 0;
$previous_month_wip = 0;
foreach ($months as $month) { foreach ($months as $month) {
// Normalize month keys from fetched data // Normalize month keys from fetched data
$cost = $monthly_costs[$month] ?? 0; $cost = $monthly_costs[$month] ?? 0;
$wip = $monthly_wip[$month] ?? 0; $base_monthly_wip = $monthly_wip[$month] ?? 0;
$billing = $monthly_billing[$month] ?? 0; $billing = $monthly_billing[$month] ?? 0;
$expenses = 0; // Placeholder for expenses
// Cumulative calculations // Cumulative calculations
$cumulative_billing += $billing; $cumulative_billing += $billing;
$cumulative_wip += $wip;
$cumulative_cost += $cost; $cumulative_cost += $cost;
// WIP Calculation (new formula)
// current month WIP = previous month WIP + Month Expenses + base_monthly_wip - month Billing
$current_wip = $previous_month_wip + $expenses + $base_monthly_wip - $billing;
$financial_data['WIP'][$month] = $current_wip;
$financial_data['Billings'][$month] = $cumulative_billing; $financial_data['Billings'][$month] = $cumulative_billing;
$financial_data['WIP'][$month] = $cumulative_wip;
$financial_data['Cost'][$month] = $cumulative_cost; $financial_data['Cost'][$month] = $cumulative_cost;
$financial_data['Opening Balance'][$month] = 0; // Placeholder
// Other metrics are 0 for now $financial_data['Expenses'][$month] = $expenses;
$financial_data['Opening Balance'][$month] = 0;
$financial_data['Expenses'][$month] = 0;
// Calculated metrics (NSR and Margin) // Calculated metrics (NSR and Margin)
$nsr = $financial_data['WIP'][$month] + $financial_data['Billings'][$month] - $financial_data['Opening Balance'][$month] - $financial_data['Expenses'][$month]; $nsr = $financial_data['WIP'][$month] + $financial_data['Billings'][$month] - $financial_data['Opening Balance'][$month] - $financial_data['Expenses'][$month];
@ -97,6 +99,9 @@ try {
$margin = ($nsr != 0) ? (($nsr - $financial_data['Cost'][$month]) / $nsr) : 0; $margin = ($nsr != 0) ? (($nsr - $financial_data['Cost'][$month]) / $nsr) : 0;
$financial_data['Margin'][$month] = $margin; $financial_data['Margin'][$month] = $margin;
// Update previous month's WIP for the next iteration
$previous_month_wip = $current_wip;
} }
} }
} catch (PDOException $e) { } catch (PDOException $e) {

View File

@ -356,6 +356,7 @@ try {
<th>Total Annual Cost</th> <th>Total Annual Cost</th>
<th>Gross Revenue</th> <th>Gross Revenue</th>
<th>Discounted Revenue</th> <th>Discounted Revenue</th>
<th>Daily Cost</th>
<th>Actions</th> <th>Actions</th>
</tr> </tr>
</thead> </thead>
@ -384,6 +385,7 @@ try {
<td>&euro;<?php echo number_format($row['totalAnnualCost'], 2); ?></td> <td>&euro;<?php echo number_format($row['totalAnnualCost'], 2); ?></td>
<td>&euro;<?php echo number_format($row['grossRevenue'], 2); ?></td> <td>&euro;<?php echo number_format($row['grossRevenue'], 2); ?></td>
<td>&euro;<?php echo number_format($row['discountedRevenue'], 2); ?></td> <td>&euro;<?php echo number_format($row['discountedRevenue'], 2); ?></td>
<td>&euro;<?php echo number_format($row['totalMonthlyCost'] / 20, 2); ?></td>
<td> <td>
<div class="d-flex"> <div class="d-flex">
<button class="btn btn-sm btn-outline-info me-2 view-btn" <button class="btn btn-sm btn-outline-info me-2 view-btn"
@ -651,6 +653,10 @@ try {
<label class="form-label">Discounted Revenue (&euro;)</label> <label class="form-label">Discounted Revenue (&euro;)</label>
<input type="text" class="form-control" id="view-discountedRevenue" readonly> <input type="text" class="form-control" id="view-discountedRevenue" readonly>
</div> </div>
<div class="col-md-6 mb-3">
<label class="form-label">Daily Cost (&euro;)</label>
<input type="text" class="form-control" id="view-dailyCost" readonly>
</div>
</div> </div>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">

View File

@ -81,7 +81,7 @@ 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) 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)) 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);
@ -100,27 +100,29 @@ if ($project_id) {
// 2. Calculate cumulative values month by month // 2. Calculate cumulative values month by month
$cumulative_billing = 0; $cumulative_billing = 0;
$cumulative_wip = 0;
$cumulative_cost = 0; $cumulative_cost = 0;
$previous_month_wip = 0;
foreach ($months as $month) { foreach ($months as $month) {
// Normalize month keys from fetched data // Normalize month keys from fetched data
$cost = $monthly_costs[$month] ?? 0; $cost = $monthly_costs[$month] ?? 0;
$wip = $monthly_wip[$month] ?? 0; $base_monthly_wip = $monthly_wip[$month] ?? 0;
$billing = $monthly_billing[$month] ?? 0; $billing = $monthly_billing[$month] ?? 0;
$expenses = 0; // Placeholder for expenses
// Cumulative calculations // Cumulative calculations
$cumulative_billing += $billing; $cumulative_billing += $billing;
$cumulative_wip += $wip;
$cumulative_cost += $cost; $cumulative_cost += $cost;
// WIP Calculation (new formula)
// current month WIP = previous month WIP + Month Expenses + base_monthly_wip - month Billing
$current_wip = $previous_month_wip + $expenses + $base_monthly_wip - $billing;
$financial_data['WIP'][$month] = $current_wip;
$financial_data['Billings'][$month] = $cumulative_billing; $financial_data['Billings'][$month] = $cumulative_billing;
$financial_data['WIP'][$month] = $cumulative_wip;
$financial_data['Cost'][$month] = $cumulative_cost; $financial_data['Cost'][$month] = $cumulative_cost;
$financial_data['Opening Balance'][$month] = 0; // Placeholder
// Other metrics are 0 for now $financial_data['Expenses'][$month] = $expenses;
$financial_data['Opening Balance'][$month] = 0;
$financial_data['Expenses'][$month] = 0;
// Calculated metrics (NSR and Margin) // Calculated metrics (NSR and Margin)
$nsr = $financial_data['WIP'][$month] + $financial_data['Billings'][$month] - $financial_data['Opening Balance'][$month] - $financial_data['Expenses'][$month]; $nsr = $financial_data['WIP'][$month] + $financial_data['Billings'][$month] - $financial_data['Opening Balance'][$month] - $financial_data['Expenses'][$month];
@ -128,6 +130,9 @@ if ($project_id) {
$margin = ($nsr != 0) ? (($nsr - $financial_data['Cost'][$month]) / $nsr) : 0; $margin = ($nsr != 0) ? (($nsr - $financial_data['Cost'][$month]) / $nsr) : 0;
$financial_data['Margin'][$month] = $margin; $financial_data['Margin'][$month] = $margin;
// Update previous month's WIP for the next iteration
$previous_month_wip = $current_wip;
} }
} }
} }