trytond-stock_unit_load/stock.py

140 lines
4.2 KiB
Python

# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from functools import wraps
import datetime
from trytond.pool import PoolMeta, Pool
from trytond.model import fields, ModelView, Workflow
from trytond.pyson import PYSONEncoder, Date, Bool
from trytond.transaction import Transaction
from trytond.wizard import Wizard, StateView, Button, StateAction
__all__ = ['Move', 'UnitLoadsByLocationsStart',
'UnitLoadsByLocations', 'Move2']
def set_unit_load_shipment(func):
@wraps(func)
def wrapper(cls, moves):
pool = Pool()
UnitLoad = pool.get('stock.unit_load')
uls_to_save = list(set([move.unit_load for move in moves
if move.unit_load]))
func(cls, moves)
UnitLoad.set_shipment(uls_to_save)
return wrapper
class Move(metaclass=PoolMeta):
__name__ = 'stock.move'
unit_load = fields.Many2One('stock.unit_load', 'Unit load',
ondelete='RESTRICT', select=True, states={
'readonly': Bool(True)
})
@classmethod
def validate(cls, records):
super(Move, cls).validate(records)
for record in records:
if record.unit_load and record.time_ is None:
cls.raise_user_error('ul_time_required')
@classmethod
def __setup__(cls):
super(Move, cls).__setup__()
cls._error_messages.update({
'ul_time_required': 'Time in Unit load moves is required.'})
@classmethod
@set_unit_load_shipment
@Workflow.transition('assigned')
def assign(cls, moves):
super().assign(moves)
@classmethod
@set_unit_load_shipment
@Workflow.transition('done')
def do(cls, moves):
super().do(moves)
@classmethod
@set_unit_load_shipment
@Workflow.transition('cancel')
def cancel(cls, moves):
super().cancel(moves)
@classmethod
@set_unit_load_shipment
def delete(cls, moves):
super().delete(moves)
@classmethod
def _get_origin(cls):
origins = super()._get_origin()
return origins + ['stock.unit_load']
class UnitLoadsByLocationsStart(ModelView):
"""Unit loads by Locations"""
__name__ = 'stock.unit_loads_by_locations.start'
forecast_date = fields.Date(
'At Date', help=('Allow to compute expected '
'stock quantities for this date.\n'
'* An empty value is an infinite date in the future.\n'
'* A date in the past will provide historical values.'))
@staticmethod
def default_forecast_date():
Date_ = Pool().get('ir.date')
return Date_.today()
class UnitLoadsByLocations(Wizard):
"""Unit loads by Locations"""
__name__ = 'stock.unit_loads_by_locations'
start = StateView('stock.unit_loads_by_locations.start',
'stock_unit_load.uls_by_locations_start_view_form',
[Button('Cancel', 'end', 'tryton-cancel'),
Button('Open', 'open', 'tryton-ok', True)])
open = StateAction('stock_unit_load.act_uls_by_locations')
def do_open(self, action):
pool = Pool()
Location = pool.get('stock.location')
Lang = pool.get('ir.lang')
context = {}
context['locations'] = Transaction().context.get('active_ids')
date = self.start.forecast_date or datetime.date.max
context['stock_date_end'] = Date(date.year, date.month, date.day)
action['pyson_context'] = PYSONEncoder().encode(context)
locations = Location.browse(context['locations'])
for code in [Transaction().language, 'en_US']:
langs = Lang.search([
('code', '=', code),
])
if langs:
break
lang = langs[0]
date = lang.strftime(date)
action['name'] += ' - (%s) @ %s' % (
','.join(l.name for l in locations), date)
return action, {}
class Move2(metaclass=PoolMeta):
__name__ = 'stock.move'
@fields.depends('state', 'origin', 'shipment', 'unit_load')
def on_change_with_cancelation_allowed(self, name=None):
res = super(Move, self).on_change_with_cancelation_allowed(name=name)
return res and not self.unit_load