39 lines
1.5 KiB
PHP
39 lines
1.5 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../../../session.php';
|
|
require_once '../../../assets/utils/db_auth.php';
|
|
require_once '../../../assets/utils/classes/UsageGuard.php';
|
|
require_once '../../../assets/utils/classes/QuotationManager.php';
|
|
|
|
$action = $data['action'] ?? '';
|
|
$id = (int)($data['id'] ?? 0);
|
|
$items = json_decode($data['items'] ?? '[]', true) ?: [];
|
|
|
|
$qm = new QuotationManager($pdo2, $company_id);
|
|
|
|
try {
|
|
if ($action === 'create') {
|
|
require_role($user_role, ['owner', 'admin', 'staff']);
|
|
// One transaction: a failure while writing the lines must not leave the header behind.
|
|
$new_id = dbTransaction($pdo2, fn() => $qm->save(array_merge($data, ['id' => 0, 'items' => $items]), $logging));
|
|
$answer['success'] = 1;
|
|
$answer['message'] = 'Quotation created.';
|
|
$answer['new_id'] = $new_id;
|
|
(new UsageGuard($pdo1, $company_id, $packages))->increment();
|
|
} elseif ($action === 'update') {
|
|
require_role($user_role, ['owner', 'admin', 'staff']);
|
|
dbTransaction($pdo2, fn() => $qm->save(array_merge($data, ['id' => $id, 'items' => $items]), $logging));
|
|
$answer['success'] = 1;
|
|
$answer['message'] = 'Quotation updated.';
|
|
} else {
|
|
throw new Exception('Unknown action.');
|
|
}
|
|
} catch (PDOException $e) {
|
|
$answer['message'] = 'Database error, please try again.';
|
|
http_response_code(500);
|
|
} catch (Exception $e) {
|
|
$answer['message'] = $e->getMessage();
|
|
http_response_code(400);
|
|
}
|
|
|
|
exit(json_encode($answer));
|