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