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.

189 lines
7.5 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(
  28. self, cr, uid, ids, dummy_name, dummy_arg, context=None):
  29. if context is None:
  30. context = {}
  31. #TODO: do a permission test on returned ids
  32. cr.execute(
  33. '''select r.id, left_partner_id, right_partner_id
  34. from res_partner_relation r
  35. join res_partner_relation_type t
  36. on r.type_id = t.id
  37. where ((left_partner_id in %s and own_tab_left=False)
  38. or (right_partner_id in %s and own_tab_right=False))''' +
  39. ' order by ' + self.pool['res.partner.relation']._order,
  40. (tuple(ids), tuple(ids))
  41. )
  42. result = dict([(i, []) for i in ids])
  43. for row in cr.fetchall():
  44. if row[1] in result:
  45. result[row[1]].append(row[0])
  46. if row[2] in result:
  47. result[row[2]].append(row[0])
  48. return result
  49. def _create_relation_type_tab(
  50. self, cr, uid, rel_type, inverse, field_names, context=None):
  51. '''Create an xml node containing the relation's tab to be added to the
  52. view. Add the field(s) created on the form to field_names.'''
  53. name = rel_type.name if not inverse else rel_type.name_inverse
  54. contact_type = rel_type['contact_type_' +
  55. ('left' if not inverse else 'right')]
  56. partner_category = rel_type['partner_category_' +
  57. ('left' if not inverse
  58. else 'right')]
  59. tab = etree.Element('page')
  60. tab.set('string', name)
  61. invisible = [('id', '=', False)]
  62. if contact_type:
  63. invisible = expression.OR([
  64. invisible,
  65. [('is_company', '=', contact_type != 'c')]])
  66. if partner_category:
  67. invisible = expression.OR([
  68. invisible,
  69. [('category_id', '!=', partner_category.id)]])
  70. attrs = {
  71. 'invisible': invisible,
  72. }
  73. tab.set('attrs', repr(attrs))
  74. transfer_modifiers_to_node(attrs, tab)
  75. field_name = 'relation_ids_own_tab_%s_%s' % (
  76. rel_type.id,
  77. 'left' if not inverse else 'right')
  78. field_names.append(field_name)
  79. this_partner_name = '%s_partner_id' % (
  80. 'left' if not inverse else 'right')
  81. other_partner_name = '%s_partner_id' % (
  82. 'left' if inverse else 'right')
  83. field = etree.Element(
  84. 'field',
  85. name=field_name,
  86. context=('{"default_type_id": %s, "default_%s": id, '
  87. '"active_test": False}') % (
  88. rel_type.id,
  89. this_partner_name))
  90. tab.append(field)
  91. tree = etree.Element('tree', editable='bottom')
  92. field.append(tree)
  93. onchange_type_values = self.pool['res.partner.relation']\
  94. .on_change_type_selection_id(cr, uid, None,
  95. rel_type.id * 10 +
  96. (1 if inverse else 0),
  97. context=context)
  98. tree.append(etree.Element(
  99. 'field',
  100. string=_('Partner'),
  101. domain=repr(
  102. onchange_type_values['domain']['partner_id_display']),
  103. widget='many2one_clickable',
  104. name=other_partner_name))
  105. tree.append(etree.Element(
  106. 'field',
  107. name='date_start'))
  108. tree.append(etree.Element(
  109. 'field',
  110. name='date_end'))
  111. tree.append(etree.Element(
  112. 'field',
  113. name='active'))
  114. tree.append(etree.Element('field', name='type_id',
  115. invisible='True'))
  116. tree.append(etree.Element('field', name=this_partner_name,
  117. invisible='True'))
  118. return tab
  119. def _add_relation_type_tab(
  120. self, cr, uid, rel_type, inverse, field_names, relation_tab,
  121. context=None):
  122. '''add the xml node to the view'''
  123. tab = self._create_relation_type_tab(
  124. cr, uid, rel_type, inverse, field_names, context=context)
  125. relation_tab.addnext(tab)
  126. def fields_view_get(self, cr, uid, view_id=None, view_type='form',
  127. context=None, toolbar=False, submenu=False):
  128. if context is None:
  129. context = {}
  130. result = super(ResPartner, self).fields_view_get(
  131. cr, uid, view_id=view_id, view_type=view_type, context=context,
  132. toolbar=toolbar, submenu=submenu)
  133. if view_type == 'form' and not context.get('check_view_ids'):
  134. res_partner_relation_type = self.pool['res.partner.relation.type']
  135. own_tab_types = res_partner_relation_type.browse(
  136. cr, uid,
  137. res_partner_relation_type.search(
  138. cr, uid,
  139. [
  140. '|',
  141. ('own_tab_left', '=', True),
  142. ('own_tab_right', '=', True)
  143. ],
  144. context=context),
  145. context=context)
  146. view = etree.fromstring(result['arch'])
  147. relation_tab = view.xpath(
  148. '//field[@name="relation_ids"]/ancestor::page')
  149. if not relation_tab:
  150. return result
  151. relation_tab = relation_tab[0]
  152. field_names = []
  153. if not view.xpath('//field[@name="id"]'):
  154. view.append(etree.Element('field', name='id',
  155. invisible='True'))
  156. field_names.append('id')
  157. for rel_type in own_tab_types:
  158. if rel_type.own_tab_left:
  159. self._add_relation_type_tab(
  160. cr, uid, rel_type, False, field_names, relation_tab,
  161. context=context)
  162. if rel_type.own_tab_right:
  163. self._add_relation_type_tab(
  164. cr, uid, rel_type, True, field_names, relation_tab,
  165. context=context)
  166. result['arch'], fields = self\
  167. ._BaseModel__view_look_dom_arch(
  168. cr, uid, view, result['view_id'], context=context)
  169. for field_name in field_names:
  170. result['fields'][field_name] = fields[field_name]
  171. return result