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