1 angular.module('egHoldingsMod', ['egCoreMod','egGridMod'])
3 .factory('holdingsSvc',
5 function(egCore , $q) {
7 var service = function() {
9 this.copies = []; // record search results
10 this.index = 0; // search grid index
15 service.prototype.flesh = {
18 acp : ['status','location','circ_lib','parts','age_protect','copy_alerts', 'latest_inventory'],
19 acn : ['prefix','suffix','copies'],
20 alci : ['inventory_workstation']
24 service.prototype.fetchAgain = function() {
31 empty_org: this.empty_org
35 // resolved with the last received copy
36 service.prototype.fetch = function(opts) {
39 if (svc.ongoing && svc.p) {
41 console.log('Canceling fetch for org '+ svc.org.id());
42 if (svc.p.reject) svc.p.reject();
46 var empty_org = opts.empty_org;
50 var empty = opts.empty;
52 if (!rid) return $q.when();
53 if (!org) return $q.when();
58 svc.empty_org = opts.empty_org;
62 svc.empty = opts.empty;
67 var org_list = egCore.org.descendants(org.id(), true);
68 console.log('Holdings fetch with: rid='+rid+' org='+org_list+' copy='+copy+' vol='+vol+' empty='+empty);
71 org_list.map(function(o){svc.org_use_map[''+o]=0;})
73 var p = egCore.pcrud.search(
75 {record : rid, owning_lib : org_list, deleted : 'f', label : {'!=' : '##URI##'}},
78 function() { // finished
81 // create virtual field for displaying active parts
82 angular.forEach(svc.copies, function (cp) {
83 cp.monograph_parts = '';
84 if (cp.parts && cp.parts.length > 0) {
85 cp.monograph_parts = cp.parts.map(function(obj) { return obj.label; }).join(', ');
86 cp.monograph_parts_sortkeys = cp.parts.map(function(obj) { return obj.label_sortkey; }).join();
90 svc.copies = svc.copies.sort(
92 function compare_array (x, y, i) {
93 if (x[i] && y[i]) { // both have values
94 if (x[i] == y[i]) { // need to look deeper
95 return compare_array(x, y, ++i);
98 if (x[i] < y[i]) { // x is first
100 } else if (x[i] > y[i]) { // y is first
104 } else { // no orgs to compare ...
111 var owner_order = compare_array(a.owner_list, b.owner_list, 0);
113 // now compare on CN label
114 if (a.call_number.label < b.call_number.label) return -1;
115 if (a.call_number.label > b.call_number.label) return 1;
117 // also parts sortkeys combined string
118 if (a.monograph_parts_sortkeys < b.monograph_parts_sortkeys) return -1;
119 if (a.monograph_parts_sortkeys > b.monograph_parts_sortkeys) return 1;
122 if (a.copy_number < b.copy_number) return -1;
123 if (a.copy_number > b.copy_number) return 1;
126 if (a.barcode < b.barcode) return -1;
127 if (a.barcode > b.barcode) return 1;
133 // create virtual field for copy alert count
134 angular.forEach(svc.copies, function (cp) {
135 if (cp.copy_alerts) {
136 cp.copy_alert_count = cp.copy_alerts.filter(function(aca) { return aca.ack_time == null ;}).length;
138 else cp.copy_alert_count = 0;
141 // create a label using just the unique part of the owner list
144 angular.forEach(svc.copies, function (cp) {
145 if (!prev_owner_list) {
146 cp.owner_label = cp.owner_list.join(' ... ');
148 var current_owner_list = cp.owner_list.slice();
149 while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
150 current_owner_list.shift();
151 prev_owner_list.shift();
153 cp.owner_label = current_owner_list.join(' ... ');
157 prev_owner_list = cp.owner_list.slice();
160 var new_list = svc.copies;
161 if (!copy || !vol) { // collapse copy rows, supply a count instead
166 var current_blob = { copy_count : 0 };
167 angular.forEach(new_list, function (cp) {
169 prev_key = cp.owner_list.join('') + cp.call_number.label;
170 if (cp.barcode) current_blob.copy_count = 1;
171 current_blob.index = index++;
172 current_blob.id_list = cp.id_list;
173 if (cp.raw) current_blob.raw = cp.raw;
174 current_blob.call_number = cp.call_number;
175 current_blob.owner_list = cp.owner_list;
176 current_blob.owner_label = cp.owner_label;
177 current_blob.owner_id = cp.owner_id;
179 var current_key = cp.owner_list.join('') + cp.call_number.label;
180 if (prev_key == current_key) { // collapse into current_blob
181 current_blob.copy_count++;
182 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
183 current_blob.raw = current_blob.raw.concat(cp.raw);
185 current_blob.barcode = current_blob.copy_count;
186 cp_list.push(current_blob);
187 prev_key = current_key;
188 current_blob = { copy_count : 0 };
189 if (cp.barcode) current_blob.copy_count = 1;
190 current_blob.index = index++;
191 current_blob.id_list = cp.id_list;
192 if (cp.raw) current_blob.raw = cp.raw;
193 current_blob.owner_label = cp.owner_label;
194 current_blob.owner_id = cp.owner_id;
195 current_blob.call_number = cp.call_number;
196 current_blob.owner_list = cp.owner_list;
201 current_blob.barcode = current_blob.copy_count;
202 cp_list.push(current_blob);
205 if (!vol) { // do the same for vol rows
210 current_blob = { copy_count : 0 };
211 angular.forEach(cp_list, function (cp) {
213 prev_key = cp.owner_list.join('');
214 current_blob.index = index++;
215 current_blob.id_list = cp.id_list;
216 if (cp.raw) current_blob.raw = cp.raw;
217 current_blob.cn_count = 1;
218 current_blob.copy_count = cp.copy_count;
219 current_blob.owner_list = cp.owner_list;
220 current_blob.owner_label = cp.owner_label;
221 current_blob.owner_id = cp.owner_id;
223 var current_key = cp.owner_list.join('');
224 if (prev_key == current_key) { // collapse into current_blob
225 current_blob.cn_count++;
226 current_blob.copy_count += cp.copy_count;
227 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
229 if (current_blob.raw) current_blob.raw = current_blob.raw.concat(cp.raw);
230 else current_blob.raw = cp.raw;
233 current_blob.barcode = current_blob.copy_count;
234 current_blob.call_number = { label : current_blob.cn_count };
235 cn_list.push(current_blob);
236 prev_key = current_key;
237 current_blob = { copy_count : 0 };
238 current_blob.index = index++;
239 current_blob.id_list = cp.id_list;
240 if (cp.raw) current_blob.raw = cp.raw;
241 current_blob.owner_label = cp.owner_label;
242 current_blob.owner_id = cp.owner_id;
243 current_blob.cn_count = 1;
244 current_blob.copy_count = cp.copy_count;
245 current_blob.owner_list = cp.owner_list;
250 current_blob.barcode = current_blob.copy_count;
251 current_blob.call_number = { label : current_blob.cn_count };
252 cn_list.push(current_blob);
260 var empty_org_list = [];
261 angular.forEach(svc.org_use_map,function(v,k){
262 if (v == 0) empty_org_list.push(k);
265 angular.forEach(empty_org_list, function (oid) {
266 var owner = egCore.org.get(oid);
267 if (owner.ou_type().can_have_vols() != 't') return;
270 while (owner.parent_ou()) { // we're going to skip the top of the tree...
271 owner_list.unshift(owner.shortname());
272 owner = egCore.org.get(owner.parent_ou());
275 var owner_label = owner_list.join(' ... ');
280 call_number: { label : '' },
283 owner_list : owner_list,
284 owner_label: owner_label,
292 svc.copies = new_list;
298 // notify reads the stream of copies, one at a time.
300 if (p.cancel) return;
302 var copies = cn.copies().filter(function(cp){ return cp.deleted() == 'f' });
305 angular.forEach(copies, function (cp) {
309 var owner_id = cn.owning_lib();
310 var owner = egCore.org.get(owner_id);
311 svc.org_use_map[''+owner_id] += 1;
313 var owner_name_list = [];
314 while (owner.parent_ou()) { // we're going to skip the top of the tree...
315 owner_name_list.unshift(owner.shortname());
316 owner = egCore.org.get(owner.parent_ou());
321 angular.forEach(copies, function (cp) {
322 var flat_cp = egCore.idl.toHash(cp);
323 flat_cp.owner_id = owner_id;
324 flat_cp.owner_list = owner_name_list;
325 flat_cp.id_list = [flat_cp.id];
330 svc.copies = svc.copies.concat(flat);
335 owner_list : owner_name_list,
336 call_number: egCore.idl.toHash(cn),
350 .directive("egVolumeList", function () {
358 templateUrl: './cat/share/t_volume_list',
360 ['$scope','holdingsSvc','egCore','egGridDataProvider','$uibModal',
361 function($scope , holdingsSvc , egCore , egGridDataProvider, $uibModal) {
362 var holdingsSvcInst = new holdingsSvc();
364 $scope.holdingsGridControls = {};
365 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
366 get : function(offset, count) {
367 return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
371 function gatherHoldingsIds () {
374 $scope.holdingsGridControls.allItems(),
375 function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
380 var spawn_volume_editor = function (copies_too) {
383 'open-ils.actor.anon_cache.set_value',
384 null, 'edit-these-copies', {
385 record_id: $scope.recordId,
386 copies: gatherHoldingsIds(),
388 hide_copies : ((copies_too) ? false : true)
390 ).then(function(key) {
393 templateUrl: './cat/share/t_embedded_volcopy',
396 windowClass: 'eg-wide-modal',
398 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
400 egCore.env.basePath + 'cat/volcopy/' + key + '/embedded';
401 $scope.ok = function(args) { $uibModalInstance.close(args) }
402 $scope.cancel = function () { $uibModalInstance.dismiss() }
404 }).result.then(function() {
410 $scope.edit_volumes = function() {
411 spawn_volume_editor(false);
413 $scope.edit_copies = function() {
414 spawn_volume_editor(true);
417 function load_holdings() {
418 holdingsSvcInst.fetch({
419 rid : $scope.recordId,
420 org : egCore.org.root(),
425 $scope.holdingsGridDataProvider.refresh();
428 $scope.$watch('recordId',
429 function(newVal, oldVal) {
430 if (newVal && newVal !== oldVal) {