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?|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','$location','$timeout','egCore','egGridDataProvider','egItem',
56 function($scope , $location , $timeout , egCore , 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.requestItems = function() {
104 itemSvc.requestItems([$scope.args.copyId]);
107 $scope.attach_to_peer_bib = function() {
108 itemSvc.attach_to_peer_bib([{
109 id : $scope.args.copyId,
110 barcode : $scope.args.copyBarcode
114 $scope.selectedHoldingsCopyDelete = function () {
115 itemSvc.selectedHoldingsCopyDelete([{
116 id : $scope.args.copyId,
117 barcode : $scope.args.copyBarcode
121 $scope.checkin = function () {
123 id : $scope.args.copyId,
124 barcode : $scope.args.copyBarcode
128 $scope.renew = function () {
130 id : $scope.args.copyId,
131 barcode : $scope.args.copyBarcode
135 $scope.cancel_transit = function () {
136 itemSvc.cancel_transit([{
137 id : $scope.args.copyId,
138 barcode : $scope.args.copyBarcode
142 $scope.selectedHoldingsDamaged = function () {
143 itemSvc.selectedHoldingsDamaged([{
144 id : $scope.args.copyId,
145 barcode : $scope.args.copyBarcode
149 $scope.selectedHoldingsMissing = function () {
150 itemSvc.selectedHoldingsMissing([{
151 id : $scope.args.copyId,
152 barcode : $scope.args.copyBarcode
156 $scope.selectedHoldingsVolCopyAdd = function () {
157 itemSvc.spawnHoldingsAdd([{
158 id : $scope.args.copyId,
159 'call_number.owning_lib' : $scope.args.cnOwningLib,
160 'call_number.record.id' : $scope.args.recordId,
161 barcode : $scope.args.copyBarcode
164 $scope.selectedHoldingsCopyAdd = function () {
165 itemSvc.spawnHoldingsAdd([{
166 id : $scope.args.copyId,
167 'call_number.id' : $scope.args.cnId,
168 'call_number.owning_lib' : $scope.args.cnOwningLib,
169 'call_number.record.id' : $scope.args.recordId,
170 barcode : $scope.args.copyBarcode
174 $scope.selectedHoldingsVolCopyEdit = function () {
175 itemSvc.spawnHoldingsEdit([{
176 id : $scope.args.copyId,
177 'call_number.id' : $scope.args.cnId,
178 'call_number.owning_lib' : $scope.args.cnOwningLib,
179 'call_number.record.id' : $scope.args.recordId,
180 barcode : $scope.args.copyBarcode
183 $scope.selectedHoldingsVolEdit = function () {
184 itemSvc.spawnHoldingsEdit([{
185 id : $scope.args.copyId,
186 'call_number.id' : $scope.args.cnId,
187 'call_number.owning_lib' : $scope.args.cnOwningLib,
188 'call_number.record.id' : $scope.args.recordId,
189 barcode : $scope.args.copyBarcode
192 $scope.selectedHoldingsCopyEdit = function () {
193 itemSvc.spawnHoldingsEdit([{
194 id : $scope.args.copyId,
195 'call_number.id' : $scope.args.cnId,
196 'call_number.owning_lib' : $scope.args.cnOwningLib,
197 'call_number.record.id' : $scope.args.recordId,
198 barcode : $scope.args.copyBarcode
202 $scope.replaceBarcodes = function() {
203 itemSvc.replaceBarcodes([{
204 id : $scope.args.copyId,
205 barcode : $scope.args.copyBarcode
209 $scope.changeItemOwningLib = function() {
210 itemSvc.changeItemOwningLib([{
211 id : $scope.args.copyId,
212 'call_number.id' : $scope.args.cnId,
213 'call_number.owning_lib' : $scope.args.cnOwningLib,
214 'call_number.record.id' : $scope.args.recordId,
215 'call_number.label' : $scope.args.cnLabel,
216 'call_number.label_class' : $scope.args.cnLabelClass,
217 'call_number.prefix.id' : $scope.args.cnPrefixId,
218 'call_number.suffix.id' : $scope.args.cnSuffixId,
219 barcode : $scope.args.copyBarcode
223 $scope.transferItems = function (){
224 itemSvc.transferItems([{
225 id : $scope.args.copyId,
226 barcode : $scope.args.copyBarcode
233 * List view - grid stuff
235 .controller('ListCtrl',
236 ['$scope','$q','$routeParams','$location','$timeout','$window','egCore','egGridDataProvider','egItem','egUser','$uibModal','egCirc','egConfirmDialog',
237 function($scope , $q , $routeParams , $location , $timeout , $window , egCore , egGridDataProvider , itemSvc , egUser , $uibModal , egCirc , egConfirmDialog) {
239 var cp_list = $routeParams.idList;
241 copyId = cp_list.split(',');
244 $scope.context.page = 'list';
247 var provider = egGridDataProvider.instance();
248 provider.get = function(offset, count) {
252 $scope.gridDataProvider = egGridDataProvider.instance({
253 get : function(offset, count) {
254 //return provider.arrayNotifier(itemSvc.copies, offset, count);
255 return this.arrayNotifier(itemSvc.copies, offset, count);
259 // If a copy was just displayed in the detail view, ensure it's
260 // focused in the list view.
261 var selected = false;
262 var copyGrid = $scope.gridControls = {
263 itemRetrieved : function(item) {
264 if (selected || !itemSvc.copy) return;
265 if (itemSvc.copy.id() == item.id) {
266 copyGrid.selectItems([item.index]);
272 $scope.$watch('barcodesFromFile', function(newVal, oldVal) {
273 if (newVal && newVal != oldVal) {
274 $scope.args.barcode = '';
277 angular.forEach(newVal.split(/\n/), function(line) {
279 // scrub any trailing spaces or commas from the barcode
280 line = line.replace(/(.*?)($|\s.*|,.*)/,'$1');
284 itemSvc.fetch(barcodes).then(
287 copyGrid.selectItems([itemSvc.copies[0].index]);
293 $scope.context.search = function(args) {
294 if (!args.barcode) return;
295 $scope.context.itemNotFound = false;
296 itemSvc.fetch(args.barcode).then(function(res) {
299 copyGrid.selectItems([res.index]);
300 $scope.args.barcode = '';
302 $scope.context.itemNotFound = true;
303 egCore.audio.play('warning.item_status.itemNotFound');
305 $scope.context.selectBarcode = true;
309 var add_barcode_to_list = function (b) {
310 //console.log('listCtrl: add_barcode_to_list',b);
311 $scope.context.search({barcode:b});
313 itemSvc.add_barcode_to_list = add_barcode_to_list;
315 $scope.context.toggleDisplay = function() {
316 var item = copyGrid.selectedItems()[0];
318 $location.path('/cat/item/' + item.id);
321 $scope.context.show_triggered_events = function() {
322 var item = copyGrid.selectedItems()[0];
324 $location.path('/cat/item/' + item.id + '/triggered_events');
327 function gatherSelectedRecordIds () {
330 copyGrid.selectedItems(),
332 if (rid_list.indexOf(item['call_number.record.id']) == -1)
333 rid_list.push(item['call_number.record.id'])
339 function gatherSelectedVolumeIds (rid) {
342 copyGrid.selectedItems(),
344 if (rid && item['call_number.record.id'] != rid) return;
345 if (cn_id_list.indexOf(item['call_number.id']) == -1)
346 cn_id_list.push(item['call_number.id'])
352 function gatherSelectedHoldingsIds (rid) {
355 copyGrid.selectedItems(),
357 if (rid && item['call_number.record.id'] != rid) return;
358 cp_id_list.push(item.id)
364 $scope.add_copies_to_bucket = function() {
365 var copy_list = gatherSelectedHoldingsIds();
366 itemSvc.add_copies_to_bucket(copy_list);
369 $scope.need_one_selected = function() {
370 var items = $scope.gridControls.selectedItems();
371 if (items.length == 1) return false;
375 $scope.make_copies_bookable = function() {
376 itemSvc.make_copies_bookable(copyGrid.selectedItems());
379 $scope.book_copies_now = function() {
380 itemSvc.book_copies_now(copyGrid.selectedItems());
383 $scope.requestItems = function() {
384 var copy_list = gatherSelectedHoldingsIds();
385 itemSvc.requestItems(copy_list);
388 $scope.replaceBarcodes = function() {
389 itemSvc.replaceBarcodes(copyGrid.selectedItems());
392 $scope.attach_to_peer_bib = function() {
393 itemSvc.attach_to_peer_bib(copyGrid.selectedItems());
396 $scope.selectedHoldingsCopyDelete = function () {
397 itemSvc.selectedHoldingsCopyDelete(copyGrid.selectedItems());
400 $scope.selectedHoldingsItemStatusTgrEvt= function() {
401 var item = copyGrid.selectedItems()[0];
403 $location.path('/cat/item/' + item.id + '/triggered_events');
406 $scope.selectedHoldingsItemStatusHolds= function() {
407 var item = copyGrid.selectedItems()[0];
409 $location.path('/cat/item/' + item.id + '/holds');
412 $scope.cancel_transit = function () {
413 itemSvc.cancel_transit(copyGrid.selectedItems());
416 $scope.selectedHoldingsDamaged = function () {
417 itemSvc.selectedHoldingsDamaged(copyGrid.selectedItems());
420 $scope.selectedHoldingsMissing = function () {
421 itemSvc.selectedHoldingsMissing(copyGrid.selectedItems());
424 $scope.checkin = function () {
425 itemSvc.checkin(copyGrid.selectedItems());
428 $scope.renew = function () {
429 itemSvc.renew(copyGrid.selectedItems());
432 $scope.selectedHoldingsVolCopyAdd = function () {
433 itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),true,false);
435 $scope.selectedHoldingsCopyAdd = function () {
436 itemSvc.spawnHoldingsAdd(copyGrid.selectedItems(),false,true);
439 $scope.showBibHolds = function () {
440 angular.forEach(gatherSelectedRecordIds(), function (r) {
441 var url = egCore.env.basePath + 'cat/catalog/record/' + r + '/holds';
442 $timeout(function() { $window.open(url, '_blank') });
446 $scope.selectedHoldingsVolCopyEdit = function () {
447 itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,false);
449 $scope.selectedHoldingsVolEdit = function () {
450 itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),false,true);
452 $scope.selectedHoldingsCopyEdit = function () {
453 itemSvc.spawnHoldingsEdit(copyGrid.selectedItems(),true,false);
456 $scope.changeItemOwningLib = function() {
457 itemSvc.changeItemOwningLib(copyGrid.selectedItems());
460 $scope.transferItems = function (){
461 itemSvc.transferItems(copyGrid.selectedItems());
464 $scope.print_labels = function() {
467 'open-ils.actor.anon_cache.set_value',
468 null, 'print-labels-these-copies', {
469 copies : gatherSelectedHoldingsIds()
471 ).then(function(key) {
473 var url = egCore.env.basePath + 'cat/printlabels/' + key;
474 $timeout(function() { $window.open(url, '_blank') });
476 alert('Could not create anonymous cache key!');
481 $scope.print_list = function() {
482 var print_data = { copies : copyGrid.allItems() };
484 if (print_data.copies.length == 0) return $q.when();
486 return egCore.print.print({
487 template : 'item_status',
492 if (copyId.length > 0) {
493 itemSvc.fetch(null,copyId).then(
503 * Detail view -- shows one copy
505 .controller('ViewCtrl',
506 ['$scope','$q','$location','$routeParams','$timeout','$window','egCore','egItem','egBilling',
507 function($scope , $q , $location , $routeParams , $timeout , $window , egCore , itemSvc , egBilling) {
508 var copyId = $routeParams.id;
509 $scope.args.copyId = copyId;
510 $scope.tab = $routeParams.tab || 'summary';
511 $scope.context.page = 'detail';
512 $scope.summaryRecord = null;
515 if ($scope.tab == 'edit') {
516 $scope.tab = 'summary';
521 // use the cached record info
523 $scope.recordId = itemSvc.copy.call_number().record().id();
524 $scope.args.recordId = $scope.recordId;
525 $scope.args.cnId = itemSvc.copy.call_number().id();
526 $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
527 $scope.args.cnLabel = itemSvc.copy.call_number().label();
528 $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
529 $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
530 $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
531 $scope.args.copyBarcode = itemSvc.copy.barcode();
534 function loadCopy(barcode) {
535 $scope.context.itemNotFound = false;
537 // Avoid re-fetching the same copy while jumping tabs.
538 // In addition to being quicker, this helps to avoid flickering
539 // of the top panel which is always visible in the detail view.
541 // 'barcode' represents the loading of a new item - refetch it
542 // regardless of whether it matches the current item.
543 if (!barcode && itemSvc.copy && itemSvc.copy.id() == copyId) {
544 $scope.copy = itemSvc.copy;
545 $scope.recordId = itemSvc.copy.call_number().record().id();
546 $scope.args.recordId = $scope.recordId;
547 $scope.args.cnId = itemSvc.copy.call_number().id();
548 $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
549 $scope.args.cnLabel = itemSvc.copy.call_number().label();
550 $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
551 $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
552 $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
553 $scope.args.copyBarcode = itemSvc.copy.barcode();
560 var deferred = $q.defer();
561 itemSvc.fetch(barcode, copyId, true).then(function(res) {
562 $scope.context.selectBarcode = true;
566 $scope.context.itemNotFound = true;
567 egCore.audio.play('warning.item_status.itemNotFound');
568 deferred.reject(); // avoid propagation of data fetch calls
577 $scope.recordId = copy.call_number().record().id();
578 $scope.args.recordId = $scope.recordId;
579 $scope.args.cnId = itemSvc.copy.call_number().id();
580 $scope.args.cnOwningLib = itemSvc.copy.call_number().owning_lib();
581 $scope.args.cnLabel = itemSvc.copy.call_number().label();
582 $scope.args.cnLabelClass = itemSvc.copy.call_number().label_class();
583 $scope.args.cnPrefixId = itemSvc.copy.call_number().prefix().id();
584 $scope.args.cnSuffixId = itemSvc.copy.call_number().suffix().id();
585 $scope.args.copyBarcode = copy.barcode();
586 $scope.args.barcode = '';
588 // locally flesh org units
589 copy.circ_lib(egCore.org.get(copy.circ_lib()));
590 copy.call_number().owning_lib(
591 egCore.org.get(copy.call_number().owning_lib()));
593 var r = copy.call_number().record();
594 if (r.owner()) r.owner(egCore.org.get(r.owner()));
596 // make boolean for auto-magic true/false display
598 ['ref','opac_visible','holdable','circulate'],
599 function(field) { copy[field](Boolean(copy[field]() == 't')) }
602 // finally, if this is a different copy, redirect.
603 // Note that we flesh first since the copy we just
604 // fetched will be used after the redirect.
605 if (copyId && copyId != copy.id()) {
606 // if a new barcode is scanned in the detail view,
607 // update the url to match the ID of the new copy
608 $location.path('/cat/item/' + copy.id() + '/' + $scope.tab);
609 deferred.reject(); // avoid propagation of data fetch calls
617 return deferred.promise;
620 // if loadPrev load the two most recent circulations
621 function loadCurrentCirc(loadPrev) {
623 delete $scope.circ_summary;
624 delete $scope.prev_circ_summary;
625 delete $scope.prev_circ_usr;
628 egCore.pcrud.search('aacs',
629 {target_copy : copyId},
635 'checkin_workstation',
638 'recurring_fine_rule'
642 order_by : {aacs : 'xact_start desc'},
646 ).then(null, null, function(circ) {
649 // load the chain for this circ
652 'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
653 egCore.auth.token(), $scope.circ.id()
654 ).then(function(summary) {
655 $scope.circ_summary = summary;
658 if (!loadPrev) return;
660 // load the chain for the previous circ, plus the user
663 'open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary',
664 egCore.auth.token(), $scope.circ.id()
666 ).then(null, null, function(summary) {
667 $scope.prev_circ_summary = summary.summary;
669 if (summary.usr) { // aged circs have no 'usr'.
670 egCore.pcrud.retrieve('au', summary.usr,
671 {flesh : 1, flesh_fields : {au : ['card']}})
673 .then(function(user) { $scope.prev_circ_usr = user });
680 function fetchMaxCircHistory() {
681 if (maxHistory) return $q.when(maxHistory);
682 return egCore.org.settings(
683 'circ.item_checkout_history.max')
684 .then(function(set) {
685 maxHistory = set['circ.item_checkout_history.max'] || 4;
690 $scope.addBilling = function(circ) {
691 egBilling.showBillDialog({
697 $scope.retrieveAllPatrons = function() {
698 var users = new Set();
699 angular.forEach($scope.circ_list.map(function(circ) { return circ.usr(); }),function(usr) {
700 // aged circs have no 'usr'.
701 if (usr) users.add(usr);
703 users.forEach(function(usr) {
704 $timeout(function() {
705 var url = $location.absUrl().replace(
707 '/circ/patron/' + usr.id() + '/checkout');
708 $window.open(url, '_blank')
713 function loadCircHistory() {
714 $scope.circ_list = [];
717 itemSvc.copy.call_number().id() == -1 ?
718 itemSvc.copy.circ_lib().id() :
719 itemSvc.copy.call_number().owning_lib().id()
721 // there is an extra layer of permissibility over circ
723 egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
724 .then(function(orgIds) {
726 if (orgIds.indexOf(copy_org) == -1) {
727 console.log('User is not allowed to view circ history');
731 return fetchMaxCircHistory();
733 }).then(function(count) {
735 egCore.pcrud.search('aacs',
736 {target_copy : copyId},
742 'checkin_workstation',
743 'recurring_fine_rule'
747 order_by : {aacs : 'xact_start desc'},
751 ).then(null, null, function(circ) {
753 // flesh circ_lib locally
754 circ.circ_lib(egCore.org.get(circ.circ_lib()));
755 circ.checkin_lib(egCore.org.get(circ.checkin_lib()));
756 $scope.circ_list.push(circ);
762 function loadCircCounts() {
764 delete $scope.circ_counts;
765 $scope.total_circs = 0;
766 $scope.total_circs_this_year = 0;
767 $scope.total_circs_prev_year = 0;
770 egCore.pcrud.search('circbyyr',
771 {copy : copyId}, null, {atomic : true})
773 .then(function(counts) {
774 $scope.circ_counts = counts;
776 angular.forEach(counts, function(count) {
777 $scope.total_circs += Number(count.count());
780 var this_year = counts.filter(function(c) {
781 return c.year() == new Date().getFullYear();
784 $scope.total_circs_this_year =
785 this_year.length ? this_year[0].count() : 0;
787 var prev_year = counts.filter(function(c) {
788 return c.year() == new Date().getFullYear() - 1;
791 $scope.total_circs_prev_year =
792 prev_year.length ? prev_year[0].count() : 0;
797 function loadHolds() {
801 egCore.pcrud.search('ahr',
802 { current_copy : copyId,
804 fulfillment_time : null,
805 capture_time : {'<>' : null}
809 ahr : ['requestor', 'usr'],
813 ).then(null, null, function(hold) {
815 hold.pickup_lib(egCore.org.get(hold.pickup_lib()));
816 if (hold.current_shelf_lib()) {
817 hold.current_shelf_lib(
818 egCore.org.get(hold.current_shelf_lib()));
820 hold.behind_desk(Boolean(hold.behind_desk() == 't'));
824 function loadTransits() {
825 delete $scope.transit;
826 delete $scope.hold_transit;
829 egCore.pcrud.search('atc',
830 {target_copy : copyId},
831 {order_by : {atc : 'source_send_time DESC'}}
833 ).then(null, null, function(transit) {
834 $scope.transit = transit;
835 transit.source(egCore.org.get(transit.source()));
836 transit.dest(egCore.org.get(transit.dest()));
841 // we don't need all data on all tabs, so fetch what's needed when needed.
842 function loadTabData() {
850 loadCurrentCirc(true);
862 case 'triggered_events':
863 var url = $location.absUrl().replace(/\/staff.*/, '/actor/user/event_log');
864 url += '?copy_id=' + encodeURIComponent(copyId);
865 $scope.triggered_events_url = url;
872 'open-ils.actor.anon_cache.set_value',
873 null, 'edit-these-copies', {
874 record_id: $scope.recordId,
879 ).then(function(key) {
881 var url = egCore.env.basePath + 'cat/volcopy/' + key;
882 $window.location.href = url;
884 alert('Could not create anonymous cache key!');
892 $scope.context.toggleDisplay = function() {
893 $location.path('/cat/item/search');
896 // handle the barcode scan box, which will replace our current copy
897 $scope.context.search = function(args) {
898 loadCopy(args.barcode).then(loadTabData);
901 $scope.context.show_triggered_events = function() {
902 $location.path('/cat/item/' + copyId + '/triggered_events');
905 loadCopy().then(loadTabData);