res_partner_birthday.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # -*- encoding: utf-8 -*-
  2. from openerp import models, fields, api, _
  3. from openerp.exceptions import Warning
  4. from pytz import timezone
  5. from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT
  6. from datetime import datetime,timedelta
  7. from dateutil.relativedelta import relativedelta
  8. from openerp.tools import openerp
  9. import base64
  10. DATE_FORMAT = '%Y-%m-%d'
  11. class res_partner_birthday(models.Model):
  12. _name = 'res.partner.birthday'
  13. partner_id = fields.Many2one('res.partner', string="Socio", ondelete="cascade")
  14. partner_name = fields.Char(related='partner_id.name', store=True, string="Nombre")
  15. partner_birthday = fields.Date(related='partner_id.fecha_nac',store=True, string="Fecha Nac")
  16. partner_picture = fields.Binary(related='partner_id.image_medium')
  17. partner_year = fields.Integer(string="Edad")
  18. partner_year_string = fields.Char(string="Mensaje")
  19. birthday_date = fields.Date('Cumpleaños')
  20. @api.model
  21. def _get_default_image2(self):
  22. image = open(openerp.modules.get_module_resource('birthday_calendar', 'static/src/img', 'globo1.png'),'rb')
  23. return base64.b64encode(image.read())
  24. photo = fields.Binary(string="Imagen", default=_get_default_image2)
  25. @api.multi
  26. def send_whatsapp_msg(self):
  27. company = self.env.user.company_id
  28. if self.partner_id.mobile:
  29. mensaje = "Feliz cumpleaños.! Muchas felicidades de parte de todo el equipo de " + str(company.name)
  30. mensaje_no_space = "%20".join(mensaje.split())
  31. url = "https://api.whatsapp.com/send?phone=" + str(self.partner_id.mobile) + "&text=" + mensaje_no_space
  32. return {'type': 'ir.actions.act_url', 'url': url, 'target': 'new'}
  33. else:
  34. raise Warning(_("No existe teléfono móvil definido"))
  35. class res_partner(models.Model):
  36. _inherit = 'res.partner'
  37. birthday_dates = fields.One2many('res.partner.birthday', 'partner_id', string="Fechas")
  38. @api.model
  39. def create(self, vals):
  40. ctx = dict(self._context or {})
  41. partner_id = super(res_partner, self.with_context(ctx)).create(vals)
  42. if partner_id.fecha_nac:
  43. fecha_nac2 = datetime.strptime(partner_id.fecha_nac, DATE_FORMAT)
  44. for i in range(100):
  45. dates = {}
  46. birthday_date = fecha_nac2+relativedelta(years=i)
  47. dates = {
  48. 'partner_id': partner_id.id,
  49. 'birthday_date': birthday_date.strftime("%Y-%m-%d"),
  50. 'partner_year': i,
  51. 'partner_year_string':'Cumple '+str(i)+' años'
  52. }
  53. self.env['res.partner.birthday'].create(dates)
  54. return partner_id
  55. @api.multi
  56. def write(self, vals):
  57. if vals.get('fecha_nac') and self.fecha_nac and self.fecha_nac != vals.get('fecha_nac'):
  58. ids = self.env['res.partner.birthday'].search([('partner_id','=',self.id)])
  59. for id in ids:
  60. self.env.cr.execute("DELETE FROM res_partner_birthday WHERE id= %s" % (id.id))
  61. fecha_nac2 = datetime.strptime(vals['fecha_nac'], DATE_FORMAT)
  62. for i in range(100):
  63. dates = {}
  64. birthday_date = fecha_nac2+relativedelta(years=i)
  65. dates = {
  66. 'partner_id': self.id,
  67. 'birthday_date': birthday_date.strftime("%Y-%m-%d"),
  68. 'partner_year': i,
  69. 'partner_year_string':'Cumple '+str(i)+' años'
  70. }
  71. self.env['res.partner.birthday'].create(dates)
  72. if vals.get('fecha_nac') and not self.fecha_nac:
  73. fecha_nac2 = datetime.strptime(vals['fecha_nac'], DATE_FORMAT)
  74. for i in range(100):
  75. dates = {}
  76. birthday_date = fecha_nac2+relativedelta(years=i)
  77. dates = {
  78. 'partner_id': self.id,
  79. 'birthday_date': birthday_date.strftime("%Y-%m-%d"),
  80. 'partner_year': i,
  81. 'partner_year_string':'Cumple '+str(i)+' años'
  82. }
  83. self.env['res.partner.birthday'].create(dates)
  84. if self.fecha_nac and vals.get('fecha_nac') == False:
  85. ids = self.env['res.partner.birthday'].search([('partner_id','=',self.id)])
  86. for id in ids:
  87. self.env.cr.execute("DELETE FROM res_partner_birthday WHERE id= %s" % (id.id))
  88. update = super(res_partner, self).write(vals)
  89. return update