Files

109 lines
3.7 KiB
Python
Raw Permalink Normal View History

2016-06-27 19:50:54 +02:00
# 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.model import ModelView, fields
2021-12-11 17:58:43 +01:00
from trytond.pool import Pool, PoolMeta
2016-06-27 19:50:54 +02:00
from trytond.pyson import Eval
2021-12-11 17:58:43 +01:00
from trytond.wizard import Button, StateTransition, StateView, Wizard
2016-06-27 19:50:54 +02:00
2018-06-26 19:11:49 +02:00
class Production(metaclass=PoolMeta):
2016-06-27 19:50:54 +02:00
__name__ = 'production'
@classmethod
def __setup__(cls):
2025-04-04 00:45:18 +02:00
super().__setup__()
2016-06-27 19:50:54 +02:00
cls._buttons.update({
'split_wizard': {
'invisible': (~Eval('state').in_(['request', 'draft'])
| ~Eval('bom') | ~Eval('quantity', 0)),
2018-02-19 11:39:07 +01:00
'depends': ['state', 'bom', 'quantity'],
2016-06-27 19:50:54 +02:00
},
})
@classmethod
@ModelView.button_action('production_split.wizard_split_production')
def split_wizard(cls, productions):
pass
2023-08-19 15:22:30 +02:00
def split(self, quantity, unit, count=None):
2016-06-27 19:50:54 +02:00
"""
Split production into productions of quantity.
If count is not defined, the production will be split until remainder
is less than quantity.
Return the split productions
"""
pool = Pool()
Uom = pool.get('product.uom')
productions = [self]
2025-10-28 11:45:10 +01:00
n_productions = 0
2023-08-19 15:22:30 +02:00
remainder = Uom.compute_qty(self.unit, self.quantity, unit)
2016-06-27 19:50:54 +02:00
if remainder <= quantity:
return productions
self.quantity = quantity
2023-08-19 15:22:30 +02:00
self.unit = unit
2016-06-27 19:50:54 +02:00
self.save()
remainder -= quantity
if count:
count -= 1
while (remainder > quantity
and (count or count is None)):
2025-10-28 11:45:10 +01:00
n_productions += 1
remainder -= quantity
remainder = unit.round(remainder)
if count:
count -= 1
if n_productions:
productions.extend(self.copy([self] * n_productions, {
'reference': lambda data: data['reference'],
2016-06-27 19:50:54 +02:00
'quantity': quantity,
2023-08-19 15:22:30 +02:00
'unit': unit.id,
2016-06-27 19:50:54 +02:00
'inputs': None,
'outputs': None,
}))
assert remainder >= 0
if remainder:
productions.extend(self.copy([self], {
'quantity': remainder,
2023-08-19 15:22:30 +02:00
'unit': unit.id,
2016-06-27 19:50:54 +02:00
}))
for production in productions:
production.explode_bom()
self.__class__.save(productions)
return productions
class SplitProduction(Wizard):
__name__ = 'production.split'
start = StateView('production.split.start',
'production_split.split_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Split', 'split', 'tryton-ok', default=True),
])
split = StateTransition()
def default_start(self, fields):
return {
2023-08-19 15:22:30 +02:00
'unit': self.record.unit.id,
'uom_category': self.record.unit.category.id,
}
2016-06-27 19:50:54 +02:00
def transition_split(self):
self.record.split(
2023-08-19 15:22:30 +02:00
self.start.quantity, self.start.unit, count=self.start.count)
2016-06-27 19:50:54 +02:00
return 'end'
class SplitProductionStart(ModelView):
__name__ = 'production.split.start'
count = fields.Integer('Count', help='The limit number of productions')
2023-08-19 15:22:30 +02:00
quantity = fields.Float("Quantity", digits='unit', required=True)
unit = fields.Many2One(
'product.uom', "Unit", required=True,
2016-06-27 19:50:54 +02:00
domain=[
('category', '=', Eval('uom_category', -1)),
])
2023-08-20 12:17:27 +02:00
uom_category = fields.Many2One(
'product.uom.category', "UoM Category", readonly=True,
help="The category of Unit of Measure.")