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.

238 lines
9.7 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. # Person specific fields
  60. # add a 'birthdate' as date field, i.e different from char
  61. # 'birthdate' introduced v6.1!
  62. 'birthdate_date': fields.date('Birthdate'),
  63. 'nationality_id': fields.many2one('res.country', 'Nationality'),
  64. }
  65. _defaults = {
  66. 'contact_type': 'standalone',
  67. }
  68. def _basecontact_check_context(self, cr, user, mode, context=None):
  69. """ Remove 'search_show_all_positions' for non-search mode.
  70. Keeping it in context can result in unexpected behaviour (ex: reading
  71. one2many might return wrong result - i.e with "attached contact"
  72. removed even if it's directly linked to a company).
  73. """
  74. context = dict(context or {})
  75. if mode != 'search':
  76. context.pop('search_show_all_positions', None)
  77. return context
  78. def search(
  79. self, cr, user, args, offset=0, limit=None, order=None,
  80. context=None, count=False):
  81. """ Display only standalone contact matching ``args`` or having
  82. attached contact matching ``args`` """
  83. if context is None:
  84. context = {}
  85. if context.get('search_show_all_positions') is False:
  86. args = expression.normalize_domain(args)
  87. attached_contact_args = expression.AND(
  88. (args, [('contact_type', '=', 'attached')])
  89. )
  90. attached_contact_ids = super(res_partner, self).search(
  91. cr, user, attached_contact_args, context=context
  92. )
  93. args = expression.OR((
  94. expression.AND(([('contact_type', '=', 'standalone')], args)),
  95. [('other_contact_ids', 'in', attached_contact_ids)],
  96. ))
  97. return super(res_partner, self).search(
  98. cr, user, args, offset=offset, limit=limit, order=order,
  99. context=context, count=count
  100. )
  101. def create(self, cr, user, vals, context=None):
  102. context = self._basecontact_check_context(cr, user, 'create', context)
  103. if not vals.get('name') and vals.get('contact_id'):
  104. vals['name'] = self.browse(
  105. cr, user, vals['contact_id'], context=context).name
  106. return super(res_partner, self).create(cr, user, vals, context=context)
  107. def read(
  108. self, cr, user, ids, fields=None, context=None,
  109. load='_classic_read'):
  110. context = self._basecontact_check_context(cr, user, 'read', context)
  111. return super(res_partner, self).read(
  112. cr, user, ids, fields=fields, context=context, load=load)
  113. def write(self, cr, user, ids, vals, context=None):
  114. context = self._basecontact_check_context(cr, user, 'write', context)
  115. return super(
  116. res_partner, self).write(cr, user, ids, vals, context=context)
  117. def unlink(self, cr, user, ids, context=None):
  118. context = self._basecontact_check_context(cr, user, 'unlink', context)
  119. return super(res_partner, self).unlink(cr, user, ids, context=context)
  120. def _commercial_partner_compute(
  121. self, cr, uid, ids, name, args, context=None):
  122. """ Returns the partner that is considered the commercial
  123. entity of this partner. The commercial entity holds the master data
  124. for all commercial fields (see :py:meth:`~_commercial_fields`) """
  125. result = super(res_partner, self)._commercial_partner_compute(
  126. cr, uid, ids, name, args, context=context)
  127. for partner in self.browse(cr, uid, ids, context=context):
  128. if partner.contact_type == 'attached' and not partner.parent_id:
  129. result[partner.id] = partner.contact_id.id
  130. return result
  131. def _contact_fields(self, cr, uid, context=None):
  132. """ Returns the list of contact fields that are synced from the parent
  133. when a partner is attached to him. """
  134. return ['name', 'title']
  135. def _contact_sync_from_parent(self, cr, uid, partner, context=None):
  136. """ Handle sync of contact fields when a new parent contact entity
  137. is set, as if they were related fields
  138. """
  139. if partner.contact_id:
  140. contact_fields = self._contact_fields(cr, uid, context=context)
  141. sync_vals = self._update_fields_values(
  142. cr, uid, partner.contact_id, contact_fields, context=context
  143. )
  144. partner.write(sync_vals)
  145. def update_contact(self, cr, uid, ids, vals, context=None):
  146. if context is None:
  147. context = {}
  148. if context.get('__update_contact_lock'):
  149. return
  150. contact_fields = self._contact_fields(cr, uid, context=context)
  151. contact_vals = dict(
  152. (field, vals[field]) for field in contact_fields if field in vals
  153. )
  154. if contact_vals:
  155. ctx = dict(context, __update_contact_lock=True)
  156. self.write(cr, uid, ids, contact_vals, context=ctx)
  157. def _fields_sync(self, cr, uid, partner, update_values, context=None):
  158. """Sync commercial fields and address fields from company and to
  159. children, contact fields from contact and to attached contact
  160. after create/update, just as if those were all modeled as
  161. fields.related to the parent
  162. """
  163. super(res_partner, self)._fields_sync(
  164. cr, uid, partner, update_values, context=context
  165. )
  166. contact_fields = self._contact_fields(cr, uid, context=context)
  167. # 1. From UPSTREAM: sync from parent contact
  168. if update_values.get('contact_id'):
  169. self._contact_sync_from_parent(cr, uid, partner, context=context)
  170. # 2. To DOWNSTREAM: sync contact fields to parent or related
  171. elif any(field in contact_fields for field in update_values):
  172. update_ids = [
  173. c.id for c in partner.other_contact_ids if not c.is_company
  174. ]
  175. if partner.contact_id:
  176. update_ids.append(partner.contact_id.id)
  177. self.update_contact(
  178. cr, uid, update_ids, update_values, context=context
  179. )
  180. def onchange_contact_id(self, cr, uid, ids, contact_id, context=None):
  181. values = {}
  182. if contact_id:
  183. values['name'] = self.browse(
  184. cr, uid, contact_id, context=context).name
  185. return {'value': values}
  186. def onchange_contact_type(self, cr, uid, ids, contact_type, context=None):
  187. values = {}
  188. if contact_type == 'standalone':
  189. values['contact_id'] = False
  190. return {'value': values}
  191. class ir_actions_window(orm.Model):
  192. _inherit = 'ir.actions.act_window'
  193. def read(
  194. self, cr, user, ids, fields=None, context=None,
  195. load='_classic_read'):
  196. action_ids = ids
  197. if isinstance(ids, (int, long)):
  198. action_ids = [ids]
  199. actions = super(ir_actions_window, self).read(
  200. cr, user, action_ids, fields=fields, context=context, load=load
  201. )
  202. for action in actions:
  203. if action.get('res_model', '') == 'res.partner':
  204. # By default, only show standalone contact
  205. action_context = action.get('context', '{}') or '{}'
  206. if 'search_show_all_positions' not in action_context:
  207. action['context'] = action_context.replace(
  208. '{', "{'search_show_all_positions': False,", 1
  209. )
  210. if isinstance(ids, (int, long)):
  211. if actions:
  212. return actions[0]
  213. return False
  214. return actions