36293-vm/save_override.php
2025-11-27 10:58:53 +00:00

57 lines
1.8 KiB
PHP

<?php
require_once __DIR__ . '/db/config.php';
header('Content-Type: application/json');
$response = ['success' => false, 'error' => 'Invalid request'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$project_id = $_POST['projectId'] ?? null;
$month = $_POST['month'] ?? null;
$metrics = [
'wip' => $_POST['wip'] ?? null,
'opening_balance' => $_POST['openingBalance'] ?? null,
'billings' => $_POST['billings'] ?? null,
'expenses' => $_POST['expenses'] ?? null,
];
if ($project_id && $month && !in_array(null, $metrics, true)) {
try {
$pdo = db();
$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,
':metric' => $metricName,
':value' => $value
]);
}
}
$response = ['success' => true];
} catch (PDOException $e) {
$response['error'] = 'Database error: ' . $e->getMessage();
error_log($e->getMessage());
}
} else {
$response['error'] = 'Missing required fields.';
}
}
echo json_encode($response);