From 2e558a55bbbd16a2b2708ab812dc4cf555d4c449 Mon Sep 17 00:00:00 2001 From: Thanakorn S Date: Mon, 9 Mar 2026 17:03:18 +0700 Subject: [PATCH] stock database design --- app/assets/js/custom.js | 33 +++++++++++++++---- app/ics/add_stock_in.php | 25 +++++++++----- app/inventory/api/engine/manage_warehouse.php | 20 ++++++++++- app/inventory/manage_storage.php | 3 ++ app/inventory/manage_warehouse.php | 2 +- 5 files changed, 66 insertions(+), 17 deletions(-) diff --git a/app/assets/js/custom.js b/app/assets/js/custom.js index 25747f9..361e4f3 100644 --- a/app/assets/js/custom.js +++ b/app/assets/js/custom.js @@ -33,6 +33,10 @@ $(function () { type: 'POST', dataType: 'json', data: { json: json }, + // for CSRF validation + headers: { + 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') + }, success: function (res) { @@ -80,7 +84,10 @@ $(function () { // Replace input value with SKU $(this) .val(product.sku) - .attr("secondary", product.id); + .attr("secondary", product.sku); + $('input#product_name') + .val(product.product_name) + .attr("secondary", product.product_name) console.log("Input updated with SKU:", product.sku); @@ -90,12 +97,21 @@ $(function () { }) - // Allow HTML rendering in dropdown - .data("ui-autocomplete")._renderItem = function (ul, item) { - return $("
  • ") - .append($("
    ").html(item.label)) - .appendTo(ul); - }; + // Allow HTML rendering in dropdown + .data("ui-autocomplete")._renderItem = function (ul, item) { + return $("
  • ") + .append($("
    ").html(item.label)) + .appendTo(ul); + }; + + // Add this ↓ + $el.on('blur', function () { + var secondary = $(this).attr('secondary'); + if (secondary) { + $(this).val(secondary); + $('input#product_name').val($('input#product_name').attr('secondary')); + } + }); }); @@ -147,6 +163,7 @@ function prepare_form_data(check_required, raw_data) { if (!is_valid) { alert("Please fill in all mandatory fields."); + isAjaxProcessing = false; return false; } } @@ -178,6 +195,7 @@ function ajax_request(options) { let payloadJson = prepare_form_data(options.checkRequired ?? 0, true); if (payloadJson === false) { + isAjaxProcessing = false; return Promise.reject("validation_failed"); } @@ -195,6 +213,7 @@ function ajax_request(options) { // add action to JSON payloadJson['action'] = options.action; }else{ + isAjaxProcessing = false; return Promise.reject("please_define_action"); } diff --git a/app/ics/add_stock_in.php b/app/ics/add_stock_in.php index 08d17e4..99b084e 100644 --- a/app/ics/add_stock_in.php +++ b/app/ics/add_stock_in.php @@ -28,16 +28,20 @@

    Add Stock

    -
    - +
    + + placeholder="Type product sku..." autocomplete="off" required> +
    +
    + +
    - +
    @@ -45,26 +49,26 @@
    - +
    -
    -
    -
    @@ -106,6 +110,7 @@ autoPrepare: true, checkRequired: 0, debugMode: 0, + action: 'manage', onSuccess: function(res) { window.location.href = "ics/stock_in.php"; @@ -123,6 +128,7 @@ url: "inventory/api/engine/warehouse.php", autoPrepare: true, checkRequired: 0, + action: 'read', onSuccess: function(res) { var option = ``; $.each(res.output, function(key, item) { @@ -143,6 +149,7 @@ autoPrepare: true, checkRequired: 0, noLoading: true, + action: 'read', onSuccess: function(res) { var option = ``; $.each(res.output, function(key, item) { @@ -163,6 +170,7 @@ autoPrepare: true, checkRequired: 0, noLoading: true, + action: 'read', onSuccess: function(res) { var option = ``; $.each(res.output, function(key, item) { @@ -183,6 +191,7 @@ autoPrepare: true, checkRequired: 0, noLoading: true, + action: 'read', onSuccess: function(res) { var option = ``; $.each(res.output, function(key, item) { diff --git a/app/inventory/api/engine/manage_warehouse.php b/app/inventory/api/engine/manage_warehouse.php index 7caeb32..d0425c1 100644 --- a/app/inventory/api/engine/manage_warehouse.php +++ b/app/inventory/api/engine/manage_warehouse.php @@ -5,6 +5,8 @@ require '../../../assets/utils/db_auth.php'; $id = (int)($data['id'] ?? 0); +$pdo2->beginTransaction(); + // get existing log $sql = "SELECT `log` FROM md_warehouse @@ -51,6 +53,7 @@ if ($id > 0) { ":status" => (int)($data["status"] ?? 1), ":log" => json_encode($table_log, JSON_UNESCAPED_UNICODE), ]); + db_check($sth, $answer); } else { @@ -70,10 +73,25 @@ if ($id > 0) { ":status" => (int)($data["status"] ?? 1), ":log" => json_encode($table_log, JSON_UNESCAPED_UNICODE), ]); + db_check($sth, $answer); + + + + 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); + } } -db_check($sth, $answer); +$pdo2->commit(); $answer["success"] = 1; exit(json_encode($answer)); diff --git a/app/inventory/manage_storage.php b/app/inventory/manage_storage.php index a018887..8185301 100644 --- a/app/inventory/manage_storage.php +++ b/app/inventory/manage_storage.php @@ -90,6 +90,7 @@ autoPrepare: true, checkRequired: 0, debugMode: 0, + action: 'manage', onSuccess: function(res) { window.location.href = "inventory/warehouse.php"; @@ -106,6 +107,7 @@ url: "inventory/api/engine/warehouse.php", autoPrepare: true, checkRequired: 0, + action: 'read', onSuccess: function(res) { var option = ``; $.each(res.output, function(key, item) { @@ -124,6 +126,7 @@ url: "inventory/api/engine/retrieve_storage.php", autoPrepare: true, checkRequired: 0, + action: 'read', onSuccess: function(res) { $.each(res.output, function(key, item) { diff --git a/app/inventory/manage_warehouse.php b/app/inventory/manage_warehouse.php index 06ebc43..0b80f35 100644 --- a/app/inventory/manage_warehouse.php +++ b/app/inventory/manage_warehouse.php @@ -81,7 +81,7 @@ return ajax_request({ url: "inventory/api/engine/manage_warehouse.php", autoPrepare: true, - checkRequired: 0, + checkRequired: 1, debugMode: 0, action: 'manage', onSuccess: function(res) {