From 9eb4895b78e71627f1a2a64c405a28d7f4c38a03 Mon Sep 17 00:00:00 2001 From: benjamin <benjamin@le-filament.com> Date: Thu, 15 Sep 2022 18:28:05 +0200 Subject: [PATCH] [update] theoric timesheet hours --- models/ur_month_timesheet.py | 58 +++++++++++++++++------------- static/src/xml/month_timesheet.xml | 54 ++++++++++++++-------------- 2 files changed, 61 insertions(+), 51 deletions(-) diff --git a/models/ur_month_timesheet.py b/models/ur_month_timesheet.py index 2d4d813..10bc441 100644 --- a/models/ur_month_timesheet.py +++ b/models/ur_month_timesheet.py @@ -1,6 +1,10 @@ # © 2019 Le Filament (<http://www.le-filament.com>) # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +from datetime import date + +from dateutil.relativedelta import relativedelta + from odoo import api, fields, models @@ -30,7 +34,7 @@ MONTHS = [ class ScopMonthTimesheet(models.Model): _name = "ur.month.timesheet" _description = "Heures theoriques mensuelles" - _order = "year, month" + _order = "date_timesheet" def _default_ur(self): return self.env["res.company"]._ur_default_get() @@ -39,6 +43,11 @@ class ScopMonthTimesheet(models.Model): selection=get_years(), string="Année", default=fields.Date.today().year ) month = fields.Selection(selection=MONTHS, string="Mois") + date_timesheet = fields.Date( + string="Mois format date", + compute="_compute_date_timesheet", + store=True, + ) company_id = fields.Many2one( comodel_name="res.company", string="Société", @@ -61,34 +70,33 @@ class ScopMonthTimesheet(models.Model): ) ] + @api.depends("year", "month") + def _compute_date_timesheet(self): + for month in self: + date_timesheet = date(int(month.year), int(month.month), 1) + month.date_timesheet = date_timesheet + @api.model def get_month_values(self): - month_values = self.search( + today = date.today() + first_date = today.replace(day=1) - relativedelta(months=6) + last_date = today.replace(day=1) + relativedelta(months=6) + values = self.search( [ - "&", - "|", - "&", - ("year", "<", fields.Date.today().year), - ("month", ">", fields.Date.today().month), - "&", - ("year", "<", fields.Date.today().year + 1), - ("month", "<=", fields.Date.today().month + 1), - ("ur_id", "=", self.env.user.ur_id.id), - ], - limit=12, - order="year desc, month desc", - ).sorted(reverse=False) + ("date_timesheet", ">=", first_date), + ("date_timesheet", "<=", last_date), + ("ur_id", "=", self.env.company.ur_id.id), + ] + ) + return { - "month": month_values.mapped( - lambda x: { - "year": x.year, - "num_month": x.month, - "month": self._fields["month"].selection[int(x.month) - 1][1], + "month_values": values.mapped( + lambda m: { + "year": m.year, + "month": self._fields["month"].selection[int(m.month) - 1][1], + "working_time": m.working_time, + "date_timesheet": m.date_timesheet, } ), - "values": month_values.mapped("working_time"), - "today": { - "year": fields.Date.today().year, - "month": fields.Date.today().month, - }, + "today": today.replace(day=1), } diff --git a/static/src/xml/month_timesheet.xml b/static/src/xml/month_timesheet.xml index de78bcc..77874dc 100644 --- a/static/src/xml/month_timesheet.xml +++ b/static/src/xml/month_timesheet.xml @@ -3,32 +3,34 @@ License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). --> <templates xml:space="preserve"> <t t-name="ScopMonthTimesheet"> - <table class="table"> - <thead> - <tr> - <t t-foreach="widget.values.month" t-as="m"> - <t - t-if="m.num_month == widget.values.today.month and m.year == widget.values.today.year" - > - <th class="text-success"> + <t t-if="widget.values.month_values"> + <table class="table"> + <thead> + <tr> + <th t-foreach="widget.values.month_values" t-as="m"> + <div + t-att-class="m.date_timesheet == widget.values.today ? 'text-success' : ''" + > <t t-esc="m.month" /> <t t-esc="m.year" /> - </th> - </t> - <t t-else=""> - <th> - <t t-esc="m.month" /> <t t-esc="m.year" /> - </th> - </t> - </t> - </tr> - </thead> - <tbody> - <tr> - <t t-foreach="widget.values.values" t-as="v"> - <td><t t-esc="v" /> h</td> - </t> - </tr> - </tbody> - </table> + </div> + </th> + </tr> + </thead> + <tbody> + <tr> + <td t-foreach="widget.values.month_values" t-as="m"> + <div + t-att-class="m.date_timesheet == widget.values.today ? 'text-right text-success' : 'text-right'" + > + <t t-esc="m.working_time" /> h + </div> + </td> + </tr> + </tbody> + </table> + </t> + <t t-else=""> + <p>Aucun temps de travail théorique n'a été configuré.</p> + </t> </t> </templates> -- GitLab