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