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';
73 i._format = $scope.$root.egDateAndTimeFormat;
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;
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);
88 i._text_class = 'hidden';
91 $scope.closings.push(i);
93 }).then(deferred.resolve, null, deferred.notify)});
95 return deferred.promise;
99 $scope.gridDataProvider = provider;
101 $scope.refresh_page = function () {
102 $scope.closings = [];
103 $timeout(function(){provider.refresh()});
106 $scope.org_changed = $scope.refresh_page;
107 $scope.$watch('date_filter', $scope.refresh_page);
109 function spawn_editor(cl, action) {
110 var deferred = $q.defer();
112 templateUrl: './admin/local/actor/edit_closed_dates',
115 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
116 $scope.focusMe = true;
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();
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;;
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';
139 $scope.ok = function(args) { $uibModalInstance.close(args) }
140 $scope.cancel = function () { $uibModalInstance.dismiss() }
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;
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;
159 $scope.is_emergency = false;
162 return $scope.is_emergency;
165 $scope.update_org_unit = function () { $scope.args.aoucd.org_unit($scope.org_unit.id()) }
167 $scope.$watch('args.create_aec', function (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());
172 if (!cl.emergency_closing()) $scope.args.aec = null;
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) });
180 }).result.then(function(args) {
182 var start = args.start;
185 args.aoucd.full_day(0);
186 args.aoucd.multi_day(0);
188 if (args.type == 'full') {
189 args.aoucd.full_day(1);
190 end = new Date(start);
193 if (args.type == 'multi') {
194 args.aoucd.full_day(1);
195 args.aoucd.multi_day(1);
198 if (args.type == 'multi' || args.type == 'full') {
209 args.aoucd.close_start(start.toISOString());
210 args.aoucd.close_end(end.toISOString());
212 if (action == 'create') {
213 var new_aoucd_list = [];
214 var libraries = [args.aoucd.org_unit()];
216 if (args.apply_to_all)
217 libraries = egCore.org.descendants(args.aoucd.org_unit(), true);
219 egProgressDialog.open({
220 label : egCore.strings.CREATING_CLOSINGS,
222 max : libraries.length
225 function make_next () {
226 var l = libraries.shift();
229 egProgressDialog.close();
230 $scope.refresh_page();
231 deferred.resolve([new_aoucd_list,args]);
233 args.aoucd.org_unit(l);
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);
249 'open-ils.actor.org_unit.closed.update',
250 egCore.auth.token(), args.aoucd
251 ).then(function(new_aoucd) { deferred.resolve([new_aoucd,args]); });
254 return deferred.promise;
257 $scope.create_aoucd = function() {
258 var cl = new egCore.idl.aoucd();
261 cl.org_unit($scope.context_org.id());
262 cl.close_start(new Date().toISOString());
263 cl.close_end(cl.close_start());
265 spawn_editor(cl, 'create').then(function(content) {
266 if (content && content[0] && content[1] && content[1].process_immediately) {
268 function process_next () {
269 var new_cl = content[0].shift();
272 $scope.refresh_page();
274 egProgressDialog.open({label : egCore.strings.PROCESSING_EMERGENCY});
277 'open-ils.actor.org_unit.closed.process_emergency',
278 egCore.auth.token(), new_cl
281 egProgressDialog.close();
282 $scope.gridControls.refresh();
287 if (status.stage != 'start' && status.stage != 'complete') {
288 egProgressDialog.update({
289 value : status[status.stage][0],
290 max : status[status.stage][1],
300 $scope.refresh_page();
305 $scope.update_aoucd = function(selected) {
306 if (!selected || !selected.length) return;
308 egCore.pcrud.retrieve('aoucd', selected[0].id, {
309 join : { "aec" : { type : "left" } },
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) {
318 'open-ils.actor.org_unit.closed.process_emergency',
319 egCore.auth.token(), content[0]
326 $scope.delete_aoucd = function(selected) {
327 if (!selected || !selected.length) return;
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() {
337 'open-ils.actor.org_unit.closed.delete',
338 egCore.auth.token(), cl
340 $scope.gridControls.refresh();
346 $scope.gridControls = {
347 activateItem : function (item) {
348 $scope.update_aoucd([item]);