generic API response + ICS: warehouse_balance md_rack update
This commit is contained in:
@@ -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));
|
||||
|
||||
@@ -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));
|
||||
|
||||
?>
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user