Files
tryton/modules/stock/shipment.py
T

3194 lines
116 KiB
Python
Raw Normal View History

2014-12-02 12:56:04 +01: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.
import datetime
from collections import defaultdict
2018-09-01 01:02:04 +02:00
from functools import partial
from itertools import groupby
2018-08-09 20:43:51 +02:00
from sql import Null
2021-02-25 21:13:56 +01:00
from sql.conditionals import Coalesce
from sql.functions import CharLength
from trytond.i18n import gettext, lazy_gettext
from trytond.model import (
2025-12-02 16:01:39 +01:00
ChatMixin, Index, ModelSQL, ModelView, Workflow, dualmethod, fields, sort)
2018-12-29 14:20:29 +01:00
from trytond.model.exceptions import AccessError
2009-10-02 15:12:04 +02:00
from trytond.modules.company import CompanyReport
2020-04-10 18:53:44 +02:00
from trytond.modules.company.model import employee_field, set_employee
2021-12-11 17:58:44 +01:00
from trytond.pool import Pool
from trytond.pyson import Bool, Eval, Id, If, TimeDelta
2021-12-11 17:58:44 +01:00
from trytond.transaction import Transaction
from trytond.wizard import Button, StateTransition, StateView, Wizard
2017-02-01 21:41:00 +01:00
from .exceptions import ShipmentCheckQuantityWarning
2017-02-01 21:41:00 +01:00
2019-09-20 11:47:15 +02:00
class ShipmentMixin:
__slots__ = ()
2019-09-20 11:47:15 +02:00
_rec_name = 'number'
number = fields.Char(
"Number", readonly=True,
help="The main identifier for the shipment.")
reference = fields.Char(
"Reference",
help="The external identifier for the shipment.")
planned_date = fields.Date(
lazy_gettext('stock.msg_shipment_planned_date'),
states={
'readonly': ~Eval('state').in_(['request', 'draft']),
},
help=lazy_gettext('stock.msg_shipment_planned_date_help'))
origin_planned_date = fields.Date(
lazy_gettext('stock.msg_shipment_origin_planned_date'),
readonly=True,
help=lazy_gettext('stock.msg_shipment_origin_planned_date_help'))
effective_date = fields.Date(
lazy_gettext('stock.msg_shipment_effective_date'),
states={
'readonly': Eval('state').in_(['cancelled', 'done']),
},
help=lazy_gettext('stock.msg_shipment_effective_date_help'))
delay = fields.Function(
fields.TimeDelta(
lazy_gettext('stock.msg_shipment_delay'),
states={
'invisible': (
~Eval('origin_planned_date')
& ~Eval('planned_date')),
}),
'get_delay')
@classmethod
def __setup__(cls):
cls.number.search_unaccented = False
cls.reference.search_unaccented = False
super().__setup__()
t = cls.__table__()
cls._sql_indexes.update({
Index(t, (Coalesce(t.reference, ''), Index.Similarity())),
})
cls._order = [
('effective_date', 'ASC NULLS LAST'),
('id', 'ASC'),
]
@classmethod
def order_number(cls, tables):
table, _ = tables[None]
return [
~((table.state == 'cancelled') & (table.number == Null)),
CharLength(table.number), table.number]
@classmethod
def order_effective_date(cls, tables):
table, _ = tables[None]
return [Coalesce(table.effective_date, table.planned_date)]
2024-02-19 22:51:55 +01:00
@classmethod
def set_number(cls, shipments):
'''
Fill the number field from sequence
'''
pool = Pool()
Config = pool.get('stock.configuration')
config = Config(1)
2024-08-25 12:18:07 +02:00
sequence_name = cls.__name__[len('stock.'):].replace('.', '_')
for company, c_shipments in groupby(
shipments, key=lambda s: s.company):
c_shipments = [s for s in c_shipments if not s.number]
if c_shipments:
sequence = config.get_multivalue(
sequence_name + '_sequence', company=company.id)
for shipment, number in zip(
c_shipments, sequence.get_many(len(c_shipments))):
shipment.number = number
2024-02-19 22:51:55 +01:00
cls.save(shipments)
def get_delay(self, name):
pool = Pool()
Date = pool.get('ir.date')
planned_date = self.origin_planned_date or self.planned_date
if planned_date is not None:
if self.effective_date:
return self.effective_date - planned_date
elif self.planned_date:
return self.planned_date - planned_date
else:
with Transaction().set_context(company=self.company.id):
today = Date.today()
return today - planned_date
def get_rec_name(self, name):
items = []
if self.number:
items.append(self.number)
if self.reference:
items.append(f'[{self.reference}]')
if not items:
items.append(f'({self.id})')
return ' '.join(items)
@classmethod
def search_rec_name(cls, name, clause):
_, operator, value = clause
if operator.startswith('!') or operator.startswith('not '):
bool_op = 'AND'
else:
bool_op = 'OR'
domain = [bool_op,
('number', operator, value),
('reference', operator, value),
]
return domain
2019-09-20 11:47:15 +02:00
@classmethod
def view_attributes(cls):
2021-04-09 10:52:03 +02:00
return super().view_attributes() + [
('/tree', 'visual', If(Eval('state') == 'cancelled', 'muted', '')),
('/tree/field[@name="delay"]', 'visual',
If(Eval('delay', datetime.timedelta()) > TimeDelta(),
'danger', '')),
2019-09-20 11:47:15 +02:00
]
@classmethod
def copy(cls, shipments, default=None):
default = default.copy() if default is not None else {}
default.setdefault('number')
2025-08-01 00:10:49 +02:00
default.setdefault('reference')
return super().copy(shipments, default=default)
@classmethod
def on_modification(cls, mode, shipments, field_names=None):
pool = Pool()
Move = pool.get('stock.move')
super().on_modification(mode, shipments, field_names=field_names)
if (mode in {'create', 'write'}
and (field_names is None or 'planned_date' in field_names)):
cls._set_move_planned_date(shipments)
elif mode == 'delete':
if hasattr(cls, 'moves'):
moves = [m for s in shipments for m in s.moves]
Move.delete(moves)
@classmethod
def check_modification(cls, mode, shipments, values=None, external=False):
super().check_modification(
mode, shipments, values=values, external=external)
if mode == 'delete':
for shipment in shipments:
if shipment.state not in {'cancelled', 'request', 'draft'}:
raise AccessError(gettext(
'stock.msg_shipment_delete_cancel',
shipment=shipment.rec_name))
@classmethod
def _set_move_planned_date(cls, shipments):
raise NotImplementedError
2019-09-20 11:47:15 +02:00
2020-07-25 08:19:31 +02:00
class ShipmentAssignMixin(ShipmentMixin):
__slots__ = ()
_assign_moves_field = None
partially_assigned = fields.Function(
fields.Boolean("Partially Assigned"),
'get_partially_assigned',
searcher='search_partially_assigned')
2020-07-25 08:19:31 +02:00
@classmethod
def assign_wizard(cls, shipments):
raise NotImplementedError
@property
def assign_moves(self):
return getattr(self, self._assign_moves_field)
2020-07-25 08:19:31 +02:00
@dualmethod
@ModelView.button
def assign_try(cls, shipments):
raise NotImplementedError
@dualmethod
def assign_reset(cls, shipments):
cls.wait(shipments)
@dualmethod
@ModelView.button
def assign_force(cls, shipments):
cls.assign(shipments)
@dualmethod
def assign_ignore(cls, shipments, moves=None):
2020-07-25 08:19:31 +02:00
pool = Pool()
Move = pool.get('stock.move')
assign_moves = {
m for s in shipments for m in s.assign_moves
if m.assignation_required and m.state in {'staging', 'draft'}}
if moves is None:
moves = list(assign_moves)
else:
moves = [m for m in moves if m in assign_moves]
Move.write(moves, {
2020-07-25 08:19:31 +02:00
'quantity': 0,
})
to_assign = [
s for s in shipments
if all(
m.state not in {'staging', 'draft'}
for m in s.assign_moves if m.assignation_required)]
if to_assign:
cls.assign(to_assign)
2020-07-25 08:19:31 +02:00
@classmethod
def _get_assign_domain(cls):
pool = Pool()
Date = pool.get('ir.date')
2021-12-21 23:14:59 +01:00
context = Transaction().context
return [
2021-12-21 23:14:59 +01:00
('company', '=', context.get('company')),
('state', '=', 'waiting'),
('planned_date', '<=', Date.today()),
]
@classmethod
2024-02-09 16:42:28 +01:00
def to_assign(cls):
return cls.search(cls._get_assign_domain())
@property
def assign_order_key(self):
return (self.planned_date, self.create_date)
def get_partially_assigned(self, name):
return (self.state != 'assigned'
and any(m.state == 'assigned' for m in self.assign_moves
if m.assignation_required))
@classmethod
def search_partially_assigned(cls, name, clause):
operators = {
'=': 'where',
'!=': 'not where',
}
reverse = {
'=': '!=',
'!=': '=',
}
if clause[1] in operators:
if not clause[2]:
operator = reverse[clause[1]]
else:
operator = clause[1]
return [
(cls._assign_moves_field, operators[operator], [
('state', '=', 'assigned'),
('assignation_required', '=', True),
]),
('state', '=', 'waiting'),
]
else:
return []
2020-07-25 08:19:31 +02:00
class ShipmentCheckQuantity:
"Check quantities per product between source and target moves"
__slots__ = ()
@property
def _check_quantity_source_moves(self):
raise NotImplementedError
@property
def _check_quantity_target_moves(self):
raise NotImplementedError
def check_quantity(self):
pool = Pool()
Warning = pool.get('res.user.warning')
Lang = pool.get('ir.lang')
lang = Lang.get()
source_qties = defaultdict(float)
for move in self._check_quantity_source_moves:
source_qties[move.product] += move.internal_quantity
target_qties = defaultdict(float)
for move in self._check_quantity_target_moves:
target_qties[move.product] += move.internal_quantity
diffs = {}
for product, incoming_qty in source_qties.items():
unit = product.default_uom
incoming_qty = unit.round(incoming_qty)
inventory_qty = unit.round(target_qties.pop(product, 0))
diff = inventory_qty - incoming_qty
if diff:
diffs[product] = diff
diffs.update((k, v) for k, v in target_qties.items() if v)
if diffs:
warning_name = Warning.format(
'check_quantity_product', [self])
if Warning.check(warning_name):
quantities = []
for product, quantity in diffs.items():
quantity = lang.format_number_symbol(
quantity, product.default_uom)
quantities.append(f"{product.rec_name}: {quantity}")
raise ShipmentCheckQuantityWarning(warning_name,
gettext(
'stock.msg_shipment_check_quantity',
shipment=self.rec_name,
quantities=', '.join(quantities)))
class ShipmentIn(
2025-12-02 16:01:39 +01:00
ShipmentCheckQuantity, ShipmentMixin, Workflow, ModelSQL, ModelView,
ChatMixin):
2012-09-24 22:18:17 +02:00
__name__ = 'stock.shipment.in'
2021-04-20 09:18:58 +02:00
company = fields.Many2One(
'company.company', "Company", required=True,
2011-10-04 18:17:41 +02:00
states={
2015-06-28 23:45:31 +02:00
'readonly': Eval('state') != 'draft',
2011-10-04 18:17:41 +02:00
},
context={
'party_contact_mechanism_usage': 'delivery',
},
2018-12-04 12:25:45 +01:00
help="The company the shipment is associated with.")
2008-10-22 20:13:23 +02:00
supplier = fields.Many2One('party.party', 'Supplier',
2011-08-04 16:57:42 +02:00
states={
2015-06-28 23:45:31 +02:00
'readonly': (((Eval('state') != 'draft')
| Eval('incoming_moves', [0]))
& Eval('supplier')),
2014-01-26 14:29:37 +01:00
}, required=True,
context={
'company': Eval('company', -1),
'party_contact_mechanism_usage': 'delivery',
},
depends={'company'},
2018-12-04 12:25:45 +01:00
help="The party that supplied the stock.")
supplier_location = fields.Function(fields.Many2One('stock.location',
2014-01-26 14:29:37 +01:00
'Supplier Location'),
2012-09-24 22:18:17 +02:00
'on_change_with_supplier_location')
2008-10-22 20:13:23 +02:00
contact_address = fields.Many2One('party.address', 'Contact Address',
2011-08-04 16:57:42 +02:00
states={
2015-06-28 23:45:31 +02:00
'readonly': Eval('state') != 'draft',
},
domain=[('party', '=', Eval('supplier', -1))],
2018-12-04 12:25:45 +01:00
help="The address at which the supplier can be contacted.")
warehouse = fields.Many2One('stock.location', "Warehouse",
2011-08-04 16:57:42 +02:00
required=True, domain=[('type', '=', 'warehouse')],
states={
'readonly': (Eval('state').in_(['cancelled', 'done'])
| Eval('incoming_moves', [0]) | Eval('inventory_moves', [0])),
},
2018-12-04 12:25:45 +01:00
help="Where the stock is received.")
2022-12-30 01:36:00 +01:00
warehouse_input = fields.Many2One(
'stock.location', "Warehouse Input", required=True,
domain=[
['OR',
('type', '=', 'storage'),
('id', '=', Eval('warehouse_storage', -1)),
],
2022-12-30 01:36:00 +01:00
If(Eval('state') == 'draft',
('parent', 'child_of', [Eval('warehouse', -1)]),
()),
],
states={
'readonly': Eval('state') != 'draft',
})
warehouse_storage = fields.Many2One(
'stock.location', "Warehouse Storage", required=True,
domain=[
('type', 'in', ['storage', 'view']),
2022-12-30 01:36:00 +01:00
If(Eval('state') == 'draft',
('parent', 'child_of', [Eval('warehouse', -1)]),
()),
],
states={
'readonly': Eval('state') != 'draft',
})
2013-05-27 18:15:02 +02:00
incoming_moves = fields.Function(fields.One2Many('stock.move', 'shipment',
'Incoming Moves',
add_remove=[
2013-03-23 15:42:36 +01:00
('shipment', '=', None),
('state', '=', 'draft'),
2018-03-24 12:01:58 +01:00
If(Eval('warehouse_input') == Eval('warehouse_storage'),
('to_location', 'child_of',
[Eval('warehouse_input', -1)], 'parent'),
('to_location', '=', Eval('warehouse_input'))),
],
2023-02-22 16:58:23 +01:00
order=[
('product', 'ASC'),
('id', 'ASC'),
],
search_order=[
('planned_date', 'ASC NULLS LAST'),
('id', 'ASC')
],
domain=[
2022-12-30 01:36:00 +01:00
If(Eval('state') == 'draft',
('from_location', '=', Eval('supplier_location')),
()),
2018-03-24 12:01:58 +01:00
If(Eval('warehouse_input') == Eval('warehouse_storage'),
('to_location', 'child_of',
[Eval('warehouse_input', -1)], 'parent'),
('to_location', '=', Eval('warehouse_input'))),
('company', '=', Eval('company', -1)),
],
states={
'readonly': (
2022-12-30 01:36:00 +01:00
(Eval('state') != 'draft')
| ~Eval('warehouse') | ~Eval('supplier')),
},
2018-12-04 12:25:45 +01:00
help="The moves that bring the stock into the warehouse."),
2011-08-04 16:57:42 +02:00
'get_incoming_moves', setter='set_incoming_moves')
2013-05-27 18:15:02 +02:00
inventory_moves = fields.Function(fields.One2Many('stock.move', 'shipment',
'Inventory Moves',
domain=[
('from_location', '=', Eval('warehouse_input', -1)),
If(~Eval('state').in_(['done', 'cancelled']),
2020-10-11 23:46:10 +02:00
['OR',
('to_location', 'child_of',
[Eval('warehouse_storage', -1)], 'parent'),
('to_location.waste_warehouses', '=',
Eval('warehouse', -1)),
2020-10-11 23:46:10 +02:00
],
[],),
('company', '=', Eval('company', -1)),
],
2023-02-22 16:58:23 +01:00
order=[
('to_location', 'ASC'),
('product', 'ASC'),
('id', 'ASC'),
],
states={
'readonly': Eval('state').in_(['draft', 'done', 'cancelled']),
2018-03-24 12:01:58 +01:00
'invisible': (
Eval('warehouse_input') == Eval('warehouse_storage')),
},
2018-12-04 12:25:45 +01:00
help="The moves that put the stock away into the storage area."),
2011-08-04 16:57:42 +02:00
'get_inventory_moves', setter='set_inventory_moves')
2024-02-06 00:12:55 +01:00
moves = fields.One2Many(
'stock.move', 'shipment', "Moves",
domain=[('company', '=', Eval('company', -1))],
2024-02-06 00:12:55 +01:00
states={
'readonly': True,
})
2013-09-19 17:34:23 +02:00
origins = fields.Function(fields.Char('Origins'), 'get_origins')
2017-02-01 21:41:00 +01:00
received_by = employee_field("Received By")
done_by = employee_field("Done By")
state = fields.Selection([
('draft', 'Draft'),
2022-02-15 00:08:47 +01:00
('received', 'Received'),
('done', 'Done'),
('cancelled', 'Cancelled'),
2022-02-15 00:08:47 +01:00
], "State", readonly=True, sort=False,
2018-12-04 12:25:45 +01:00
help="The current state of the shipment.")
2008-04-08 14:27:53 +02:00
2012-09-24 22:18:17 +02:00
@classmethod
def __setup__(cls):
2025-04-04 00:45:18 +02:00
super().__setup__()
t = cls.__table__()
cls._sql_indexes.update({
Index(
t,
2024-07-27 18:36:14 +02:00
(t.state, Index.Equality(cardinality='low')),
where=t.state.in_(['draft', 'received'])),
})
2021-02-25 21:13:56 +01:00
cls._order = [
('id', 'DESC'),
]
2012-09-24 22:18:17 +02:00
cls._transitions |= set((
2012-03-25 00:18:19 +01:00
('draft', 'received'),
('received', 'done'),
('draft', 'cancelled'),
('received', 'cancelled'),
('cancelled', 'draft'),
('done', 'cancelled'),
2012-03-25 00:18:19 +01:00
))
2012-09-24 22:18:17 +02:00
cls._buttons.update({
2012-03-25 00:18:19 +01:00
'cancel': {
'invisible': (
(Eval('state') == 'cancelled')
| ((Eval('state') == 'done')
& ~Id('stock',
'group_stock_cancellation').in_(
Eval('context', {}).get('groups', [])))),
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
'draft': {
'invisible': Eval('state') != 'cancelled',
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
'receive': {
'invisible': Eval('state') != 'draft',
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
'do': {
2012-03-25 00:18:19 +01:00
'invisible': Eval('state') != 'received',
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
})
2008-04-08 14:27:53 +02:00
2012-09-24 22:18:17 +02:00
@classmethod
def __register__(cls, module_name):
2022-12-30 01:36:00 +01:00
pool = Pool()
Location = pool.get('stock.location')
cursor = Transaction().connection.cursor()
sql_table = cls.__table__()
2022-12-30 01:36:00 +01:00
location = Location.__table__()
2011-10-04 18:17:41 +02:00
2025-04-04 00:45:18 +02:00
super().__register__(module_name)
2022-12-30 01:36:00 +01:00
# Migration from 6.6: fill warehouse locations
cursor.execute(*sql_table.update(
[sql_table.warehouse_input],
location.select(
location.input_location,
where=location.id == sql_table.warehouse),
where=sql_table.warehouse_input == Null))
cursor.execute(*sql_table.update(
[sql_table.warehouse_storage],
location.select(
location.storage_location,
where=location.id == sql_table.warehouse),
where=sql_table.warehouse_storage == Null))
2021-02-25 21:13:56 +01:00
@classmethod
def order_effective_date(cls, tables):
table, _ = tables[None]
return [Coalesce(table.effective_date, table.planned_date)]
2012-09-24 22:18:17 +02:00
@staticmethod
def default_state():
2008-04-08 14:27:53 +02:00
return 'draft'
2012-09-24 22:18:17 +02:00
@classmethod
def default_warehouse(cls):
Location = Pool().get('stock.location')
return Location.get_default_warehouse()
2008-11-11 23:31:30 +01:00
2022-12-30 01:36:00 +01:00
@fields.depends('warehouse')
def on_change_warehouse(self):
if self.warehouse:
self.warehouse_input = self.warehouse.input_location
self.warehouse_storage = self.warehouse.storage_location
else:
self.warehouse_input = self.warehouse_storage = None
2012-09-24 22:18:17 +02:00
@staticmethod
def default_company():
2012-03-27 10:44:04 +02:00
return Transaction().context.get('company')
2011-10-04 18:17:41 +02:00
2014-01-26 14:29:37 +01:00
@fields.depends('supplier')
2012-09-24 22:18:17 +02:00
def on_change_supplier(self):
2014-10-29 20:40:25 +01:00
self.contact_address = None
2013-12-12 13:50:05 +01:00
if self.supplier:
2014-10-29 20:40:25 +01:00
self.contact_address = self.supplier.address_get()
2014-01-26 14:29:37 +01:00
@fields.depends('supplier')
2012-09-24 22:18:17 +02:00
def on_change_with_supplier_location(self, name=None):
2023-03-26 09:49:00 +02:00
return self.supplier.supplier_location if self.supplier else None
2012-09-24 22:18:17 +02:00
def get_incoming_moves(self, name):
2018-03-24 12:01:58 +01:00
if self.warehouse_input == self.warehouse_storage:
moves = self.moves
2023-02-22 16:58:23 +01:00
else:
moves = filter(
lambda m: m.to_location == self.warehouse_input, self.moves)
return sort(moves, self.__class__.incoming_moves.order)
2012-09-24 22:18:17 +02:00
@classmethod
def set_incoming_moves(cls, shipments, name, value):
2008-12-04 18:34:30 +01:00
if not value:
return
2012-09-24 22:18:17 +02:00
cls.write(shipments, {
'moves': value,
})
2012-09-24 22:18:17 +02:00
def get_inventory_moves(self, name):
moves = filter(
lambda m: m.from_location == self.warehouse_input, self.moves)
return sort(moves, self.__class__.inventory_moves.order)
2012-09-24 22:18:17 +02:00
@classmethod
def set_inventory_moves(cls, shipments, name, value):
2008-12-04 18:34:30 +01:00
if not value:
return
2012-09-24 22:18:17 +02:00
cls.write(shipments, {
'moves': value,
})
2012-09-24 22:18:17 +02:00
@property
def _move_planned_date(self):
'''
Return the planned date for incoming moves and inventory_moves
'''
2012-09-24 22:18:17 +02:00
return self.planned_date, self.planned_date
2012-09-24 22:18:17 +02:00
@classmethod
def _set_move_planned_date(cls, shipments):
'''
Set planned date of moves for the shipments
'''
2012-09-24 22:18:17 +02:00
Move = Pool().get('stock.move')
to_write = []
2012-09-24 22:18:17 +02:00
for shipment in shipments:
dates = shipment._move_planned_date
incoming_date, inventory_date = dates
if incoming_date:
incoming_moves_to_write = [
m for m in shipment.incoming_moves
if (m.state not in {'done', 'cancelled'})]
if incoming_moves_to_write:
to_write.extend(
(incoming_moves_to_write, {
'planned_date': incoming_date,
}))
if inventory_date:
inventory_moves_to_write = [
m for m in shipment.inventory_moves
if (m.state not in {'done', 'cancelled'})]
if inventory_moves_to_write:
to_write.extend(
(inventory_moves_to_write, {
'planned_date': inventory_date,
}))
if to_write:
Move.write(*to_write)
2014-09-17 21:45:58 +02:00
def get_origins(self, name):
2018-06-26 19:11:55 +02:00
return ', '.join(set(filter(None,
(m.origin_name for m in self.incoming_moves))))
2013-09-19 17:34:23 +02:00
2025-12-02 16:01:39 +01:00
def chat_language(self, audience='internal'):
language = super().chat_language(audience=audience)
if audience == 'public':
language = self.supplier.lang.code if self.supplier.lang else None
return language
2012-09-24 22:18:17 +02:00
@classmethod
def preprocess_values(cls, mode, values):
pool = Pool()
Configuration = pool.get('stock.configuration')
values = super().preprocess_values(mode, values)
if mode == 'create' and not values.get('number'):
company_id = values.get('company', cls.default_company())
if company_id is not None:
configuration = Configuration(1)
if sequence := configuration.get_multivalue(
'shipment_in_sequence', company=company_id):
values['number'] = sequence.get()
return values
2008-04-17 02:31:55 +02:00
2012-09-24 22:18:17 +02:00
@classmethod
def copy(cls, shipments, default=None):
2008-11-04 23:32:11 +01:00
if default is None:
default = {}
2018-09-01 01:02:04 +02:00
else:
default = default.copy()
default.setdefault('moves.origin', None)
2018-09-01 01:02:04 +02:00
default.setdefault('received_by', None)
default.setdefault('done_by', None)
2025-04-04 00:45:18 +02:00
return super().copy(shipments, default=default)
2008-11-04 23:32:11 +01:00
2019-04-11 18:54:55 +02:00
def _get_inventory_move(self, incoming_move):
'Return inventory move for the incoming move'
2013-03-12 10:35:12 +01:00
pool = Pool()
Move = pool.get('stock.move')
2020-03-06 23:53:19 +01:00
Date = pool.get('ir.date')
if incoming_move.quantity <= 0.0:
return None
with Transaction().set_context(company=self.company.id):
today = Date.today()
2013-03-12 10:35:12 +01:00
move = Move()
move.product = incoming_move.product
2023-08-22 13:18:33 +02:00
move.unit = incoming_move.unit
2013-03-12 10:35:12 +01:00
move.quantity = incoming_move.quantity
move.from_location = incoming_move.to_location
2019-04-11 18:54:55 +02:00
move.to_location = self.warehouse_storage
move.state = (
'staging' if incoming_move.state == 'staging' else 'draft')
move.planned_date = max(
filter(None, [self._move_planned_date[1], today]))
2013-03-12 10:35:12 +01:00
move.company = incoming_move.company
2019-07-08 17:18:14 +02:00
move.origin = incoming_move
2013-03-12 10:35:12 +01:00
return move
2012-09-24 22:18:17 +02:00
@classmethod
def create_inventory_moves(cls, shipments):
2012-03-25 00:18:19 +01:00
for shipment in shipments:
2018-03-24 12:01:58 +01:00
if shipment.warehouse_storage == shipment.warehouse_input:
# Do not create inventory moves
continue
2013-03-12 10:35:12 +01:00
# Use moves instead of inventory_moves because save reset before
# adding new records and as set_inventory_moves is just a proxy to
# moves, it will reset also the incoming_moves
moves = list(shipment.moves)
2012-03-25 00:18:19 +01:00
for incoming_move in shipment.incoming_moves:
2019-04-11 18:54:55 +02:00
move = shipment._get_inventory_move(incoming_move)
2013-03-12 10:35:12 +01:00
if move:
moves.append(move)
shipment.moves = moves
cls.save(shipments)
2008-04-08 14:27:53 +02:00
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('cancelled')
2012-09-24 22:18:17 +02:00
def cancel(cls, shipments):
Move = Pool().get('stock.move')
2012-12-27 21:53:07 +01:00
Move.cancel([m for s in shipments
for m in s.incoming_moves + s.inventory_moves])
2012-03-25 00:18:19 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('draft')
2012-09-24 22:18:17 +02:00
def draft(cls, shipments):
Move = Pool().get('stock.move')
2015-10-21 16:48:02 +02:00
Move.draft([m for s in shipments for m in s.incoming_moves
if m.state != 'staging'])
2012-09-24 22:18:17 +02:00
Move.delete([m for s in shipments for m in s.inventory_moves
if m.state in {'staging', 'draft', 'cancelled'}])
2012-03-25 00:18:19 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('received')
2017-02-01 21:41:00 +01:00
@set_employee('received_by')
2012-09-24 22:18:17 +02:00
def receive(cls, shipments):
Move = Pool().get('stock.move')
2012-12-27 21:53:07 +01:00
Move.do([m for s in shipments for m in s.incoming_moves])
Move.delete([m for s in shipments for m in s.inventory_moves
2020-06-06 10:30:11 +01:00
if m.state in ('draft', 'cancelled')])
2012-09-24 22:18:17 +02:00
cls.create_inventory_moves(shipments)
2018-03-24 12:01:58 +01:00
# Set received state to allow done transition
cls.write(shipments, {'state': 'received'})
to_do = [s for s in shipments
if s.warehouse_storage == s.warehouse_input]
if to_do:
cls.do(to_do)
2012-03-25 00:18:19 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('done')
2017-02-01 21:41:00 +01:00
@set_employee('done_by')
def do(cls, shipments):
2012-09-24 22:18:17 +02:00
pool = Pool()
Move = pool.get('stock.move')
Date = pool.get('ir.date')
inventory_moves = []
for shipment in shipments:
if shipment.warehouse_storage != shipment.warehouse_input:
shipment.check_quantity()
inventory_moves.extend(shipment.inventory_moves)
Move.do(inventory_moves)
for company, c_shipments in groupby(
shipments, key=lambda s: s.company):
with Transaction().set_context(company=company.id):
today = Date.today()
cls.write([s for s in c_shipments if not s.effective_date], {
'effective_date': today,
})
2008-05-08 17:24:49 +02:00
@property
def _check_quantity_source_moves(self):
return self.incoming_moves
@property
def _check_quantity_target_moves(self):
return self.inventory_moves
2008-04-08 14:27:53 +02:00
2025-12-02 16:01:39 +01:00
class ShipmentInReturn(
ShipmentAssignMixin, Workflow, ModelSQL, ModelView, ChatMixin):
2012-09-24 22:18:17 +02:00
__name__ = 'stock.shipment.in.return'
_assign_moves_field = 'moves'
2021-04-20 09:18:58 +02:00
company = fields.Many2One(
'company.company', "Company", required=True,
2011-10-04 18:17:41 +02:00
states={
2015-06-28 23:45:31 +02:00
'readonly': Eval('state') != 'draft',
2011-10-04 18:17:41 +02:00
},
context={
'party_contact_mechanism_usage': 'delivery',
},
2018-12-04 12:25:45 +01:00
help="The company the shipment is associated with.")
supplier = fields.Many2One('party.party', 'Supplier',
states={
'readonly': (((Eval('state') != 'draft')
| Eval('moves', [0]))
& Eval('supplier', 0)),
}, required=True,
context={
'company': Eval('company', -1),
'party_contact_mechanism_usage': 'delivery',
},
depends={'company'},
2018-12-04 12:25:45 +01:00
help="The party that supplied the stock.")
delivery_address = fields.Many2One('party.address', 'Delivery Address',
states={
'readonly': Eval('state') != 'draft',
},
2022-09-10 18:34:29 +02:00
domain=['OR',
('party', '=', Eval('supplier', -1)),
2022-09-10 18:34:29 +02:00
('warehouses', 'where', [
('id', '=', Eval('warehouse', -1)),
If(Eval('state') == 'draft',
('allow_pickup', '=', True),
()),
]),
],
2018-12-04 12:25:45 +01:00
help="Where the stock is sent to.")
2010-01-20 11:04:28 +01:00
from_location = fields.Many2One('stock.location', "From Location",
2011-08-04 16:57:42 +02:00
required=True, states={
2015-06-28 23:45:31 +02:00
'readonly': (Eval('state') != 'draft') | Eval('moves', [0]),
}, domain=[('type', 'in', ['storage', 'view'])],
2018-12-04 12:25:45 +01:00
help="Where the stock is moved from.")
2009-01-27 15:15:01 +01:00
to_location = fields.Many2One('stock.location', "To Location",
2011-08-04 16:57:42 +02:00
required=True, states={
2015-06-28 23:45:31 +02:00
'readonly': (Eval('state') != 'draft') | Eval('moves', [0]),
2011-08-04 16:57:42 +02:00
}, domain=[('type', '=', 'supplier')],
2018-12-04 12:25:45 +01:00
help="Where the stock is moved to.")
2022-09-10 18:34:29 +02:00
warehouse = fields.Function(
fields.Many2One('stock.location', "Warehouse"),
'on_change_with_warehouse')
2013-03-23 15:42:36 +01:00
moves = fields.One2Many('stock.move', 'shipment', 'Moves',
2010-01-20 11:04:28 +01:00
states={
2015-06-28 23:45:31 +02:00
'readonly': (((Eval('state') != 'draft') | ~Eval('from_location'))
& Eval('to_location')),
2011-08-04 16:57:42 +02:00
},
domain=[
If(Eval('state') == 'draft', [
('from_location', '=', Eval('from_location')),
('to_location', '=', Eval('to_location')),
],
If(~Eval('state').in_(['done', 'cancelled']), [
('from_location', 'child_of',
[Eval('from_location', -1)], 'parent'),
('to_location', 'child_of',
[Eval('to_location', -1)], 'parent'),
],
[])),
('company', '=', Eval('company', -1)),
],
2023-02-22 16:58:23 +01:00
order=[
('from_location', 'ASC'),
('product', 'ASC'),
('id', 'ASC'),
],
2018-12-04 12:25:45 +01:00
help="The moves that return the stock to the supplier.")
2013-09-19 17:34:23 +02:00
origins = fields.Function(fields.Char('Origins'), 'get_origins')
2017-02-01 21:41:00 +01:00
assigned_by = employee_field("Assigned By")
done_by = employee_field("Done By")
2009-01-27 15:15:01 +01:00
state = fields.Selection([
('draft', 'Draft'),
('waiting', 'Waiting'),
2022-02-15 00:08:47 +01:00
('assigned', 'Assigned'),
2009-01-27 15:15:01 +01:00
('done', 'Done'),
2022-02-15 00:08:47 +01:00
('cancelled', 'Cancelled'),
], 'State', readonly=True, sort=False,
2018-12-04 12:25:45 +01:00
help="The current state of the shipment.")
2009-01-27 15:15:01 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
def __setup__(cls):
2025-04-04 00:45:18 +02:00
super().__setup__()
t = cls.__table__()
cls._sql_indexes.update({
Index(
t,
2024-07-27 18:36:14 +02:00
(t.state, Index.Equality(cardinality='low')),
where=t.state.in_(['draft', 'waiting', 'assigned'])),
})
2021-02-25 21:13:56 +01:00
cls._order = [
('effective_date', 'ASC NULLS LAST'),
('id', 'ASC'),
]
2012-09-24 22:18:17 +02:00
cls._transitions |= set((
2012-03-25 00:18:19 +01:00
('draft', 'waiting'),
('waiting', 'assigned'),
('waiting', 'draft'),
('assigned', 'done'),
('assigned', 'waiting'),
('draft', 'cancelled'),
('waiting', 'cancelled'),
('assigned', 'cancelled'),
('cancelled', 'draft'),
('done', 'cancelled'),
2012-03-25 00:18:19 +01:00
))
2012-09-24 22:18:17 +02:00
cls._buttons.update({
2012-03-25 00:18:19 +01:00
'cancel': {
'invisible': (
(Eval('state') == 'cancelled')
| ((Eval('state') == 'done')
& ~Id('stock',
'group_stock_cancellation').in_(
Eval('context', {}).get('groups', [])))),
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
'draft': {
'invisible': ~Eval('state').in_(['waiting', 'cancelled']),
'icon': If(Eval('state') == 'cancelled',
2018-08-19 20:02:58 +02:00
'tryton-undo',
If(Eval('state') == 'waiting',
'tryton-back',
'tryton-forward')),
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
'wait': {
'invisible': ~Eval('state').in_(['assigned', 'draft']),
'icon': If(Eval('state') == 'assigned',
2018-08-19 20:02:58 +02:00
'tryton-back', 'tryton-forward'),
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
'do': {
2012-03-25 00:18:19 +01:00
'invisible': Eval('state') != 'assigned',
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
2012-09-10 15:28:38 +02:00
'assign_wizard': {
'invisible': Eval('state') != 'waiting',
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-09-10 15:28:38 +02:00
},
2012-03-25 00:18:19 +01:00
'assign_try': {},
'assign_force': {},
})
2009-01-27 15:15:01 +01:00
2012-09-24 22:18:17 +02:00
@staticmethod
def default_state():
2012-03-25 00:18:19 +01:00
return 'draft'
2012-09-24 22:18:17 +02:00
@staticmethod
def default_company():
2012-03-27 10:44:04 +02:00
return Transaction().context.get('company')
2009-01-27 15:15:01 +01:00
@fields.depends('supplier')
def on_change_supplier(self):
if self.supplier:
self.delivery_address = self.supplier.address_get('delivery')
self.to_location = self.supplier.supplier_location
2022-09-10 18:34:29 +02:00
@fields.depends('from_location')
def on_change_with_warehouse(self, name=None):
2023-03-26 09:49:00 +02:00
return self.from_location.warehouse if self.from_location else None
2022-09-10 18:34:29 +02:00
2012-09-24 22:18:17 +02:00
@property
def _move_planned_date(self):
'''
Return the planned date for the moves
'''
2012-09-24 22:18:17 +02:00
return self.planned_date
2012-09-24 22:18:17 +02:00
@classmethod
def _set_move_planned_date(cls, shipments):
'''
Set planned date of moves for the shipments
'''
2012-09-24 22:18:17 +02:00
Move = Pool().get('stock.move')
to_write = []
2012-09-24 22:18:17 +02:00
for shipment in shipments:
moves = [m for m in shipment.moves
if (m.state not in {'done', 'cancelled'}
and m.planned_date != shipment._move_planned_date)]
if moves:
to_write.extend((moves, {
'planned_date': shipment._move_planned_date,
}))
if to_write:
Move.write(*to_write)
2014-09-17 21:45:58 +02:00
def get_origins(self, name):
2018-06-26 19:11:55 +02:00
return ', '.join(set(filter(None,
2014-09-17 21:45:58 +02:00
(m.origin_name for m in self.moves))))
2013-09-19 17:34:23 +02:00
2025-12-02 16:01:39 +01:00
def chat_language(self, audience='internal'):
language = super().chat_language(audience=audience)
if audience == 'public':
language = self.supplier.lang.code if self.supplier.lang else None
return language
2017-01-25 23:41:54 +01:00
@classmethod
def copy(cls, shipments, default=None):
if default is None:
default = {}
2018-09-01 01:02:04 +02:00
else:
default = default.copy()
default.setdefault('moves.origin', None)
2018-09-01 01:02:04 +02:00
default.setdefault('assigned_by', None)
default.setdefault('done_by', None)
2025-04-04 00:45:18 +02:00
return super().copy(shipments, default=default)
2017-01-25 23:41:54 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('draft')
2012-09-24 22:18:17 +02:00
def draft(cls, shipments):
Move = Pool().get('stock.move')
2015-10-21 16:48:02 +02:00
Move.draft([m for s in shipments for m in s.moves
if m.state != 'staging'])
for shipment in shipments:
Move.write([m for m in shipment.moves
if m.state != 'done'], {
'from_location': shipment.from_location.id,
'to_location': shipment.to_location.id,
'planned_date': shipment.planned_date,
})
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('waiting')
def wait(cls, shipments, moves=None):
"""
If moves is set, only this subset is set to draft.
"""
2012-09-24 22:18:17 +02:00
Move = Pool().get('stock.move')
if moves is None:
moves = sum((s.moves for s in shipments), ())
else:
assert all(m.shipment in shipments for m in moves)
Move.draft(moves)
2024-02-19 22:51:55 +01:00
cls.set_number(shipments)
2012-12-27 21:53:07 +01:00
cls._set_move_planned_date(shipments)
2009-01-27 15:15:01 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@Workflow.transition('assigned')
2017-02-01 21:41:00 +01:00
@set_employee('assigned_by')
2012-09-24 22:18:17 +02:00
def assign(cls, shipments):
Move = Pool().get('stock.move')
2020-07-25 08:19:31 +02:00
Move.assign([m for s in shipments for m in s.assign_moves])
2009-01-27 15:15:01 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('done')
2017-02-01 21:41:00 +01:00
@set_employee('done_by')
def do(cls, shipments):
2012-09-24 22:18:17 +02:00
pool = Pool()
Move = pool.get('stock.move')
Date = pool.get('ir.date')
2012-12-27 21:53:07 +01:00
Move.do([m for s in shipments for m in s.moves])
for company, c_shipments in groupby(
shipments, key=lambda s: s.company):
with Transaction().set_context(company=company.id):
today = Date.today()
cls.write([s for s in c_shipments if not s.effective_date], {
'effective_date': today,
})
2009-01-27 15:15:01 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('cancelled')
2012-09-24 22:18:17 +02:00
def cancel(cls, shipments):
Move = Pool().get('stock.move')
2012-12-27 21:53:07 +01:00
Move.cancel([m for s in shipments for m in s.moves])
2009-01-27 15:15:01 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
2012-09-10 15:28:38 +02:00
@ModelView.button_action('stock.wizard_shipment_in_return_assign')
2012-09-24 22:18:17 +02:00
def assign_wizard(cls, shipments):
2012-09-10 15:28:38 +02:00
pass
2020-07-25 08:19:31 +02:00
@dualmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
def assign_try(cls, shipments, with_childs=None):
2011-07-03 13:19:14 +02:00
pool = Pool()
2012-09-24 22:18:17 +02:00
Move = pool.get('stock.move')
shipments = [s for s in shipments if s.state == 'waiting']
to_assign = defaultdict(list)
for shipment in shipments:
location_type = shipment.from_location.type
2020-07-25 08:19:31 +02:00
for move in shipment.assign_moves:
if move.assignation_required:
to_assign[location_type].append(move)
success = True
2018-06-26 19:11:55 +02:00
for location_type, moves in to_assign.items():
if with_childs is None:
_with_childs = location_type == 'view'
elif not with_childs and location_type == 'view':
_with_childs = True
else:
_with_childs = with_childs
if not Move.assign_try(moves, with_childs=_with_childs):
success = False
if success:
cls.assign(shipments)
else:
to_assign = []
for shipment in shipments:
if any(
m.state in {'staging', 'draft'}
for m in shipment.assign_moves
if m.assignation_required):
continue
to_assign.append(shipment)
if to_assign:
cls.assign(to_assign)
2009-01-27 15:15:01 +01:00
2020-10-12 19:20:33 +02:00
@classmethod
def _get_reschedule_domain(cls, date):
return [
('state', 'in', ['waiting', 'assigned']),
2020-10-12 19:20:33 +02:00
('planned_date', '<', date),
]
@classmethod
def reschedule(cls, date=None):
pool = Pool()
Date = pool.get('ir.date')
if date is None:
date = Date.today()
shipments = cls.search(cls._get_reschedule_domain(date))
cls.write(shipments, {'planned_date': date})
2009-01-27 15:15:01 +01:00
class ShipmentOut(
ShipmentCheckQuantity, ShipmentAssignMixin, Workflow, ModelSQL,
2025-12-02 16:01:39 +01:00
ModelView, ChatMixin):
2012-09-24 22:18:17 +02:00
__name__ = 'stock.shipment.out'
_assign_moves_field = 'moves'
2021-04-20 09:18:58 +02:00
company = fields.Many2One(
'company.company', "Company", required=True,
2011-10-04 18:17:41 +02:00
states={
2015-06-28 23:45:31 +02:00
'readonly': Eval('state') != 'draft',
2011-10-04 18:17:41 +02:00
},
context={
'party_contact_mechanism_usage': 'delivery',
},
2018-12-04 12:25:45 +01:00
help="The company the shipment is associated with.")
2008-10-22 20:13:23 +02:00
customer = fields.Many2One('party.party', 'Customer', required=True,
2011-08-04 16:57:42 +02:00
states={
2015-06-28 23:45:31 +02:00
'readonly': ((Eval('state') != 'draft')
| Eval('outgoing_moves', [0])),
2014-01-26 14:29:37 +01:00
},
context={
'company': Eval('company', -1),
'party_contact_mechanism_usage': 'delivery',
},
depends={'company'},
2018-12-04 12:25:45 +01:00
help="The party that purchased the stock.")
customer_location = fields.Function(fields.Many2One('stock.location',
2014-01-26 14:29:37 +01:00
'Customer Location'), 'on_change_with_customer_location')
2011-08-04 16:57:42 +02:00
delivery_address = fields.Many2One('party.address',
'Delivery Address', required=True,
states={
2015-06-28 23:45:31 +02:00
'readonly': Eval('state') != 'draft',
2022-09-10 18:34:29 +02:00
},
domain=['OR',
('party', '=', Eval('customer', -1)),
2022-09-10 18:34:29 +02:00
('warehouses', 'where', [
('id', '=', Eval('warehouse', -1)),
If(Eval('state') == 'draft',
('allow_pickup', '=', True),
()),
]),
],
2018-12-04 12:25:45 +01:00
help="Where the stock is sent to.")
2011-08-04 16:57:42 +02:00
warehouse = fields.Many2One('stock.location', "Warehouse", required=True,
states={
2015-06-28 23:45:31 +02:00
'readonly': ((Eval('state') != 'draft')
| Eval('outgoing_moves', [0]) | Eval('inventory_moves', [0])),
2011-08-04 16:57:42 +02:00
}, domain=[('type', '=', 'warehouse')],
2018-12-04 12:25:45 +01:00
help="Where the stock is sent from.")
2022-12-30 01:36:00 +01:00
warehouse_storage = fields.Many2One(
'stock.location', "Warehouse Storage", required=True,
domain=[
('type', 'in', ['storage', 'view']),
2022-12-30 01:36:00 +01:00
If(Eval('state') == 'draft',
('parent', 'child_of', [Eval('warehouse', -1)]),
()),
],
states={
'readonly': Eval('state') != 'draft',
})
warehouse_output = fields.Many2One(
'stock.location', "Warehouse Output", required=True,
domain=[
['OR',
('type', '=', 'storage'),
('id', '=', Eval('warehouse_output', -1)),
],
2022-12-30 01:36:00 +01:00
If(Eval('state') == 'draft',
('parent', 'child_of', [Eval('warehouse', -1)]),
()),
],
states={
'readonly': Eval('state') != 'draft',
})
2013-05-27 18:15:02 +02:00
outgoing_moves = fields.Function(fields.One2Many('stock.move', 'shipment',
'Outgoing Moves',
domain=[
If(Eval('warehouse_output') == Eval('warehouse_storage'),
('from_location', 'child_of',
[Eval('warehouse_output', -1)], 'parent'),
('from_location', '=', Eval('warehouse_output', -1))),
2022-12-30 01:36:00 +01:00
If(~Eval('state').in_(['done', 'cancelled']),
('to_location', '=', Eval('customer_location', -1)),
2022-12-30 01:36:00 +01:00
()),
('company', '=', Eval('company', -1)),
],
2023-02-22 16:58:23 +01:00
order=[
('product', 'ASC'),
('id', 'ASC'),
],
states={
'readonly': (Eval('state').in_(
If(Eval('warehouse_storage')
== Eval('warehouse_output'),
['done', 'cancelled'],
['waiting', 'packed', 'done', 'cancelled'],
))
| ~Eval('warehouse') | ~Eval('customer')),
},
2018-12-04 12:25:45 +01:00
help="The moves that send the stock to the customer."),
2011-08-04 16:57:42 +02:00
'get_outgoing_moves', setter='set_outgoing_moves')
2013-05-27 18:15:02 +02:00
inventory_moves = fields.Function(fields.One2Many('stock.move', 'shipment',
'Inventory Moves',
domain=[
If(Eval('state').in_(['waiting']),
('from_location', 'child_of',
[Eval('warehouse_storage', -1)], 'parent'),
()),
('to_location', '=', Eval('warehouse_output', -1)),
('company', '=', Eval('company', -1)),
],
2023-02-22 16:58:23 +01:00
order=[
('from_location', 'ASC'),
('product', 'ASC'),
('id', 'ASC'),
],
states={
2012-03-28 18:00:35 +02:00
'readonly': Eval('state').in_(
['draft', 'assigned', 'picked', 'packed', 'done',
'cancelled']),
'invisible': (
Eval('warehouse_storage') == Eval('warehouse_output')),
},
2018-12-04 12:25:45 +01:00
help="The moves that pick the stock from the storage area."),
2011-08-04 16:57:42 +02:00
'get_inventory_moves', setter='set_inventory_moves')
2024-02-06 00:12:55 +01:00
moves = fields.One2Many(
'stock.move', 'shipment', "Moves",
domain=[('company', '=', Eval('company', -1))],
2024-02-06 00:12:55 +01:00
states={
'readonly': True,
})
2013-09-19 17:34:23 +02:00
origins = fields.Function(fields.Char('Origins'), 'get_origins')
picked_by = employee_field("Picked By")
2017-02-01 21:41:00 +01:00
packed_by = employee_field("Packed By")
2024-05-06 00:34:03 +02:00
shipped_by = employee_field("Shipped By")
2017-02-01 21:41:00 +01:00
done_by = employee_field("Done By")
state = fields.Selection([
('draft', 'Draft'),
2022-02-15 00:08:47 +01:00
('waiting', 'Waiting'),
('assigned', 'Assigned'),
('picked', 'Picked'),
('packed', 'Packed'),
2024-05-06 00:34:03 +02:00
('shipped', "Shipped"),
2022-02-15 00:08:47 +01:00
('done', 'Done'),
('cancelled', 'Cancelled'),
], "State", readonly=True, sort=False,
2018-12-04 12:25:45 +01:00
help="The current state of the shipment.")
2008-04-08 14:27:53 +02:00
2012-09-24 22:18:17 +02:00
@classmethod
def __setup__(cls):
2025-04-04 00:45:18 +02:00
super().__setup__()
t = cls.__table__()
cls._sql_indexes.update({
Index(
t,
2024-07-27 18:36:14 +02:00
(t.state, Index.Equality(cardinality='low')),
where=t.state.in_([
'draft', 'waiting', 'assigned',
2024-05-06 00:34:03 +02:00
'picked', 'packed', 'shipped'])),
})
2012-09-24 22:18:17 +02:00
cls._transitions |= set((
2012-03-25 00:18:19 +01:00
('draft', 'waiting'),
('waiting', 'assigned'),
('waiting', 'picked'),
('assigned', 'picked'),
('assigned', 'packed'),
('picked', 'packed'),
2024-05-06 00:34:03 +02:00
('packed', 'shipped'),
2012-03-25 00:18:19 +01:00
('packed', 'done'),
2022-09-08 13:19:52 +02:00
('packed', 'waiting'),
('packed', 'picked'),
2024-05-06 00:34:03 +02:00
('shipped', 'done'),
2012-03-25 00:18:19 +01:00
('assigned', 'waiting'),
('waiting', 'waiting'),
('waiting', 'draft'),
('draft', 'cancelled'),
('waiting', 'cancelled'),
('assigned', 'cancelled'),
('picked', 'cancelled'),
('packed', 'cancelled'),
2024-05-06 00:34:03 +02:00
('shipped', 'cancelled'),
('cancelled', 'draft'),
('done', 'cancelled'),
2012-03-25 00:18:19 +01:00
))
2012-09-24 22:18:17 +02:00
cls._buttons.update({
2012-03-25 00:18:19 +01:00
'cancel': {
'invisible': (
(Eval('state') == 'cancelled')
| ((Eval('state') == 'done')
& ~Id('stock',
'group_stock_cancellation').in_(
Eval('context', {}).get('groups', [])))),
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
'draft': {
'invisible': ~Eval('state').in_(['waiting', 'cancelled']),
'icon': If(Eval('state') == 'cancelled',
2018-08-19 20:02:58 +02:00
'tryton-undo',
If(Eval('state') == 'waiting',
'tryton-back',
'tryton-forward')),
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
'wait': {
2022-09-08 13:19:52 +02:00
'invisible': (
~(Eval('state').in_(['assigned', 'waiting', 'draft'])
| ((Eval('state') == 'packed')
& (Eval('warehouse_storage')
== Eval('warehouse_output'))))),
'icon': If(Eval('state').in_(['assigned', 'packed']),
2018-08-19 20:02:58 +02:00
'tryton-back',
2012-03-25 00:18:19 +01:00
If(Eval('state') == 'waiting',
'tryton-clear',
2018-08-19 20:02:58 +02:00
'tryton-forward')),
2022-09-08 13:19:52 +02:00
'depends': [
'state', 'warehouse_storage', 'warehouse_output'],
2012-03-25 00:18:19 +01:00
},
'pick': {
'invisible': If(
Eval('warehouse_storage') == Eval('warehouse_output'),
True,
2022-09-08 13:19:52 +02:00
~Eval('state').in_(['assigned', 'packed'])),
'icon': If(Eval('state') == 'packed',
'tryton-back',
'tryton-forward'),
'depends': [
'state', 'warehouse_storage', 'warehouse_output'],
2012-03-25 00:18:19 +01:00
},
'pack': {
'invisible': If(
Eval('warehouse_storage') == Eval('warehouse_output'),
Eval('state') != 'assigned',
Eval('state') != 'picked'),
'depends': [
'state', 'warehouse_storage', 'warehouse_output'],
},
2024-05-06 00:34:03 +02:00
'ship': {
'invisible': Eval('state') != 'packed',
'depends': ['state'],
},
'do': {
2024-08-21 10:32:12 +02:00
'invisible': ~Eval('state').in_(['packed', 'shipped']),
2012-03-25 00:18:19 +01:00
},
2012-09-10 15:28:38 +02:00
'assign_wizard': {
'invisible': Eval('state') != 'waiting',
2012-09-10 15:28:38 +02:00
},
2012-03-25 00:18:19 +01:00
'assign_try': {},
'assign_force': {},
})
2008-04-08 14:27:53 +02:00
2012-09-24 22:18:17 +02:00
@classmethod
def __register__(cls, module_name):
2022-12-30 01:36:00 +01:00
pool = Pool()
Location = pool.get('stock.location')
cursor = Transaction().connection.cursor()
sql_table = cls.__table__()
2022-12-30 01:36:00 +01:00
location = Location.__table__()
2011-10-04 18:17:41 +02:00
2025-04-04 00:45:18 +02:00
super().__register__(module_name)
2009-02-02 13:13:06 +01:00
2022-12-30 01:36:00 +01:00
# Migration from 6.6: fill warehouse locations
cursor.execute(*sql_table.update(
[sql_table.warehouse_storage],
location.select(
location.storage_location,
where=location.id == sql_table.warehouse),
where=sql_table.warehouse_storage == Null))
cursor.execute(*sql_table.update(
[sql_table.warehouse_output],
location.select(
location.output_location,
where=location.id == sql_table.warehouse),
where=sql_table.warehouse_output == Null))
2012-09-24 22:18:17 +02:00
@staticmethod
def default_state():
2008-04-08 14:27:53 +02:00
return 'draft'
2012-09-24 22:18:17 +02:00
@classmethod
def default_warehouse(cls):
Location = Pool().get('stock.location')
return Location.get_default_warehouse()
2008-11-11 23:31:30 +01:00
2012-09-24 22:18:17 +02:00
@staticmethod
def default_company():
2012-03-27 10:44:04 +02:00
return Transaction().context.get('company')
2011-10-04 18:17:41 +02:00
2022-12-30 01:36:00 +01:00
@fields.depends('warehouse')
def on_change_warehouse(self):
if self.warehouse:
if self.warehouse.picking_location:
self.warehouse_storage = self.warehouse.picking_location
else:
self.warehouse_storage = self.warehouse.storage_location
self.warehouse_output = self.warehouse.output_location
else:
self.warehouse_storage = self.warehouse_output = None
2022-09-10 18:34:29 +02:00
@fields.depends('customer', 'warehouse')
2012-09-24 22:18:17 +02:00
def on_change_customer(self):
2014-10-29 20:40:25 +01:00
self.delivery_address = None
2013-12-12 13:50:05 +01:00
if self.customer:
2022-09-10 18:34:29 +02:00
with Transaction().set_context(
warehouse=self.warehouse.id if self.warehouse else None):
self.delivery_address = self.customer.address_get(
type='delivery')
2008-05-16 01:46:44 +02:00
2014-01-26 14:29:37 +01:00
@fields.depends('customer')
2012-09-24 22:18:17 +02:00
def on_change_with_customer_location(self, name=None):
2023-03-26 09:49:00 +02:00
return self.customer.customer_location if self.customer else None
2012-09-24 22:18:17 +02:00
def get_outgoing_moves(self, name):
if self.warehouse_output == self.warehouse_storage:
moves = self.moves
else:
moves = filter(
lambda m: m.from_location == self.warehouse_output, self.moves)
return sort(moves, self.__class__.outgoing_moves.order)
2012-09-24 22:18:17 +02:00
@classmethod
def set_outgoing_moves(cls, shipments, name, value):
2008-12-04 18:34:30 +01:00
if not value:
return
2012-09-24 22:18:17 +02:00
cls.write(shipments, {
'moves': value,
})
2012-09-24 22:18:17 +02:00
def get_inventory_moves(self, name):
moves = filter(
lambda m: m.to_location == self.warehouse_output, self.moves)
return sort(moves, self.__class__.inventory_moves.order)
2012-09-24 22:18:17 +02:00
@classmethod
def set_inventory_moves(cls, shipments, name, value):
2008-12-04 18:34:30 +01:00
if not value:
return
2012-09-24 22:18:17 +02:00
cls.write(shipments, {
'moves': value,
})
2014-09-17 21:45:58 +02:00
def get_origins(self, name):
2018-06-26 19:11:55 +02:00
return ', '.join(set(filter(None,
(m.origin_name for m in self.outgoing_moves))))
2014-12-02 12:56:04 +01:00
2025-12-02 16:01:39 +01:00
def chat_language(self, audience='internal'):
language = super().chat_language(audience=audience)
if audience == 'public':
language = self.customer.lang.code if self.customer.lang else None
return language
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('draft')
2012-09-24 22:18:17 +02:00
def draft(cls, shipments):
Move = Pool().get('stock.move')
Move.draft([m for s in shipments for m in s.outgoing_moves
2015-10-21 16:48:02 +02:00
if m.state != 'staging'])
Move.delete([m for s in shipments for m in s.inventory_moves
if m.state in {'staging', 'draft', 'cancelled'}])
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('waiting')
def wait(cls, shipments, moves=None):
2008-10-06 12:18:36 +02:00
"""
Complete inventory moves to match the products and quantities
that are in the outgoing moves.
If moves is set, only this subset is set to draft.
2008-10-06 12:18:36 +02:00
"""
2012-09-24 22:18:17 +02:00
Move = Pool().get('stock.move')
if moves is None:
moves = sum((s.inventory_moves for s in shipments), ())
else:
assert all(m.shipment in shipments for m in moves)
Move.draft(moves)
2012-09-24 22:18:17 +02:00
Move.delete([m for s in shipments for m in s.inventory_moves
2020-06-06 10:30:11 +01:00
if m.state in ('draft', 'cancelled')])
Move.draft([
m for s in shipments for m in s.outgoing_moves
if m.state != 'staging'])
2013-01-06 22:18:13 +01:00
to_create = []
2012-03-25 00:18:19 +01:00
for shipment in shipments:
if shipment.warehouse_storage == shipment.warehouse_output:
# Do not create inventory moves
continue
2012-03-25 00:18:19 +01:00
for move in shipment.outgoing_moves:
2020-06-06 10:30:11 +01:00
if move.state in ('cancelled', 'done'):
2012-03-25 00:18:19 +01:00
continue
inventory_move = shipment._get_inventory_move(move)
if inventory_move:
to_create.append(inventory_move)
2013-01-06 22:18:13 +01:00
if to_create:
2014-11-01 11:54:45 +01:00
Move.save(to_create)
2024-02-19 22:51:55 +01:00
cls.set_number(shipments)
def _get_inventory_move(self, move):
'Return inventory move for the outgoing move if necessary'
pool = Pool()
Move = pool.get('stock.move')
Uom = pool.get('product.uom')
quantity = move.quantity
for inventory_move in self.inventory_moves:
if (inventory_move.origin == move
and inventory_move.state != 'cancelled'):
quantity -= Uom.compute_qty(
2023-08-22 13:18:33 +02:00
inventory_move.unit, inventory_move.quantity, move.unit)
quantity = move.unit.round(quantity)
if quantity <= 0:
return
inventory_move = Move(
from_location=self.warehouse_storage,
to_location=move.from_location,
product=move.product,
2023-08-22 13:18:33 +02:00
unit=move.unit,
quantity=quantity,
shipment=self,
planned_date=move.planned_date,
company=move.company,
2019-07-08 17:18:14 +02:00
origin=move,
2015-01-02 15:40:11 +01:00
state='staging' if move.state == 'staging' else 'draft',
)
if inventory_move.on_change_with_unit_price_required():
inventory_move.unit_price = move.unit_price
inventory_move.currency = move.currency
else:
inventory_move.unit_price = None
inventory_move.currency = None
return inventory_move
2012-03-25 00:18:19 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@Workflow.transition('assigned')
2012-09-24 22:18:17 +02:00
def assign(cls, shipments):
2020-07-25 08:19:31 +02:00
pool = Pool()
Move = pool.get('stock.move')
Move.assign([m for s in shipments for m in s.assign_moves])
2019-08-18 19:22:43 +02:00
cls._sync_inventory_to_outgoing(shipments, quantity=False)
2012-03-25 00:18:19 +01:00
@classmethod
@ModelView.button
@Workflow.transition('picked')
@set_employee('picked_by')
def pick(cls, shipments):
pool = Pool()
Move = pool.get('stock.move')
2021-03-06 19:19:53 +01:00
Move.delete([
m for s in shipments for m in s.inventory_moves
if m.state == 'staging' or not m.quantity])
Move.do([m for s in shipments for m in s.inventory_moves])
2022-09-08 13:19:52 +02:00
Move.draft([m for s in shipments for m in s.outgoing_moves])
cls._sync_inventory_to_outgoing(shipments, quantity=True)
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('packed')
2017-02-01 21:41:00 +01:00
@set_employee('packed_by')
2012-09-24 22:18:17 +02:00
def pack(cls, shipments):
pool = Pool()
Move = pool.get('stock.move')
outgoing_moves, to_delete = [], []
for shipment in shipments:
for move in shipment.inventory_moves:
if move.state not in {'done', 'cancelled'}:
raise AccessError(
gettext('stock.msg_shipment_pack_inventory_done',
shipment=shipment.rec_name))
if shipment.warehouse_storage != shipment.warehouse_output:
shipment.check_quantity()
for move in shipment.outgoing_moves:
if move.quantity:
outgoing_moves.append(move)
else:
to_delete.append(move)
Move.delete(to_delete)
Move.assign(outgoing_moves)
@property
def _check_quantity_source_moves(self):
return self.inventory_moves
@property
def _check_quantity_target_moves(self):
return self.outgoing_moves
2012-03-25 00:18:19 +01:00
2019-08-18 19:22:43 +02:00
def _sync_move_key(self, move):
return (
('product', move.product),
2023-08-22 13:18:33 +02:00
('unit', move.unit),
)
2019-08-18 19:22:43 +02:00
def _sync_outgoing_move(self, template=None):
pool = Pool()
Move = pool.get('stock.move')
move = Move(
from_location=self.warehouse_output,
to_location=self.customer_location,
quantity=0,
shipment=self,
planned_date=self.planned_date,
company=self.company,
)
if template:
move.origin = template.origin
if move.on_change_with_unit_price_required():
if template:
move.unit_price = template.unit_price
move.currency = template.currency
else:
move.unit_price = 0
move.currency = self.company.currency
else:
move.unit_price = None
move.currency = None
2019-08-18 19:22:43 +02:00
return move
@classmethod
2019-08-18 19:22:43 +02:00
def _sync_inventory_to_outgoing(cls, shipments, quantity=True):
pool = Pool()
Move = pool.get('stock.move')
Uom = pool.get('product.uom')
2019-08-18 19:22:43 +02:00
def active(move):
2020-06-06 10:30:11 +01:00
return move.state != 'cancelled'
2019-08-18 19:22:43 +02:00
moves, imoves = [], []
2012-03-25 00:18:19 +01:00
for shipment in shipments:
if shipment.warehouse_storage == shipment.warehouse_output:
2019-08-18 19:22:43 +02:00
# Do not have inventory moves
continue
2012-03-25 00:18:19 +01:00
2019-08-18 19:22:43 +02:00
outgoing_moves = {m: m for m in shipment.outgoing_moves}
inventory_qty = defaultdict(lambda: defaultdict(float))
inventory_moves = defaultdict(lambda: defaultdict(list))
2019-08-18 19:22:43 +02:00
for move in filter(active, shipment.outgoing_moves):
key = shipment._sync_move_key(move)
inventory_qty[move][key] = 0
for move in filter(active, shipment.inventory_moves):
key = shipment._sync_move_key(move)
outgoing_move = outgoing_moves.get(move.origin)
qty_default_uom = Uom.compute_qty(
2023-08-22 13:18:33 +02:00
move.unit, move.quantity,
2012-04-03 20:07:43 +02:00
move.product.default_uom, round=False)
2019-08-18 19:22:43 +02:00
inventory_qty[outgoing_move][key] += qty_default_uom
inventory_moves[outgoing_move][key].append(move)
2019-08-18 19:22:43 +02:00
for outgoing_move in inventory_qty:
if outgoing_move:
outgoing_key = shipment._sync_move_key(outgoing_move)
for key, qty in inventory_qty[outgoing_move].items():
if not quantity and outgoing_move:
# Do not create outgoing move with origin
# to allow to reset to draft
2012-03-25 00:18:19 +01:00
continue
2019-08-18 19:22:43 +02:00
if outgoing_move and key == outgoing_key:
move = outgoing_move
2012-03-25 00:18:19 +01:00
else:
2019-08-18 19:22:43 +02:00
move = shipment._sync_outgoing_move(outgoing_move)
for name, value in key:
setattr(move, name, value)
for imove in inventory_moves[outgoing_move][key]:
imove.origin = move
imoves.append(imove)
2019-08-18 19:22:43 +02:00
qty = Uom.compute_qty(
2023-08-22 13:18:33 +02:00
move.product.default_uom, qty, move.unit)
2019-08-18 19:22:43 +02:00
if quantity and move.quantity != qty:
move.quantity = qty
moves.append(move)
2019-08-18 19:22:43 +02:00
Move.save(moves)
Move.save(imoves)
2012-03-25 00:18:19 +01:00
2024-05-06 00:34:03 +02:00
@classmethod
@ModelView.button
@Workflow.transition('shipped')
@set_employee('shipped_by')
def ship(cls, shipments):
pool = Pool()
Move = pool.get('stock.move')
Move.delete([
m for s in shipments for m in s.outgoing_moves
if m.state == 'staging'])
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('done')
2017-02-01 21:41:00 +01:00
@set_employee('done_by')
def do(cls, shipments):
2012-09-24 22:18:17 +02:00
pool = Pool()
Move = pool.get('stock.move')
Date = pool.get('ir.date')
2012-03-25 00:18:19 +01:00
2021-03-06 19:19:53 +01:00
Move.delete([
m for s in shipments for m in s.outgoing_moves
if m.state == 'staging'])
2012-12-27 21:53:07 +01:00
Move.do([m for s in shipments for m in s.outgoing_moves])
for company, c_shipments in groupby(
shipments, key=lambda s: s.company):
with Transaction().set_context(company=company.id):
today = Date.today()
cls.write([s for s in c_shipments if not s.effective_date], {
'effective_date': today,
})
2012-03-25 00:18:19 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('cancelled')
2012-09-24 22:18:17 +02:00
def cancel(cls, shipments):
Move = Pool().get('stock.move')
2012-12-27 21:53:07 +01:00
Move.cancel([m for s in shipments
for m in s.outgoing_moves + s.inventory_moves])
2008-04-17 02:31:55 +02:00
2012-09-24 22:18:17 +02:00
@property
def _move_planned_date(self):
'''
Return the planned date for outgoing moves and inventory moves
'''
2012-09-24 22:18:17 +02:00
return self.planned_date, self.planned_date
2012-09-24 22:18:17 +02:00
@classmethod
2018-09-19 00:15:59 +02:00
def _set_move_planned_date(cls, shipments):
'''
Set planned date of moves for the shipments
'''
2012-09-24 22:18:17 +02:00
Move = Pool().get('stock.move')
to_write = []
2012-09-24 22:18:17 +02:00
for shipment in shipments:
outgoing_date, inventory_date = shipment._move_planned_date
out_moves_to_write = [x for x in shipment.outgoing_moves
if (x.state not in {'done', 'cancelled'}
and x.planned_date != outgoing_date)]
if out_moves_to_write:
to_write.extend((out_moves_to_write, {
'planned_date': outgoing_date,
}))
inv_moves_to_write = [x for x in shipment.inventory_moves
if (x.state not in {'done', 'cancelled'}
and x.planned_date != inventory_date)]
if inv_moves_to_write:
to_write.extend((inv_moves_to_write, {
'planned_date': inventory_date,
}))
if to_write:
Move.write(*to_write)
2012-09-24 22:18:17 +02:00
@classmethod
def copy(cls, shipments, default=None):
2008-11-04 23:32:11 +01:00
if default is None:
default = {}
2018-09-01 01:02:04 +02:00
else:
default = default.copy()
default.setdefault('moves.origin', None)
default.setdefault('picked_by', None)
2018-09-01 01:02:04 +02:00
default.setdefault('packed_by', None)
default.setdefault('done_by', None)
2025-04-04 00:45:18 +02:00
return super().copy(shipments, default=default)
2008-11-04 23:32:11 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
2012-09-10 15:28:38 +02:00
@ModelView.button_action('stock.wizard_shipment_out_assign')
2012-09-24 22:18:17 +02:00
def assign_wizard(cls, shipments):
2012-09-10 15:28:38 +02:00
pass
2020-07-25 08:19:31 +02:00
@property
def assign_moves(self):
if self.warehouse_storage != self.warehouse_output:
return self.inventory_moves
else:
return self.outgoing_moves
2020-07-25 08:19:31 +02:00
@dualmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
2012-09-24 22:18:17 +02:00
def assign_try(cls, shipments):
Move = Pool().get('stock.move')
shipments = [
s for s in shipments
if s.state == 'waiting']
2020-07-25 08:19:31 +02:00
to_assign = [
m for s in shipments for m in s.assign_moves
2017-06-24 10:34:21 +02:00
if m.assignation_required]
if Move.assign_try(to_assign):
2012-09-24 22:18:17 +02:00
cls.assign(shipments)
2012-03-25 00:18:19 +01:00
else:
to_assign = []
for shipment in shipments:
if any(
m.state in {'staging', 'draft'}
for m in shipment.assign_moves
if m.assignation_required):
continue
to_assign.append(shipment)
if to_assign:
cls.assign(to_assign)
2008-04-17 02:31:55 +02:00
2020-10-12 19:20:33 +02:00
@classmethod
def _get_reschedule_domain(cls, date):
return [
('state', 'in', ['waiting', 'assigned', 'picked', 'packed']),
2020-10-12 19:20:33 +02:00
('planned_date', '<', date),
]
@classmethod
def reschedule(cls, date=None):
pool = Pool()
Date = pool.get('ir.date')
if date is None:
date = Date.today()
shipments = cls.search(cls._get_reschedule_domain(date))
cls.write(shipments, {'planned_date': date})
2008-11-14 11:12:38 +01:00
class ShipmentOutReturn(
2025-12-02 16:01:39 +01:00
ShipmentCheckQuantity, ShipmentMixin, Workflow, ModelSQL, ModelView,
ChatMixin):
2012-09-24 22:18:17 +02:00
__name__ = 'stock.shipment.out.return'
2021-04-20 09:18:58 +02:00
company = fields.Many2One(
'company.company', "Company", required=True,
2011-10-04 18:17:41 +02:00
states={
2015-06-28 23:45:31 +02:00
'readonly': Eval('state') != 'draft',
2011-10-04 18:17:41 +02:00
},
context={
'party_contact_mechanism_usage': 'delivery',
},
2018-12-04 12:25:45 +01:00
help="The company the shipment is associated with.")
2011-08-04 16:57:42 +02:00
customer = fields.Many2One('party.party', 'Customer', required=True,
states={
2015-06-28 23:45:31 +02:00
'readonly': ((Eval('state') != 'draft')
| Eval('incoming_moves', [0])),
2014-01-26 14:29:37 +01:00
},
context={
'company': Eval('company', -1),
'party_contact_mechanism_usage': 'delivery',
},
depends={'company'},
2018-12-04 12:25:45 +01:00
help="The party that purchased the stock.")
customer_location = fields.Function(fields.Many2One('stock.location',
2014-01-26 14:29:37 +01:00
'Customer Location'), 'on_change_with_customer_location')
2022-09-10 18:34:29 +02:00
contact_address = fields.Many2One(
'party.address', "Contact Address",
2011-08-04 16:57:42 +02:00
states={
2015-06-28 23:45:31 +02:00
'readonly': Eval('state') != 'draft',
},
domain=[('party', '=', Eval('customer', -1))],
2018-12-04 12:25:45 +01:00
help="The address the customer can be contacted at.")
2011-08-04 16:57:42 +02:00
warehouse = fields.Many2One('stock.location', "Warehouse", required=True,
states={
2015-06-28 23:45:31 +02:00
'readonly': ((Eval('state') != 'draft')
| Eval('incoming_moves', [0]) | Eval('inventory_moves', [0])),
2011-08-04 16:57:42 +02:00
}, domain=[('type', '=', 'warehouse')],
2018-12-04 12:25:45 +01:00
help="Where the stock is returned.")
2022-12-30 01:36:00 +01:00
warehouse_storage = fields.Many2One(
'stock.location', "Warehouse Storage", required=True,
domain=[
('type', 'in', ['storage', 'view']),
2022-12-30 01:36:00 +01:00
If(Eval('state') == 'draft',
('parent', 'child_of', [Eval('warehouse', -1)]),
()),
],
states={
'readonly': Eval('state') != 'draft',
})
warehouse_input = fields.Many2One(
'stock.location', "Warehouse Input", required=True,
domain=[
['OR',
('type', '=', 'storage'),
('id', '=', Eval('warehouse_storage', -1)),
],
2022-12-30 01:36:00 +01:00
If(Eval('state') == 'draft',
('parent', 'child_of', [Eval('warehouse', -1)]),
()),
],
states={
'readonly': Eval('state') != 'draft',
})
2013-05-27 18:15:02 +02:00
incoming_moves = fields.Function(fields.One2Many('stock.move', 'shipment',
'Incoming Moves',
domain=[
2022-12-30 01:36:00 +01:00
If(Eval('state') == 'draft',
('from_location', '=', Eval('customer_location')),
()),
If(Eval('warehouse_input') == Eval('warehouse_storage'),
('to_location', 'child_of',
[Eval('warehouse_input', -1)], 'parent'),
('to_location', '=', Eval('warehouse_input'))),
('company', '=', Eval('company', -1)),
],
2023-02-22 16:58:23 +01:00
order=[
('product', 'ASC'),
('id', 'ASC'),
],
states={
'readonly': ((Eval('state') != 'draft')
| ~Eval('warehouse') | ~Eval('customer')),
},
2018-12-04 12:25:45 +01:00
help="The moves that bring the stock into the warehouse."),
2011-08-04 16:57:42 +02:00
'get_incoming_moves', setter='set_incoming_moves')
2013-05-27 18:15:02 +02:00
inventory_moves = fields.Function(fields.One2Many('stock.move', 'shipment',
'Inventory Moves',
domain=[
('from_location', '=', Eval('warehouse_input', -1)),
If(Eval('state').in_(['received']),
2020-10-11 23:46:10 +02:00
['OR',
('to_location', 'child_of',
[Eval('warehouse_storage', -1)], 'parent'),
('to_location.waste_warehouses', '=',
Eval('warehouse', -1)),
2020-10-11 23:46:10 +02:00
],
[]),
('company', '=', Eval('company', -1)),
],
2023-02-22 16:58:23 +01:00
order=[
('to_location', 'ASC'),
('product', 'ASC'),
('id', 'ASC'),
],
states={
'readonly': Eval('state').in_(['draft', 'cancelled', 'done']),
'invisible': (
Eval('warehouse_input') == Eval('warehouse_storage')),
},
2018-12-04 12:25:45 +01:00
help="The moves that put the stock away into the storage area."),
2011-08-04 16:57:42 +02:00
'get_inventory_moves', setter='set_inventory_moves')
2024-02-06 00:12:55 +01:00
moves = fields.One2Many(
'stock.move', 'shipment', "Moves",
domain=[('company', '=', Eval('company', -1))],
2024-02-06 00:12:55 +01:00
states={
'readonly': True,
})
2013-09-19 17:34:23 +02:00
origins = fields.Function(fields.Char('Origins'), 'get_origins')
2017-02-01 21:41:00 +01:00
received_by = employee_field("Received By")
done_by = employee_field("Done By")
2009-01-27 15:15:01 +01:00
state = fields.Selection([
('draft', 'Draft'),
2022-02-15 00:08:47 +01:00
('received', 'Received'),
2009-01-27 15:15:01 +01:00
('done', 'Done'),
('cancelled', 'Cancelled'),
2022-02-15 00:08:47 +01:00
], "State", readonly=True, sort=False,
2018-12-04 12:25:45 +01:00
help="The current state of the shipment.")
2009-01-27 15:15:01 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
def __setup__(cls):
2025-04-04 00:45:18 +02:00
super().__setup__()
t = cls.__table__()
cls._sql_indexes.update({
Index(
t,
2024-07-27 18:36:14 +02:00
(t.state, Index.Equality(cardinality='low')),
where=t.state.in_(['draft', 'received'])),
})
2012-09-24 22:18:17 +02:00
cls._transitions |= set((
2012-03-25 00:18:19 +01:00
('draft', 'received'),
('received', 'done'),
2014-08-11 17:12:07 +05:30
('received', 'draft'),
('draft', 'cancelled'),
('received', 'cancelled'),
('cancelled', 'draft'),
('done', 'cancelled'),
2012-03-25 00:18:19 +01:00
))
2012-09-24 22:18:17 +02:00
cls._buttons.update({
2012-03-25 00:18:19 +01:00
'cancel': {
'invisible': (
(Eval('state') == 'cancelled')
| ((Eval('state') == 'done')
& ~Id('stock',
'group_stock_cancellation').in_(
Eval('context', {}).get('groups', [])))),
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
'draft': {
'invisible': Eval('state') != 'cancelled',
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
'receive': {
'invisible': Eval('state') != 'draft',
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
'do': {
2012-03-25 00:18:19 +01:00
'invisible': Eval('state') != 'received',
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
})
2009-01-27 15:15:01 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
def __register__(cls, module_name):
2022-12-30 01:36:00 +01:00
pool = Pool()
Location = pool.get('stock.location')
cursor = Transaction().connection.cursor()
2018-07-24 22:01:08 +02:00
table = cls.__table_handler__(module_name)
sql_table = cls.__table__()
2022-12-30 01:36:00 +01:00
location = Location.__table__()
2011-10-04 18:17:41 +02:00
2022-09-10 18:34:29 +02:00
# Migration from 6.4: rename delivery_address to contact_address
table.column_rename('delivery_address', 'contact_address')
2025-04-04 00:45:18 +02:00
super().__register__(module_name)
2009-01-27 15:15:01 +01:00
2022-09-10 18:34:29 +02:00
# Migration from 6.4: remove required on contact_address
table.not_null_action('contact_address', 'remove')
2022-12-30 01:36:00 +01:00
# Migration from 6.6: fill warehouse locations
cursor.execute(*sql_table.update(
[sql_table.warehouse_input],
location.select(
location.input_location,
where=location.id == sql_table.warehouse),
where=sql_table.warehouse_input == Null))
cursor.execute(*sql_table.update(
[sql_table.warehouse_storage],
location.select(
location.storage_location,
where=location.id == sql_table.warehouse),
where=sql_table.warehouse_storage == Null))
2012-09-24 22:18:17 +02:00
@staticmethod
def default_state():
2009-01-27 15:15:01 +01:00
return 'draft'
2012-09-24 22:18:17 +02:00
@classmethod
def default_warehouse(cls):
Location = Pool().get('stock.location')
return Location.get_default_warehouse()
2009-01-27 15:15:01 +01:00
2022-12-30 01:36:00 +01:00
@fields.depends('warehouse')
def on_change_warehouse(self):
if self.warehouse:
self.warehouse_input = self.warehouse.input_location
self.warehouse_storage = self.warehouse.storage_location
else:
self.warehouse_input = self.warehouse_storage = None
2012-09-24 22:18:17 +02:00
@staticmethod
def default_company():
2012-03-27 10:44:04 +02:00
return Transaction().context.get('company')
2011-10-04 18:17:41 +02:00
2014-01-26 14:29:37 +01:00
@fields.depends('customer')
2012-09-24 22:18:17 +02:00
def on_change_customer(self):
2022-09-10 18:34:29 +02:00
self.contact_address = None
2013-12-12 13:50:05 +01:00
if self.customer:
2022-09-10 18:34:29 +02:00
self.contact_address = self.customer.address_get()
2009-01-27 15:15:01 +01:00
2014-01-26 14:29:37 +01:00
@fields.depends('customer')
2012-09-24 22:18:17 +02:00
def on_change_with_customer_location(self, name=None):
2023-03-26 09:49:00 +02:00
return self.customer.customer_location if self.customer else None
2012-09-24 22:18:17 +02:00
def get_incoming_moves(self, name):
if self.warehouse_input == self.warehouse_storage:
moves = self.moves
else:
moves = filter(
lambda m: m.to_location == self.warehouse_input, self.moves)
return sort(moves, self.__class__.incoming_moves.order)
2012-09-24 22:18:17 +02:00
@classmethod
def set_incoming_moves(cls, shipments, name, value):
2009-01-27 15:15:01 +01:00
if not value:
return
2012-09-24 22:18:17 +02:00
cls.write(shipments, {
'moves': value,
})
2009-01-27 15:15:01 +01:00
2012-09-24 22:18:17 +02:00
def get_inventory_moves(self, name):
moves = filter(
lambda m: m.from_location == self.warehouse_input, self.moves)
return sort(moves, self.__class__.inventory_moves.order)
2009-01-27 15:15:01 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
def set_inventory_moves(cls, shipments, name, value):
2009-01-27 15:15:01 +01:00
if not value:
return
2012-09-24 22:18:17 +02:00
cls.write(shipments, {
'moves': value,
})
2009-01-27 15:15:01 +01:00
2012-09-24 22:18:17 +02:00
def _get_move_planned_date(self):
'''
Return the planned date for incoming moves and inventory moves
'''
2012-09-24 22:18:17 +02:00
return self.planned_date, self.planned_date
2012-09-24 22:18:17 +02:00
@classmethod
def _set_move_planned_date(cls, shipments):
'''
Set planned date of moves for the shipments
'''
2012-09-24 22:18:17 +02:00
Move = Pool().get('stock.move')
to_write = []
2012-09-24 22:18:17 +02:00
for shipment in shipments:
dates = shipment._get_move_planned_date()
incoming_date, inventory_date = dates
incoming_moves_to_write = [x for x in shipment.incoming_moves
if (x.state not in {'done', 'cancelled'}
and x.planned_date != incoming_date)]
if incoming_moves_to_write:
to_write.extend((incoming_moves_to_write, {
'planned_date': incoming_date,
}))
inventory_moves_to_write = [x for x in shipment.inventory_moves
if (x.state not in {'done', 'cancelled'}
and x.planned_date != inventory_date)]
if inventory_moves_to_write:
to_write.extend((inventory_moves_to_write, {
'planned_date': inventory_date,
}))
if to_write:
Move.write(*to_write)
2014-09-17 21:45:58 +02:00
def get_origins(self, name):
2018-06-26 19:11:55 +02:00
return ', '.join(set(filter(None,
(m.origin_name for m in self.incoming_moves))))
2013-09-19 17:34:23 +02:00
2025-12-02 16:01:39 +01:00
def chat_language(self, audience='internal'):
language = super().chat_language(audience=audience)
if audience == 'public':
language = self.customer.lang.code if self.customer.lang else None
return language
2012-09-24 22:18:17 +02:00
@classmethod
def preprocess_values(cls, mode, values):
pool = Pool()
Configuration = pool.get('stock.configuration')
values = super().preprocess_values(mode, values)
if mode == 'create' and not values.get('number'):
company_id = values.get('company', cls.default_company())
if company_id is not None:
configuration = Configuration(1)
if sequence := configuration.get_multivalue(
'shipment_out_return_sequence', company=company_id):
values['number'] = sequence.get()
return values
2009-01-27 15:15:01 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
def copy(cls, shipments, default=None):
2009-01-27 15:15:01 +01:00
if default is None:
default = {}
default = default.copy()
default.setdefault('moves.origin', None)
2018-09-01 01:02:04 +02:00
default.setdefault('received_by', None)
default.setdefault('done_by', None)
2025-04-04 00:45:18 +02:00
return super().copy(shipments, default=default)
2009-01-27 15:15:01 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('draft')
2012-09-24 22:18:17 +02:00
def draft(cls, shipments):
Move = Pool().get('stock.move')
2015-10-21 16:48:02 +02:00
Move.draft([m for s in shipments for m in s.incoming_moves
if m.state != 'staging'])
2012-09-24 22:18:17 +02:00
Move.delete([m for s in shipments for m in s.inventory_moves
if m.state in {'staging', 'draft', 'cancelled'}])
2009-01-27 15:15:01 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('received')
2017-02-01 21:41:00 +01:00
@set_employee('received_by')
2012-09-24 22:18:17 +02:00
def receive(cls, shipments):
Move = Pool().get('stock.move')
2012-12-27 21:53:07 +01:00
Move.do([m for s in shipments for m in s.incoming_moves])
2012-09-24 22:18:17 +02:00
cls.create_inventory_moves(shipments)
# Set received state to allow done transition
cls.write(shipments, {'state': 'received'})
to_do = [s for s in shipments
if s.warehouse_storage == s.warehouse_input]
if to_do:
cls.do(to_do)
2012-03-25 00:18:19 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('done')
2017-02-01 21:41:00 +01:00
@set_employee('done_by')
def do(cls, shipments):
2012-09-24 22:18:17 +02:00
pool = Pool()
Move = pool.get('stock.move')
Date = pool.get('ir.date')
inventory_moves = []
for shipment in shipments:
if shipment.warehouse_storage != shipment.warehouse_input:
shipment.check_quantity()
inventory_moves.extend(shipment.inventory_moves)
Move.do(inventory_moves)
for company, c_shipments in groupby(
shipments, key=lambda s: s.company):
with Transaction().set_context(company=company.id):
today = Date.today()
cls.write([s for s in c_shipments if not s.effective_date], {
'effective_date': today,
})
2009-01-27 15:15:01 +01:00
@property
def _check_quantity_source_moves(self):
return self.incoming_moves
@property
def _check_quantity_target_moves(self):
return self.inventory_moves
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('cancelled')
2012-09-24 22:18:17 +02:00
def cancel(cls, shipments):
Move = Pool().get('stock.move')
2012-12-27 21:53:07 +01:00
Move.cancel([m for s in shipments
for m in s.incoming_moves + s.inventory_moves])
2009-01-27 15:15:01 +01:00
2019-04-11 18:54:55 +02:00
def _get_inventory_move(self, incoming_move):
'Return inventory move for the incoming move'
2013-03-12 10:35:12 +01:00
pool = Pool()
Move = pool.get('stock.move')
2020-03-06 23:53:19 +01:00
Date = pool.get('ir.date')
2009-01-27 15:15:01 +01:00
if incoming_move.quantity <= 0.0:
2013-03-12 10:35:12 +01:00
return
with Transaction().set_context(company=self.company.id):
today = Date.today()
2013-03-12 10:35:12 +01:00
move = Move()
move.product = incoming_move.product
2023-08-22 13:18:33 +02:00
move.unit = incoming_move.unit
2013-03-12 10:35:12 +01:00
move.quantity = incoming_move.quantity
move.from_location = incoming_move.to_location
2019-04-11 18:54:55 +02:00
move.to_location = self.warehouse_storage
move.state = (
'staging' if incoming_move.state == 'staging' else 'draft')
move.planned_date = max(
filter(None, [self._get_move_planned_date()[1], today]))
2013-03-12 10:35:12 +01:00
move.company = incoming_move.company
2019-07-08 17:18:14 +02:00
move.origin = incoming_move
2013-03-12 10:35:12 +01:00
return move
2009-01-27 15:15:01 +01:00
2012-09-24 22:18:17 +02:00
@classmethod
def create_inventory_moves(cls, shipments):
2012-03-25 00:18:19 +01:00
for shipment in shipments:
if shipment.warehouse_storage == shipment.warehouse_input:
# Do not create inventory moves
continue
2013-03-12 10:35:12 +01:00
# Use moves instead of inventory_moves because save reset before
# adding new records and as set_inventory_moves is just a proxy to
# moves, it will reset also the incoming_moves
moves = list(shipment.moves)
2012-03-25 00:18:19 +01:00
for incoming_move in shipment.incoming_moves:
2019-04-11 18:54:55 +02:00
move = shipment._get_inventory_move(incoming_move)
2013-03-12 10:35:12 +01:00
if move:
moves.append(move)
shipment.moves = moves
cls.save(shipments)
2009-01-27 15:15:01 +01:00
class ShipmentInternal(
ShipmentCheckQuantity, ShipmentAssignMixin, Workflow, ModelSQL,
2025-12-02 16:01:39 +01:00
ModelView, ChatMixin):
2012-09-24 22:18:17 +02:00
__name__ = 'stock.shipment.internal'
_assign_moves_field = 'moves'
effective_start_date = fields.Date('Effective Start Date',
domain=[
If(Eval('effective_start_date') & Eval('effective_date'),
('effective_start_date', '<=', Eval('effective_date')),
()),
],
states={
'readonly': Eval('state').in_(['cancelled', 'shipped', 'done']),
},
2018-12-04 12:25:45 +01:00
help="When the stock was actually sent.")
planned_start_date = fields.Date('Planned Start Date',
domain=[
If(Eval('planned_start_date') & Eval('planned_date'),
('planned_start_date', '<=', Eval('planned_date')),
()),
],
states={
2017-01-15 00:17:40 +01:00
'readonly': ~Eval('state').in_(['request', 'draft']),
'required': Bool(Eval('planned_date')),
},
2018-12-04 12:25:45 +01:00
help="When the stock is expected to be sent.")
2021-04-20 09:18:58 +02:00
company = fields.Many2One(
'company.company', "Company", required=True,
2011-10-04 18:17:41 +02:00
states={
2017-01-15 00:17:40 +01:00
'readonly': ~Eval('state').in_(['request', 'draft']),
2011-10-04 18:17:41 +02:00
},
context={
'party_contact_mechanism_usage': 'delivery',
},
2018-12-04 12:25:45 +01:00
help="The company the shipment is associated with.")
2010-01-20 11:04:28 +01:00
from_location = fields.Many2One('stock.location', "From Location",
2011-08-04 16:57:42 +02:00
required=True, states={
2017-01-15 00:17:40 +01:00
'readonly': (~Eval('state').in_(['request', 'draft'])
| Eval('moves', [0])),
2010-01-20 11:04:28 +01:00
},
2011-08-04 16:57:42 +02:00
domain=[
('type', 'in', ['view', 'storage', 'lost_found']),
],
2018-12-04 12:25:45 +01:00
help="Where the stock is moved from.")
2008-10-06 12:20:19 +02:00
to_location = fields.Many2One('stock.location', "To Location",
2011-08-04 16:57:42 +02:00
required=True, states={
2017-01-15 00:17:40 +01:00
'readonly': (~Eval('state').in_(['request', 'draft'])
| Eval('moves', [0])),
2010-01-20 11:04:28 +01:00
}, domain=[
('type', 'in', ['view', 'storage', 'lost_found']),
],
2018-12-04 12:25:45 +01:00
help="Where the stock is moved to.")
transit_location = fields.Function(fields.Many2One('stock.location',
2018-12-04 12:25:45 +01:00
'Transit Location',
help="Where the stock is located while it is in transit between "
"the warehouses."),
'on_change_with_transit_location')
internal_transit_location = fields.Many2One(
'stock.location', "Internal Transit Location",
readonly=True,
domain=[
('type', '=', 'storage'),
('parent', '=', None),
('id', 'not in', [
Eval('from_location', -1), Eval('to_location', -1)]),
])
2021-03-21 12:52:58 +01:00
warehouse = fields.Function(
fields.Many2One(
'stock.location', "Warehouse",
domain=[
('type', '=', 'warehouse'),
],
2021-03-21 12:52:58 +01:00
help="Where the stock is sent from."),
'on_change_with_warehouse')
to_warehouse = fields.Function(
fields.Many2One(
'stock.location', "To Warehouse",
domain=[
('type', '=', 'warehouse'),
],
help="Where the stock is sent to."),
'on_change_with_to_warehouse')
2013-03-23 15:42:36 +01:00
moves = fields.One2Many('stock.move', 'shipment', 'Moves',
2010-01-20 11:04:28 +01:00
states={
'readonly': (Eval('state').in_(['cancelled', 'assigned', 'done'])
| ~Eval('from_location') | ~Eval('to_location')),
'invisible': (Bool(Eval('transit_location'))
2017-01-15 00:17:40 +01:00
& ~Eval('state').in_(['request', 'draft'])),
2011-10-04 18:17:41 +02:00
},
domain=[
2017-01-15 00:17:40 +01:00
If(Eval('state').in_(['request', 'draft']), [
('from_location', '=', Eval('from_location')),
('to_location', '=', Eval('to_location')),
],
If(~Eval('state').in_(['done', 'cancelled']),
If(~Eval('transit_location'),
[
('from_location', 'child_of',
[Eval('from_location', -1)], 'parent'),
('to_location', 'child_of',
[Eval('to_location', -1)], 'parent'),
],
['OR',
[
('from_location', 'child_of',
[Eval('from_location', -1)], 'parent'),
('to_location', '=', Eval('transit_location')),
],
[
('from_location', '=',
Eval('transit_location')),
('to_location', 'child_of',
[Eval('to_location', -1)], 'parent'),
],
]),
[])),
('company', '=', Eval('company', -1)),
],
2023-02-22 16:58:23 +01:00
order=[
('from_location', 'ASC'),
('product', 'ASC'),
('id', 'ASC'),
],
2018-12-04 12:25:45 +01:00
help="The moves that perform the shipment.")
outgoing_moves = fields.Function(fields.One2Many('stock.move', 'shipment',
'Outgoing Moves',
domain=[
If(Eval('state').in_(['request', 'draft']), [
('from_location', 'child_of',
[Eval('from_location', -1)], 'parent'),
If(~Eval('transit_location'),
('to_location', 'child_of',
[Eval('to_location', -1)], 'parent'),
('to_location', '=', Eval('transit_location'))),
],
[]),
],
2023-02-22 16:58:23 +01:00
order=[
('from_location', 'ASC'),
('product', 'ASC'),
('id', 'ASC'),
],
states={
'readonly': Eval('state').in_(
['assigned', 'shipped', 'done', 'cancelled']),
'invisible': (~Eval('transit_location')
2017-01-15 00:17:40 +01:00
| Eval('state').in_(['request', 'draft'])),
},
2018-12-04 12:25:45 +01:00
help="The moves that send the stock out."),
'get_outgoing_moves', setter='set_moves')
incoming_moves = fields.Function(fields.One2Many('stock.move', 'shipment',
'Incoming Moves',
domain=[
If(~Eval('state').in_(['done', 'cancelled']), [
If(~Eval('transit_location'),
('from_location', 'child_of',
[Eval('from_location', -1)], 'parent'),
('from_location', '=', Eval('transit_location'))),
('to_location', 'child_of',
[Eval('to_location', -1)], 'parent'),
],
[]),
],
2023-02-22 16:58:23 +01:00
order=[
('to_location', 'ASC'),
('product', 'ASC'),
('id', 'ASC'),
],
states={
'readonly': Eval('state').in_(['done', 'cancelled']),
'invisible': (~Eval('transit_location')
2017-01-15 00:17:40 +01:00
| Eval('state').in_(['request', 'draft'])),
},
2018-12-04 12:25:45 +01:00
help="The moves that receive the stock in."),
'get_incoming_moves', setter='set_moves')
2017-02-01 21:41:00 +01:00
assigned_by = employee_field("Received By")
2024-12-22 18:23:21 +01:00
packed_by = employee_field("Packed By")
2017-02-01 21:41:00 +01:00
shipped_by = employee_field("Shipped By")
done_by = employee_field("Done By")
2008-10-06 12:20:19 +02:00
state = fields.Selection([
2017-01-15 00:17:40 +01:00
('request', 'Request'),
('draft', 'Draft'),
('waiting', 'Waiting'),
('assigned', 'Assigned'),
2024-12-22 18:23:21 +01:00
('packed', "Packed"),
('shipped', 'Shipped'),
('done', 'Done'),
2022-02-15 00:08:47 +01:00
('cancelled', 'Cancelled'),
], "State", readonly=True, sort=False,
2018-12-04 12:25:45 +01:00
help="The current state of the shipment.")
state_string = state.translated('state')
2008-10-06 12:20:19 +02:00
2012-09-24 22:18:17 +02:00
@classmethod
def __setup__(cls):
2025-04-04 00:45:18 +02:00
super().__setup__()
t = cls.__table__()
cls._sql_indexes.update({
Index(
t,
2024-07-27 18:36:14 +02:00
(t.state, Index.Equality(cardinality='low')),
where=t.state.in_([
'request', 'draft', 'waiting',
'assigned', 'shipped'])),
})
2012-09-24 22:18:17 +02:00
cls._transitions |= set((
2017-01-15 00:17:40 +01:00
('request', 'draft'),
2012-03-25 00:18:19 +01:00
('draft', 'waiting'),
('waiting', 'waiting'),
('waiting', 'assigned'),
2024-12-22 18:23:21 +01:00
('assigned', 'packed'),
2012-03-25 00:18:19 +01:00
('assigned', 'done'),
2024-12-22 18:23:21 +01:00
('packed', 'shipped'),
('packed', 'waiting'),
('shipped', 'done'),
2012-03-25 00:18:19 +01:00
('waiting', 'draft'),
('assigned', 'waiting'),
('request', 'cancelled'),
('draft', 'cancelled'),
('waiting', 'cancelled'),
('assigned', 'cancelled'),
('packed', 'cancelled'),
('cancelled', 'draft'),
('done', 'cancelled'),
2012-03-25 00:18:19 +01:00
))
2012-09-24 22:18:17 +02:00
cls._buttons.update({
2012-03-25 00:18:19 +01:00
'cancel': {
'invisible': (
(Eval('state') == 'cancelled')
| (Eval('state').in_(['shipped', 'done'])
& ~Id('stock',
'group_stock_cancellation').in_(
Eval('context', {}).get('groups', [])))),
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
'draft': {
2017-01-15 00:17:40 +01:00
'invisible': ~Eval('state').in_(
['cancelled', 'request', 'waiting']),
'icon': If(Eval('state') == 'cancelled',
2018-08-19 20:02:58 +02:00
'tryton-undo',
2017-01-15 00:17:40 +01:00
If(Eval('state') == 'request',
2018-08-19 20:02:58 +02:00
'tryton-forward',
'tryton-back')),
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
'wait': {
'invisible': ~Eval('state').in_(['assigned', 'waiting',
'draft']),
'icon': If(Eval('state') == 'assigned',
2018-08-19 20:02:58 +02:00
'tryton-back',
2012-03-25 00:18:19 +01:00
If(Eval('state') == 'waiting',
'tryton-clear',
2018-08-19 20:02:58 +02:00
'tryton-forward')),
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-03-25 00:18:19 +01:00
},
2024-12-22 18:23:21 +01:00
'pack': {
'invisible': (
(Eval('state') != 'assigned')
| ~Eval('transit_location')),
'depends': ['state', 'transit_location'],
},
'ship': {
2024-12-22 18:23:21 +01:00
'invisible': ((Eval('state') != 'packed')
2020-03-01 12:33:54 +01:00
| ~Eval('transit_location')),
2018-02-19 11:41:53 +01:00
'depends': ['state', 'transit_location'],
},
'do': {
'invisible': If(
~Eval('transit_location'),
Eval('state') != 'assigned',
Eval('state') != 'shipped'),
2018-02-19 11:41:53 +01:00
'depends': ['state', 'transit_location'],
2012-03-25 00:18:19 +01:00
},
2012-09-10 15:28:38 +02:00
'assign_wizard': {
'invisible': Eval('state') != 'waiting',
2018-02-19 11:41:53 +01:00
'depends': ['state'],
2012-09-10 15:28:38 +02:00
},
2012-03-25 00:18:19 +01:00
'assign_try': {},
'assign_force': {},
})
2021-02-25 21:13:56 +01:00
@classmethod
def order_effective_date(cls, tables):
table, _ = tables[None]
return [Coalesce(
table.effective_start_date, table.effective_date,
table.planned_start_date, table.planned_date)]
2012-09-24 22:18:17 +02:00
@staticmethod
def default_state():
2008-10-06 12:20:19 +02:00
return 'draft'
2012-09-24 22:18:17 +02:00
@staticmethod
def default_company():
2012-03-27 10:44:04 +02:00
return Transaction().context.get('company')
2011-10-04 18:17:41 +02:00
@fields.depends(
'state', 'from_location', 'to_location', 'company',
'internal_transit_location')
def on_change_with_transit_location(self, name=None):
pool = Pool()
Config = pool.get('stock.configuration')
if self.state in {'request', 'draft'}:
from_ = self.from_location
to = self.to_location
if (from_
and to
and from_.warehouse != to.warehouse
and from_.warehouse
and to.warehouse):
return Config(1).get_multivalue(
'shipment_internal_transit',
company=self.company.id if self.company else None)
else:
return self.internal_transit_location
2021-03-21 12:52:58 +01:00
@fields.depends('from_location')
def on_change_with_warehouse(self, name=None):
2023-03-26 09:49:00 +02:00
return self.from_location.warehouse if self.from_location else None
2021-03-21 12:52:58 +01:00
@fields.depends('to_location')
def on_change_with_to_warehouse(self, name=None):
return self.to_location.warehouse if self.to_location else None
@fields.depends(
'planned_date', 'from_location', 'to_location',
methods=['on_change_with_transit_location'])
def on_change_with_planned_start_date(self, pattern=None):
pool = Pool()
LocationLeadTime = pool.get('stock.location.lead_time')
transit_location = self.on_change_with_transit_location()
if self.planned_date and transit_location:
if pattern is None:
pattern = {}
pattern.setdefault('warehouse_from',
self.from_location.warehouse.id
if self.from_location and self.from_location.warehouse
else None)
pattern.setdefault('warehouse_to',
self.to_location.warehouse.id
if self.to_location and self.to_location.warehouse
else None)
lead_time = LocationLeadTime.get_lead_time(pattern)
if lead_time:
return self.planned_date - lead_time
return self.planned_date
def get_outgoing_moves(self, name):
if not self.transit_location:
moves = self.moves
2023-02-22 16:58:23 +01:00
else:
moves = filter(
lambda m: m.to_location == self.transit_location, self.moves)
return sort(moves, self.__class__.outgoing_moves.order)
def get_incoming_moves(self, name):
if not self.transit_location:
moves = self.moves
2023-02-22 16:58:23 +01:00
else:
moves = filter(
lambda m: m.from_location == self.transit_location, self.moves)
return sort(moves, self.__class__.incoming_moves.order)
@classmethod
def set_moves(cls, shipments, name, value):
if not value:
return
cls.write(shipments, {
'moves': value,
})
@classmethod
def copy(cls, shipments, default=None):
2018-09-01 01:02:04 +02:00
def shipment_field(data, name):
model, shipment_id = data['shipment'].split(',', 1)
assert model == cls.__name__
shipment_id = int(shipment_id)
shipment = id2shipments[shipment_id]
return getattr(shipment, name)
def outgoing_moves(data):
shipment = id2shipments[data['id']]
return shipment.outgoing_moves
id2shipments = {s.id: s for s in shipments}
if default is None:
default = {}
else:
default = default.copy()
2018-09-01 01:02:04 +02:00
default.setdefault('moves', outgoing_moves)
default.setdefault('moves.origin', None)
2018-09-01 01:02:04 +02:00
default.setdefault('moves.from_location', partial(
shipment_field, name='from_location'))
default.setdefault('moves.to_location', partial(
shipment_field, name='to_location'))
default.setdefault('moves.planned_date', partial(
shipment_field, name='planned_date'))
default.setdefault('assigned_by', None)
2024-12-22 18:23:21 +01:00
default.setdefault('packed_by', None)
2018-09-01 01:02:04 +02:00
default.setdefault('shipped_by', None)
default.setdefault('done_by', None)
return super().copy(shipments, default=default)
2019-08-18 19:22:43 +02:00
def _sync_move_key(self, move):
return (
('product', move.product),
2023-08-22 13:18:33 +02:00
('unit', move.unit),
2019-08-18 19:22:43 +02:00
)
def _sync_incoming_move(self, template=None):
pool = Pool()
Move = pool.get('stock.move')
move = Move(
from_location=self.transit_location,
to_location=self.to_location,
quantity=0,
shipment=self,
planned_date=self.planned_date,
company=self.company,
)
if template:
move.origin = template.origin
move.state = (
'staging' if template.state == 'staging' else 'draft')
if move.on_change_with_unit_price_required():
if template:
move.unit_price = template.unit_price
move.currency = template.currency
else:
move.unit_price = 0
move.currency = self.company.currency
else:
move.unit_price = None
move.currency = None
2019-08-18 19:22:43 +02:00
return move
@classmethod
def _sync_moves(cls, shipments):
pool = Pool()
Move = pool.get('stock.move')
Uom = pool.get('product.uom')
2019-08-18 19:22:43 +02:00
def active(move):
2020-06-06 10:30:11 +01:00
return move.state != 'cancelled'
2019-08-18 19:22:43 +02:00
moves, omoves = [], []
for shipment in shipments:
2017-01-15 00:17:40 +01:00
if not shipment.transit_location:
continue
2019-08-18 19:22:43 +02:00
incoming_moves = {m: m for m in shipment.incoming_moves}
outgoing_qty = defaultdict(lambda: defaultdict(float))
outgoing_moves = defaultdict(lambda: defaultdict(list))
2019-08-18 19:22:43 +02:00
for move in filter(active, shipment.incoming_moves):
key = shipment._sync_move_key(move)
outgoing_qty[move][key] = 0
for move in filter(active, shipment.outgoing_moves):
key = shipment._sync_move_key(move)
incoming_move = incoming_moves.get(move.origin)
qty_default_uom = Uom.compute_qty(
2023-08-22 13:18:33 +02:00
move.unit, move.quantity,
2019-08-18 19:22:43 +02:00
move.product.default_uom, round=False)
outgoing_qty[incoming_move][key] += qty_default_uom
outgoing_moves[incoming_move][key].append(move)
2019-08-18 19:22:43 +02:00
for incoming_move in outgoing_qty:
if incoming_move:
incoming_key = shipment._sync_move_key(incoming_move)
for key, qty in outgoing_qty[incoming_move].items():
if incoming_move and key == incoming_key:
move = incoming_move
else:
move = shipment._sync_incoming_move(incoming_move)
for name, value in key:
setattr(move, name, value)
for omove in outgoing_moves[incoming_move][key]:
omove.origin = move
omoves.append(omove)
2019-08-18 19:22:43 +02:00
qty = Uom.compute_qty(
2023-08-22 13:18:33 +02:00
move.product.default_uom, qty, move.unit)
2019-08-18 19:22:43 +02:00
if move.quantity != qty:
move.quantity = qty
moves.append(move)
# Save incoming moves first to get id for outgoing moves
2019-08-18 19:22:43 +02:00
Move.save(moves)
Move.save(omoves)
2017-01-15 00:17:40 +01:00
@classmethod
def _set_transit(cls, shipments):
pool = Pool()
Move = pool.get('stock.move')
to_write = []
for shipment in shipments:
if not shipment.transit_location:
continue
moves = [m for m in shipment.moves
if m.state != 'done'
and m.from_location != shipment.transit_location
and m.to_location != shipment.transit_location]
if not moves:
continue
2017-01-15 00:17:40 +01:00
Move.copy(moves, default={
2019-08-18 19:22:43 +02:00
'to_location': shipment.transit_location.id,
2019-09-16 23:58:02 +02:00
'planned_date': shipment.planned_start_date,
2019-08-18 19:22:43 +02:00
'origin': lambda data: '%s,%s' % (
Move.__name__, data['id']),
2017-01-15 00:17:40 +01:00
})
to_write.append(moves)
to_write.append({
2019-08-18 19:22:43 +02:00
'from_location': shipment.transit_location.id,
2019-09-16 23:58:02 +02:00
'planned_date': shipment.planned_date,
2017-01-15 00:17:40 +01:00
})
if to_write:
Move.write(*to_write)
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('draft')
2012-09-24 22:18:17 +02:00
def draft(cls, shipments):
Move = Pool().get('stock.move')
2012-03-25 00:18:19 +01:00
# First reset state to draft to allow update from and to location
2015-10-21 16:48:02 +02:00
Move.draft([m for s in shipments for m in s.moves
if m.state != 'staging'])
Move.delete([m for s in shipments for m in s.moves
if m.from_location == s.transit_location])
2012-03-25 00:18:19 +01:00
for shipment in shipments:
2012-09-24 22:18:17 +02:00
Move.write([m for m in shipment.moves
if m.state != 'done'], {
2013-02-24 16:18:17 +01:00
'from_location': shipment.from_location.id,
'to_location': shipment.to_location.id,
'planned_date': shipment.planned_date,
})
2008-10-06 12:20:19 +02:00
@classmethod
@ModelView.button
@Workflow.transition('waiting')
def wait(cls, shipments, moves=None):
"""
If moves is set, only this subset is set to draft.
"""
Move = Pool().get('stock.move')
if moves is None:
moves = sum((s.moves for s in shipments), ())
else:
assert all(m.shipment in shipments for m in moves)
Move.draft(moves)
moves = []
2017-01-15 00:17:40 +01:00
for shipment in shipments:
if shipment.transit_location:
continue
for move in shipment.moves:
if move.state != 'done':
move.planned_date = shipment.planned_date
moves.append(move)
Move.save(moves)
2024-02-19 22:51:55 +01:00
cls.set_number(shipments)
2017-01-15 00:17:40 +01:00
cls._set_transit(shipments)
cls._sync_moves(shipments)
for shipment in shipments:
shipment.internal_transit_location = shipment.transit_location
shipment.state = 'waiting'
cls.save(shipments)
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@Workflow.transition('assigned')
2017-02-01 21:41:00 +01:00
@set_employee('assigned_by')
2012-09-24 22:18:17 +02:00
def assign(cls, shipments):
2020-07-25 08:19:31 +02:00
pool = Pool()
Move = pool.get('stock.move')
Move.assign([m for s in shipments for m in s.assign_moves])
2008-10-06 12:20:19 +02:00
2024-12-22 18:23:21 +01:00
@classmethod
@ModelView.button
@Workflow.transition('packed')
@set_employee('packed_by')
def pack(cls, shipments):
pool = Pool()
Move = pool.get('stock.move')
Move.delete([
m for s in shipments for m in s.outgoing_moves
if m.state == 'staging' or not m.quantity])
cls._sync_moves(shipments)
@classmethod
@ModelView.button
@Workflow.transition('shipped')
2017-02-01 21:41:00 +01:00
@set_employee('shipped_by')
def ship(cls, shipments):
pool = Pool()
Move = pool.get('stock.move')
Date = pool.get('ir.date')
Move.do([m for s in shipments for m in s.outgoing_moves])
cls._sync_moves(shipments)
for company, c_shipments in groupby(
shipments, key=lambda s: s.company):
with Transaction().set_context(company=company.id):
today = Date.today()
cls.write([s for s in c_shipments if not s.effective_start_date], {
'effective_start_date': today,
})
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('done')
2017-02-01 21:41:00 +01:00
@set_employee('done_by')
def do(cls, shipments):
2012-09-24 22:18:17 +02:00
pool = Pool()
Move = pool.get('stock.move')
Date = pool.get('ir.date')
incoming_moves = []
for shipment in shipments:
if shipment.transit_location:
shipment.check_quantity()
incoming_moves.extend(shipment.incoming_moves)
Move.do(incoming_moves)
for company, c_shipments in groupby(
shipments, key=lambda s: s.company):
with Transaction().set_context(company=company.id):
today = Date.today()
cls.write([s for s in c_shipments if not s.effective_date], {
'effective_date': today,
})
@property
def _check_quantity_source_moves(self):
return self.incoming_moves
@property
def _check_quantity_target_moves(self):
return self.outgoing_moves
2012-09-24 22:18:17 +02:00
@classmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
@Workflow.transition('cancelled')
2012-09-24 22:18:17 +02:00
def cancel(cls, shipments):
Move = Pool().get('stock.move')
2012-12-27 21:53:07 +01:00
Move.cancel([m for s in shipments for m in s.moves])
2008-10-06 12:20:19 +02:00
2012-09-24 22:18:17 +02:00
@classmethod
2012-09-10 15:28:38 +02:00
@ModelView.button_action('stock.wizard_shipment_internal_assign')
2012-09-24 22:18:17 +02:00
def assign_wizard(cls, shipments):
2012-09-10 15:28:38 +02:00
pass
2020-07-25 08:19:31 +02:00
@property
def assign_moves(self):
return self.outgoing_moves
@dualmethod
2012-03-25 00:18:19 +01:00
@ModelView.button
2012-09-24 22:18:17 +02:00
def assign_try(cls, shipments):
Move = Pool().get('stock.move')
2020-07-25 08:19:31 +02:00
to_assign = [
m for s in shipments for m in s.assign_moves
2017-06-24 10:34:21 +02:00
if m.assignation_required]
if Move.assign_try(to_assign):
2012-09-24 22:18:17 +02:00
cls.assign(shipments)
2012-03-25 00:18:19 +01:00
else:
to_assign = []
for shipment in shipments:
if any(
m.state in {'staging', 'draft'}
for m in shipment.assign_moves
if m.assignation_required):
continue
to_assign.append(shipment)
if to_assign:
cls.assign(to_assign)
2008-10-06 12:20:19 +02:00
@property
def _move_planned_date(self):
'''
Return the planned date for incoming moves and inventory_moves
'''
return self.planned_start_date, self.planned_date
@classmethod
def _set_move_planned_date(cls, shipments):
'''
Set planned date of moves for the shipments
'''
Move = Pool().get('stock.move')
to_write = []
for shipment in shipments:
dates = shipment._move_planned_date
if (shipment.transit_location
and shipment.state not in {'request', 'draft'}):
outgoing_date, incoming_date = dates
outgoing_moves = [m for m in shipment.outgoing_moves
if (m.state not in {'done', 'cancelled'}
and m.planned_date != outgoing_date)]
if outgoing_moves:
to_write.append(outgoing_moves)
to_write.append({
'planned_date': outgoing_date,
})
incoming_moves = [m for m in shipment.incoming_moves
if (m.state not in {'done', 'cancelled'}
and m.planned_date != incoming_date)]
if incoming_moves:
to_write.append(incoming_moves)
to_write.append({
'planned_date': incoming_date,
})
else:
planned_start_date = shipment.planned_start_date
moves = [m for m in shipment.moves
if (m.state not in {'done', 'cancelled'}
and m.planned_date != planned_start_date)]
if moves:
to_write.append(moves)
to_write.append({
'planned_date': planned_start_date,
})
if to_write:
Move.write(*to_write)
2020-10-12 19:20:33 +02:00
@classmethod
def _get_reschedule_domain(cls, date):
return [
('state', 'in', ['waiting', 'assigned']),
2020-10-12 19:20:33 +02:00
('planned_date', '<', date),
]
@classmethod
def reschedule(cls, date=None):
pool = Pool()
Date = pool.get('ir.date')
if date is None:
date = Date.today()
shipments = cls.search(cls._get_reschedule_domain(date))
for shipment in shipments:
shipment.planned_date = date
shipment.planned_start_date = (
shipment.on_change_with_planned_start_date())
cls.save(shipments)
2008-10-22 20:13:23 +02:00
2020-07-25 08:19:31 +02:00
class Assign(Wizard):
__name__ = 'stock.shipment.assign'
2012-01-02 18:07:56 +01:00
start = StateTransition()
2020-07-25 08:19:31 +02:00
partial = StateView(
'stock.shipment.assign.partial',
'stock.shipment_assign_partial_view_form', [
Button("Cancel", 'cancel', 'tryton-cancel'),
Button("Wait", 'end', 'tryton-ok', True),
Button("Ignore", 'ignore', 'tryton-forward'),
Button("Force", 'force', 'tryton-forward',
2012-01-02 18:07:56 +01:00
states={
'invisible': ~Id('stock',
'group_stock_force_assignment').in_(
Eval('context', {}).get('groups', [])),
}),
])
2020-07-25 08:19:31 +02:00
cancel = StateTransition()
2012-01-02 18:07:56 +01:00
force = StateTransition()
2020-07-25 08:19:31 +02:00
ignore = StateTransition()
2012-01-02 18:07:56 +01:00
2012-09-24 22:18:17 +02:00
def transition_start(self):
self.record.assign_try()
if self.record.state == 'assigned':
2009-01-27 15:15:01 +01:00
return 'end'
else:
2020-07-25 08:19:31 +02:00
return 'partial'
def default_partial(self, fields):
values = {}
if 'moves' in fields:
values['moves'] = [
m.id for m in self.record.assign_moves
if m.state in {'staging', 'draft'}]
return values
def transition_cancel(self):
self.record.assign_reset()
return 'end'
2009-01-27 15:15:01 +01:00
2012-09-24 22:18:17 +02:00
def transition_force(self):
2020-07-25 08:19:31 +02:00
self.record.assign_force()
2012-01-02 18:07:56 +01:00
return 'end'
2009-01-27 15:15:01 +01:00
2020-07-25 08:19:31 +02:00
def transition_ignore(self):
self.record.assign_ignore(self.partial.moves)
2020-07-25 08:19:31 +02:00
return 'end'
class AssignPartial(ModelView):
__name__ = 'stock.shipment.assign.partial'
moves = fields.Many2Many(
'stock.move', None, None, "Moves", readonly=True,
help="The moves that were not assigned.")
2009-01-27 15:15:01 +01:00
2015-01-20 18:49:56 +01:00
class ShipmentReport(CompanyReport):
@classmethod
def moves(cls, shipment):
raise NotImplementedError
2015-01-20 18:49:56 +01:00
@classmethod
2023-02-22 16:58:23 +01:00
def moves_order(cls, shipment):
return []
2015-01-20 18:49:56 +01:00
@classmethod
2023-02-22 16:58:23 +01:00
def get_context(cls, shipments, header, data):
report_context = super().get_context(shipments, header, data)
report_context['moves'] = cls.moves
return report_context
2015-01-20 18:49:56 +01:00
2024-04-18 14:49:14 +02:00
class DeliveryNote(ShipmentReport):
__name__ = 'stock.shipment.out.delivery_note'
@classmethod
def execute(cls, ids, data):
2026-02-27 12:55:11 +01:00
with Transaction().set_context(address_with_party=True):
2025-04-04 00:45:18 +02:00
return super().execute(ids, data)
2024-04-18 14:49:14 +02:00
@classmethod
def moves(cls, shipment):
moves = [m for m in shipment.outgoing_moves if m.state != 'cancelled']
2024-04-18 14:49:14 +02:00
return sort(moves, cls.moves_order(shipment))
@classmethod
def moves_order(cls, shipment):
return shipment.__class__.outgoing_moves.order
2015-01-20 18:49:56 +01:00
class PickingList(ShipmentReport):
2012-09-24 22:18:17 +02:00
__name__ = 'stock.shipment.out.picking_list'
2009-03-26 19:01:42 +01:00
@classmethod
def moves(cls, shipment):
if shipment.warehouse_storage == shipment.warehouse_output:
2023-02-22 16:58:23 +01:00
moves = shipment.outgoing_moves
else:
2023-02-22 16:58:23 +01:00
moves = shipment.inventory_moves
moves = [m for m in moves if m.state != 'cancelled']
2023-02-22 16:58:23 +01:00
return sort(moves, cls.moves_order(shipment))
@classmethod
def moves_order(cls, shipment):
return shipment.__class__.inventory_moves.order
2009-03-27 16:08:07 +01:00
2015-01-20 18:49:56 +01:00
class SupplierRestockingList(ShipmentReport):
2012-09-24 22:18:17 +02:00
__name__ = 'stock.shipment.in.restocking_list'
2009-03-27 17:11:46 +01:00
@classmethod
def moves(cls, shipment):
if shipment.warehouse_input == shipment.warehouse_storage:
2023-02-22 16:58:23 +01:00
moves = shipment.incoming_moves
else:
2023-02-22 16:58:23 +01:00
moves = shipment.inventory_moves
moves = [m for m in moves if m.state != 'cancelled']
2023-02-22 16:58:23 +01:00
return sort(moves, cls.moves_order(shipment))
@classmethod
def moves_order(cls, shipment):
return shipment.__class__.inventory_moves.order
2009-03-27 17:11:46 +01:00
2015-01-20 18:49:56 +01:00
class CustomerReturnRestockingList(ShipmentReport):
2012-09-24 22:18:17 +02:00
__name__ = 'stock.shipment.out.return.restocking_list'
2009-03-27 17:11:46 +01:00
@classmethod
def moves(cls, shipment):
if shipment.warehouse_input == shipment.warehouse_storage:
2023-02-22 16:58:23 +01:00
moves = shipment.incoming_moves
else:
2023-02-22 16:58:23 +01:00
moves = shipment.inventory_moves
moves = [m for m in moves if m.state != 'cancelled']
2023-02-22 16:58:23 +01:00
return sort(moves, cls.moves_order(shipment))
@classmethod
def moves_order(cls, shipment):
return shipment.__class__.inventory_moves.order
2009-03-27 17:11:46 +01:00
2015-01-20 18:49:56 +01:00
class InteralShipmentReport(ShipmentReport):
2012-09-24 22:18:17 +02:00
__name__ = 'stock.shipment.internal.report'
2024-04-18 14:49:14 +02:00
@classmethod
def execute(cls, ids, data):
with Transaction().set_context(address_with_party=True):
return super().execute(ids, data)
2024-04-18 14:49:14 +02:00
@classmethod
def moves(cls, shipment):
if shipment.transit_location:
if shipment.state == 'shipped':
2023-02-22 16:58:23 +01:00
moves = shipment.incoming_moves
else:
2023-02-22 16:58:23 +01:00
moves = shipment.outgoing_moves
else:
2023-02-22 16:58:23 +01:00
moves = shipment.moves
moves = [m for m in moves if m.state != 'cancelled']
2023-02-22 16:58:23 +01:00
return sort(moves, cls.moves_order(shipment))
@classmethod
def moves_order(cls, shipment):
if shipment.state == 'shipped':
return shipment.__class__.incoming_moves.order
else:
return shipment.__class__.outgoing_moves.order