From e052849c3d9be7c4611e90c602cf6d87b7cf70ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 18 Mar 2024 18:35:22 +0100 Subject: [PATCH] Use split function to add lots on move when available The split function ensures that customer moves of drop shipment are split also when splitting supplier moves. Closes #13037 --- modules/stock_lot/setup.py | 5 ++- modules/stock_lot/stock.py | 38 ++++++++++++------- .../tests/scenario_stock_lot_move_add.json | 4 ++ .../tests/scenario_stock_lot_move_add.rst | 5 ++- modules/stock_lot/tests/test_module.py | 1 + modules/stock_lot/tryton.cfg | 1 + 6 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 modules/stock_lot/tests/scenario_stock_lot_move_add.json diff --git a/modules/stock_lot/setup.py b/modules/stock_lot/setup.py index 654f6f9c10..583af7850a 100755 --- a/modules/stock_lot/setup.py +++ b/modules/stock_lot/setup.py @@ -47,7 +47,10 @@ for dep in info.get('depends', []): requires.append(get_require_version('trytond_%s' % dep)) requires.append(get_require_version('trytond')) -tests_require = [get_require_version('proteus')] +tests_require = [ + get_require_version('proteus'), + get_require_version('trytond_stock_split'), + ] setup(name=name, version=version, diff --git a/modules/stock_lot/stock.py b/modules/stock_lot/stock.py index 2de1cb7c37..0398014bb7 100644 --- a/modules/stock_lot/stock.py +++ b/modules/stock_lot/stock.py @@ -541,20 +541,32 @@ class MoveAddLots(Wizard): lot = line.get_lot(self.record) lots.append(lot) Lot.save(lots) - if quantity_remaining: - self.record.quantity = quantity_remaining - self.record.save() - for i, (line, lot) in enumerate(zip(self.start.lots, lots)): - if not i and not quantity_remaining: - self.record.quantity = line.quantity - self.record.lot = lot + if hasattr(self.model, 'split'): + move = self.record + for line, lot in zip(self.start.lots, lots): + splits = move.split(line.quantity, self.record.unit, count=1) + splits.remove(move) + move.lot = lot + move.save() + if splits: + move, = splits + else: + break + else: + if quantity_remaining: + self.record.quantity = quantity_remaining self.record.save() - else: - with Transaction().set_context(_stock_move_split=True): - self.model.copy([self.record], { - 'quantity': line.quantity, - 'lot': lot.id, - }) + for i, (line, lot) in enumerate(zip(self.start.lots, lots)): + if not i and not quantity_remaining: + self.record.quantity = line.quantity + self.record.lot = lot + self.record.save() + else: + with Transaction().set_context(_stock_move_split=True): + self.model.copy([self.record], { + 'quantity': line.quantity, + 'lot': lot.id, + }) return 'end' diff --git a/modules/stock_lot/tests/scenario_stock_lot_move_add.json b/modules/stock_lot/tests/scenario_stock_lot_move_add.json new file mode 100644 index 0000000000..fd03b4616c --- /dev/null +++ b/modules/stock_lot/tests/scenario_stock_lot_move_add.json @@ -0,0 +1,4 @@ +[ + {"stock_split": false} + ,{"stock_split": true} +] diff --git a/modules/stock_lot/tests/scenario_stock_lot_move_add.rst b/modules/stock_lot/tests/scenario_stock_lot_move_add.rst index 6c6fb0c2ed..be5741687d 100644 --- a/modules/stock_lot/tests/scenario_stock_lot_move_add.rst +++ b/modules/stock_lot/tests/scenario_stock_lot_move_add.rst @@ -12,7 +12,10 @@ Imports:: Activate modules:: - >>> config = activate_modules('stock_lot') + >>> modules = ['stock_lot'] + >>> if globals().get('stock_split', False): + ... modules.append('stock_split') + >>> config = activate_modules(modules) Create company:: diff --git a/modules/stock_lot/tests/test_module.py b/modules/stock_lot/tests/test_module.py index 0f5e93b360..2f35628bcb 100644 --- a/modules/stock_lot/tests/test_module.py +++ b/modules/stock_lot/tests/test_module.py @@ -14,6 +14,7 @@ from trytond.transaction import Transaction class StockLotTestCase(CompanyTestMixin, ModuleTestCase): 'Test Stock Lot module' module = 'stock_lot' + extras = ['stock_split'] @with_transaction() def test_products_by_location(self): diff --git a/modules/stock_lot/tryton.cfg b/modules/stock_lot/tryton.cfg index b00ed820a4..25f5f3888d 100644 --- a/modules/stock_lot/tryton.cfg +++ b/modules/stock_lot/tryton.cfg @@ -6,6 +6,7 @@ depends: stock extras_depend: sale_supply_drop_shipment + stock_split xml: ir.xml stock.xml