diff --git a/yaltik_crm_action/models/calendar_event.py b/yaltik_crm_action/models/calendar_event.py index ffa4162..5774a59 100644 --- a/yaltik_crm_action/models/calendar_event.py +++ b/yaltik_crm_action/models/calendar_event.py @@ -18,7 +18,7 @@ """ Calendar Event adaptations """ -from odoo import models, fields +from odoo import models, fields, api class CalendarEvent(models.Model): @@ -26,3 +26,14 @@ class CalendarEvent(models.Model): _inherit = 'calendar.event' partner_location_id = fields.Many2one('res.partner', 'Partner location') + crm_action_ids = fields.One2many('crm.action', 'event_id') + + @api.multi + def write(self, vals): + """ Ensures date action coherence """ + super(CalendarEvent, self).write(vals) + if 'start' in vals: + for event in self: + if event.crm_action_ids: + event.crm_action_ids.write({'date': event.start}) + return True diff --git a/yaltik_crm_action/models/crm_action.py b/yaltik_crm_action/models/crm_action.py index c10efed..d568154 100644 --- a/yaltik_crm_action/models/crm_action.py +++ b/yaltik_crm_action/models/crm_action.py @@ -18,6 +18,7 @@ """ CRM Action """ +from datetime import timedelta from odoo import models, fields, api, _ @@ -124,6 +125,22 @@ class CrmAction(models.Model): action.event_id = False event.unlink() + @api.multi + def write(self, vals): + """ Ensures date event and basic partner_id coherence """ + super(CrmAction, self).write(vals) + for action in self: + if action.event_id: + if action.event_id.start != action.date: + stop = fields.Datetime.from_string(action.date) + \ + timedelta(hours=action.event_id.duration) + action.event_id.write({'start': action.date, + 'stop': fields.Datetime.to_string(stop)}) + if action.partner_id and \ + action.partner_id not in action.event_id.partner_ids: + action.event_id.partner_ids |= action.partner_id + return True + class CrmActionType(models.Model): """ CRM Action Type """