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.

232 lines
9.4 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2013-TODAY OpenERP SA (<http://www.openerp.com>).
  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 openerp.osv import fields, orm, expression
  22. from openerp.tools.translate import _
  23. class res_partner(orm.Model):
  24. _inherit = 'res.partner'
  25. def _type_selection(self, cr, uid, context=None):
  26. return [
  27. ('standalone', _('Standalone Contact')),
  28. ('attached', _('Attached to existing Contact')),
  29. ]
  30. def _get_contact_type(self, cr, uid, ids, field_name, args, context=None):
  31. result = dict.fromkeys(ids, 'standalone')
  32. for partner in self.browse(cr, uid, ids, context=context):
  33. if partner.contact_id:
  34. result[partner.id] = 'attached'
  35. return result
  36. _columns = {
  37. 'contact_type': fields.function(
  38. _get_contact_type,
  39. type='selection',
  40. selection=lambda self, *a, **kw: self._type_selection(*a, **kw),
  41. string='Contact Type',
  42. required=True,
  43. select=1,
  44. store=True,
  45. ),
  46. 'contact_id': fields.many2one(
  47. 'res.partner',
  48. 'Main Contact',
  49. domain=[
  50. ('is_company', '=', False),
  51. ('contact_type', '=', 'standalone'),
  52. ],
  53. ),
  54. 'other_contact_ids': fields.one2many(
  55. 'res.partner',
  56. 'contact_id',
  57. 'Others Positions',
  58. ),
  59. }
  60. _defaults = {
  61. 'contact_type': 'standalone',
  62. }
  63. def _basecontact_check_context(self, cr, user, mode, context=None):
  64. """ Remove 'search_show_all_positions' for non-search mode.
  65. Keeping it in context can result in unexpected behaviour (ex: reading
  66. one2many might return wrong result - i.e with "attached contact"
  67. removed even if it's directly linked to a company).
  68. """
  69. context = dict(context or {})
  70. if mode != 'search':
  71. context.pop('search_show_all_positions', None)
  72. return context
  73. def search(
  74. self, cr, user, args, offset=0, limit=None, order=None,
  75. context=None, count=False):
  76. """ Display only standalone contact matching ``args`` or having
  77. attached contact matching ``args`` """
  78. if context is None:
  79. context = {}
  80. if context.get('search_show_all_positions') is False:
  81. args = expression.normalize_domain(args)
  82. attached_contact_args = expression.AND(
  83. (args, [('contact_type', '=', 'attached')])
  84. )
  85. attached_contact_ids = super(res_partner, self).search(
  86. cr, user, attached_contact_args, context=context
  87. )
  88. args = expression.OR((
  89. expression.AND(([('contact_type', '=', 'standalone')], args)),
  90. [('other_contact_ids', 'in', attached_contact_ids)],
  91. ))
  92. return super(res_partner, self).search(
  93. cr, user, args, offset=offset, limit=limit, order=order,
  94. context=context, count=count
  95. )
  96. def create(self, cr, user, vals, context=None):
  97. context = self._basecontact_check_context(cr, user, 'create', context)
  98. if not vals.get('name') and vals.get('contact_id'):
  99. vals['name'] = self.browse(
  100. cr, user, vals['contact_id'], context=context).name
  101. return super(res_partner, self).create(cr, user, vals, context=context)
  102. def read(
  103. self, cr, user, ids, fields=None, context=None,
  104. load='_classic_read'):
  105. context = self._basecontact_check_context(cr, user, 'read', context)
  106. return super(res_partner, self).read(
  107. cr, user, ids, fields=fields, context=context, load=load)
  108. def write(self, cr, user, ids, vals, context=None):
  109. context = self._basecontact_check_context(cr, user, 'write', context)
  110. return super(
  111. res_partner, self).write(cr, user, ids, vals, context=context)
  112. def unlink(self, cr, user, ids, context=None):
  113. context = self._basecontact_check_context(cr, user, 'unlink', context)
  114. return super(res_partner, self).unlink(cr, user, ids, context=context)
  115. def _commercial_partner_compute(
  116. self, cr, uid, ids, name, args, context=None):
  117. """ Returns the partner that is considered the commercial
  118. entity of this partner. The commercial entity holds the master data
  119. for all commercial fields (see :py:meth:`~_commercial_fields`) """
  120. result = super(res_partner, self)._commercial_partner_compute(
  121. cr, uid, ids, name, args, context=context)
  122. for partner in self.browse(cr, uid, ids, context=context):
  123. if partner.contact_type == 'attached' and not partner.parent_id:
  124. result[partner.id] = partner.contact_id.id
  125. return result
  126. def _contact_fields(self, cr, uid, context=None):
  127. """ Returns the list of contact fields that are synced from the parent
  128. when a partner is attached to him. """
  129. return ['name', 'title']
  130. def _contact_sync_from_parent(self, cr, uid, partner, context=None):
  131. """ Handle sync of contact fields when a new parent contact entity
  132. is set, as if they were related fields
  133. """
  134. if partner.contact_id:
  135. contact_fields = self._contact_fields(cr, uid, context=context)
  136. sync_vals = self._update_fields_values(
  137. cr, uid, partner.contact_id, contact_fields, context=context
  138. )
  139. partner.write(sync_vals)
  140. def update_contact(self, cr, uid, ids, vals, context=None):
  141. if context is None:
  142. context = {}
  143. if context.get('__update_contact_lock'):
  144. return
  145. contact_fields = self._contact_fields(cr, uid, context=context)
  146. contact_vals = dict(
  147. (field, vals[field]) for field in contact_fields if field in vals
  148. )
  149. if contact_vals:
  150. ctx = dict(context, __update_contact_lock=True)
  151. self.write(cr, uid, ids, contact_vals, context=ctx)
  152. def _fields_sync(self, cr, uid, partner, update_values, context=None):
  153. """Sync commercial fields and address fields from company and to
  154. children, contact fields from contact and to attached contact
  155. after create/update, just as if those were all modeled as
  156. fields.related to the parent
  157. """
  158. super(res_partner, self)._fields_sync(
  159. cr, uid, partner, update_values, context=context
  160. )
  161. contact_fields = self._contact_fields(cr, uid, context=context)
  162. # 1. From UPSTREAM: sync from parent contact
  163. if update_values.get('contact_id'):
  164. self._contact_sync_from_parent(cr, uid, partner, context=context)
  165. # 2. To DOWNSTREAM: sync contact fields to parent or related
  166. elif any(field in contact_fields for field in update_values):
  167. update_ids = [
  168. c.id for c in partner.other_contact_ids if not c.is_company
  169. ]
  170. if partner.contact_id:
  171. update_ids.append(partner.contact_id.id)
  172. self.update_contact(
  173. cr, uid, update_ids, update_values, context=context
  174. )
  175. def onchange_contact_id(self, cr, uid, ids, contact_id, context=None):
  176. values = {}
  177. if contact_id:
  178. values['name'] = self.browse(
  179. cr, uid, contact_id, context=context).name
  180. return {'value': values}
  181. def onchange_contact_type(self, cr, uid, ids, contact_type, context=None):
  182. values = {}
  183. if contact_type == 'standalone':
  184. values['contact_id'] = False
  185. return {'value': values}
  186. class ir_actions_window(orm.Model):
  187. _inherit = 'ir.actions.act_window'
  188. def read(
  189. self, cr, user, ids, fields=None, context=None,
  190. load='_classic_read'):
  191. action_ids = ids
  192. if isinstance(ids, (int, long)):
  193. action_ids = [ids]
  194. actions = super(ir_actions_window, self).read(
  195. cr, user, action_ids, fields=fields, context=context, load=load
  196. )
  197. for action in actions:
  198. if action.get('res_model', '') == 'res.partner':
  199. # By default, only show standalone contact
  200. action_context = action.get('context', '{}') or '{}'
  201. if 'search_show_all_positions' not in action_context:
  202. action['context'] = action_context.replace(
  203. '{', "{'search_show_all_positions': False,", 1
  204. )
  205. if isinstance(ids, (int, long)):
  206. if actions:
  207. return actions[0]
  208. return False
  209. return actions