|
| 1 | +from odoo import _, fields, models |
| 2 | +from odoo.exceptions import UserError |
| 3 | + |
| 4 | + |
| 5 | +class HrTimesheetSheet(models.Model): |
| 6 | + _inherit = "hr_timesheet.sheet" |
| 7 | + |
| 8 | + purchase_order_id = fields.Many2one("purchase.order", readonly=True) |
| 9 | + allow_generate_purchase_order = fields.Boolean( |
| 10 | + related="employee_id.allow_generate_purchase_order" |
| 11 | + ) |
| 12 | + |
| 13 | + def action_create_purchase_order(self): |
| 14 | + """ |
| 15 | + Create Purchase Order |
| 16 | + """ |
| 17 | + purchase_order_obj = self.env["purchase.order"].sudo() |
| 18 | + order_count = 0 |
| 19 | + group_by_employee = {} |
| 20 | + for record in self: |
| 21 | + group_by_employee.setdefault(record.employee_id, []).append(record) |
| 22 | + for employee, timesheets in group_by_employee.items(): |
| 23 | + if any([timesheet.purchase_order_id for timesheet in timesheets]): |
| 24 | + raise UserError( |
| 25 | + _( |
| 26 | + "One of the Timesheet Sheets selected for employee {} " |
| 27 | + "is already related to a PO.", |
| 28 | + ).format( |
| 29 | + employee.name, |
| 30 | + ) |
| 31 | + ) |
| 32 | + if not employee.allow_generate_purchase_order: |
| 33 | + raise UserError( |
| 34 | + _( |
| 35 | + "Employee {} is not enabled for PO creation from Timesheet Sheets." |
| 36 | + ).format( |
| 37 | + employee.name, |
| 38 | + ) |
| 39 | + ) |
| 40 | + if not employee.billing_partner_id: |
| 41 | + raise UserError( |
| 42 | + _("Not specified billing partner for the employee: {}.").format( |
| 43 | + employee.name, |
| 44 | + ) |
| 45 | + ) |
| 46 | + if not all([timesheet.state == "done" for timesheet in timesheets]): |
| 47 | + raise UserError( |
| 48 | + _("Timesheet Sheets must be approved to create a PO from them."), |
| 49 | + ) |
| 50 | + |
| 51 | + order = purchase_order_obj.create( |
| 52 | + { |
| 53 | + "partner_id": employee.billing_partner_id.id, |
| 54 | + "order_line": [ |
| 55 | + ( |
| 56 | + 0, |
| 57 | + 0, |
| 58 | + { |
| 59 | + "product_id": employee.company_id.timesheet_product_id.id, |
| 60 | + "product_qty": sum( |
| 61 | + [timesheet.total_time for timesheet in timesheets] |
| 62 | + ), |
| 63 | + "price_unit": employee.timesheet_cost, |
| 64 | + }, |
| 65 | + ) |
| 66 | + ], |
| 67 | + } |
| 68 | + ) |
| 69 | + order_count += 1 |
| 70 | + for timesheet in timesheets: |
| 71 | + timesheet.purchase_order_id = order.id |
| 72 | + return { |
| 73 | + "type": "ir.actions.client", |
| 74 | + "tag": "display_notification", |
| 75 | + "params": { |
| 76 | + "type": "success", |
| 77 | + "message": _( |
| 78 | + "{} POs created from timesheet sheet selected " |
| 79 | + "for the following employees: {}", |
| 80 | + ).format( |
| 81 | + order_count, |
| 82 | + ", ".join([employee.name for employee in group_by_employee.keys()]), |
| 83 | + ), |
| 84 | + "next": { |
| 85 | + "type": "ir.actions.act_window_close", |
| 86 | + }, |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + def action_open_purchase_order(self): |
| 91 | + """ |
| 92 | + Return action to open related Purchase Order |
| 93 | + """ |
| 94 | + self.ensure_one() |
| 95 | + if self.purchase_order_id: |
| 96 | + action = self.env["ir.actions.act_window"]._for_xml_id( |
| 97 | + "purchase.action_rfq_form" |
| 98 | + ) |
| 99 | + action["res_id"] = self.purchase_order_id.id |
| 100 | + action["target"] = "current" |
| 101 | + return action |
| 102 | + |
| 103 | + def action_confirm_purchase_order(self): |
| 104 | + """ |
| 105 | + Confirm purchase orders |
| 106 | + """ |
| 107 | + self.filtered(lambda rec: rec.purchase_order_id).mapped( |
| 108 | + "purchase_order_id" |
| 109 | + ).sudo().button_confirm() |
| 110 | + |
| 111 | + def action_timesheet_draft(self): |
| 112 | + sheets_with_po = self.filtered(lambda sheet: sheet.purchase_order_id) |
| 113 | + if sheets_with_po: |
| 114 | + raise UserError( |
| 115 | + _( |
| 116 | + "Cannot set to draft a Timesheet Sheet from which a PO has been created. " |
| 117 | + "Please delete the related PO first.", |
| 118 | + ), |
| 119 | + ) |
| 120 | + super().action_timesheet_draft() |
0 commit comments