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