[OOP] contact,inventory,ics,dashboard

This commit is contained in:
Thanakorn S
2026-04-28 15:44:57 +07:00
parent 3d3b497c08
commit 2061624641
49 changed files with 1946 additions and 2146 deletions
+9 -59
View File
@@ -1,74 +1,24 @@
<?php
<?php
session_start();
require '../../../assets/utils/db_auth.php';
require '../../../assets/utils/classes/ProductManager.php';
try {
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging) {
$id = (int)$data['id'];
$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);
$table_log[] = $logging;
if ($id > 0) {
$sql = "UPDATE md_product_category SET
`category` = :category,
`slug` = :slug,
`description` = :description,
`status` = :status,
`log` = :log
WHERE id = :id AND company_id = :company_id";
$sth = $pdo->prepare($sql);
$sth->execute([
":id" => $id,
":company_id" => $company_id,
":category" => $data["category"],
":slug" => $data["slug"],
":description" => $data["description"],
":status" => $data["status"],
":log" => json_encode($table_log),
]);
} else {
$sql = "INSERT INTO md_product_category
(company_id, category, slug, `description`, `status`, `log`)
VALUES
(:company_id, :category, :slug, :description, :status, :log)";
$sth = $pdo->prepare($sql);
$sth->execute([
":company_id" => $company_id,
":category" => $data["category"],
":slug" => $data["slug"],
":description" => $data["description"],
":status" => $data["status"],
":log" => json_encode($table_log),
]);
}
$product = new ProductManager($pdo, $company_id);
$product->saveCategory($data, $logging);
});
$answer["success"] = 1;
$answer['success'] = 1;
} catch (PDOException $e) {
$answer["success"] = 0;
$answer["message"] = "Database error, please try again";
$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['success'] = 0;
$answer['message'] = $e->getMessage();
http_response_code(400);
}
+25 -100
View File
@@ -1,144 +1,69 @@
<?php
<?php
session_start();
require '../../../assets/utils/db_auth.php';
require '../../../assets/utils/classes/FileUploader.php';
require '../../../assets/utils/classes/ProductManager.php';
$id = (int)$data['id'];
// ── Business rule: min_stock < reorder_point ──────────────────────────
// The warning tier (balance between min_stock and reorder_point) only
// exists when min_stock is strictly less than reorder_point. Enforcing
// this invariant keeps the low-stock report meaningful.
$min_stock = (float) ($data['min_stock'] ?? 0);
$reorder_point = (float) ($data['reorder_point'] ?? 0);
$id = (int)$data['id'];
$min_stock = (float)($data['min_stock'] ?? 0);
$reorder_point = (float)($data['reorder_point'] ?? 0);
if ($min_stock < 0 || $reorder_point < 0) {
$answer["success"] = 0;
$answer["message"] = "Min stock and reorder point cannot be negative.";
$answer['success'] = 0;
$answer['message'] = 'Min stock and reorder point cannot be negative.';
http_response_code(400);
exit(json_encode($answer));
}
if ($min_stock >= $reorder_point) {
$answer["success"] = 0;
$answer["message"] = "Min stock ({$min_stock}) must be less than reorder point ({$reorder_point}).";
$answer['success'] = 0;
$answer['message'] = "Min stock ({$min_stock}) must be less than reorder point ({$reorder_point}).";
http_response_code(400);
exit(json_encode($answer));
}
try {
/** 1. FILE HANDLING **/
$uploader = new FileUploader($include_url . "uploads/product/");
$uploader = new FileUploader($include_url . 'uploads/product/');
if (!empty($_FILES['product_image']['name'])) {
// Cleanup old image when updating
if ($id > 0) {
$sql = "SELECT `product_image` FROM md_product WHERE company_id = :company_id AND id = :id";
$sth = $pdo2->prepare($sql);
$sth->execute([":company_id" => $company_id, ":id" => $id]);
$existing_files = $sth->fetchColumn();
$uploader->cleanup($existing_files, '');
$sth->execute([':company_id' => $company_id, ':id' => $id]);
$uploader->cleanup($sth->fetchColumn(), '');
}
$errors = $uploader->upload('product_image');
if (!empty($errors)) {
$answer["success"] = 0;
$answer["message"] = $errors[0];
$answer['success'] = 0;
$answer['message'] = $errors[0];
exit(json_encode($answer));
}
// Get the single uploaded filename directly
$db_image_string = $uploader->getUploadedFiles()[0] ?? '';
} else {
// No new file — keep existing, skip cleanup entirely
$db_image_string = $data['keep_files'] ?? '';
}
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging, $id, $db_image_string) {
// 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
]);
$table_log = json_decode($sth->fetchColumn() ?: '[]', true);
$table_log[] = $logging;
if ($id > 0) {
$sql = "UPDATE md_product SET
product_name = :product_name,
sku = :sku,
price = :price,
min_stock = :min_stock,
reorder_point = :reorder_point,
category = :category,
product_image = :product_image,
`description` = :description,
`status` = :status,
`log` = :log
WHERE id = :id AND company_id = :company_id";
$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"],
":reorder_point" => $data["reorder_point"],
":category" => $data["category"],
":product_image"=> $db_image_string,
":description" => $data["description"],
":status" => $data["status"],
":log" => json_encode($table_log),
]);
} else {
$sql = "INSERT INTO md_product
(company_id, product_name, sku, price, min_stock, reorder_point, category, product_image, `description`, `status`, `log`)
VALUES
(:company_id, :product_name, :sku, :price, :min_stock, :reorder_point, :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"],
":reorder_point" => $data["reorder_point"],
":category" => $data["category"],
":product_image"=> $db_image_string,
":description" => $data["description"],
":status" => $data["status"],
":log" => json_encode($table_log),
]);
}
/** 2. DATABASE **/
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging, $db_image_string) {
$product = new ProductManager($pdo, $company_id);
$product->saveProduct($data, $logging, $db_image_string);
});
$answer["success"] = 1;
$answer['success'] = 1;
} catch (PDOException $e) {
$answer["success"] = 0;
$answer["message"] = "Database error, please try again";
if (isset($uploader)) $uploader->rollbackUploads();
$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();
if (isset($uploader)) $uploader->rollbackUploads();
$answer['success'] = 0;
$answer['message'] = $e->getMessage();
http_response_code(400);
}
+7 -79
View File
@@ -5,92 +5,20 @@
try {
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging) {
$id = (int)($data['id'] ?? 0);
$sql = "SELECT `log` FROM md_storage
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;
$params = [
":company_id" => $company_id,
":warehouse" => $data["warehouse"] ?? "",
":zone" => $data["zone"] ?? "",
":aisle_from" => $data["aisle_from"] ?? "",
":aisle_to" => $data["aisle_to"] ?? "",
":rack_from" => $data["rack_from"] ?? "",
":rack_to" => $data["rack_to"] ?? "",
":description" => $data["description"] ?? "",
":status" => (int)($data["status"] ?? 1),
":log" => json_encode($table_log, JSON_UNESCAPED_UNICODE),
];
if ($id > 0) {
$sql = "UPDATE md_storage SET
warehouse = :warehouse,
zone = :zone,
aisle_from = :aisle_from,
aisle_to = :aisle_to,
rack_from = :rack_from,
rack_to = :rack_to,
description = :description,
status = :status,
`log` = :log
WHERE id = :id AND company_id = :company_id";
$params[":id"] = $id;
$sth = $pdo->prepare($sql);
$sth->execute($params);
$storage_id = $id;
} else {
$sql = "INSERT INTO md_storage
(company_id, warehouse, zone, aisle_from, aisle_to,
rack_from, rack_to, description, status, `log`)
VALUES
(:company_id, :warehouse, :zone, :aisle_from, :aisle_to,
:rack_from, :rack_to, :description, :status, :log)";
$sth = $pdo->prepare($sql);
$sth->execute($params);
$storage_id = (int) $pdo->lastInsertId();
}
// Sync md_rack rows — same $pdo, inside the same transaction
$wm = new WarehouseManager($pdo, $company_id);
$wm->syncRacks($storage_id, [
'warehouse' => $data['warehouse'],
'zone' => $data['zone'],
'aisle_from' => $data['aisle_from'],
'aisle_to' => $data['aisle_to'],
'rack_from' => $data['rack_from'],
'rack_to' => $data['rack_to'],
]);
$wh = new WarehouseManager($pdo, $company_id);
$wh->saveStorage($data, $logging);
});
$answer["success"] = 1;
$answer['success'] = 1;
} catch (PDOException $e) {
$answer["success"] = 0;
$answer["message"] = "Database error, please try again";
$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['success'] = 0;
$answer['message'] = $e->getMessage();
http_response_code(400);
}
+8 -61
View File
@@ -1,77 +1,24 @@
<?php
session_start();
require '../../../assets/utils/db_auth.php';
require '../../../assets/utils/classes/WarehouseManager.php';
try {
dbTransaction($pdo2, function($pdo) use ($data, $company_id, $logging) {
$id = (int)($data['id'] ?? 0);
$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
]);
$table_log = json_decode($sth->fetchColumn() ?: '[]', true);
if (!is_array($table_log)) $table_log = [];
$table_log[] = $logging;
$params = [
":company_id" => $company_id,
":warehouse_name" => $data["warehouse_name"] ?? "",
":location" => $data["location"] ?? "",
":manager" => (int)($data["manager"] ?? 0),
":description" => $data["description"] ?? "",
":status" => (int)($data["status"] ?? 1),
":log" => json_encode($table_log, JSON_UNESCAPED_UNICODE),
];
if ($id > 0) {
$sql = "UPDATE md_warehouse SET
`warehouse_name` = :warehouse_name,
`location` = :location,
`manager` = :manager,
`description` = :description,
`status` = :status,
`log` = :log
WHERE id = :id AND company_id = :company_id";
$params[":id"] = $id;
} else {
$sql = "INSERT INTO md_warehouse
(company_id, warehouse_name, location, manager, description, status, `log`)
VALUES
(:company_id, :warehouse_name, :location, :manager, :description, :status, :log)";
}
$sth = $pdo->prepare($sql);
$sth->execute($params);
// 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);
}
$wh = new WarehouseManager($pdo, $company_id);
$wh->saveWarehouse($data, $logging);
});
$answer["success"] = 1;
$answer['success'] = 1;
} catch (PDOException $e) {
$answer["success"] = 0;
$answer["message"] = "Database error, please try again";
$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['success'] = 0;
$answer['message'] = $e->getMessage();
http_response_code(400);
}
+5 -19
View File
@@ -1,24 +1,10 @@
<?php
<?php
session_start();
require '../../../assets/utils/db_auth.php';
require '../../../assets/utils/classes/ProductManager.php';
$sql = "SELECT a.* , IFNULL(SUM(b.total_in-b.total_out),0) AS product_balance
FROM md_product a
LEFT JOIN warehouse_balance b ON
a.company_id = b.company_id AND
a.sku = b.product_sku
WHERE
a.company_id = {$company_id}
GROUP BY a.id";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id
]);
db_check($sth, $answer);
$answer["output"] = $sth->fetchAll(PDO::FETCH_ASSOC);
$answer["success"] = 1;
$product = new ProductManager($pdo2, $company_id);
$answer['output'] = $product->getProductList();
$answer['success'] = 1;
exit(json_encode($answer));
?>
+5 -19
View File
@@ -1,24 +1,10 @@
<?php
<?php
session_start();
require '../../../assets/utils/db_auth.php';
require '../../../assets/utils/classes/ProductManager.php';
$sql = "SELECT a.* , COUNT(b.id) AS product_count
FROM md_product_category a
LEFT JOIN md_product b ON
a.company_id = b.company_id AND
a.id = b.category
WHERE
a.company_id = {$company_id}
GROUP BY a.id";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id
]);
db_check($sth, $answer);
$answer["output"] = $sth->fetchAll(PDO::FETCH_ASSOC);
$answer["success"] = 1;
$product = new ProductManager($pdo2, $company_id);
$answer['output'] = $product->getCategoryList();
$answer['success'] = 1;
exit(json_encode($answer));
?>
+5 -20
View File
@@ -1,25 +1,10 @@
<?php
<?php
session_start();
require '../../../assets/utils/db_auth.php';
require '../../../assets/utils/classes/ProductManager.php';
$id = (int)$data['id'];
$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);
$answer["output"] = $sth->fetch(PDO::FETCH_ASSOC);
$answer["success"] = 1;
$product = new ProductManager($pdo2, $company_id);
$answer['output'] = $product->getCategoryById((int)$data['id']);
$answer['success'] = 1;
exit(json_encode($answer));
?>
+5 -20
View File
@@ -1,25 +1,10 @@
<?php
<?php
session_start();
require '../../../assets/utils/db_auth.php';
require '../../../assets/utils/classes/ProductManager.php';
$id = (int)$data['id'];
$sql = "SELECT *
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);
$answer["output"] = $sth->fetch(PDO::FETCH_ASSOC);
$answer["success"] = 1;
$product = new ProductManager($pdo2, $company_id);
$answer['output'] = $product->getProductById((int)$data['id']);
$answer['success'] = 1;
exit(json_encode($answer));
?>
+5 -20
View File
@@ -1,25 +1,10 @@
<?php
<?php
session_start();
require '../../../assets/utils/db_auth.php';
require '../../../assets/utils/classes/WarehouseManager.php';
$id = (int)$data['id'];
$sql = "SELECT *
FROM md_storage
WHERE
company_id = :company_id and
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;
$wh = new WarehouseManager($pdo2, $company_id);
$answer['output'] = $wh->getStorageById((int)$data['id']);
$answer['success'] = 1;
exit(json_encode($answer));
?>
@@ -1,25 +1,10 @@
<?php
<?php
session_start();
require '../../../assets/utils/db_auth.php';
require '../../../assets/utils/classes/WarehouseManager.php';
$id = (int)$data['id'];
$sql = "SELECT *
FROM md_warehouse
WHERE
company_id = :company_id and
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;
$wh = new WarehouseManager($pdo2, $company_id);
$answer['output'] = $wh->getWarehouseById((int)$data['id']);
$answer['success'] = 1;
exit(json_encode($answer));
?>
+5 -24
View File
@@ -1,29 +1,10 @@
<?php
<?php
session_start();
require '../../../assets/utils/db_auth.php';
require '../../../assets/utils/classes/WarehouseManager.php';
$sql = "SELECT a.*, b.warehouse_name
FROM md_storage a
LEFT JOIN md_warehouse b ON
a.company_id = b.company_id and
a.warehouse = b.id
WHERE
a.company_id = :company_id ";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id
]);
db_check($sth, $answer);
$answer["output"] = $sth->fetchAll(PDO::FETCH_ASSOC);
// modify output
foreach($answer["output"] as $key => $item){
}
$answer["success"] = 1;
$wh = new WarehouseManager($pdo2, $company_id);
$answer['output'] = $wh->getStorageList();
$answer['success'] = 1;
exit(json_encode($answer));
?>
+5 -31
View File
@@ -1,36 +1,10 @@
<?php
<?php
session_start();
require '../../../assets/utils/db_auth.php';
require '../../../assets/utils/classes/WarehouseManager.php';
$sql = "SELECT
a.*,
COALESCE(r.total_racks, 0) AS capacity,
COALESCE(r.occupied_racks, 0) AS space_used,
COALESCE(r.unique_product, 0) AS unique_product,
c.name AS manager_name,
c.surname AS manager_surname
FROM md_warehouse a
LEFT JOIN (
SELECT
company_id, warehouse,
COUNT(*) AS total_racks,
COUNT(product_sku) AS occupied_racks,
COUNT(DISTINCT product_sku) AS unique_product
FROM md_rack
GROUP BY company_id, warehouse
) r ON a.company_id = r.company_id AND a.id = r.warehouse
LEFT JOIN wms.user c ON a.manager = c.user_id
WHERE a.company_id = :company_id";
$sth = $pdo2->prepare($sql);
$sth->execute([
":company_id" => $company_id
]);
db_check($sth, $answer);
$answer["output"] = $sth->fetchAll(PDO::FETCH_ASSOC);
$answer["success"] = 1;
$wh = new WarehouseManager($pdo2, $company_id);
$answer['output'] = $wh->getWarehouseListAll();
$answer['success'] = 1;
exit(json_encode($answer));
?>