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.

82 lines
3.6 KiB

  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2013 Savoir-faire Linux
  6. # (<http://www.savoirfairelinux.com>).
  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. import time
  23. from openerp.osv import orm
  24. class res_partner(orm.Model):
  25. _inherit = 'res.partner'
  26. def create(self, cr, user, vals, context=None):
  27. context = self._basecontact_check_context(cr, user, 'create', context)
  28. if not vals.get('name') and vals.get('contact_id'):
  29. vals['name'] = self.browse(
  30. cr, user, vals['contact_id'], context=context).name
  31. if vals.get('end_date'):
  32. if vals.get('end_date', "Z") <= time.strftime('%Y-%m-%d'):
  33. vals['active'] = False
  34. if vals.get('contact_type') == 'standalone':
  35. contact_vals = dict(
  36. filter(lambda (k, v): k != 'parent_id', vals.iteritems())
  37. )
  38. contact_vals['active'] = True
  39. contact_vals['function_id'] = None
  40. vals['contact_id'] = super(res_partner, self).create(
  41. cr, user, contact_vals, context=context
  42. )
  43. self.write(cr, user, vals['contact_id'], {
  44. 'firstname': vals.get('firstname', ''),
  45. 'lastname': vals.get('lastname', ''),
  46. }, context=context)
  47. # Check if we create existing contact from company(ie: company is true)
  48. # Check if we create another function from contact view
  49. if vals.get('contact_type') == 'attached' or (
  50. not vals.get('contact_type') and vals.get('contact_id')):
  51. contact_info = self.browse(
  52. cr, user, vals.get('contact_id'), context=context)
  53. vals['firstname'] = contact_info.firstname
  54. vals['title'] = contact_info.title.id or ''
  55. if ('contact_id'in vals and ('reset_password' in context or
  56. 'install_mode' in context)):
  57. return vals['contact_id']
  58. res = super(res_partner, self).create(cr, user, vals, context=context)
  59. return res
  60. def write(self, cr, user, ids, vals, context=None):
  61. context = self._basecontact_check_context(cr, user, 'write', context)
  62. if vals.get('end_date'):
  63. if vals.get('end_date', "Z") <= time.strftime('%Y-%m-%d'):
  64. vals['active'] = False
  65. if vals.get('contact_type') == 'standalone':
  66. contact_vals = dict(
  67. filter(lambda (k, v): k != 'parent_id', vals.iteritems())
  68. )
  69. contact_vals['active'] = True
  70. contact_vals['function_id'] = None
  71. vals['contact_id'] = super(res_partner, self).write(
  72. cr, user, contact_vals, context=context
  73. )
  74. return super(res_partner, self).write(
  75. cr, user, ids, vals, context=context
  76. )