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
+25
View File
@@ -0,0 +1,25 @@
<?php
session_start();
require '../../../assets/utils/db_auth.php';
$sql = "SELECT *
FROM md_contact
WHERE
company_id = :company_id and
contact_name like :keyword
LIMIT 50
";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":keyword" => '%'.$data["keyword"].'%'
]);
db_check($sth, $answer);
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
$answer["result"] = $result;
$answer["success"] = 1;
exit(json_encode($answer));
?>
+87
View File
@@ -0,0 +1,87 @@
<?php
session_start();
require '../../../assets/utils/db_auth.php';
try {
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging, $uuid, &$answer) {
$id = (int)$data['id'];
$warehouse_id = (int)$data["warehouse"];
$whMgmt = new WarehouseManager($pdo, $company_id);
$warehouse = $whMgmt->getStockContext($warehouse_id, $id);
$row = $warehouse['row'] ?? [];
$table = $warehouse['table'];
$table_log = json_decode($row['log'] ?? '[]', true);
$table_log[] = $logging;
if ($id > 0) {
$sql = "UPDATE `$table` SET
`product_sku` = :product_sku,
`in` = :quantity,
`zone` = :zone,
`aisle` = :aisle,
`rack` = :rack,
`contact_id` = :contact_id,
`description` = :description,
`log` = :log
WHERE id = :id AND company_id = :company_id";
$sth = $pdo->prepare($sql);
$sth->execute([
":id" => $id,
":company_id" => $company_id,
":product_sku" => $data["product_sku"],
":quantity" => $data["quantity"],
":zone" => $data["zone"],
":aisle" => $data["aisle"],
":rack" => $data["rack"],
":contact_id" => (int)$data["contact_id"],
":description" => $data["description"],
":log" => json_encode($table_log),
]);
} else {
$sql = "INSERT INTO `$table`
(uuid, company_id, `date`, product_sku, `in`, zone, aisle, rack, contact_id, `description`, `log`, `type`)
VALUES
(:uuid, :company_id, :date, :product_sku, :quantity, :zone, :aisle, :rack, :contact_id, :description, :log, 'in')";
$sth = $pdo->prepare($sql);
$sth->execute([
":uuid" => $uuid,
":company_id" => $company_id,
":date" => date('Y-m-d H:i:s'),
":product_sku" => $data["product_sku"],
":quantity" => $data["quantity"],
":zone" => $data["zone"],
":aisle" => $data["aisle"],
":rack" => $data["rack"],
":contact_id" => (int)$data["contact_id"],
":description" => $data["description"],
":log" => json_encode($table_log),
]);
}
$whMgmt->adjustBalance('in', $warehouse_id, $data["product_sku"], $row['in'] ?? 0, $data["quantity"]);
});
$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";
}
exit(json_encode($answer));
?>
+87
View File
@@ -0,0 +1,87 @@
<?php
session_start();
require '../../../assets/utils/db_auth.php';
try {
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging, $uuid) {
$id = (int)$data['id'];
$warehouse_id = (int)$data["warehouse"];
$whMgmt = new WarehouseManager($pdo, $company_id);
$warehouse = $whMgmt->getStockContext($warehouse_id, $id);
$row = $warehouse['row'] ?? [];
$table = $warehouse['table'];
$table_log = json_decode($row['log'] ?? '[]', true);
$table_log[] = $logging;
if ($id > 0) {
$sql = "UPDATE `$table` SET
`product_sku` = :product_sku,
`out` = :quantity,
`zone` = :zone,
`aisle` = :aisle,
`rack` = :rack,
`contact_id` = :contact_id,
`description` = :description,
`log` = :log
WHERE id = :id AND company_id = :company_id";
$sth = $pdo->prepare($sql);
$sth->execute([
":id" => $id,
":company_id" => $company_id,
":product_sku" => $data["product_sku"],
":quantity" => $data["quantity"],
":zone" => $data["zone"],
":aisle" => $data["aisle"],
":rack" => $data["rack"],
":contact_id" => (int)$data["contact_id"],
":description" => $data["description"],
":log" => json_encode($table_log),
]);
} else {
$sql = "INSERT INTO `$table`
(uuid, company_id, `date`, product_sku, `out`, zone, aisle, rack, contact_id, `description`, `log`, `type`)
VALUES
(:uuid, :company_id, :date, :product_sku, :quantity, :zone, :aisle, :rack, :contact_id, :description, :log, 'out')";
$sth = $pdo->prepare($sql);
$sth->execute([
":uuid" => $uuid,
":company_id" => $company_id,
":date" => date('Y-m-d H:i:s'),
":product_sku" => $data["product_sku"],
":quantity" => $data["quantity"],
":zone" => $data["zone"],
":aisle" => $data["aisle"],
":rack" => $data["rack"],
":contact_id" => (int)$data["contact_id"],
":description" => $data["description"],
":log" => json_encode($table_log),
]);
}
$whMgmt->adjustBalance('out', $warehouse_id, $data["product_sku"], $row['out'] ?? 0, $data["quantity"]);
});
$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";
}
exit(json_encode($answer));
?>
@@ -0,0 +1,113 @@
<?php
session_start();
require '../../../assets/utils/db_auth.php';
try {
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging, $uuid) {
$id = (int)$data['id'];
$whMgmt = new WarehouseManager($pdo, $company_id);
for ($i = 0; $i < 2; $i++) {
$data['type'] = ($i == 0) ? "from" : "to";
$data['ref_type'] = ($i == 0) ? "to" : "from";
$data['col'] = ($i == 0) ? "out" : "in";
$warehouse_id = (int)$data['warehouse_' . $data["type"]];
$ref_warehouse_id = (int)$data['warehouse_' . $data["ref_type"]];
$data["warehouse"] = $data['warehouse_' . $data["type"]];
$data["zone"] = $data['zone_' . $data["type"]];
$data["aisle"] = $data['aisle_' . $data["type"]];
$data["rack"] = $data['rack_' . $data["type"]];
$warehouse = $whMgmt->getStockContext($warehouse_id, $id);
$row = $warehouse['row'] ?? [];
$table = $warehouse['table'];
// get uuid for update
if ($id > 0 && $data['type'] == "from") {
$sql = "SELECT uuid FROM `$table`
WHERE company_id = :company_id AND id = :id";
$sth = $pdo->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":id" => $id
]);
$uuid = $sth->fetchColumn();
}
$table_log = json_decode($row['log'] ?? '[]', true);
$table_log[] = $logging;
if ($id > 0) {
$sql = "UPDATE `$table` SET
`product_sku` = :product_sku,
`{$data['col']}` = :quantity,
`zone` = :zone,
`aisle` = :aisle,
`rack` = :rack,
`contact_id` = :contact_id,
`description` = :description,
`log` = :log
WHERE uuid = :uuid AND company_id = :company_id";
$sth = $pdo->prepare($sql);
$sth->execute([
":uuid" => $uuid,
":company_id" => $company_id,
":product_sku" => $data["product_sku"],
":quantity" => $data["quantity"],
":zone" => $data["zone"],
":aisle" => $data["aisle"],
":rack" => $data["rack"],
":contact_id" => (int)$data["contact_id"],
":description" => $data["description"],
":log" => json_encode($table_log),
]);
} else {
$sql = "INSERT INTO `$table`
(uuid, company_id, `date`, product_sku, `{$data['col']}`, ref_warehouse, zone, aisle, rack, contact_id, `description`, `log`, `type`)
VALUES
(:uuid, :company_id, :date, :product_sku, :quantity, :ref_warehouse, :zone, :aisle, :rack, :contact_id, :description, :log, 'transfer')";
$sth = $pdo->prepare($sql);
$sth->execute([
":uuid" => $uuid,
":company_id" => $company_id,
":date" => date('Y-m-d H:i:s'),
":product_sku" => $data["product_sku"],
":quantity" => $data["quantity"],
":ref_warehouse" => $ref_warehouse_id,
":zone" => $data["zone"],
":aisle" => $data["aisle"],
":rack" => $data["rack"],
":contact_id" => (int)$data["contact_id"],
":description" => $data["description"],
":log" => json_encode($table_log),
]);
}
$whMgmt->adjustBalance($data['col'], $warehouse_id, $data["product_sku"], $row[$data['col']] ?? 0, $data["quantity"]);
}
});
$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";
}
exit(json_encode($answer));
?>
+10 -3
View File
@@ -4,7 +4,14 @@
require '../../../assets/utils/db_auth.php';
$id = (int)$data['id'];
$warehouse = (int)$data['warehouse'];
// identity source of zone and aisle
if(empty($data["type"])){
$warehouse = (int)$data['warehouse'];
$zone = $data['zone'];
}else{
$warehouse = (int)$data['warehouse_'.$data["type"]];
$zone = $data['zone_'.$data["type"]];
}
$sql = "SELECT *
FROM md_storage
@@ -18,7 +25,7 @@
$sth->execute([
":company_id" => $company_id,
":warehouse" => $warehouse,
":zone" => $data["zone"]
":zone" => $zone
]);
db_check($sth, $answer);
@@ -26,7 +33,7 @@
$output = [];
foreach ($pre as $key => $value) {
$output = array_merge($output, range($value["aisle_form"], $value["aisle_to"]));
$output = array_merge($output, range($value["aisle_from"], $value["aisle_to"]));
}
$answer["output"] = $output;
+24 -5
View File
@@ -3,8 +3,26 @@
require '../../../assets/utils/db_auth.php';
// identity source of zone and aisle
if(empty($data["type"])){
$warehouse = (int)$data['warehouse'];
$zone = $data['zone'];
$aisle = $data["aisle"];
}else{
$warehouse = (int)$data['warehouse_'.$data["type"]];
$zone = $data['zone_'.$data["type"]];
$aisle = $data["aisle_".$data["type"]];
}
// Detect if numeric and zero-pad both input and query columns
if (is_numeric($aisle)) {
$aisle = str_pad($aisle, 5, '0', STR_PAD_LEFT);
$condition = "LPAD(aisle_from, 5, '0') <= :aisle_from AND LPAD(aisle_to, 5, '0') >= :aisle_to";
} else {
$condition = ":aisle_from >= aisle_from AND :aisle_to <= aisle_to";
}
$id = (int)$data['id'];
$warehouse = (int)$data['warehouse'];
$sql = "SELECT *
FROM md_storage
@@ -12,15 +30,16 @@
company_id = :company_id and
warehouse = :warehouse and
`zone` = :zone and
:aisle between aisle_from and aisle_to
$condition
";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":warehouse" => $warehouse,
":zone" => $data["zone"],
":aisle" => $data["aisle"]
":zone" => $zone,
":aisle_from" => $aisle,
":aisle_to" => $aisle
]);
db_check($sth, $answer);
@@ -28,7 +47,7 @@
$output = [];
foreach ($pre as $key => $value) {
$output = array_merge($output, range($value["rack_form"], $value["rack_to"]));
$output = array_merge($output, range($value["rack_from"], $value["rack_to"]));
}
$answer["output"] = $output;
+51
View File
@@ -0,0 +1,51 @@
<?php
session_start();
require '../../../assets/utils/db_auth.php';
$id = (int)$data['id'];
$warehouse = (int)$data["wh"];
// get warehouse name
$sql = "SELECT warehouse_name
FROM md_warehouse
WHERE
company_id = :company_id and
id = :warehouse
";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":warehouse" => $warehouse
]);
db_check($sth, $answer);
$warehouse = $sth->fetchColumn();
$table = "td_stock_" . $warehouse;
$sql = "SELECT a.* , a.in as quantity, b.contact_name, c.product_name
FROM `{$table}` a
LEFT JOIN md_contact b ON
a.company_id = b.company_id and
a.contact_id = b.id
LEFT JOIN md_product c ON
a.company_id = b.company_id and
a.product_sku = c.sku
WHERE
a.company_id = :company_id and
a.id = :id ";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":id" => $id
]);
db_check($sth, $answer);
$answer["output"] = $sth->fetch(PDO::FETCH_ASSOC);
$answer["success"] = 1;
exit(json_encode($answer));
?>
+51
View File
@@ -0,0 +1,51 @@
<?php
session_start();
require '../../../assets/utils/db_auth.php';
$id = (int)$data['id'];
$warehouse = (int)$data["wh"];
// get warehouse name
$sql = "SELECT warehouse_name
FROM md_warehouse
WHERE
company_id = :company_id and
id = :warehouse
";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":warehouse" => $warehouse
]);
db_check($sth, $answer);
$warehouse = $sth->fetchColumn();
$table = "td_stock_" . $warehouse;
$sql = "SELECT a.* , a.out as quantity, b.contact_name, c.product_name
FROM `{$table}` a
LEFT JOIN md_contact b ON
a.company_id = b.company_id and
a.contact_id = b.id
LEFT JOIN md_product c ON
a.company_id = b.company_id and
a.product_sku = c.sku
WHERE
a.company_id = :company_id and
a.id = :id ";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":id" => $id
]);
db_check($sth, $answer);
$answer["output"] = $sth->fetch(PDO::FETCH_ASSOC);
$answer["success"] = 1;
exit(json_encode($answer));
?>
@@ -0,0 +1,101 @@
<?php
session_start();
require '../../../assets/utils/db_auth.php';
$id = (int)$data['id'];
$warehouse = (int)$data["wh"];
/**
* FROM warehouse, zone, aisle, rack, product name, quantity, contact name
*/
// get warehouse name
$sql = "SELECT warehouse_name
FROM md_warehouse
WHERE
company_id = :company_id and
id = :warehouse
";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":warehouse" => $warehouse
]);
db_check($sth, $answer);
$warehouse = $sth->fetchColumn();
$table = "td_stock_" . $warehouse;
$sql = "SELECT a.* , a.out as quantity, b.contact_name, c.product_name
FROM `{$table}` a
LEFT JOIN md_contact b ON
a.company_id = b.company_id and
a.contact_id = b.id
LEFT JOIN md_product c ON
a.company_id = b.company_id and
a.product_sku = c.sku
WHERE
a.company_id = :company_id and
a.id = :id ";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":id" => $id
]);
db_check($sth, $answer);
$output = $sth->fetch(PDO::FETCH_ASSOC);
/**
* TO warehouse, zone, aisle, rack, product name, quantity, contact name
*/
// get warehouse name
$sql = "SELECT warehouse_name
FROM md_warehouse
WHERE
company_id = :company_id and
id = :warehouse
";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":warehouse" => $output["ref_warehouse"]
]);
db_check($sth, $answer);
$warehouse = $sth->fetchColumn();
$table = "td_stock_" . $warehouse;
$sql = "SELECT a.* , a.in as quantity, b.contact_name, c.product_name
FROM `{$table}` a
LEFT JOIN md_contact b ON
a.company_id = b.company_id and
a.contact_id = b.id
LEFT JOIN md_product c ON
a.company_id = b.company_id and
a.product_sku = c.sku
WHERE
a.company_id = :company_id and
a.uuid = :id ";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":id" => $output["uuid"]
]);
db_check($sth, $answer);
$output["ref"] = $sth->fetch(PDO::FETCH_ASSOC);
$answer["output"] = $output;
$answer["success"] = 1;
exit(json_encode($answer));
?>
+7 -1
View File
@@ -4,7 +4,13 @@
require '../../../assets/utils/db_auth.php';
$id = (int)$data['id'];
$warehouse = (int)$data['warehouse'];
// identity source of zone and aisle
if(empty($data["type"])){
$warehouse = (int)$data['warehouse'];
}else{
$warehouse = (int)$data['warehouse_'.$data["type"]];
}
$sql = "SELECT *
FROM md_storage
+53
View File
@@ -0,0 +1,53 @@
<?php
session_start();
require '../../../assets/utils/db_auth.php';
$id = (int)$data['id'];
$warehouse = (int)$data["warehouse"];
// get warehouse name
$sql = "SELECT warehouse_name
FROM md_warehouse
WHERE
company_id = :company_id and
id = :warehouse
";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":warehouse" => $warehouse
]);
db_check($sth, $answer);
$warehouse = $sth->fetchColumn();
if(!$warehouse) {
$answer["message"] = "Warehouse not found";
exit(json_encode($answer));
}
$table = "td_stock_" . $warehouse;
$sql = "SELECT a.* , ROUND(a.in,2) as quantity ,
b.product_name
FROM `{$table}` a
LEFT JOIN md_product b ON
a.company_id = b.company_id and
a.product_sku = b.sku
WHERE
a.company_id = {$company_id} and
a.type = 'in'
ORDER BY a.date DESC";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id
]);
db_check($sth, $answer);
$answer["output"] = $sth->fetchAll(PDO::FETCH_ASSOC);
$answer["success"] = 1;
exit(json_encode($answer));
?>
+53
View File
@@ -0,0 +1,53 @@
<?php
session_start();
require '../../../assets/utils/db_auth.php';
$id = (int)$data['id'];
$warehouse = (int)$data["warehouse"];
// get warehouse name
$sql = "SELECT warehouse_name
FROM md_warehouse
WHERE
company_id = :company_id and
id = :warehouse
";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":warehouse" => $warehouse
]);
db_check($sth, $answer);
$warehouse = $sth->fetchColumn();
if(!$warehouse) {
$answer["message"] = "Warehouse not found";
exit(json_encode($answer));
}
$table = "td_stock_" . $warehouse;
$sql = "SELECT a.* , ROUND(a.out,2) as quantity ,
b.product_name
FROM `{$table}` a
LEFT JOIN md_product b ON
a.company_id = b.company_id and
a.product_sku = b.sku
WHERE
a.company_id = {$company_id} and
a.type = 'out'
ORDER BY a.date DESC";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id
]);
db_check($sth, $answer);
$answer["output"] = $sth->fetchAll(PDO::FETCH_ASSOC);
$answer["success"] = 1;
exit(json_encode($answer));
?>
+54
View File
@@ -0,0 +1,54 @@
<?php
session_start();
require '../../../assets/utils/db_auth.php';
$id = (int)$data['id'];
$warehouse = (int)$data["warehouse"];
// get warehouse name
$sql = "SELECT warehouse_name
FROM md_warehouse
WHERE
company_id = :company_id and
id = :warehouse
";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":warehouse" => $warehouse
]);
db_check($sth, $answer);
$warehouse = $sth->fetchColumn();
if(!$warehouse) {
$answer["message"] = "Warehouse not found";
exit(json_encode($answer));
}
$table = "td_stock_" . $warehouse;
$sql = "SELECT a.* , ROUND(a.in,2) as quantity,
b.product_name
FROM `{$table}` a
LEFT JOIN md_product b ON
a.company_id = b.company_id and
a.product_sku = b.sku
WHERE
a.company_id = {$company_id} and
a.type = 'transfer' and
a.out > 0
ORDER BY a.date DESC";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id
]);
db_check($sth, $answer);
$answer["output"] = $sth->fetchAll(PDO::FETCH_ASSOC);
$answer["success"] = 1;
exit(json_encode($answer));
?>
+24
View File
@@ -0,0 +1,24 @@
<?php
session_start();
require '../../../assets/utils/db_auth.php';
$sql = "SELECT SUM(capacity) as total_capacity, count(*) as total_warehouse
FROM md_warehouse a
WHERE
a.company_id = :company_id
";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id
]);
db_check($sth, $answer);
$output = $sth->fetch(PDO::FETCH_ASSOC);
$answer["output"] = $output;
$answer["success"] = 1;
exit(json_encode($answer));
?>