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