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.

330 lines
13 KiB

  1. # -*- coding: utf-8 -*-
  2. '''Define model res.partner.relation'''
  3. ##############################################################################
  4. #
  5. # OpenERP, Open Source Management Solution
  6. # This module copyright (C) 2013 Therp BV (<http://therp.nl>).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Affero General Public License as
  10. # published by the Free Software Foundation, either version 3 of the
  11. # License, or (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Affero General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Affero General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ##############################################################################
  22. from openerp.osv.orm import Model
  23. from openerp.osv import fields
  24. from openerp.tools.translate import _
  25. class ResPartnerRelation(Model):
  26. '''Model res.partner.relation is used to describe all links or relations
  27. between partners in the database.
  28. In many parts of the code we have to know whether the active partner is
  29. the left partner, or the right partner. If the active partner is the
  30. right partner we have to show the inverse name.
  31. Because the active partner is crucial for the working of partner
  32. relationships, we make sure on the res.partner model that the partner id
  33. is set in the context where needed.
  34. '''
  35. _name = 'res.partner.relation'
  36. _description = 'Partner relation'
  37. _order = 'active desc, date_start desc, date_end desc'
  38. def _on_right_partner(self, cr, uid, right_partner_id, context=None):
  39. '''Determine wether functions are called in a situation where the
  40. active partner is the right partner. Default False!
  41. '''
  42. if (context and 'active_ids' in context
  43. and right_partner_id in context.get('active_ids', [])):
  44. return True
  45. return False
  46. def _correct_vals(self, cr, uid, vals, context=None):
  47. '''Fill type and left and right partner id, according to wether
  48. we have a normal relation type or an inverse relation type'''
  49. vals = vals.copy()
  50. # If type_selection_id ends in 1, it is a reverse relation type
  51. if 'type_selection_id' in vals:
  52. prts_model = self.pool['res.partner.relation.type.selection']
  53. type_selection_id = vals['type_selection_id']
  54. (type_id, is_reverse) = (
  55. prts_model.get_type_from_selection_id(
  56. cr, uid, type_selection_id))
  57. vals['type_id'] = type_id
  58. if context.get('active_id'):
  59. if is_reverse:
  60. vals['right_partner_id'] = context['active_id']
  61. else:
  62. vals['left_partner_id'] = context['active_id']
  63. if vals.get('partner_id_display'):
  64. if is_reverse:
  65. vals['left_partner_id'] = vals['partner_id_display']
  66. else:
  67. vals['right_partner_id'] = vals['partner_id_display']
  68. return vals
  69. def _get_computed_fields(
  70. self, cr, uid, ids, field_names, arg, context=None):
  71. '''Return a dictionary of dictionaries, with for every partner for
  72. ids, the computed values.'''
  73. def get_values(this, dummy_field_names, dummy_arg, context=None):
  74. '''Get computed values for record'''
  75. values = {}
  76. on_right_partner = self._on_right_partner(
  77. cr, uid, this.right_partner_id.id, context=context)
  78. # type_selection_id
  79. values['type_selection_id'] = (
  80. ((this.type_id.id) * 10) + (on_right_partner and 1 or 0))
  81. # partner_id_display
  82. values['partner_id_display'] = (
  83. on_right_partner and this.left_partner_id.id
  84. or this.right_partner_id.id
  85. )
  86. # is_relation_expired
  87. today = fields.date.context_today(self, cr, uid, context=context)
  88. values['is_relation_expired'] = (
  89. this.date_end and (this.date_end < today))
  90. # is_relation_future
  91. values['is_relation_future'] = this.date_start > today
  92. return values
  93. return dict([
  94. (this.id, get_values(this, field_names, arg, context=context))
  95. for this in self.browse(cr, uid, ids, context=context)
  96. ])
  97. def write(self, cr, uid, ids, vals, context=None):
  98. '''Override write to correct values, before being stored.'''
  99. vals = self._correct_vals(cr, uid, vals, context=context)
  100. return super(ResPartnerRelation, self).write(
  101. cr, uid, ids, vals, context=context)
  102. def create(self, cr, uid, vals, context=None):
  103. '''Override create to correct values, before being stored.'''
  104. vals = self._correct_vals(cr, uid, vals, context=context)
  105. return super(ResPartnerRelation, self).create(
  106. cr, uid, vals, context=context)
  107. def on_change_type_selection_id(
  108. self, cr, uid, dummy_ids, type_selection_id, context=None):
  109. '''Set domain on partner_id_display, when selection a relation type'''
  110. result = {
  111. 'domain': {'partner_id_display': []},
  112. 'value': {'type_id': False}
  113. }
  114. if not type_selection_id:
  115. return result
  116. prts_model = self.pool['res.partner.relation.type.selection']
  117. type_model = self.pool['res.partner.relation.type']
  118. (type_id, is_reverse) = (
  119. prts_model.get_type_from_selection_id(
  120. cr, uid, type_selection_id)
  121. )
  122. result['value']['type_id'] = type_id
  123. type_obj = type_model.browse(cr, uid, type_id, context=context)
  124. partner_domain = []
  125. check_contact_type = type_obj.contact_type_right
  126. check_partner_category = (
  127. type_obj.partner_category_right and
  128. type_obj.partner_category_right.id
  129. )
  130. if is_reverse:
  131. # partner_id_display is left partner
  132. check_contact_type = type_obj.contact_type_left
  133. check_partner_category = (
  134. type_obj.partner_category_left and
  135. type_obj.partner_category_left.id
  136. )
  137. if check_contact_type == 'c':
  138. partner_domain.append(('is_company', '=', True))
  139. if check_contact_type == 'p':
  140. partner_domain.append(('is_company', '=', False))
  141. if check_partner_category:
  142. partner_domain.append(
  143. ('category_id', 'child_of', check_partner_category))
  144. result['domain']['partner_id_display'] = partner_domain
  145. return result
  146. _columns = {
  147. 'left_partner_id': fields.many2one(
  148. 'res.partner', string='Left partner', required=True,
  149. auto_join=True, ondelete='cascade'),
  150. 'right_partner_id': fields.many2one(
  151. 'res.partner', string='Right partner', required=True,
  152. auto_join=True, ondelete='cascade'),
  153. 'type_id': fields.many2one(
  154. 'res.partner.relation.type', string='Type', required=True,
  155. auto_join=True),
  156. 'date_start': fields.date('Starting date'),
  157. 'date_end': fields.date('Ending date'),
  158. 'type_selection_id': fields.function(
  159. _get_computed_fields,
  160. multi="computed_fields",
  161. fnct_inv=lambda *args: None,
  162. type='many2one', obj='res.partner.relation.type.selection',
  163. string='Type',
  164. ),
  165. 'partner_id_display': fields.function(
  166. _get_computed_fields,
  167. multi="computed_fields",
  168. fnct_inv=lambda *args: None,
  169. type='many2one', obj='res.partner',
  170. string='Partner'
  171. ),
  172. 'is_relation_expired': fields.function(
  173. _get_computed_fields,
  174. multi="computed_fields",
  175. type='boolean',
  176. method=True,
  177. string='Relation is expired',
  178. ),
  179. 'is_relation_future': fields.function(
  180. _get_computed_fields,
  181. multi="computed_fields",
  182. type='boolean',
  183. method=True,
  184. string='Relation is in the future',
  185. ),
  186. 'active': fields.boolean('Active'),
  187. }
  188. _defaults = {
  189. 'active': True,
  190. }
  191. def _check_dates(self, cr, uid, ids, context=None):
  192. '''End date should not be before start date, if noth filled'''
  193. for line in self.browse(cr, uid, ids, context=context):
  194. if line.date_start and line.date_end:
  195. if line.date_start > line.date_end:
  196. return False
  197. return True
  198. def _check_partner_type_left(self, cr, uid, ids, context=None):
  199. '''Check left partner for required company or person'''
  200. for this in self.browse(cr, uid, ids, context=context):
  201. ptype = this.type_id.contact_type_left
  202. company = this.left_partner_id.is_company
  203. if (ptype == 'c' and not company) or (ptype == 'p' and company):
  204. return False
  205. return True
  206. def _check_partner_type_right(self, cr, uid, ids, context=None):
  207. '''Check right partner for required company or person'''
  208. for this in self.browse(cr, uid, ids, context=context):
  209. ptype = this.type_id.contact_type_right
  210. company = this.right_partner_id.is_company
  211. if (ptype == 'c' and not company) or (ptype == 'p' and company):
  212. return False
  213. return True
  214. def _check_not_with_self(self, cr, uid, ids, context=None):
  215. '''Not allowed to link partner to same partner'''
  216. for this in self.browse(cr, uid, ids, context=context):
  217. if this.left_partner_id == this.right_partner_id:
  218. return False
  219. return True
  220. def _check_relation_uniqueness(self, cr, uid, ids, context=None):
  221. '''Forbid multiple active relations of the same type between the same
  222. partners'''
  223. for this in self.browse(cr, uid, ids, context=context):
  224. if not this.active:
  225. continue
  226. if self.search(
  227. cr, uid,
  228. [
  229. ('type_id', '=', this.type_id.id),
  230. ('active', '=', True),
  231. ('id', '!=', this.id),
  232. ('left_partner_id', '=', this.left_partner_id.id),
  233. ('right_partner_id', '=', this.right_partner_id.id),
  234. ],
  235. context=context):
  236. return False
  237. return True
  238. _constraints = [
  239. (
  240. _check_dates,
  241. 'The starting date cannot be after the ending date.',
  242. ['date_start', 'date_end']
  243. ),
  244. (
  245. _check_partner_type_left,
  246. 'The left partner is not applicable for this relation type.',
  247. ['left_partner_id', 'type_id']
  248. ),
  249. (
  250. _check_partner_type_right,
  251. 'The right partner is not applicable for this relation type.',
  252. ['right_partner_id', 'type_id']
  253. ),
  254. (
  255. _check_not_with_self,
  256. 'Partners cannot have a relation with themselves.',
  257. ['left_partner_id', 'right_partner_id']
  258. ),
  259. (
  260. _check_relation_uniqueness,
  261. "The same relation can't be created twice.",
  262. ['left_partner_id', 'right_partner_id', 'active']
  263. )
  264. ]
  265. def get_action_related_partners(self, cr, uid, ids, context=None):
  266. '''return a window action showing a list of partners taking part in the
  267. relations names by ids. Context key 'partner_relations_show_side'
  268. determines if we show 'left' side, 'right' side or 'all' (default)
  269. partners.
  270. If active_model is res.partner.relation.all, left=this and
  271. right=other'''
  272. if context is None:
  273. context = {}
  274. field_names = {}
  275. if context.get('active_model', self._name) == self._name:
  276. field_names = {
  277. 'left': ['left'],
  278. 'right': ['right'],
  279. 'all': ['left', 'right']
  280. }
  281. elif context.get('active_model') == 'res.partner.relation.all':
  282. field_names = {
  283. 'left': ['this'],
  284. 'right': ['other'],
  285. 'all': ['this', 'other']
  286. }
  287. else:
  288. assert False, 'Unknown active_model!'
  289. partner_ids = []
  290. field_names = field_names[
  291. context.get('partner_relations_show_side', 'all')]
  292. field_names = ['%s_partner_id' % n for n in field_names]
  293. for relation in self.pool[context.get('active_model')].read(
  294. cr, uid, ids, context=context, load='_classic_write'):
  295. for name in field_names:
  296. partner_ids.append(relation[name])
  297. return {
  298. 'name': _('Related partners'),
  299. 'type': 'ir.actions.act_window',
  300. 'res_model': 'res.partner',
  301. 'domain': [('id', 'in', partner_ids)],
  302. 'views': [(False, 'tree'), (False, 'form')],
  303. 'view_type': 'form'
  304. }