- Upgraded all plain `require` to `require_once` across 172 api/engine and api/engine_report files to prevent class-redeclaration errors - Added issue button, issue_invoice() with GL toastr, and delete_invoice() to expense/manage_purchase_invoice.php, bringing it in line with po/manage_purchase_invoice.php - Added can_delete role guard (admin/owner only) to trash icons on revenue/invoice.php and expense/purchase_invoice.php, matching the existing pattern in finance/receipt.php and finance/payment.php Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
72 lines
2.4 KiB
PHP
72 lines
2.4 KiB
PHP
<?php
|
|
session_start();
|
|
require_once '../../../assets/utils/db_auth.php';
|
|
require_once '../../../assets/utils/classes/FileUploader.php';
|
|
require_once '../../../assets/utils/classes/ProductManager.php';
|
|
|
|
require_role($user_role, ['owner', 'admin']);
|
|
|
|
$id = (int)$data['id'];
|
|
$min_stock = (float)($data['min_stock'] ?? 0);
|
|
$reorder_point = (float)($data['reorder_point'] ?? 0);
|
|
|
|
if ($min_stock < 0 || $reorder_point < 0) {
|
|
$answer['success'] = 0;
|
|
$answer['message'] = 'Min stock and reorder point cannot be negative.';
|
|
http_response_code(400);
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
if ($min_stock > $reorder_point) {
|
|
$answer['success'] = 0;
|
|
$answer['message'] = "Min stock ({$min_stock}) must be less than or equal to reorder point ({$reorder_point}).";
|
|
http_response_code(400);
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
try {
|
|
/** 1. FILE HANDLING **/
|
|
$uploader = new FileUploader($include_url . 'uploads/product/');
|
|
|
|
if (!empty($_FILES['product_image']['name'])) {
|
|
if ($id > 0) {
|
|
$product = new ProductManager($pdo2, $company_id);
|
|
$uploader->cleanup($product->getProductImage($id), '');
|
|
}
|
|
|
|
$errors = $uploader->upload('product_image');
|
|
if (!empty($errors)) {
|
|
$answer['success'] = 0;
|
|
$answer['message'] = $errors[0];
|
|
exit(json_encode($answer));
|
|
}
|
|
|
|
$db_image_string = $uploader->getUploadedFiles()[0] ?? '';
|
|
} else {
|
|
$db_image_string = $data['keep_files'] ?? '';
|
|
}
|
|
|
|
/** 2. DATABASE **/
|
|
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging, $db_image_string) {
|
|
$product = new ProductManager($pdo, $company_id);
|
|
$product->saveProduct($data, $logging, $db_image_string);
|
|
});
|
|
|
|
$answer['success'] = 1;
|
|
|
|
} catch (PDOException $e) {
|
|
if (isset($uploader)) $uploader->rollbackUploads();
|
|
$answer['success'] = 0;
|
|
$answer['message'] = 'Database error, please try again';
|
|
http_response_code(500);
|
|
|
|
} catch (Exception $e) {
|
|
if (isset($uploader)) $uploader->rollbackUploads();
|
|
$answer['success'] = 0;
|
|
$answer['message'] = $e->getMessage();
|
|
http_response_code(400);
|
|
}
|
|
|
|
exit(json_encode($answer));
|
|
?>
|