/
greeting_message.js
175 lines (154 loc) · 7.77 KB
/
greeting_message.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Copyright (c) 2004-2018 Odoo S.A.
Copyright 2018 Kolushov Alexandr <https://it-projects.info/team/KolushovAlexandr>
License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). */
odoo.define('base_attendance.greeting_message', function (require) {
"use strict";
var AbstractAction = require('web.AbstractAction');
var core = require('web.core');
var _t = core._t;
var GreetingMessage = AbstractAction.extend({
template: 'BaseAttendanceGreetingMessage',
events: {
"click .o_hr_attendance_button_dismiss": function() {
this.do_action(this.next_action, {clear_breadcrumbs: true});
},
},
init: function(parent, action) {
var self = this;
this._super.apply(this, arguments);
this.activeBarcode = true;
// if no correct action given (due to an erroneous back or refresh from the browser), we set the dismiss button to return
// to the (likely) appropriate menu, according to the user access rights
if(!action.attendance) {
this.activeBarcode = false;
this.getSession().user_has_group('base_attendance.group_hr_attendance_user').then(function(has_group) {
if(has_group) {
self.next_action = 'base_attendance.hr_attendance_action_kiosk_mode';
} else {
self.next_action = 'base_attendance.hr_attendance_action_my_attendances';
}
});
return;
}
this.next_action = action.next_action || 'base_attendance.hr_attendance_action_my_attendances';
// no listening to barcode scans if we aren't coming from the kiosk mode (and thus not going back to it with next_action)
if (this.next_action !== 'base_attendance.hr_attendance_action_kiosk_mode' && this.next_action.tag !== 'hr_attendance_kiosk_mode') {
this.activeBarcode = false;
}
this.attendance = action.attendance;
// We receive the check in/out times in UTC
// This widget only deals with display, which should be in browser's TimeZone
var moment = window.moment;
this.attendance.check_in = this.attendance.check_in && moment.utc(this.attendance.check_in).local();
this.attendance.check_out = this.attendance.check_out && moment.utc(this.attendance.check_out).local();
this.previous_attendance_change_date = action.previous_attendance_change_date && moment.utc(action.previous_attendance_change_date).local();
// check in/out times displayed in the greeting message template.
this.format_time = 'HH:mm:ss';
this.attendance.check_in_time = this.attendance.check_in && this.attendance.check_in.format(this.format_time);
this.attendance.check_out_time = this.attendance.check_out && this.attendance.check_out.format(this.format_time);
this.partner_name = action.partner_name;
},
start: function() {
if (this.attendance) {
this.attendance.check_out
? this.farewell_message()
: this.welcome_message();
}
if (this.activeBarcode) {
core.bus.on('barcode_scanned', this, this._onBarcodeScanned);
}
},
welcome_message: function() {
var self = this;
var now = this.attendance.check_in.clone();
this.return_to_main_menu = setTimeout( function() {
self.do_action(self.next_action, {clear_breadcrumbs: true});
}, 5000);
if (now.hours() < 5) {
this.$('.o_hr_attendance_message_message').append(_t("Good night"));
} else if (now.hours() < 12) {
if (now.hours() < 8 && Math.random() < 0.3) {
if (Math.random() < 0.75) {
this.$('.o_hr_attendance_message_message').append(_t("The early bird catches the worm"));
} else {
this.$('.o_hr_attendance_message_message').append(_t("First come, first served"));
}
} else {
this.$('.o_hr_attendance_message_message').append(_t("Good morning"));
}
} else if (now.hours() < 17){
this.$('.o_hr_attendance_message_message').append(_t("Good afternoon"));
} else if (now.hours() < 23){
this.$('.o_hr_attendance_message_message').append(_t("Good evening"));
} else {
this.$('.o_hr_attendance_message_message').append(_t("Good night"));
}
if(this.previous_attendance_change_date){
var last_check_out_date = this.previous_attendance_change_date.clone();
if(now - last_check_out_date > 24*7*60*60*1000){
this.$('.o_hr_attendance_random_message').html(_t("Glad to have you back, it's been a while!"));
} else if(Math.random() < 0.02){
this.$('.o_hr_attendance_random_message').html(_t("If a job is worth doing, it is worth doing well!"));
}
}
},
farewell_message: function() {
var self = this;
var now = this.attendance.check_out.clone();
this.return_to_main_menu = setTimeout( function() {
self.do_action(self.next_action, {clear_breadcrumbs: true});
}, 5000);
if(this.previous_attendance_change_date){
var last_check_in_date = this.previous_attendance_change_date.clone();
if(now - last_check_in_date > 1000*60*60*12){
this.$('.o_hr_attendance_warning_message').append(_t("Warning! Last check in was over 12 hours ago.<br/>If this isn't right, please contact Human Resources."));
clearTimeout(this.return_to_main_menu);
this.activeBarcode = false;
} else if(now - last_check_in_date > 1000*60*60*8){
this.$('.o_hr_attendance_random_message').html(_t("Another good day's work! See you soon!"));
}
}
if (now.hours() < 12) {
this.$('.o_hr_attendance_message_message').append(_t("Have a good day!"));
} else if (now.hours() < 14) {
this.$('.o_hr_attendance_message_message').append(_t("Have a nice lunch!"));
if (Math.random() < 0.05) {
this.$('.o_hr_attendance_random_message').html(_t("Eat breakfast as a king, lunch as a merchant and supper as a beggar"));
} else if (Math.random() < 0.06) {
this.$('.o_hr_attendance_random_message').html(_t("An apple a day keeps the doctor away"));
}
} else if (now.hours() < 17) {
this.$('.o_hr_attendance_message_message').append(_t("Have a good afternoon"));
} else if (now.hours() < 18 && Math.random() < 0.2) {
this.$('.o_hr_attendance_message_message').append(_t("Early to bed and early to rise, makes a man healthy, wealthy and wise"));
} else {
this.$('.o_hr_attendance_message_message').append(_t("Have a good evening"));
}
},
_onBarcodeScanned: function(barcode) {
var self = this;
if (this.return_to_main_menu) {
// in case of multiple scans in the greeting message view, delete the timer, a new one will be created.
clearTimeout(this.return_to_main_menu);
}
this._rpc({
model: 'res.partner',
method: 'attendance_scan',
args: [barcode, ],
}).then(function (result) {
if (result.action) {
self.do_action(result.action);
} else if (result.warning) {
self.do_warn(result.warning);
}
});
},
destroy: function () {
core.bus.off('barcode_scanned', this, this._onBarcodeScanned);
clearTimeout(this.return_to_main_menu);
this._super.apply(this, arguments);
},
});
core.action_registry.add('base_attendance_greeting_message', GreetingMessage);
return GreetingMessage;
});