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 svc.copies = svc.copies.sort(
83 function compare_array (x, y, i) {
84 if (x[i] && y[i]) { // both have values
85 if (x[i] == y[i]) { // need to look deeper
86 return compare_array(x, y, ++i);
89 if (x[i] < y[i]) { // x is first
91 } else if (x[i] > y[i]) { // y is first
95 } else { // no orgs to compare ...
102 var owner_order = compare_array(a.owner_list, b.owner_list, 0);
104 // now compare on CN label
105 if (a.call_number.label < b.call_number.label) return -1;
106 if (a.call_number.label > b.call_number.label) return 1;
109 if (a.copy_number < b.copy_number) return -1;
110 if (a.copy_number > b.copy_number) return 1;
113 if (a.barcode < b.barcode) return -1;
114 if (a.barcode > b.barcode) return 1;
120 // create virtual field for displaying active parts
121 angular.forEach(svc.copies, function (cp) {
122 cp.monograph_parts = '';
123 if (cp.parts && cp.parts.length > 0) {
124 cp.monograph_parts = cp.parts.map(function(obj) { return obj.label; }).join();
128 // create virtual field for copy alert count
129 angular.forEach(svc.copies, function (cp) {
130 if (cp.copy_alerts) cp.copy_alert_count = cp.copy_alerts.length;
131 else cp.copy_alert_count = 0;
134 // create a label using just the unique part of the owner list
137 angular.forEach(svc.copies, function (cp) {
138 if (!prev_owner_list) {
139 cp.owner_label = cp.owner_list.join(' ... ');
141 var current_owner_list = cp.owner_list.slice();
142 while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
143 current_owner_list.shift();
144 prev_owner_list.shift();
146 cp.owner_label = current_owner_list.join(' ... ');
150 prev_owner_list = cp.owner_list.slice();
153 var new_list = svc.copies;
154 if (!copy || !vol) { // collapse copy rows, supply a count instead
159 var current_blob = { copy_count : 0 };
160 angular.forEach(new_list, function (cp) {
162 prev_key = cp.owner_list.join('') + cp.call_number.label;
163 if (cp.barcode) current_blob.copy_count = 1;
164 current_blob.index = index++;
165 current_blob.id_list = cp.id_list;
166 if (cp.raw) current_blob.raw = cp.raw;
167 current_blob.call_number = cp.call_number;
168 current_blob.owner_list = cp.owner_list;
169 current_blob.owner_label = cp.owner_label;
170 current_blob.owner_id = cp.owner_id;
172 var current_key = cp.owner_list.join('') + cp.call_number.label;
173 if (prev_key == current_key) { // collapse into current_blob
174 current_blob.copy_count++;
175 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
176 current_blob.raw = current_blob.raw.concat(cp.raw);
178 current_blob.barcode = current_blob.copy_count;
179 cp_list.push(current_blob);
180 prev_key = current_key;
181 current_blob = { copy_count : 0 };
182 if (cp.barcode) current_blob.copy_count = 1;
183 current_blob.index = index++;
184 current_blob.id_list = cp.id_list;
185 if (cp.raw) current_blob.raw = cp.raw;
186 current_blob.owner_label = cp.owner_label;
187 current_blob.owner_id = cp.owner_id;
188 current_blob.call_number = cp.call_number;
189 current_blob.owner_list = cp.owner_list;
194 current_blob.barcode = current_blob.copy_count;
195 cp_list.push(current_blob);
198 if (!vol) { // do the same for vol rows
203 current_blob = { copy_count : 0 };
204 angular.forEach(cp_list, function (cp) {
206 prev_key = cp.owner_list.join('');
207 current_blob.index = index++;
208 current_blob.id_list = cp.id_list;
209 if (cp.raw) current_blob.raw = cp.raw;
210 current_blob.cn_count = 1;
211 current_blob.copy_count = cp.copy_count;
212 current_blob.owner_list = cp.owner_list;
213 current_blob.owner_label = cp.owner_label;
214 current_blob.owner_id = cp.owner_id;
216 var current_key = cp.owner_list.join('');
217 if (prev_key == current_key) { // collapse into current_blob
218 current_blob.cn_count++;
219 current_blob.copy_count += cp.copy_count;
220 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
222 if (current_blob.raw) current_blob.raw = current_blob.raw.concat(cp.raw);
223 else current_blob.raw = cp.raw;
226 current_blob.barcode = current_blob.copy_count;
227 current_blob.call_number = { label : current_blob.cn_count };
228 cn_list.push(current_blob);
229 prev_key = current_key;
230 current_blob = { copy_count : 0 };
231 current_blob.index = index++;
232 current_blob.id_list = cp.id_list;
233 if (cp.raw) current_blob.raw = cp.raw;
234 current_blob.owner_label = cp.owner_label;
235 current_blob.owner_id = cp.owner_id;
236 current_blob.cn_count = 1;
237 current_blob.copy_count = cp.copy_count;
238 current_blob.owner_list = cp.owner_list;
243 current_blob.barcode = current_blob.copy_count;
244 current_blob.call_number = { label : current_blob.cn_count };
245 cn_list.push(current_blob);
253 var empty_org_list = [];
254 angular.forEach(svc.org_use_map,function(v,k){
255 if (v == 0) empty_org_list.push(k);
258 angular.forEach(empty_org_list, function (oid) {
259 var owner = egCore.org.get(oid);
260 if (owner.ou_type().can_have_vols() != 't') return;
263 while (owner.parent_ou()) { // we're going to skip the top of the tree...
264 owner_list.unshift(owner.shortname());
265 owner = egCore.org.get(owner.parent_ou());
268 var owner_label = owner_list.join(' ... ');
273 call_number: { label : '' },
276 owner_list : owner_list,
277 owner_label: owner_label,
285 svc.copies = new_list;
291 // notify reads the stream of copies, one at a time.
293 if (p.cancel) return;
295 var copies = cn.copies().filter(function(cp){ return cp.deleted() == 'f' });
298 angular.forEach(copies, function (cp) {
302 var owner_id = cn.owning_lib();
303 var owner = egCore.org.get(owner_id);
304 svc.org_use_map[''+owner_id] += 1;
306 var owner_name_list = [];
307 while (owner.parent_ou()) { // we're going to skip the top of the tree...
308 owner_name_list.unshift(owner.shortname());
309 owner = egCore.org.get(owner.parent_ou());
314 angular.forEach(copies, function (cp) {
315 var flat_cp = egCore.idl.toHash(cp);
316 flat_cp.owner_id = owner_id;
317 flat_cp.owner_list = owner_name_list;
318 flat_cp.id_list = [flat_cp.id];
323 svc.copies = svc.copies.concat(flat);
328 owner_list : owner_name_list,
329 call_number: egCore.idl.toHash(cn),
343 .directive("egVolumeList", function () {
351 templateUrl: './cat/share/t_volume_list',
353 ['$scope','holdingsSvc','egCore','egGridDataProvider','$uibModal',
354 function($scope , holdingsSvc , egCore , egGridDataProvider, $uibModal) {
355 var holdingsSvcInst = new holdingsSvc();
357 $scope.holdingsGridControls = {};
358 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
359 get : function(offset, count) {
360 return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
364 function gatherHoldingsIds () {
367 $scope.holdingsGridControls.allItems(),
368 function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
373 var spawn_volume_editor = function (copies_too) {
376 'open-ils.actor.anon_cache.set_value',
377 null, 'edit-these-copies', {
378 record_id: $scope.recordId,
379 copies: gatherHoldingsIds(),
381 hide_copies : ((copies_too) ? false : true)
383 ).then(function(key) {
386 templateUrl: './cat/share/t_embedded_volcopy',
389 windowClass: 'eg-wide-modal',
391 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
393 egCore.env.basePath + 'cat/volcopy/' + key + '/embedded';
394 $scope.ok = function(args) { $uibModalInstance.close(args) }
395 $scope.cancel = function () { $uibModalInstance.dismiss() }
397 }).result.then(function() {
403 $scope.edit_volumes = function() {
404 spawn_volume_editor(false);
406 $scope.edit_copies = function() {
407 spawn_volume_editor(true);
410 function load_holdings() {
411 holdingsSvcInst.fetch({
412 rid : $scope.recordId,
413 org : egCore.org.root(),
418 $scope.holdingsGridDataProvider.refresh();
421 $scope.$watch('recordId',
422 function(newVal, oldVal) {
423 if (newVal && newVal !== oldVal) {