From 58e50dc498190255bea3fedf2d74efbeaff437a6 Mon Sep 17 00:00:00 2001 From: "sas.fajri" Date: Sun, 24 May 2026 10:22:09 +0700 Subject: [PATCH] Fix order location duplicates --- .../fo/cashiernewpayment-v27/Payment.php | 68 ++++++++++--------- ...26-05-24-fix-order-location-duplicates.sql | 46 +++++++++++++ 2 files changed, 83 insertions(+), 31 deletions(-) create mode 100644 sql/manual_changes/2026-05-24-fix-order-location-duplicates.sql diff --git a/application/controllers/mockup/fo/cashiernewpayment-v27/Payment.php b/application/controllers/mockup/fo/cashiernewpayment-v27/Payment.php index 7b02056e..7895798d 100644 --- a/application/controllers/mockup/fo/cashiernewpayment-v27/Payment.php +++ b/application/controllers/mockup/fo/cashiernewpayment-v27/Payment.php @@ -579,37 +579,43 @@ class Payment extends MY_Controller } } - function save_control() - { - if (! $this->isLogin) { - $this->sys_error("Invalid Token"); - exit; - } - $prm = $this->sys_input; - $userid = $this->sys_user['M_UserID']; - if ($prm['data'] && count($prm['data']) > 0) { - foreach ($prm['data'] as $key => $value) { - $sql = "INSERT INTO t_order_location ( - T_OrderLocationT_OrderHeaderID, - T_OrderLocationM_LocationID, - T_OrderLocationT_SampleStationID, - T_OrderLocationCreated, - T_OrderLocationLastUpdated, - T_OrderLocationUserID - ) - VALUES (?,?,?,NOW(),NOW(),?) - ON DUPLICATE KEY - UPDATE T_OrderLocationT_OrderHeaderID = ?, - T_OrderLocationM_LocationID = ?, - T_OrderLocationT_SampleStationID = ?, - T_OrderLocationLastUpdated = NOW(), - T_OrderLocationUserID = ?"; - $query = $this->db_onedev->query($sql, array($value['order_id'], $value['location_id'], $value['station_id'], $userid, $value['order_id'], $value['location_id'], $value['station_id'], $userid)); - } - $this->sys_ok(["datas" => '']); - } else { - $this->sys_error_db("data not valid", $this->db_onedev); - exit; + function save_control() + { + if (! $this->isLogin) { + $this->sys_error("Invalid Token"); + exit; + } + $prm = $this->sys_input; + $userid = $this->sys_user['M_UserID']; + if ($prm['data'] && count($prm['data']) > 0) { + foreach ($prm['data'] as $key => $value) { + $sql = "UPDATE t_order_location + SET + T_OrderLocationIsActive = 'N', + T_OrderLocationLastUpdated = NOW(), + T_OrderLocationUserID = ? + WHERE + T_OrderLocationT_OrderHeaderID = ? AND + T_OrderLocationT_SampleStationID = ? AND + T_OrderLocationIsActive = 'Y'"; + $this->db_onedev->query($sql, array($userid, $value['order_id'], $value['station_id'])); + + $sql = "INSERT INTO t_order_location ( + T_OrderLocationT_OrderHeaderID, + T_OrderLocationM_LocationID, + T_OrderLocationT_SampleStationID, + T_OrderLocationIsActive, + T_OrderLocationCreated, + T_OrderLocationLastUpdated, + T_OrderLocationUserID + ) + VALUES (?,?,?,'Y',NOW(),NOW(),?)"; + $query = $this->db_onedev->query($sql, array($value['order_id'], $value['location_id'], $value['station_id'], $userid)); + } + $this->sys_ok(["datas" => '']); + } else { + $this->sys_error_db("data not valid", $this->db_onedev); + exit; } } diff --git a/sql/manual_changes/2026-05-24-fix-order-location-duplicates.sql b/sql/manual_changes/2026-05-24-fix-order-location-duplicates.sql new file mode 100644 index 00000000..d4da5353 --- /dev/null +++ b/sql/manual_changes/2026-05-24-fix-order-location-duplicates.sql @@ -0,0 +1,46 @@ +-- Make fn_get_location tolerate duplicate active rows and clean existing duplicates. +-- Applied on one_lab. + +DROP FUNCTION IF EXISTS fn_get_location; +DELIMITER $$ +CREATE FUNCTION fn_get_location(PT_SampleStationID INT, PT_OrderHeaderID INT) RETURNS INT + READS SQL DATA +BEGIN + DECLARE RTN INT DEFAULT 0; + + SELECT T_OrderLocationM_LocationID INTO RTN + FROM t_order_location + WHERE T_OrderLocationT_OrderHeaderID = PT_OrderHeaderID + AND T_OrderLocationT_SampleStationID = PT_SampleStationID + AND T_OrderLocationIsActive = 'Y' + ORDER BY T_OrderLocationID DESC + LIMIT 1; + + IF RTN = 0 THEN + SELECT M_LocationID INTO RTN + FROM m_location + WHERE M_LocationIsActive = 'Y' + AND M_LocationT_SampleStationID = PT_SampleStationID + AND M_LocationPriority = ( + SELECT MAX(M_LocationPriority) + FROM m_location + WHERE M_LocationT_SampleStationID = PT_SampleStationID + AND M_LocationIsActive = 'Y' + ) + LIMIT 1; + END IF; + + RETURN RTN; +END$$ +DELIMITER ; + +-- Cleanup duplicate active rows so each order + sample station keeps only the newest active row. +UPDATE t_order_location current_row +JOIN t_order_location newest_row + ON newest_row.T_OrderLocationT_OrderHeaderID = current_row.T_OrderLocationT_OrderHeaderID + AND newest_row.T_OrderLocationT_SampleStationID = current_row.T_OrderLocationT_SampleStationID + AND newest_row.T_OrderLocationIsActive = 'Y' + AND newest_row.T_OrderLocationID > current_row.T_OrderLocationID +SET current_row.T_OrderLocationIsActive = 'N', + current_row.T_OrderLocationLastUpdated = NOW() +WHERE current_row.T_OrderLocationIsActive = 'Y';