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