]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/services/holdings.js
78055cf402e197860f25b9acde3858ed4e13daf9
[working/Evergreen.git] / Open-ILS / web / js / ui / default / staff / cat / services / holdings.js
1 angular.module('egHoldingsMod', ['egCoreMod','egGridMod'])
2
3 .factory('holdingsSvc', 
4        ['egCore','$q',
5 function(egCore , $q) {
6
7     var service = function() {
8         this.ongoing = false;
9         this.copies = []; // record search results
10         this.index = 0; // search grid index
11         this.org = null;
12         this.rid = null;
13     };
14
15     service.prototype.flesh = {   
16         flesh : 3,
17         flesh_fields : {
18             acp : ['status','location','circ_lib','parts','age_protect','copy_alerts', 'latest_inventory'],
19             acn : ['prefix','suffix','copies','label_class','record'],
20             bre : ['simple_record'],
21             alci : ['inventory_workstation']
22         }
23     }
24
25     service.prototype.fetchAgain = function() {
26         return this.fetch({
27             rid: this.rid,
28             org: this.org,
29             copy: this.copy,
30             vol: this.vol,
31             empty: this.empty,
32             empty_org: this.empty_org
33         })
34     };
35
36     // resolved with the last received copy
37     service.prototype.fetch = function(opts) {
38         var svc = this;
39
40         if (svc.ongoing && svc.p) {
41             svc.p.cancel = true;
42             console.log('Canceling fetch for org '+ svc.org.id());
43             if (svc.p.reject) svc.p.reject();
44         }
45
46         var rid = opts.rid;
47         var empty_org = opts.empty_org;
48         var org = opts.org;
49         var copy = opts.copy;
50         var vol = opts.vol;
51         var empty = opts.empty;
52
53         if (!rid) return $q.when();
54         if (!org) return $q.when();
55
56         svc.ongoing = true;
57
58         svc.rid = rid;
59         svc.empty_org = opts.empty_org;
60         svc.org = org;
61         svc.copy = opts.copy;
62         svc.vol = opts.vol;
63         svc.empty = opts.empty;
64
65         svc.copies = [];
66         svc.index = 0;
67
68         var org_list = egCore.org.descendants(org.id(), true);
69         console.log('Holdings fetch with: rid='+rid+' org='+org_list+' copy='+copy+' vol='+vol+' empty='+empty);
70
71         svc.org_use_map = {};
72         org_list.map(function(o){svc.org_use_map[''+o]=0;})
73
74         var p = egCore.pcrud.search(
75             'acn',
76             {record : rid, owning_lib : org_list, deleted : 'f', label : {'!=' : '##URI##'}},
77             svc.flesh
78         ).then(
79             function() { // finished
80                 if (p.cancel) return;
81
82                 // create virtual field for displaying active parts
83                 angular.forEach(svc.copies, function (cp) {
84                     cp.monograph_parts = '';
85                     if (cp.parts && cp.parts.length > 0) {
86                         cp.monograph_parts = cp.parts.map(function(obj) { return obj.label; }).join(', ');
87                         cp.monograph_parts_sortkeys = cp.parts.map(function(obj) { return obj.label_sortkey; }).join();
88                     }
89                 });
90
91                 svc.copies = svc.copies.sort(
92                     function (a, b) {
93                         function compare_array (x, y, i) {
94                             if (x[i] && y[i]) { // both have values
95                                 if (x[i] == y[i]) { // need to look deeper
96                                     return compare_array(x, y, ++i);
97                                 }
98
99                                 if (x[i] < y[i]) { // x is first
100                                     return -1;
101                                 } else if (x[i] > y[i]) { // y is first
102                                     return 1;
103                                 }
104
105                             } else { // no orgs to compare ...
106                                 if (x[i]) return -1;
107                                 if (y[i]) return 1;
108                             }
109                             return 0;
110                         }
111
112                         var owner_order = compare_array(a.owner_list, b.owner_list, 0);
113                         if (!owner_order) {
114                             // now compare on CN label
115                             if (a.call_number.label < b.call_number.label) return -1;
116                             if (a.call_number.label > b.call_number.label) return 1;
117
118                             // also parts sortkeys combined string
119                             if (a.monograph_parts_sortkeys < b.monograph_parts_sortkeys) return -1;
120                             if (a.monograph_parts_sortkeys > b.monograph_parts_sortkeys) return 1;
121
122                             // try copy number
123                             if (a.copy_number < b.copy_number) return -1;
124                             if (a.copy_number > b.copy_number) return 1;
125
126                             // finally, barcode
127                             if (a.barcode < b.barcode) return -1;
128                             if (a.barcode > b.barcode) return 1;
129                         }
130                         return owner_order;
131                     }
132                 );
133
134                 // create virtual fields for copy alert count and most recent circ
135                 angular.forEach(svc.copies, function (cp) {
136                     if (cp.copy_alerts) {
137                         cp.copy_alert_count = cp.copy_alerts.filter(function(aca) { return aca.ack_time == null ;}).length;
138                     }
139                     else cp.copy_alert_count = 0;
140
141                     var copy_circ = egCore.pcrud.search('combcirc', { target_copy : cp.id },
142                         {
143                             order_by : {combcirc : 'xact_start desc'},
144                             limit :  1
145                         }
146                     ).then(function(copy_circ) {
147                         if (copy_circ) {
148                             cp._circ = egCore.idl.toHash(copy_circ, true);
149                             cp._circ_lib = copy_circ.circ_lib();
150                             cp._duration = copy_circ.duration();
151                         }
152                         return copy_circ;
153                     });
154                 });
155
156                 // create a label using just the unique part of the owner list
157                 var index = 0;
158                 var prev_owner_list;
159                 angular.forEach(svc.copies, function (cp) {
160                     if (!prev_owner_list) {
161                         cp.owner_label = cp.owner_list.join(' ... ');
162                     } else {
163                         var current_owner_list = cp.owner_list.slice();
164                         while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
165                             current_owner_list.shift();
166                             prev_owner_list.shift();
167                         }
168                         cp.owner_label = current_owner_list.join(' ... ');
169                     }
170
171                     cp.index = index++;
172                     prev_owner_list = cp.owner_list.slice();
173                 });
174
175                 var new_list = svc.copies;
176                 if (!copy || !vol) { // collapse copy rows, supply a count instead
177
178                     index = 0;
179                     var cp_list = [];
180                     var prev_key;
181                     var current_blob = { copy_count : 0 };
182                     angular.forEach(new_list, function (cp) {
183                         if (!prev_key) {
184                             prev_key = cp.owner_list.join('') + cp.call_number.label;
185                             if (cp.barcode) current_blob.copy_count = 1;
186                             current_blob.index = index++;
187                             current_blob.id_list = cp.id_list;
188                             if (cp.raw) current_blob.raw = cp.raw;
189                             current_blob.call_number = cp.call_number;
190                             current_blob.owner_list = cp.owner_list;
191                             current_blob.owner_label = cp.owner_label;
192                             current_blob.owner_id = cp.owner_id;
193                         } else {
194                             var current_key = cp.owner_list.join('') + cp.call_number.label;
195                             if (prev_key == current_key) { // collapse into current_blob
196                                 current_blob.copy_count++;
197                                 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
198                                 current_blob.raw = current_blob.raw.concat(cp.raw);
199                             } else {
200                                 current_blob.barcode = current_blob.copy_count;
201                                 cp_list.push(current_blob);
202                                 prev_key = current_key;
203                                 current_blob = { copy_count : 0 };
204                                 if (cp.barcode) current_blob.copy_count = 1;
205                                 current_blob.index = index++;
206                                 current_blob.id_list = cp.id_list;
207                                 if (cp.raw) current_blob.raw = cp.raw;
208                                 current_blob.owner_label = cp.owner_label;
209                                 current_blob.owner_id = cp.owner_id;
210                                 current_blob.call_number = cp.call_number;
211                                 current_blob.owner_list = cp.owner_list;
212                             }
213                         }
214                     });
215
216                     current_blob.barcode = current_blob.copy_count;
217                     cp_list.push(current_blob);
218                     new_list = cp_list;
219
220                     if (!vol) { // do the same for vol rows
221
222                         index = 0;
223                         var cn_list = [];
224                         prev_key = '';
225                         current_blob = { copy_count : 0 };
226                         angular.forEach(cp_list, function (cp) {
227                             if (!prev_key) {
228                                 prev_key = cp.owner_list.join('');
229                                 current_blob.index = index++;
230                                 current_blob.id_list = cp.id_list;
231                                 if (cp.raw) current_blob.raw = cp.raw;
232                                 current_blob.cn_count = 1;
233                                 current_blob.copy_count = cp.copy_count;
234                                 current_blob.owner_list = cp.owner_list;
235                                 current_blob.owner_label = cp.owner_label;
236                                 current_blob.owner_id = cp.owner_id;
237                             } else {
238                                 var current_key = cp.owner_list.join('');
239                                 if (prev_key == current_key) { // collapse into current_blob
240                                     current_blob.cn_count++;
241                                     current_blob.copy_count += cp.copy_count;
242                                     current_blob.id_list = current_blob.id_list.concat(cp.id_list);
243                                     if (cp.raw) {
244                                         if (current_blob.raw) current_blob.raw = current_blob.raw.concat(cp.raw);
245                                         else current_blob.raw = cp.raw;
246                                     }
247                                 } else {
248                                     current_blob.barcode = current_blob.copy_count;
249                                     current_blob.call_number = { label : current_blob.cn_count };
250                                     cn_list.push(current_blob);
251                                     prev_key = current_key;
252                                     current_blob = { copy_count : 0 };
253                                     current_blob.index = index++;
254                                     current_blob.id_list = cp.id_list;
255                                     if (cp.raw) current_blob.raw = cp.raw;
256                                     current_blob.owner_label = cp.owner_label;
257                                     current_blob.owner_id = cp.owner_id;
258                                     current_blob.cn_count = 1;
259                                     current_blob.copy_count = cp.copy_count;
260                                     current_blob.owner_list = cp.owner_list;
261                                 }
262                             }
263                         });
264     
265                         current_blob.barcode = current_blob.copy_count;
266                         current_blob.call_number = { label : current_blob.cn_count };
267                         cn_list.push(current_blob);
268                         new_list = cn_list;
269     
270                     }
271                 }
272
273                 if (empty_org) {
274
275                     var empty_org_list = [];
276                     angular.forEach(svc.org_use_map,function(v,k){
277                         if (v == 0) empty_org_list.push(k);
278                     });
279
280                     angular.forEach(empty_org_list, function (oid) {
281                         var owner = egCore.org.get(oid);
282                         if (owner.ou_type().can_have_vols() != 't') return;
283
284                         var owner_list = [];
285                         while (owner.parent_ou()) { // we're going to skip the top of the tree...
286                             owner_list.unshift(owner.shortname());
287                             owner = egCore.org.get(owner.parent_ou());
288                         }
289
290                         var owner_label = owner_list.join(' ... ');
291
292                         new_list.push({
293                             index      : index++,
294                             id_list    : [],
295                             call_number: { label : '' },
296                             barcode    : '',
297                             owner_id   : oid,
298                             owner_list : owner_list,
299                             owner_label: owner_label,
300                             copy_count : 0,
301                             cn_count   : 0,
302                             copy_alert_count : 0
303                         });
304                     });
305                 }
306
307                 svc.copies = new_list;
308                 svc.ongoing = false;
309             },
310
311             null, // error
312
313             // notify reads the stream of copies, one at a time.
314             function(cn) {
315                 if (p.cancel) return;
316
317                 var copies = cn.copies().filter(function(cp){ return cp.deleted() == 'f' });
318                 cn.copies([]);
319
320                 angular.forEach(copies, function (cp) {
321                     cp.call_number(cn);
322                 });
323
324                 var owner_id = cn.owning_lib();
325                 var owner = egCore.org.get(owner_id);
326                 svc.org_use_map[''+owner_id] += 1;
327
328                 var owner_name_list = [];
329                 while (owner.parent_ou()) { // we're going to skip the top of the tree...
330                     owner_name_list.unshift(owner.shortname());
331                     owner = egCore.org.get(owner.parent_ou());
332                 }
333
334                 if (copies[0]) {
335                     var flat = [];
336                     angular.forEach(copies, function (cp) {
337                         var flat_cp = egCore.idl.toHash(cp);
338                         flat_cp.owner_id = owner_id;
339                         flat_cp.owner_list = owner_name_list;
340                         flat_cp.id_list = [flat_cp.id];
341                         flat_cp.raw = [cp];
342                         flat.push(flat_cp);
343                     });
344
345                     svc.copies = svc.copies.concat(flat);
346                 } else if (empty) {
347                     svc.copies.push({
348                         id_list    : [],
349                         owner_id   : owner_id,
350                         owner_list : owner_name_list,
351                         call_number: egCore.idl.toHash(cn),
352                         raw_call_number: cn
353                     });
354                 }
355
356                 return cn;
357             }
358         );
359
360         return svc.p = p;
361     };
362
363     return service;
364 }])
365 .directive("egVolumeList", function () {
366     return {
367         restrict:   'AE',
368         scope: {
369             recordId : '=',
370             editVolumes : '@',
371             editCopies  : '@'
372         },
373         templateUrl: './cat/share/t_volume_list',
374         controller:
375                    ['$scope','holdingsSvc','egCore','egGridDataProvider','$uibModal',
376             function($scope , holdingsSvc , egCore , egGridDataProvider,  $uibModal) {
377                 var holdingsSvcInst = new holdingsSvc();
378
379                 $scope.holdingsGridControls = {};
380                 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
381                     get : function(offset, count) {
382                         return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
383                     }
384                 });
385
386                 function gatherHoldingsIds () {
387                     var cp_id_list = [];
388                     angular.forEach(
389                         $scope.holdingsGridControls.allItems(),
390                         function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
391                     );
392                     return cp_id_list;
393                 }
394
395                 var spawn_volume_editor = function (copies_too) {
396                     egCore.net.request(
397                         'open-ils.actor',
398                         'open-ils.actor.anon_cache.set_value',
399                         null, 'edit-these-copies', {
400                             record_id: $scope.recordId,
401                             copies: gatherHoldingsIds(),
402                             hide_vols : false,
403                             hide_copies : ((copies_too) ? false : true)
404                         }
405                     ).then(function(key) {
406                         if (key) {
407                             $uibModal.open({
408                                 templateUrl: './cat/share/t_embedded_volcopy',
409                                 backdrop: 'static',
410                                 size: 'lg',
411                                 windowClass: 'eg-wide-modal',
412                                 controller:
413                                     ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
414                                     $scope.volcopy_url = 
415                                         egCore.env.basePath + 'cat/volcopy/' + key + '/embedded';
416                                     $scope.ok = function(args) { $uibModalInstance.close(args) }
417                                     $scope.cancel = function () { $uibModalInstance.dismiss() }
418                                 }]
419                             }).result.then(function() {
420                                 load_holdings();
421                             });
422                         }
423                     });
424                 }
425                 $scope.edit_volumes = function() {
426                     spawn_volume_editor(false);
427                 }
428                 $scope.edit_copies = function() {
429                     spawn_volume_editor(true);
430                 }
431
432                 function load_holdings() {
433                     holdingsSvcInst.fetch({
434                         rid   : $scope.recordId,
435                         org   : egCore.org.root(),
436                         copy  : false,
437                         vol   : true,
438                         empty : true
439                     }).then(function() {
440                         $scope.holdingsGridDataProvider.refresh();
441                     });
442                 };
443                 $scope.$watch('recordId',
444                     function(newVal, oldVal) {
445                         if (newVal && newVal !== oldVal) {
446                             load_holdings();
447                         }
448                     }
449                 );
450                 load_holdings();
451             }]
452     }
453 })
454 .filter('string_pick', function() { return function(i){ return arguments[i] || ''; }; })
455 ;