LP#1676608: copy alert and suppression matrix
[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','copy_alerts'],
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 virtual field for copy alert count
121                 angular.forEach(svc.copies, function (cp) {
122                     cp.copy_alert_count = cp.copy_alerts.length;
123                 });
124
125                 // create a label using just the unique part of the owner list
126                 var index = 0;
127                 var prev_owner_list;
128                 angular.forEach(svc.copies, function (cp) {
129                     if (!prev_owner_list) {
130                         cp.owner_label = cp.owner_list.join(' ... ');
131                     } else {
132                         var current_owner_list = cp.owner_list.slice();
133                         while (current_owner_list[1] && prev_owner_list[1] && current_owner_list[0] == prev_owner_list[0]) {
134                             current_owner_list.shift();
135                             prev_owner_list.shift();
136                         }
137                         cp.owner_label = current_owner_list.join(' ... ');
138                     }
139
140                     cp.index = index++;
141                     prev_owner_list = cp.owner_list.slice();
142                 });
143
144                 var new_list = svc.copies;
145                 if (!copy || !vol) { // collapse copy rows, supply a count instead
146
147                     index = 0;
148                     var cp_list = [];
149                     var prev_key;
150                     var current_blob = { copy_count : 0 };
151                     angular.forEach(new_list, function (cp) {
152                         if (!prev_key) {
153                             prev_key = cp.owner_list.join('') + cp.call_number.label;
154                             if (cp.barcode) current_blob.copy_count = 1;
155                             current_blob.index = index++;
156                             current_blob.id_list = cp.id_list;
157                             if (cp.raw) current_blob.raw = cp.raw;
158                             current_blob.call_number = cp.call_number;
159                             current_blob.owner_list = cp.owner_list;
160                             current_blob.owner_label = cp.owner_label;
161                             current_blob.owner_id = cp.owner_id;
162                         } else {
163                             var current_key = cp.owner_list.join('') + cp.call_number.label;
164                             if (prev_key == current_key) { // collapse into current_blob
165                                 current_blob.copy_count++;
166                                 current_blob.id_list = current_blob.id_list.concat(cp.id_list);
167                                 current_blob.raw = current_blob.raw.concat(cp.raw);
168                             } else {
169                                 current_blob.barcode = current_blob.copy_count;
170                                 cp_list.push(current_blob);
171                                 prev_key = current_key;
172                                 current_blob = { copy_count : 0 };
173                                 if (cp.barcode) current_blob.copy_count = 1;
174                                 current_blob.index = index++;
175                                 current_blob.id_list = cp.id_list;
176                                 if (cp.raw) current_blob.raw = cp.raw;
177                                 current_blob.owner_label = cp.owner_label;
178                                 current_blob.owner_id = cp.owner_id;
179                                 current_blob.call_number = cp.call_number;
180                                 current_blob.owner_list = cp.owner_list;
181                             }
182                         }
183                     });
184
185                     current_blob.barcode = current_blob.copy_count;
186                     cp_list.push(current_blob);
187                     new_list = cp_list;
188
189                     if (!vol) { // do the same for vol rows
190
191                         index = 0;
192                         var cn_list = [];
193                         prev_key = '';
194                         current_blob = { copy_count : 0 };
195                         angular.forEach(cp_list, function (cp) {
196                             if (!prev_key) {
197                                 prev_key = cp.owner_list.join('');
198                                 current_blob.index = index++;
199                                 current_blob.id_list = cp.id_list;
200                                 if (cp.raw) current_blob.raw = cp.raw;
201                                 current_blob.cn_count = 1;
202                                 current_blob.copy_count = cp.copy_count;
203                                 current_blob.owner_list = cp.owner_list;
204                                 current_blob.owner_label = cp.owner_label;
205                                 current_blob.owner_id = cp.owner_id;
206                             } else {
207                                 var current_key = cp.owner_list.join('');
208                                 if (prev_key == current_key) { // collapse into current_blob
209                                     current_blob.cn_count++;
210                                     current_blob.copy_count += cp.copy_count;
211                                     current_blob.id_list = current_blob.id_list.concat(cp.id_list);
212                                     if (cp.raw) current_blob.raw = current_blob.raw.concat(cp.raw);
213                                 } else {
214                                     current_blob.barcode = current_blob.copy_count;
215                                     current_blob.call_number = { label : current_blob.cn_count };
216                                     cn_list.push(current_blob);
217                                     prev_key = current_key;
218                                     current_blob = { copy_count : 0 };
219                                     current_blob.index = index++;
220                                     current_blob.id_list = cp.id_list;
221                                     if (cp.raw) current_blob.raw = cp.raw;
222                                     current_blob.owner_label = cp.owner_label;
223                                     current_blob.owner_id = cp.owner_id;
224                                     current_blob.cn_count = 1;
225                                     current_blob.copy_count = cp.copy_count;
226                                     current_blob.owner_list = cp.owner_list;
227                                 }
228                             }
229                         });
230     
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                         new_list = cn_list;
235     
236                     }
237                 }
238
239                 svc.copies = new_list;
240                 svc.ongoing = false;
241             },
242
243             null, // error
244
245             // notify reads the stream of copies, one at a time.
246             function(cn) {
247                 if (p.cancel) return;
248
249                 var copies = cn.copies().filter(function(cp){ return cp.deleted() == 'f' });
250                 cn.copies([]);
251
252                 angular.forEach(copies, function (cp) {
253                     cp.call_number(cn);
254                 });
255
256                 var owner_id = cn.owning_lib();
257                 var owner = egCore.org.get(owner_id);
258
259                 var owner_name_list = [];
260                 while (owner.parent_ou()) { // we're going to skip the top of the tree...
261                     owner_name_list.unshift(owner.shortname());
262                     owner = egCore.org.get(owner.parent_ou());
263                 }
264
265                 if (copies[0]) {
266                     var flat = [];
267                     angular.forEach(copies, function (cp) {
268                         var flat_cp = egCore.idl.toHash(cp);
269                         flat_cp.owner_id = owner_id;
270                         flat_cp.owner_list = owner_name_list;
271                         flat_cp.id_list = [flat_cp.id];
272                         flat_cp.raw = [cp];
273                         flat.push(flat_cp);
274                     });
275
276                     svc.copies = svc.copies.concat(flat);
277                 } else if (empty) {
278                     svc.copies.push({
279                         id_list    : [],
280                         owner_id   : owner_id,
281                         owner_list : owner_name_list,
282                         call_number: egCore.idl.toHash(cn),
283                         raw_call_number: cn
284                     });
285                 }
286
287                 return cn;
288             }
289         );
290
291         return svc.p = p;
292     };
293
294     return service;
295 }])
296 .directive("egVolumeList", function () {
297     return {
298         restrict:   'AE',
299         scope: {
300             recordId : '=',
301             editVolumes : '@',
302             editCopies  : '@'
303         },
304         templateUrl: './cat/share/t_volume_list',
305         controller:
306                    ['$scope','holdingsSvc','egCore','egGridDataProvider','$uibModal',
307             function($scope , holdingsSvc , egCore , egGridDataProvider,  $uibModal) {
308                 var holdingsSvcInst = new holdingsSvc();
309
310                 $scope.holdingsGridControls = {};
311                 $scope.holdingsGridDataProvider = egGridDataProvider.instance({
312                     get : function(offset, count) {
313                         return this.arrayNotifier(holdingsSvcInst.copies, offset, count);
314                     }
315                 });
316
317                 function gatherHoldingsIds () {
318                     var cp_id_list = [];
319                     angular.forEach(
320                         $scope.holdingsGridControls.allItems(),
321                         function (item) { cp_id_list = cp_id_list.concat(item.id_list) }
322                     );
323                     return cp_id_list;
324                 }
325
326                 var spawn_volume_editor = function (copies_too) {
327                     egCore.net.request(
328                         'open-ils.actor',
329                         'open-ils.actor.anon_cache.set_value',
330                         null, 'edit-these-copies', {
331                             record_id: $scope.recordId,
332                             copies: gatherHoldingsIds(),
333                             hide_vols : false,
334                             hide_copies : ((copies_too) ? false : true)
335                         }
336                     ).then(function(key) {
337                         if (key) {
338                             $uibModal.open({
339                                 templateUrl: './cat/share/t_embedded_volcopy',
340                                 backdrop: 'static',
341                                 size: 'lg',
342                                 windowClass: 'eg-wide-modal',
343                                 controller:
344                                     ['$scope', '$uibModalInstance', function($scope, $uibModalInstance) {
345                                     $scope.volcopy_url = 
346                                         egCore.env.basePath + 'cat/volcopy/' + key + '/embedded';
347                                     $scope.ok = function(args) { $uibModalInstance.close(args) }
348                                     $scope.cancel = function () { $uibModalInstance.dismiss() }
349                                 }]
350                             }).result.then(function() {
351                                 load_holdings();
352                             });
353                         }
354                     });
355                 }
356                 $scope.edit_volumes = function() {
357                     spawn_volume_editor(false);
358                 }
359                 $scope.edit_copies = function() {
360                     spawn_volume_editor(true);
361                 }
362
363                 function load_holdings() {
364                     holdingsSvcInst.fetch({
365                         rid   : $scope.recordId,
366                         org   : egCore.org.root(),
367                         copy  : false,
368                         vol   : true,
369                         empty : true
370                     }).then(function() {
371                         $scope.holdingsGridDataProvider.refresh();
372                     });
373                 };
374                 $scope.$watch('recordId',
375                     function(newVal, oldVal) {
376                         if (newVal && newVal !== oldVal) {
377                             load_holdings();
378                         }
379                     }
380                 );
381                 load_holdings();
382             }]
383     }
384 })
385 ;