diff --git a/models/calendar.py b/models/calendar.py index 0635c8d3358eaddb5fc21da29a6debf8712eef1f..7eee2bb42aacb086c5b12ed84fd9c10a32c76872 100644 --- a/models/calendar.py +++ b/models/calendar.py @@ -1,6 +1,9 @@ # © 2019 Le Filament (<http://www.le-filament.com>) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import pytz +from datetime import timedelta + from odoo import models, fields, api from odoo.exceptions import UserError @@ -116,18 +119,58 @@ class CGScopCalendar(models.Model): raise UserError("Le code activité UR doit être \ renseigné sur chaque entrée d'agenda") else: - self.env['account.analytic.line'].create({ + values = { 'user_id': self.env.user.id, 'project_id': event.project_id.id, 'account_id': event.project_id.analytic_account_id.id, 'ur_financial_system_id': event.ur_financial_system_id.id, - 'date': event.start, 'name': event.name, 'company_id': self.env.user.company_id.id, - 'unit_amount': event.duration if not event.allday else 8.0, 'partner_id': event.coop_id.id, 'event_id': event.id, - }) + } + # Gestion des évènements sur toute la journée + if event.allday: + # Création d'une ligne de 8h pour chaque jour + for i in range((event.stop - event.start).days + 1): + values['date'] = event.start + timedelta(days=i) + values['unit_amount'] = 8.0 + self.env['account.analytic.line'].create(values) + # Gestion des évènements sur plusieurs jours non flagués allday + if (event.stop - event.start).days > 0: + user_tz = self.env.user.tz + local = pytz.timezone(user_tz) + # Pour chaque jour + for i in range((event.stop - event.start).days + 1): + day = event.start + timedelta(days=i) + # si premier jour calcul heures + if i == 0: + end_day_tz = local.localize( + day.replace(hour=18)) + start_tz = fields.Datetime.context_timestamp( + record=self.env.user, + timestamp=event.start) + hours_cal = (end_day_tz - start_tz).seconds//3600 + hours = hours_cal if hours_cal <= 8 else 8.0 + # si dernier jour + elif i == (event.stop - event.start).days: + start_day_tz = local.localize( + day.replace(hour=8)) + stop_tz = fields.Datetime.context_timestamp( + record=self.env.user, + timestamp=event.stop) + hours_cal = (stop_tz - start_day_tz).seconds//3600 + hours = hours_cal if hours_cal <= 8 else 8.0 + else: + hours = 8.0 + values['date'] = day + values['unit_amount'] = hours + self.env['account.analytic.line'].create(values) + # Gestion des évènements classiques + else: + values['date'] = event.start + values['unit_amount'] = event.duration + self.env['account.analytic.line'].create(values) @api.multi def duplicate_entry(self):