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';
246 // use the cached record info
248 $scope.summaryRecord = itemSvc.copy.call_number().record();
250 function loadCopy(barcode) {
251 $scope.context.itemNotFound = false;
253 // Avoid re-fetching the same copy while jumping tabs.
254 // In addition to being quicker, this helps to avoid flickering
255 // of the top panel which is always visible in the detail view.
257 // 'barcode' represents the loading of a new item - refetch it
258 // regardless of whether it matches the current item.
259 if (!barcode && itemSvc.copy && itemSvc.copy.id() == copyId) {
260 $scope.copy = itemSvc.copy;
261 $scope.recordId = itemSvc.copy.call_number().record().id();
268 var deferred = $q.defer();
269 itemSvc.fetch(barcode, copyId, true).then(function(res) {
270 $scope.context.selectBarcode = true;
274 $scope.context.itemNotFound = true;
275 deferred.reject(); // avoid propagation of data fetch calls
284 $scope.recordId = copy.call_number().record().id();
285 $scope.summaryRecord = itemSvc.copy.call_number().record();
286 $scope.args.barcode = '';
288 // locally flesh org units
289 copy.circ_lib(egCore.org.get(copy.circ_lib()));
290 copy.call_number().owning_lib(
291 egCore.org.get(copy.call_number().owning_lib()));
293 var r = copy.call_number().record();
294 if (r.owner()) r.owner(egCore.org.get(r.owner()));
296 // make boolean for auto-magic true/false display
298 ['ref','opac_visible','holdable','floating','circulate'],
299 function(field) { copy[field](Boolean(copy[field]() == 't')) }
302 // finally, if this is a different copy, redirect.
303 // Note that we flesh first since the copy we just
304 // fetched will be used after the redirect.
305 if (copyId && copyId != copy.id()) {
306 // if a new barcode is scanned in the detail view,
307 // update the url to match the ID of the new copy
308 $location.path('/cat/item/' + copy.id() + '/' + $scope.tab);
309 deferred.reject(); // avoid propagation of data fetch calls
317 return deferred.promise;
320 // if loadPrev load the two most recent circulations
321 function loadCurrentCirc(loadPrev) {
323 delete $scope.circ_summary;
324 delete $scope.prev_circ_summary;
327 egCore.pcrud.search('circ',
328 {target_copy : copyId},
334 'checkin_workstation',
337 'recurring_fine_rule'
341 order_by : {circ : 'xact_start desc'},
345 ).then(null, null, function(circ) {
348 // load the chain for this circ
351 'open-ils.circ.renewal_chain.retrieve_by_circ.summary',
352 egCore.auth.token(), $scope.circ.id()
353 ).then(function(summary) {
354 $scope.circ_summary = summary.summary;
357 if (!loadPrev) return;
359 // load the chain for the previous circ, plus the user
362 'open-ils.circ.prev_renewal_chain.retrieve_by_circ.summary',
363 egCore.auth.token(), $scope.circ.id()
365 ).then(null, null, function(summary) {
366 $scope.prev_circ_summary = summary.summary;
368 egCore.pcrud.retrieve('au', summary.usr,
369 {flesh : 1, flesh_fields : {au : ['card']}})
371 .then(function(user) {
372 $scope.prev_circ_usr = user;
379 function fetchMaxCircHistory() {
380 if (maxHistory) return $q.when(maxHistory);
381 return egCore.org.settings(
382 'circ.item_checkout_history.max')
383 .then(function(set) {
384 maxHistory = set['circ.item_checkout_history.max'] || 4;
389 $scope.addBilling = function(circ) {
390 egBilling.showBillDialog({
396 $scope.retrieveAllPatrons = function() {
397 var users = new Set();
398 angular.forEach($scope.circ_list.map(function(circ) { return circ.usr(); }),function(usr) {
401 users.forEach(function(usr) {
402 $timeout(function() {
403 var url = $location.absUrl().replace(
405 '/circ/patron/' + usr.id() + '/checkout');
406 $window.open(url, '_blank')
411 function loadCircHistory() {
412 $scope.circ_list = [];
415 itemSvc.copy.call_number().id() == -1 ?
416 itemSvc.copy.circ_lib().id() :
417 itemSvc.copy.call_number().owning_lib().id()
419 // there is an extra layer of permissibility over circ
421 egCore.perm.hasPermAt('VIEW_COPY_CHECKOUT_HISTORY', true)
422 .then(function(orgIds) {
424 if (orgIds.indexOf(copy_org) == -1) {
425 console.log('User is not allowed to view circ history');
429 return fetchMaxCircHistory();
431 }).then(function(count) {
433 egCore.pcrud.search('circ',
434 {target_copy : copyId},
440 'checkin_workstation',
441 'recurring_fine_rule'
445 order_by : {circ : 'xact_start desc'},
449 ).then(null, null, function(circ) {
451 // flesh circ_lib locally
452 circ.circ_lib(egCore.org.get(circ.circ_lib()));
453 circ.checkin_lib(egCore.org.get(circ.checkin_lib()));
454 $scope.circ_list.push(circ);
460 function loadCircCounts() {
462 delete $scope.circ_counts;
463 $scope.total_circs = 0;
464 $scope.total_circs_this_year = 0;
465 $scope.total_circs_prev_year = 0;
468 egCore.pcrud.search('circbyyr',
469 {copy : copyId}, null, {atomic : true})
471 .then(function(counts) {
472 $scope.circ_counts = counts;
474 angular.forEach(counts, function(count) {
475 $scope.total_circs += Number(count.count());
478 var this_year = counts.filter(function(c) {
479 return c.year() == new Date().getFullYear();
482 $scope.total_circs_this_year =
483 this_year.length ? this_year[0].count() : 0;
485 var prev_year = counts.filter(function(c) {
486 return c.year() == new Date().getFullYear() - 1;
489 $scope.total_circs_prev_year =
490 prev_year.length ? prev_year[0].count() : 0;
495 function loadHolds() {
499 egCore.pcrud.search('ahr',
500 { current_copy : copyId,
502 fulfillment_time : null,
503 capture_time : {'<>' : null}
507 ahr : ['requestor', 'usr'],
511 ).then(null, null, function(hold) {
513 hold.pickup_lib(egCore.org.get(hold.pickup_lib()));
514 if (hold.current_shelf_lib()) {
515 hold.current_shelf_lib(
516 egCore.org.get(hold.current_shelf_lib()));
518 hold.behind_desk(Boolean(hold.behind_desk() == 't'));
522 function loadTransits() {
523 delete $scope.transit;
524 delete $scope.hold_transit;
527 egCore.pcrud.search('atc',
528 {target_copy : copyId},
529 {order_by : {atc : 'source_send_time DESC'}}
531 ).then(null, null, function(transit) {
532 $scope.transit = transit;
533 transit.source(egCore.org.get(transit.source()));
534 transit.dest(egCore.org.get(transit.dest()));
539 // we don't need all data on all tabs, so fetch what's needed when needed.
540 function loadTabData() {
548 loadCurrentCirc(true);
560 case 'triggered_events':
561 var url = $location.absUrl().replace(/\/staff.*/, '/actor/user/event_log');
562 url += '?copy_id=' + encodeURIComponent(copyId);
563 $scope.triggered_events_url = url;
568 $scope.context.toggleDisplay = function() {
569 $location.path('/cat/item/search');
572 // handle the barcode scan box, which will replace our current copy
573 $scope.context.search = function(args) {
574 loadCopy(args.barcode).then(loadTabData);
577 $scope.context.show_triggered_events = function() {
578 $location.path('/cat/item/' + copyId + '/triggered_events');
581 loadCopy().then(loadTabData);