[MIG] product_quick_bom: Migration to 12.0

This commit is contained in:
Kevin Khao
2020-01-07 15:36:05 +01:00
committed by ps-tubtim
parent c46da3555a
commit b4878fdd00
12 changed files with 579 additions and 184 deletions

View File

@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2015 Akretion (http://www.akretion.com).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from . import product
from . import bom

View File

@@ -1,25 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2015 Akretion (http://www.akretion.com).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo import fields, models, _
class MrpBom(models.Model):
_inherit = 'mrp.bom'
_sql_constraint = (
'uniq_product_template',
'uniq(product_tmpl_id)',
_('You can only have one Bom per product template'),
)
class MrpBomLine(models.Model):
_inherit = 'mrp.bom.line'
product_tmpl_id = fields.Many2one(
comodel_name='product.template',
related='bom_id.product_tmpl_id',
readonly=True,
store=True)

View File

@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2015 Akretion (http://www.akretion.com).
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
@@ -6,59 +5,33 @@ from odoo import api, fields, models
class ProductTemplate(models.Model):
_inherit = 'product.template'
_inherit = "product.template"
bom_line_ids = fields.One2many(
comodel_name='mrp.bom.line',
inverse_name='product_tmpl_id',
string='Bom Line',
help='If your product is manufactured you can select '
'here the component to produce them')
@api.model
def _extract_bom_line(self, vals):
return vals.pop('bom_line_ids', {})
@api.multi
def _prepare_bom_vals(self, vals):
self.ensure_one()
return {
'product_tmpl_id': self.id,
'bom_line_ids': vals,
}
@api.multi
def _process_bom_vals(self, vals):
for record in self:
if record.bom_ids:
record.bom_ids[0].write({'bom_line_ids': vals})
@api.depends("bom_ids")
def _compute_bom_id(self):
for rec in self:
if len(rec.bom_ids.ids) == 1:
rec.bom_id = rec.bom_ids[0]
else:
record.env['mrp.bom'].create(self._prepare_bom_vals(vals))
rec.bom_id = self.env["mrp.bom"]
@api.model
def create(self, vals):
bom_vals = self._extract_bom_line(vals)
record = super(ProductTemplate, self).create(vals)
if bom_vals:
record._process_bom_vals(bom_vals)
return record
bom_id = fields.Many2one(
"mrp.bom",
string="Bill of Materials (quick access)",
compute=_compute_bom_id,
store=True,
)
@api.multi
def write(self, vals):
""" default_type is in product and bom so _context ovewrite the default
value for bom with typ:'product' need to becarefull
"""
bom_vals = self._extract_bom_line(vals)
res = super(ProductTemplate, self).write(vals)
if bom_vals:
ctx = self._context.copy()
ctx.pop('default_type', None)
self.with_context(ctx)._process_bom_vals(bom_vals)
return res
# In case there is no bom_id yet, to simplify logic
# we just add a button to create one
def button_create_bom(self):
self.ensure_one()
vals = {
"product_tmpl_id": self.id,
"type": "normal",
}
self.env["mrp.bom"].create(vals)
@api.multi
def unlink(self):
for record in self:
if record.bom_ids:
record.bom_ids.unlink()
return super(ProductTemplate, self).unlink()
specific_bom_line_ids = fields.One2many(
related="bom_id.bom_line_ids", readonly=False
)