| 1 |
import { CalendarModel } from "@web/views/calendar/calendar_model"; |
| 2 |
import { |
| 3 |
deserializeDate, |
| 4 |
deserializeDateTime, |
| 5 |
serializeDate, |
| 6 |
serializeDateTime, |
| 7 |
} from "@web/core/l10n/dates"; |
| 8 |
import { Cache } from "@web/core/utils/cache"; |
| 9 |
const { DateTime } = luxon; |
| 10 |
|
| 11 |
export class TimeOffCalendarModel extends CalendarModel { |
| 12 |
setup(params, services) { |
| 13 |
super.setup(params, services); |
| 14 |
|
| 15 |
this.data.mandatoryDays = {}; |
| 16 |
if (this.env.isSmall) { |
| 17 |
this.meta.scale = "month"; |
| 18 |
} |
| 19 |
|
| 20 |
this._mandatoryDaysCache = new Cache( |
| 21 |
(data) => this.fetchMandatoryDays(data), |
| 22 |
(data) => `${serializeDateTime(data.range.start)},${serializeDateTime(data.range.end)}` |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* @override |
| 28 |
*/ |
| 29 |
normalizeRecord(rawRecord) { |
| 30 |
const result = super.normalizeRecord(...arguments); |
| 31 |
if (rawRecord.employee_id) { |
| 32 |
const employee = rawRecord.employee_id[1]; |
| 33 |
// If the employee's name isn't already included at the start of the title |
| 34 |
if (!result.title.startsWith(employee)) { |
| 35 |
result.title = [employee, result.title].join(" "); |
| 36 |
} |
| 37 |
} |
| 38 |
if (rawRecord.date_from && rawRecord.date_to) { |
| 39 |
const dateFrom = DateTime.fromSQL(rawRecord.date_from); |
| 40 |
const dateTo = DateTime.fromSQL(rawRecord.date_to); |
| 41 |
result.sameDay = dateFrom.hasSame(dateTo, 'day'); |
| 42 |
} |
| 43 |
if (rawRecord.request_unit_half) { |
| 44 |
result.requestDateFromPeriod = rawRecord.request_date_from_period; |
| 45 |
result.requestDateToPeriod = rawRecord.request_date_to_period; |
| 46 |
} |
| 47 |
return result; |
| 48 |
} |
| 49 |
|
| 50 |
makeContextDefaults(record) { |
| 51 |
const context = super.makeContextDefaults(record); |
| 52 |
let default_employee_id = this.employeeId; |
| 53 |
if(context['active_model'] === 'hr.employee') { |
| 54 |
default_employee_id = context.active_id |
| 55 |
} |
| 56 |
if (default_employee_id) { |
| 57 |
context["default_employee_id"] = default_employee_id |
| 58 |
} |
| 59 |
function deserialize(str) { |
| 60 |
// "YYYY-MM-DD".length == 10 |
| 61 |
return str.length > 10 ? deserializeDateTime(str) : deserializeDate(str); |
| 62 |
} |
| 63 |
if (["week", "day"].includes(this.scale)) { |
| 64 |
context["default_request_unit_hours"] = true; |
| 65 |
const hour_from = deserialize(context['default_date_from']??this.date); |
| 66 |
const hour_to = deserialize(context['default_date_to']??this.date); |
| 67 |
context['default_request_hour_from'] = hour_from.hour + hour_from.minute / 60; |
| 68 |
context['default_request_hour_to'] = hour_to.hour + hour_to.minute / 60; |
| 69 |
} |
| 70 |
|
| 71 |
if ("default_date_from" in context) { |
| 72 |
context["default_date_from"] = serializeDateTime( |
| 73 |
deserialize(context["default_date_from"]).set({ hours: 7 }) |
| 74 |
); |
| 75 |
} |
| 76 |
if ("default_date_to" in context) { |
| 77 |
context["default_date_to"] = serializeDateTime( |
| 78 |
deserialize(context["default_date_to"]).set({ hours: 19 }) |
| 79 |
); |
| 80 |
} |
| 81 |
return context; |
| 82 |
} |
| 83 |
|
| 84 |
async updateData(data) { |
| 85 |
const prom = super.updateData(data); |
| 86 |
data.mandatoryDays = await this._mandatoryDaysCache.read(data); |
| 87 |
return prom; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* @override |
| 92 |
*/ |
| 93 |
fetchUnusualDays(data) { |
| 94 |
return this.orm.call( |
| 95 |
this.meta.resModel, |
| 96 |
"get_unusual_days", |
| 97 |
[serializeDateTime(data.range.start), serializeDateTime(data.range.end)], |
| 98 |
{ |
| 99 |
context: { |
| 100 |
employee_id: this.employeeId, |
| 101 |
}, |
| 102 |
} |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
async fetchMandatoryDays(data) { |
| 107 |
return this.orm.call("hr.employee", "get_mandatory_days", [ |
| 108 |
this.employeeId, |
| 109 |
serializeDate(data.range.start, "datetime"), |
| 110 |
serializeDate(data.range.end, "datetime"), |
| 111 |
]); |
| 112 |
} |
| 113 |
|
| 114 |
get mandatoryDays() { |
| 115 |
return this.data.mandatoryDays; |
| 116 |
} |
| 117 |
|
| 118 |
get employeeId() { |
| 119 |
return ( |
| 120 |
(this.meta.context.employee_id && this.meta.context.employee_id[0]) || |
| 121 |
(this.meta.context.active_model === "hr.employee" && this.meta.context.active_id) || |
| 122 |
null |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
fetchRecords(data) { |
| 127 |
const { fieldNames, resModel } = this.meta; |
| 128 |
const context = {}; |
| 129 |
if (!this.employeeId) { |
| 130 |
context["short_name"] = 1; |
| 131 |
} |
| 132 |
const fieldNamesToAdd = resModel === "hr.leave" ? ["request_unit_half", "request_date_from_period", "request_date_to_period", "request_unit_hours"] : []; |
| 133 |
return this.orm.searchRead(resModel, this.computeDomain(data), [...fieldNames, ...fieldNamesToAdd], { context }); |
| 134 |
} |
| 135 |
|
| 136 |
computeDomain(data) { |
| 137 |
return [...super.computeDomain(data), ["state", "!=", "cancel"]]; |
| 138 |
} |
| 139 |
} |