remove actions and data integrity
This commit is contained in:
@@ -1,69 +1,18 @@
|
||||
<?php
|
||||
<?php
|
||||
session_start();
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
require '../../../assets/utils/classes/ProductManager.php';
|
||||
|
||||
try {
|
||||
$result = dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging) {
|
||||
|
||||
$id = (int)($data['id'] ?? 0);
|
||||
dbTransaction($pdo2, function($pdo) use ($data, $company_id) {
|
||||
|
||||
if ($id <= 0) {
|
||||
throw new Exception("Invalid category ID.");
|
||||
}
|
||||
$productMgmt = new ProductManager($pdo, $company_id);
|
||||
$productMgmt->deleteCategory((int)$data['id']);
|
||||
|
||||
// 1. Validate: check if category is in use by any product
|
||||
$sql = "SELECT COUNT(*)
|
||||
FROM md_product
|
||||
WHERE company_id = :company_id
|
||||
AND category = :id";
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->execute([
|
||||
":company_id" => $company_id,
|
||||
":id" => $id
|
||||
]);
|
||||
|
||||
$product_count = (int) $sth->fetchColumn();
|
||||
|
||||
if ($product_count > 0) {
|
||||
throw new Exception("Cannot remove category. It is currently used by {$product_count} product(s).");
|
||||
}
|
||||
|
||||
// 2. Append to log history
|
||||
$sql = "SELECT `log` FROM md_product_category
|
||||
WHERE company_id = :company_id AND id = :id";
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->execute([
|
||||
":company_id" => $company_id,
|
||||
":id" => $id
|
||||
]);
|
||||
|
||||
$table_log = json_decode($sth->fetchColumn() ?: '[]', true);
|
||||
if (!is_array($table_log)) $table_log = [];
|
||||
$table_log[] = $logging;
|
||||
|
||||
// 3. Soft-delete via sign flip
|
||||
$sql = "UPDATE md_product_category
|
||||
SET company_id = company_id * (-1),
|
||||
`log` = :log
|
||||
WHERE company_id = :company_id
|
||||
AND id = :id";
|
||||
$sth = $pdo->prepare($sql);
|
||||
$sth->execute([
|
||||
":company_id" => $company_id,
|
||||
":id" => $id,
|
||||
":log" => json_encode($table_log, JSON_UNESCAPED_UNICODE),
|
||||
]);
|
||||
|
||||
if ($sth->rowCount() === 0) {
|
||||
throw new Exception("Category not found or already removed.");
|
||||
}
|
||||
|
||||
return $id;
|
||||
});
|
||||
|
||||
$answer["success"] = 1;
|
||||
$answer["message"] = "Category removed successfully.";
|
||||
$answer["id"] = $result;
|
||||
|
||||
} catch (PDOException $e) {
|
||||
$answer["success"] = 0;
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
require '../../../assets/utils/classes/ProductManager.php';
|
||||
|
||||
try {
|
||||
|
||||
dbTransaction($pdo2, function($pdo) use ($data, $company_id) {
|
||||
|
||||
$productMgmt = new ProductManager($pdo, $company_id);
|
||||
$productMgmt->deleteProduct((int)$data['id']);
|
||||
|
||||
});
|
||||
|
||||
$answer["success"] = 1;
|
||||
|
||||
} catch (PDOException $e) {
|
||||
$answer["success"] = 0;
|
||||
$answer["message"] = "Database error, please try again";
|
||||
http_response_code(500);
|
||||
|
||||
} catch (Exception $e) {
|
||||
$answer["success"] = 0;
|
||||
$answer["message"] = $e->getMessage();
|
||||
http_response_code(400);
|
||||
}
|
||||
|
||||
exit(json_encode($answer));
|
||||
?>
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
require '../../../assets/utils/classes/WarehouseManager.php';
|
||||
|
||||
try {
|
||||
|
||||
dbTransaction($pdo2, function($pdo) use ($data, $company_id) {
|
||||
|
||||
$whMgmt = new WarehouseManager($pdo, $company_id);
|
||||
$whMgmt->deleteStorage((int)$data['id']);
|
||||
|
||||
});
|
||||
|
||||
$answer["success"] = 1;
|
||||
|
||||
} catch (PDOException $e) {
|
||||
$answer["success"] = 0;
|
||||
$answer["message"] = "Database error, please try again";
|
||||
http_response_code(500);
|
||||
|
||||
} catch (Exception $e) {
|
||||
$answer["success"] = 0;
|
||||
$answer["message"] = $e->getMessage();
|
||||
http_response_code(400);
|
||||
}
|
||||
|
||||
exit(json_encode($answer));
|
||||
?>
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
session_start();
|
||||
require '../../../assets/utils/db_auth.php';
|
||||
require '../../../assets/utils/classes/WarehouseManager.php';
|
||||
|
||||
try {
|
||||
|
||||
dbTransaction($pdo2, function($pdo) use ($data, $company_id) {
|
||||
|
||||
$whMgmt = new WarehouseManager($pdo, $company_id);
|
||||
$whMgmt->deleteWarehouse((int)$data['id']);
|
||||
|
||||
});
|
||||
|
||||
$answer["success"] = 1;
|
||||
|
||||
} catch (PDOException $e) {
|
||||
$answer["success"] = 0;
|
||||
$answer["message"] = "Database error, please try again";
|
||||
http_response_code(500);
|
||||
|
||||
} catch (Exception $e) {
|
||||
$answer["success"] = 0;
|
||||
$answer["message"] = $e->getMessage();
|
||||
http_response_code(400);
|
||||
}
|
||||
|
||||
exit(json_encode($answer));
|
||||
?>
|
||||
Reference in New Issue
Block a user