|
| 1 | +from datetime import timedelta |
| 2 | + |
| 3 | +from odoo import _, fields, models |
| 4 | + |
| 5 | + |
| 6 | +class PsResetOvertime(models.TransientModel): |
| 7 | + _name = "ps.reset.overtime" |
| 8 | + _description = "Reset overtime for employee" |
| 9 | + |
| 10 | + date = fields.Date( |
| 11 | + "Reset date", |
| 12 | + default=fields.Date.today().replace(month=1, day=1) - timedelta(days=1), |
| 13 | + required=True, |
| 14 | + ) |
| 15 | + |
| 16 | + def action_reset_overtime(self): |
| 17 | + PsTimeLine = self.env["ps.time.line"] |
| 18 | + OvertimeBalanceReport = self.env["overtime.balance.report"] |
| 19 | + employees = self.env["hr.employee"].browse( |
| 20 | + self.env.context.get("active_ids", []) |
| 21 | + ) |
| 22 | + uom_hour = self.env.ref("uom.product_uom_hour") |
| 23 | + created_lines = PsTimeLine.browse([]) |
| 24 | + for overtime_data in OvertimeBalanceReport.read_group( |
| 25 | + [ |
| 26 | + ("date", "<=", self.date), |
| 27 | + ("user_id.employee_ids", "in", employees.ids), |
| 28 | + ], |
| 29 | + ["user_id"], |
| 30 | + ["user_id", "overtime_balanced"], |
| 31 | + orderby="date", |
| 32 | + lazy=False, |
| 33 | + ): |
| 34 | + if not overtime_data["overtime_balanced"]: |
| 35 | + continue |
| 36 | + user_id = overtime_data["user_id"][0] |
| 37 | + ps_time_line = PsTimeLine.search( |
| 38 | + [ |
| 39 | + ( |
| 40 | + "id", |
| 41 | + "in", |
| 42 | + OvertimeBalanceReport.search(overtime_data["__domain"]).ids, |
| 43 | + ), |
| 44 | + ("project_id.overtime_hrs", "=", True), |
| 45 | + ], |
| 46 | + limit=1, |
| 47 | + ) or PsTimeLine.search( |
| 48 | + [("user_id", "=", user_id), ("project_id.overtime_hrs", "=", True)], |
| 49 | + limit=1, |
| 50 | + ) |
| 51 | + overtime_project = ps_time_line.project_id or self.env[ |
| 52 | + "project.project" |
| 53 | + ].search([("project_id.overtime_hrs", "=", True)], limit=1) |
| 54 | + overtime_project_task = ps_time_line.task_id |
| 55 | + created_lines += PsTimeLine.create( |
| 56 | + { |
| 57 | + "name": _("Reset Overtime"), |
| 58 | + "account_id": overtime_project.analytic_account_id.id, |
| 59 | + "project_id": overtime_project.id, |
| 60 | + "task_id": overtime_project_task.id, |
| 61 | + "date": self.date, |
| 62 | + "unit_amount": -overtime_data["overtime_balanced"], |
| 63 | + "product_uom_id": uom_hour.id, |
| 64 | + "ot": True, |
| 65 | + "user_id": user_id, |
| 66 | + } |
| 67 | + ) |
| 68 | + |
| 69 | + return { |
| 70 | + "type": "ir.actions.act_window", |
| 71 | + "name": _("Created lines"), |
| 72 | + "res_model": PsTimeLine._name, |
| 73 | + "views": [(False, "tree")], |
| 74 | + "domain": [("id", "in", created_lines.ids)], |
| 75 | + } |
0 commit comments