Debian Codegraph @

addons/hr_homeworking_calendar/static/src/calendar/common/calendar_model.js

raw
1 import { AttendeeCalendarModel } from "@calendar/views/attendee_calendar/attendee_calendar_model";
2 import { serializeDateTime } from "@web/core/l10n/dates";
3 import { user } from "@web/core/user";
4 import { getColor } from "@web/views/calendar/utils";
5 import { patch } from "@web/core/utils/patch";
6
7 const { Interval } = luxon;
8
9 patch(AttendeeCalendarModel.prototype, {
10 fetchEventLocation(data) {
11 let attendeeIds;
12 const filters = data.filterSections.partner_ids?.filters;
13 if (filters && filters[filters.length - 1].type === "all" && filters[filters.length - 1].active) {
14 attendeeIds = Object.keys(this.partnerColorMap);
15 } else {
16 attendeeIds = (filters || [])
17 .filter(filter => filter.type !== "all" && filter.value && filter.active)
18 .map(filter => filter.value)
19 }
20 if (!attendeeIds.includes(user.partnerId)) {
21 attendeeIds.push(user.partnerId);
22 }
23 return this.orm.call('res.partner', "get_worklocation", [
24 attendeeIds,
25 serializeDateTime(data.range.start),
26 serializeDateTime(data.range.end),
27 ]);
28 },
29
30 // returns a map of worklocations, display is used to mark the events that are to be shown in the view.
31 async loadWorkLocations(data) {
32 const res = await this.fetchEventLocation(data)
33 this.multiCalendar = Object.values(res).some(location => location.user_id !== user.userId);
34 const filters = data.filterSections.partner_ids?.filters;
35 data.userFilterActive = filters && (
36 filters.filter(filter => filter.value === user.partnerId)[0]?.active ||
37 filters[filters.length - 1].type === "all" &&
38 filters[filters.length - 1].active
39 );
40 const events = {};
41 let previousDay;
42 const rangeInterval = Interval.fromDateTimes(data.range.start.startOf("day"), data.range.end.endOf("day")).splitBy({day: 1});
43 for (const day of rangeInterval) {
44 const startDay = day.s;
45 const dayISO = startDay.toISODate();
46 const dayName = startDay.setLocale("en").weekdayLong.toLowerCase();
47 for (const employeeId in res) {
48 if (this.multiCalendar) {
49 if (!(dayISO in events)) {
50 events[dayISO] = {};
51 }
52 if (res[employeeId].exceptions && dayISO in res[employeeId].exceptions) {
53 // check if exception for that date
54 const { location_type } = res[employeeId].exceptions[dayISO];
55 if (location_type in events[dayISO]) {
56 events[dayISO][location_type].push(this.createHomeworkingRecordAt(res[employeeId], startDay, res[employeeId].exceptions[dayISO]));
57 } else {
58 events[dayISO][location_type] = [this.createHomeworkingRecordAt(res[employeeId], startDay, res[employeeId].exceptions[dayISO])];
59 }
60 }
61 else {
62 const locationKeyName = `${dayName}_location_id`;
63 if (!(locationKeyName in res[employeeId])) {
64 continue;
65 }
66 const {location_type} = res[employeeId][locationKeyName];
67 if (!location_type) {
68 continue;
69 }
70 if (location_type in events[dayISO]) {
71 events[dayISO][location_type].push(this.createHomeworkingRecordAt(res[employeeId], startDay, res[employeeId][locationKeyName]));
72 } else {
73 events[dayISO][location_type] = [this.createHomeworkingRecordAt(res[employeeId], startDay, res[employeeId][locationKeyName])];
74 }
75 }
76 } else {
77 const hasException = res[employeeId].exceptions && dayISO in res[employeeId].exceptions;
78 const workLocationData = hasException ? res[employeeId].exceptions[dayISO] : res[employeeId][`${dayName}_location_id`];
79 const currentEvent = this.createHomeworkingRecordAt(res[employeeId], startDay, workLocationData);
80 const previousEvent = events[previousDay];
81 if (previousEvent && previousEvent.icon === currentEvent.icon && previousEvent.title === currentEvent.title) {
82 previousEvent.end = previousEvent.end.plus({days:1});
83 currentEvent.display = false;
84 } else {
85 previousDay = dayISO;
86 }
87 if (currentEvent.title) {
88 events[dayISO] = currentEvent;
89 }
90 }
91 }
92 }
93 return events;
94 },
95
96 createHomeworkingRecordAt(record, day, workLocationData) {
97 const { location_type, location_name, work_location_id, hr_employee_location_id} = workLocationData;
98 const ghostRecord = !Boolean(hr_employee_location_id);
99 const id = ghostRecord ? `default-location-${record.employee_id}-${day.toMillis()}` : String(hr_employee_location_id);
100 return {
101 id,
102 title: location_name,
103 start: day,
104 end: day.plus({days:1}),
105 display: true,
106 multiCalendar: this.multiCalendar,
107 homeworking: true,
108 employeeId: record.employee_id,
109 employeeName: record.employee_name,
110 icon: location_type,
111 userId: record.user_id,
112 partnerId: record.partner_id,
113 colorIndex: this.partnerColorMap[record.partner_id],
114 resModel: "hr.employee.location",
115 work_location_id,
116 ghostRecord,
117 rawRecord: record,
118 };
119 },
120
121 get worklocations() {
122 return this.data.worklocations;
123 },
124
125 mapPartnersToColor(data) {
126 return (data.filterSections.partner_ids?.filters || [])
127 .filter(filter => filter.type !== "all" && filter.value)
128 .reduce((map, partner) => ({ ...map, [partner.value]: getColor(partner.colorIndex)}), {})
129 },
130
131 /**
132 * @override
133 */
134 async updateData(data){
135 await super.updateData(...arguments)
136 this.partnerColorMap = this.mapPartnersToColor(data);
137 data.worklocations = await this.loadWorkLocations(data);
138 }
139 })