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