]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/item/app.js
LP#1685929 - Extract itemSvc from item module to core
[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?|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         }]);
147     }
148
149     $scope.selectedHoldingsMissing = function () {
150         itemSvc.selectedHoldingsMissing([{
151             id : $scope.args.copyId,
152             barcode : $scope.args.copyBarcode
153         }]);
154     }
155
156     $scope.selectedHoldingsVolCopyAdd = function () {
157         itemSvc.spawnHoldingsAdd([{
158             id : $scope.args.copyId,
159             'call_number.owning_lib' : $scope.args.cnOwningLib,
160             'call_number.record.id' : $scope.args.recordId,
161             barcode : $scope.args.copyBarcode
162         }],true,false);
163     }
164     $scope.selectedHoldingsCopyAdd = function () {
165         itemSvc.spawnHoldingsAdd([{
166             id : $scope.args.copyId,
167             'call_number.id' : $scope.args.cnId,
168             'call_number.owning_lib' : $scope.args.cnOwningLib,
169             'call_number.record.id' : $scope.args.recordId,
170             barcode : $scope.args.copyBarcode
171         }],false,true);
172     }
173
174     $scope.selectedHoldingsVolCopyEdit = function () {
175         itemSvc.spawnHoldingsEdit([{
176             id : $scope.args.copyId,
177             'call_number.id' : $scope.args.cnId,
178             'call_number.owning_lib' : $scope.args.cnOwningLib,
179             'call_number.record.id' : $scope.args.recordId,
180             barcode : $scope.args.copyBarcode
181         }],false,false);
182     }
183     $scope.selectedHoldingsVolEdit = function () {
184         itemSvc.spawnHoldingsEdit([{
185             id : $scope.args.copyId,
186             'call_number.id' : $scope.args.cnId,
187             'call_number.owning_lib' : $scope.args.cnOwningLib,
188             'call_number.record.id' : $scope.args.recordId,
189             barcode : $scope.args.copyBarcode
190         }],false,true);
191     }
192     $scope.selectedHoldingsCopyEdit = function () {
193         itemSvc.spawnHoldingsEdit([{
194             id : $scope.args.copyId,
195             'call_number.id' : $scope.args.cnId,
196             'call_number.owning_lib' : $scope.args.cnOwningLib,
197             'call_number.record.id' : $scope.args.recordId,
198             barcode : $scope.args.copyBarcode
199         }],true,false);
200     }
201
202     $scope.replaceBarcodes = function() {
203         itemSvc.replaceBarcodes([{
204             id : $scope.args.copyId,
205             barcode : $scope.args.copyBarcode
206         }]);
207     }
208
209     $scope.changeItemOwningLib = function() {
210         itemSvc.changeItemOwningLib([{
211             id : $scope.args.copyId,
212             'call_number.id' : $scope.args.cnId,
213             'call_number.owning_lib' : $scope.args.cnOwningLib,
214             'call_number.record.id' : $scope.args.recordId,
215             'call_number.label' : $scope.args.cnLabel,
216             'call_number.label_class' : $scope.args.cnLabelClass,
217             'call_number.prefix.id' : $scope.args.cnPrefixId,
218             'call_number.suffix.id' : $scope.args.cnSuffixId,
219             barcode : $scope.args.copyBarcode
220         }]);
221     }
222
223     $scope.transferItems = function (){
224         itemSvc.transferItems([{
225             id : $scope.args.copyId,
226             barcode : $scope.args.copyBarcode
227         }]);
228     }
229
230 }])
231
232 /**
233  * List view - grid stuff
234  */
235 .controller('ListCtrl', 
236        ['$scope','$q','$routeParams','$location','$timeout','$window','egCore','egGridDataProvider','egItem','egUser','$uibModal','egCirc','egConfirmDialog',
237 function($scope , $q , $routeParams , $location , $timeout , $window , egCore , egGridDataProvider , itemSvc , egUser , $uibModal , egCirc , egConfirmDialog) {
238     var copyId = [];
239     var cp_list = $routeParams.idList;
240     if (cp_list) {
241         copyId = cp_list.split(',');
242     }
243
244     $scope.context.page = 'list';
245
246     /*
247     var provider = egGridDataProvider.instance();
248     provider.get = function(offset, count) {
249     }
250     */
251
252     $scope.gridDataProvider = egGridDataProvider.instance({
253         get : function(offset, count) {
254             //return provider.arrayNotifier(itemSvc.copies, offset, count);
255             return this.arrayNotifier(itemSvc.copies, offset, count);
256         }
257     });
258
259     // If a copy was just displayed in the detail view, ensure it's
260     // focused in the list view.
261     var selected = false;
262     var copyGrid = $scope.gridControls = {
263         itemRetrieved : function(item) {
264             if (selected || !itemSvc.copy) return;
265             if (itemSvc.copy.id() == item.id) {
266                 copyGrid.selectItems([item.index]);
267                 selected = true;
268             }
269         }
270     };
271
272     $scope.$watch('barcodesFromFile', function(newVal, oldVal) {
273         if (newVal && newVal != oldVal) {
274             $scope.args.barcode = '';
275             var barcodes = [];
276
277             angular.forEach(newVal.split(/\n/), function(line) {
278                 if (!line) return;
279                 // scrub any trailing spaces or commas from the barcode
280                 line = line.replace(/(.*?)($|\s.*|,.*)/,'$1');
281                 barcodes.push(line);
282             });
283
284             itemSvc.fetch(barcodes).then(
285                 function() {
286                     copyGrid.refresh();
287                     copyGrid.selectItems([itemSvc.copies[0].index]);
288                 }
289             );
290         }
291     });
292
293     $scope.context.search = function(args) {
294         if (!args.barcode) return;
295         $scope.context.itemNotFound = false;
296         itemSvc.fetch(args.barcode).then(function(res) {
297             if (res) {
298                 copyGrid.refresh();
299                 copyGrid.selectItems([res.index]);
300                 $scope.args.barcode = '';
301             } else {
302                 $scope.context.itemNotFound = true;
303                 egCore.audio.play('warning.item_status.itemNotFound');
304             }
305             $scope.context.selectBarcode = true;
306         })
307     }
308
309     var add_barcode_to_list = function (b) {
310         //console.log('listCtrl: add_barcode_to_list',b);
311         $scope.context.search({barcode:b});
312     }
313     itemSvc.add_barcode_to_list = add_barcode_to_list;
314
315     $scope.context.toggleDisplay = function() {
316         var item = copyGrid.selectedItems()[0];
317         if (item) 
318             $location.path('/cat/item/' + item.id);
319     }
320
321     $scope.context.show_triggered_events = function() {
322         var item = copyGrid.selectedItems()[0];
323         if (item) 
324             $location.path('/cat/item/' + item.id + '/triggered_events');
325     }
326
327     function gatherSelectedRecordIds () {
328         var rid_list = [];
329         angular.forEach(
330             copyGrid.selectedItems(),
331             function (item) {
332                 if (rid_list.indexOf(item['call_number.record.id']) == -1)
333                     rid_list.push(item['call_number.record.id'])
334             }
335         );
336         return rid_list;
337     }
338
339     function gatherSelectedVolumeIds (rid) {
340         var cn_id_list = [];
341         angular.forEach(
342             copyGrid.selectedItems(),
343             function (item) {
344                 if (rid && item['call_number.record.id'] != rid) return;
345                 if (cn_id_list.indexOf(item['call_number.id']) == -1)
346                     cn_id_list.push(item['call_number.id'])
347             }
348         );
349         return cn_id_list;
350     }
351
352     function gatherSelectedHoldingsIds (rid) {
353         var cp_id_list = [];
354         angular.forEach(
355             copyGrid.selectedItems(),
356             function (item) {
357                 if (rid && item['call_number.record.id'] != rid) return;
358                 cp_id_list.push(item.id)
359             }
360         );
361         return cp_id_list;
362     }
363
364     $scope.add_copies_to_bucket = function() {
365         var copy_list = gatherSelectedHoldingsIds();
366         itemSvc.add_copies_to_bucket(copy_list);
367     }
368
369     $scope.need_one_selected = function() {
370         var items = $scope.gridControls.selectedItems();
371         if (items.length == 1) return false;
372         return true;
373     };
374
375     $scope.make_copies_bookable = function() {
376         itemSvc.make_copies_bookable(copyGrid.selectedItems());
377     }
378
379     $scope.book_copies_now = function() {
380         itemSvc.book_copies_now(copyGrid.selectedItems());
381     }
382
383     $scope.requestItems = function() {
384         var copy_list = gatherSelectedHoldingsIds();
385         itemSvc.requestItems(copy_list);
386     }
387
388     $scope.replaceBarcodes = function() {
389         itemSvc.replaceBarcodes(copyGrid.selectedItems());
390     }
391
392     $scope.attach_to_peer_bib = function() {
393         itemSvc.attach_to_peer_bib(copyGrid.selectedItems());
394     }
395
396     $scope.selectedHoldingsCopyDelete = function () {
397         itemSvc.selectedHoldingsCopyDelete(copyGrid.selectedItems());
398     }
399
400     $scope.selectedHoldingsItemStatusTgrEvt= function() {
401         var item = copyGrid.selectedItems()[0];
402         if (item)
403             $location.path('/cat/item/' + item.id + '/triggered_events');
404     }
405
406     $scope.selectedHoldingsItemStatusHolds= function() {
407         var item = copyGrid.selectedItems()[0];
408         if (item)
409             $location.path('/cat/item/' + item.id + '/holds');
410     }
411
412     $scope.cancel_transit = function () {
413         itemSvc.cancel_transit(copyGrid.selectedItems());
414     }
415
416     $scope.selectedHoldingsDamaged = function () {
417         itemSvc.selectedHoldingsDamaged(copyGrid.selectedItems());
418     }
419
420     $scope.selectedHoldingsMissing = function () {
421         itemSvc.selectedHoldingsMissing(copyGrid.selectedItems());
422     }
423
424     $scope.checkin = function () {
425         itemSvc.checkin(copyGrid.selectedItems());
426     }
427
428     $scope.renew = function () {
429         itemSvc.renew(copyGrid.selectedItems());
430     }
431
432     $scope.selectedHoldingsVolCopyAdd = function () {
433         itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),true,false);
434     }
435     $scope.selectedHoldingsCopyAdd = function () {
436         itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),false,true);
437     }
438
439     $scope.showBibHolds = function () {
440         angular.forEach(gatherSelectedRecordIds(), function (r) {
441             var url = egCore.env.basePath + 'cat/catalog/record/' + r + '/holds';
442             $timeout(function() { $window.open(url, '_blank') });
443         });
444     }
445
446     $scope.selectedHoldingsVolCopyEdit = function () {
447         itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,false);
448     }
449     $scope.selectedHoldingsVolEdit = function () {
450         itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,true);
451     }
452     $scope.selectedHoldingsCopyEdit = function () {
453         itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),true,false);
454     }
455
456     $scope.changeItemOwningLib = function() {
457         itemSvc.changeItemOwningLib(copyGrid.selectedItems());
458     }
459
460     $scope.transferItems = function (){
461         itemSvc.transferItems(copyGrid.selectedItems());
462     }
463
464     $scope.print_labels = function() {
465         egCore.net.request(
466             'open-ils.actor',
467             'open-ils.actor.anon_cache.set_value',
468             null, 'print-labels-these-copies', {
469                 copies : gatherSelectedHoldingsIds()
470             }
471         ).then(function(key) {
472             if (key) {
473                 var url = egCore.env.basePath + 'cat/printlabels/' + key;
474                 $timeout(function() { $window.open(url, '_blank') });
475             } else {
476                 alert('Could not create anonymous cache key!');
477             }
478         });
479     }
480
481     $scope.print_list = function() {
482         var print_data = { copies : copyGrid.allItems() };
483
484         if (print_data.copies.length == 0) return $q.when();
485
486         return egCore.print.print({
487             template : 'item_status',
488             scope : print_data
489         });
490     }
491
492     if (copyId.length > 0) {
493         itemSvc.fetch(null,copyId).then(
494             function() {
495                 copyGrid.refresh();
496             }
497         );
498     }
499
500 }])
501
502 /**
503  * Detail view -- shows one copy
504  */
505 .controller('ViewCtrl', 
506        ['$scope','$q','$location','$routeParams','$timeout','$window','egCore','egItem','egBilling',
507 function($scope , $q , $location , $routeParams , $timeout , $window , egCore , itemSvc , egBilling) {
508     var copyId = $routeParams.id;
509     $scope.args.copyId = copyId;
510     $scope.tab = $routeParams.tab || 'summary';
511     $scope.context.page = 'detail';
512     $scope.summaryRecord = null;
513
514     $scope.edit = false;
515     if ($scope.tab == 'edit') {
516         $scope.tab = 'summary';
517         $scope.edit = true;
518     }
519
520
521     // use the cached record info
522     if (itemSvc.copy) {
523         $scope.recordId = itemSvc.copy.call_number().record().id();
524         $scope.args.recordId = $scope.recordId;
525         $scope.args.cnId = itemSvc.copy.call_number().id();
526         $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
527         $scope.args.cnLabel = itemSvc.copy.call_number().label();
528         $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
529         $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
530         $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
531         $scope.args.copyBarcode = itemSvc.copy.barcode();
532     }
533
534     function loadCopy(barcode) {
535         $scope.context.itemNotFound = false;
536
537         // Avoid re-fetching the same copy while jumping tabs.
538         // In addition to being quicker, this helps to avoid flickering
539         // of the top panel which is always visible in the detail view.
540         //
541         // 'barcode' represents the loading of a new item - refetch it
542         // regardless of whether it matches the current item.
543         if (!barcode && itemSvc.copy && itemSvc.copy.id() == copyId) {
544             $scope.copy = itemSvc.copy;
545             $scope.recordId = itemSvc.copy.call_number().record().id();
546             $scope.args.recordId = $scope.recordId;
547             $scope.args.cnId = itemSvc.copy.call_number().id();
548             $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
549             $scope.args.cnLabel = itemSvc.copy.call_number().label();
550             $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
551             $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
552             $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
553             $scope.args.copyBarcode = itemSvc.copy.barcode();
554             return $q.when();
555         }
556
557         delete $scope.copy;
558         delete itemSvc.copy;
559
560         var deferred = $q.defer();
561         itemSvc.fetch(barcode, copyId, true).then(function(res) {
562             $scope.context.selectBarcode = true;
563
564             if (!res) {
565                 copyId = null;
566                 $scope.context.itemNotFound = true;
567                 egCore.audio.play('warning.item_status.itemNotFound');
568                 deferred.reject(); // avoid propagation of data fetch calls
569                 return;
570             }
571
572             var copy = res.copy;
573             itemSvc.copy = copy;
574
575
576             $scope.copy = copy;
577             $scope.recordId = copy.call_number().record().id();
578             $scope.args.recordId = $scope.recordId;
579             $scope.args.cnId = itemSvc.copy.call_number().id();
580             $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
581             $scope.args.cnLabel = itemSvc.copy.call_number().label();
582             $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
583             $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
584             $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
585             $scope.args.copyBarcode = copy.barcode();
586             $scope.args.barcode = '';
587
588             // locally flesh org units
589             copy.circ_lib(egCore.org.get(copy.circ_lib()));
590             copy.call_number().owning_lib(
591                 egCore.org.get(copy.call_number().owning_lib()));
592
593             var r = copy.call_number().record();
594             if (r.owner()) r.owner(egCore.org.get(r.owner())); 
595
596             // make boolean for auto-magic true/false display
597             angular.forEach(
598                 ['ref','opac_visible','holdable','circulate'],
599                 function(field) { copy[field](Boolean(copy[field]() == 't')) }
600             );
601
602             // finally, if this is a different copy, redirect.
603             // Note that we flesh first since the copy we just
604             // fetched will be used after the redirect.
605             if (copyId && copyId != copy.id()) {
606                 // if a new barcode is scanned in the detail view,
607                 // update the url to match the ID of the new copy
608                 $location.path('/cat/item/' + copy.id() + '/' + $scope.tab);
609                 deferred.reject(); // avoid propagation of data fetch calls
610                 return;
611             }
612             copyId = copy.id();
613
614             deferred.resolve();
615         });
616
617         return deferred.promise;
618     }
619
620     // if loadPrev load the two most recent circulations
621     function loadCurrentCirc(loadPrev) {
622         delete $scope.circ;
623         delete $scope.circ_summary;
624         delete $scope.prev_circ_summary;
625         delete $scope.prev_circ_usr;
626         if (!copyId) return;
627         
628         egCore.pcrud.search('aacs', 
629             {target_copy : copyId},
630             {   flesh : 2,
631                 flesh_fields : {
632                     aacs : [
633                         'usr',
634                         'workstation',                                         
635                         'checkin_workstation',                                 
636                         'duration_rule',                                       
637                         'max_fine_rule',                                       
638                         'recurring_fine_rule'   
639                     ],
640                     au : ['card']
641                 },
642                 order_by : {aacs : 'xact_start desc'}, 
643                 limit :  1
644             }
645
646         ).then(null, null, function(circ) {
647             $scope.circ = circ;
648
649             // load the chain for this circ
650             egCore.net.request(
651                 'open-ils.circ',
652                 'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
653                 egCore.auth.token(), $scope.circ.id()
654             ).then(function(summary) {
655                 $scope.circ_summary = summary;
656             });
657
658             if (!loadPrev) return;
659
660             // load the chain for the previous circ, plus the user
661             egCore.net.request(
662                 'open-ils.circ',
663                 'open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary',
664                 egCore.auth.token(), $scope.circ.id()
665
666             ).then(null, null, function(summary) {
667                 $scope.prev_circ_summary = summary.summary;
668
669                 if (summary.usr) { // aged circs have no 'usr'.
670                     egCore.pcrud.retrieve('au', summary.usr,
671                         {flesh : 1, flesh_fields : {au : ['card']}})
672
673                     .then(function(user) { $scope.prev_circ_usr = user });
674                 }
675             });
676         });
677     }
678
679     var maxHistory;
680     function fetchMaxCircHistory() {
681         if (maxHistory) return $q.when(maxHistory);
682         return egCore.org.settings(
683             'circ.item_checkout_history.max')
684         .then(function(set) {
685             maxHistory = set['circ.item_checkout_history.max'] || 4;
686             return maxHistory;
687         });
688     }
689
690     $scope.addBilling = function(circ) {
691         egBilling.showBillDialog({
692             xact_id : circ.id(),
693             patron : circ.usr()
694         });
695     }
696
697     $scope.retrieveAllPatrons = function() {
698         var users = new Set();
699         angular.forEach($scope.circ_list.map(function(circ) { return circ.usr(); }),function(usr) {
700             // aged circs have no 'usr'.
701             if (usr) users.add(usr);
702         });
703         users.forEach(function(usr) {
704             $timeout(function() {
705                 var url = $location.absUrl().replace(
706                     /\/cat\/.*/,
707                     '/circ/patron/' + usr.id() + '/checkout');
708                 $window.open(url, '_blank')
709             });
710         });
711     }
712
713     function loadCircHistory() {
714         $scope.circ_list = [];
715
716         var copy_org = 
717             itemSvc.copy.call_number().id() == -1 ?
718             itemSvc.copy.circ_lib().id() :
719             itemSvc.copy.call_number().owning_lib().id()
720
721         // there is an extra layer of permissibility over circ
722         // history views
723         egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
724         .then(function(orgIds) {
725
726             if (orgIds.indexOf(copy_org) == -1) {
727                 console.log('User is not allowed to view circ history');
728                 return $q.when(0);
729             }
730
731             return fetchMaxCircHistory();
732
733         }).then(function(count) {
734
735             egCore.pcrud.search('aacs', 
736                 {target_copy : copyId},
737                 {   flesh : 2,
738                     flesh_fields : {
739                         aacs : [
740                             'usr',
741                             'workstation',                                         
742                             'checkin_workstation',                                 
743                             'recurring_fine_rule'   
744                         ],
745                         au : ['card']
746                     },
747                     order_by : {aacs : 'xact_start desc'}, 
748                     limit :  count
749                 }
750
751             ).then(null, null, function(circ) {
752
753                 // flesh circ_lib locally
754                 circ.circ_lib(egCore.org.get(circ.circ_lib()));
755                 circ.checkin_lib(egCore.org.get(circ.checkin_lib()));
756                 $scope.circ_list.push(circ);
757             });
758         });
759     }
760
761
762     function loadCircCounts() {
763
764         delete $scope.circ_counts;
765         $scope.total_circs = 0;
766         $scope.total_circs_this_year = 0;
767         $scope.total_circs_prev_year = 0;
768         if (!copyId) return;
769
770         egCore.pcrud.search('circbyyr', 
771             {copy : copyId}, null, {atomic : true})
772
773         .then(function(counts) {
774             $scope.circ_counts = counts;
775
776             angular.forEach(counts, function(count) {
777                 $scope.total_circs += Number(count.count());
778             });
779
780             var this_year = counts.filter(function(c) {
781                 return c.year() == new Date().getFullYear();
782             });
783
784             $scope.total_circs_this_year = 
785                 this_year.length ? this_year[0].count() : 0;
786
787             var prev_year = counts.filter(function(c) {
788                 return c.year() == new Date().getFullYear() - 1;
789             });
790
791             $scope.total_circs_prev_year = 
792                 prev_year.length ? prev_year[0].count() : 0;
793
794         });
795     }
796
797     function loadHolds() {
798         delete $scope.hold;
799         if (!copyId) return;
800
801         egCore.pcrud.search('ahr', 
802             {   current_copy : copyId, 
803                 cancel_time : null, 
804                 fulfillment_time : null,
805                 capture_time : {'<>' : null}
806             }, {
807                 flesh : 2,
808                 flesh_fields : {
809                     ahr : ['requestor', 'usr'],
810                     au  : ['card']
811                 }
812             }
813         ).then(null, null, function(hold) {
814             $scope.hold = hold;
815             hold.pickup_lib(egCore.org.get(hold.pickup_lib()));
816             if (hold.current_shelf_lib()) {
817                 hold.current_shelf_lib(
818                     egCore.org.get(hold.current_shelf_lib()));
819             }
820             hold.behind_desk(Boolean(hold.behind_desk() == 't'));
821         });
822     }
823
824     function loadTransits() {
825         delete $scope.transit;
826         delete $scope.hold_transit;
827         if (!copyId) return;
828
829         egCore.pcrud.search('atc', 
830             {target_copy : copyId},
831             {order_by : {atc : 'source_send_time DESC'}}
832
833         ).then(null, null, function(transit) {
834             $scope.transit = transit;
835             transit.source(egCore.org.get(transit.source()));
836             transit.dest(egCore.org.get(transit.dest()));
837         })
838     }
839
840
841     // we don't need all data on all tabs, so fetch what's needed when needed.
842     function loadTabData() {
843         switch($scope.tab) {
844             case 'summary':
845                 loadCurrentCirc();
846                 loadCircCounts();
847                 break;
848
849             case 'circs':
850                 loadCurrentCirc(true);
851                 break;
852
853             case 'circ_list':
854                 loadCircHistory();
855                 break;
856
857             case 'holds':
858                 loadHolds()
859                 loadTransits();
860                 break;
861
862             case 'triggered_events':
863                 var url = $location.absUrl().replace(/\/staff.*/, '/actor/user/event_log');
864                 url += '?copy_id=' + encodeURIComponent(copyId);
865                 $scope.triggered_events_url = url;
866                 $scope.funcs = {};
867         }
868
869         if ($scope.edit) {
870             egCore.net.request(
871                 'open-ils.actor',
872                 'open-ils.actor.anon_cache.set_value',
873                 null, 'edit-these-copies', {
874                     record_id: $scope.recordId,
875                     copies: [copyId],
876                     hide_vols : true,
877                     hide_copies : false
878                 }
879             ).then(function(key) {
880                 if (key) {
881                     var url = egCore.env.basePath + 'cat/volcopy/' + key;
882                     $window.location.href = url;
883                 } else {
884                     alert('Could not create anonymous cache key!');
885                 }
886             });
887         }
888
889         return;
890     }
891
892     $scope.context.toggleDisplay = function() {
893         $location.path('/cat/item/search');
894     }
895
896     // handle the barcode scan box, which will replace our current copy
897     $scope.context.search = function(args) {
898         loadCopy(args.barcode).then(loadTabData);
899     }
900
901     $scope.context.show_triggered_events = function() {
902         $location.path('/cat/item/' + copyId + '/triggered_events');
903     }
904
905     loadCopy().then(loadTabData);
906 }])