res_partner.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. class ResPartner(models.Model):
  5. _inherit = 'res.partner'
  6. property_stock_subcontractor = fields.Many2one(
  7. 'stock.location', string="Subcontractor Location", company_dependent=True,
  8. help="The stock location used as source and destination when sending\
  9. goods to this contact during a subcontracting process.")
  10. is_subcontractor = fields.Boolean(
  11. string="Subcontractor", store=False, search="_search_is_subcontractor", compute="_compute_is_subcontractor")
  12. bom_ids = fields.Many2many('mrp.bom', compute='_compute_bom_ids', string="BoMs for which the Partner is one of the subcontractors")
  13. production_ids = fields.Many2many('mrp.production', compute='_compute_production_ids', string="MRP Productions for which the Partner is the subcontractor")
  14. picking_ids = fields.Many2many('stock.picking', compute='_compute_picking_ids', string="Stock Pickings for which the Partner is the subcontractor")
  15. def _compute_bom_ids(self):
  16. results = self.env['mrp.bom'].read_group([('subcontractor_ids.commercial_partner_id', 'in', self.commercial_partner_id.ids)], ['ids:array_agg(id)', 'subcontractor_ids'], ['subcontractor_ids'])
  17. for partner in self:
  18. bom_ids = []
  19. for res in results:
  20. if partner.id == res['subcontractor_ids'][0] or res['subcontractor_ids'][0] in partner.child_ids.ids:
  21. bom_ids += res['ids']
  22. partner.bom_ids = bom_ids
  23. def _compute_production_ids(self):
  24. results = self.env['mrp.production'].read_group([('subcontractor_id.commercial_partner_id', 'in', self.commercial_partner_id.ids)], ['ids:array_agg(id)'], ['subcontractor_id'])
  25. for partner in self:
  26. production_ids = []
  27. for res in results:
  28. if partner.id == res['subcontractor_id'][0] or res['subcontractor_id'][0] in partner.child_ids.ids:
  29. production_ids += res['ids']
  30. partner.production_ids = production_ids
  31. def _compute_picking_ids(self):
  32. results = self.env['stock.picking'].read_group([('partner_id.commercial_partner_id', 'in', self.commercial_partner_id.ids)], ['ids:array_agg(id)'], ['partner_id'])
  33. for partner in self:
  34. picking_ids = []
  35. for res in results:
  36. if partner.id == res['partner_id'][0] or res['partner_id'][0] in partner.child_ids.ids:
  37. picking_ids += res['ids']
  38. partner.picking_ids = picking_ids
  39. def _search_is_subcontractor(self, operator, value):
  40. assert operator in ('=', '!=', '<>') and value in (True, False), 'Operation not supported'
  41. subcontractor_ids = self.env['mrp.bom'].search(
  42. [('type', '=', 'subcontract')]).subcontractor_ids.ids
  43. if (operator == '=' and value is True) or (operator in ('<>', '!=') and value is False):
  44. search_operator = 'in'
  45. else:
  46. search_operator = 'not in'
  47. return [('id', search_operator, subcontractor_ids)]
  48. @api.depends_context('uid')
  49. def _compute_is_subcontractor(self):
  50. """ Check if the user is a subcontractor before giving sudo access
  51. """
  52. for partner in self:
  53. partner.is_subcontractor = (partner.user_has_groups('base.group_portal') and partner.env['mrp.bom'].search_count([
  54. ('type', '=', 'subcontract'),
  55. ('subcontractor_ids', 'in', (partner.env.user.partner_id | partner.env.user.partner_id.commercial_partner_id).ids),
  56. ]))