Debian Codegraph @

addons/hr_holidays/static/src/views/calendar/calendar_controller.js

raw
1 import { _t } from "@web/core/l10n/translation";
2 import { ConfirmationDialog } from "@web/core/confirmation_dialog/confirmation_dialog";
3 import { CalendarController } from "@web/views/calendar/calendar_controller";
4
5 import { serializeDate } from "@web/core/l10n/dates";
6
7 import { TimeOffCalendarSidePanel } from "./calendar_side_panel/calendar_side_panel";
8 import { TimeOffCalendarMobileFilterPanel } from "./calendar_filter_panel/calendar_mobile_filter_panel";
9 import { TimeOffFormViewDialog } from "../view_dialog/form_view_dialog";
10 import { useLeaveCancelWizard } from "../hooks";
11 import { EventBus, useSubEnv } from "@odoo/owl";
12
13 export class TimeOffCalendarController extends CalendarController {
14 static components = {
15 ...CalendarController.components,
16 CalendarSidePanel: TimeOffCalendarSidePanel,
17 MobileFilterPanel: TimeOffCalendarMobileFilterPanel,
18 };
19 static template = "hr_holidays.CalendarController";
20 setup() {
21 super.setup();
22 useSubEnv({
23 timeOffBus: new EventBus(),
24 });
25 this.leaveCancelWizard = useLeaveCancelWizard();
26 }
27
28 get employeeId() {
29 return this.model.employeeId;
30 }
31
32 newTimeOffRequest() {
33 const context = {};
34 if (this.props.context.active_id && this.props.context.active_model === "hr.employee") {
35 context["default_employee_id"] = this.props.context.active_id;
36 } else if (this.employeeId) {
37 context["default_employee_id"] = this.employeeId;
38 }
39 if (this.model.meta.scale == "day") {
40 context["default_date_from"] = serializeDate(
41 this.model.data.range.start.set({ hours: 7 }),
42 "datetime"
43 );
44 context["default_date_to"] = serializeDate(
45 this.model.data.range.end.set({ hours: 19 }),
46 "datetime"
47 );
48 }
49
50 this.displayDialog(TimeOffFormViewDialog, {
51 resModel: "hr.leave",
52 title: _t("New Time Off"),
53 viewId: this.model.formViewId,
54 onRecordSaved: () => {
55 this.model.load();
56 this.env.timeOffBus.trigger("update_dashboard");
57 },
58 onRecordDeleted: (record) => {},
59 onLeaveCancelled: (record) => {},
60 size: "md",
61 context: context,
62 });
63 }
64
65 _deleteRecord(resId, canCancel) {
66 if (!canCancel) {
67 this.displayDialog(ConfirmationDialog, {
68 title: _t("Confirmation"),
69 body: _t("Are you sure you want to delete this record?"),
70 confirm: async () => {
71 await this.model.unlinkRecord(resId);
72 this.env.timeOffBus.trigger("update_dashboard");
73 },
74 cancel: () => {},
75 });
76 } else {
77 this.leaveCancelWizard(resId, () => {
78 this.model.load();
79 this.env.timeOffBus.trigger("update_dashboard");
80 });
81 }
82 }
83
84 deleteRecord(record) {
85 this._deleteRecord(record.id, record.rawRecord.can_cancel);
86 }
87
88 _editRecord(record, context, props = {}) {
89 const onDialogClosed = () => {
90 this.model.load();
91 this.env.timeOffBus.trigger("update_dashboard");
92 };
93
94 return new Promise((resolve) => {
95 this.displayDialog(
96 TimeOffFormViewDialog,
97 {
98 ...props,
99 resModel: this.model.resModel,
100 resId: record.id || false,
101 context,
102 title: _t("Time Off Request"),
103 viewId: this.model.formViewId,
104 onRecordSaved: onDialogClosed,
105 onRecordDeleted: (record) =>
106 this._deleteRecord(record.resId, record.data.can_cancel),
107 onLeaveCancelled: onDialogClosed,
108 size: "md",
109 },
110 { onClose: () => resolve() }
111 );
112 });
113 }
114
115 async editRecord(record, context = {}) {
116 return this._editRecord(record, context);
117 }
118 }
119
120 export class TimeOffReportCalendarController extends TimeOffCalendarController {
121 async editRecord(record, context = {}) {
122 return this._editRecord(record, context, { canExpand: false });
123 }
124 }