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.

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