changeset 2987a2201834 in modules/stock_lot:default
details: https://hg.tryton.org/modules/stock_lot?cmd=changeset;node=2987a2201834
description:
Improve moves synchronisation on shipments
By using the origin as synchronisation key, the code is simplified but
also it
allows to follow when products are changed.
The same design is applied to the internal shipment which allows to
synchronise
also the lots.
issue8363
review261811002
diffstat:
CHANGELOG | 2 +
__init__.py | 1 +
stock.py | 48 +++++-------------------------
tests/scenario_stock_lot_shipment_out.rst | 47 +++++++++++++----------------
4 files changed, 33 insertions(+), 65 deletions(-)
diffs (180 lines):
diff -r bb8ee89f9a45 -r 2987a2201834 CHANGELOG
--- a/CHANGELOG Fri Aug 09 16:40:27 2019 +0200
+++ b/CHANGELOG Sun Aug 18 19:22:43 2019 +0200
@@ -1,3 +1,5 @@
+* Synchronize lot on internal shipment
+
Version 5.2.0 - 2019-05-06
* Bug fixes (see mercurial logs for details)
diff -r bb8ee89f9a45 -r 2987a2201834 __init__.py
--- a/__init__.py Fri Aug 09 16:40:27 2019 +0200
+++ b/__init__.py Sun Aug 18 19:22:43 2019 +0200
@@ -15,6 +15,7 @@
stock.ShipmentIn,
stock.ShipmentOut,
stock.ShipmentOutReturn,
+ stock.ShipmentInternal,
stock.Period,
stock.PeriodCacheLot,
stock.Inventory,
diff -r bb8ee89f9a45 -r 2987a2201834 stock.py
--- a/stock.py Fri Aug 09 16:40:27 2019 +0200
+++ b/stock.py Sun Aug 18 19:22:43 2019 +0200
@@ -1,7 +1,6 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import datetime
-from collections import defaultdict
from functools import wraps
from trytond.i18n import gettext
@@ -161,44 +160,8 @@
class ShipmentOut(metaclass=PoolMeta):
__name__ = 'stock.shipment.out'
- @classmethod
- def _sync_inventory_to_outgoing(cls, shipments, create=True, write=True):
- pool = Pool()
- Uom = pool.get('product.uom')
- Move = pool.get('stock.move')
- super(ShipmentOut, cls)._sync_inventory_to_outgoing(
- shipments, create=create, write=write)
- to_write = []
- for shipment in shipments:
- outgoing_by_product = defaultdict(list)
- for move in shipment.outgoing_moves:
- outgoing_by_product[move.product.id].append(move)
- for move in shipment.inventory_moves:
- if not move.lot:
- continue
- quantity = Uom.compute_qty(move.uom, move.quantity,
- move.product.default_uom, round=False)
- outgoing_moves = outgoing_by_product[move.product.id]
- while outgoing_moves and quantity > 0:
- out_move = outgoing_moves.pop()
- out_quantity = Uom.compute_qty(out_move.uom,
- out_move.quantity, out_move.product.default_uom,
- round=False)
- values = {}
- if quantity < out_quantity:
- with Transaction().set_context(_stock_move_split=True):
- outgoing_moves.extend(
- Move.copy([out_move], default={
- 'quantity': out_move.uom.round(
- out_quantity - quantity),
- }))
- values['quantity'] = out_move.uom.round(quantity)
- values['lot'] = move.lot.id
- to_write.extend(([out_move], values))
- quantity -= out_quantity
- assert move.uom.round(quantity) <= 0
- if to_write:
- Move.write(*to_write)
+ def _sync_move_key(self, move):
+ return super()._sync_move_key(move) + (('lot', move.lot),)
class ShipmentOutReturn(metaclass=PoolMeta):
@@ -211,6 +174,13 @@
return move
+class ShipmentInternal(metaclass=PoolMeta):
+ __name__ = 'stock.shipment.internal'
+
+ def _sync_move_key(self, move):
+ return super()._sync_move_key(move) + (('lot', move.lot),)
+
+
class Period(metaclass=PoolMeta):
__name__ = 'stock.period'
lot_caches = fields.One2Many('stock.period.cache.lot', 'period',
diff -r bb8ee89f9a45 -r 2987a2201834 tests/scenario_stock_lot_shipment_out.rst
--- a/tests/scenario_stock_lot_shipment_out.rst Fri Aug 09 16:40:27 2019 +0200
+++ b/tests/scenario_stock_lot_shipment_out.rst Sun Aug 18 19:22:43 2019 +0200
@@ -59,12 +59,7 @@
>>> shipment_out.customer = customer
>>> shipment_out.warehouse = warehouse_loc
>>> shipment_out.company = company
-
-Add two shipment lines of same product::
-
- >>> StockMove = Model.get('stock.move')
- >>> move = StockMove()
- >>> shipment_out.outgoing_moves.append(move)
+ >>> move = shipment_out.outgoing_moves.new()
>>> move.product = product
>>> move.uom =unit
>>> move.quantity = 10
@@ -73,39 +68,38 @@
>>> move.company = company
>>> move.unit_price = Decimal('1')
>>> move.currency = company.currency
- >>> move = StockMove()
- >>> shipment_out.outgoing_moves.append(move)
- >>> move.product = product
- >>> move.uom =unit
- >>> move.quantity = 4
- >>> move.from_location = output_loc
- >>> move.to_location = customer_loc
- >>> move.company = company
- >>> move.unit_price = Decimal('1')
- >>> move.currency = company.currency
>>> shipment_out.save()
Set the shipment state to waiting::
>>> shipment_out.click('wait')
>>> len(shipment_out.outgoing_moves)
- 2
+ 1
>>> len(shipment_out.inventory_moves)
- 2
+ 1
+
+Split inventory move::
-Assign the shipment with 2 lines of 7 products::
+ >>> move, = shipment_out.inventory_moves
+ >>> move.quantity = 7
+ >>> move.save()
+ >>> with config.set_context(_stock_move_split=True):
+ ... _ = move.duplicate(default=dict(quantity=3))
+ >>> shipment_out.reload()
- >>> for move in shipment_out.inventory_moves:
- ... move.quantity = 7
+Assign the shipment::
+
>>> shipment_out.click('assign_force')
>>> shipment_out.state
'assigned'
+ >>> len(shipment_out.outgoing_moves)
+ 1
Set 2 lots::
>>> Lot = Model.get('stock.lot')
>>> for i, move in enumerate(shipment_out.inventory_moves, start=1):
- ... lot = Lot(number='%05i' % i, product=product)
+ ... lot = Lot(number='%05i' % i, product=move.product)
... lot.save()
... move.lot = lot
>>> shipment_out.save()
@@ -118,10 +112,11 @@
>>> len(shipment_out.outgoing_moves)
3
>>> sorted([m.quantity for m in shipment_out.outgoing_moves])
- [3.0, 4.0, 7.0]
+ [0.0, 3.0, 7.0]
>>> lot_quantities = {}
>>> for move in shipment_out.outgoing_moves:
- ... quantity = lot_quantities.setdefault(move.lot.number, 0)
- ... lot_quantities[move.lot.number] += move.quantity
+ ... number = move.lot.number if move.lot else ''
+ ... quantity = lot_quantities.setdefault(number, 0)
+ ... lot_quantities[number] += move.quantity
>>> sorted(lot_quantities.items())
- [('00001', 7.0), ('00002', 7.0)]
+ [('', 0.0), ('00001', 3.0), ('00002', 7.0)]