Auto commit: 2025-11-27T11:39:02.361Z

This commit is contained in:
Flatlogic Bot 2025-11-27 11:39:02 +00:00
parent 982b6cfee6
commit 893b3b9bfa
3 changed files with 82 additions and 36 deletions

View File

@ -1,37 +1,31 @@
document.addEventListener('DOMContentLoaded', function () {
const overrideBtn = document.getElementById('override-btn');
const overrideBtns = document.querySelectorAll('.btn-override');
if (overrideBtn) {
overrideBtn.addEventListener('click', function () {
overrideBtns.forEach(btn => {
btn.addEventListener('click', function () {
const projectId = this.dataset.projectId;
const month = this.dataset.month;
if (this.textContent.trim() === 'Override') {
this.textContent = 'Save';
this.classList.remove('btn-warning');
this.classList.remove('btn-outline-primary');
this.classList.add('btn-success');
makeEditable('WIP', month);
makeEditable('Opening-Balance', month);
makeEditable('Opening Balance', month);
makeEditable('Billings', month);
makeEditable('Expenses', month);
makeEditable('Cost', month);
} else if (this.textContent.trim() === 'Save') {
const wip = document.getElementById(`wip-${month}-input`).value;
const openingBalance = document.getElementById(`opening-balance-${month}-input`).value;
const billings = document.getElementById(`billings-${month}-input`).value;
const expenses = document.getElementById(`expenses-${month}-input`).value;
const cost = document.getElementById(`cost-${month}-input`).value;
const formData = new FormData();
formData.append('projectId', projectId);
formData.append('month', month);
formData.append('wip', wip);
formData.append('openingBalance', openingBalance);
formData.append('billings', billings);
formData.append('expenses', expenses);
formData.append('cost', cost);
formData.append('month', month + '-01');
formData.append('wip', document.getElementById(`wip-${month}-input`).value);
formData.append('openingBalance', document.getElementById(`opening-balance-${month}-input`).value);
formData.append('billings', document.getElementById(`billings-${month}-input`).value);
formData.append('expenses', document.getElementById(`expenses-${month}-input`).value);
formData.append('cost', document.getElementById(`cost-${month}-input`).value);
fetch('save_override.php', {
method: 'POST',
@ -40,19 +34,26 @@ document.addEventListener('DOMContentLoaded', function () {
.then(response => response.json())
.then(data => {
if (data.success) {
this.textContent = 'Overridden';
this.classList.remove('btn-success');
this.classList.add('btn-secondary');
this.disabled = true;
// Replace button with "Overridden" badge
this.parentElement.innerHTML = '<span class="badge bg-success">Overridden</span>';
updateMetricCell('WIP', month, wip);
updateMetricCell('Opening-Balance', month, openingBalance);
updateMetricCell('Billings', month, billings);
updateMetricCell('Expenses', month, expenses);
updateMetricCell('Cost', month, cost);
updateMetricCell('WIP', month, data.wip);
updateMetricCell('Opening Balance', month, data.openingBalance);
updateMetricCell('Billings', month, data.billings);
updateMetricCell('Expenses', month, data.expenses);
updateMetricCell('Cost', month, data.cost);
updateMetricCell('NSR', month, data.nsr);
updateMetricCell('Margin', month, data.margin);
// Show the next override button
const nextButtonCell = this.parentElement.nextElementSibling;
if (nextButtonCell) {
const nextButton = nextButtonCell.querySelector('.btn-override');
if (nextButton) {
nextButton.style.display = 'block';
}
}
} else {
alert('Error saving override: ' + data.error);
}
@ -63,7 +64,7 @@ document.addEventListener('DOMContentLoaded', function () {
});
}
});
}
});
function makeEditable(metric, month) {
const cellId = `${metric.toLowerCase().replace(/\s/g, '-')}-${month}`;

View File

@ -268,9 +268,6 @@ if (!$project) {
foreach ($months as $month): ?>
<th>
<?php echo date('M Y', strtotime($month)); ?>
<?php if ($is_first_month): ?>
<button id="override-btn" class="btn btn-warning btn-sm mt-1" data-project-id="<?php echo $project_id; ?>" data-month="<?php echo $month; ?>">Override</button>
<?php endif; ?>
</th>
<?php
$is_first_month = false;
@ -284,12 +281,11 @@ if (!$project) {
<tr>
<td class="fw-bold bg-body-tertiary"><?php echo $metric; ?></td>
<?php
$is_first_month_for_body = true;
foreach ($months as $month):
$cell_id = '';
$refreshable_metrics = array_merge($editable_metrics, ['NSR', 'Margin']);
if ($is_first_month_for_body && in_array($metric, $refreshable_metrics)) {
$cell_id = 'id="' . str_replace(' ', '-', strtolower($metric)) . '-' . $month . '"';
if (in_array($metric, $refreshable_metrics)) {
$cell_id = 'id="' . str_replace(' ', '-', strtolower($metric)) . '-' . date('Y-m', strtotime($month)) . '"';
}
?>
<td class="text-end" <?php echo $cell_id; ?>>
@ -302,10 +298,49 @@ if (!$project) {
?>
</td>
<?php
$is_first_month_for_body = false;
endforeach; ?>
</tr>
<?php endforeach; ?>
<tr>
<td class="fw-bold bg-body-tertiary">Actions</td>
<?php
$override_status = [];
$override_check_stmt = $pdo->prepare("SELECT month, is_overridden FROM projectFinanceMonthly WHERE projectId = :pid GROUP BY month, is_overridden");
$override_check_stmt->execute([':pid' => $project_id]);
$statuses = $override_check_stmt->fetchAll(PDO::FETCH_ASSOC);
$monthly_override_status = [];
foreach($months as $month){
$monthly_override_status[$month] = 0;
}
foreach($statuses as $status){
if($status['is_overridden']){
$monthly_override_status[$status['month']] = 1;
}
}
$first_unoverridden_month = null;
foreach($monthly_override_status as $month => $status){
if(!$status){
$first_unoverridden_month = $month;
break;
}
}
foreach ($months as $month):
?>
<td class="text-center">
<?php
if ($monthly_override_status[$month]) {
echo '<span class="badge bg-success">Overridden</span>';
} else {
$display_style = ($month === $first_unoverridden_month) ? '' : 'style="display: none;"';
echo '<button class="btn btn-sm btn-outline-primary btn-override" data-month="' . date('Y-m', strtotime($month)) . '" id="override-btn-' . $month . '" ' . $display_style . ' data-project-id="' . $project_id . '">Override</button>';
}
?>
</td>
<?php endforeach; ?>
</tr>
</tbody>
</table>
</div>

View File

@ -73,7 +73,16 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
':value' => $margin
]);
$response = ['success' => true, 'nsr' => $nsr, 'margin' => $margin];
$response = [
'success' => true,
'wip' => $wip,
'openingBalance' => $opening_balance,
'billings' => $billings,
'expenses' => $expenses,
'cost' => $cost,
'nsr' => $nsr,
'margin' => $margin
];
} catch (PDOException $e) {
$response['error'] = 'Database error: ' . $e->getMessage();
@ -84,4 +93,5 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
}
}
echo json_encode($response);
echo json_encode($response);
exit;