]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/admin/local/actor/closed_dates.js
LP#1766716: Adjust date display logic
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / admin / local / actor / closed_dates.js
1 angular.module('egAdminClosed',
2     ['ngRoute','ui.bootstrap','egCoreMod','egUiMod','egGridMod','ngToast'])
3
4 .config(['ngToastProvider', function(ngToastProvider) {
5   ngToastProvider.configure({
6     verticalPosition: 'bottom',
7     animation: 'fade'
8   });
9 }])
10
11 .controller('ClosedDates',
12        ['$scope','$q','$timeout','$location','$window','$uibModal','ngToast',
13         'egCore','egGridDataProvider','egConfirmDialog','egProgressDialog','$timeout',
14 function($scope , $q , $timeout , $location , $window , $uibModal , ngToast ,
15          egCore , egGridDataProvider , egConfirmDialog , egProgressDialog , $timeout) {
16
17     egCore.startup.go().then(function () {
18
19         $scope.context_org = egCore.org.get(egCore.auth.user().ws_ou());
20         $scope.date_filter = new Date();
21     });
22
23     $scope.closings = [];
24     var provider = egGridDataProvider.instance({
25       get : function(offset, count) {
26         $scope.refresh_generation = new Date().getTime();
27         $scope.closings = [];
28         var deferred = $q.defer();
29         egCore.startup.go().then(function(){egCore.pcrud.search(
30             'aoucd', 
31             { org_unit : $scope.context_org.id(),
32               "-or" : [
33                 { close_end : { ">=" : $scope.date_filter.toISOString() } },
34                 { "-and" : { emergency_closing : { "!=" : null }, "+aec" : { process_end_time : { "=" : null } } } }
35               ]
36             },
37             {   order_by : { aoucd : 'close_start' },
38                 limit : count,
39                 offset: offset,
40                 join  : { "aec" : { type : "left" } },
41                 flesh : 2,
42                 flesh_fields : { aoucd : ['emergency_closing'], aec : ['status'] }
43             }
44         ).then(function () {
45             return $scope.closings;
46         }, null, function(cl) {
47             if (!cl) return deferred.resolve();
48
49             var i = egCore.idl.toHash(cl);
50
51             function refresh_emergency_status (status) {
52                 if (status._generation == $scope.refresh_generation) {
53                     egCore.pcrud.retrieve('aecs',status.id).then(function(s) {
54
55                         status.circulations = s.circulations();
56                         status.circulations_complete = s.circulations_complete();
57                         status.holds = s.holds();
58                         status.holds_complete = s.holds_complete();
59                         status.reservations = s.reservations();
60                         status.reservations_complete = s.reservations_complete();
61
62                         if (s.process_start_time() && !s.process_end_time())
63                             $timeout(refresh_emergency_status, 2000, true, status);
64                     });
65                 }
66             }
67
68             var now = new Date();
69             var s = new Date(i.close_start);
70             var e = new Date(i.close_end);
71             i._duration = ((e - s) / 1000) + 1;
72             i._duration = '' + i._duration + ' seconds';
73             i._format = $scope.$root.egDateAndTimeFormat;
74
75             if (i.emergency_closing) {
76                 var x = i.emergency_closing.status.circulations - i.emergency_closing.status.circulations_complete;
77                 x += i.emergency_closing.status.holds - i.emergency_closing.status.holds_complete;
78                 x += i.emergency_closing.status.reservations - i.emergency_closing.status.reservations_complete;
79
80                 if (i.emergency_closing.process_end_time) {
81                     i._text_class = 'rounded bg-success';
82                 } else { // still work to do!
83                     i._text_class = 'rounded bg-primary';
84                     i.emergency_closing.status._generation = $scope.refresh_generation;;
85                     refresh_emergency_status(i.emergency_closing.status);
86                 }
87             } else {
88                 i._text_class = 'hidden';
89             }
90
91             $scope.closings.push(i);
92             return i;
93         }).then(deferred.resolve, null, deferred.notify)});
94
95         return deferred.promise;
96       }
97     });
98
99     $scope.gridDataProvider = provider;
100
101     $scope.refresh_page = function () {
102         $scope.closings = [];
103         $timeout(function(){provider.refresh()});
104     }
105
106     $scope.org_changed = $scope.refresh_page;
107     $scope.$watch('date_filter', $scope.refresh_page);
108
109     function spawn_editor(cl, action) {
110         var deferred = $q.defer();
111         $uibModal.open({
112             templateUrl: './admin/local/actor/edit_closed_dates',
113             backdrop: 'static',
114             controller:
115                 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
116                 $scope.focusMe = true;
117                 $scope.args = {};
118                 $scope.args.create_aec = false;
119                 $scope.args.apply_to_all = false;
120                 $scope.args.process_immediately = false;
121                 $scope.args.type = /^[t1]/.test(cl.multi_day()) ? 'multi' : /^[t1]/.test(cl.full_day()) ? 'full' : 'detailed';
122                 $scope.args.is_not_detailed = $scope.args.type == 'detailed' ? false : true;
123                 $scope.args.aoucd = cl;
124                 $scope.args.aec = cl.emergency_closing();
125
126                 $scope.unprocessed = true;
127                 if ($scope.args.aec) {
128                     $scope.args.aoucd.emergency_closing($scope.args.aec.id()); // detatch for now
129                     $scope.unprocessed = $scope.args.aec.process_start_time() ? false : true;
130                     $scope.args.create_aec = $scope.unprocessed;;
131                 }
132
133                 $scope.org_unit = egCore.org.get(cl.org_unit());
134                 $scope.args.start = new Date(cl.close_start());
135                 $scope.args.end = new Date(cl.close_end());
136                 $scope.args.reason = cl.reason();
137                 $scope.is_update = action == 'update';
138
139                 $scope.ok = function(args) { $uibModalInstance.close(args) }
140                 $scope.cancel = function () { $uibModalInstance.dismiss() }
141
142                 $scope.is_emergency = $scope.aec ? true : false;
143                 $scope.check_if_emergency = function () {
144                     if ($scope.args.aoucd.emergency_closing()) {
145                         ngToast.danger(egCore.strings.EMERGENCY_CLOSING);
146                         $scope.is_emergency = true;
147                         return $scope.is_emergency;
148                     }
149                     egCore.net.request(
150                         'open-ils.actor',
151                         'open-ils.actor.org_unit.closed_date.emergency_test',
152                         egCore.auth.token(), $scope.args.aoucd
153                     ).then(function (res) {
154                         $scope.duration_rule_count = parseInt(res);
155                         if ($scope.duration_rule_count) {
156                             ngToast.danger(egCore.strings.POSSIBLE_EMERGENCY_CLOSING);
157                             $scope.is_emergency = true;
158                         } else {
159                             $scope.is_emergency = false;
160                         }
161                     });
162                     return $scope.is_emergency;
163                 }
164
165                 $scope.update_org_unit = function () { $scope.args.aoucd.org_unit($scope.org_unit.id()) }
166
167                 $scope.$watch('args.create_aec', function (n) {
168                     if (n) {
169                         if (!$scope.args.aec) $scope.args.aec = new egCore.idl.aec();
170                         if (!$scope.args.aec.creator()) $scope.args.aec.creator(egCore.auth.user().id());
171                     } else {
172                         if (!cl.emergency_closing()) $scope.args.aec = null;
173                     }
174                 });
175                 $scope.$watch('args.type', function (n) { $scope.args.is_not_detailed = n != 'detailed' });
176                 $scope.$watch('args.start', function (n) { $scope.args.aoucd.close_start(n.toISOString()); if (n) $scope.check_if_emergency() });
177                 $scope.$watch('args.end', function (n) { $scope.args.aoucd.close_end(n.toISOString()) });
178                 $scope.$watch('args.reason', function (n) { $scope.args.aoucd.reason(n) });
179              }]
180         }).result.then(function(args) {
181
182             var start = args.start;
183             var end = args.end;
184
185             args.aoucd.full_day(0);
186             args.aoucd.multi_day(0);
187
188             if (args.type == 'full') {
189                 args.aoucd.full_day(1);
190                 end = new Date(start);
191             }
192
193             if (args.type == 'multi') {
194                 args.aoucd.full_day(1);
195                 args.aoucd.multi_day(1);
196             }
197
198             if (args.type == 'multi' || args.type == 'full') {
199
200                 start.setHours(0);
201                 start.setMinutes(0);
202                 start.setSeconds(0);
203
204                 end.setHours(23);
205                 end.setMinutes(59);
206                 end.setSeconds(59);
207             }
208
209             args.aoucd.close_start(start.toISOString());
210             args.aoucd.close_end(end.toISOString());
211
212             if (action == 'create') {
213                 var new_aoucd_list = [];
214                 var libraries = [args.aoucd.org_unit()];
215
216                 if (args.apply_to_all)
217                     libraries = egCore.org.descendants(args.aoucd.org_unit(), true);
218
219                 egProgressDialog.open({
220                     label : egCore.strings.CREATING_CLOSINGS,
221                     value : 0,
222                     max   : libraries.length
223                 });
224
225                 function make_next () {
226                     var l = libraries.shift();
227
228                     if (!l) {
229                         egProgressDialog.close();
230                         $scope.refresh_page();
231                         deferred.resolve([new_aoucd_list,args]);
232                     } else {
233                         args.aoucd.org_unit(l);
234                         egCore.net.request(
235                             'open-ils.actor',
236                             'open-ils.actor.org_unit.closed.create',
237                             egCore.auth.token(), args.aoucd, args.aec 
238                         ).then(function (new_aoucd) {
239                             new_aoucd_list.push(new_aoucd);
240                             make_next();
241                         });
242                     }
243                 }
244
245                 make_next();
246             } else {
247                 egCore.net.request(
248                     'open-ils.actor',
249                     'open-ils.actor.org_unit.closed.update',
250                     egCore.auth.token(), args.aoucd
251                 ).then(function(new_aoucd) { deferred.resolve([new_aoucd,args]); });
252             }
253         });
254         return deferred.promise;
255     }
256
257     $scope.create_aoucd = function() {
258         var cl = new egCore.idl.aoucd();
259         cl.isnew(1);
260         cl.full_day(1);
261         cl.org_unit($scope.context_org.id());
262         cl.close_start(new Date().toISOString());
263         cl.close_end(cl.close_start());
264
265         spawn_editor(cl, 'create').then(function(content) {
266             if (content && content[0] && content[1] && content[1].process_immediately) {
267
268                 function process_next () {
269                     var new_cl = content[0].shift();
270
271                     if (!new_cl) {
272                         $scope.refresh_page();
273                     } else {
274                         egProgressDialog.open({label : egCore.strings.PROCESSING_EMERGENCY});
275                         egCore.net.request(
276                             'open-ils.actor',
277                             'open-ils.actor.org_unit.closed.process_emergency',
278                             egCore.auth.token(), new_cl
279                         ).then(
280                             function () {
281                                 egProgressDialog.close();
282                                 $scope.gridControls.refresh();
283                                 process_next();
284                             },
285                             null,
286                             function (status) {
287                                 if (status.stage != 'start' && status.stage != 'complete') {
288                                     egProgressDialog.update({
289                                         value : status[status.stage][0],
290                                         max   : status[status.stage][1],
291                                     });
292                                 }
293                             }
294                         );
295                     }
296                 }
297
298                 process_next();
299             } else {
300                 $scope.refresh_page();
301             }
302         });
303     }
304
305     $scope.update_aoucd = function(selected) {
306         if (!selected || !selected.length) return;
307
308         egCore.pcrud.retrieve('aoucd', selected[0].id, {
309             join  : { "aec" : { type : "left" } },
310             flesh : 2,
311             flesh_fields : { aoucd : ['emergency_closing'], aec : ['status'] }
312         }).then(function(cl) {
313             spawn_editor(cl, 'update').then(function(content) {
314                 $scope.gridControls.refresh();
315                 if (content && content[0] && content[1] && content[1].process_immediately) {
316                     egCore.net.request(
317                         'open-ils.actor',
318                         'open-ils.actor.org_unit.closed.process_emergency',
319                         egCore.auth.token(), content[0]
320                     );
321                 }
322             });
323         });
324     }
325
326     $scope.delete_aoucd = function(selected) {
327         if (!selected || !selected.length) return;
328
329         egCore.pcrud.retrieve('aoucd', selected[0].id).then(function(cl) {
330             egConfirmDialog.open(
331                 egCore.strings.CONFIRM_CLOSED_DELETE,
332                 egCore.strings.CONFIRM_CLOSED_DELETE_BODY,
333                 { reason : cl.reason(), org : egCore.org.get(cl.org_unit()) }
334             ).result.then(function() {
335                 egCore.net.request(
336                     'open-ils.actor',
337                     'open-ils.actor.org_unit.closed.delete',
338                     egCore.auth.token(), cl
339                 ).then(function() {
340                     $scope.gridControls.refresh();
341                 });
342             });            
343         });
344     }
345
346     $scope.gridControls = {
347         activateItem : function (item) {
348             $scope.update_aoucd([item]);
349         }
350     };
351
352 }])
353