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) cp.copy_alert_count = cp.copy_alerts.length;
136 else cp.copy_alert_count = 0;
139 // create a label using just the unique part of the owner list
142 angular.forEach(svc.copies, function (cp) {
143 if (!prev_owner_list) {
144 cp.owner_label = cp.owner_list.join(' ... ');
146 var current_owner_list = cp.owner_list.slice();
147 while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
148 current_owner_list.shift();
149 prev_owner_list.shift();
151 cp.owner_label = current_owner_list.join(' ... ');
155 prev_owner_list = cp.owner_list.slice();
158 var new_list = svc.copies;
159 if (!copy || !vol) { // collapse copy rows, supply a count instead
164 var current_blob = { copy_count : 0 };
165 angular.forEach(new_list, function (cp) {
167 prev_key = cp.owner_list.join('') + cp.call_number.label;
168 if (cp.barcode) current_blob.copy_count = 1;
169 current_blob.index = index++;
170 current_blob.id_list = cp.id_list;
171 if (cp.raw) current_blob.raw = cp.raw;
172 current_blob.call_number = cp.call_number;
173 current_blob.owner_list = cp.owner_list;
174 current_blob.owner_label = cp.owner_label;
175 current_blob.owner_id = cp.owner_id;
177 var current_key = cp.owner_list.join('') + cp.call_number.label;
178 if (prev_key == current_key) { // collapse into current_blob
179 current_blob.copy_count++;
180 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
181 current_blob.raw = current_blob.raw.concat(cp.raw);
183 current_blob.barcode = current_blob.copy_count;
184 cp_list.push(current_blob);
185 prev_key = current_key;
186 current_blob = { copy_count : 0 };
187 if (cp.barcode) current_blob.copy_count = 1;
188 current_blob.index = index++;
189 current_blob.id_list = cp.id_list;
190 if (cp.raw) current_blob.raw = cp.raw;
191 current_blob.owner_label = cp.owner_label;
192 current_blob.owner_id = cp.owner_id;
193 current_blob.call_number = cp.call_number;
194 current_blob.owner_list = cp.owner_list;
199 current_blob.barcode = current_blob.copy_count;
200 cp_list.push(current_blob);
203 if (!vol) { // do the same for vol rows
208 current_blob = { copy_count : 0 };
209 angular.forEach(cp_list, function (cp) {
211 prev_key = cp.owner_list.join('');
212 current_blob.index = index++;
213 current_blob.id_list = cp.id_list;
214 if (cp.raw) current_blob.raw = cp.raw;
215 current_blob.cn_count = 1;
216 current_blob.copy_count = cp.copy_count;
217 current_blob.owner_list = cp.owner_list;
218 current_blob.owner_label = cp.owner_label;
219 current_blob.owner_id = cp.owner_id;
221 var current_key = cp.owner_list.join('');
222 if (prev_key == current_key) { // collapse into current_blob
223 current_blob.cn_count++;
224 current_blob.copy_count += cp.copy_count;
225 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
227 if (current_blob.raw) current_blob.raw = current_blob.raw.concat(cp.raw);
228 else current_blob.raw = cp.raw;
231 current_blob.barcode = current_blob.copy_count;
232 current_blob.call_number = { label : current_blob.cn_count };
233 cn_list.push(current_blob);
234 prev_key = current_key;
235 current_blob = { copy_count : 0 };
236 current_blob.index = index++;
237 current_blob.id_list = cp.id_list;
238 if (cp.raw) current_blob.raw = cp.raw;
239 current_blob.owner_label = cp.owner_label;
240 current_blob.owner_id = cp.owner_id;
241 current_blob.cn_count = 1;
242 current_blob.copy_count = cp.copy_count;
243 current_blob.owner_list = cp.owner_list;
248 current_blob.barcode = current_blob.copy_count;
249 current_blob.call_number = { label : current_blob.cn_count };
250 cn_list.push(current_blob);
258 var empty_org_list = [];
259 angular.forEach(svc.org_use_map,function(v,k){
260 if (v == 0) empty_org_list.push(k);
263 angular.forEach(empty_org_list, function (oid) {
264 var owner = egCore.org.get(oid);
265 if (owner.ou_type().can_have_vols() != 't') return;
268 while (owner.parent_ou()) { // we're going to skip the top of the tree...
269 owner_list.unshift(owner.shortname());
270 owner = egCore.org.get(owner.parent_ou());
273 var owner_label = owner_list.join(' ... ');
278 call_number: { label : '' },
281 owner_list : owner_list,
282 owner_label: owner_label,
290 svc.copies = new_list;
296 // notify reads the stream of copies, one at a time.
298 if (p.cancel) return;
300 var copies = cn.copies().filter(function(cp){ return cp.deleted() == 'f' });
303 angular.forEach(copies, function (cp) {
307 var owner_id = cn.owning_lib();
308 var owner = egCore.org.get(owner_id);
309 svc.org_use_map[''+owner_id] += 1;
311 var owner_name_list = [];
312 while (owner.parent_ou()) { // we're going to skip the top of the tree...
313 owner_name_list.unshift(owner.shortname());
314 owner = egCore.org.get(owner.parent_ou());
319 angular.forEach(copies, function (cp) {
320 var flat_cp = egCore.idl.toHash(cp);
321 flat_cp.owner_id = owner_id;
322 flat_cp.owner_list = owner_name_list;
323 flat_cp.id_list = [flat_cp.id];
328 svc.copies = svc.copies.concat(flat);
333 owner_list : owner_name_list,
334 call_number: egCore.idl.toHash(cn),
348 .directive("egVolumeList", function () {
356 templateUrl: './cat/share/t_volume_list',
358 ['$scope','holdingsSvc','egCore','egGridDataProvider','$uibModal',
359 function($scope , holdingsSvc , egCore , egGridDataProvider, $uibModal) {
360 var holdingsSvcInst = new holdingsSvc();
362 $scope.holdingsGridControls = {};
363 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
364 get : function(offset, count) {
365 return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
369 function gatherHoldingsIds () {
372 $scope.holdingsGridControls.allItems(),
373 function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
378 var spawn_volume_editor = function (copies_too) {
381 'open-ils.actor.anon_cache.set_value',
382 null, 'edit-these-copies', {
383 record_id: $scope.recordId,
384 copies: gatherHoldingsIds(),
386 hide_copies : ((copies_too) ? false : true)
388 ).then(function(key) {
391 templateUrl: './cat/share/t_embedded_volcopy',
394 windowClass: 'eg-wide-modal',
396 ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
398 egCore.env.basePath + 'cat/volcopy/' + key + '/embedded';
399 $scope.ok = function(args) { $uibModalInstance.close(args) }
400 $scope.cancel = function () { $uibModalInstance.dismiss() }
402 }).result.then(function() {
408 $scope.edit_volumes = function() {
409 spawn_volume_editor(false);
411 $scope.edit_copies = function() {
412 spawn_volume_editor(true);
415 function load_holdings() {
416 holdingsSvcInst.fetch({
417 rid : $scope.recordId,
418 org : egCore.org.root(),
423 $scope.holdingsGridDataProvider.refresh();
426 $scope.$watch('recordId',
427 function(newVal, oldVal) {
428 if (newVal && newVal !== oldVal) {