5 angular.module('egItemStatus',
6 ['ngRoute', 'ui.bootstrap', 'egCoreMod', 'egUiMod', 'egGridMod'])
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'});
55 copies : [], // copy barcode search results
56 index : 0 // search grid index
62 acp : ['call_number','location','status','location'],
63 acn : ['record','prefix','suffix'],
64 bre : ['simple_record','creator','editor']
67 // avoid fleshing MARC on the bre
68 // note: don't add simple_record.. not sure why
69 bre : ['id','tcn_value','creator','editor'],
73 // resolved with the last received copy
74 service.fetch = function(barcode, id, noListDupes) {
78 promise = egCore.pcrud.search('acp',
79 {barcode : barcode, deleted : 'f'}, service.flesh);
81 promise = egCore.pcrud.retrieve('acp', id, service.flesh);
86 function() {return lastRes},
89 // notify reads the stream of copies, one at a time.
94 // use the existing copy if possible
95 flatCopy = service.copies.filter(
96 function(c) {return c.id == copy.id()})[0];
100 flatCopy = egCore.idl.toHash(copy, true);
101 flatCopy.index = service.index++;
102 service.copies.unshift(flatCopy);
107 index : flatCopy.index
117 * Search bar along the top of the page.
118 * Parent scope for list and detail views
120 .controller('SearchCtrl',
121 ['$scope','$location','egCore','egGridDataProvider','itemSvc',
122 function($scope , $location , egCore , egGridDataProvider , itemSvc) {
123 $scope.args = {}; // search args
125 // sub-scopes (search / detail-view) apply their version
126 // of retrieval function to $scope.context.search
127 // and display toggling via $scope.context.toggleDisplay
132 $scope.toggleView = function($event) {
133 $scope.context.toggleDisplay();
134 $event.preventDefault(); // avoid form submission
139 * List view - grid stuff
141 .controller('ListCtrl',
142 ['$scope','$q','$routeParams','$location','$timeout','egCore','egGridDataProvider','itemSvc',
143 function($scope , $q , $routeParams , $location , $timeout , egCore , egGridDataProvider , itemSvc) {
145 var cp_list = $routeParams.idList;
147 copyId = cp_list.split(',');
150 $scope.context.page = 'list';
153 var provider = egGridDataProvider.instance();
154 provider.get = function(offset, count) {
158 $scope.gridDataProvider = egGridDataProvider.instance({
159 get : function(offset, count) {
160 //return provider.arrayNotifier(itemSvc.copies, offset, count);
161 return this.arrayNotifier(itemSvc.copies, offset, count);
165 // If a copy was just displayed in the detail view, ensure it's
166 // focused in the list view.
167 var selected = false;
168 var copyGrid = $scope.gridControls = {
169 itemRetrieved : function(item) {
170 if (selected || !itemSvc.copy) return;
171 if (itemSvc.copy.id() == item.id) {
172 copyGrid.selectItems([item.index]);
178 $scope.$watch('barcodesFromFile', function(newVal, oldVal) {
179 if (newVal && newVal != oldVal) {
180 $scope.args.barcode = '';
183 angular.forEach(newVal.split(/\n/), function(line) {
185 // scrub any trailing spaces or commas from the barcode
186 line = line.replace(/(.*?)($|\s.*|,.*)/,'$1');
190 itemSvc.fetch(barcodes).then(
193 copyGrid.selectItems([itemSvc.copies[0].index]);
199 $scope.context.search = function(args) {
200 if (!args.barcode) return;
201 $scope.context.itemNotFound = false;
202 itemSvc.fetch(args.barcode).then(function(res) {
205 copyGrid.selectItems([res.index]);
206 $scope.args.barcode = '';
208 $scope.context.itemNotFound = true;
210 $scope.context.selectBarcode = true;
214 $scope.context.toggleDisplay = function() {
215 var item = copyGrid.selectedItems()[0];
217 $location.path('/cat/item/' + item.id);
220 $scope.context.show_triggered_events = function() {
221 var item = copyGrid.selectedItems()[0];
223 $location.path('/cat/item/' + item.id + '/triggered_events');
226 if (copyId.length > 0) {
227 itemSvc.fetch(null,copyId).then(
237 * Detail view -- shows one copy
239 .controller('ViewCtrl',
240 ['$scope','$q','$location','$routeParams','$timeout','$window','egCore','itemSvc','egBilling',
241 function($scope , $q , $location , $routeParams , $timeout , $window , egCore , itemSvc , egBilling) {
242 var copyId = $routeParams.id;
243 $scope.tab = $routeParams.tab || 'summary';
244 $scope.context.page = 'detail';
247 if ($scope.tab == 'edit') {
248 $scope.tab = 'summary';
253 // use the cached record info
255 $scope.summaryRecord = itemSvc.copy.call_number().record();
257 function loadCopy(barcode) {
258 $scope.context.itemNotFound = false;
260 // Avoid re-fetching the same copy while jumping tabs.
261 // In addition to being quicker, this helps to avoid flickering
262 // of the top panel which is always visible in the detail view.
264 // 'barcode' represents the loading of a new item - refetch it
265 // regardless of whether it matches the current item.
266 if (!barcode && itemSvc.copy && itemSvc.copy.id() == copyId) {
267 $scope.copy = itemSvc.copy;
268 $scope.recordId = itemSvc.copy.call_number().record().id();
275 var deferred = $q.defer();
276 itemSvc.fetch(barcode, copyId, true).then(function(res) {
277 $scope.context.selectBarcode = true;
281 $scope.context.itemNotFound = true;
282 deferred.reject(); // avoid propagation of data fetch calls
291 $scope.recordId = copy.call_number().record().id();
292 $scope.summaryRecord = itemSvc.copy.call_number().record();
293 $scope.args.barcode = '';
295 // locally flesh org units
296 copy.circ_lib(egCore.org.get(copy.circ_lib()));
297 copy.call_number().owning_lib(
298 egCore.org.get(copy.call_number().owning_lib()));
300 var r = copy.call_number().record();
301 if (r.owner()) r.owner(egCore.org.get(r.owner()));
303 // make boolean for auto-magic true/false display
305 ['ref','opac_visible','holdable','floating','circulate'],
306 function(field) { copy[field](Boolean(copy[field]() == 't')) }
309 // finally, if this is a different copy, redirect.
310 // Note that we flesh first since the copy we just
311 // fetched will be used after the redirect.
312 if (copyId && copyId != copy.id()) {
313 // if a new barcode is scanned in the detail view,
314 // update the url to match the ID of the new copy
315 $location.path('/cat/item/' + copy.id() + '/' + $scope.tab);
316 deferred.reject(); // avoid propagation of data fetch calls
324 return deferred.promise;
327 // if loadPrev load the two most recent circulations
328 function loadCurrentCirc(loadPrev) {
330 delete $scope.circ_summary;
331 delete $scope.prev_circ_summary;
334 egCore.pcrud.search('circ',
335 {target_copy : copyId},
341 'checkin_workstation',
344 'recurring_fine_rule'
348 order_by : {circ : 'xact_start desc'},
352 ).then(null, null, function(circ) {
355 // load the chain for this circ
358 'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
359 egCore.auth.token(), $scope.circ.id()
360 ).then(function(summary) {
361 $scope.circ_summary = summary.summary;
364 if (!loadPrev) return;
366 // load the chain for the previous circ, plus the user
369 'open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary',
370 egCore.auth.token(), $scope.circ.id()
372 ).then(null, null, function(summary) {
373 $scope.prev_circ_summary = summary.summary;
375 egCore.pcrud.retrieve('au', summary.usr,
376 {flesh : 1, flesh_fields : {au : ['card']}})
378 .then(function(user) {
379 $scope.prev_circ_usr = user;
386 function fetchMaxCircHistory() {
387 if (maxHistory) return $q.when(maxHistory);
388 return egCore.org.settings(
389 'circ.item_checkout_history.max')
390 .then(function(set) {
391 maxHistory = set['circ.item_checkout_history.max'] || 4;
396 $scope.addBilling = function(circ) {
397 egBilling.showBillDialog({
403 $scope.retrieveAllPatrons = function() {
404 var users = new Set();
405 angular.forEach($scope.circ_list.map(function(circ) { return circ.usr(); }),function(usr) {
408 users.forEach(function(usr) {
409 $timeout(function() {
410 var url = $location.absUrl().replace(
412 '/circ/patron/' + usr.id() + '/checkout');
413 $window.open(url, '_blank')
418 function loadCircHistory() {
419 $scope.circ_list = [];
422 itemSvc.copy.call_number().id() == -1 ?
423 itemSvc.copy.circ_lib().id() :
424 itemSvc.copy.call_number().owning_lib().id()
426 // there is an extra layer of permissibility over circ
428 egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
429 .then(function(orgIds) {
431 if (orgIds.indexOf(copy_org) == -1) {
432 console.log('User is not allowed to view circ history');
436 return fetchMaxCircHistory();
438 }).then(function(count) {
440 egCore.pcrud.search('circ',
441 {target_copy : copyId},
447 'checkin_workstation',
448 'recurring_fine_rule'
452 order_by : {circ : 'xact_start desc'},
456 ).then(null, null, function(circ) {
458 // flesh circ_lib locally
459 circ.circ_lib(egCore.org.get(circ.circ_lib()));
460 circ.checkin_lib(egCore.org.get(circ.checkin_lib()));
461 $scope.circ_list.push(circ);
467 function loadCircCounts() {
469 delete $scope.circ_counts;
470 $scope.total_circs = 0;
471 $scope.total_circs_this_year = 0;
472 $scope.total_circs_prev_year = 0;
475 egCore.pcrud.search('circbyyr',
476 {copy : copyId}, null, {atomic : true})
478 .then(function(counts) {
479 $scope.circ_counts = counts;
481 angular.forEach(counts, function(count) {
482 $scope.total_circs += Number(count.count());
485 var this_year = counts.filter(function(c) {
486 return c.year() == new Date().getFullYear();
489 $scope.total_circs_this_year =
490 this_year.length ? this_year[0].count() : 0;
492 var prev_year = counts.filter(function(c) {
493 return c.year() == new Date().getFullYear() - 1;
496 $scope.total_circs_prev_year =
497 prev_year.length ? prev_year[0].count() : 0;
502 function loadHolds() {
506 egCore.pcrud.search('ahr',
507 { current_copy : copyId,
509 fulfillment_time : null,
510 capture_time : {'<>' : null}
514 ahr : ['requestor', 'usr'],
518 ).then(null, null, function(hold) {
520 hold.pickup_lib(egCore.org.get(hold.pickup_lib()));
521 if (hold.current_shelf_lib()) {
522 hold.current_shelf_lib(
523 egCore.org.get(hold.current_shelf_lib()));
525 hold.behind_desk(Boolean(hold.behind_desk() == 't'));
529 function loadTransits() {
530 delete $scope.transit;
531 delete $scope.hold_transit;
534 egCore.pcrud.search('atc',
535 {target_copy : copyId},
536 {order_by : {atc : 'source_send_time DESC'}}
538 ).then(null, null, function(transit) {
539 $scope.transit = transit;
540 transit.source(egCore.org.get(transit.source()));
541 transit.dest(egCore.org.get(transit.dest()));
546 // we don't need all data on all tabs, so fetch what's needed when needed.
547 function loadTabData() {
555 loadCurrentCirc(true);
567 case 'triggered_events':
568 var url = $location.absUrl().replace(/\/staff.*/, '/actor/user/event_log');
569 url += '?copy_id=' + encodeURIComponent(copyId);
570 $scope.triggered_events_url = url;
577 'open-ils.actor.anon_cache.set_value',
578 null, 'edit-these-copies', {
579 record_id: $scope.recordId,
584 ).then(function(key) {
586 var url = egCore.env.basePath + 'cat/volcopy/' + key;
587 $window.location.href = url;
589 alert('Could not create anonymous cache key!');
597 $scope.context.toggleDisplay = function() {
598 $location.path('/cat/item/search');
601 // handle the barcode scan box, which will replace our current copy
602 $scope.context.search = function(args) {
603 loadCopy(args.barcode).then(loadTabData);
606 $scope.context.show_triggered_events = function() {
607 $location.path('/cat/item/' + copyId + '/triggered_events');
610 loadCopy().then(loadTabData);