addons-cm/hr_timesheet_sheet_no_review/wizards/unfreeze_wizard.py

98 lines
3.5 KiB
Python

# Copyright 2026 Criptomart
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from markupsafe import Markup
from odoo import _, api, fields, models
class HrTimesheetSheetUnfreezeWizard(models.TransientModel):
_name = "hr_timesheet.sheet.unfreeze.wizard"
_description = "Unfreeze Pending Timesheet Sheets"
sheet_ids = fields.Many2many(
comodel_name="hr_timesheet.sheet",
string="Frozen Sheets To Release",
)
sheet_count = fields.Integer(
string="Frozen Sheets",
compute="_compute_frozen_data",
)
timesheet_count = fields.Integer(
string="Blocked Timesheets",
compute="_compute_frozen_data",
)
timesheet_hours = fields.Float(
string="Blocked Hours",
compute="_compute_frozen_data",
)
summary_html = fields.Html(
string="Summary",
compute="_compute_summary_html",
)
@api.model
def default_get(self, fields_list):
res = super().default_get(fields_list)
if "sheet_ids" in fields_list:
sheet_model = self.env["hr_timesheet.sheet"]
sheets = sheet_model.search(sheet_model._unfreeze_domain())
res["sheet_ids"] = [fields.Command.set(sheets.ids)]
return res
@api.depends("sheet_ids")
def _compute_frozen_data(self):
"""The figures describe ``sheet_ids``, not the database.
Depending on a real field is what makes the figures appear in the
interface: opening an action on a transient model builds the record
with ``default_get()`` + ``onchange()``, and that flow seeds every
field of the view into the cache before computing anything. A compute
with no dependency is then a cache hit on the default value, and the
dialog reports zero.
"""
line_model = self.env["account.analytic.line"]
for wizard in self:
sheets = wizard.sheet_ids
groups = line_model.read_group(
[("sheet_id", "in", sheets.ids)], ["unit_amount"], []
)
group = groups[0] if groups else {}
wizard.sheet_count = len(sheets)
wizard.timesheet_count = group.get("__count") or 0
wizard.timesheet_hours = group.get("unit_amount") or 0.0
@api.depends("sheet_count", "timesheet_count", "timesheet_hours")
def _compute_summary_html(self):
for wizard in self:
wizard.summary_html = wizard._build_summary_html()
def _build_summary_html(self):
self.ensure_one()
if not self.sheet_count:
return Markup("<p>%s</p>") % _(
"There is no frozen timesheet sheet. Nothing to unfreeze."
)
return Markup("<p>%s</p>") % _(
"The timesheet sheets frozen by the review workflow, and the "
"timesheet entries they keep locked, are:"
)
def action_unfreeze(self):
self.ensure_one()
count = self.env["hr_timesheet.sheet"]._unfreeze_sheets(self.sheet_ids)
if count:
message = _("%s timesheet sheet(s) have been set back to draft.", count)
else:
message = _("No frozen timesheet sheet was found. Nothing was changed.")
return {
"type": "ir.actions.client",
"tag": "display_notification",
"params": {
"title": _("Unfreeze Pending Sheets"),
"message": message,
"type": "success" if count else "info",
"sticky": False,
"next": {"type": "ir.actions.act_window_close"},
},
}