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.

338 lines
14 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, except_orm
  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 and
  43. 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(self, 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, self.right_partner_id.id, context=context)
  78. # type_selection_id
  79. values['type_selection_id'] = (
  80. ((self.type_id.id) * 10) + (on_right_partner and 1 or 0))
  81. # partner_id_display
  82. values['partner_id_display'] = (
  83. self.left_partner_id.id
  84. if on_right_partner
  85. else self.right_partner_id.id
  86. )
  87. # is_relation_expired
  88. today = fields.date.context_today(self, cr, uid, context=context)
  89. values['is_relation_expired'] = (
  90. self.date_end and (self.date_end < today))
  91. # is_relation_future
  92. values['is_relation_future'] = self.date_start > today
  93. return values
  94. return dict([
  95. (i.id, get_values(i, field_names, arg, context=context))
  96. for i in self.browse(cr, uid, ids, context=context)
  97. ])
  98. def write(self, cr, uid, ids, vals, context=None):
  99. '''Override write to correct values, before being stored.'''
  100. vals = self._correct_vals(cr, uid, vals, context=context)
  101. return super(ResPartnerRelation, self).write(
  102. cr, uid, ids, vals, context=context)
  103. def create(self, cr, uid, vals, context=None):
  104. '''Override create to correct values, before being stored.'''
  105. vals = self._correct_vals(cr, uid, vals, context=context)
  106. return super(ResPartnerRelation, self).create(
  107. cr, uid, vals, context=context)
  108. def on_change_type_selection_id(
  109. self, cr, uid, dummy_ids, type_selection_id, context=None):
  110. '''Set domain on partner_id_display, when selection a relation type'''
  111. result = {
  112. 'domain': {'partner_id_display': []},
  113. 'value': {'type_id': False}
  114. }
  115. if not type_selection_id:
  116. return result
  117. prts_model = self.pool['res.partner.relation.type.selection']
  118. type_model = self.pool['res.partner.relation.type']
  119. (type_id, is_reverse) = (
  120. prts_model.get_type_from_selection_id(
  121. cr, uid, type_selection_id)
  122. )
  123. result['value']['type_id'] = type_id
  124. type_obj = type_model.browse(cr, uid, type_id, context=context)
  125. partner_domain = []
  126. check_contact_type = type_obj.contact_type_right
  127. check_partner_category = (
  128. type_obj.partner_category_right and
  129. type_obj.partner_category_right.id
  130. )
  131. if is_reverse:
  132. # partner_id_display is left partner
  133. check_contact_type = type_obj.contact_type_left
  134. check_partner_category = (
  135. type_obj.partner_category_left and
  136. type_obj.partner_category_left.id
  137. )
  138. if check_contact_type == 'c':
  139. partner_domain.append(('is_company', '=', True))
  140. if check_contact_type == 'p':
  141. partner_domain.append(('is_company', '=', False))
  142. if check_partner_category:
  143. partner_domain.append(
  144. ('category_id', 'child_of', check_partner_category))
  145. result['domain']['partner_id_display'] = partner_domain
  146. return result
  147. _columns = {
  148. 'left_partner_id': fields.many2one(
  149. 'res.partner', string='Left partner', required=True,
  150. auto_join=True, ondelete='cascade'),
  151. 'right_partner_id': fields.many2one(
  152. 'res.partner', string='Right partner', required=True,
  153. auto_join=True, ondelete='cascade'),
  154. 'type_id': fields.many2one(
  155. 'res.partner.relation.type', string='Type', required=True,
  156. auto_join=True),
  157. 'date_start': fields.date('Starting date'),
  158. 'date_end': fields.date('Ending date'),
  159. 'type_selection_id': fields.function(
  160. _get_computed_fields,
  161. multi="computed_fields",
  162. fnct_inv=lambda *args: None,
  163. type='many2one', obj='res.partner.relation.type.selection',
  164. string='Type',
  165. ),
  166. 'partner_id_display': fields.function(
  167. _get_computed_fields,
  168. multi="computed_fields",
  169. fnct_inv=lambda *args: None,
  170. type='many2one', obj='res.partner',
  171. string='Partner'
  172. ),
  173. 'is_relation_expired': fields.function(
  174. _get_computed_fields,
  175. multi="computed_fields",
  176. type='boolean',
  177. method=True,
  178. string='Relation is expired',
  179. ),
  180. 'is_relation_future': fields.function(
  181. _get_computed_fields,
  182. multi="computed_fields",
  183. type='boolean',
  184. method=True,
  185. string='Relation is in the future',
  186. ),
  187. 'active': fields.boolean('Active'),
  188. }
  189. _defaults = {
  190. 'active': True,
  191. }
  192. def _check_dates(self, cr, uid, ids, context=None):
  193. '''End date should not be before start date, if noth filled'''
  194. for line in self.browse(cr, uid, ids, context=context):
  195. if line.date_start and line.date_end:
  196. if line.date_start > line.date_end:
  197. return False
  198. return True
  199. def _check_partner_type_left(self, cr, uid, ids, context=None):
  200. '''Check left partner for required company or person'''
  201. for this in self.browse(cr, uid, ids, context=context):
  202. ptype = this.type_id.contact_type_left
  203. company = this.left_partner_id.is_company
  204. if (ptype == 'c' and not company) or (ptype == 'p' and company):
  205. return False
  206. return True
  207. def _check_partner_type_right(self, cr, uid, ids, context=None):
  208. '''Check right partner for required company or person'''
  209. for this in self.browse(cr, uid, ids, context=context):
  210. ptype = this.type_id.contact_type_right
  211. company = this.right_partner_id.is_company
  212. if (ptype == 'c' and not company) or (ptype == 'p' and company):
  213. return False
  214. return True
  215. def _check_not_with_self(self, cr, uid, ids, context=None):
  216. '''Not allowed to link partner to same partner'''
  217. for this in self.browse(cr, uid, ids, context=context):
  218. if this.left_partner_id == this.right_partner_id:
  219. return False
  220. return True
  221. def _check_relation_uniqueness(self, cr, uid, ids, context=None):
  222. '''Forbid multiple active relations of the same type between the same
  223. partners'''
  224. for this in self.browse(cr, uid, ids, context=context):
  225. if not this.active:
  226. continue
  227. domain = [
  228. ('type_id', '=', this.type_id.id),
  229. ('active', '=', True),
  230. ('id', '!=', this.id),
  231. ('left_partner_id', '=', this.left_partner_id.id),
  232. ('right_partner_id', '=', this.right_partner_id.id),
  233. ]
  234. if this.date_start:
  235. domain += ['|', ('date_end', '=', False),
  236. ('date_end', '>=', this.date_start)]
  237. if this.date_end:
  238. domain += ['|', ('date_start', '=', False),
  239. ('date_start', '<=', this.date_end)]
  240. if self.search(cr, uid, domain, context=context):
  241. raise except_orm(
  242. _('Overlapping relation'),
  243. _('There is already a similar relation '
  244. 'with overlapping dates'))
  245. return True
  246. _constraints = [
  247. (
  248. _check_dates,
  249. 'The starting date cannot be after the ending date.',
  250. ['date_start', 'date_end']
  251. ),
  252. (
  253. _check_partner_type_left,
  254. 'The left partner is not applicable for this relation type.',
  255. ['left_partner_id', 'type_id']
  256. ),
  257. (
  258. _check_partner_type_right,
  259. 'The right partner is not applicable for this relation type.',
  260. ['right_partner_id', 'type_id']
  261. ),
  262. (
  263. _check_not_with_self,
  264. 'Partners cannot have a relation with themselves.',
  265. ['left_partner_id', 'right_partner_id']
  266. ),
  267. (
  268. _check_relation_uniqueness,
  269. "The same relation can't be created twice.",
  270. ['left_partner_id', 'right_partner_id', 'active']
  271. )
  272. ]
  273. def get_action_related_partners(self, cr, uid, ids, context=None):
  274. '''return a window action showing a list of partners taking part in the
  275. relations names by ids. Context key 'partner_relations_show_side'
  276. determines if we show 'left' side, 'right' side or 'all' (default)
  277. partners.
  278. If active_model is res.partner.relation.all, left=this and
  279. right=other'''
  280. if context is None:
  281. context = {}
  282. field_names = {}
  283. if context.get('active_model', self._name) == self._name:
  284. field_names = {
  285. 'left': ['left'],
  286. 'right': ['right'],
  287. 'all': ['left', 'right']
  288. }
  289. elif context.get('active_model') == 'res.partner.relation.all':
  290. field_names = {
  291. 'left': ['this'],
  292. 'right': ['other'],
  293. 'all': ['this', 'other']
  294. }
  295. else:
  296. assert False, 'Unknown active_model!'
  297. partner_ids = []
  298. field_names = field_names[
  299. context.get('partner_relations_show_side', 'all')]
  300. field_names = ['%s_partner_id' % n for n in field_names]
  301. for relation in self.pool[context.get('active_model')].read(
  302. cr, uid, ids, context=context, load='_classic_write'):
  303. for name in field_names:
  304. partner_ids.append(relation[name])
  305. return {
  306. 'name': _('Related partners'),
  307. 'type': 'ir.actions.act_window',
  308. 'res_model': 'res.partner',
  309. 'domain': [('id', 'in', partner_ids)],
  310. 'views': [(False, 'tree'), (False, 'form')],
  311. 'view_type': 'form'
  312. }