1 angular.module('egAdminClosed',
2 ['ngRoute','ui.bootstrap','egCoreMod','egUiMod','egGridMod','ngToast'])
4 .config(['ngToastProvider', function(ngToastProvider) {
5 ngToastProvider.configure({
6 verticalPosition: 'bottom',
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) {
17 egCore.startup.go().then(function () {
19 $scope.context_org = egCore.org.get(egCore.auth.user().ws_ou());
20 $scope.date_filter = new Date();
24 var provider = egGridDataProvider.instance({
25 get : function(offset, count) {
26 $scope.refresh_generation = new Date().getTime();
28 var deferred = $q.defer();
29 egCore.startup.go().then(function(){egCore.pcrud.search(
31 { org_unit : $scope.context_org.id(),
33 { close_end : { ">=" : $scope.date_filter.toISOString() } },
34 { "-and" : { emergency_closing : { "!=" : null }, "+aec" : { process_end_time : { "=" : null } } } }
37 { order_by : { aoucd : 'close_start' },
40 join : { "aec" : { type : "left" } },
42 flesh_fields : { aoucd : ['emergency_closing'], aec : ['status'] }
45 return $scope.closings;
46 }, null, function(cl) {
47 if (!cl) return deferred.resolve();
49 var i = egCore.idl.toHash(cl);
51 function refresh_emergency_status (status) {
52 if (status._generation == $scope.refresh_generation) {
53 egCore.pcrud.retrieve('aecs',status.id).then(function(s) {
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();
62 if (s.process_start_time() && !s.process_end_time())
63 $timeout(refresh_emergency_status, 2000, true, status);
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';
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;
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);
87 i._text_class = 'hidden';
90 $scope.closings.push(i);
92 }).then(deferred.resolve, null, deferred.notify)});
94 return deferred.promise;
98 $scope.gridDataProvider = provider;
100 $scope.refresh_page = function () {
101 $scope.closings = [];
102 $timeout(function(){provider.refresh()});
105 $scope.org_changed = $scope.refresh_page;
106 $scope.$watch('date_filter', $scope.refresh_page);
108 function spawn_editor(cl, action) {
109 var deferred = $q.defer();
111 templateUrl: './admin/local/actor/edit_closed_dates',
114 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
115 $scope.focusMe = true;
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();
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;;
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';
138 $scope.ok = function(args) { $uibModalInstance.close(args) }
139 $scope.cancel = function () { $uibModalInstance.dismiss() }
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;
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;
158 $scope.is_emergency = false;
161 return $scope.is_emergency;
164 $scope.update_org_unit = function () { $scope.args.aoucd.org_unit($scope.org_unit.id()) }
166 $scope.$watch('args.create_aec', function (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());
171 if (!cl.emergency_closing()) $scope.args.aec = null;
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) });
179 }).result.then(function(args) {
181 var start = args.start;
184 args.aoucd.full_day(0);
185 args.aoucd.multi_day(0);
187 if (args.type == 'full') {
188 args.aoucd.full_day(1);
189 end = new Date(start);
192 if (args.type == 'multi') {
193 args.aoucd.full_day(1);
194 args.aoucd.multi_day(1);
197 if (args.type == 'multi' || args.type == 'full') {
208 args.aoucd.close_start(start.toISOString());
209 args.aoucd.close_end(end.toISOString());
211 if (action == 'create') {
212 var new_aoucd_list = [];
213 var libraries = [args.aoucd.org_unit()];
215 if (args.apply_to_all)
216 libraries = egCore.org.descendants(args.aoucd.org_unit(), true);
218 egProgressDialog.open({
219 label : egCore.strings.CREATING_CLOSINGS,
221 max : libraries.length
224 function make_next () {
225 var l = libraries.shift();
228 egProgressDialog.close();
229 $scope.refresh_page();
230 deferred.resolve([new_aoucd_list,args]);
232 args.aoucd.org_unit(l);
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);
248 'open-ils.actor.org_unit.closed.update',
249 egCore.auth.token(), args.aoucd
250 ).then(function(new_aoucd) { deferred.resolve([new_aoucd,args]); });
253 return deferred.promise;
256 $scope.create_aoucd = function() {
257 var cl = new egCore.idl.aoucd();
260 cl.org_unit($scope.context_org.id());
261 cl.close_start(new Date().toISOString());
262 cl.close_end(cl.close_start());
264 spawn_editor(cl, 'create').then(function(content) {
265 if (content && content[0] && content[1] && content[1].process_immediately) {
267 function process_next () {
268 var new_cl = content[0].shift();
271 $scope.refresh_page();
273 egProgressDialog.open({label : egCore.strings.PROCESSING_EMERGENCY});
276 'open-ils.actor.org_unit.closed.process_emergency',
277 egCore.auth.token(), new_cl
280 egProgressDialog.close();
281 $scope.gridControls.refresh();
286 if (status.stage != 'start' && status.stage != 'complete') {
287 egProgressDialog.update({
288 value : status[status.stage][0],
289 max : status[status.stage][1],
299 $scope.refresh_page();
304 $scope.update_aoucd = function(selected) {
305 if (!selected || !selected.length) return;
307 egCore.pcrud.retrieve('aoucd', selected[0].id, {
308 join : { "aec" : { type : "left" } },
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) {
317 'open-ils.actor.org_unit.closed.process_emergency',
318 egCore.auth.token(), content[0]
325 $scope.delete_aoucd = function(selected) {
326 if (!selected || !selected.length) return;
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() {
336 'open-ils.actor.org_unit.closed.delete',
337 egCore.auth.token(), cl
339 $scope.gridControls.refresh();
345 $scope.gridControls = {
346 activateItem : function (item) {
347 $scope.update_aoucd([item]);