- users, SMTP and batch-lock endpoints are owner/admin only - engines answer 400/403/404/409/500 instead of 200 with an error body; database errors no longer leak to the client
62 lines
1.7 KiB
PHP
62 lines
1.7 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/OrderManager.php';
|
|
|
|
require_role($user_role, ['owner', 'admin', 'staff']);
|
|
|
|
$action = $data['action'] ?? '';
|
|
$data['items'] = json_decode($data['items'] ?? '[]', true) ?: [];
|
|
|
|
if (!in_array($action, ['create', 'update'], true)) {
|
|
http_response_code(400);
|
|
$answer['message'] = 'Unknown action';
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
if (empty($data['items'])) {
|
|
http_response_code(400);
|
|
$answer['message'] = 'At least one item is required';
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
try {
|
|
$new_id = null;
|
|
|
|
dbTransaction($pdo2, function($pdo) use ($data, $action, $company_id, $logging, &$new_id) {
|
|
$order = new OrderManager($pdo, $company_id);
|
|
|
|
if ($action === 'create') {
|
|
$data['source'] = 'revenue';
|
|
$data['source_id'] = 0;
|
|
} else {
|
|
$id = (int)($data['id'] ?? 0);
|
|
if (!$id) {
|
|
throw new Exception('Order ID required.');
|
|
}
|
|
|
|
$order->assertRevenueOrderEditable($id);
|
|
}
|
|
|
|
$new_id = $order->saveOrder($data, $logging);
|
|
});
|
|
|
|
$answer['success'] = 1;
|
|
$answer['message'] = $action === 'create' ? 'Sales order created' : 'Sales order updated';
|
|
if ($new_id) {
|
|
$answer['new_id'] = $new_id;
|
|
(new UsageGuard($pdo1, $company_id, $packages))->increment();
|
|
}
|
|
|
|
} 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));
|