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