|
1 |
| -from odoo import fields, models |
| 1 | +from datetime import timedelta |
| 2 | + |
| 3 | +from odoo import api, fields, models |
| 4 | + |
| 5 | +from .hr_timesheet_recurrence import DAYS |
2 | 6 |
|
3 | 7 |
|
4 | 8 | class HrEmployeeBase(models.AbstractModel):
|
5 | 9 | _inherit = "hr.employee.base"
|
6 | 10 |
|
7 |
| - allow_generate_purchase_order = fields.Boolean( |
8 |
| - string="Generate POs from Timesheet", default=False |
9 |
| - ) |
| 11 | + allow_generate_purchase_order = fields.Boolean(string="Generate POs from Timesheet") |
10 | 12 | billing_partner_id = fields.Many2one("res.partner")
|
| 13 | + |
| 14 | + is_auto_po_generate = fields.Boolean(string="Automatic PO generation") |
| 15 | + recurrence_id = fields.Many2one("hr.timesheet.recurrence", copy=False) |
| 16 | + |
| 17 | + is_send_po = fields.Boolean(string="Send RFQ by email after creation") |
| 18 | + |
| 19 | + next_recurrence_date = fields.Date(related="recurrence_id.next_recurrence_date") |
| 20 | + repeat_interval = fields.Integer( |
| 21 | + string="Repeat Every", default=1, compute="_compute_repeat", readonly=False |
| 22 | + ) |
| 23 | + repeat_unit = fields.Selection( |
| 24 | + [ |
| 25 | + ("day", "Days"), |
| 26 | + ("week", "Weeks"), |
| 27 | + ("month", "Months"), |
| 28 | + ("year", "Years"), |
| 29 | + ], |
| 30 | + default="week", |
| 31 | + compute="_compute_repeat", |
| 32 | + readonly=False, |
| 33 | + ) |
| 34 | + repeat_type = fields.Selection( |
| 35 | + [ |
| 36 | + ("forever", "Forever"), |
| 37 | + ("until", "End Date"), |
| 38 | + ("after", "Number of Repetitions"), |
| 39 | + ], |
| 40 | + default="forever", |
| 41 | + string="Until", |
| 42 | + compute="_compute_repeat", |
| 43 | + readonly=False, |
| 44 | + ) |
| 45 | + repeat_until = fields.Date( |
| 46 | + string="End Date", compute="_compute_repeat", readonly=False |
| 47 | + ) |
| 48 | + repeat_number = fields.Integer( |
| 49 | + string="Repetitions", default=1, compute="_compute_repeat", readonly=False |
| 50 | + ) |
| 51 | + |
| 52 | + repeat_on_month = fields.Selection( |
| 53 | + [ |
| 54 | + ("date", "Date of the Month"), |
| 55 | + ("day", "Day of the Month"), |
| 56 | + ], |
| 57 | + default="date", |
| 58 | + compute="_compute_repeat", |
| 59 | + readonly=False, |
| 60 | + ) |
| 61 | + |
| 62 | + repeat_on_year = fields.Selection( |
| 63 | + [ |
| 64 | + ("date", "Date of the Year"), |
| 65 | + ("day", "Day of the Year"), |
| 66 | + ], |
| 67 | + default="date", |
| 68 | + compute="_compute_repeat", |
| 69 | + readonly=False, |
| 70 | + ) |
| 71 | + |
| 72 | + mon = fields.Boolean(string="Mon", compute="_compute_repeat", readonly=False) |
| 73 | + tue = fields.Boolean(string="Tue", compute="_compute_repeat", readonly=False) |
| 74 | + wed = fields.Boolean(string="Wed", compute="_compute_repeat", readonly=False) |
| 75 | + thu = fields.Boolean(string="Thu", compute="_compute_repeat", readonly=False) |
| 76 | + fri = fields.Boolean(string="Fri", compute="_compute_repeat", readonly=False) |
| 77 | + sat = fields.Boolean(string="Sat", compute="_compute_repeat", readonly=False) |
| 78 | + sun = fields.Boolean(string="Sun", compute="_compute_repeat", readonly=False) |
| 79 | + |
| 80 | + repeat_day = fields.Integer( |
| 81 | + compute="_compute_repeat", |
| 82 | + readonly=False, |
| 83 | + ) |
| 84 | + |
| 85 | + @api.onchange("repeat_day", "repeat_month") |
| 86 | + def _onchange_repeat_day(self): |
| 87 | + if 0 > self.repeat_day or self.repeat_day > 31: |
| 88 | + self.repeat_day = 1 |
| 89 | + if self.repeat_month == "february" and self.repeat_day > 29: |
| 90 | + self.repeat_day = 28 |
| 91 | + |
| 92 | + repeat_week = fields.Selection( |
| 93 | + [ |
| 94 | + ("first", "First"), |
| 95 | + ("second", "Second"), |
| 96 | + ("third", "Third"), |
| 97 | + ("last", "Last"), |
| 98 | + ], |
| 99 | + default="first", |
| 100 | + compute="_compute_repeat", |
| 101 | + readonly=False, |
| 102 | + ) |
| 103 | + repeat_weekday = fields.Selection( |
| 104 | + [ |
| 105 | + ("mon", "Monday"), |
| 106 | + ("tue", "Tuesday"), |
| 107 | + ("wed", "Wednesday"), |
| 108 | + ("thu", "Thursday"), |
| 109 | + ("fri", "Friday"), |
| 110 | + ("sat", "Saturday"), |
| 111 | + ("sun", "Sunday"), |
| 112 | + ], |
| 113 | + string="Day Of The Week", |
| 114 | + compute="_compute_repeat", |
| 115 | + readonly=False, |
| 116 | + ) |
| 117 | + repeat_month = fields.Selection( |
| 118 | + [ |
| 119 | + ("january", "January"), |
| 120 | + ("february", "February"), |
| 121 | + ("march", "March"), |
| 122 | + ("april", "April"), |
| 123 | + ("may", "May"), |
| 124 | + ("june", "June"), |
| 125 | + ("july", "July"), |
| 126 | + ("august", "August"), |
| 127 | + ("september", "September"), |
| 128 | + ("october", "October"), |
| 129 | + ("november", "November"), |
| 130 | + ("december", "December"), |
| 131 | + ], |
| 132 | + compute="_compute_repeat", |
| 133 | + readonly=False, |
| 134 | + ) |
| 135 | + |
| 136 | + repeat_show_dow = fields.Boolean(compute="_compute_repeat_visibility") |
| 137 | + repeat_show_day = fields.Boolean(compute="_compute_repeat_visibility") |
| 138 | + repeat_show_week = fields.Boolean(compute="_compute_repeat_visibility") |
| 139 | + repeat_show_month = fields.Boolean(compute="_compute_repeat_visibility") |
| 140 | + |
| 141 | + @api.depends( |
| 142 | + "is_auto_po_generate", "repeat_unit", "repeat_on_month", "repeat_on_year" |
| 143 | + ) |
| 144 | + def _compute_repeat_visibility(self): |
| 145 | + """Based on the selected parameters sets |
| 146 | + the fields that should be visible to the user |
| 147 | + """ |
| 148 | + for item in self: |
| 149 | + item.repeat_show_day = ( |
| 150 | + item.is_auto_po_generate |
| 151 | + and (item.repeat_unit == "month" and item.repeat_on_month == "date") |
| 152 | + or (item.repeat_unit == "year" and item.repeat_on_year == "date") |
| 153 | + ) |
| 154 | + item.repeat_show_week = ( |
| 155 | + item.is_auto_po_generate |
| 156 | + and (item.repeat_unit == "month" and item.repeat_on_month == "day") |
| 157 | + or (item.repeat_unit == "year" and item.repeat_on_year == "day") |
| 158 | + ) |
| 159 | + item.repeat_show_dow = ( |
| 160 | + item.is_auto_po_generate and item.repeat_unit == "week" |
| 161 | + ) |
| 162 | + item.repeat_show_month = ( |
| 163 | + item.is_auto_po_generate and item.repeat_unit == "year" |
| 164 | + ) |
| 165 | + |
| 166 | + @api.depends("is_auto_po_generate") |
| 167 | + def _compute_repeat(self): |
| 168 | + rec_fields = self._get_recurrence_fields() |
| 169 | + defaults = self.default_get(rec_fields) |
| 170 | + for employee in self: |
| 171 | + for f in rec_fields: |
| 172 | + if employee.recurrence_id: |
| 173 | + employee[f] = employee.recurrence_id[f] |
| 174 | + else: |
| 175 | + employee[f] = ( |
| 176 | + defaults.get(f) if employee.is_auto_po_generate else False |
| 177 | + ) |
| 178 | + |
| 179 | + @api.model |
| 180 | + def _get_recurrence_fields(self): |
| 181 | + return [ |
| 182 | + "repeat_interval", |
| 183 | + "repeat_unit", |
| 184 | + "repeat_type", |
| 185 | + "repeat_until", |
| 186 | + "repeat_number", |
| 187 | + "repeat_on_month", |
| 188 | + "repeat_on_year", |
| 189 | + "mon", |
| 190 | + "tue", |
| 191 | + "wed", |
| 192 | + "thu", |
| 193 | + "fri", |
| 194 | + "sat", |
| 195 | + "sun", |
| 196 | + "repeat_day", |
| 197 | + "repeat_week", |
| 198 | + "repeat_month", |
| 199 | + "repeat_weekday", |
| 200 | + ] |
| 201 | + |
| 202 | + @api.model |
| 203 | + def default_get(self, default_fields): |
| 204 | + vals = super().default_get(default_fields) |
| 205 | + days = list(DAYS.keys()) |
| 206 | + week_start = fields.Datetime.today().weekday() |
| 207 | + if all(d in default_fields for d in days): |
| 208 | + vals[days[week_start]] = True |
| 209 | + if "repeat_day" in default_fields: |
| 210 | + vals["repeat_day"] = str(fields.Datetime.today().day) |
| 211 | + if "repeat_month" in default_fields: |
| 212 | + vals["repeat_month"] = self._fields.get("repeat_month").selection[ |
| 213 | + fields.Datetime.today().month - 1 |
| 214 | + ][0] |
| 215 | + if "repeat_until" in default_fields: |
| 216 | + vals["repeat_until"] = fields.Date.today() + timedelta(days=7) |
| 217 | + if "repeat_weekday" in default_fields: |
| 218 | + vals["repeat_weekday"] = self._fields.get("repeat_weekday").selection[ |
| 219 | + week_start |
| 220 | + ][0] |
| 221 | + return vals |
| 222 | + |
| 223 | + def write(self, vals): |
| 224 | + rec_fields = vals.keys() & self._get_recurrence_fields() |
| 225 | + if "is_auto_po_generate" in vals and not vals.get("is_auto_po_generate"): |
| 226 | + self.recurrence_id.unlink() |
| 227 | + if rec_fields: |
| 228 | + rec_values = {rec_field: vals[rec_field] for rec_field in rec_fields} |
| 229 | + for timesheet in self: |
| 230 | + if timesheet.recurrence_id: |
| 231 | + timesheet.recurrence_id.write(rec_values) |
| 232 | + elif vals.get("is_auto_po_generate"): |
| 233 | + rec_values["next_recurrence_date"] = fields.Datetime.today() |
| 234 | + recurrence = self.env["hr.timesheet.recurrence"].create(rec_values) |
| 235 | + timesheet.recurrence_id = recurrence.id |
| 236 | + return super().write(vals) |
| 237 | + |
| 238 | + @api.model |
| 239 | + def create(self, vals): |
| 240 | + rec_fields = vals.keys() & self._get_recurrence_fields() |
| 241 | + if rec_fields and vals.get("is_auto_po_generate"): |
| 242 | + rec_values = {rec_field: vals[rec_field] for rec_field in rec_fields} |
| 243 | + rec_values["next_recurrence_date"] = fields.Datetime.today() |
| 244 | + recurrence = self.env["hr.timesheet.recurrence"].create(rec_values) |
| 245 | + vals["recurrence_id"] = recurrence.id |
| 246 | + return super().create(vals) |
0 commit comments