You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
7.2 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2014 Therp BV (<http://therp.nl>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from lxml import etree
  22. from openerp.osv.orm import Model, transfer_modifiers_to_node
  23. from openerp.osv import expression
  24. from openerp.tools.translate import _
  25. class ResPartner(Model):
  26. _inherit = 'res.partner'
  27. def _get_relation_ids_select(self, cr, uid, ids, field_name, arg,
  28. context=None):
  29. cr.execute(
  30. '''select r.id, left_partner_id, right_partner_id
  31. from res_partner_relation r
  32. join res_partner_relation_type t
  33. on r.type_id = t.id
  34. where ((left_partner_id in %s and own_tab_left=False)
  35. or (right_partner_id in %s and own_tab_right=False))''' +
  36. ' order by ' + self.pool['res.partner.relation']._order,
  37. (tuple(ids), tuple(ids))
  38. )
  39. return cr.fetchall()
  40. def _create_relation_type_tab(
  41. self, cr, uid, rel_type, inverse, field_names, context=None):
  42. '''Create an xml node containing the relation's tab to be added to the
  43. view. Add the field(s) created on the form to field_names.'''
  44. name = rel_type.name if not inverse else rel_type.name_inverse
  45. contact_type = rel_type['contact_type_' +
  46. ('left' if not inverse else 'right')]
  47. partner_category = rel_type['partner_category_' +
  48. ('left' if not inverse
  49. else 'right')]
  50. tab = etree.Element('page')
  51. tab.set('string', name)
  52. invisible = [('id', '=', False)]
  53. if contact_type:
  54. invisible = expression.OR([
  55. invisible,
  56. [('is_company', '=', contact_type != 'c')]])
  57. if partner_category:
  58. invisible = expression.OR([
  59. invisible,
  60. [('category_id', '!=', partner_category.id)]])
  61. attrs = {
  62. 'invisible': invisible,
  63. }
  64. tab.set('attrs', repr(attrs))
  65. transfer_modifiers_to_node(attrs, tab)
  66. field_name = 'relation_ids_own_tab_%s_%s' % (
  67. rel_type.id,
  68. 'left' if not inverse else 'right')
  69. field_names.append(field_name)
  70. this_partner_name = '%s_partner_id' % (
  71. 'left' if not inverse else 'right')
  72. other_partner_name = '%s_partner_id' % (
  73. 'left' if inverse else 'right')
  74. field = etree.Element(
  75. 'field',
  76. name=field_name,
  77. context=('{"default_type_id": %s, "default_%s": id, '
  78. '"active_test": False}') % (
  79. rel_type.id,
  80. this_partner_name))
  81. tab.append(field)
  82. tree = etree.Element('tree', editable='bottom')
  83. field.append(tree)
  84. onchange_type_values = self.pool['res.partner.relation']\
  85. .on_change_type_selection_id(cr, uid, None,
  86. rel_type.id * 10 +
  87. (1 if inverse else 0),
  88. context=context)
  89. tree.append(etree.Element(
  90. 'field',
  91. string=_('Partner'),
  92. domain=repr(
  93. onchange_type_values['domain']['partner_id_display']),
  94. widget='many2one_clickable',
  95. name=other_partner_name))
  96. tree.append(etree.Element(
  97. 'field',
  98. name='date_start'))
  99. tree.append(etree.Element(
  100. 'field',
  101. name='date_end'))
  102. tree.append(etree.Element(
  103. 'field',
  104. name='active'))
  105. tree.append(etree.Element('field', name='type_id',
  106. invisible='True'))
  107. tree.append(etree.Element('field', name=this_partner_name,
  108. invisible='True'))
  109. return tab
  110. def _add_relation_type_tab(
  111. self, cr, uid, rel_type, inverse, field_names, relation_tab,
  112. context=None):
  113. '''add the xml node to the view'''
  114. tab = self._create_relation_type_tab(
  115. cr, uid, rel_type, inverse, field_names, context=context)
  116. relation_tab.addnext(tab)
  117. def fields_view_get(self, cr, uid, view_id=None, view_type='form',
  118. context=None, toolbar=False, submenu=False):
  119. if context is None:
  120. context = {}
  121. result = super(ResPartner, self).fields_view_get(
  122. cr, uid, view_id=view_id, view_type=view_type, context=context,
  123. toolbar=toolbar, submenu=submenu)
  124. if view_type == 'form' and not context.get('check_view_ids'):
  125. res_partner_relation_type = self.pool['res.partner.relation.type']
  126. own_tab_types = res_partner_relation_type.browse(
  127. cr, uid,
  128. res_partner_relation_type.search(
  129. cr, uid,
  130. [
  131. '|',
  132. ('own_tab_left', '=', True),
  133. ('own_tab_right', '=', True)
  134. ],
  135. context=context),
  136. context=context)
  137. view = etree.fromstring(result['arch'])
  138. relation_tab = view.xpath(
  139. '//field[@name="relation_ids"]/ancestor::page')
  140. if not relation_tab:
  141. return result
  142. relation_tab = relation_tab[0]
  143. field_names = []
  144. if not view.xpath('//field[@name="id"]'):
  145. view.append(etree.Element('field', name='id',
  146. invisible='True'))
  147. field_names.append('id')
  148. for rel_type in own_tab_types:
  149. if rel_type.own_tab_left:
  150. self._add_relation_type_tab(
  151. cr, uid, rel_type, False, field_names, relation_tab,
  152. context=context)
  153. if rel_type.own_tab_right:
  154. self._add_relation_type_tab(
  155. cr, uid, rel_type, True, field_names, relation_tab,
  156. context=context)
  157. result['arch'], fields = self\
  158. ._BaseModel__view_look_dom_arch(
  159. cr, uid, view, result['view_id'], context=context)
  160. for field_name in field_names:
  161. result['fields'][field_name] = fields[field_name]
  162. return result