summaryrefslogtreecommitdiff
path: root/addons/mrp_subcontracting/models
diff options
context:
space:
mode:
authorArnold Moyaux <arm@odoo.com>2019-07-31 13:42:42 +0000
committerArnold Moyaux <arm@odoo.com>2019-08-19 15:03:59 +0000
commit33cf89a6c675744338654ebf66e3c75f77bb341c (patch)
tree4187d8f7fe7519dab92d9578059dc8593019d158 /addons/mrp_subcontracting/models
parent319ae318601dcfb59bf02b2bd8d29f78f0ff46ba (diff)
[REF] mrp_subcontracting: remove partner type
Replace the type subcontractor on the partner. Instead replace it by a property for a subcontracting location. The purpose is to have a simplier configuration. The user would just need to add the partner on the BoM in order to start a subcontracting process.
Diffstat (limited to 'addons/mrp_subcontracting/models')
-rw-r--r--addons/mrp_subcontracting/models/mrp_bom.py3
-rw-r--r--addons/mrp_subcontracting/models/res_company.py27
-rw-r--r--addons/mrp_subcontracting/models/res_partner.py53
-rw-r--r--addons/mrp_subcontracting/models/stock_move.py10
-rw-r--r--addons/mrp_subcontracting/models/stock_picking.py9
5 files changed, 31 insertions, 71 deletions
diff --git a/addons/mrp_subcontracting/models/mrp_bom.py b/addons/mrp_subcontracting/models/mrp_bom.py
index 8f0d87d0b73..b848ec854d1 100644
--- a/addons/mrp_subcontracting/models/mrp_bom.py
+++ b/addons/mrp_subcontracting/models/mrp_bom.py
@@ -8,7 +8,7 @@ class MrpBom(models.Model):
_inherit = 'mrp.bom'
type = fields.Selection(selection_add=[('subcontract', 'Subcontracting')])
- subcontractor_ids = fields.One2many('res.partner', 'bom_id', domain=[('type', '=', 'subcontractor')], string='Subcontractors')
+ subcontractor_ids = fields.Many2many('res.partner', 'mrp_bom_subcontractor', string='Subcontractors')
def _bom_subcontract_find(self, product_tmpl=None, product=None, picking_type=None, company_id=False, bom_type='subcontract', subcontractor=False):
domain = self._bom_find_domain(product_tmpl=product_tmpl, product=product, picking_type=picking_type, company_id=company_id, bom_type=bom_type)
@@ -17,4 +17,3 @@ class MrpBom(models.Model):
else:
domain += [('subcontractor_ids', '=', False)]
return self.search(domain, order='subcontractor_ids, sequence, product_id', limit=1)
-
diff --git a/addons/mrp_subcontracting/models/res_company.py b/addons/mrp_subcontracting/models/res_company.py
index a6456213a63..74cd5555591 100644
--- a/addons/mrp_subcontracting/models/res_company.py
+++ b/addons/mrp_subcontracting/models/res_company.py
@@ -9,24 +9,33 @@ class ResCompany(models.Model):
subcontracting_location_id = fields.Many2one('stock.location')
+ @api.model
+ def create_missing_subcontracting_location(self):
+ company_without_subcontracting_loc = self.env['res.company'].search(
+ [('subcontracting_location_id', '=', False)])
+ company_without_subcontracting_loc._create_subcontracting_location()
+
def _create_per_company_locations(self):
super(ResCompany, self)._create_per_company_locations()
self._create_subcontracting_location()
def _create_subcontracting_location(self):
+ parent_location = self.env.ref('stock.stock_location_locations_partner', raise_if_not_found=False)
+ property_stock_subcontractor_res_partner_field = self.env['ir.model.fields'].search([
+ ('model', '=', 'res.partner'),
+ ('name', '=', 'property_stock_subcontractor')
+ ])
for company in self:
- parent_location = self.env.ref('stock.stock_location_locations_partner', raise_if_not_found=False)
subcontracting_location = self.env['stock.location'].create({
'name': _('%s: Subcontracting Location') % company.name,
'usage': 'internal',
'location_id': parent_location.id,
'company_id': company.id,
})
- company.write({'subcontracting_location_id': subcontracting_location.id})
-
- @api.model
- def create_missing_subcontracting_location(self):
- company_without_subcontracting_loc = self.env['res.company'].search(
- [('subcontracting_location_id', '=', False)])
- for company in company_without_subcontracting_loc:
- company._create_subcontracting_location()
+ self.env['ir.property'].create({
+ 'name': 'property_stock_subcontractor_%s' % company.name,
+ 'fields_id': property_stock_subcontractor_res_partner_field.id,
+ 'company_id': company.id,
+ 'value': 'stock.location,%d' % subcontracting_location.id,
+ })
+ company.subcontracting_location_id = subcontracting_location
diff --git a/addons/mrp_subcontracting/models/res_partner.py b/addons/mrp_subcontracting/models/res_partner.py
index 925296fee0c..603d5dbbe93 100644
--- a/addons/mrp_subcontracting/models/res_partner.py
+++ b/addons/mrp_subcontracting/models/res_partner.py
@@ -1,56 +1,13 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
-from odoo import api, fields, models, _
+from odoo import fields, models
class ResPartner(models.Model):
_inherit = 'res.partner'
- type = fields.Selection(selection_add=[('subcontractor', 'Subcontractor')])
- bom_id = fields.Many2one('mrp.bom', domain=[('type', '=', 'subcontract')])
-
- def _get_name(self):
- name = super(ResPartner, self)._get_name()
- if self.type == 'subcontractor':
- subcontractor = _('Subcontractor')
- if name.endswith(', '):
- name = "%s%s" % (name, subcontractor)
- else:
- name = '%s, %s' % (name, subcontractor)
- return name
-
- @api.onchange('type')
- def _onchange_partner_type(self):
- if self.type == 'subcontractor':
- company = self.company_id or self.parent_id.company_id
- subcontracting_location = company.subcontracting_location_id
- self.update({
- 'property_stock_customer': subcontracting_location,
- 'property_stock_supplier': subcontracting_location
- })
- else:
- self.update({
- 'property_stock_customer': self.env.ref('stock.stock_location_customers'),
- 'property_stock_supplier': self.env.ref('stock.stock_location_suppliers')
- })
-
- @api.model_create_multi
- def create(self, vals_list):
- for vals in vals_list:
- if vals.get('type') == 'subcontractor':
- if not vals.get('property_stock_supplier') and not vals.get('property_stock_customer'):
- company = vals.get('company_id') and self.env['res.company'].browse(vals['company_id']) or \
- self.browse(vals.get('parent_id')).company_id
- subcontracting_location = company.subcontracting_location_id
- vals['property_stock_supplier'] = subcontracting_location
- vals['property_stock_customer'] = subcontracting_location
- return super(ResPartner, self).create(vals_list)
-
- def write(self, vals):
- if vals.get('type') == 'subcontractor':
- subcontracting_location = self.env.company.subcontracting_location_id
- if not vals.get('property_stock_supplier') and not vals.get('property_stock_customer'):
- vals['property_stock_supplier'] = subcontracting_location.id
- vals['property_stock_customer'] = subcontracting_location.id
- return super(ResPartner, self).write(vals)
+ property_stock_subcontractor = fields.Many2one(
+ 'stock.location', string="Subcontractor Location", company_dependent=True,
+ help="The stock location used as source and destination when sending\
+ goods to this contact during a subcontracting process.")
diff --git a/addons/mrp_subcontracting/models/stock_move.py b/addons/mrp_subcontracting/models/stock_move.py
index eba26222c8b..8d01c2423fa 100644
--- a/addons/mrp_subcontracting/models/stock_move.py
+++ b/addons/mrp_subcontracting/models/stock_move.py
@@ -47,19 +47,17 @@ class StockMove(models.Model):
def _action_confirm(self, merge=True, merge_into=False):
subcontract_details_per_picking = defaultdict(list)
for move in self:
- if not move.picking_id:
+ if move.location_id.usage != 'supplier' or move.location_dest_id.usage == 'supplier':
continue
- if not move.picking_id._is_subcontract():
+ if not move.picking_id:
continue
bom = move._get_subcontract_bom()
if not bom:
- error_message = _('Please define a BoM of type subcontracting for the product "%s". If you don\'t want to subcontract the product "%s", do not select a partner of type subcontractor.')
- error_message += '\n\n'
- error_message += _('If there is well a BoM of type subcontracting defined, check if you have set the correct subcontractors on it.')
- raise UserError(error_message % (move.product_id.name, move.product_id.name))
+ continue
subcontract_details_per_picking[move.picking_id].append((move, bom))
move.write({
'is_subcontract': True,
+ 'location_id': self.picking_id.partner_id.property_stock_subcontractor.id
})
for picking, subcontract_details in subcontract_details_per_picking.items():
picking._subcontracted_produce(subcontract_details)
diff --git a/addons/mrp_subcontracting/models/stock_picking.py b/addons/mrp_subcontracting/models/stock_picking.py
index 4ab048ad799..c2502469b8c 100644
--- a/addons/mrp_subcontracting/models/stock_picking.py
+++ b/addons/mrp_subcontracting/models/stock_picking.py
@@ -58,10 +58,7 @@ class StockPicking(models.Model):
# -------------------------------------------------------------------------
def _is_subcontract(self):
self.ensure_one()
- if self.partner_id.type == 'subcontractor' and \
- self.picking_type_id.code == 'incoming':
- return True
- return False
+ return self.picking_type_id.code == 'incoming' and any(m.is_subcontract for m in self.move_lines)
def _get_subcontracted_productions(self):
self.ensure_one()
@@ -75,8 +72,8 @@ class StockPicking(models.Model):
'product_id': product.id,
'product_uom_id': subcontract_move.product_uom.id,
'bom_id': bom.id,
- 'location_src_id': subcontract_move.picking_id.partner_id.property_stock_supplier.id,
- 'location_dest_id': subcontract_move.location_id.id,
+ 'location_src_id': subcontract_move.picking_id.partner_id.property_stock_subcontractor.id,
+ 'location_dest_id': subcontract_move.picking_id.partner_id.property_stock_subcontractor.id,
'product_qty': subcontract_move.product_uom_qty,
'picking_type_id': warehouse.subcontracting_type_id.id
}