File: stock_scrap.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (94 lines) | stat: -rw-r--r-- 4,452 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import _, api, fields, models


class StockScrap(models.Model):
    _inherit = 'stock.scrap'

    production_id = fields.Many2one(
        'mrp.production', 'Manufacturing Order',
        check_company=True)
    workorder_id = fields.Many2one(
        'mrp.workorder', 'Work Order',
        check_company=True) # Not to restrict or prefer quants, but informative
    product_is_kit = fields.Boolean(related='product_id.is_kits')
    product_template = fields.Many2one(related='product_id.product_tmpl_id')
    bom_id = fields.Many2one(
        'mrp.bom', 'Kit',
        domain="[('type', '=', 'phantom'), '|', ('product_id', '=', product_id), '&', ('product_id', '=', False), ('product_tmpl_id', '=', product_template)]",
        check_company=True)

    @api.depends('workorder_id', 'production_id')
    def _compute_location_id(self):
        remaining_scrap = self.browse()

        for scrap in self:
            if scrap.production_id:
                if scrap.production_id.state != 'done':
                    scrap.location_id = scrap.production_id.location_src_id.id
                else:
                    scrap.location_id = scrap.production_id.location_dest_id.id
            elif scrap.workorder_id:
                scrap.location_id = scrap.workorder_id.production_id.location_src_id.id
            else:
                remaining_scrap |= scrap

        res = super(StockScrap, remaining_scrap)._compute_location_id()
        return res

    def _prepare_move_values(self):
        vals = super(StockScrap, self)._prepare_move_values()
        if self.production_id:
            vals['origin'] = vals['origin'] or self.production_id.name
            if self.product_id in self.production_id.move_finished_ids.mapped('product_id'):
                vals.update({'production_id': self.production_id.id})
            else:
                vals.update({'raw_material_production_id': self.production_id.id})
        return vals

    @api.onchange('lot_id')
    def _onchange_serial_number(self):
        if self.product_id.tracking == 'serial' and self.lot_id:
            if self.production_id:
                message, recommended_location = self.env['stock.quant'].sudo()._check_serial_number(self.product_id,
                                                                                                    self.lot_id,
                                                                                                    self.company_id,
                                                                                                    self.location_id,
                                                                                                    self.production_id.location_dest_id)
                if message:
                    if recommended_location:
                        self.location_id = recommended_location
                    return {'warning': {'title': _('Warning'), 'message': message}}
            else:
                return super()._onchange_serial_number()

    @api.depends('move_ids', 'move_ids.move_line_ids.quantity', 'product_id')
    def _compute_scrap_qty(self):
        self.scrap_qty = 1
        for scrap in self:
            if not scrap.bom_id:
                return super(StockScrap, scrap)._compute_scrap_qty()
            if scrap.move_ids:
                filters = {
                    'incoming_moves': lambda m: True,
                    'outgoing_moves': lambda m: False
                }
                scrap.scrap_qty = scrap.move_ids._compute_kit_quantities(scrap.product_id, scrap.scrap_qty, scrap.bom_id, filters)

    def _should_check_available_qty(self):
        return super()._should_check_available_qty() or self.product_is_kit

    def do_replenish(self, values=False):
        self.ensure_one()
        values = values or {}
        if self.production_id and self.production_id.procurement_group_id:
            values.update({
                'group_id': self.production_id.procurement_group_id,
                'move_dest_ids': self.production_id.procurement_group_id.stock_move_ids.filtered(
                    lambda m: m.location_id == self.location_id
                              and m.product_id == self.product_id
                              and m.state not in ('assigned', 'done', 'cancel'))
            })
        super().do_replenish(values)