]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/item/app.js
LP#1729922: correctly display most recent transit in item status
[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','$location','$timeout','egCore','egGridDataProvider','egItem',
56 function($scope , $location , $timeout , egCore , egGridDataProvider , itemSvc) {
57     $scope.args = {}; // search args
58
59     // sub-scopes (search / detail-view) apply their version 
60     // of retrieval function to $scope.context.search
61     // and display toggling via $scope.context.toggleDisplay
62     $scope.context = {
63         selectBarcode : true
64     };
65
66     $scope.toggleView = function($event) {
67         $scope.context.toggleDisplay();
68         $event.preventDefault(); // avoid form submission
69     }
70
71     // The functions that follow in this controller are never called
72     // when the List View is active, only the Detail View.
73     
74     // In this context, we're only ever dealing with 1 item, so
75     // we can simply refresh the page.  These various itemSvc
76     // functions used to live in the ListCtrl, but they're now
77     // shared between SearchCtrl (for Actions for the Detail View)
78     // and ListCtrl (Actions in the egGrid)
79     itemSvc.add_barcode_to_list = function(b) {
80         //console.log('SearchCtrl: add_barcode_to_list',b);
81         // timeout so audible can happen upon checkin
82         $timeout(function() { location.href = location.href; }, 1000);
83     }
84
85     $scope.add_copies_to_bucket = function() {
86         itemSvc.add_copies_to_bucket([$scope.args.copyId]);
87     }
88
89     $scope.make_copies_bookable = function() {
90         itemSvc.make_copies_bookable([{
91             id : $scope.args.copyId,
92             'call_number.record.id' : $scope.args.recordId
93         }]);
94     }
95
96     $scope.book_copies_now = function() {
97         itemSvc.book_copies_now([{
98             id : $scope.args.copyId,
99             'call_number.record.id' : $scope.args.recordId
100         }]);
101     }
102
103     $scope.requestItems = function() {
104         itemSvc.requestItems([$scope.args.copyId]);
105     }
106
107     $scope.attach_to_peer_bib = function() {
108         itemSvc.attach_to_peer_bib([{
109             id : $scope.args.copyId,
110             barcode : $scope.args.copyBarcode
111         }]);
112     }
113
114     $scope.selectedHoldingsCopyDelete = function () {
115         itemSvc.selectedHoldingsCopyDelete([{
116             id : $scope.args.copyId,
117             barcode : $scope.args.copyBarcode
118         }]);
119     }
120
121     $scope.checkin = function () {
122         itemSvc.checkin([{
123             id : $scope.args.copyId,
124             barcode : $scope.args.copyBarcode
125         }]);
126     }
127
128     $scope.renew = function () {
129         itemSvc.renew([{
130             id : $scope.args.copyId,
131             barcode : $scope.args.copyBarcode
132         }]);
133     }
134
135     $scope.cancel_transit = function () {
136         itemSvc.cancel_transit([{
137             id : $scope.args.copyId,
138             barcode : $scope.args.copyBarcode
139         }]);
140     }
141
142     $scope.selectedHoldingsDamaged = function () {
143         itemSvc.selectedHoldingsDamaged([{
144             id : $scope.args.copyId,
145             barcode : $scope.args.copyBarcode,
146             refresh : true
147         }]);
148     }
149
150     $scope.selectedHoldingsMissing = function () {
151         itemSvc.selectedHoldingsMissing([{
152             id : $scope.args.copyId,
153             barcode : $scope.args.copyBarcode
154         }]);
155     }
156
157     $scope.selectedHoldingsVolCopyAdd = function () {
158         itemSvc.spawnHoldingsAdd([{
159             id : $scope.args.copyId,
160             'call_number.owning_lib' : $scope.args.cnOwningLib,
161             'call_number.record.id' : $scope.args.recordId,
162             barcode : $scope.args.copyBarcode
163         }],true,false);
164     }
165     $scope.selectedHoldingsCopyAdd = function () {
166         itemSvc.spawnHoldingsAdd([{
167             id : $scope.args.copyId,
168             'call_number.id' : $scope.args.cnId,
169             'call_number.owning_lib' : $scope.args.cnOwningLib,
170             'call_number.record.id' : $scope.args.recordId,
171             barcode : $scope.args.copyBarcode
172         }],false,true);
173     }
174
175     $scope.selectedHoldingsVolCopyEdit = function () {
176         itemSvc.spawnHoldingsEdit([{
177             id : $scope.args.copyId,
178             'call_number.id' : $scope.args.cnId,
179             'call_number.owning_lib' : $scope.args.cnOwningLib,
180             'call_number.record.id' : $scope.args.recordId,
181             barcode : $scope.args.copyBarcode
182         }],false,false);
183     }
184     $scope.selectedHoldingsVolEdit = function () {
185         itemSvc.spawnHoldingsEdit([{
186             id : $scope.args.copyId,
187             'call_number.id' : $scope.args.cnId,
188             'call_number.owning_lib' : $scope.args.cnOwningLib,
189             'call_number.record.id' : $scope.args.recordId,
190             barcode : $scope.args.copyBarcode
191         }],false,true);
192     }
193     $scope.selectedHoldingsCopyEdit = function () {
194         itemSvc.spawnHoldingsEdit([{
195             id : $scope.args.copyId,
196             'call_number.id' : $scope.args.cnId,
197             'call_number.owning_lib' : $scope.args.cnOwningLib,
198             'call_number.record.id' : $scope.args.recordId,
199             barcode : $scope.args.copyBarcode
200         }],true,false);
201     }
202
203     $scope.replaceBarcodes = function() {
204         itemSvc.replaceBarcodes([{
205             id : $scope.args.copyId,
206             barcode : $scope.args.copyBarcode
207         }]);
208     }
209
210     $scope.changeItemOwningLib = function() {
211         itemSvc.changeItemOwningLib([{
212             id : $scope.args.copyId,
213             'call_number.id' : $scope.args.cnId,
214             'call_number.owning_lib' : $scope.args.cnOwningLib,
215             'call_number.record.id' : $scope.args.recordId,
216             'call_number.label' : $scope.args.cnLabel,
217             'call_number.label_class' : $scope.args.cnLabelClass,
218             'call_number.prefix.id' : $scope.args.cnPrefixId,
219             'call_number.suffix.id' : $scope.args.cnSuffixId,
220             barcode : $scope.args.copyBarcode
221         }]);
222     }
223
224     $scope.transferItems = function (){
225         itemSvc.transferItems([{
226             id : $scope.args.copyId,
227             barcode : $scope.args.copyBarcode
228         }]);
229     }
230
231 }])
232
233 /**
234  * List view - grid stuff
235  */
236 .controller('ListCtrl', 
237        ['$scope','$q','$routeParams','$location','$timeout','$window','egCore','egGridDataProvider','egItem','egUser','$uibModal','egCirc','egConfirmDialog',
238 function($scope , $q , $routeParams , $location , $timeout , $window , egCore , egGridDataProvider , itemSvc , egUser , $uibModal , egCirc , egConfirmDialog) {
239     var copyId = [];
240     var cp_list = $routeParams.idList;
241     if (cp_list) {
242         copyId = cp_list.split(',');
243     }
244
245     $scope.context.page = 'list';
246
247     /*
248     var provider = egGridDataProvider.instance();
249     provider.get = function(offset, count) {
250     }
251     */
252
253     $scope.gridDataProvider = egGridDataProvider.instance({
254         get : function(offset, count) {
255             //return provider.arrayNotifier(itemSvc.copies, offset, count);
256             return this.arrayNotifier(itemSvc.copies, offset, count);
257         }
258     });
259
260     // If a copy was just displayed in the detail view, ensure it's
261     // focused in the list view.
262     var selected = false;
263     var copyGrid = $scope.gridControls = {
264         itemRetrieved : function(item) {
265             if (selected || !itemSvc.copy) return;
266             if (itemSvc.copy.id() == item.id) {
267                 copyGrid.selectItems([item.index]);
268                 selected = true;
269             }
270         }
271     };
272
273     $scope.$watch('barcodesFromFile', function(newVal, oldVal) {
274         if (newVal && newVal != oldVal) {
275             $scope.args.barcode = '';
276             var barcodes = [];
277
278             angular.forEach(newVal.split(/\n/), function(line) {
279                 if (!line) return;
280                 // scrub any trailing spaces or commas from the barcode
281                 line = line.replace(/(.*?)($|\s.*|,.*)/,'$1');
282                 barcodes.push(line);
283             });
284
285             if (barcodes.length > 0) {
286                 var promises = [];
287                 angular.forEach(barcodes, function (b) {
288                     promises.push(itemSvc.fetch(b));
289                 });
290
291                 $q.all(promises).then(
292                     function() {
293                         copyGrid.refresh();
294                         copyGrid.selectItems([itemSvc.copies[0].index]);
295                     }
296                 );
297             }
298         }
299     });
300
301     $scope.context.search = function(args) {
302         if (!args.barcode) return;
303         $scope.context.itemNotFound = false;
304         itemSvc.fetch(args.barcode).then(function(res) {
305             if (res) {
306                 copyGrid.refresh();
307                 copyGrid.selectItems([res.index]);
308                 $scope.args.barcode = '';
309             } else {
310                 $scope.context.itemNotFound = true;
311                 egCore.audio.play('warning.item_status.itemNotFound');
312             }
313             $scope.context.selectBarcode = true;
314         })
315     }
316
317     var add_barcode_to_list = function (b) {
318         //console.log('listCtrl: add_barcode_to_list',b);
319         $scope.context.search({barcode:b});
320     }
321     itemSvc.add_barcode_to_list = add_barcode_to_list;
322
323     $scope.context.toggleDisplay = function() {
324         var item = copyGrid.selectedItems()[0];
325         if (item) 
326             $location.path('/cat/item/' + item.id);
327     }
328
329     $scope.context.show_triggered_events = function() {
330         var item = copyGrid.selectedItems()[0];
331         if (item) 
332             $location.path('/cat/item/' + item.id + '/triggered_events');
333     }
334
335     function gatherSelectedRecordIds () {
336         var rid_list = [];
337         angular.forEach(
338             copyGrid.selectedItems(),
339             function (item) {
340                 if (rid_list.indexOf(item['call_number.record.id']) == -1)
341                     rid_list.push(item['call_number.record.id'])
342             }
343         );
344         return rid_list;
345     }
346
347     function gatherSelectedVolumeIds (rid) {
348         var cn_id_list = [];
349         angular.forEach(
350             copyGrid.selectedItems(),
351             function (item) {
352                 if (rid && item['call_number.record.id'] != rid) return;
353                 if (cn_id_list.indexOf(item['call_number.id']) == -1)
354                     cn_id_list.push(item['call_number.id'])
355             }
356         );
357         return cn_id_list;
358     }
359
360     function gatherSelectedHoldingsIds (rid) {
361         var cp_id_list = [];
362         angular.forEach(
363             copyGrid.selectedItems(),
364             function (item) {
365                 if (rid && item['call_number.record.id'] != rid) return;
366                 cp_id_list.push(item.id)
367             }
368         );
369         return cp_id_list;
370     }
371
372     $scope.add_copies_to_bucket = function() {
373         var copy_list = gatherSelectedHoldingsIds();
374         itemSvc.add_copies_to_bucket(copy_list);
375     }
376
377     $scope.need_one_selected = function() {
378         var items = $scope.gridControls.selectedItems();
379         if (items.length == 1) return false;
380         return true;
381     };
382
383     $scope.make_copies_bookable = function() {
384         itemSvc.make_copies_bookable(copyGrid.selectedItems());
385     }
386
387     $scope.book_copies_now = function() {
388         itemSvc.book_copies_now(copyGrid.selectedItems());
389     }
390
391     $scope.requestItems = function() {
392         var copy_list = gatherSelectedHoldingsIds();
393         itemSvc.requestItems(copy_list);
394     }
395
396     $scope.replaceBarcodes = function() {
397         itemSvc.replaceBarcodes(copyGrid.selectedItems());
398     }
399
400     $scope.attach_to_peer_bib = function() {
401         itemSvc.attach_to_peer_bib(copyGrid.selectedItems());
402     }
403
404     $scope.selectedHoldingsCopyDelete = function () {
405         itemSvc.selectedHoldingsCopyDelete(copyGrid.selectedItems());
406     }
407
408     $scope.selectedHoldingsItemStatusTgrEvt= function() {
409         var item = copyGrid.selectedItems()[0];
410         if (item)
411             $location.path('/cat/item/' + item.id + '/triggered_events');
412     }
413
414     $scope.selectedHoldingsItemStatusHolds= function() {
415         var item = copyGrid.selectedItems()[0];
416         if (item)
417             $location.path('/cat/item/' + item.id + '/holds');
418     }
419
420     $scope.cancel_transit = function () {
421         itemSvc.cancel_transit(copyGrid.selectedItems());
422     }
423
424     $scope.selectedHoldingsDamaged = function () {
425         itemSvc.selectedHoldingsDamaged(copyGrid.selectedItems());
426     }
427
428     $scope.selectedHoldingsMissing = function () {
429         itemSvc.selectedHoldingsMissing(copyGrid.selectedItems());
430     }
431
432     $scope.checkin = function () {
433         itemSvc.checkin(copyGrid.selectedItems());
434     }
435
436     $scope.renew = function () {
437         itemSvc.renew(copyGrid.selectedItems());
438     }
439
440     $scope.selectedHoldingsVolCopyAdd = function () {
441         itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),true,false);
442     }
443     $scope.selectedHoldingsCopyAdd = function () {
444         itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),false,true);
445     }
446
447     $scope.showBibHolds = function () {
448         angular.forEach(gatherSelectedRecordIds(), function (r) {
449             var url = egCore.env.basePath + 'cat/catalog/record/' + r + '/holds';
450             $timeout(function() { $window.open(url, '_blank') });
451         });
452     }
453
454     $scope.selectedHoldingsVolCopyEdit = function () {
455         itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,false);
456     }
457     $scope.selectedHoldingsVolEdit = function () {
458         itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,true);
459     }
460     $scope.selectedHoldingsCopyEdit = function () {
461         itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),true,false);
462     }
463
464     $scope.changeItemOwningLib = function() {
465         itemSvc.changeItemOwningLib(copyGrid.selectedItems());
466     }
467
468     $scope.transferItems = function (){
469         itemSvc.transferItems(copyGrid.selectedItems());
470     }
471
472     $scope.print_labels = function() {
473         egCore.net.request(
474             'open-ils.actor',
475             'open-ils.actor.anon_cache.set_value',
476             null, 'print-labels-these-copies', {
477                 copies : gatherSelectedHoldingsIds()
478             }
479         ).then(function(key) {
480             if (key) {
481                 var url = egCore.env.basePath + 'cat/printlabels/' + key;
482                 $timeout(function() { $window.open(url, '_blank') });
483             } else {
484                 alert('Could not create anonymous cache key!');
485             }
486         });
487     }
488
489     $scope.print_list = function() {
490         var print_data = { copies : copyGrid.allItems() };
491
492         if (print_data.copies.length == 0) return $q.when();
493
494         return egCore.print.print({
495             template : 'item_status',
496             scope : print_data
497         });
498     }
499
500     if (copyId.length > 0) {
501         itemSvc.fetch(null,copyId).then(
502             function() {
503                 copyGrid.refresh();
504             }
505         );
506     }
507
508 }])
509
510 /**
511  * Detail view -- shows one copy
512  */
513 .controller('ViewCtrl', 
514        ['$scope','$q','$location','$routeParams','$timeout','$window','egCore','egItem','egBilling',
515 function($scope , $q , $location , $routeParams , $timeout , $window , egCore , itemSvc , egBilling) {
516     var copyId = $routeParams.id;
517     $scope.args.copyId = copyId;
518     $scope.tab = $routeParams.tab || 'summary';
519     $scope.context.page = 'detail';
520     $scope.summaryRecord = null;
521
522     $scope.edit = false;
523     if ($scope.tab == 'edit') {
524         $scope.tab = 'summary';
525         $scope.edit = true;
526     }
527
528
529     // use the cached record info
530     if (itemSvc.copy) {
531         $scope.recordId = itemSvc.copy.call_number().record().id();
532         $scope.args.recordId = $scope.recordId;
533         $scope.args.cnId = itemSvc.copy.call_number().id();
534         $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
535         $scope.args.cnLabel = itemSvc.copy.call_number().label();
536         $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
537         $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
538         $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
539         $scope.args.copyBarcode = itemSvc.copy.barcode();
540     }
541
542     function loadCopy(barcode) {
543         $scope.context.itemNotFound = false;
544
545         // Avoid re-fetching the same copy while jumping tabs.
546         // In addition to being quicker, this helps to avoid flickering
547         // of the top panel which is always visible in the detail view.
548         //
549         // 'barcode' represents the loading of a new item - refetch it
550         // regardless of whether it matches the current item.
551         if (!barcode && itemSvc.copy && itemSvc.copy.id() == copyId) {
552             $scope.copy = itemSvc.copy;
553             $scope.recordId = itemSvc.copy.call_number().record().id();
554             $scope.args.recordId = $scope.recordId;
555             $scope.args.cnId = itemSvc.copy.call_number().id();
556             $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
557             $scope.args.cnLabel = itemSvc.copy.call_number().label();
558             $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
559             $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
560             $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
561             $scope.args.copyBarcode = itemSvc.copy.barcode();
562             return $q.when();
563         }
564
565         delete $scope.copy;
566         delete itemSvc.copy;
567
568         var deferred = $q.defer();
569         itemSvc.fetch(barcode, copyId, true).then(function(res) {
570             $scope.context.selectBarcode = true;
571
572             if (!res) {
573                 copyId = null;
574                 $scope.context.itemNotFound = true;
575                 egCore.audio.play('warning.item_status.itemNotFound');
576                 deferred.reject(); // avoid propagation of data fetch calls
577                 return;
578             }
579
580             var copy = res.copy;
581             itemSvc.copy = copy;
582
583
584             $scope.copy = copy;
585             $scope.recordId = copy.call_number().record().id();
586             $scope.args.recordId = $scope.recordId;
587             $scope.args.cnId = itemSvc.copy.call_number().id();
588             $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
589             $scope.args.cnLabel = itemSvc.copy.call_number().label();
590             $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
591             $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
592             $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
593             $scope.args.copyBarcode = copy.barcode();
594             $scope.args.barcode = '';
595
596             // locally flesh org units
597             copy.circ_lib(egCore.org.get(copy.circ_lib()));
598             copy.call_number().owning_lib(
599                 egCore.org.get(copy.call_number().owning_lib()));
600
601             var r = copy.call_number().record();
602             if (r.owner()) r.owner(egCore.org.get(r.owner())); 
603
604             // make boolean for auto-magic true/false display
605             angular.forEach(
606                 ['ref','opac_visible','holdable','circulate'],
607                 function(field) { copy[field](Boolean(copy[field]() == 't')) }
608             );
609
610             // finally, if this is a different copy, redirect.
611             // Note that we flesh first since the copy we just
612             // fetched will be used after the redirect.
613             if (copyId && copyId != copy.id()) {
614                 // if a new barcode is scanned in the detail view,
615                 // update the url to match the ID of the new copy
616                 $location.path('/cat/item/' + copy.id() + '/' + $scope.tab);
617                 deferred.reject(); // avoid propagation of data fetch calls
618                 return;
619             }
620             copyId = copy.id();
621
622             deferred.resolve();
623         });
624
625         return deferred.promise;
626     }
627
628     // load the two most recent circulations in /circs tab
629     function loadCurrentCirc() {
630         delete $scope.circ;
631         delete $scope.circ_summary;
632         delete $scope.prev_circ_summary;
633         delete $scope.prev_circ_usr;
634         if (!copyId) return;
635         
636         var copy_org =
637             itemSvc.copy.call_number().id() == -1 ?
638             itemSvc.copy.circ_lib().id() :
639             itemSvc.copy.call_number().owning_lib().id();
640
641         // since a user can still view patron checkout history here, check perms
642         egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
643         .then(function(orgIds){
644             if(orgIds.indexOf(copy_org) == -1){
645                 console.warn('User is not allowed to view circ history!');
646                 $q.when(0);
647             }
648
649             return fetchMaxCircHistory();
650         })
651         .then(function(maxHistCount){
652
653             if (!maxHistCount) $scope.isMaxCircHistoryZero = true;
654
655             egCore.pcrud.search('aacs',
656                 {target_copy : copyId},
657                 {   flesh : 2,
658                     flesh_fields : {
659                         aacs : [
660                             'usr',
661                             'workstation',
662                             'checkin_workstation',
663                             'duration_rule',
664                             'max_fine_rule',
665                             'recurring_fine_rule'
666                         ],
667                         au : ['card']
668                     },
669                     order_by : {aacs : 'xact_start desc'},
670                     limit :  1
671                 }
672
673             ).then(null, null, function(circ) {
674                 $scope.circ = circ;
675
676                 if (!circ) return $q.when();
677
678                 // load the chain for this circ
679                 egCore.net.request(
680                     'open-ils.circ',
681                     'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
682                     egCore.auth.token(), $scope.circ.id()
683                 ).then(function(summary) {
684                     $scope.circ_summary = summary;
685                 });
686
687                 if (maxHistCount <= 1) return;
688
689                 // load the chain for the previous circ, plus the user
690                 egCore.net.request(
691                     'open-ils.circ',
692                     'open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary',
693                     egCore.auth.token(), $scope.circ.id()
694
695                 ).then(null, null, function(summary) {
696                     $scope.prev_circ_summary = summary.summary;
697
698                     if (summary.usr) { // aged circs have no 'usr'.
699                         egCore.pcrud.retrieve('au', summary.usr,
700                             {flesh : 1, flesh_fields : {au : ['card']}})
701
702                         .then(function(user) { $scope.prev_circ_usr = user });
703                     }
704                 });
705             });
706         })
707     }
708
709     var maxHistory;
710     function fetchMaxCircHistory() {
711         if (maxHistory) return $q.when(maxHistory);
712         return egCore.org.settings(
713             'circ.item_checkout_history.max')
714         .then(function(set) {
715             maxHistory = set['circ.item_checkout_history.max'] || 4;
716             return Number(maxHistory);
717         });
718     }
719
720     $scope.addBilling = function(circ) {
721         egBilling.showBillDialog({
722             xact_id : circ.id(),
723             patron : circ.usr()
724         });
725     }
726
727     $scope.retrieveAllPatrons = function() {
728         var users = new Set();
729         angular.forEach($scope.circ_list.map(function(circ) { return circ.usr(); }),function(usr) {
730             // aged circs have no 'usr'.
731             if (usr) users.add(usr);
732         });
733         users.forEach(function(usr) {
734             $timeout(function() {
735                 var url = $location.absUrl().replace(
736                     /\/cat\/.*/,
737                     '/circ/patron/' + usr.id() + '/checkout');
738                 $window.open(url, '_blank')
739             });
740         });
741     }
742
743     // load data for /circ_list tab
744     function loadCircHistory() {
745         $scope.circ_list = [];
746
747         var copy_org = 
748             itemSvc.copy.call_number().id() == -1 ?
749             itemSvc.copy.circ_lib().id() :
750             itemSvc.copy.call_number().owning_lib().id();
751
752         // there is an extra layer of permissibility over circ
753         // history views
754         egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
755         .then(function(orgIds) {
756
757             if (orgIds.indexOf(copy_org) == -1) {
758                 console.log('User is not allowed to view circ history');
759                 return $q.when(0);
760             }
761
762             return fetchMaxCircHistory();
763
764         }).then(function(maxHistCount) {
765
766             if(!maxHistCount) $scope.isMaxCircHistoryZero = true;
767
768             egCore.pcrud.search('aacs',
769                 {target_copy : copyId},
770                 {   flesh : 2,
771                     flesh_fields : {
772                         aacs : [
773                             'usr',
774                             'workstation',
775                             'checkin_workstation',
776                             'recurring_fine_rule'
777                         ],
778                         au : ['card']
779                     },
780                     order_by : {aacs : 'xact_start desc'},
781                     // fetch at least one to see if copy ever circulated
782                     limit : $scope.isMaxCircHistoryZero ? 1 : maxHistCount
783                 }
784
785             ).then(null, null, function(circ) {
786
787                 $scope.circ = circ;
788
789                 // flesh circ_lib locally
790                 circ.circ_lib(egCore.org.get(circ.circ_lib()));
791                 circ.checkin_lib(egCore.org.get(circ.checkin_lib()));
792                 $scope.circ_list.push(circ);
793             });
794         });
795     }
796
797
798     function loadCircCounts() {
799
800         delete $scope.circ_counts;
801         $scope.total_circs = 0;
802         $scope.total_circs_this_year = 0;
803         $scope.total_circs_prev_year = 0;
804         if (!copyId) return;
805
806         egCore.pcrud.search('circbyyr', 
807             {copy : copyId}, null, {atomic : true})
808
809         .then(function(counts) {
810             $scope.circ_counts = counts;
811
812             angular.forEach(counts, function(count) {
813                 $scope.total_circs += Number(count.count());
814             });
815
816             var this_year = counts.filter(function(c) {
817                 return c.year() == new Date().getFullYear();
818             });
819
820             $scope.total_circs_this_year = 
821                 this_year.length ? this_year[0].count() : 0;
822
823             var prev_year = counts.filter(function(c) {
824                 return c.year() == new Date().getFullYear() - 1;
825             });
826
827             $scope.total_circs_prev_year = 
828                 prev_year.length ? prev_year[0].count() : 0;
829
830         });
831     }
832
833     function loadHolds() {
834         delete $scope.hold;
835         if (!copyId) return;
836
837         egCore.pcrud.search('ahr', 
838             {   current_copy : copyId, 
839                 cancel_time : null, 
840                 fulfillment_time : null,
841                 capture_time : {'<>' : null}
842             }, {
843                 flesh : 2,
844                 flesh_fields : {
845                     ahr : ['requestor', 'usr'],
846                     au  : ['card']
847                 }
848             }
849         ).then(null, null, function(hold) {
850             $scope.hold = hold;
851             hold.pickup_lib(egCore.org.get(hold.pickup_lib()));
852             if (hold.current_shelf_lib()) {
853                 hold.current_shelf_lib(
854                     egCore.org.get(hold.current_shelf_lib()));
855             }
856             hold.behind_desk(Boolean(hold.behind_desk() == 't'));
857         });
858     }
859
860     function loadMostRecentTransit() {
861         delete $scope.transit;
862         delete $scope.hold_transit;
863         if (!copyId) return;
864
865         egCore.pcrud.search('atc', 
866             {target_copy : copyId},
867             {
868                 order_by : {atc : 'source_send_time DESC'},
869                 limit : 1
870             }
871
872         ).then(null, null, function(transit) {
873             // use progress callback since we'll get up to one result
874             $scope.transit = transit;
875             transit.source(egCore.org.get(transit.source()));
876             transit.dest(egCore.org.get(transit.dest()));
877         })
878     }
879
880
881     // we don't need all data on all tabs, so fetch what's needed when needed.
882     function loadTabData() {
883         switch($scope.tab) {
884             case 'summary':
885                 loadCurrentCirc();
886                 loadCircCounts();
887                 break;
888
889             case 'circs':
890                 loadCurrentCirc();
891                 break;
892
893             case 'circ_list':
894                 loadCircHistory();
895                 break;
896
897             case 'holds':
898                 loadHolds()
899                 loadMostRecentTransit();
900                 break;
901
902             case 'triggered_events':
903                 var url = $location.absUrl().replace(/\/staff.*/, '/actor/user/event_log');
904                 url += '?copy_id=' + encodeURIComponent(copyId);
905                 $scope.triggered_events_url = url;
906                 $scope.funcs = {};
907         }
908
909         if ($scope.edit) {
910             egCore.net.request(
911                 'open-ils.actor',
912                 'open-ils.actor.anon_cache.set_value',
913                 null, 'edit-these-copies', {
914                     record_id: $scope.recordId,
915                     copies: [copyId],
916                     hide_vols : true,
917                     hide_copies : false
918                 }
919             ).then(function(key) {
920                 if (key) {
921                     var url = egCore.env.basePath + 'cat/volcopy/' + key;
922                     $window.location.href = url;
923                 } else {
924                     alert('Could not create anonymous cache key!');
925                 }
926             });
927         }
928
929         return;
930     }
931
932     $scope.context.toggleDisplay = function() {
933         $location.path('/cat/item/search');
934     }
935
936     // handle the barcode scan box, which will replace our current copy
937     $scope.context.search = function(args) {
938         loadCopy(args.barcode).then(loadTabData);
939     }
940
941     $scope.context.show_triggered_events = function() {
942         $location.path('/cat/item/' + copyId + '/triggered_events');
943     }
944
945     loadCopy().then(loadTabData);
946 }])