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]);
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 itemSvc.requestItems(copy_list);
544 $scope.replaceBarcodes = function() {
545 itemSvc.replaceBarcodes(copyGrid.selectedItems());
548 $scope.attach_to_peer_bib = function() {
549 itemSvc.attach_to_peer_bib(copyGrid.selectedItems());
552 $scope.selectedHoldingsCopyDelete = function () {
553 itemSvc.selectedHoldingsCopyDelete(copyGrid.selectedItems());
556 $scope.selectedHoldingsItemStatusTgrEvt= function() {
557 var item = copyGrid.selectedItems()[0];
559 $location.path('/cat/item/' + item.id + '/triggered_events');
562 $scope.selectedHoldingsItemStatusHolds= function() {
563 var item = copyGrid.selectedItems()[0];
565 $location.path('/cat/item/' + item.id + '/holds');
568 $scope.cancel_transit = function () {
569 itemSvc.cancel_transit(copyGrid.selectedItems());
572 $scope.selectedHoldingsDamaged = function () {
573 itemSvc.selectedHoldingsDamaged(copyGrid.selectedItems());
576 $scope.selectedHoldingsDiscard = function () {
577 itemSvc.selectedHoldingsDiscard(copyGrid.selectedItems());
580 $scope.selectedHoldingsMissing = function () {
581 itemSvc.selectedHoldingsMissing(copyGrid.selectedItems());
584 $scope.checkin = function () {
585 itemSvc.checkin(copyGrid.selectedItems());
588 $scope.renew = function () {
589 itemSvc.renew(copyGrid.selectedItems());
592 $scope.selectedHoldingsVolCopyAdd = function () {
593 itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),true,false);
595 $scope.selectedHoldingsCopyAdd = function () {
596 itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),false,true);
599 $scope.selectedHoldingsCopyAlertsAdd = function(items) {
601 angular.forEach(items, function(item) {
602 if (item.id) copy_ids.push(item.id);
604 egCirc.add_copy_alerts(copy_ids).then(function() {
605 // update grid items?
609 $scope.selectedHoldingsCopyAlertsEdit = function(items) {
611 angular.forEach(items, function(item) {
612 if (item.id) copy_ids.push(item.id);
614 egCirc.manage_copy_alerts(copy_ids).then(function() {
615 // update grid items?
619 $scope.gridCellHandlers = {};
620 $scope.gridCellHandlers.copyAlertsEdit = function(id) {
621 egCirc.manage_copy_alerts([id]).then(function() {
622 // update grid items?
626 $scope.showBibHolds = function () {
627 angular.forEach(gatherSelectedRecordIds(), function (r) {
628 var url = egCore.env.basePath + 'cat/catalog/record/' + r + '/holds';
629 $timeout(function() { $window.open(url, '_blank') });
633 $scope.selectedHoldingsVolCopyEdit = function () {
634 itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,false);
636 $scope.selectedHoldingsVolEdit = function () {
637 itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,true);
639 $scope.selectedHoldingsCopyEdit = function () {
640 itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),true,false);
643 $scope.changeItemOwningLib = function() {
644 itemSvc.changeItemOwningLib(copyGrid.selectedItems());
647 $scope.transferItems = function (){
648 itemSvc.transferItems(copyGrid.selectedItems());
651 $scope.print_labels = function() {
654 'open-ils.actor.anon_cache.set_value',
655 null, 'print-labels-these-copies', {
656 copies : gatherSelectedHoldingsIds()
658 ).then(function(key) {
660 var url = egCore.env.basePath + 'cat/printlabels/' + key;
661 $timeout(function() { $window.open(url, '_blank') });
663 alert('Could not create anonymous cache key!');
668 $scope.print_list = function() {
669 var print_data = { copies : copyGrid.allItems() };
671 if (print_data.copies.length == 0) return $q.when();
673 return egCore.print.print({
674 template : 'item_status',
679 $scope.show_in_catalog = function(){
680 itemSvc.show_in_catalog(copyGrid.selectedItems());
683 if (copyId.length > 0) {
685 angular.forEach(copyId, function (c) {
686 fetch_list.push(itemSvc.fetch(null,c));
689 return $q.all(fetch_list).then(function (res) { copyGrid.refresh(); });
692 $scope.statusIconColumn = {
694 template: function(item) {
696 if (modified_items.has(item['id'])) {
697 icon = '<span class="glyphicon glyphicon-floppy-saved"' +
698 'title="' + egCore.strings.ITEM_SUCCESSFULLY_MODIFIED + '" ' +
699 'aria-label="' + egCore.strings.ITEM_SUCCESSFULLY_MODIFIED + '">' +
706 if (typeof BroadcastChannel != 'undefined') {
707 var holdings_bChannel = new BroadcastChannel("eg.holdings.update");
708 holdings_bChannel.onmessage = function(e) {
709 angular.forEach(e.data.copies, function(i) {
710 modified_items.add(i);
712 ngToast.create(egCore.strings.ITEMS_SUCCESSFULLY_MODIFIED);
713 $scope.refreshGridData();
715 $scope.$on('$destroy', function() {
716 holdings_bChannel.close();
723 * Detail view -- shows one copy
725 .controller('ViewCtrl',
726 ['$scope','$q','$location','$routeParams','$timeout','$window','egCore','egItem','egBilling','egCirc',
727 function($scope , $q , $location , $routeParams , $timeout , $window , egCore , itemSvc , egBilling , egCirc) {
728 var copyId = $routeParams.id;
729 $scope.args.copyId = copyId;
730 $scope.tab = $routeParams.tab || 'summary';
731 $scope.context.page = 'detail';
732 $scope.summaryRecord = null;
735 if ($scope.tab == 'edit') {
736 $scope.tab = 'summary';
741 // use the cached record info
743 $scope.copy_alert_count = itemSvc.copy.copy_alerts().filter(function(aca) {
744 return !aca.ack_time();
746 $scope.recordId = itemSvc.copy.call_number().record().id();
747 $scope.args.recordId = $scope.recordId;
748 $scope.args.cnId = itemSvc.copy.call_number().id();
749 $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
750 $scope.args.cnLabel = itemSvc.copy.call_number().label();
751 $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
752 $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
753 $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
754 $scope.args.copyBarcode = itemSvc.copy.barcode();
757 function loadCopy(barcode) {
758 $scope.context.itemNotFound = false;
760 // Avoid re-fetching the same copy while jumping tabs.
761 // In addition to being quicker, this helps to avoid flickering
762 // of the top panel which is always visible in the detail view.
764 // 'barcode' represents the loading of a new item - refetch it
765 // regardless of whether it matches the current item.
766 if (!barcode && itemSvc.copy && itemSvc.copy.id() == copyId) {
767 $scope.copy = itemSvc.copy;
768 if (itemSvc.latest_inventory && itemSvc.latest_inventory.copy() == copyId) {
769 $scope.latest_inventory = itemSvc.latest_inventory;
771 $scope.copy_alert_count = itemSvc.copy.copy_alerts().filter(function(aca) {
772 return !aca.ack_time();
774 $scope.recordId = itemSvc.copy.call_number().record().id();
775 $scope.args.recordId = $scope.recordId;
776 $scope.args.cnId = itemSvc.copy.call_number().id();
777 $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
778 $scope.args.cnLabel = itemSvc.copy.call_number().label();
779 $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
780 $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
781 $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
782 $scope.args.copyBarcode = itemSvc.copy.barcode();
789 var deferred = $q.defer();
790 itemSvc.fetch(barcode, copyId, true).then(function(res) {
791 $scope.context.selectBarcode = true;
795 $scope.context.itemNotFound = true;
796 egCore.audio.play('warning.item_status.itemNotFound');
797 deferred.reject(); // avoid propagation of data fetch calls
803 if (res.latest_inventory) itemSvc.latest_inventory = res.latest_inventory;
807 $scope.latest_inventory = res.latest_inventory;
808 $scope.copy_alert_count = copy.copy_alerts().filter(function(aca) {
809 return !aca.ack_time();
811 console.debug($scope.copy_alert_count);
812 $scope.recordId = copy.call_number().record().id();
813 $scope.args.recordId = $scope.recordId;
814 $scope.args.cnId = itemSvc.copy.call_number().id();
815 $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
816 $scope.args.cnLabel = itemSvc.copy.call_number().label();
817 $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
818 $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
819 $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
820 $scope.args.copyBarcode = copy.barcode();
821 $scope.args.barcode = '';
823 // locally flesh org units
824 copy.circ_lib(egCore.org.get(copy.circ_lib()));
825 copy.call_number().owning_lib(
826 egCore.org.get(copy.call_number().owning_lib()));
828 var r = copy.call_number().record();
829 if (r.owner()) r.owner(egCore.org.get(r.owner()));
831 // make boolean for auto-magic true/false display
833 ['ref','opac_visible','holdable','circulate'],
834 function(field) { copy[field](Boolean(copy[field]() == 't')) }
837 // finally, if this is a different copy, redirect.
838 // Note that we flesh first since the copy we just
839 // fetched will be used after the redirect.
840 if (copyId && copyId != copy.id()) {
841 // if a new barcode is scanned in the detail view,
842 // update the url to match the ID of the new copy
843 $location.path('/cat/item/' + copy.id() + '/' + $scope.tab);
844 deferred.reject(); // avoid propagation of data fetch calls
852 return deferred.promise;
855 // load the two most recent circulations in /circs tab
856 function loadCurrentCirc() {
858 delete $scope.circ_summary;
859 delete $scope.prev_circ_summary;
860 delete $scope.prev_circ_usr;
864 itemSvc.copy.call_number().id() == -1 ?
865 itemSvc.copy.circ_lib().id() :
866 itemSvc.copy.call_number().owning_lib().id();
868 // since a user can still view patron checkout history here, check perms
869 egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
870 .then(function(orgIds){
871 if(orgIds.indexOf(copy_org) == -1){
872 console.warn('User is not allowed to view circ history!');
876 return fetchMaxCircHistory();
878 .then(function(maxHistCount){
880 if (!maxHistCount) $scope.isMaxCircHistoryZero = true;
882 egCore.pcrud.search('aacs',
883 {target_copy : copyId},
889 'checkin_workstation',
892 'recurring_fine_rule'
896 order_by : {aacs : 'xact_start desc'},
900 ).then(null, null, function(circ) {
903 if (!circ) return $q.when();
905 // load the chain for this circ
908 'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
909 egCore.auth.token(), $scope.circ.id()
910 ).then(function(summary) {
911 $scope.circ_summary = summary;
914 if (maxHistCount <= 1) return;
916 // load the chain for the previous circ, plus the user
919 'open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary',
920 egCore.auth.token(), $scope.circ.id()
922 ).then(null, null, function(summary) {
923 $scope.prev_circ_summary = summary.summary;
925 if (summary.usr) { // aged circs have no 'usr'.
926 egCore.pcrud.retrieve('au', summary.usr,
927 {flesh : 1, flesh_fields : {au : ['card']}})
929 .then(function(user) { $scope.prev_circ_usr = user });
937 function fetchMaxCircHistory() {
938 if (maxHistory) return $q.when(maxHistory);
939 return egCore.org.settings(
940 'circ.item_checkout_history.max')
941 .then(function(set) {
942 maxHistory = set['circ.item_checkout_history.max'] || 4;
943 return Number(maxHistory);
947 $scope.addBilling = function(circ) {
948 egBilling.showBillDialog({
954 $scope.retrieveAllPatrons = function() {
955 var users = new Set();
956 angular.forEach($scope.circ_list.map(function(circ) { return circ.usr(); }),function(usr) {
957 // aged circs have no 'usr'.
958 if (usr) users.add(usr);
960 users.forEach(function(usr) {
961 $timeout(function() {
962 var url = $location.absUrl().replace(
964 '/circ/patron/' + usr.id() + '/checkout');
965 $window.open(url, '_blank')
970 // load data for /circ_list tab
971 function loadCircHistory() {
972 $scope.circ_list = [];
975 itemSvc.copy.call_number().id() == -1 ?
976 itemSvc.copy.circ_lib().id() :
977 itemSvc.copy.call_number().owning_lib().id();
979 // there is an extra layer of permissibility over circ
981 egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
982 .then(function(orgIds) {
984 if (orgIds.indexOf(copy_org) == -1) {
985 console.log('User is not allowed to view circ history');
989 return fetchMaxCircHistory();
991 }).then(function(maxHistCount) {
993 if(!maxHistCount) $scope.isMaxCircHistoryZero = true;
995 egCore.pcrud.search('aacs',
996 {target_copy : copyId},
1002 'checkin_workstation',
1003 'recurring_fine_rule'
1007 order_by : {aacs : 'xact_start desc'},
1008 // fetch at least one to see if copy ever circulated
1009 limit : $scope.isMaxCircHistoryZero ? 1 : maxHistCount
1012 ).then(null, null, function(circ) {
1016 // flesh circ_lib locally
1017 circ.circ_lib(egCore.org.get(circ.circ_lib()));
1018 circ.checkin_lib(egCore.org.get(circ.checkin_lib()));
1019 $scope.circ_list.push(circ);
1025 function loadCircCounts() {
1027 delete $scope.circ_counts;
1028 $scope.total_circs = 0;
1029 $scope.total_circs_this_year = 0;
1030 $scope.total_circs_prev_year = 0;
1031 if (!copyId) return;
1033 egCore.pcrud.search('circbyyr',
1034 {copy : copyId}, null, {atomic : true})
1036 .then(function(counts) {
1037 $scope.circ_counts = counts;
1039 angular.forEach(counts, function(count) {
1040 $scope.total_circs += Number(count.count());
1043 var this_year = counts.filter(function(c) {
1044 return c.year() == new Date().getFullYear();
1047 $scope.total_circs_this_year = (function() {
1049 if (this_year.length == 2) {
1050 total = (Number(this_year[0].count()) + Number(this_year[1].count()));
1051 } else if (this_year.length == 1) {
1052 total = Number(this_year[0].count());
1057 var prev_year = counts.filter(function(c) {
1058 return c.year() == new Date().getFullYear() - 1;
1061 $scope.total_circs_prev_year = (function() {
1063 if (prev_year.length == 2) {
1064 total = (Number(prev_year[0].count()) + Number(prev_year[1].count()));
1065 } else if (prev_year.length == 1) {
1066 total = Number(prev_year[0].count());
1074 function loadHolds() {
1076 if (!copyId) return;
1078 egCore.pcrud.search('ahr',
1079 { current_copy : copyId,
1081 fulfillment_time : null,
1082 capture_time : {'<>' : null}
1086 ahr : ['requestor', 'usr'],
1090 ).then(null, null, function(hold) {
1092 hold.pickup_lib(egCore.org.get(hold.pickup_lib()));
1093 if (hold.current_shelf_lib()) {
1094 hold.current_shelf_lib(
1095 egCore.org.get(hold.current_shelf_lib()));
1097 hold.behind_desk(Boolean(hold.behind_desk() == 't'));
1101 function loadMostRecentTransit() {
1102 delete $scope.transit;
1103 delete $scope.hold_transit;
1104 if (!copyId) return;
1106 egCore.pcrud.search('atc',
1107 {target_copy : copyId},
1109 order_by : {atc : 'source_send_time DESC'},
1113 ).then(null, null, function(transit) {
1114 // use progress callback since we'll get up to one result
1115 $scope.transit = transit;
1116 transit.source(egCore.org.get(transit.source()));
1117 transit.dest(egCore.org.get(transit.dest()));
1122 // we don't need all data on all tabs, so fetch what's needed when needed.
1123 function loadTabData() {
1124 switch($scope.tab) {
1140 loadMostRecentTransit();
1143 case 'triggered_events':
1144 var url = $location.absUrl().replace(/\/staff.*/, '/actor/user/event_log');
1145 url += '?copy_id=' + encodeURIComponent(copyId);
1146 $scope.triggered_events_url = url;
1153 'open-ils.actor.anon_cache.set_value',
1154 null, 'edit-these-copies', {
1155 record_id: $scope.recordId,
1160 ).then(function(key) {
1162 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1163 $window.location.href = url;
1165 alert('Could not create anonymous cache key!');
1173 $scope.addCopyAlerts = function(copy_id) {
1174 egCirc.add_copy_alerts([copy_id]).then(function() {
1176 loadCopy($scope.copy.barcode()).then(loadTabData);
1179 $scope.manageCopyAlerts = function(copy_id) {
1180 egCirc.manage_copy_alerts([copy_id]).then(function() {
1182 loadCopy($scope.copy.barcode()).then(loadTabData);
1186 $scope.context.toggleDisplay = function() {
1187 $location.path('/cat/item/search');
1190 // handle the barcode scan box, which will replace our current copy
1191 $scope.context.search = function(args) {
1192 loadCopy(args.barcode).then(loadTabData);
1195 $scope.context.show_triggered_events = function() {
1196 $location.path('/cat/item/' + copyId + '/triggered_events');
1199 loadCopy().then(loadTabData);