remove actions and data integrity

This commit is contained in:
Thanakorn S
2026-04-24 14:25:39 +07:00
parent a75e180686
commit 92d116fd21
26 changed files with 1325 additions and 249 deletions
+5 -56
View File
@@ -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));
?>
+23 -1
View File
@@ -298,6 +298,28 @@
function remove_product(id,element) {
return ajax_request({
url: "<?php echo $server_url?>inventory/api/engine/remove_product.php",
autoPrepare: true,
checkRequired: 0,
debugMode: 0,
data: {
id: id
},
action: 'delete',
onSuccess: function(res) {
element.parent().parent().remove();
}
});
}
function change_page_category(page_num) {
var offset = (page_num - 1) * prop_limit;
@@ -382,7 +404,7 @@
</td>
<td class="py-3">
<a href="<?php echo $server_url?>inventory/manage_product.php?id=${item['id']}" class=""><i class="ti ti-eye fs-5"></i></a>
<a href="#" class="link-danger"><i class="ti ti-trash ms-2 fs-5"></i></a>
<a href="javascript:;" class="link-danger" onclick="remove_product(${item['id']},$(this))"><i class="ti ti-trash ms-2 fs-5"></i></a>
</td>
</tr>`;
})
+45 -2
View File
@@ -335,7 +335,7 @@
</td>
<td class="py-3">
<a href="<?php echo $server_url?>inventory/manage_warehouse.php?id=${item['id']}" class=""><i class="ti ti-eye fs-5"></i></a>
<a href="#" class="link-danger"><i class="ti ti-trash ms-2 fs-5"></i></a>
<a href="javascript:;" class="link-danger" onclick="remove_warehouse(${item['id']},$(this))"><i class="ti ti-trash ms-2 fs-5"></i></a>
</td>
</tr>`;
})
@@ -393,7 +393,7 @@
<td class="py-3"></td>
<td class="py-3">
<a href="<?php echo $server_url?>inventory/manage_storage.php?id=${item['id']}" class=""><i class="ti ti-eye fs-5"></i></a>
<a href="#" class="link-danger"><i class="ti ti-trash ms-2 fs-5"></i></a>
<a href="javascript:;" class="link-danger" onclick="remove_storage(${item['id']},$(this))"><i class="ti ti-trash ms-2 fs-5"></i></a>
</td>
</tr>`;
})
@@ -402,6 +402,49 @@
}
function remove_warehouse(id,element) {
return ajax_request({
url: "<?php echo $server_url?>inventory/api/engine/remove_warehouse.php",
autoPrepare: true,
checkRequired: 0,
debugMode: 0,
data: {
id: id
},
action: 'delete',
onSuccess: function(res) {
element.parent().parent().remove();
}
});
}
function remove_storage(id,element) {
return ajax_request({
url: "<?php echo $server_url?>inventory/api/engine/remove_storage.php",
autoPrepare: true,
checkRequired: 0,
debugMode: 0,
data: {
id: id
},
action: 'delete',
onSuccess: function(res) {
element.parent().parent().remove();
}
});
}
$(async function() {
try {
await retrieve_stats();