]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/ui/default/staff/cat/services/holdings.js
f0e0185c789c3fb55d8ecd6204f1f5fb85fee87f
[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 : 2, 
17         flesh_fields : {
18             acp : ['status','location','circ_lib','parts','age_protect'],
19             acn : ['prefix','suffix','copies']
20         }
21     }
22
23     service.prototype.fetchAgain = function() {
24         return this.fetch({
25             rid: this.rid,
26             org: this.org,
27             copy: this.copy,
28             vol: this.vol,
29             empty: this.empty
30         })
31     };
32
33     // resolved with the last received copy
34     service.prototype.fetch = function(opts) {
35         var svc = this;
36
37         if (svc.ongoing && svc.p) {
38             svc.p.cancel = true;
39             console.log('Canceling fetch for org '+ svc.org.id());
40             if (svc.p.reject) svc.p.reject();
41         }
42
43         var rid = opts.rid;
44         var org = opts.org;
45         var copy = opts.copy;
46         var vol = opts.vol;
47         var empty = opts.empty;
48
49         if (!rid) return $q.when();
50         if (!org) return $q.when();
51
52         svc.ongoing = true;
53
54         svc.rid = rid;
55         svc.org = org;
56         svc.copy = opts.copy;
57         svc.vol = opts.vol;
58         svc.empty = opts.empty;
59
60         svc.copies = [];
61         svc.index = 0;
62
63         var org_list = egCore.org.descendants(org.id(), true);
64         console.log('Holdings fetch with: rid='+rid+' org='+org_list+' copy='+copy+' vol='+vol+' empty='+empty);
65
66         var p = egCore.pcrud.search(
67             'acn',
68             {record : rid, owning_lib : org_list, deleted : 'f'},
69             svc.flesh
70         ).then(
71             function() { // finished
72                 if (p.cancel) return;
73                 svc.copies = svc.copies.sort(
74                     function (a, b) {
75                         function compare_array (x, y, i) {
76                             if (x[i] && y[i]) { // both have values
77                                 if (x[i] == y[i]) { // need to look deeper
78                                     return compare_array(x, y, ++i);
79                                 }
80
81                                 if (x[i] < y[i]) { // x is first
82                                     return -1;
83                                 } else if (x[i] > y[i]) { // y is first
84                                     return 1;
85                                 }
86
87                             } else { // no orgs to compare ...
88                                 if (x[i]) return -1;
89                                 if (y[i]) return 1;
90                             }
91                             return 0;
92                         }
93
94                         var owner_order = compare_array(a.owner_list, b.owner_list, 0);
95                         if (!owner_order) {
96                             // now compare on CN label
97                             if (a.call_number.label < b.call_number.label) return -1;
98                             if (a.call_number.label > b.call_number.label) return 1;
99
100                             // try copy number
101                             if (a.copy_number < b.copy_number) return -1;
102                             if (a.copy_number > b.copy_number) return 1;
103
104                             // finally, barcode
105                             if (a.barcode < b.barcode) return -1;
106                             if (a.barcode > b.barcode) return 1;
107                         }
108                         return owner_order;
109                     }
110                 );
111
112                 // create virtual field for displaying active parts
113                 angular.forEach(svc.copies, function (cp) {
114                     cp.monograph_parts = '';
115                     if (cp.parts && cp.parts.length > 0) {
116                         cp.monograph_parts = cp.parts.map(function(obj) { return obj.label; }).join();
117                     }
118                 });
119
120                 // create a label using just the unique part of the owner list
121                 var index = 0;
122                 var prev_owner_list;
123                 angular.forEach(svc.copies, function (cp) {
124                     if (!prev_owner_list) {
125                         cp.owner_label = cp.owner_list.join(' ... ');
126                     } else {
127                         var current_owner_list = cp.owner_list.slice();
128                         while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
129                             current_owner_list.shift();
130                             prev_owner_list.shift();
131                         }
132                         cp.owner_label = current_owner_list.join(' ... ');
133                     }
134
135                     cp.index = index++;
136                     prev_owner_list = cp.owner_list.slice();
137                 });
138
139                 var new_list = svc.copies;
140                 if (!copy || !vol) { // collapse copy rows, supply a count instead
141
142                     index = 0;
143                     var cp_list = [];
144                     var prev_key;
145                     var current_blob = { copy_count : 0 };
146                     angular.forEach(new_list, function (cp) {
147                         if (!prev_key) {
148                             prev_key = cp.owner_list.join('') + cp.call_number.label;
149                             if (cp.barcode) current_blob.copy_count = 1;
150                             current_blob.index = index++;
151                             current_blob.id_list = cp.id_list;
152                             if (cp.raw) current_blob.raw = cp.raw;
153                             current_blob.call_number = cp.call_number;
154                             current_blob.owner_list = cp.owner_list;
155                             current_blob.owner_label = cp.owner_label;
156                             current_blob.owner_id = cp.owner_id;
157                         } else {
158                             var current_key = cp.owner_list.join('') + cp.call_number.label;
159                             if (prev_key == current_key) { // collapse into current_blob
160                                 current_blob.copy_count++;
161                                 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
162                                 current_blob.raw = current_blob.raw.concat(cp.raw);
163                             } else {
164                                 current_blob.barcode = current_blob.copy_count;
165                                 cp_list.push(current_blob);
166                                 prev_key = current_key;
167                                 current_blob = { copy_count : 0 };
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.owner_label = cp.owner_label;
173                                 current_blob.owner_id = cp.owner_id;
174                                 current_blob.call_number = cp.call_number;
175                                 current_blob.owner_list = cp.owner_list;
176                             }
177                         }
178                     });
179
180                     current_blob.barcode = current_blob.copy_count;
181                     cp_list.push(current_blob);
182                     new_list = cp_list;
183
184                     if (!vol) { // do the same for vol rows
185
186                         index = 0;
187                         var cn_list = [];
188                         prev_key = '';
189                         current_blob = { copy_count : 0 };
190                         angular.forEach(cp_list, function (cp) {
191                             if (!prev_key) {
192                                 prev_key = cp.owner_list.join('');
193                                 current_blob.index = index++;
194                                 current_blob.id_list = cp.id_list;
195                                 if (cp.raw) current_blob.raw = cp.raw;
196                                 current_blob.cn_count = 1;
197                                 current_blob.copy_count = cp.copy_count;
198                                 current_blob.owner_list = cp.owner_list;
199                                 current_blob.owner_label = cp.owner_label;
200                                 current_blob.owner_id = cp.owner_id;
201                             } else {
202                                 var current_key = cp.owner_list.join('');
203                                 if (prev_key == current_key) { // collapse into current_blob
204                                     current_blob.cn_count++;
205                                     current_blob.copy_count += cp.copy_count;
206                                     current_blob.id_list = current_blob.id_list.concat(cp.id_list);
207                                     if (cp.raw) current_blob.raw = current_blob.raw.concat(cp.raw);
208                                 } else {
209                                     current_blob.barcode = current_blob.copy_count;
210                                     current_blob.call_number = { label : current_blob.cn_count };
211                                     cn_list.push(current_blob);
212                                     prev_key = current_key;
213                                     current_blob = { copy_count : 0 };
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.owner_label = cp.owner_label;
218                                     current_blob.owner_id = cp.owner_id;
219                                     current_blob.cn_count = 1;
220                                     current_blob.copy_count = cp.copy_count;
221                                     current_blob.owner_list = cp.owner_list;
222                                 }
223                             }
224                         });
225     
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                         new_list = cn_list;
230     
231                     }
232                 }
233
234                 svc.copies = new_list;
235                 svc.ongoing = false;
236             },
237
238             null, // error
239
240             // notify reads the stream of copies, one at a time.
241             function(cn) {
242                 if (p.cancel) return;
243
244                 var copies = cn.copies().filter(function(cp){ return cp.deleted() == 'f' });
245                 cn.copies([]);
246
247                 angular.forEach(copies, function (cp) {
248                     cp.call_number(cn);
249                 });
250
251                 var owner_id = cn.owning_lib();
252                 var owner = egCore.org.get(owner_id);
253
254                 var owner_name_list = [];
255                 while (owner.parent_ou()) { // we're going to skip the top of the tree...
256                     owner_name_list.unshift(owner.shortname());
257                     owner = egCore.org.get(owner.parent_ou());
258                 }
259
260                 if (copies[0]) {
261                     var flat = [];
262                     angular.forEach(copies, function (cp) {
263                         var flat_cp = egCore.idl.toHash(cp);
264                         flat_cp.owner_id = owner_id;
265                         flat_cp.owner_list = owner_name_list;
266                         flat_cp.id_list = [flat_cp.id];
267                         flat_cp.raw = [cp];
268                         flat.push(flat_cp);
269                     });
270
271                     svc.copies = svc.copies.concat(flat);
272                 } else if (empty) {
273                     svc.copies.push({
274                         id_list    : [],
275                         owner_id   : owner_id,
276                         owner_list : owner_name_list,
277                         call_number: egCore.idl.toHash(cn),
278                         raw_call_number: cn
279                     });
280                 }
281
282                 return cn;
283             }
284         );
285
286         return svc.p = p;
287     };
288
289     return service;
290 }])
291 .directive("egVolumeList", function () {
292     return {
293         restrict:   'AE',
294         scope: {
295             recordId : '=',
296             editVolumes : '@',
297             editCopies  : '@'
298         },
299         templateUrl: './cat/share/t_volume_list',
300         controller:
301                    ['$scope','holdingsSvc','egCore','egGridDataProvider','$uibModal',
302             function($scope , holdingsSvc , egCore , egGridDataProvider,  $uibModal) {
303                 var holdingsSvcInst = new holdingsSvc();
304
305                 $scope.holdingsGridControls = {};
306                 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
307                     get : function(offset, count) {
308                         return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
309                     }
310                 });
311
312                 function gatherHoldingsIds () {
313                     var cp_id_list = [];
314                     angular.forEach(
315                         $scope.holdingsGridControls.allItems(),
316                         function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
317                     );
318                     return cp_id_list;
319                 }
320
321                 var spawn_volume_editor = function (copies_too) {
322                     egCore.net.request(
323                         'open-ils.actor',
324                         'open-ils.actor.anon_cache.set_value',
325                         null, 'edit-these-copies', {
326                             record_id: $scope.recordId,
327                             copies: gatherHoldingsIds(),
328                             hide_vols : false,
329                             hide_copies : ((copies_too) ? false : true)
330                         }
331                     ).then(function(key) {
332                         if (key) {
333                             $uibModal.open({
334                                 templateUrl: './cat/share/t_embedded_volcopy',
335                                 backdrop: 'static',
336                                 size: 'lg',
337                                 windowClass: 'eg-wide-modal',
338                                 controller:
339                                     ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
340                                     $scope.volcopy_url = 
341                                         egCore.env.basePath + 'cat/volcopy/' + key + '/embedded';
342                                     $scope.ok = function(args) { $uibModalInstance.close(args) }
343                                     $scope.cancel = function () { $uibModalInstance.dismiss() }
344                                 }]
345                             }).result.then(function() {
346                                 load_holdings();
347                             });
348                         }
349                     });
350                 }
351                 $scope.edit_volumes = function() {
352                     spawn_volume_editor(false);
353                 }
354                 $scope.edit_copies = function() {
355                     spawn_volume_editor(true);
356                 }
357
358                 function load_holdings() {
359                     holdingsSvcInst.fetch({
360                         rid   : $scope.recordId,
361                         org   : egCore.org.root(),
362                         copy  : false,
363                         vol   : true,
364                         empty : true
365                     }).then(function() {
366                         $scope.holdingsGridDataProvider.refresh();
367                     });
368                 };
369                 $scope.$watch('recordId',
370                     function(newVal, oldVal) {
371                         if (newVal && newVal !== oldVal) {
372                             load_holdings();
373                         }
374                     }
375                 );
376                 load_holdings();
377             }]
378     }
379 })
380 ;