36293-vm/save_billing.php
2025-11-27 12:16:59 +00:00

48 lines
1.6 KiB
PHP

<?php
header('Content-Type: application/json');
require_once __DIR__ . '/db/config.php';
$response = ['success' => false, 'error' => 'Invalid request'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$projectId = $_POST['projectId'] ?? null;
$billingDataJson = $_POST['billingData'] ?? null;
if ($projectId && $billingDataJson) {
$billingData = json_decode($billingDataJson, true);
if (is_array($billingData)) {
try {
$pdo = db();
$stmt = $pdo->prepare("UPDATE billingMonthly SET amount = :amount WHERE projectId = :projectId AND month = :month");
$pdo->beginTransaction();
foreach ($billingData as $item) {
$stmt->execute([
':amount' => $item['amount'],
':projectId' => $projectId,
':month' => $item['month']
]);
}
$pdo->commit();
$response['success'] = true;
unset($response['error']);
} catch (PDOException $e) {
if ($pdo->inTransaction()) {
$pdo->rollBack();
}
$response['error'] = 'Database error: ' . $e->getMessage();
} catch (Exception $e) {
$response['error'] = 'An unexpected error occurred: ' . $e->getMessage();
}
} else {
$response['error'] = 'Invalid billing data format.';
}
} else {
$response['error'] = 'Missing required fields.';
}
}
echo json_encode($response);