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',
56 function($scope , $q , $window , $location , $timeout , egCore , egNet , egGridDataProvider , itemSvc) {
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.make_copies_bookable = function() {
90 itemSvc.make_copies_bookable([{
91 id : $scope.args.copyId,
92 'call_number.record.id' : $scope.args.recordId
96 $scope.book_copies_now = function() {
97 itemSvc.book_copies_now([{
98 id : $scope.args.copyId,
99 'call_number.record.id' : $scope.args.recordId
103 $scope.findAcquisition = function() {
106 $scope.openAcquisitionLineItem([$scope.args.copyId]);
109 $scope.openAcquisitionLineItem = function (cp_list) {
110 var hasResults = false;
113 angular.forEach(cp_list, function (copyId) {
117 'open-ils.acq.lineitem.retrieve.by_copy_id',
120 ).then(function (acqData) {
123 acqData = egCore.idl.toHash(acqData);
124 var url = '/eg/acq/po/view/' + acqData.purchase_order + '/' + acqData.id;
125 $timeout(function () { $window.open(url, '_blank') });
133 $q.all(promises).then(function () {
134 !hasResults ? alert('There is no corresponding purchase order for this item.') : false;
138 $scope.requestItems = function() {
139 itemSvc.requestItems([$scope.args.copyId]);
142 $scope.update_inventory = function() {
143 itemSvc.updateInventory([$scope.args.copyId], null)
144 .then(function(res) {
145 $timeout(function() { location.href = location.href; }, 1000);
149 $scope.attach_to_peer_bib = function() {
150 itemSvc.attach_to_peer_bib([{
151 id : $scope.args.copyId,
152 barcode : $scope.args.copyBarcode
156 $scope.selectedHoldingsCopyDelete = function () {
157 itemSvc.selectedHoldingsCopyDelete([{
158 id : $scope.args.copyId,
159 barcode : $scope.args.copyBarcode
163 $scope.checkin = function () {
165 id : $scope.args.copyId,
166 barcode : $scope.args.copyBarcode
170 $scope.renew = function () {
172 id : $scope.args.copyId,
173 barcode : $scope.args.copyBarcode
177 $scope.cancel_transit = function () {
178 itemSvc.cancel_transit([{
179 id : $scope.args.copyId,
180 barcode : $scope.args.copyBarcode
184 $scope.selectedHoldingsDamaged = function () {
185 itemSvc.selectedHoldingsDamaged([{
186 id : $scope.args.copyId,
187 barcode : $scope.args.copyBarcode,
192 $scope.selectedHoldingsDiscard = function () {
193 itemSvc.selectedHoldingsDiscard([{
194 id : $scope.args.copyId,
195 barcode : $scope.args.barcode
199 $scope.selectedHoldingsMissing = function () {
200 itemSvc.selectedHoldingsMissing([{
201 id : $scope.args.copyId,
202 barcode : $scope.args.barcode
206 $scope.selectedHoldingsVolCopyAdd = function () {
207 itemSvc.spawnHoldingsAdd([{
208 id : $scope.args.copyId,
209 'call_number.owning_lib' : $scope.args.cnOwningLib,
210 'call_number.record.id' : $scope.args.recordId,
211 barcode : $scope.args.copyBarcode
214 $scope.selectedHoldingsCopyAdd = function () {
215 itemSvc.spawnHoldingsAdd([{
216 id : $scope.args.copyId,
217 'call_number.id' : $scope.args.cnId,
218 'call_number.owning_lib' : $scope.args.cnOwningLib,
219 'call_number.record.id' : $scope.args.recordId,
220 barcode : $scope.args.copyBarcode
224 $scope.selectedHoldingsVolCopyEdit = function () {
225 itemSvc.spawnHoldingsEdit([{
226 id : $scope.args.copyId,
227 'call_number.id' : $scope.args.cnId,
228 'call_number.owning_lib' : $scope.args.cnOwningLib,
229 'call_number.record.id' : $scope.args.recordId,
230 barcode : $scope.args.copyBarcode
233 $scope.selectedHoldingsVolEdit = function () {
234 itemSvc.spawnHoldingsEdit([{
235 id : $scope.args.copyId,
236 'call_number.id' : $scope.args.cnId,
237 'call_number.owning_lib' : $scope.args.cnOwningLib,
238 'call_number.record.id' : $scope.args.recordId,
239 barcode : $scope.args.copyBarcode
242 $scope.selectedHoldingsCopyEdit = function () {
243 itemSvc.spawnHoldingsEdit([{
244 id : $scope.args.copyId,
245 'call_number.id' : $scope.args.cnId,
246 'call_number.owning_lib' : $scope.args.cnOwningLib,
247 'call_number.record.id' : $scope.args.recordId,
248 barcode : $scope.args.copyBarcode
252 $scope.replaceBarcodes = function() {
253 itemSvc.replaceBarcodes([{
254 id : $scope.args.copyId,
255 barcode : $scope.args.copyBarcode
259 $scope.changeItemOwningLib = function() {
260 itemSvc.changeItemOwningLib([{
261 id : $scope.args.copyId,
262 'call_number.id' : $scope.args.cnId,
263 'call_number.owning_lib' : $scope.args.cnOwningLib,
264 'call_number.record.id' : $scope.args.recordId,
265 'call_number.label' : $scope.args.cnLabel,
266 'call_number.label_class' : $scope.args.cnLabelClass,
267 'call_number.prefix.id' : $scope.args.cnPrefixId,
268 'call_number.suffix.id' : $scope.args.cnSuffixId,
269 barcode : $scope.args.copyBarcode
273 $scope.transferItems = function (){
274 itemSvc.transferItems([{
275 id : $scope.args.copyId,
276 barcode : $scope.args.copyBarcode
283 * List view - grid stuff
285 .controller('ListCtrl',
286 ['$scope','$q','$routeParams','$location','$timeout','$window','egCore',
287 'egGridDataProvider','egItem','egUser','$uibModal','egCirc','egConfirmDialog',
288 'egProgressDialog', 'ngToast',
289 // function($scope , $q , $routeParams , $location , $timeout , $window , egCore ,
290 // egGridDataProvider , itemSvc , egUser , $uibModal , egCirc , egConfirmDialog,
291 // egProgressDialog, ngToast) {
292 function($scope , $q , $routeParams , $location , $timeout , $window , egCore , egGridDataProvider , itemSvc , egUser , $uibModal , egCirc , egConfirmDialog,
293 egProgressDialog, ngToast) {
295 var cp_list = $routeParams.idList;
297 copyId = cp_list.split(',');
300 var modified_items = new Set();
302 $scope.context.page = 'list';
305 var provider = egGridDataProvider.instance();
306 provider.get = function(offset, count) {
310 $scope.gridDataProvider = egGridDataProvider.instance({
311 get : function(offset, count) {
312 //return provider.arrayNotifier(itemSvc.copies, offset, count);
313 return this.arrayNotifier(itemSvc.copies, offset, count);
317 // If a copy was just displayed in the detail view, ensure it's
318 // focused in the list view.
319 var selected = false;
320 var copyGrid = $scope.gridControls = {
321 itemRetrieved : function(item) {
322 if (selected || !itemSvc.copy) return;
323 if (itemSvc.copy.id() == item.id) {
324 copyGrid.selectItems([item.index]);
330 $scope.$watch('barcodesFromFile', function(newVal, oldVal) {
331 if (newVal && newVal != oldVal) {
332 $scope.args.barcode = '';
335 angular.forEach(newVal.split(/\r?\n/), function(line) {
336 //remove all whitespace and commas
337 line = line.replace(/[\s,]+/g,'');
339 //Or remove leading/trailing whitespace
340 //line = line.replace(/(^[\s,]+|[\s,]+$/g,'');
346 // Serialize copy retrieval since there may be many, many copies.
347 function fetch_next_copy() {
348 var barcode = barcodes.pop();
349 egProgressDialog.increment();
351 if (barcode == undefined) { // All done here.
352 egProgressDialog.close();
354 if(itemSvc.copies[0]){ // Were any copies actually retrieved
355 copyGrid.selectItems([itemSvc.copies[0].index]);
360 itemSvc.fetch(barcode).then(fetch_next_copy);
363 if (barcodes.length) {
364 egProgressDialog.open({value: 0, max: barcodes.length});
370 $scope.context.search = function(args) {
371 if (!args.barcode) return;
372 $scope.context.itemNotFound = false;
373 itemSvc.fetch(args.barcode).then(function(res) {
376 copyGrid.selectItems([res.index]);
377 $scope.args.barcode = '';
379 $scope.context.itemNotFound = true;
380 egCore.audio.play('warning.item_status.itemNotFound');
382 $scope.context.selectBarcode = true;
386 var add_barcode_to_list = function (b) {
387 //console.log('listCtrl: add_barcode_to_list',b);
388 $scope.context.search({barcode:b});
390 itemSvc.add_barcode_to_list = add_barcode_to_list;
392 $scope.context.toggleDisplay = function() {
393 var item = copyGrid.selectedItems()[0];
395 $location.path('/cat/item/' + item.id);
398 $scope.context.show_triggered_events = function() {
399 var item = copyGrid.selectedItems()[0];
401 $location.path('/cat/item/' + item.id + '/triggered_events');
404 function gatherSelectedRecordIds () {
407 copyGrid.selectedItems(),
409 if (rid_list.indexOf(item['call_number.record.id']) == -1)
410 rid_list.push(item['call_number.record.id'])
416 function gatherSelectedVolumeIds (rid) {
419 copyGrid.selectedItems(),
421 if (rid && item['call_number.record.id'] != rid) return;
422 if (cn_id_list.indexOf(item['call_number.id']) == -1)
423 cn_id_list.push(item['call_number.id'])
429 function gatherSelectedHoldingsIds (rid) {
432 copyGrid.selectedItems(),
434 if (rid && item['call_number.record.id'] != rid) return;
435 cp_id_list.push(item.id)
441 $scope.refreshGridData = function() {
442 var chain = $q.when();
443 var all_items = itemSvc.copies.map(function(item) {
446 angular.forEach(all_items.reverse(), function(i) {
447 itemSvc.copies.shift();
448 chain = chain.then(function() {
449 return itemSvc.fetch(null, i);
452 return chain.then(function() {
458 $scope.add_copies_to_bucket = function() {
459 var copy_list = gatherSelectedHoldingsIds();
460 itemSvc.add_copies_to_bucket(copy_list);
463 $scope.locateAcquisition = function() {
464 if (gatherSelectedHoldingsIds) {
465 var cp_list = gatherSelectedHoldingsIds();
467 if (cp_list.length > 0) {
468 $scope.openAcquisitionLineItem(cp_list);
474 $scope.update_inventory = function() {
475 var copy_list = gatherSelectedHoldingsIds();
476 itemSvc.updateInventory(copy_list, $scope.gridControls.allItems()).then(function(res) {
478 $scope.gridControls.allItems(res);
479 ngToast.create(egCore.strings.SUCCESS_UPDATE_INVENTORY);
481 ngToast.warning(egCore.strings.FAIL_UPDATE_INVENTORY);
486 $scope.need_one_selected = function() {
487 var items = $scope.gridControls.selectedItems();
488 if (items.length == 1) return false;
492 $scope.make_copies_bookable = function() {
493 itemSvc.make_copies_bookable(copyGrid.selectedItems());
496 $scope.book_copies_now = function() {
497 itemSvc.book_copies_now(copyGrid.selectedItems());
500 $scope.requestItems = function() {
501 var copy_list = gatherSelectedHoldingsIds();
502 itemSvc.requestItems(copy_list);
505 $scope.replaceBarcodes = function() {
506 itemSvc.replaceBarcodes(copyGrid.selectedItems());
509 $scope.attach_to_peer_bib = function() {
510 itemSvc.attach_to_peer_bib(copyGrid.selectedItems());
513 $scope.selectedHoldingsCopyDelete = function () {
514 itemSvc.selectedHoldingsCopyDelete(copyGrid.selectedItems());
517 $scope.selectedHoldingsItemStatusTgrEvt= function() {
518 var item = copyGrid.selectedItems()[0];
520 $location.path('/cat/item/' + item.id + '/triggered_events');
523 $scope.selectedHoldingsItemStatusHolds= function() {
524 var item = copyGrid.selectedItems()[0];
526 $location.path('/cat/item/' + item.id + '/holds');
529 $scope.cancel_transit = function () {
530 itemSvc.cancel_transit(copyGrid.selectedItems());
533 $scope.selectedHoldingsDamaged = function () {
534 itemSvc.selectedHoldingsDamaged(copyGrid.selectedItems());
537 $scope.selectedHoldingsDiscard = function () {
538 itemSvc.selectedHoldingsDiscard(copyGrid.selectedItems());
541 $scope.selectedHoldingsMissing = function () {
542 itemSvc.selectedHoldingsMissing(copyGrid.selectedItems());
545 $scope.checkin = function () {
546 itemSvc.checkin(copyGrid.selectedItems());
549 $scope.renew = function () {
550 itemSvc.renew(copyGrid.selectedItems());
553 $scope.selectedHoldingsVolCopyAdd = function () {
554 itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),true,false);
556 $scope.selectedHoldingsCopyAdd = function () {
557 itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),false,true);
560 $scope.selectedHoldingsCopyAlertsAdd = function(items) {
562 angular.forEach(items, function(item) {
563 if (item.id) copy_ids.push(item.id);
565 egCirc.add_copy_alerts(copy_ids).then(function() {
566 // update grid items?
570 $scope.selectedHoldingsCopyAlertsEdit = function(items) {
572 angular.forEach(items, function(item) {
573 if (item.id) copy_ids.push(item.id);
575 egCirc.manage_copy_alerts(copy_ids).then(function() {
576 // update grid items?
580 $scope.gridCellHandlers = {};
581 $scope.gridCellHandlers.copyAlertsEdit = function(id) {
582 egCirc.manage_copy_alerts([id]).then(function() {
583 // update grid items?
587 $scope.showBibHolds = function () {
588 angular.forEach(gatherSelectedRecordIds(), function (r) {
589 var url = egCore.env.basePath + 'cat/catalog/record/' + r + '/holds';
590 $timeout(function() { $window.open(url, '_blank') });
594 $scope.selectedHoldingsVolCopyEdit = function () {
595 itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,false);
597 $scope.selectedHoldingsVolEdit = function () {
598 itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,true);
600 $scope.selectedHoldingsCopyEdit = function () {
601 itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),true,false);
604 $scope.changeItemOwningLib = function() {
605 itemSvc.changeItemOwningLib(copyGrid.selectedItems());
608 $scope.transferItems = function (){
609 itemSvc.transferItems(copyGrid.selectedItems());
612 $scope.print_labels = function() {
615 'open-ils.actor.anon_cache.set_value',
616 null, 'print-labels-these-copies', {
617 copies : gatherSelectedHoldingsIds()
619 ).then(function(key) {
621 var url = egCore.env.basePath + 'cat/printlabels/' + key;
622 $timeout(function() { $window.open(url, '_blank') });
624 alert('Could not create anonymous cache key!');
629 $scope.print_list = function() {
630 var print_data = { copies : copyGrid.allItems() };
632 if (print_data.copies.length == 0) return $q.when();
634 return egCore.print.print({
635 template : 'item_status',
640 $scope.show_in_catalog = function(){
641 itemSvc.show_in_catalog(copyGrid.selectedItems());
644 if (copyId.length > 0) {
646 angular.forEach(copyId, function (c) {
647 fetch_list.push(itemSvc.fetch(null,c));
650 return $q.all(fetch_list).then(function (res) { copyGrid.refresh(); });
653 $scope.statusIconColumn = {
655 template: function(item) {
657 if (modified_items.has(item['id'])) {
658 icon = '<span class="glyphicon glyphicon-floppy-saved"' +
659 'title="' + egCore.strings.ITEM_SUCCESSFULLY_MODIFIED + '" ' +
660 'aria-label="' + egCore.strings.ITEM_SUCCESSFULLY_MODIFIED + '">' +
667 if (typeof BroadcastChannel != 'undefined') {
668 var holdings_bChannel = new BroadcastChannel("eg.holdings.update");
669 holdings_bChannel.onmessage = function(e) {
670 angular.forEach(e.data.copies, function(i) {
671 modified_items.add(i);
673 ngToast.create(egCore.strings.ITEMS_SUCCESSFULLY_MODIFIED);
674 $scope.refreshGridData();
676 $scope.$on('$destroy', function() {
677 holdings_bChannel.close();
684 * Detail view -- shows one copy
686 .controller('ViewCtrl',
687 ['$scope','$q','$location','$routeParams','$timeout','$window','egCore','egItem','egBilling','egCirc',
688 function($scope , $q , $location , $routeParams , $timeout , $window , egCore , itemSvc , egBilling , egCirc) {
689 var copyId = $routeParams.id;
690 $scope.args.copyId = copyId;
691 $scope.tab = $routeParams.tab || 'summary';
692 $scope.context.page = 'detail';
693 $scope.summaryRecord = null;
696 if ($scope.tab == 'edit') {
697 $scope.tab = 'summary';
702 // use the cached record info
704 $scope.copy_alert_count = itemSvc.copy.copy_alerts().filter(function(aca) {
705 return !aca.ack_time();
707 $scope.recordId = itemSvc.copy.call_number().record().id();
708 $scope.args.recordId = $scope.recordId;
709 $scope.args.cnId = itemSvc.copy.call_number().id();
710 $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
711 $scope.args.cnLabel = itemSvc.copy.call_number().label();
712 $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
713 $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
714 $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
715 $scope.args.copyBarcode = itemSvc.copy.barcode();
718 function loadCopy(barcode) {
719 $scope.context.itemNotFound = false;
721 // Avoid re-fetching the same copy while jumping tabs.
722 // In addition to being quicker, this helps to avoid flickering
723 // of the top panel which is always visible in the detail view.
725 // 'barcode' represents the loading of a new item - refetch it
726 // regardless of whether it matches the current item.
727 if (!barcode && itemSvc.copy && itemSvc.copy.id() == copyId) {
728 $scope.copy = itemSvc.copy;
729 if (itemSvc.latest_inventory && itemSvc.latest_inventory.copy() == copyId) {
730 $scope.latest_inventory = itemSvc.latest_inventory;
732 $scope.copy_alert_count = itemSvc.copy.copy_alerts().filter(function(aca) {
733 return !aca.ack_time();
735 $scope.recordId = itemSvc.copy.call_number().record().id();
736 $scope.args.recordId = $scope.recordId;
737 $scope.args.cnId = itemSvc.copy.call_number().id();
738 $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
739 $scope.args.cnLabel = itemSvc.copy.call_number().label();
740 $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
741 $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
742 $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
743 $scope.args.copyBarcode = itemSvc.copy.barcode();
750 var deferred = $q.defer();
751 itemSvc.fetch(barcode, copyId, true).then(function(res) {
752 $scope.context.selectBarcode = true;
756 $scope.context.itemNotFound = true;
757 egCore.audio.play('warning.item_status.itemNotFound');
758 deferred.reject(); // avoid propagation of data fetch calls
764 if (res.latest_inventory) itemSvc.latest_inventory = res.latest_inventory;
768 $scope.latest_inventory = res.latest_inventory;
769 $scope.copy_alert_count = copy.copy_alerts().filter(function(aca) {
770 return !aca.ack_time();
772 console.debug($scope.copy_alert_count);
773 $scope.recordId = copy.call_number().record().id();
774 $scope.args.recordId = $scope.recordId;
775 $scope.args.cnId = itemSvc.copy.call_number().id();
776 $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
777 $scope.args.cnLabel = itemSvc.copy.call_number().label();
778 $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
779 $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
780 $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
781 $scope.args.copyBarcode = copy.barcode();
782 $scope.args.barcode = '';
784 // locally flesh org units
785 copy.circ_lib(egCore.org.get(copy.circ_lib()));
786 copy.call_number().owning_lib(
787 egCore.org.get(copy.call_number().owning_lib()));
789 var r = copy.call_number().record();
790 if (r.owner()) r.owner(egCore.org.get(r.owner()));
792 // make boolean for auto-magic true/false display
794 ['ref','opac_visible','holdable','circulate'],
795 function(field) { copy[field](Boolean(copy[field]() == 't')) }
798 // finally, if this is a different copy, redirect.
799 // Note that we flesh first since the copy we just
800 // fetched will be used after the redirect.
801 if (copyId && copyId != copy.id()) {
802 // if a new barcode is scanned in the detail view,
803 // update the url to match the ID of the new copy
804 $location.path('/cat/item/' + copy.id() + '/' + $scope.tab);
805 deferred.reject(); // avoid propagation of data fetch calls
813 return deferred.promise;
816 // load the two most recent circulations in /circs tab
817 function loadCurrentCirc() {
819 delete $scope.circ_summary;
820 delete $scope.prev_circ_summary;
821 delete $scope.prev_circ_usr;
825 itemSvc.copy.call_number().id() == -1 ?
826 itemSvc.copy.circ_lib().id() :
827 itemSvc.copy.call_number().owning_lib().id();
829 // since a user can still view patron checkout history here, check perms
830 egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
831 .then(function(orgIds){
832 if(orgIds.indexOf(copy_org) == -1){
833 console.warn('User is not allowed to view circ history!');
837 return fetchMaxCircHistory();
839 .then(function(maxHistCount){
841 if (!maxHistCount) $scope.isMaxCircHistoryZero = true;
843 egCore.pcrud.search('aacs',
844 {target_copy : copyId},
850 'checkin_workstation',
853 'recurring_fine_rule'
857 order_by : {aacs : 'xact_start desc'},
861 ).then(null, null, function(circ) {
864 if (!circ) return $q.when();
866 // load the chain for this circ
869 'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
870 egCore.auth.token(), $scope.circ.id()
871 ).then(function(summary) {
872 $scope.circ_summary = summary;
875 if (maxHistCount <= 1) return;
877 // load the chain for the previous circ, plus the user
880 'open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary',
881 egCore.auth.token(), $scope.circ.id()
883 ).then(null, null, function(summary) {
884 $scope.prev_circ_summary = summary.summary;
886 if (summary.usr) { // aged circs have no 'usr'.
887 egCore.pcrud.retrieve('au', summary.usr,
888 {flesh : 1, flesh_fields : {au : ['card']}})
890 .then(function(user) { $scope.prev_circ_usr = user });
898 function fetchMaxCircHistory() {
899 if (maxHistory) return $q.when(maxHistory);
900 return egCore.org.settings(
901 'circ.item_checkout_history.max')
902 .then(function(set) {
903 maxHistory = set['circ.item_checkout_history.max'] || 4;
904 return Number(maxHistory);
908 $scope.addBilling = function(circ) {
909 egBilling.showBillDialog({
915 $scope.retrieveAllPatrons = function() {
916 var users = new Set();
917 angular.forEach($scope.circ_list.map(function(circ) { return circ.usr(); }),function(usr) {
918 // aged circs have no 'usr'.
919 if (usr) users.add(usr);
921 users.forEach(function(usr) {
922 $timeout(function() {
923 var url = $location.absUrl().replace(
925 '/circ/patron/' + usr.id() + '/checkout');
926 $window.open(url, '_blank')
931 // load data for /circ_list tab
932 function loadCircHistory() {
933 $scope.circ_list = [];
936 itemSvc.copy.call_number().id() == -1 ?
937 itemSvc.copy.circ_lib().id() :
938 itemSvc.copy.call_number().owning_lib().id();
940 // there is an extra layer of permissibility over circ
942 egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
943 .then(function(orgIds) {
945 if (orgIds.indexOf(copy_org) == -1) {
946 console.log('User is not allowed to view circ history');
950 return fetchMaxCircHistory();
952 }).then(function(maxHistCount) {
954 if(!maxHistCount) $scope.isMaxCircHistoryZero = true;
956 egCore.pcrud.search('aacs',
957 {target_copy : copyId},
963 'checkin_workstation',
964 'recurring_fine_rule'
968 order_by : {aacs : 'xact_start desc'},
969 // fetch at least one to see if copy ever circulated
970 limit : $scope.isMaxCircHistoryZero ? 1 : maxHistCount
973 ).then(null, null, function(circ) {
977 // flesh circ_lib locally
978 circ.circ_lib(egCore.org.get(circ.circ_lib()));
979 circ.checkin_lib(egCore.org.get(circ.checkin_lib()));
980 $scope.circ_list.push(circ);
986 function loadCircCounts() {
988 delete $scope.circ_counts;
989 $scope.total_circs = 0;
990 $scope.total_circs_this_year = 0;
991 $scope.total_circs_prev_year = 0;
994 egCore.pcrud.search('circbyyr',
995 {copy : copyId}, null, {atomic : true})
997 .then(function(counts) {
998 $scope.circ_counts = counts;
1000 angular.forEach(counts, function(count) {
1001 $scope.total_circs += Number(count.count());
1004 var this_year = counts.filter(function(c) {
1005 return c.year() == new Date().getFullYear();
1008 $scope.total_circs_this_year = (function() {
1010 if (this_year.length == 2) {
1011 total = (Number(this_year[0].count()) + Number(this_year[1].count()));
1012 } else if (this_year.length == 1) {
1013 total = Number(this_year[0].count());
1018 var prev_year = counts.filter(function(c) {
1019 return c.year() == new Date().getFullYear() - 1;
1022 $scope.total_circs_prev_year = (function() {
1024 if (prev_year.length == 2) {
1025 total = (Number(prev_year[0].count()) + Number(prev_year[1].count()));
1026 } else if (prev_year.length == 1) {
1027 total = Number(prev_year[0].count());
1035 function loadHolds() {
1037 if (!copyId) return;
1039 egCore.pcrud.search('ahr',
1040 { current_copy : copyId,
1042 fulfillment_time : null,
1043 capture_time : {'<>' : null}
1047 ahr : ['requestor', 'usr'],
1051 ).then(null, null, function(hold) {
1053 hold.pickup_lib(egCore.org.get(hold.pickup_lib()));
1054 if (hold.current_shelf_lib()) {
1055 hold.current_shelf_lib(
1056 egCore.org.get(hold.current_shelf_lib()));
1058 hold.behind_desk(Boolean(hold.behind_desk() == 't'));
1062 function loadMostRecentTransit() {
1063 delete $scope.transit;
1064 delete $scope.hold_transit;
1065 if (!copyId) return;
1067 egCore.pcrud.search('atc',
1068 {target_copy : copyId},
1070 order_by : {atc : 'source_send_time DESC'},
1074 ).then(null, null, function(transit) {
1075 // use progress callback since we'll get up to one result
1076 $scope.transit = transit;
1077 transit.source(egCore.org.get(transit.source()));
1078 transit.dest(egCore.org.get(transit.dest()));
1083 // we don't need all data on all tabs, so fetch what's needed when needed.
1084 function loadTabData() {
1085 switch($scope.tab) {
1101 loadMostRecentTransit();
1104 case 'triggered_events':
1105 var url = $location.absUrl().replace(/\/staff.*/, '/actor/user/event_log');
1106 url += '?copy_id=' + encodeURIComponent(copyId);
1107 $scope.triggered_events_url = url;
1114 'open-ils.actor.anon_cache.set_value',
1115 null, 'edit-these-copies', {
1116 record_id: $scope.recordId,
1121 ).then(function(key) {
1123 var url = egCore.env.basePath + 'cat/volcopy/' + key;
1124 $window.location.href = url;
1126 alert('Could not create anonymous cache key!');
1134 $scope.addCopyAlerts = function(copy_id) {
1135 egCirc.add_copy_alerts([copy_id]).then(function() {
1137 loadCopy($scope.copy.barcode()).then(loadTabData);
1140 $scope.manageCopyAlerts = function(copy_id) {
1141 egCirc.manage_copy_alerts([copy_id]).then(function() {
1143 loadCopy($scope.copy.barcode()).then(loadTabData);
1147 $scope.context.toggleDisplay = function() {
1148 $location.path('/cat/item/search');
1151 // handle the barcode scan box, which will replace our current copy
1152 $scope.context.search = function(args) {
1153 loadCopy(args.barcode).then(loadTabData);
1156 $scope.context.show_triggered_events = function() {
1157 $location.path('/cat/item/' + copyId + '/triggered_events');
1160 loadCopy().then(loadTabData);