| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from odoo import api, fields, models, _ |
| 5 |
|
| 6 |
|
| 7 |
class SaleOrder(models.Model): |
| 8 |
_inherit = 'sale.order' |
| 9 |
|
| 10 |
mrp_production_count = fields.Integer( |
| 11 |
"Count of MO generated", |
| 12 |
compute='_compute_mrp_production_ids', |
| 13 |
groups='mrp.group_mrp_user') |
| 14 |
mrp_production_ids = fields.Many2many( |
| 15 |
'mrp.production', |
| 16 |
compute='_compute_mrp_production_ids', |
| 17 |
string='Manufacturing orders associated with this sales order.', |
| 18 |
groups='mrp.group_mrp_user') |
| 19 |
|
| 20 |
@api.depends('stock_reference_ids.production_ids') |
| 21 |
def _compute_mrp_production_ids(self): |
| 22 |
for sale in self: |
| 23 |
|
| 24 |
mos = sale.stock_reference_ids.production_ids |
| 25 |
sale.mrp_production_ids = mos.filtered(lambda mo: not mo.production_group_id.parent_ids and mo.state != 'cancel') |
| 26 |
sale.mrp_production_count = len(sale.mrp_production_ids) |
| 27 |
|
| 28 |
def action_view_mrp_production(self): |
| 29 |
self.ensure_one() |
| 30 |
action = { |
| 31 |
'res_model': 'mrp.production', |
| 32 |
'type': 'ir.actions.act_window', |
| 33 |
} |
| 34 |
if len(self.mrp_production_ids) == 1: |
| 35 |
action.update({ |
| 36 |
'view_mode': 'form', |
| 37 |
'res_id': self.mrp_production_ids.id, |
| 38 |
}) |
| 39 |
else: |
| 40 |
action.update({ |
| 41 |
'name': _("Manufacturing Orders Generated by %s", self.name), |
| 42 |
'domain': [('id', 'in', self.mrp_production_ids.ids)], |
| 43 |
'view_mode': 'list,form', |
| 44 |
}) |
| 45 |
return action |