From 887d4e0603a6d3224622695edb9656e1365079bc Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 31 Aug 2020 20:02:06 +0200 Subject: [PATCH] Add unique key on table of inventory details --- .../install/mysql/migration/12.0.0-13.0.0.sql | 6 +++++- .../mysql/tables/llx_inventorydet.key.sql | 3 +++ .../install/mysql/tables/llx_inventorydet.sql | 20 +++++++++---------- htdocs/product/inventory/inventory.php | 9 +++++++-- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/htdocs/install/mysql/migration/12.0.0-13.0.0.sql b/htdocs/install/mysql/migration/12.0.0-13.0.0.sql index b73826a21ad..92f08f43e67 100644 --- a/htdocs/install/mysql/migration/12.0.0-13.0.0.sql +++ b/htdocs/install/mysql/migration/12.0.0-13.0.0.sql @@ -301,4 +301,8 @@ insert into llx_c_action_trigger (code,label,description,elementtype,rang) value ALTER TABLE llx_actioncomm_reminder ADD COLUMN entity integer NOT NULL DEFAULT 1; ALTER TABLE llx_actioncomm_reminder ADD COLUMN fk_actioncomm integer NOT NULL; ALTER TABLE llx_actioncomm_reminder ADD COLUMN fk_email_template integer; -ALTER TABLE llx_actioncomm_reminder DROP INDEX uk_actioncomm_reminder_unique, ADD UNIQUE uk_actioncomm_reminder_unique (fk_user, typeremind, offsetvalue, offsetunit, fk_actioncomm); \ No newline at end of file +ALTER TABLE llx_actioncomm_reminder DROP INDEX uk_actioncomm_reminder_unique, ADD UNIQUE uk_actioncomm_reminder_unique (fk_user, typeremind, offsetvalue, offsetunit, fk_actioncomm); + +ALTER TABLE llx_inventorydet ADD UNIQUE uk_inventorydet(fk_inventory, fk_warehouse, fk_product, batch); + + diff --git a/htdocs/install/mysql/tables/llx_inventorydet.key.sql b/htdocs/install/mysql/tables/llx_inventorydet.key.sql index 44890608b7e..cfe82448fd6 100644 --- a/htdocs/install/mysql/tables/llx_inventorydet.key.sql +++ b/htdocs/install/mysql/tables/llx_inventorydet.key.sql @@ -20,3 +20,6 @@ ALTER TABLE llx_inventorydet ADD INDEX idx_inventorydet_tms (tms); ALTER TABLE llx_inventorydet ADD INDEX idx_inventorydet_datec (datec); ALTER TABLE llx_inventorydet ADD INDEX idx_inventorydet_fk_inventory (fk_inventory); + +ALTER TABLE llx_inventorydet ADD UNIQUE uk_inventorydet(fk_inventory, fk_warehouse, fk_product, batch); + diff --git a/htdocs/install/mysql/tables/llx_inventorydet.sql b/htdocs/install/mysql/tables/llx_inventorydet.sql index 161e34183a0..8c770895194 100644 --- a/htdocs/install/mysql/tables/llx_inventorydet.sql +++ b/htdocs/install/mysql/tables/llx_inventorydet.sql @@ -19,15 +19,15 @@ CREATE TABLE llx_inventorydet ( -rowid integer NOT NULL AUTO_INCREMENT PRIMARY KEY, -datec datetime DEFAULT NULL, -tms timestamp, -fk_inventory integer DEFAULT 0, -fk_warehouse integer DEFAULT 0, -fk_product integer DEFAULT 0, -batch varchar(128) DEFAULT NULL, -- Lot or serial number -qty_stock double DEFAULT NULL, -- The targeted value. can be filled during draft edition -qty_view double DEFAULT NULL, -- must be filled once regulation is done -qty_regulated double DEFAULT NULL -- must be filled once regulation is done + rowid integer NOT NULL AUTO_INCREMENT PRIMARY KEY, + datec datetime DEFAULT NULL, + tms timestamp, + fk_inventory integer DEFAULT 0, + fk_warehouse integer DEFAULT 0, + fk_product integer DEFAULT 0, + batch varchar(128) DEFAULT NULL, -- Lot or serial number + qty_stock double DEFAULT NULL, -- The targeted value. can be filled during draft edition + qty_view double DEFAULT NULL, -- must be filled once regulation is done + qty_regulated double DEFAULT NULL -- must be filled once regulation is done ) ENGINE=innodb; diff --git a/htdocs/product/inventory/inventory.php b/htdocs/product/inventory/inventory.php index d9a5e081496..a93b31dcd46 100644 --- a/htdocs/product/inventory/inventory.php +++ b/htdocs/product/inventory/inventory.php @@ -132,7 +132,8 @@ if (empty($reshook)) $error++; setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Product")), null, 'errors'); } - if (! $error) { + + if (! $error && ! empty($conf->productbatch->enabled)) { $tmpproduct = new Product($db); $result = $tmpproduct->fetch($fk_product); @@ -157,7 +158,11 @@ if (empty($reshook)) $result = $tmp->create($user); if ($result < 0) { - dol_print_error($db, $tmp->error, $tmp->errors); + if ($db->lasterrno() == 'DB_ERROR_RECORD_ALREADY_EXISTS') { + setEventMessages($langs->trans("DuplicateRecord"), null, 'errors'); + } else { + dol_print_error($db, $tmp->error, $tmp->errors); + } } } }