79 lines
2.8 KiB
PHP
79 lines
2.8 KiB
PHP
<?php
|
|
session_start();
|
|
require '../../../assets/utils/db_auth.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);
|
|
}
|
|
|
|
});
|
|
|
|
$answer["success"] = 1;
|
|
|
|
} 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));
|
|
?>
|