45 lines
1.5 KiB
PHP
45 lines
1.5 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../../../session.php';
|
|
require_once '../../../assets/utils/db_auth.php';
|
|
require_role($user_role, ['owner', 'admin', 'staff']);
|
|
require_once '../../../assets/utils/classes/UsageGuard.php';
|
|
require_once '../../../assets/utils/classes/InvoiceManager.php';
|
|
require_once '../../../assets/utils/notify_node.php';
|
|
|
|
try {
|
|
$action = $data['action'] ?? 'update';
|
|
|
|
if ($action === 'save_formula') {
|
|
$id = (int)($data['id'] ?? 0);
|
|
$formula_id = isset($data['formula_id']) && (int)$data['formula_id'] > 0
|
|
? (int)$data['formula_id'] : null;
|
|
$inv = new InvoiceManager($pdo2, $company_id);
|
|
$inv->saveFormula($id, $formula_id);
|
|
$answer['success'] = 1;
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
$is_new = !(int)($data['id'] ?? 0);
|
|
|
|
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging) {
|
|
$inv = new InvoiceManager($pdo, $company_id);
|
|
$inv->saveInvoice($data, $logging);
|
|
});
|
|
|
|
$answer['success'] = 1;
|
|
if ($is_new) {
|
|
(new UsageGuard($pdo1, $company_id, $packages))->increment();
|
|
}
|
|
notify_node('invoice_updated', [], $company_id);
|
|
|
|
} 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));
|
|
?>
|