]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/item/app.js
LP#1676608: conditionally enable manage copy alerts button on item status details
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / item / app.js
1 /**
2  * Item Display
3  */
4
5 angular.module('egItemStatus', 
6     ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod', 'egGridMod', 'egUserMod'])
7
8 .filter('boolText', function(){
9     return function (v) {
10         return v == 't';
11     }
12 })
13
14 .config(function($routeProvider, $locationProvider, $compileProvider) {
15     $locationProvider.html5Mode(true);
16     $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|mailto|blob):/); // grid export
17         
18     var resolver = {delay : function(egStartup) {return egStartup.go()}};
19
20     // search page shows the list view by default
21     $routeProvider.when('/cat/item/search', {
22         templateUrl: './cat/item/t_list',
23         controller: 'ListCtrl',
24         resolve : resolver
25     });
26
27     // search page shows the list view by default
28     $routeProvider.when('/cat/item/search/:idList', {
29         templateUrl: './cat/item/t_list',
30         controller: 'ListCtrl',
31         resolve : resolver
32     });
33
34     $routeProvider.when('/cat/item/:id', {
35         templateUrl: './cat/item/t_view',
36         controller: 'ViewCtrl',
37         resolve : resolver
38     });
39
40     $routeProvider.when('/cat/item/:id/:tab', {
41         templateUrl: './cat/item/t_view',
42         controller: 'ViewCtrl',
43         resolve : resolver
44     });
45
46     // default page / bucket view
47     $routeProvider.otherwise({redirectTo : '/cat/item/search'});
48 })
49
50 /**
51  * Search bar along the top of the page.
52  * Parent scope for list and detail views
53  */
54 .controller('SearchCtrl', 
55        ['$scope','$location','$timeout','egCore','egGridDataProvider','egItem',
56 function($scope , $location , $timeout , egCore , egGridDataProvider , itemSvc) {
57     $scope.args = {}; // search args
58
59     // sub-scopes (search / detail-view) apply their version 
60     // of retrieval function to $scope.context.search
61     // and display toggling via $scope.context.toggleDisplay
62     $scope.context = {
63         selectBarcode : true
64     };
65
66     $scope.toggleView = function($event) {
67         $scope.context.toggleDisplay();
68         $event.preventDefault(); // avoid form submission
69     }
70
71     // The functions that follow in this controller are never called
72     // when the List View is active, only the Detail View.
73     
74     // In this context, we're only ever dealing with 1 item, so
75     // we can simply refresh the page.  These various itemSvc
76     // functions used to live in the ListCtrl, but they're now
77     // shared between SearchCtrl (for Actions for the Detail View)
78     // and ListCtrl (Actions in the egGrid)
79     itemSvc.add_barcode_to_list = function(b) {
80         //console.log('SearchCtrl: add_barcode_to_list',b);
81         // timeout so audible can happen upon checkin
82         $timeout(function() { location.href = location.href; }, 1000);
83     }
84
85     $scope.add_copies_to_bucket = function() {
86         itemSvc.add_copies_to_bucket([$scope.args.copyId]);
87     }
88
89     $scope.make_copies_bookable = function() {
90         itemSvc.make_copies_bookable([{
91             id : $scope.args.copyId,
92             'call_number.record.id' : $scope.args.recordId
93         }]);
94     }
95
96     $scope.book_copies_now = function() {
97         itemSvc.book_copies_now([{
98             id : $scope.args.copyId,
99             'call_number.record.id' : $scope.args.recordId
100         }]);
101     }
102
103     $scope.requestItems = function() {
104         itemSvc.requestItems([$scope.args.copyId]);
105     }
106
107     $scope.attach_to_peer_bib = function() {
108         itemSvc.attach_to_peer_bib([{
109             id : $scope.args.copyId,
110             barcode : $scope.args.copyBarcode
111         }]);
112     }
113
114     $scope.selectedHoldingsCopyDelete = function () {
115         itemSvc.selectedHoldingsCopyDelete([{
116             id : $scope.args.copyId,
117             barcode : $scope.args.copyBarcode
118         }]);
119     }
120
121     $scope.checkin = function () {
122         itemSvc.checkin([{
123             id : $scope.args.copyId,
124             barcode : $scope.args.copyBarcode
125         }]);
126     }
127
128     $scope.renew = function () {
129         itemSvc.renew([{
130             id : $scope.args.copyId,
131             barcode : $scope.args.copyBarcode
132         }]);
133     }
134
135     $scope.cancel_transit = function () {
136         itemSvc.cancel_transit([{
137             id : $scope.args.copyId,
138             barcode : $scope.args.copyBarcode
139         }]);
140     }
141
142     $scope.selectedHoldingsDamaged = function () {
143         itemSvc.selectedHoldingsDamaged([{
144             id : $scope.args.copyId,
145             barcode : $scope.args.copyBarcode,
146             refresh : true
147         }]);
148     }
149
150     $scope.selectedHoldingsMissing = function () {
151         itemSvc.selectedHoldingsMissing([{
152             id : $scope.args.copyId,
153             barcode : $scope.args.copyBarcode
154         }]);
155     }
156
157     $scope.selectedHoldingsVolCopyAdd = function () {
158         itemSvc.spawnHoldingsAdd([{
159             id : $scope.args.copyId,
160             'call_number.owning_lib' : $scope.args.cnOwningLib,
161             'call_number.record.id' : $scope.args.recordId,
162             barcode : $scope.args.copyBarcode
163         }],true,false);
164     }
165     $scope.selectedHoldingsCopyAdd = function () {
166         itemSvc.spawnHoldingsAdd([{
167             id : $scope.args.copyId,
168             'call_number.id' : $scope.args.cnId,
169             'call_number.owning_lib' : $scope.args.cnOwningLib,
170             'call_number.record.id' : $scope.args.recordId,
171             barcode : $scope.args.copyBarcode
172         }],false,true);
173     }
174
175     $scope.selectedHoldingsVolCopyEdit = function () {
176         itemSvc.spawnHoldingsEdit([{
177             id : $scope.args.copyId,
178             'call_number.id' : $scope.args.cnId,
179             'call_number.owning_lib' : $scope.args.cnOwningLib,
180             'call_number.record.id' : $scope.args.recordId,
181             barcode : $scope.args.copyBarcode
182         }],false,false);
183     }
184     $scope.selectedHoldingsVolEdit = function () {
185         itemSvc.spawnHoldingsEdit([{
186             id : $scope.args.copyId,
187             'call_number.id' : $scope.args.cnId,
188             'call_number.owning_lib' : $scope.args.cnOwningLib,
189             'call_number.record.id' : $scope.args.recordId,
190             barcode : $scope.args.copyBarcode
191         }],false,true);
192     }
193     $scope.selectedHoldingsCopyEdit = function () {
194         itemSvc.spawnHoldingsEdit([{
195             id : $scope.args.copyId,
196             'call_number.id' : $scope.args.cnId,
197             'call_number.owning_lib' : $scope.args.cnOwningLib,
198             'call_number.record.id' : $scope.args.recordId,
199             barcode : $scope.args.copyBarcode
200         }],true,false);
201     }
202
203     $scope.replaceBarcodes = function() {
204         itemSvc.replaceBarcodes([{
205             id : $scope.args.copyId,
206             barcode : $scope.args.copyBarcode
207         }]);
208     }
209
210     $scope.changeItemOwningLib = function() {
211         itemSvc.changeItemOwningLib([{
212             id : $scope.args.copyId,
213             'call_number.id' : $scope.args.cnId,
214             'call_number.owning_lib' : $scope.args.cnOwningLib,
215             'call_number.record.id' : $scope.args.recordId,
216             'call_number.label' : $scope.args.cnLabel,
217             'call_number.label_class' : $scope.args.cnLabelClass,
218             'call_number.prefix.id' : $scope.args.cnPrefixId,
219             'call_number.suffix.id' : $scope.args.cnSuffixId,
220             barcode : $scope.args.copyBarcode
221         }]);
222     }
223
224     $scope.transferItems = function (){
225         itemSvc.transferItems([{
226             id : $scope.args.copyId,
227             barcode : $scope.args.copyBarcode
228         }]);
229     }
230
231 }])
232
233 /**
234  * List view - grid stuff
235  */
236 .controller('ListCtrl', 
237        ['$scope','$q','$routeParams','$location','$timeout','$window','egCore',
238         'egGridDataProvider','egItem','egUser','$uibModal','egCirc','egConfirmDialog',
239         'egProgressDialog',
240 function($scope , $q , $routeParams , $location , $timeout , $window , egCore , 
241          egGridDataProvider , itemSvc , egUser , $uibModal , egCirc , egConfirmDialog,
242          egProgressDialog) {
243
244     var copyId = [];
245     var cp_list = $routeParams.idList;
246     if (cp_list) {
247         copyId = cp_list.split(',');
248     }
249
250     $scope.context.page = 'list';
251
252     /*
253     var provider = egGridDataProvider.instance();
254     provider.get = function(offset, count) {
255     }
256     */
257
258     $scope.gridDataProvider = egGridDataProvider.instance({
259         get : function(offset, count) {
260             //return provider.arrayNotifier(itemSvc.copies, offset, count);
261             return this.arrayNotifier(itemSvc.copies, offset, count);
262         }
263     });
264
265     // If a copy was just displayed in the detail view, ensure it's
266     // focused in the list view.
267     var selected = false;
268     var copyGrid = $scope.gridControls = {
269         itemRetrieved : function(item) {
270             if (selected || !itemSvc.copy) return;
271             if (itemSvc.copy.id() == item.id) {
272                 copyGrid.selectItems([item.index]);
273                 selected = true;
274             }
275         }
276     };
277
278     $scope.$watch('barcodesFromFile', function(newVal, oldVal) {
279         if (newVal && newVal != oldVal) {
280             $scope.args.barcode = '';
281             var barcodes = [];
282
283             angular.forEach(newVal.split(/\n/), function(line) {
284                 if (!line) return;
285                 // scrub any trailing spaces or commas from the barcode
286                 line = line.replace(/(.*?)($|\s.*|,.*)/,'$1');
287                 barcodes.push(line);
288             });
289
290             // Serialize copy retrieval since there may be many, many copies.
291             function fetch_next_copy() {
292                 var barcode = barcodes.pop();
293                 egProgressDialog.increment();
294
295                 if (!barcode) { // All done here.
296                     egProgressDialog.close();
297                     copyGrid.refresh();
298                     copyGrid.selectItems([itemSvc.copies[0].index]);
299                     return;
300                 }
301
302                 itemSvc.fetch(barcode).then(fetch_next_copy);
303             }
304
305             if (barcodes.length) {
306                 egProgressDialog.open({value: 0, max: barcodes.length});
307                 fetch_next_copy();
308             }
309         }
310     });
311
312     $scope.context.search = function(args) {
313         if (!args.barcode) return;
314         $scope.context.itemNotFound = false;
315         itemSvc.fetch(args.barcode).then(function(res) {
316             if (res) {
317                 copyGrid.refresh();
318                 copyGrid.selectItems([res.index]);
319                 $scope.args.barcode = '';
320             } else {
321                 $scope.context.itemNotFound = true;
322                 egCore.audio.play('warning.item_status.itemNotFound');
323             }
324             $scope.context.selectBarcode = true;
325         })
326     }
327
328     var add_barcode_to_list = function (b) {
329         //console.log('listCtrl: add_barcode_to_list',b);
330         $scope.context.search({barcode:b});
331     }
332     itemSvc.add_barcode_to_list = add_barcode_to_list;
333
334     $scope.context.toggleDisplay = function() {
335         var item = copyGrid.selectedItems()[0];
336         if (item) 
337             $location.path('/cat/item/' + item.id);
338     }
339
340     $scope.context.show_triggered_events = function() {
341         var item = copyGrid.selectedItems()[0];
342         if (item) 
343             $location.path('/cat/item/' + item.id + '/triggered_events');
344     }
345
346     function gatherSelectedRecordIds () {
347         var rid_list = [];
348         angular.forEach(
349             copyGrid.selectedItems(),
350             function (item) {
351                 if (rid_list.indexOf(item['call_number.record.id']) == -1)
352                     rid_list.push(item['call_number.record.id'])
353             }
354         );
355         return rid_list;
356     }
357
358     function gatherSelectedVolumeIds (rid) {
359         var cn_id_list = [];
360         angular.forEach(
361             copyGrid.selectedItems(),
362             function (item) {
363                 if (rid && item['call_number.record.id'] != rid) return;
364                 if (cn_id_list.indexOf(item['call_number.id']) == -1)
365                     cn_id_list.push(item['call_number.id'])
366             }
367         );
368         return cn_id_list;
369     }
370
371     function gatherSelectedHoldingsIds (rid) {
372         var cp_id_list = [];
373         angular.forEach(
374             copyGrid.selectedItems(),
375             function (item) {
376                 if (rid && item['call_number.record.id'] != rid) return;
377                 cp_id_list.push(item.id)
378             }
379         );
380         return cp_id_list;
381     }
382
383     $scope.add_copies_to_bucket = function() {
384         var copy_list = gatherSelectedHoldingsIds();
385         itemSvc.add_copies_to_bucket(copy_list);
386     }
387
388     $scope.need_one_selected = function() {
389         var items = $scope.gridControls.selectedItems();
390         if (items.length == 1) return false;
391         return true;
392     };
393
394     $scope.make_copies_bookable = function() {
395         itemSvc.make_copies_bookable(copyGrid.selectedItems());
396     }
397
398     $scope.book_copies_now = function() {
399         itemSvc.book_copies_now(copyGrid.selectedItems());
400     }
401
402     $scope.requestItems = function() {
403         var copy_list = gatherSelectedHoldingsIds();
404         itemSvc.requestItems(copy_list);
405     }
406
407     $scope.replaceBarcodes = function() {
408         itemSvc.replaceBarcodes(copyGrid.selectedItems());
409     }
410
411     $scope.attach_to_peer_bib = function() {
412         itemSvc.attach_to_peer_bib(copyGrid.selectedItems());
413     }
414
415     $scope.selectedHoldingsCopyDelete = function () {
416         itemSvc.selectedHoldingsCopyDelete(copyGrid.selectedItems());
417     }
418
419     $scope.selectedHoldingsItemStatusTgrEvt= function() {
420         var item = copyGrid.selectedItems()[0];
421         if (item)
422             $location.path('/cat/item/' + item.id + '/triggered_events');
423     }
424
425     $scope.selectedHoldingsItemStatusHolds= function() {
426         var item = copyGrid.selectedItems()[0];
427         if (item)
428             $location.path('/cat/item/' + item.id + '/holds');
429     }
430
431     $scope.cancel_transit = function () {
432         itemSvc.cancel_transit(copyGrid.selectedItems());
433     }
434
435     $scope.selectedHoldingsDamaged = function () {
436         itemSvc.selectedHoldingsDamaged(copyGrid.selectedItems());
437     }
438
439     $scope.selectedHoldingsMissing = function () {
440         itemSvc.selectedHoldingsMissing(copyGrid.selectedItems());
441     }
442
443     $scope.checkin = function () {
444         itemSvc.checkin(copyGrid.selectedItems());
445     }
446
447     $scope.renew = function () {
448         itemSvc.renew(copyGrid.selectedItems());
449     }
450
451     $scope.selectedHoldingsVolCopyAdd = function () {
452         itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),true,false);
453     }
454     $scope.selectedHoldingsCopyAdd = function () {
455         itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),false,true);
456     }
457
458     $scope.selectedHoldingsCopyAlertsAdd = function(items) {
459         var copy_ids = [];
460         angular.forEach(items, function(item) {
461             if (item.id) copy_ids.push(item.id);
462         });
463         egCirc.add_copy_alerts(copy_ids).then(function() {
464             // update grid items?
465         });
466     }
467
468     $scope.selectedHoldingsCopyAlertsEdit = function(items) {
469         var copy_ids = [];
470         angular.forEach(items, function(item) {
471             if (item.id) copy_ids.push(item.id);
472         });
473         egCirc.manage_copy_alerts(copy_ids).then(function() {
474             // update grid items?
475         });
476     }
477
478     $scope.gridCellHandlers = {};
479     $scope.gridCellHandlers.copyAlertsEdit = function(id) {
480         egCirc.manage_copy_alerts([id]).then(function() {
481             // update grid items?
482         });
483     };
484
485     $scope.showBibHolds = function () {
486         angular.forEach(gatherSelectedRecordIds(), function (r) {
487             var url = egCore.env.basePath + 'cat/catalog/record/' + r + '/holds';
488             $timeout(function() { $window.open(url, '_blank') });
489         });
490     }
491
492     $scope.selectedHoldingsVolCopyEdit = function () {
493         itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,false);
494     }
495     $scope.selectedHoldingsVolEdit = function () {
496         itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,true);
497     }
498     $scope.selectedHoldingsCopyEdit = function () {
499         itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),true,false);
500     }
501
502     $scope.changeItemOwningLib = function() {
503         itemSvc.changeItemOwningLib(copyGrid.selectedItems());
504     }
505
506     $scope.transferItems = function (){
507         itemSvc.transferItems(copyGrid.selectedItems());
508     }
509
510     $scope.print_labels = function() {
511         egCore.net.request(
512             'open-ils.actor',
513             'open-ils.actor.anon_cache.set_value',
514             null, 'print-labels-these-copies', {
515                 copies : gatherSelectedHoldingsIds()
516             }
517         ).then(function(key) {
518             if (key) {
519                 var url = egCore.env.basePath + 'cat/printlabels/' + key;
520                 $timeout(function() { $window.open(url, '_blank') });
521             } else {
522                 alert('Could not create anonymous cache key!');
523             }
524         });
525     }
526
527     $scope.print_list = function() {
528         var print_data = { copies : copyGrid.allItems() };
529
530         if (print_data.copies.length == 0) return $q.when();
531
532         return egCore.print.print({
533             template : 'item_status',
534             scope : print_data
535         });
536     }
537
538     if (copyId.length > 0) {
539         itemSvc.fetch(null,copyId).then(
540             function() {
541                 copyGrid.refresh();
542             }
543         );
544     }
545
546 }])
547
548 /**
549  * Detail view -- shows one copy
550  */
551 .controller('ViewCtrl', 
552        ['$scope','$q','$location','$routeParams','$timeout','$window','egCore','egItem','egBilling','egCirc',
553 function($scope , $q , $location , $routeParams , $timeout , $window , egCore , itemSvc , egBilling , egCirc) {
554     var copyId = $routeParams.id;
555     $scope.args.copyId = copyId;
556     $scope.tab = $routeParams.tab || 'summary';
557     $scope.context.page = 'detail';
558     $scope.summaryRecord = null;
559
560     $scope.edit = false;
561     if ($scope.tab == 'edit') {
562         $scope.tab = 'summary';
563         $scope.edit = true;
564     }
565
566
567     // use the cached record info
568     if (itemSvc.copy) {
569         $scope.copy_alert_count = itemSvc.copy.copy_alerts().filter(function(aca) {
570             return !aca.ack_time();
571         }).length;
572         $scope.recordId = itemSvc.copy.call_number().record().id();
573         $scope.args.recordId = $scope.recordId;
574         $scope.args.cnId = itemSvc.copy.call_number().id();
575         $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
576         $scope.args.cnLabel = itemSvc.copy.call_number().label();
577         $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
578         $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
579         $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
580         $scope.args.copyBarcode = itemSvc.copy.barcode();
581     }
582
583     function loadCopy(barcode) {
584         $scope.context.itemNotFound = false;
585
586         // Avoid re-fetching the same copy while jumping tabs.
587         // In addition to being quicker, this helps to avoid flickering
588         // of the top panel which is always visible in the detail view.
589         //
590         // 'barcode' represents the loading of a new item - refetch it
591         // regardless of whether it matches the current item.
592         if (!barcode && itemSvc.copy && itemSvc.copy.id() == copyId) {
593             $scope.copy = itemSvc.copy;
594             $scope.copy_alert_count = itemSvc.copy.copy_alerts().filter(function(aca) {
595                 return !aca.ack_time();
596             }).length;
597             $scope.recordId = itemSvc.copy.call_number().record().id();
598             $scope.args.recordId = $scope.recordId;
599             $scope.args.cnId = itemSvc.copy.call_number().id();
600             $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
601             $scope.args.cnLabel = itemSvc.copy.call_number().label();
602             $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
603             $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
604             $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
605             $scope.args.copyBarcode = itemSvc.copy.barcode();
606             return $q.when();
607         }
608
609         delete $scope.copy;
610         delete itemSvc.copy;
611
612         var deferred = $q.defer();
613         itemSvc.fetch(barcode, copyId, true).then(function(res) {
614             $scope.context.selectBarcode = true;
615
616             if (!res) {
617                 copyId = null;
618                 $scope.context.itemNotFound = true;
619                 egCore.audio.play('warning.item_status.itemNotFound');
620                 deferred.reject(); // avoid propagation of data fetch calls
621                 return;
622             }
623
624             var copy = res.copy;
625             itemSvc.copy = copy;
626
627
628             $scope.copy = copy;
629             $scope.copy_alert_count = copy.copy_alerts().filter(function(aca) {
630                 return !aca.ack_time();
631             }).length;
632 console.debug($scope.copy_alert_count);
633             $scope.recordId = copy.call_number().record().id();
634             $scope.args.recordId = $scope.recordId;
635             $scope.args.cnId = itemSvc.copy.call_number().id();
636             $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
637             $scope.args.cnLabel = itemSvc.copy.call_number().label();
638             $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
639             $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
640             $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
641             $scope.args.copyBarcode = copy.barcode();
642             $scope.args.barcode = '';
643
644             // locally flesh org units
645             copy.circ_lib(egCore.org.get(copy.circ_lib()));
646             copy.call_number().owning_lib(
647                 egCore.org.get(copy.call_number().owning_lib()));
648
649             var r = copy.call_number().record();
650             if (r.owner()) r.owner(egCore.org.get(r.owner())); 
651
652             // make boolean for auto-magic true/false display
653             angular.forEach(
654                 ['ref','opac_visible','holdable','circulate'],
655                 function(field) { copy[field](Boolean(copy[field]() == 't')) }
656             );
657
658             // finally, if this is a different copy, redirect.
659             // Note that we flesh first since the copy we just
660             // fetched will be used after the redirect.
661             if (copyId && copyId != copy.id()) {
662                 // if a new barcode is scanned in the detail view,
663                 // update the url to match the ID of the new copy
664                 $location.path('/cat/item/' + copy.id() + '/' + $scope.tab);
665                 deferred.reject(); // avoid propagation of data fetch calls
666                 return;
667             }
668             copyId = copy.id();
669
670             deferred.resolve();
671         });
672
673         return deferred.promise;
674     }
675
676     // load the two most recent circulations in /circs tab
677     function loadCurrentCirc() {
678         delete $scope.circ;
679         delete $scope.circ_summary;
680         delete $scope.prev_circ_summary;
681         delete $scope.prev_circ_usr;
682         if (!copyId) return;
683         
684         var copy_org =
685             itemSvc.copy.call_number().id() == -1 ?
686             itemSvc.copy.circ_lib().id() :
687             itemSvc.copy.call_number().owning_lib().id();
688
689         // since a user can still view patron checkout history here, check perms
690         egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
691         .then(function(orgIds){
692             if(orgIds.indexOf(copy_org) == -1){
693                 console.warn('User is not allowed to view circ history!');
694                 $q.when(0);
695             }
696
697             return fetchMaxCircHistory();
698         })
699         .then(function(maxHistCount){
700
701             if (!maxHistCount) $scope.isMaxCircHistoryZero = true;
702
703             egCore.pcrud.search('aacs',
704                 {target_copy : copyId},
705                 {   flesh : 2,
706                     flesh_fields : {
707                         aacs : [
708                             'usr',
709                             'workstation',
710                             'checkin_workstation',
711                             'duration_rule',
712                             'max_fine_rule',
713                             'recurring_fine_rule'
714                         ],
715                         au : ['card']
716                     },
717                     order_by : {aacs : 'xact_start desc'},
718                     limit :  1
719                 }
720
721             ).then(null, null, function(circ) {
722                 $scope.circ = circ;
723
724                 if (!circ) return $q.when();
725
726                 // load the chain for this circ
727                 egCore.net.request(
728                     'open-ils.circ',
729                     'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
730                     egCore.auth.token(), $scope.circ.id()
731                 ).then(function(summary) {
732                     $scope.circ_summary = summary;
733                 });
734
735                 if (maxHistCount <= 1) return;
736
737                 // load the chain for the previous circ, plus the user
738                 egCore.net.request(
739                     'open-ils.circ',
740                     'open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary',
741                     egCore.auth.token(), $scope.circ.id()
742
743                 ).then(null, null, function(summary) {
744                     $scope.prev_circ_summary = summary.summary;
745
746                     if (summary.usr) { // aged circs have no 'usr'.
747                         egCore.pcrud.retrieve('au', summary.usr,
748                             {flesh : 1, flesh_fields : {au : ['card']}})
749
750                         .then(function(user) { $scope.prev_circ_usr = user });
751                     }
752                 });
753             });
754         })
755     }
756
757     var maxHistory;
758     function fetchMaxCircHistory() {
759         if (maxHistory) return $q.when(maxHistory);
760         return egCore.org.settings(
761             'circ.item_checkout_history.max')
762         .then(function(set) {
763             maxHistory = set['circ.item_checkout_history.max'] || 4;
764             return Number(maxHistory);
765         });
766     }
767
768     $scope.addBilling = function(circ) {
769         egBilling.showBillDialog({
770             xact_id : circ.id(),
771             patron : circ.usr()
772         });
773     }
774
775     $scope.retrieveAllPatrons = function() {
776         var users = new Set();
777         angular.forEach($scope.circ_list.map(function(circ) { return circ.usr(); }),function(usr) {
778             // aged circs have no 'usr'.
779             if (usr) users.add(usr);
780         });
781         users.forEach(function(usr) {
782             $timeout(function() {
783                 var url = $location.absUrl().replace(
784                     /\/cat\/.*/,
785                     '/circ/patron/' + usr.id() + '/checkout');
786                 $window.open(url, '_blank')
787             });
788         });
789     }
790
791     // load data for /circ_list tab
792     function loadCircHistory() {
793         $scope.circ_list = [];
794
795         var copy_org = 
796             itemSvc.copy.call_number().id() == -1 ?
797             itemSvc.copy.circ_lib().id() :
798             itemSvc.copy.call_number().owning_lib().id();
799
800         // there is an extra layer of permissibility over circ
801         // history views
802         egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
803         .then(function(orgIds) {
804
805             if (orgIds.indexOf(copy_org) == -1) {
806                 console.log('User is not allowed to view circ history');
807                 return $q.when(0);
808             }
809
810             return fetchMaxCircHistory();
811
812         }).then(function(maxHistCount) {
813
814             if(!maxHistCount) $scope.isMaxCircHistoryZero = true;
815
816             egCore.pcrud.search('aacs',
817                 {target_copy : copyId},
818                 {   flesh : 2,
819                     flesh_fields : {
820                         aacs : [
821                             'usr',
822                             'workstation',
823                             'checkin_workstation',
824                             'recurring_fine_rule'
825                         ],
826                         au : ['card']
827                     },
828                     order_by : {aacs : 'xact_start desc'},
829                     // fetch at least one to see if copy ever circulated
830                     limit : $scope.isMaxCircHistoryZero ? 1 : maxHistCount
831                 }
832
833             ).then(null, null, function(circ) {
834
835                 $scope.circ = circ;
836
837                 // flesh circ_lib locally
838                 circ.circ_lib(egCore.org.get(circ.circ_lib()));
839                 circ.checkin_lib(egCore.org.get(circ.checkin_lib()));
840                 $scope.circ_list.push(circ);
841             });
842         });
843     }
844
845
846     function loadCircCounts() {
847
848         delete $scope.circ_counts;
849         $scope.total_circs = 0;
850         $scope.total_circs_this_year = 0;
851         $scope.total_circs_prev_year = 0;
852         if (!copyId) return;
853
854         egCore.pcrud.search('circbyyr', 
855             {copy : copyId}, null, {atomic : true})
856
857         .then(function(counts) {
858             $scope.circ_counts = counts;
859
860             angular.forEach(counts, function(count) {
861                 $scope.total_circs += Number(count.count());
862             });
863
864             var this_year = counts.filter(function(c) {
865                 return c.year() == new Date().getFullYear();
866             });
867
868             $scope.total_circs_this_year = 
869                 this_year.length ? this_year[0].count() : 0;
870
871             var prev_year = counts.filter(function(c) {
872                 return c.year() == new Date().getFullYear() - 1;
873             });
874
875             $scope.total_circs_prev_year = 
876                 prev_year.length ? prev_year[0].count() : 0;
877
878         });
879     }
880
881     function loadHolds() {
882         delete $scope.hold;
883         if (!copyId) return;
884
885         egCore.pcrud.search('ahr', 
886             {   current_copy : copyId, 
887                 cancel_time : null, 
888                 fulfillment_time : null,
889                 capture_time : {'<>' : null}
890             }, {
891                 flesh : 2,
892                 flesh_fields : {
893                     ahr : ['requestor', 'usr'],
894                     au  : ['card']
895                 }
896             }
897         ).then(null, null, function(hold) {
898             $scope.hold = hold;
899             hold.pickup_lib(egCore.org.get(hold.pickup_lib()));
900             if (hold.current_shelf_lib()) {
901                 hold.current_shelf_lib(
902                     egCore.org.get(hold.current_shelf_lib()));
903             }
904             hold.behind_desk(Boolean(hold.behind_desk() == 't'));
905         });
906     }
907
908     function loadMostRecentTransit() {
909         delete $scope.transit;
910         delete $scope.hold_transit;
911         if (!copyId) return;
912
913         egCore.pcrud.search('atc', 
914             {target_copy : copyId},
915             {
916                 order_by : {atc : 'source_send_time DESC'},
917                 limit : 1
918             }
919
920         ).then(null, null, function(transit) {
921             // use progress callback since we'll get up to one result
922             $scope.transit = transit;
923             transit.source(egCore.org.get(transit.source()));
924             transit.dest(egCore.org.get(transit.dest()));
925         })
926     }
927
928
929     // we don't need all data on all tabs, so fetch what's needed when needed.
930     function loadTabData() {
931         switch($scope.tab) {
932             case 'summary':
933                 loadCurrentCirc();
934                 loadCircCounts();
935                 break;
936
937             case 'circs':
938                 loadCurrentCirc();
939                 break;
940
941             case 'circ_list':
942                 loadCircHistory();
943                 break;
944
945             case 'holds':
946                 loadHolds()
947                 loadMostRecentTransit();
948                 break;
949
950             case 'triggered_events':
951                 var url = $location.absUrl().replace(/\/staff.*/, '/actor/user/event_log');
952                 url += '?copy_id=' + encodeURIComponent(copyId);
953                 $scope.triggered_events_url = url;
954                 $scope.funcs = {};
955         }
956
957         if ($scope.edit) {
958             egCore.net.request(
959                 'open-ils.actor',
960                 'open-ils.actor.anon_cache.set_value',
961                 null, 'edit-these-copies', {
962                     record_id: $scope.recordId,
963                     copies: [copyId],
964                     hide_vols : true,
965                     hide_copies : false
966                 }
967             ).then(function(key) {
968                 if (key) {
969                     var url = egCore.env.basePath + 'cat/volcopy/' + key;
970                     $window.location.href = url;
971                 } else {
972                     alert('Could not create anonymous cache key!');
973                 }
974             });
975         }
976
977         return;
978     }
979
980     $scope.addCopyAlerts = function(copy_id) {
981         egCirc.add_copy_alerts([copy_id]).then(function() {
982             // force a refresh
983             loadCopy($scope.copy.barcode()).then(loadTabData);
984         });
985     }
986     $scope.manageCopyAlerts = function(copy_id) {
987         egCirc.manage_copy_alerts([copy_id]).then(function() {
988             // force a refresh
989             loadCopy($scope.copy.barcode()).then(loadTabData);
990         });
991     }
992
993     $scope.context.toggleDisplay = function() {
994         $location.path('/cat/item/search');
995     }
996
997     // handle the barcode scan box, which will replace our current copy
998     $scope.context.search = function(args) {
999         loadCopy(args.barcode).then(loadTabData);
1000     }
1001
1002     $scope.context.show_triggered_events = function() {
1003         $location.path('/cat/item/' + copyId + '/triggered_events');
1004     }
1005
1006     loadCopy().then(loadTabData);
1007 }])