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
+61 -80
View File
@@ -1,98 +1,79 @@
<?php
session_start();
session_start();
require '../../../assets/utils/db_auth.php';
require '../../../assets/utils/db_auth.php';
try {
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging) {
$id = (int)($data['id'] ?? 0);
$id = (int)($data['id'] ?? 0);
$pdo2->beginTransaction();
$sql = "SELECT `log` FROM md_warehouse
WHERE company_id = :company_id AND id = :id";
$sth = $pdo->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":id" => $id
]);
// get existing log
$sql = "SELECT `log`
FROM md_warehouse
WHERE company_id = :company_id
AND id = :id";
$sth = $pdo2->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;
db_check($sth, $answer);
$params = [
":company_id" => $company_id,
":warehouse_name" => $data["warehouse_name"] ?? "",
":location" => $data["location"] ?? "",
":manager" => (int)($data["manager"] ?? 0),
":capacity" => (int)($data["capacity"] ?? 0),
":description" => $data["description"] ?? "",
":status" => (int)($data["status"] ?? 1),
":log" => json_encode($table_log, JSON_UNESCAPED_UNICODE),
];
$row = $sth->fetch(PDO::FETCH_ASSOC);
if ($id > 0) {
$table_log = json_decode($row['log'] ?? '[]', true);
if (!is_array($table_log)) $table_log = [];
$sql = "UPDATE md_warehouse SET
`warehouse_name` = :warehouse_name,
`location` = :location,
`manager` = :manager,
`capacity` = :capacity,
`description` = :description,
`status` = :status,
`log` = :log
WHERE id = :id AND company_id = :company_id";
// add new log entry (assumes $logging already prepared)
$table_log[] = $logging;
$params[":id"] = $id;
if ($id > 0) {
} else {
$sql = "UPDATE md_warehouse SET
warehouse_name = :warehouse_name,
location = :location,
manager = :manager,
capacity = :capacity,
description = :description,
status = :status,
`log` = :log
WHERE id = :id
AND company_id = :company_id";
$sql = "INSERT INTO md_warehouse
(company_id, warehouse_name, location, manager, capacity, description, status, `log`)
VALUES
(:company_id, :warehouse_name, :location, :manager, :capacity, :description, :status, :log)";
}
$sth = $pdo2->prepare($sql);
$sth->execute([
":id" => $id,
":company_id" => $company_id,
":warehouse_name" => $data["warehouse_name"] ?? "",
":location" => $data["location"] ?? "",
":manager" => (int)($data["manager"] ?? 0),
":capacity" => (int)($data["capacity"] ?? 0),
":description" => $data["description"] ?? "",
":status" => (int)($data["status"] ?? 1),
":log" => json_encode($table_log, JSON_UNESCAPED_UNICODE),
]);
db_check($sth, $answer);
$sth = $pdo->prepare($sql);
$sth->execute($params);
} else {
// Create stock table for new warehouse
if ($id == 0 && !empty($data["warehouse_name"])) {
$safe_name = 'td_stock_' . preg_replace('/[^a-zA-Z0-9_]/', '', $data["warehouse_name"]);
$sql = "CREATE TABLE `$safe_name` LIKE `td_stock`";
$pdo->exec($sql);
}
$sql = "INSERT INTO md_warehouse
(company_id, warehouse_name, location, manager, capacity, description, status, `log`)
VALUES
(:company_id, :warehouse_name, :location, :manager, :capacity, :description, :status, :log)";
});
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":warehouse_name" => $data["warehouse_name"] ?? "",
":location" => $data["location"] ?? "",
":manager" => (int)($data["manager"] ?? 0),
":capacity" => (int)($data["capacity"] ?? 0),
":description" => $data["description"] ?? "",
":status" => (int)($data["status"] ?? 1),
":log" => json_encode($table_log, JSON_UNESCAPED_UNICODE),
]);
db_check($sth, $answer);
$answer["success"] = 1;
} catch (PDOException $e) {
$answer["success"] = 0;
$answer["message"] = "Database error, please try again";
if(!empty($data["warehouse_name"])){
/**
* Create warehouse table
*/
$safe_name = 'td_stock_' . preg_replace('/[^a-zA-Z0-9_]/', '', $data["warehouse_name"]);
$sql = "CREATE TABLE `$safe_name` LIKE `td_stock`";
$sth = $pdo2->prepare($sql);
$sth->execute();
db_check($sth, $answer);
}
}
} catch (Exception $e) {
$answer["success"] = 0;
$answer["message"] = "Something went wrong";
}
$pdo2->commit();
$answer["success"] = 1;
exit(json_encode($answer));
?>
exit(json_encode($answer));
?>