Factorize explode bom to use depends

Closes #11916
This commit is contained in:
Cédric Krier
2022-12-26 13:22:06 +01:00
parent bd3bbe72e0
commit 1df14f932b
3 changed files with 42 additions and 51 deletions
+30 -37
View File
@@ -21,9 +21,6 @@ from trytond.transaction import Transaction
from .exceptions import CostWarning
BOM_CHANGES = ['bom', 'product', 'quantity', 'uom', 'warehouse', 'location',
'company', 'inputs', 'outputs']
class Production(ShipmentAssignMixin, Workflow, ModelSQL, ModelView):
"Production"
@@ -357,31 +354,37 @@ class Production(ShipmentAssignMixin, Workflow, ModelSQL, ModelView):
models = cls._get_origin()
return [(None, '')] + [(m, get_name(m)) for m in models]
def _move(self, from_location, to_location, company, product, uom,
quantity):
Move = Pool().get('stock.move')
@fields.depends(
'company', 'location', methods=['picking_location', 'output_location'])
def _move(self, type, product, uom, quantity):
pool = Pool()
Move = pool.get('stock.move')
assert type in {'input', 'output'}
move = Move(
product=product,
uom=uom,
quantity=quantity,
from_location=from_location,
to_location=to_location,
company=company,
currency=company.currency if company else None,
state='draft',
)
company=self.company,
currency=self.company.currency if self.company else None)
if type == 'input':
move.from_location = self.picking_location
move.to_location = self.location
move.production_input = self
else:
move.from_location = self.location
move.to_location = self.output_location
move.production_output = self
return move
def _explode_move_values(self, from_location, to_location, company,
bom_io, quantity):
move = self._move(from_location, to_location, company,
bom_io.product, bom_io.uom, quantity)
move.from_location = from_location.id if from_location else None
move.to_location = to_location.id if to_location else None
@fields.depends(methods=['_move'])
def _explode_move_values(self, type, bom_io, quantity):
move = self._move(type, bom_io.product, bom_io.uom, quantity)
move.unit_price_required = move.on_change_with_unit_price_required()
return move
@fields.depends(*BOM_CHANGES)
@fields.depends(
'bom', 'product', 'uom', 'quantity', 'company', 'inputs', 'outputs',
methods=['_explode_move_values'])
def explode_bom(self):
pool = Pool()
Uom = pool.get('product.uom')
@@ -393,9 +396,7 @@ class Production(ShipmentAssignMixin, Workflow, ModelSQL, ModelView):
inputs = []
for input_ in self.bom.inputs:
quantity = input_.compute_quantity(factor)
move = self._explode_move_values(
self.picking_location, self.location, self.company,
input_, quantity)
move = self._explode_move_values('input', input_, quantity)
if move:
inputs.append(move)
quantity = Uom.compute_qty(input_.uom, quantity,
@@ -405,9 +406,7 @@ class Production(ShipmentAssignMixin, Workflow, ModelSQL, ModelView):
outputs = []
for output in self.bom.outputs:
quantity = output.compute_quantity(factor)
move = self._explode_move_values(
self.location, self.output_location, self.company, output,
quantity)
move = self._explode_move_values('output', output, quantity)
if move:
move.unit_price = Decimal(0)
move.currency = self.company.currency
@@ -487,17 +486,12 @@ class Production(ShipmentAssignMixin, Workflow, ModelSQL, ModelView):
Move = pool.get('stock.move')
to_save = []
for production in productions:
location = production.location
company = production.company
if not production.bom:
if production.product:
move = production._move(
location, production.output_location, company,
production.product, production.uom,
'input', production.product, production.uom,
production.quantity)
if move:
move.production_output = production
move.unit_price = Decimal(0)
move.currency = production.company.currency
to_save.append(move)
@@ -509,19 +503,16 @@ class Production(ShipmentAssignMixin, Workflow, ModelSQL, ModelView):
quantity = input_.compute_quantity(factor)
product = input_.product
move = production._move(
production.picking_location, location, company, product,
input_.uom, quantity)
'input', product, input_.uom, quantity)
if move:
move.production_input = production
to_save.append(move)
for output in production.bom.outputs:
quantity = output.compute_quantity(factor)
product = output.product
move = production._move(location, production.output_location,
company, product, output.uom, quantity)
move = production._move(
'output', product, output.uom, quantity)
if move:
move.production_output = production
move.unit_price = Decimal(0)
move.currency = production.company.currency
to_save.append(move)
@@ -817,12 +808,14 @@ class Production(ShipmentAssignMixin, Workflow, ModelSQL, ModelView):
cls.save(productions)
@property
@fields.depends('warehouse')
def picking_location(self):
if self.warehouse:
return (self.warehouse.production_picking_location
or self.warehouse.storage_location)
@property
@fields.depends('warehouse')
def output_location(self):
if self.warehouse:
return (self.warehouse.production_output_location
@@ -10,7 +10,6 @@ Imports::
>>> from trytond.tests.tools import activate_modules
>>> from trytond.modules.company.tests.tools import create_company, \
... get_company
>>> from trytond.modules.production.production import BOM_CHANGES
>>> today = dt.date.today()
>>> yesterday = today - dt.timedelta(days=1)
@@ -148,7 +147,11 @@ Test reset bom button::
>>> for input in production.inputs:
... input.quantity += 1
>>> production.click('reset_bom', change=BOM_CHANGES)
>>> production.click(
... 'reset_bom',
... change=[
... 'bom', 'product', 'uom', 'quantity',
... 'inputs', 'outputs', 'company', 'warehouse', 'location'])
>>> sorted([i.quantity for i in production.inputs]) == [10, 300]
True
>>> output, = production.outputs
+7 -12
View File
@@ -1,20 +1,15 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.pool import Pool, PoolMeta
from trytond.model import fields
from trytond.pool import PoolMeta
class Production(metaclass=PoolMeta):
__name__ = 'production'
def _explode_move_values(
self, from_location, to_location, company, bom_io, quantity):
pool = Pool()
ProductionBomOutput = pool.get('production.bom.output')
move = super(Production, self)._explode_move_values(
from_location, to_location, company, bom_io, quantity)
if move and isinstance(bom_io, ProductionBomOutput):
for product_location in bom_io.product.locations:
if product_location.warehouse != to_location.warehouse:
continue
move.to_location = product_location.location
@fields.depends('warehouse')
def _explode_move_values(self, type, bom_io, quantity):
move = super()._explode_move_values(type, bom_io, quantity)
if move and type == 'output' and self.warehouse:
move.set_product_location(warehouse=self.warehouse.id)
return move