5 angular.module('egItemStatus',
6 ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod', 'egGridMod', 'egUserMod'])
8 .filter('boolText', function(){
14 .config(function($routeProvider, $locationProvider, $compileProvider) {
15 $locationProvider.html5Mode(true);
16 $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|mailto|blob):/); // grid export
18 var resolver = {delay : function(egStartup) {return egStartup.go()}};
20 // search page shows the list view by default
21 $routeProvider.when('/cat/item/search', {
22 templateUrl: './cat/item/t_list',
23 controller: 'ListCtrl',
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',
34 $routeProvider.when('/cat/item/:id', {
35 templateUrl: './cat/item/t_view',
36 controller: 'ViewCtrl',
40 $routeProvider.when('/cat/item/:id/:tab', {
41 templateUrl: './cat/item/t_view',
42 controller: 'ViewCtrl',
46 // default page / bucket view
47 $routeProvider.otherwise({redirectTo : '/cat/item/search'});
51 * Search bar along the top of the page.
52 * Parent scope for list and detail views
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
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
66 $scope.toggleView = function($event) {
67 $scope.context.toggleDisplay();
68 $event.preventDefault(); // avoid form submission
71 // The functions that follow in this controller are never called
72 // when the List View is active, only the Detail View.
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);
85 $scope.add_copies_to_bucket = function() {
86 itemSvc.add_copies_to_bucket([$scope.args.copyId]);
89 $scope.show_in_catalog = function() {
90 window.open('/eg/staff/cat/catalog/record/' + $scope.args.recordId + '/catalog', '_blank');
93 $scope.print_labels = function() {
94 itemSvc.print_spine_labels([$scope.args.copyId]);
98 $scope.make_copies_bookable = function() {
99 itemSvc.make_copies_bookable([{
100 id : $scope.args.copyId,
101 'call_number.record.id' : $scope.args.recordId
105 $scope.book_copies_now = function() {
106 itemSvc.book_copies_now([$scope.args.copyBarcode]);
109 $scope.findAcquisition = function() {
112 $scope.openAcquisitionLineItem([$scope.args.copyId]);
115 $scope.openAcquisitionLineItem = function (cp_list) {
116 var hasResults = false;
119 angular.forEach(cp_list, function (copyId) {
123 'open-ils.acq.lineitem.retrieve.by_copy_id',
126 ).then(function (acqData) {
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') });
139 $q.all(promises).then(function () {
140 !hasResults ? alert('There is no corresponding purchase order for this item.') : false;
144 $scope.manage_reservations = function() {
145 itemSvc.manage_reservations([$scope.args.copyBarcode]);
148 $scope.requestItems = function() {
149 itemSvc.requestItems([$scope.args.copyId],[$scope.args.recordId]);
152 $scope.update_inventory = function() {
153 itemSvc.updateInventory([$scope.args.copyId], null)
154 .then(function(res) {
155 $timeout(function() { location.href = location.href; }, 1000);
159 $scope.show_triggered_events = function() {
160 $location.path('/cat/item/' + $scope.args.copyId + '/triggered_events');
163 $scope.show_item_holds = function() {
164 $location.path('/cat/item/' + $scope.args.copyId + '/holds');
167 $scope.show_record_holds = function() {
168 window.open('/eg/staff/cat/catalog/record/' + $scope.args.recordId + '/holds', '_blank');
171 $scope.add_item_alerts = function() {
172 egCirc.add_copy_alerts([$scope.args.copyId]);
175 $scope.manage_item_alerts = function() {
176 egCirc.manage_copy_alerts([$scope.args.copyId]);
180 $scope.attach_to_peer_bib = function() {
181 itemSvc.attach_to_peer_bib([{
182 id : $scope.args.copyId,
183 barcode : $scope.args.copyBarcode
187 $scope.selectedHoldingsCopyDelete = function () {
188 itemSvc.selectedHoldingsCopyDelete([{
189 id : $scope.args.copyId,
190 barcode : $scope.args.copyBarcode
194 $scope.checkin = function () {
196 id : $scope.args.copyId,
197 barcode : $scope.args.copyBarcode
201 $scope.renew = function () {
203 id : $scope.args.copyId,
204 barcode : $scope.args.copyBarcode
208 $scope.cancel_transit = function () {
209 itemSvc.cancel_transit([{
210 id : $scope.args.copyId,
211 barcode : $scope.args.copyBarcode
215 $scope.selectedHoldingsDamaged = function () {
216 itemSvc.selectedHoldingsDamaged([{
217 id : $scope.args.copyId,
218 barcode : $scope.args.copyBarcode,
223 $scope.selectedHoldingsDiscard = function () {
224 itemSvc.selectedHoldingsDiscard([{
225 id : $scope.args.copyId,
226 barcode : $scope.args.barcode
230 $scope.selectedHoldingsMissing = function () {
231 itemSvc.selectedHoldingsMissing([{
232 id : $scope.args.copyId,
233 barcode : $scope.args.barcode
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
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
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
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
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
283 $scope.replaceBarcodes = function() {
284 itemSvc.replaceBarcodes([{
285 id : $scope.args.copyId,
286 barcode : $scope.args.copyBarcode
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
304 $scope.transferItems = function (){
305 itemSvc.transferItems([{
306 id : $scope.args.copyId,
307 barcode : $scope.args.copyBarcode
314 * List view - grid stuff
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) {
326 var cp_list = $routeParams.idList;
328 copyId = cp_list.split(',');
331 var modified_items = new Set();
333 $scope.context.page = 'list';
336 var provider = egGridDataProvider.instance();
337 provider.get = function(offset, count) {
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);
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]);
361 $scope.$watch('barcodesFromFile', function(newVal, oldVal) {
362 if (newVal && newVal != oldVal) {
363 $scope.args.barcode = '';
366 angular.forEach(newVal.split(/\r?\n/), function(line) {
367 //remove all whitespace and commas
368 line = line.replace(/[\s,]+/g,'');
370 //Or remove leading/trailing whitespace
371 //line = line.replace(/(^[\s,]+|[\s,]+$/g,'');
377 // Serialize copy retrieval since there may be many, many copies.
378 function fetch_next_copy() {
379 var barcode = barcodes.pop();
380 egProgressDialog.increment();
382 if (barcode == undefined) { // All done here.
383 egProgressDialog.close();
385 if(itemSvc.copies[0]){ // Were any copies actually retrieved
386 copyGrid.selectItems([itemSvc.copies[0].index]);
391 itemSvc.fetch(barcode).then(fetch_next_copy);
394 if (barcodes.length) {
395 egProgressDialog.open({value: 0, max: barcodes.length});
401 $scope.context.search = function(args) {
402 if (!args.barcode) return;
403 $scope.context.itemNotFound = false;
404 itemSvc.fetch(args.barcode).then(function(res) {
407 copyGrid.selectItems([res.index]);
408 $scope.args.barcode = '';
410 $scope.context.itemNotFound = true;
411 egCore.audio.play('warning.item_status.itemNotFound');
413 $scope.context.selectBarcode = true;
417 var add_barcode_to_list = function (b) {
418 //console.log('listCtrl: add_barcode_to_list',b);
419 $scope.context.search({barcode:b});
421 itemSvc.add_barcode_to_list = add_barcode_to_list;
423 $scope.context.toggleDisplay = function() {
424 var item = copyGrid.selectedItems()[0];
426 $location.path('/cat/item/' + item.id);
429 $scope.context.show_triggered_events = function() {
430 var item = copyGrid.selectedItems()[0];
432 $location.path('/cat/item/' + item.id + '/triggered_events');
435 function gatherSelectedRecordIds () {
438 copyGrid.selectedItems(),
440 if (rid_list.indexOf(item['call_number.record.id']) == -1)
441 rid_list.push(item['call_number.record.id'])
447 function gatherSelectedVolumeIds (rid) {
450 copyGrid.selectedItems(),
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'])
460 function gatherSelectedHoldingsIds (rid) {
463 copyGrid.selectedItems(),
465 if (rid && item['call_number.record.id'] != rid) return;
466 cp_id_list.push(item.id)
472 $scope.refreshGridData = function() {
473 var chain = $q.when();
474 var all_items = itemSvc.copies.map(function(item) {
477 angular.forEach(all_items.reverse(), function(i) {
478 itemSvc.copies.shift();
479 chain = chain.then(function() {
480 return itemSvc.fetch(null, i);
483 return chain.then(function() {
489 $scope.add_copies_to_bucket = function() {
490 var copy_list = gatherSelectedHoldingsIds();
491 itemSvc.add_copies_to_bucket(copy_list);
494 $scope.locateAcquisition = function() {
495 if (gatherSelectedHoldingsIds) {
496 var cp_list = gatherSelectedHoldingsIds();
498 if (cp_list.length > 0) {
499 $scope.openAcquisitionLineItem(cp_list);
505 $scope.update_inventory = function() {
506 var copy_list = gatherSelectedHoldingsIds();
507 itemSvc.updateInventory(copy_list, $scope.gridControls.allItems()).then(function(res) {
509 $scope.gridControls.allItems(res);
510 ngToast.create(egCore.strings.SUCCESS_UPDATE_INVENTORY);
512 ngToast.warning(egCore.strings.FAIL_UPDATE_INVENTORY);
517 $scope.need_one_selected = function() {
518 var items = $scope.gridControls.selectedItems();
519 if (items.length == 1) return false;
523 $scope.make_copies_bookable = function() {
524 itemSvc.make_copies_bookable(copyGrid.selectedItems());
527 $scope.book_copies_now = function() {
528 var item = copyGrid.selectedItems()[0];
530 itemSvc.book_copies_now(item.barcode);
533 $scope.manage_reservations = function() {
534 var item = copyGrid.selectedItems()[0];
536 itemSvc.manage_reservations(item.barcode);
539 $scope.requestItems = function() {
540 var copy_list = gatherSelectedHoldingsIds();
541 var record_list = gatherSelectedRecordIds();
542 itemSvc.requestItems(copy_list,record_list);
545 $scope.replaceBarcodes = function() {
546 itemSvc.replaceBarcodes(copyGrid.selectedItems());
549 $scope.attach_to_peer_bib = function() {
550 itemSvc.attach_to_peer_bib(copyGrid.selectedItems());
553 $scope.selectedHoldingsCopyDelete = function () {
554 itemSvc.selectedHoldingsCopyDelete(copyGrid.selectedItems());
557 $scope.selectedHoldingsItemStatusTgrEvt= function() {
558 var item = copyGrid.selectedItems()[0];
560 $location.path('/cat/item/' + item.id + '/triggered_events');
563 $scope.selectedHoldingsItemStatusHolds= function() {
564 var item = copyGrid.selectedItems()[0];
566 $location.path('/cat/item/' + item.id + '/holds');
569 $scope.cancel_transit = function () {
570 itemSvc.cancel_transit(copyGrid.selectedItems());
573 $scope.selectedHoldingsDamaged = function () {
574 itemSvc.selectedHoldingsDamaged(copyGrid.selectedItems());
577 $scope.selectedHoldingsDiscard = function () {
578 itemSvc.selectedHoldingsDiscard(copyGrid.selectedItems());
581 $scope.selectedHoldingsMissing = function () {
582 itemSvc.selectedHoldingsMissing(copyGrid.selectedItems());
585 $scope.checkin = function () {
586 itemSvc.checkin(copyGrid.selectedItems());
589 $scope.renew = function () {
590 itemSvc.renew(copyGrid.selectedItems());
593 $scope.selectedHoldingsVolCopyAdd = function () {
594 itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),true,false);
596 $scope.selectedHoldingsCopyAdd = function () {
597 itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),false,true);
600 $scope.selectedHoldingsCopyAlertsAdd = function(items) {
602 angular.forEach(items, function(item) {
603 if (item.id) copy_ids.push(item.id);
605 egCirc.add_copy_alerts(copy_ids).then(function() {
606 // update grid items?
610 $scope.selectedHoldingsCopyAlertsEdit = function(items) {
612 angular.forEach(items, function(item) {
613 if (item.id) copy_ids.push(item.id);
615 egCirc.manage_copy_alerts(copy_ids).then(function() {
616 // update grid items?
620 $scope.gridCellHandlers = {};
621 $scope.gridCellHandlers.copyAlertsEdit = function(id) {
622 egCirc.manage_copy_alerts([id]).then(function() {
623 // update grid items?
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') });
634 $scope.selectedHoldingsVolCopyEdit = function () {
635 itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,false);
637 $scope.selectedHoldingsVolEdit = function () {
638 itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,true);
640 $scope.selectedHoldingsCopyEdit = function () {
641 itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),true,false);
644 $scope.changeItemOwningLib = function() {
645 itemSvc.changeItemOwningLib(copyGrid.selectedItems());
648 $scope.transferItems = function (){
649 itemSvc.transferItems(copyGrid.selectedItems());
652 $scope.print_labels = function() {
655 'open-ils.actor.anon_cache.set_value',
656 null, 'print-labels-these-copies', {
657 copies : gatherSelectedHoldingsIds()
659 ).then(function(key) {
661 var url = egCore.env.basePath + 'cat/printlabels/' + key;
662 $timeout(function() { $window.open(url, '_blank') });
664 alert('Could not create anonymous cache key!');
669 $scope.print_list = function() {
670 var print_data = { copies : copyGrid.allItems() };
672 if (print_data.copies.length == 0) return $q.when();
674 return egCore.print.print({
675 template : 'item_status',
680 $scope.show_in_catalog = function(){
681 itemSvc.show_in_catalog(copyGrid.selectedItems());
684 if (copyId.length > 0) {
686 angular.forEach(copyId, function (c) {
687 fetch_list.push(itemSvc.fetch(null,c));
690 return $q.all(fetch_list).then(function (res) { copyGrid.refresh(); });
693 $scope.statusIconColumn = {
695 template: function(item) {
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 + '">' +
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);
713 ngToast.create(egCore.strings.ITEMS_SUCCESSFULLY_MODIFIED);
714 $scope.refreshGridData();
716 $scope.$on('$destroy', function() {
717 holdings_bChannel.close();
724 * Detail view -- shows one copy
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;
736 if ($scope.tab == 'edit') {
737 $scope.tab = 'summary';
742 // use the cached record info
744 $scope.copy_alert_count = itemSvc.copy.copy_alerts().filter(function(aca) {
745 return !aca.ack_time();
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();
758 function loadCopy(barcode) {
759 $scope.context.itemNotFound = false;
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.
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;
772 $scope.copy_alert_count = itemSvc.copy.copy_alerts().filter(function(aca) {
773 return !aca.ack_time();
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();
790 var deferred = $q.defer();
791 itemSvc.fetch(barcode, copyId, true).then(function(res) {
792 $scope.context.selectBarcode = true;
796 $scope.context.itemNotFound = true;
797 egCore.audio.play('warning.item_status.itemNotFound');
798 deferred.reject(); // avoid propagation of data fetch calls
804 if (res.latest_inventory) itemSvc.latest_inventory = res.latest_inventory;
808 $scope.latest_inventory = res.latest_inventory;
809 $scope.copy_alert_count = copy.copy_alerts().filter(function(aca) {
810 return !aca.ack_time();
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 = '';
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()));
829 var r = copy.call_number().record();
830 if (r.owner()) r.owner(egCore.org.get(r.owner()));
832 // make boolean for auto-magic true/false display
834 ['ref','opac_visible','holdable','circulate'],
835 function(field) { copy[field](Boolean(copy[field]() == 't')) }
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
853 return deferred.promise;
856 // load the two most recent circulations in /circs tab
857 function loadCurrentCirc() {
859 delete $scope.circ_summary;
860 delete $scope.prev_circ_summary;
861 delete $scope.prev_circ_usr;
865 itemSvc.copy.call_number().id() == -1 ?
866 itemSvc.copy.circ_lib().id() :
867 itemSvc.copy.call_number().owning_lib().id();
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!');
877 return fetchMaxCircHistory();
879 .then(function(maxHistCount){
881 if (!maxHistCount) $scope.isMaxCircHistoryZero = true;
883 egCore.pcrud.search('aacs',
884 {target_copy : copyId},
890 'checkin_workstation',
893 'recurring_fine_rule'
897 order_by : {aacs : 'xact_start desc'},
901 ).then(null, null, function(circ) {
904 if (!circ) return $q.when();
906 // load the chain for this 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;
915 if (maxHistCount <= 1) return;
917 // load the chain for the previous circ, plus the user
920 'open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary',
921 egCore.auth.token(), $scope.circ.id()
923 ).then(null, null, function(summary) {
924 $scope.prev_circ_summary = summary.summary;
926 if (summary.usr) { // aged circs have no 'usr'.
927 egCore.pcrud.retrieve('au', summary.usr,
928 {flesh : 1, flesh_fields : {au : ['card']}})
930 .then(function(user) { $scope.prev_circ_usr = user });
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);
948 $scope.addBilling = function(circ) {
949 egBilling.showBillDialog({
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);
961 users.forEach(function(usr) {
962 $timeout(function() {
963 var url = $location.absUrl().replace(
965 '/circ/patron/' + usr.id() + '/checkout');
966 $window.open(url, '_blank')
971 // load data for /circ_list tab
972 function loadCircHistory() {
973 $scope.circ_list = [];
976 itemSvc.copy.call_number().id() == -1 ?
977 itemSvc.copy.circ_lib().id() :
978 itemSvc.copy.call_number().owning_lib().id();
980 // there is an extra layer of permissibility over circ
982 egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
983 .then(function(orgIds) {
985 if (orgIds.indexOf(copy_org) == -1) {
986 console.log('User is not allowed to view circ history');
990 return fetchMaxCircHistory();
992 }).then(function(maxHistCount) {
994 if(!maxHistCount) $scope.isMaxCircHistoryZero = true;
996 egCore.pcrud.search('aacs',
997 {target_copy : copyId},
1003 'checkin_workstation',
1004 'recurring_fine_rule'
1008 order_by : {aacs : 'xact_start desc'},
1009 // fetch at least one to see if copy ever circulated
1010 limit : $scope.isMaxCircHistoryZero ? 1 : maxHistCount
1013 ).then(null, null, function(circ) {
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);
1026 function loadCircCounts() {
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;
1034 egCore.pcrud.search('circbyyr',
1035 {copy : copyId}, null, {atomic : true})
1037 .then(function(counts) {
1038 $scope.circ_counts = counts;
1040 angular.forEach(counts, function(count) {
1041 $scope.total_circs += Number(count.count());
1044 var this_year = counts.filter(function(c) {
1045 return c.year() == new Date().getFullYear();
1048 $scope.total_circs_this_year = (function() {
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());
1058 var prev_year = counts.filter(function(c) {
1059 return c.year() == new Date().getFullYear() - 1;
1062 $scope.total_circs_prev_year = (function() {
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());
1075 function loadHolds() {
1077 if (!copyId) return;
1079 egCore.pcrud.search('ahr',
1080 { current_copy : copyId,
1082 fulfillment_time : null,
1083 capture_time : {'<>' : null}
1087 ahr : ['requestor', 'usr'],
1091 ).then(null, null, function(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()));
1098 hold.behind_desk(Boolean(hold.behind_desk() == 't'));
1102 function loadMostRecentTransit() {
1103 delete $scope.transit;
1104 delete $scope.hold_transit;
1105 if (!copyId) return;
1107 egCore.pcrud.search('atc',
1108 {target_copy : copyId},
1110 order_by : {atc : 'source_send_time DESC'},
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()));
1123 // we don't need all data on all tabs, so fetch what's needed when needed.
1124 function loadTabData() {
1125 switch($scope.tab) {
1141 loadMostRecentTransit();
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;
1154 'open-ils.actor.anon_cache.set_value',
1155 null, 'edit-these-copies', {
1156 record_id: $scope.recordId,
1161 ).then(function(key) {
1163 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1164 $window.location.href = url;
1166 alert('Could not create anonymous cache key!');
1174 $scope.addCopyAlerts = function(copy_id) {
1175 egCirc.add_copy_alerts([copy_id]).then(function() {
1177 loadCopy($scope.copy.barcode()).then(loadTabData);
1180 $scope.manageCopyAlerts = function(copy_id) {
1181 egCirc.manage_copy_alerts([copy_id]).then(function() {
1183 loadCopy($scope.copy.barcode()).then(loadTabData);
1187 $scope.context.toggleDisplay = function() {
1188 $location.path('/cat/item/search');
1191 // handle the barcode scan box, which will replace our current copy
1192 $scope.context.search = function(args) {
1193 loadCopy(args.barcode).then(loadTabData);
1196 $scope.context.show_triggered_events = function() {
1197 $location.path('/cat/item/' + copyId + '/triggered_events');
1200 loadCopy().then(loadTabData);