# -*- encoding: utf-8 -*- from openerp import models, fields, api, _ from openerp.exceptions import Warning from pytz import timezone from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT, DEFAULT_SERVER_DATE_FORMAT from datetime import datetime,timedelta from dateutil.relativedelta import relativedelta from openerp.tools import openerp import base64 DATE_FORMAT = '%Y-%m-%d' class res_partner_birthday(models.Model): _name = 'res.partner.birthday' partner_id = fields.Many2one('res.partner', string="Socio", ondelete="cascade") partner_name = fields.Char(related='partner_id.name', store=True, string="Nombre") partner_birthday = fields.Date(related='partner_id.fecha_nac',store=True, string="Fecha Nac") partner_picture = fields.Binary(related='partner_id.image_medium') partner_year = fields.Integer(string="Edad") partner_year_string = fields.Char(string="Mensaje") birthday_date = fields.Date('Cumpleaños') @api.model def _get_default_image2(self): image = open(openerp.modules.get_module_resource('birthday_calendar', 'static/src/img', 'globo1.png'),'rb') return base64.b64encode(image.read()) photo = fields.Binary(string="Imagen", default=_get_default_image2) @api.multi def send_whatsapp_msg(self): company = self.env.user.company_id if self.partner_id.mobile: mensaje = "Feliz cumpleaños.! Muchas felicidades de parte de todo el equipo de " + str(company.name) mensaje_no_space = "%20".join(mensaje.split()) url = "https://api.whatsapp.com/send?phone=" + str(self.partner_id.mobile) + "&text=" + mensaje_no_space return {'type': 'ir.actions.act_url', 'url': url, 'target': 'new'} else: raise Warning(_("No existe teléfono móvil definido")) class res_partner(models.Model): _inherit = 'res.partner' birthday_dates = fields.One2many('res.partner.birthday', 'partner_id', string="Fechas") @api.model def create(self, vals): ctx = dict(self._context or {}) partner_id = super(res_partner, self.with_context(ctx)).create(vals) if partner_id.fecha_nac: fecha_nac2 = datetime.strptime(partner_id.fecha_nac, DATE_FORMAT) for i in range(100): dates = {} birthday_date = fecha_nac2+relativedelta(years=i) dates = { 'partner_id': partner_id.id, 'birthday_date': birthday_date.strftime("%Y-%m-%d"), 'partner_year': i, 'partner_year_string':'Cumple '+str(i)+' años' } self.env['res.partner.birthday'].create(dates) return partner_id @api.multi def write(self, vals): if vals.get('fecha_nac') and self.fecha_nac and self.fecha_nac != vals.get('fecha_nac'): ids = self.env['res.partner.birthday'].search([('partner_id','=',self.id)]) for id in ids: self.env.cr.execute("DELETE FROM res_partner_birthday WHERE id= %s" % (id.id)) fecha_nac2 = datetime.strptime(vals['fecha_nac'], DATE_FORMAT) for i in range(100): dates = {} birthday_date = fecha_nac2+relativedelta(years=i) dates = { 'partner_id': self.id, 'birthday_date': birthday_date.strftime("%Y-%m-%d"), 'partner_year': i, 'partner_year_string':'Cumple '+str(i)+' años' } self.env['res.partner.birthday'].create(dates) if vals.get('fecha_nac') and not self.fecha_nac: fecha_nac2 = datetime.strptime(vals['fecha_nac'], DATE_FORMAT) for i in range(100): dates = {} birthday_date = fecha_nac2+relativedelta(years=i) dates = { 'partner_id': self.id, 'birthday_date': birthday_date.strftime("%Y-%m-%d"), 'partner_year': i, 'partner_year_string':'Cumple '+str(i)+' años' } self.env['res.partner.birthday'].create(dates) if self.fecha_nac and vals.get('fecha_nac') == False: ids = self.env['res.partner.birthday'].search([('partner_id','=',self.id)]) for id in ids: self.env.cr.execute("DELETE FROM res_partner_birthday WHERE id= %s" % (id.id)) update = super(res_partner, self).write(vals) return update