ICS modules

This commit is contained in:
Thanakorn S
2026-03-21 14:58:15 +07:00
parent 2e558a55bb
commit a4f474b5df
45 changed files with 3110 additions and 597 deletions
+71 -61
View File
@@ -3,77 +3,87 @@
require '../../../assets/utils/db_auth.php';
$id = (int)$data['id'];
try {
// get existing log
$sql = "SELECT `log`
FROM md_product
WHERE
company_id = :company_id and
id = :id ";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":id" => $id
]);
db_check($sth, $answer);
$row = $sth->fetch(PDO::FETCH_ASSOC);
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging) {
$table_log = json_decode($row['log'] ?? '[]', true);
$id = (int)$data['id'];
$table_log[] = $logging;
// get existing log
$sql = "SELECT `log` FROM md_product
WHERE company_id = :company_id AND id = :id";
$sth = $pdo->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":id" => $id
]);
if ($id > 0) {
$table_log = json_decode($sth->fetchColumn() ?: '[]', true);
$table_log[] = $logging;
$sql = "UPDATE md_product SET
product_name = :product_name,
sku = :sku,
price = :price,
category = :category,
product_image = :product_image,
`description` = :description,
`status` = :status,
`log` = :log
WHERE id = :id AND company_id = :company_id";
if ($id > 0) {
$sth = $pdo2->prepare($sql);
$sth->execute([
":id" => $id,
":company_id" => $company_id,
":product_name" => $data["product_name"],
":sku" => $data["sku"],
":price" => $data["price"],
":category" => $data["category"],
":product_image"=> $data["product_image"],
":description" => $data["description"],
":status" => $data["status"],
":log" => json_encode($table_log),
]);
$sql = "UPDATE md_product SET
product_name = :product_name,
sku = :sku,
price = :price,
min_stock = :min_stock,
category = :category,
product_image = :product_image,
`description` = :description,
`status` = :status,
`log` = :log
WHERE id = :id AND company_id = :company_id";
} else {
$sth = $pdo->prepare($sql);
$sth->execute([
":id" => $id,
":company_id" => $company_id,
":product_name" => $data["product_name"],
":sku" => $data["sku"],
":price" => $data["price"],
":min_stock" => $data["min_stock"],
":category" => $data["category"],
":product_image"=> $data["product_image"],
":description" => $data["description"],
":status" => $data["status"],
":log" => json_encode($table_log),
]);
$sql = "INSERT INTO md_product
(company_id, product_name, sku, price, category, product_image, `description`, `status`, `log`)
VALUES
(:company_id, :product_name, :sku, :price, :category, :product_image, :description, :status, :log)";
} else {
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":product_name" => $data["product_name"],
":sku" => $data["sku"],
":price" => $data["price"],
":category" => $data["category"],
":product_image"=> $data["product_image"],
":description" => $data["description"],
":status" => $data["status"],
":log" => json_encode($table_log),
]);
$sql = "INSERT INTO md_product
(company_id, product_name, sku, price, min_stock, category, product_image, `description`, `status`, `log`)
VALUES
(:company_id, :product_name, :sku, :price, :min_stock, :category, :product_image, :description, :status, :log)";
$sth = $pdo->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":product_name" => $data["product_name"],
":sku" => $data["sku"],
":price" => $data["price"],
":min_stock" => $data["min_stock"],
":category" => $data["category"],
":product_image"=> $data["product_image"],
":description" => $data["description"],
":status" => $data["status"],
":log" => json_encode($table_log),
]);
}
});
$answer["success"] = 1;
} catch (PDOException $e) {
$answer["success"] = 0;
$answer["message"] = "Database error, please try again";
} catch (Exception $e) {
$answer["success"] = 0;
$answer["message"] = "Something went wrong";
}
$answer["success"] = 1;
exit(json_encode($answer));
?>