trytonpsk-stock_co/shipment.py

696 lines
24 KiB
Python
Raw Normal View History

2021-08-26 20:16:55 +02:00
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from datetime import date, timedelta
2021-08-27 21:42:38 +02:00
from decimal import Decimal
2021-09-18 18:12:48 +02:00
from operator import itemgetter
2023-08-26 15:35:24 +02:00
from sql import Table, Null
2021-09-25 07:02:37 +02:00
2021-09-20 21:40:07 +02:00
from trytond.modules.company import CompanyReport
2021-08-26 20:16:55 +02:00
from trytond.pool import Pool, PoolMeta
from trytond.wizard import (
Wizard, StateTransition, StateView,
Button, StateReport)
2021-08-26 20:16:55 +02:00
from trytond.transaction import Transaction
from trytond.model import ModelView, fields
2021-08-27 21:42:38 +02:00
from trytond.report import Report
from trytond.pyson import Eval
2021-08-26 20:16:55 +02:00
2021-09-18 18:12:48 +02:00
type_shipment = {
'out': 'Envio a Clientes',
'in': 'Envio de Proveedor',
'internal': 'Envio Interno',
}
2021-08-26 20:16:55 +02:00
2021-09-01 08:34:02 +02:00
class ShipmentIn(metaclass=PoolMeta):
'Shipment In'
__name__ = 'stock.shipment.in'
@classmethod
def __setup__(cls):
super(ShipmentIn, cls).__setup__()
cls.reference.states = {
'required': Eval('state').in_(['received']),
}
2023-10-13 18:47:32 +02:00
class ShipmentOut(metaclass=PoolMeta):
'Shipment Out'
__name__ = 'stock.shipment.out'
def get_sale_reference(self, name=None):
2023-10-20 17:11:24 +02:00
for move in self.moves:
if move.origin and str(move.origin).split(',')[0] == 'sale.line':
Sale = Pool().get('sale.sale')
sale = Sale(move.origin.sale.id)
return sale
return None
2023-10-13 18:47:32 +02:00
2021-08-26 20:16:55 +02:00
class InternalShipment(metaclass=PoolMeta):
'Internal Shipment'
__name__ = 'stock.shipment.internal'
2021-08-27 21:42:38 +02:00
@classmethod
def __setup__(cls):
super(InternalShipment, cls).__setup__()
2021-08-28 18:27:55 +02:00
cls.from_location.domain = [
2021-09-01 08:34:02 +02:00
('name', 'ilike', 'ZA%'),
2021-08-28 18:27:55 +02:00
('active', '=', True)
2021-09-01 08:34:02 +02:00
]
2021-08-28 18:27:55 +02:00
cls.to_location.domain = [
2021-09-01 08:34:02 +02:00
('name', 'ilike', 'ZA%'),
2021-08-28 18:27:55 +02:00
('active', '=', True)
2021-09-01 08:34:02 +02:00
]
2021-08-28 18:27:55 +02:00
2021-08-26 20:16:55 +02:00
@classmethod
def wait(cls, shipments):
super(InternalShipment, cls).wait(shipments)
cls.set_employee(shipments, 'shipped_by')
2021-09-25 07:02:37 +02:00
@classmethod
@ModelView.button_action('stock.wizard_shipment_internal_assign')
def assign_wizard(cls, shipments):
super(InternalShipment, cls).assign_wizard(shipments)
for sh in shipments:
cls.assign_lots(sh)
2021-08-26 20:16:55 +02:00
@classmethod
def done(cls, shipments):
super(InternalShipment, cls).done(shipments)
cls.set_employee(shipments, 'done_by')
@classmethod
def set_employee(cls, shipments, field):
employee_id = Transaction().context.get('employee')
if employee_id:
cls.write(shipments, {field: employee_id})
2021-09-25 07:02:37 +02:00
@classmethod
def assign_lots(cls, shipment):
"""
Look for the oldest lot or the one closest to the expiration
date and try assign it to shipment moves
"""
Move = Pool().get('stock.move')
Lot = Pool().get('stock.lot')
def _get_lots(product_id):
locations_ids = {'locations': [shipment.from_location.id]}
2022-02-21 18:27:26 +01:00
order = []
fields = Lot.fields_get(fields_names=['expiration_date'])
if 'expiration_date' in fields.keys():
order.append(('expiration_date', 'ASC NULLS LAST'))
order.append(('create_date', 'ASC'))
2021-09-25 07:02:37 +02:00
with Transaction().set_context(locations_ids):
dom = [
('product', '=', product_id),
('active', '=', True),
('quantity', '>', 0),
]
2022-02-21 18:27:26 +01:00
lots = Lot.search(dom, order=order)
2021-09-25 07:02:37 +02:00
return lots
for move in shipment.moves:
if move.product.lot_is_required:
lots_list = _get_lots(move.product.id)
balance_qty = move.quantity
for lt in lots_list:
if lt.quantity >= balance_qty:
move.lot = lt.id
move.save()
break
else:
balance_qty = balance_qty - lt.quantity
move.quantity = lt.quantity
move.lot = lt.id
move.save()
if balance_qty:
move, = Move.create([{
'quantity': balance_qty,
'internal_quantity': balance_qty,
'product': move.product.id,
'lot': lt.id,
'from_location': move.from_location.id,
'to_location': move.to_location.id,
'shipment': str(shipment),
'uom': move.uom.id,
'company': move.company.id,
'state': move.state,
}])
2022-08-20 16:15:41 +02:00
2022-06-14 16:48:05 +02:00
@fields.depends('effective_date', 'from_location', 'to_location')
def on_change_with_effective_start_date(self):
if self.effective_date:
return self.effective_date
2022-08-20 16:15:41 +02:00
2022-06-14 16:48:05 +02:00
@classmethod
def create(cls, vlist):
shipments = super().create(vlist)
cls._set_move_effective_date(shipments)
return shipments
2022-08-20 16:15:41 +02:00
2022-06-14 16:48:05 +02:00
@classmethod
def write(cls, *args):
super().write(*args)
cls._set_move_effective_date(sum(args[::2], []))
2022-08-20 16:15:41 +02:00
2022-06-14 16:48:05 +02:00
@property
def _move_effective_date(self):
'''
Return the effective date for incoming moves and inventory_moves
'''
2022-08-20 16:15:41 +02:00
return self.effective_start_date, self.effective_date
2021-09-25 07:02:37 +02:00
2022-06-14 16:48:05 +02:00
@classmethod
def _set_move_effective_date(cls, shipments):
'''
Set effective date of moves for the shipments
'''
Move = Pool().get('stock.move')
to_write = []
for shipment in shipments:
dates = shipment._move_effective_date
outgoing_date, incoming_date = dates
outgoing_moves = [m for m in shipment.outgoing_moves
if (m.state not in ('assigned', 'done', 'cancelled')
and m.effective_date != outgoing_date)]
if outgoing_moves:
to_write.append(outgoing_moves)
to_write.append({
2023-08-26 15:35:24 +02:00
'effective_date': outgoing_date,
})
2022-06-14 16:48:05 +02:00
if shipment.transit_location:
incoming_moves = [m for m in shipment.incoming_moves
if (m.state not in ('assigned', 'done', 'cancelled')
and m.effective_date != incoming_date)]
if incoming_moves:
to_write.append(incoming_moves)
to_write.append({
2023-08-26 15:35:24 +02:00
'effective_date': incoming_date,
})
2022-06-14 16:48:05 +02:00
if to_write:
Move.write(*to_write)
2022-08-20 16:15:41 +02:00
2022-01-18 06:12:42 +01:00
def load_current_stock(self):
pool = Pool()
Internal = pool.get('stock.shipment.internal')
Product = pool.get('product.product')
moves_target = []
ctx = {
'locations': [self.from_location.id],
'stock_date_end': self.planned_date,
}
with Transaction().set_context(ctx):
products = Product.search([
('quantity', '>', 0)
])
for product in products:
if product.quantity <= 0:
continue
moves_target.append({
'product': product.id,
'uom': product.default_uom.id,
'quantity': product.quantity,
'internal_quantity': product.quantity,
'from_location': self.from_location.id,
'to_location': self.to_location.id,
'planned_date': self.planned_date or self.effective_date,
'effective_date': self.planned_date or self.effective_date,
'state': 'draft',
})
Internal.write([self], {
'moves': [('create', moves_target)],
})
return 'end'
2022-08-20 16:15:41 +02:00
2021-08-26 20:16:55 +02:00
class CreateInternalShipmentStart(ModelView):
'Create Internal Shipment Start'
__name__ = 'stock_co.create_internal_shipment.start'
from_location = fields.Many2One('stock.location', "From Location",
2022-04-27 15:23:44 +02:00
required=True, domain=[
('type', 'in', [
'view', 'storage', 'lost_found']),
2023-08-26 15:35:24 +02:00
])
2021-08-26 20:16:55 +02:00
to_location = fields.Many2One('stock.location', "To Location",
2022-04-27 15:23:44 +02:00
required=True, domain=[
('type', 'in', [
'view', 'storage', 'lost_found']),
])
2021-08-26 20:16:55 +02:00
class CreateInternalShipment(Wizard):
'Create Internal Shipment'
__name__ = 'stock_co.create_internal_shipment'
start = StateView('stock_co.create_internal_shipment.start',
2022-04-27 15:23:44 +02:00
'stock_co.create_internal_shipment_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Ok', 'accept', 'tryton-ok', default=True),
])
2021-08-26 20:16:55 +02:00
accept = StateTransition()
def transition_accept(self):
pool = Pool()
Internal = pool.get('stock.shipment.internal')
ShipmentSupplier = pool.get('stock.shipment.in')
shipment_id = Transaction().context.get('active_id')
shipment_supplier = ShipmentSupplier(shipment_id)
moves_target = []
today = date.today()
for move in shipment_supplier.incoming_moves:
moves_target.append({
'product': move.product.id,
'uom': move.product.default_uom.id,
'quantity': move.quantity,
'internal_quantity': move.quantity,
'from_location': self.start.from_location.id,
'to_location': self.start.to_location.id,
'planned_date': today,
'effective_date': today,
'state': 'draft',
})
data = {
'company': shipment_supplier.company.id,
'effective_date': today,
'planned_date': today,
'effective_start_date': today,
'planned_start_date': today,
'from_location': self.start.from_location.id,
'to_location': self.start.to_location.id,
'state': 'draft',
'moves': [('create', moves_target)],
}
Internal.create([data])
return 'end'
2021-09-20 21:40:07 +02:00
class ShipmentInReturnReport(CompanyReport):
'Shipment In Return Report'
__name__ = 'stock.shipment.in.return.report'
2021-08-26 20:16:55 +02:00
class ShipmentOutForceDraft(Wizard):
'Shipment Out Force Draft'
__name__ = 'stock.shipment.out.force_draft'
start_state = 'force_draft'
force_draft = StateTransition()
def transition_force_draft(self):
id_ = Transaction().context['active_id']
Shipment = Pool().get('stock.shipment.out')
stock_move = Table('stock_move')
if id_:
shipment = Shipment(id_)
cursor = Transaction().connection.cursor()
for rec in shipment.inventory_moves:
cursor.execute(*stock_move.delete(
where=stock_move.id == rec.id)
)
for rec in shipment.outgoing_moves:
cursor.execute(*stock_move.update(
2023-08-26 15:35:24 +02:00
columns=[stock_move.state, stock_move.effective_date],
values=['draft', Null],
2021-08-26 20:16:55 +02:00
where=stock_move.id == rec.id)
)
shipment.state = 'draft'
shipment.save()
return 'end'
class ShipmentInternalForceDraft(Wizard):
'Shipment Internal Force Draft'
__name__ = 'stock.shipment.internal.force_draft'
start_state = 'force_draft'
force_draft = StateTransition()
def transition_force_draft(self):
id_ = Transaction().context['active_id']
Shipment = Pool().get('stock.shipment.internal')
stock_move = Table('stock_move')
if id_:
shipment = Shipment(id_)
if shipment.effective_date > date.today() - timedelta(days=30):
cursor = Transaction().connection.cursor()
for rec in shipment.moves:
cursor.execute(*stock_move.update(
columns=[stock_move.state],
values=['draft'],
where=stock_move.id == rec.id)
)
shipment.state = 'draft'
shipment.save()
2021-08-26 20:16:55 +02:00
return 'end'
2021-08-27 21:42:38 +02:00
2021-12-09 20:10:33 +01:00
class ShipmentInForceDraft(Wizard):
'Shipment In Force Draft'
__name__ = 'stock.shipment.in.force_draft'
start_state = 'force_draft'
force_draft = StateTransition()
def transition_force_draft(self):
shipment = Table('stock_shipment_in')
move = Table('stock_move')
id_shipment = Transaction().context['active_id']
cursor = Transaction().connection.cursor()
if not id_shipment:
return 'end'
cursor.execute(*shipment.update(
columns=[shipment.state],
values=['draft'],
where=shipment.id == id_shipment)
)
pool = Pool()
ShipmentIn = pool.get('stock.shipment.in')
shipmentin = ShipmentIn(id_shipment)
moves_ids = [m.id for m in shipmentin.inventory_moves]
2022-01-13 23:37:19 +01:00
if moves_ids:
cursor.execute(*move.delete(
where=move.id.in_(moves_ids))
)
2021-12-09 20:10:33 +01:00
moves_ids = [m.id for m in shipmentin.incoming_moves]
2022-01-13 23:37:19 +01:00
if moves_ids:
cursor.execute(*move.update(
columns=[move.state],
values=['draft'],
where=move.id.in_(moves_ids))
)
2021-12-09 20:10:33 +01:00
return 'end'
2022-01-19 17:10:01 +01:00
class ShipmentInReturnForceDraft(Wizard):
'Shipment In Return Force Draft'
__name__ = 'stock.shipment.in.return.force_draft'
start_state = 'force_draft'
force_draft = StateTransition()
def transition_force_draft(self):
shipment = Table('stock_shipment_in_return')
move = Table('stock_move')
id_shipment = Transaction().context['active_id']
cursor = Transaction().connection.cursor()
if not id_shipment:
return 'end'
cursor.execute(*shipment.update(
columns=[shipment.state],
values=['draft'],
where=shipment.id == id_shipment)
)
pool = Pool()
ShipmentIn = pool.get('stock.shipment.in.return')
shipment_in = ShipmentIn(id_shipment)
moves_ids = [m.id for m in shipment_in.moves]
if moves_ids:
cursor.execute(*move.update(
columns=[move.state],
values=['draft'],
where=move.id.in_(moves_ids))
)
return 'end'
2021-08-27 21:42:38 +02:00
class ShipmentDetailedStart(ModelView):
'Shipment Detailed Start'
__name__ = 'stock.shipment.shipment_detailed.start'
company = fields.Many2One('company.company', 'Company', required=True)
start_date = fields.Date('Start Date', required=True)
end_date = fields.Date('End Date', required=True)
type_shipment = fields.Selection([
('in', 'Supplier'),
('out', 'Customer'),
('internal', 'Internal'),
], 'Type Shipment', required=True)
2021-10-25 22:26:35 +02:00
grouped = fields.Boolean('Grouped')
2023-08-26 15:35:24 +02:00
from_locations = fields.Many2Many('stock.location', None, None,
'From Location', domain=[
('name', 'ilike', 'ZA%'),
('active', '=', True)
])
to_locations = fields.Many2Many('stock.location', None, None, 'To Location',
domain=[
('name', 'ilike', 'ZA%'),
('active', '=', True)
])
2021-08-27 21:42:38 +02:00
@staticmethod
def default_company():
return Transaction().context.get('company')
class ShipmentDetailed(Wizard):
'Shipment Detailed'
__name__ = 'stock.shipment.shipment_detailed'
start = StateView('stock.shipment.shipment_detailed.start',
2023-08-26 15:35:24 +02:00
'stock_co.print_shipment_detailed_start_view_form', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Print', 'print_', 'tryton-ok', default=True),
])
2021-08-27 21:42:38 +02:00
print_ = StateReport('stock.shipment.shipment_detailed.report')
def do_print_(self, action):
from_locations = None
to_locations = None
if self.start.from_locations:
from_locations = [n.id for n in self.start.from_locations]
if self.start.to_locations:
to_locations = [n.id for n in self.start.to_locations]
2022-04-27 15:23:44 +02:00
2021-08-27 21:42:38 +02:00
data = {
'company': self.start.company.id,
'start_date': self.start.start_date,
'end_date': self.start.end_date,
'type_shipment': self.start.type_shipment,
2021-10-25 22:26:35 +02:00
'grouped': self.start.grouped,
'from_locations': from_locations,
'to_locations': to_locations
2021-08-27 21:42:38 +02:00
}
return action, data
def transition_print_(self):
return 'end'
class ShipmentDetailedReport(Report):
'Shipment Detailed Report'
__name__ = 'stock.shipment.shipment_detailed.report'
@classmethod
def get_context(cls, records, header, data):
report_context = super().get_context(records, header, data)
pool = Pool()
2021-09-18 18:12:48 +02:00
company = Transaction().context.get('company.rec_name')
2021-12-11 16:53:36 +01:00
type_shipment_ = data['type_shipment']
model = 'stock.shipment.' + type_shipment_
2021-08-27 21:42:38 +02:00
ModelShipment = pool.get(model)
2021-09-18 18:12:48 +02:00
Move = pool.get('stock.move')
Product = pool.get('product.product')
2021-08-27 21:42:38 +02:00
dom_shipment = [
('company', '=', data['company']),
('effective_date', '>=', data['start_date']),
('effective_date', '<=', data['end_date']),
('state', 'not in', ['cancelled', 'draft'])
2021-08-27 21:42:38 +02:00
]
if data['from_locations']:
2023-08-26 15:35:24 +02:00
dom_shipment.append(('from_location', 'in', data['from_locations']))
if data['to_locations']:
2023-08-26 15:35:24 +02:00
dom_shipment.append(('to_location', 'in', data['to_locations']))
2021-10-21 21:33:43 +02:00
fields_names = ['id']
2023-08-26 15:35:24 +02:00
shipments = ModelShipment.search_read(
dom_shipment,
fields_names=fields_names,
order=[('effective_date', 'ASC')]
)
shipments_id = [model + ',' + str(sh['id']) for sh in shipments]
2021-09-18 18:12:48 +02:00
fields_names = [
'product.account_category.name', 'product.name', 'product.code',
'product.cost_price', 'quantity', 'to_location.name',
'from_location.name', 'shipment.reference',
2022-05-20 22:17:49 +02:00
'effective_date', 'shipment.number'
2021-09-18 18:12:48 +02:00
]
fields = ModelShipment.fields_get(fields_names=[
'operation_center', 'customer', 'supplier',
'incoming_moves', 'analytic_account'])
if 'operation_center' in fields.keys():
fields_names.append('shipment.operation_center.rec_name')
if 'analytic_account' in fields.keys():
fields_names.append('shipment.analytic_account.rec_name')
2021-12-11 16:53:36 +01:00
if type_shipment_ == 'in':
fields_names.append('shipment.supplier.name')
elif type_shipment_ == 'out':
fields_names.append('shipment.customer.name')
2021-09-18 18:12:48 +02:00
moves = Move.search_read(
('shipment', 'in', shipments_id),
2021-09-21 22:59:05 +02:00
fields_names=fields_names,
order=[('to_location', 'DESC'), ('create_date', 'ASC')]
2021-09-18 18:12:48 +02:00
)
dgetter = itemgetter('product.', 'quantity')
product_browse = Product.browse
for m in moves:
product, quantity = dgetter(m)
product_, = product_browse([product['id']])
try:
oc = m['shipment.']['operation_center.']['rec_name']
2023-08-26 15:35:24 +02:00
except Exception:
2021-09-18 18:12:48 +02:00
oc = ''
try:
analytic = m['shipment.']['analytic_account.']['rec_name']
2023-08-26 15:35:24 +02:00
except Exception:
analytic = ''
2021-12-11 16:53:36 +01:00
if type_shipment_ == 'in':
party = m['shipment.']['supplier.']['name']
elif type_shipment_ == 'out':
2022-01-18 16:26:52 +01:00
party = m['shipment.']['customer.']['name']
2021-12-11 16:53:36 +01:00
else:
party = ''
2021-09-18 18:12:48 +02:00
cost_price = product['cost_price']
category = product.get('account_category.', '')
if category:
category = category['name']
2021-11-05 22:34:49 +01:00
category_ad = ''
if product_.categories:
category_ad = product_.categories[0].name
2021-09-18 18:12:48 +02:00
value = {
2021-12-11 16:53:36 +01:00
'party': party,
'oc': oc,
'analytic': analytic,
2021-09-18 18:12:48 +02:00
'product': product['name'],
2022-08-20 16:15:41 +02:00
'code': product['code'],
2021-09-18 18:12:48 +02:00
'cost_price': cost_price,
'category': category,
2021-11-05 22:35:42 +01:00
'category_ad': category_ad,
2021-12-09 20:10:33 +01:00
'cost_base': Decimal(str(round(float(cost_price) * quantity, 2))),
2021-10-20 23:20:27 +02:00
}
2021-10-22 21:26:20 +02:00
try:
2021-12-11 16:53:36 +01:00
value['cost_unit_w_tax'] = float(product_.cost_price_taxed)
2021-11-05 22:34:49 +01:00
value['cost_w_tax'] = float(
product_.cost_price_taxed) * quantity
2021-10-22 21:26:20 +02:00
value['last_cost'] = product_.last_cost
2023-08-26 15:35:24 +02:00
except Exception:
2021-10-25 22:26:35 +02:00
value['cost_w_tax'] = 0
2021-12-11 16:53:36 +01:00
value['cost_unit_w_tax'] = 0
2021-10-25 22:26:35 +02:00
value['last_cost'] = 0
2021-10-22 21:26:20 +02:00
try:
m.update(product_.attributes)
2023-08-26 15:35:24 +02:00
except Exception:
2021-10-22 21:26:20 +02:00
pass
2021-12-01 16:55:25 +01:00
try:
value['price_w_tax'] = float(
product_.sale_price_w_tax) * quantity
2023-08-26 15:35:24 +02:00
except Exception:
2021-12-01 16:55:25 +01:00
value['price_w_tax'] = 0
2021-10-22 21:26:20 +02:00
try:
2021-10-20 23:20:27 +02:00
value['section'] = product_.section.name
value['conservation'] = product_.conservation.name
2023-08-26 15:35:24 +02:00
except Exception:
2021-10-20 23:16:08 +02:00
value['conservation'] = None
2021-10-22 21:26:20 +02:00
value['section'] = None
2021-08-30 22:39:59 +02:00
2021-10-22 21:26:20 +02:00
m.update(value)
2021-10-25 22:26:35 +02:00
if not data['grouped']:
report_context['records'] = moves
else:
records = {}
for m in moves:
2023-08-26 15:35:24 +02:00
location_id = str(m['to_location.']['id'])
product_id = str(m['product.']['id'])
key = location_id + '_' + product_id
2021-10-25 22:26:35 +02:00
try:
records[key]['cost_w_tax'] += m['cost_w_tax']
2021-12-01 16:55:25 +01:00
records[key]['price_w_tax'] += m['price_w_tax']
2021-10-25 22:26:35 +02:00
records[key]['quantity'] += m['quantity']
records[key]['cost_base'] += m['cost_base']
records[key]['last_cost'] = m['last_cost']
2023-08-26 15:35:24 +02:00
except Exception:
2021-10-25 22:26:35 +02:00
records[key] = m
records[key]['shipment.']['reference'] = ''
records[key]['effective_date'] = ''
2021-10-25 22:26:35 +02:00
report_context['records'] = records.values()
2021-10-21 21:33:43 +02:00
2021-09-18 18:12:48 +02:00
report_context['company'] = company
2021-08-27 21:42:38 +02:00
report_context['Decimal'] = Decimal
2021-09-18 18:12:48 +02:00
report_context['kind'] = type_shipment[data['type_shipment']]
2021-08-27 21:42:38 +02:00
return report_context
2021-09-15 16:32:11 +02:00
class Assign(metaclass=PoolMeta):
"Assign Shipment"
__name__ = 'stock.shipment.assign'
split = StateTransition()
@classmethod
def __setup__(cls):
super(Assign, cls).__setup__()
cls.partial.buttons.append(Button('Split', 'split', 'tryton-ok'))
def transition_split(self):
self.split_shipment()
return 'end'
def split_shipment(self):
pool = Pool()
Move = pool.get('stock.move')
Shipment = pool.get(Transaction().context.get('active_model'))
2022-09-23 19:13:10 +02:00
shipment_ = Shipment(Transaction().context.get('active_id'))
number_reference = shipment_.number
2021-09-15 16:32:11 +02:00
if Shipment.__name__ == 'stock.shipment.out':
2022-09-23 19:13:10 +02:00
Move.draft(shipment_.inventory_moves)
shipment, = Shipment.copy([shipment_], default={'moves': None})
Shipment.write([shipment], {'reference': number_reference})
move_to_write = []
for m in self.partial.moves:
move, = Move.copy([m.origin], default={'quantity': m.quantity})
Move.write([m], {'origin': str(m)})
move_to_write.append(move)
Move.write(list(self.partial.moves) + move_to_write, {'shipment': str(shipment)})
inventory_ids = []
for m in shipment_.inventory_moves:
Move.write([m.origin], {'quantity': m.quantity})
inventory_ids.append(m.origin.id)
for m in shipment_.outgoing_moves:
if m.id not in inventory_ids:
Move.delete([m])
else:
shipment, = Shipment.copy([shipment_], default={'moves': None})
Shipment.write([shipment], {'reference': number_reference})
Move.write(list(self.partial.moves), {'shipment': str(shipment)})
2021-09-15 16:32:11 +02:00
return 'end'
2022-01-18 06:12:42 +01:00
class ShipmentInternalLoadStock(Wizard):
'Shipment Internal Load Stock'
__name__ = 'stock_co.shipment_internal_load_stock'
start_state = 'create_moves'
create_moves = StateTransition()
def transition_create_moves(self):
Internal = Pool().get('stock.shipment.internal')
active_id = Transaction().context['active_id']
internal = Internal(active_id)
if not internal.moves:
internal.load_current_stock()
return 'end'