generic API response + ICS: warehouse_balance md_rack update

This commit is contained in:
Thanakorn S
2026-04-23 16:06:19 +07:00
parent ed23c269ea
commit a75e180686
25 changed files with 934 additions and 342 deletions
@@ -64,10 +64,12 @@
} 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));
@@ -112,11 +112,13 @@
} 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();
$answer["error"] = $e->getMessage();
http_response_code(400);
}
exit(json_encode($answer));
@@ -86,10 +86,12 @@
} 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));
@@ -67,10 +67,12 @@
} 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));
+65 -39
View File
@@ -1,54 +1,80 @@
<?php
session_start();
require '../../../assets/utils/db_auth.php';
$id = (int)$data['id'];
try {
$result = dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging) {
// get data
$sql = "SELECT *
FROM md_product_category
WHERE
company_id = :company_id and
id = :id ";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":id" => $id
]);
db_check($sth, $answer);
$json = $sth->fetch(PDO::FETCH_ASSOC);
$json = json_encode($json);
$id = (int)($data['id'] ?? 0);
if ($id <= 0) {
throw new Exception("Invalid category ID.");
}
// delete data
$sql = "DELETE FROM md_product_category
WHERE
company_id = :company_id and
id = :id ";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $id,
":id" => $id
]);
db_check($sth, $answer);
// 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).");
}
// insert into archive
$sql = "INSERT INTO archive (`company_id`,`user_id`,`date`,`json`) VALUES (:company_id,:user_id,NOW(),:json)";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":user_id" => $user_id,
":json" => $json
]);
db_check($sth, $answer);
// 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;
$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);
}
$answer["success"] = 1;
exit(json_encode($answer));
?>
+1 -1
View File
@@ -10,7 +10,7 @@
$answer["total_location"] = $report->getTotalLocation();
$answer["total_capacity"] = $report->getTotalCapacity();
$answer["space_used"] = $report->getSpaceUsed();
$answer["total_product"] = $report->getTotalProduct();
$answer["total_product"] = $report->getTotalProductInStock();
$answer["success"] = 1;
exit(json_encode($answer));
+10 -6
View File
@@ -150,7 +150,7 @@
<th>Category</th>
<th>Slug</th>
<th>Description</th>
<th>Products</th>
<th style="text-align: right;">Product Count</th>
<th>Status</th>
<th>Actions</th>
</tr>
@@ -190,11 +190,12 @@
<table class="table table-hover mb-0 table-centered" id="product">
<thead class="table-secondary border-light">
<tr>
<th width="75px"></th>
<th>Product</th>
<th>SKU</th>
<th>Current Stock</th>
<th>Min Stock</th>
<th>Reorder Point</th>
<th style="text-align: right;">Current Stock</th>
<th style="text-align: right;">Min Stock</th>
<th style="text-align: right;">Reorder Point</th>
<th>Status</th>
<th>Action</th>
</tr>
@@ -309,7 +310,7 @@
<td class="py-3">${item['category']}</td>
<td class="py-3">${item['slug']}</td>
<td class="py-3">${item['description']}</td>
<td class="py-3">${item['product_count']}</td>
<td class="py-3" style="text-align: right;">${item['product_count']}</td>
<td class="py-3">
${(item['status']==1)?'<span class="badge bg-success">Active</span>':'<span class="badge bg-secondary">Inactive</span>'}
</td>
@@ -367,7 +368,10 @@
var body = ``;
$.each(page_data, function(key, item) {
body += `<tr>
body += `<tr height="50px">
<td class="py-3">
<img src="<?php echo $server_url?>uploads/product/${item['product_image']}" alt="${item['product_name']}" class="img-fluid" style="height: 25px;">
</td>
<td class="py-3">${item['product_name']}</td>
<td class="py-3">${item['sku']}</td>
<td class="py-3" style='text-align: right;'>${format_number(item['product_balance'],4)}</td>