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.

102 lines
3.9 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2014 Therp BV (<http://therp.nl>).
  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.orm import Model
  22. from openerp.osv import fields
  23. from openerp.tools import drop_view_if_exists
  24. from res_partner_relation_type_selection import ResPartnerRelationTypeSelection
  25. class ResPartnerRelationAll(Model):
  26. _auto = False
  27. _log_access = False
  28. _name = 'res.partner.relation.all'
  29. _description = 'All (non-inverse + inverse) relations between partners'
  30. def _auto_init(self, cr, context=None):
  31. drop_view_if_exists(cr, self._table)
  32. cr.execute(
  33. '''create or replace view %s as
  34. select
  35. id * 10 as id,
  36. id as relation_id,
  37. type_id,
  38. cast('a' as char(1)) as record_type,
  39. left_partner_id as this_partner_id,
  40. right_partner_id as other_partner_id,
  41. date_start,
  42. date_end,
  43. active,
  44. type_id * 10 as type_selection_id
  45. from res_partner_relation
  46. union select
  47. id * 10 + 1,
  48. id,
  49. type_id,
  50. cast('b' as char(1)),
  51. right_partner_id,
  52. left_partner_id,
  53. date_start,
  54. date_end,
  55. active,
  56. type_id * 10 + 1
  57. from res_partner_relation''' % self._table)
  58. return super(ResPartnerRelationAll, self)._auto_init(
  59. cr, context=context)
  60. _columns = {
  61. 'record_type': fields.selection(
  62. ResPartnerRelationTypeSelection._RECORD_TYPES, 'Record type',
  63. readonly=True),
  64. 'relation_id': fields.many2one(
  65. 'res.partner.relation', 'Relation', readonly=True),
  66. 'type_id': fields.many2one(
  67. 'res.partner.relation.type', 'Relation type', readonly=True),
  68. 'type_selection_id': fields.many2one(
  69. 'res.partner.relation.type.selection', 'Relation type',
  70. readonly=True),
  71. 'this_partner_id': fields.many2one(
  72. 'res.partner', 'Current partner', readonly=True),
  73. 'other_partner_id': fields.many2one(
  74. 'res.partner', 'Other partner', readonly=True),
  75. 'date_start': fields.date('Starting date'),
  76. 'date_end': fields.date('Ending date'),
  77. 'active': fields.boolean('Active'),
  78. }
  79. def name_get(self, cr, uid, ids, context=None):
  80. return dict([
  81. (this.id, '%s %s %s' % (
  82. this.this_partner_id.name,
  83. this.type_selection_id.name_get()[0][1],
  84. this.other_partner_id.name,
  85. ))
  86. for this in self.browse(cr, uid, ids, context=context)])
  87. def write(self, cr, uid, ids, vals, context=None):
  88. '''divert non-problematic writes to underlying table'''
  89. return self.pool['res.partner.relation'].write(
  90. cr, uid,
  91. [i / 10 for i in ids],
  92. dict([(k, vals[k])
  93. for k in vals
  94. if not self._columns[k].readonly]),
  95. context=context)