mirror of
https://github.com/bringout/oca-ocb-mrp.git
synced 2026-04-26 06:12:07 +02:00
19.0 vanilla
This commit is contained in:
parent
accf5918df
commit
6e65e8c877
688 changed files with 225434 additions and 199401 deletions
|
|
@ -1,36 +1,38 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, fields, models, _
|
||||
from odoo.exceptions import UserError, ValidationError
|
||||
from odoo.tools import float_compare, float_round
|
||||
from odoo.osv import expression
|
||||
|
||||
from collections import defaultdict
|
||||
|
||||
from odoo import _, api, fields, models
|
||||
from odoo.exceptions import UserError
|
||||
from odoo.tools import float_compare, float_round
|
||||
from odoo.tools.misc import clean_context
|
||||
|
||||
|
||||
class MrpUnbuild(models.Model):
|
||||
_name = "mrp.unbuild"
|
||||
_name = 'mrp.unbuild'
|
||||
_description = "Unbuild Order"
|
||||
_inherit = ['mail.thread', 'mail.activity.mixin']
|
||||
_order = 'id desc'
|
||||
|
||||
name = fields.Char('Reference', copy=False, readonly=True, default=lambda x: _('New'))
|
||||
name = fields.Char('Reference', copy=False, readonly=True, default=lambda s: s.env._('New'))
|
||||
product_id = fields.Many2one(
|
||||
'product.product', 'Product', check_company=True,
|
||||
domain="[('type', 'in', ['product', 'consu']), '|', ('company_id', '=', False), ('company_id', '=', company_id)]",
|
||||
required=True, states={'done': [('readonly', True)]})
|
||||
domain="[('type', '=', 'consu')]",
|
||||
compute='_compute_product_id', store=True, precompute=True, readonly=False,
|
||||
required=True)
|
||||
company_id = fields.Many2one(
|
||||
'res.company', 'Company',
|
||||
default=lambda s: s.env.company,
|
||||
required=True, index=True, states={'done': [('readonly', True)]})
|
||||
required=True, index=True)
|
||||
product_qty = fields.Float(
|
||||
'Quantity', default=1.0,
|
||||
required=True, states={'done': [('readonly', True)]})
|
||||
digits='Product Unit',
|
||||
compute='_compute_product_qty', store=True, precompute=True, readonly=False,
|
||||
required=True)
|
||||
product_uom_id = fields.Many2one(
|
||||
'uom.uom', 'Unit of Measure',
|
||||
'uom.uom', 'Unit',
|
||||
compute='_compute_product_uom_id', store=True, readonly=False, precompute=True,
|
||||
required=True, states={'done': [('readonly', True)]})
|
||||
required=True)
|
||||
bom_id = fields.Many2one(
|
||||
'mrp.bom', 'Bill of Material',
|
||||
domain="""[
|
||||
|
|
@ -43,30 +45,31 @@ class MrpUnbuild(models.Model):
|
|||
'|',
|
||||
('company_id', '=', company_id),
|
||||
('company_id', '=', False)
|
||||
]
|
||||
""",
|
||||
states={'done': [('readonly', True)]}, check_company=True)
|
||||
]""",
|
||||
compute='_compute_bom_id', store=True,
|
||||
check_company=True)
|
||||
mo_id = fields.Many2one(
|
||||
'mrp.production', 'Manufacturing Order',
|
||||
domain="[('state', '=', 'done'), ('company_id', '=', company_id), ('product_id', '=?', product_id), ('bom_id', '=?', bom_id)]",
|
||||
states={'done': [('readonly', True)]}, check_company=True)
|
||||
domain="[('state', '=', 'done'), ('product_id', '=?', product_id), ('bom_id', '=?', bom_id)]",
|
||||
check_company=True, index='btree_not_null')
|
||||
mo_bom_id = fields.Many2one('mrp.bom', 'Bill of Material used on the Production Order', related='mo_id.bom_id')
|
||||
lot_producing_ids = fields.Many2many('stock.lot', string='Lot/Serial Numbers', related='mo_id.lot_producing_ids')
|
||||
lot_id = fields.Many2one(
|
||||
'stock.lot', 'Lot/Serial Number',
|
||||
domain="[('product_id', '=', product_id), ('company_id', '=', company_id)]", check_company=True)
|
||||
has_tracking=fields.Selection(related='product_id.tracking', readonly=True)
|
||||
domain="[('product_id', '=', product_id),('id', 'in', lot_producing_ids)]", check_company=True)
|
||||
has_tracking = fields.Selection(related='product_id.tracking', readonly=True)
|
||||
location_id = fields.Many2one(
|
||||
'stock.location', 'Source Location',
|
||||
domain="[('usage','=','internal'), '|', ('company_id', '=', False), ('company_id', '=', company_id)]",
|
||||
domain="[('usage','=','internal')]",
|
||||
check_company=True,
|
||||
compute='_compute_location_id', store=True, readonly=False, precompute=True,
|
||||
required=True, states={'done': [('readonly', True)]}, help="Location where the product you want to unbuild is.")
|
||||
required=True, help="Location where the product you want to unbuild is.")
|
||||
location_dest_id = fields.Many2one(
|
||||
'stock.location', 'Destination Location',
|
||||
domain="[('usage','=','internal'), '|', ('company_id', '=', False), ('company_id', '=', company_id)]",
|
||||
domain="[('usage','=','internal')]",
|
||||
check_company=True,
|
||||
compute='_compute_location_id', store=True, readonly=False, precompute=True,
|
||||
required=True, states={'done': [('readonly', True)]}, help="Location where you want to send the components resulting from the unbuild order.")
|
||||
required=True, help="Location where you want to send the components resulting from the unbuild order.")
|
||||
consume_line_ids = fields.One2many(
|
||||
'stock.move', 'consume_unbuild_id', readonly=True,
|
||||
string='Consumed Disassembly Lines')
|
||||
|
|
@ -77,6 +80,11 @@ class MrpUnbuild(models.Model):
|
|||
('draft', 'Draft'),
|
||||
('done', 'Done')], string='Status', default='draft')
|
||||
|
||||
_qty_positive = models.Constraint(
|
||||
'check (product_qty > 0)',
|
||||
'The quantity to unbuild must be positive!',
|
||||
)
|
||||
|
||||
@api.depends('mo_id', 'product_id')
|
||||
def _compute_product_uom_id(self):
|
||||
for record in self:
|
||||
|
|
@ -95,30 +103,30 @@ class MrpUnbuild(models.Model):
|
|||
if order.location_dest_id.company_id != order.company_id:
|
||||
order.location_dest_id = warehouse.lot_stock_id
|
||||
|
||||
@api.onchange('mo_id')
|
||||
def _onchange_mo_id(self):
|
||||
if self.mo_id:
|
||||
self.product_id = self.mo_id.product_id.id
|
||||
self.bom_id = self.mo_id.bom_id
|
||||
self.product_uom_id = self.mo_id.product_uom_id
|
||||
self.lot_id = self.mo_id.lot_producing_id
|
||||
if self.has_tracking == 'serial':
|
||||
self.product_qty = 1
|
||||
@api.depends('mo_id', 'product_id', 'company_id')
|
||||
def _compute_bom_id(self):
|
||||
for order in self:
|
||||
if order.mo_id:
|
||||
order.bom_id = order.mo_id.bom_id
|
||||
else:
|
||||
self.product_qty = self.mo_id.qty_produced
|
||||
order.bom_id = self.env['mrp.bom']._bom_find(
|
||||
order.product_id, company_id=order.company_id.id
|
||||
)[order.product_id]
|
||||
|
||||
@api.depends('mo_id')
|
||||
def _compute_product_id(self):
|
||||
for order in self:
|
||||
if order.mo_id and order.mo_id.product_id:
|
||||
order.product_id = order.mo_id.product_id
|
||||
|
||||
@api.onchange('product_id')
|
||||
def _onchange_product_id(self):
|
||||
if self.product_id:
|
||||
self.bom_id = self.env['mrp.bom']._bom_find(self.product_id, company_id=self.company_id.id)[self.product_id]
|
||||
self.product_uom_id = self.mo_id.product_id == self.product_id and self.mo_id.product_uom_id.id or self.product_id.uom_id.id
|
||||
|
||||
@api.constrains('product_qty')
|
||||
def _check_qty(self):
|
||||
for unbuild in self:
|
||||
if unbuild.product_qty <= 0:
|
||||
raise ValidationError(_('Unbuild Order product quantity has to be strictly positive.'))
|
||||
@api.depends('mo_id')
|
||||
def _compute_product_qty(self):
|
||||
for order in self:
|
||||
if order.mo_id:
|
||||
if order.has_tracking == 'serial':
|
||||
order.product_qty = 1
|
||||
else:
|
||||
order.product_qty = order.mo_id.qty_produced
|
||||
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
|
|
@ -136,7 +144,7 @@ class MrpUnbuild(models.Model):
|
|||
return {
|
||||
'move_id': finished_move.id,
|
||||
'lot_id': self.lot_id.id,
|
||||
'qty_done': finished_move.product_uom_qty,
|
||||
'quantity': finished_move.product_uom_qty - finished_move.quantity,
|
||||
'product_id': finished_move.product_id.id,
|
||||
'product_uom_id': finished_move.product_uom.id,
|
||||
'location_id': finished_move.location_id.id,
|
||||
|
|
@ -147,7 +155,7 @@ class MrpUnbuild(models.Model):
|
|||
return {
|
||||
'move_id': move.id,
|
||||
'lot_id': origin_move_line.lot_id.id,
|
||||
'qty_done': taken_quantity,
|
||||
'quantity': taken_quantity,
|
||||
'product_id': move.product_id.id,
|
||||
'product_uom_id': origin_move_line.product_uom_id.id,
|
||||
'location_id': move.location_id.id,
|
||||
|
|
@ -157,70 +165,86 @@ class MrpUnbuild(models.Model):
|
|||
def action_unbuild(self):
|
||||
self.ensure_one()
|
||||
self._check_company()
|
||||
# remove the default_* keys that were only needed in the unbuild wizard
|
||||
self = self.with_env(self.env(context=clean_context(self.env.context))) # noqa: PLW0642
|
||||
if self.product_id.tracking != 'none' and not self.lot_id.id:
|
||||
raise UserError(_('You should provide a lot number for the final product.'))
|
||||
|
||||
if self.mo_id:
|
||||
if self.mo_id.state != 'done':
|
||||
raise UserError(_('You cannot unbuild a undone manufacturing order.'))
|
||||
if self.mo_id and self.mo_id.state != 'done':
|
||||
raise UserError(_('You cannot unbuild a undone manufacturing order.'))
|
||||
|
||||
consume_moves = self._generate_consume_moves()
|
||||
consume_moves._action_confirm()
|
||||
produce_moves = self._generate_produce_moves()
|
||||
produce_moves.with_context(default_lot_id=False)._action_confirm()
|
||||
produce_moves._action_confirm()
|
||||
produce_moves.quantity = 0
|
||||
|
||||
# Collect component lots already restored by previous unbuilds on the same MO
|
||||
previously_unbuilt_lots = (self.mo_id.unbuild_ids - self).produce_line_ids.filtered(lambda ml: ml.product_id != self.product_id and ml.product_id.tracking == 'serial').lot_ids
|
||||
|
||||
finished_moves = consume_moves.filtered(lambda m: m.product_id == self.product_id)
|
||||
consume_moves -= finished_moves
|
||||
error_message = _(
|
||||
"Please specify a manufacturing order.\n"
|
||||
"It will allow us to retrieve the lots/serial numbers of the correct components and/or byproducts."
|
||||
)
|
||||
|
||||
if any(produce_move.has_tracking != 'none' and not self.mo_id for produce_move in produce_moves):
|
||||
raise UserError(_('Some of your components are tracked, you have to specify a manufacturing order in order to retrieve the correct components.'))
|
||||
raise UserError(error_message)
|
||||
|
||||
if any(consume_move.has_tracking != 'none' and not self.mo_id for consume_move in consume_moves):
|
||||
raise UserError(_('Some of your byproducts are tracked, you have to specify a manufacturing order in order to retrieve the correct byproducts.'))
|
||||
raise UserError(error_message)
|
||||
|
||||
for finished_move in finished_moves:
|
||||
if finished_move.has_tracking != 'none':
|
||||
if float_compare(finished_move.product_uom_qty, finished_move.quantity, precision_rounding=finished_move.product_uom.rounding) > 0:
|
||||
finished_move_line_vals = self._prepare_finished_move_line_vals(finished_move)
|
||||
self.env["stock.move.line"].create(finished_move_line_vals)
|
||||
else:
|
||||
finished_move.quantity_done = self.product_qty
|
||||
self.env['stock.move.line'].create(finished_move_line_vals)
|
||||
|
||||
# TODO: Will fail if user do more than one unbuild with lot on the same MO. Need to check what other unbuild has aready took
|
||||
qty_already_used = defaultdict(float)
|
||||
for move in produce_moves | consume_moves:
|
||||
if move.has_tracking != 'none':
|
||||
original_move = move in produce_moves and self.mo_id.move_raw_ids or self.mo_id.move_finished_ids
|
||||
original_move = original_move.filtered(lambda m: m.product_id == move.product_id)
|
||||
needed_quantity = move.product_uom_qty
|
||||
moves_lines = original_move.mapped('move_line_ids')
|
||||
if move in produce_moves and self.lot_id:
|
||||
moves_lines = moves_lines.filtered(lambda ml: self.lot_id in ml.produce_line_ids.lot_id) # FIXME sle: double check with arm
|
||||
for move_line in moves_lines:
|
||||
# Iterate over all move_lines until we unbuilded the correct quantity.
|
||||
taken_quantity = min(needed_quantity, move_line.qty_done - qty_already_used[move_line])
|
||||
if taken_quantity:
|
||||
move_line_vals = self._prepare_move_line_vals(move, move_line, taken_quantity)
|
||||
self.env["stock.move.line"].create(move_line_vals)
|
||||
needed_quantity -= taken_quantity
|
||||
qty_already_used[move_line] += taken_quantity
|
||||
else:
|
||||
move.quantity_done = float_round(move.product_uom_qty, precision_rounding=move.product_uom.rounding)
|
||||
if float_compare(move.product_uom_qty, move.quantity, precision_rounding=move.product_uom.rounding) < 1:
|
||||
continue
|
||||
original_move = move in produce_moves and self.mo_id.move_raw_ids or self.mo_id.move_finished_ids
|
||||
original_move = original_move.filtered(lambda m: m.product_id == move.product_id)
|
||||
if not original_move:
|
||||
move.quantity = move.product_uom.round(move.product_uom_qty)
|
||||
continue
|
||||
needed_quantity = move.product_uom_qty
|
||||
moves_lines = original_move.mapped('move_line_ids')
|
||||
if move in produce_moves and self.lot_id:
|
||||
moves_lines = moves_lines.filtered(
|
||||
lambda ml: self.lot_id in ml.produce_line_ids.lot_id and ml.lot_id not in previously_unbuilt_lots
|
||||
)
|
||||
for move_line in moves_lines:
|
||||
# Iterate over all move_lines until we unbuilded the correct quantity.
|
||||
taken_quantity = min(needed_quantity, move_line.quantity - qty_already_used[move_line])
|
||||
taken_quantity = move.product_uom.round(taken_quantity)
|
||||
if taken_quantity:
|
||||
move_line_vals = self._prepare_move_line_vals(move, move_line, taken_quantity)
|
||||
if move_line.owner_id:
|
||||
move_line_vals['owner_id'] = move_line.owner_id.id
|
||||
unbuild_move_line = self.env["stock.move.line"].create(move_line_vals)
|
||||
needed_quantity -= taken_quantity
|
||||
qty_already_used[move_line] += taken_quantity
|
||||
unbuild_move_line._apply_putaway_strategy()
|
||||
|
||||
(finished_moves | consume_moves | produce_moves).picked = True
|
||||
finished_moves._action_done()
|
||||
consume_moves._action_done()
|
||||
produce_moves._action_done()
|
||||
produced_move_line_ids = produce_moves.mapped('move_line_ids').filtered(lambda ml: ml.qty_done > 0)
|
||||
produced_move_line_ids = produce_moves.mapped('move_line_ids').filtered(lambda ml: ml.quantity > 0)
|
||||
consume_moves.mapped('move_line_ids').write({'produce_line_ids': [(6, 0, produced_move_line_ids.ids)]})
|
||||
if self.mo_id:
|
||||
unbuild_msg = _(
|
||||
"%(qty)s %(measure)s unbuilt in %(order)s",
|
||||
unbuild_msg = _("%(qty)s %(measure)s unbuilt in %(order)s",
|
||||
qty=self.product_qty,
|
||||
measure=self.product_uom_id.name,
|
||||
order=self._get_html_link(),
|
||||
)
|
||||
self.mo_id.message_post(
|
||||
body=unbuild_msg,
|
||||
subtype_id=self.env.ref('mail.mt_note').id)
|
||||
subtype_xmlid='mail.mt_note',
|
||||
)
|
||||
return self.write({'state': 'done'})
|
||||
|
||||
def _generate_consume_moves(self):
|
||||
|
|
@ -228,8 +252,7 @@ class MrpUnbuild(models.Model):
|
|||
for unbuild in self:
|
||||
if unbuild.mo_id:
|
||||
finished_moves = unbuild.mo_id.move_finished_ids.filtered(lambda move: move.state == 'done')
|
||||
moved_qty = unbuild.mo_id.product_uom_id._compute_quantity(unbuild.mo_id.qty_produced, unbuild.product_uom_id)
|
||||
factor = unbuild.product_qty / moved_qty if moved_qty else 0
|
||||
factor = unbuild.product_qty / unbuild.mo_id.product_uom_id._compute_quantity(unbuild.mo_id.qty_produced, unbuild.product_uom_id)
|
||||
for finished_move in finished_moves:
|
||||
moves += unbuild._generate_move_from_existing_move(finished_move, factor, unbuild.location_id, finished_move.location_id)
|
||||
else:
|
||||
|
|
@ -259,10 +282,9 @@ class MrpUnbuild(models.Model):
|
|||
|
||||
def _generate_move_from_existing_move(self, move, factor, location_id, location_dest_id):
|
||||
return self.env['stock.move'].create({
|
||||
'name': self.name,
|
||||
'date': self.create_date,
|
||||
'product_id': move.product_id.id,
|
||||
'product_uom_qty': move.quantity_done * factor,
|
||||
'product_uom_qty': move.quantity * factor,
|
||||
'product_uom': move.product_uom.id,
|
||||
'procure_method': 'make_to_stock',
|
||||
'location_dest_id': location_dest_id.id,
|
||||
|
|
@ -279,7 +301,6 @@ class MrpUnbuild(models.Model):
|
|||
location_dest_id = bom_line_id and self.location_dest_id or product_prod_location
|
||||
warehouse = location_dest_id.warehouse_id
|
||||
return self.env['stock.move'].create({
|
||||
'name': self.name,
|
||||
'date': self.create_date,
|
||||
'bom_line_id': bom_line_id,
|
||||
'byproduct_id': byproduct_id,
|
||||
|
|
@ -296,14 +317,14 @@ class MrpUnbuild(models.Model):
|
|||
|
||||
def action_validate(self):
|
||||
self.ensure_one()
|
||||
precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')
|
||||
precision = self.env['decimal.precision'].precision_get('Product Unit')
|
||||
available_qty = self.env['stock.quant']._get_available_quantity(self.product_id, self.location_id, self.lot_id, strict=True)
|
||||
unbuild_qty = self.product_uom_id._compute_quantity(self.product_qty, self.product_id.uom_id)
|
||||
if float_compare(available_qty, unbuild_qty, precision_digits=precision) >= 0:
|
||||
return self.action_unbuild()
|
||||
else:
|
||||
return {
|
||||
'name': self.product_id.display_name + _(': Insufficient Quantity To Unbuild'),
|
||||
'name': _('%(product)s: Insufficient Quantity To Unbuild', product=self.product_id.display_name),
|
||||
'view_mode': 'form',
|
||||
'res_model': 'stock.warn.insufficient.qty.unbuild',
|
||||
'view_id': self.env.ref('mrp.stock_warn_insufficient_qty_unbuild_form_view').id,
|
||||
|
|
@ -313,7 +334,7 @@ class MrpUnbuild(models.Model):
|
|||
'default_location_id': self.location_id.id,
|
||||
'default_unbuild_id': self.id,
|
||||
'default_quantity': unbuild_qty,
|
||||
'default_product_uom_name': self.product_id.uom_name
|
||||
'default_product_uom_name': self.product_id.uom_name,
|
||||
},
|
||||
'target': 'new'
|
||||
'target': 'new',
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue