]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/standing_penalties.js
Merge remote branch 'working/user/shadowspar/ttopac-altcleanup' into template-toolkit...
[Evergreen.git] / Open-ILS / xul / staff_client / server / patron / standing_penalties.js
1 var list; var archived_list; var data; var error; var net; var rows; var archived_rows;
2
3 function default_focus() { document.getElementById('apply_btn').focus(); } // parent interfaces often call this
4
5 function penalty_init() {
6     try {
7         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
8
9         commonStrings = document.getElementById('commonStrings');
10         patronStrings = document.getElementById('patronStrings');
11
12         if (typeof JSAN == 'undefined') {
13             throw(
14                 commonStrings.getString('common.jsan.missing')
15             );
16         }
17
18         JSAN.errorLevel = "die"; // none, warn, or die
19         JSAN.addRepository('..');
20
21         JSAN.use('OpenILS.data'); data = new OpenILS.data(); data.stash_retrieve();
22         XML_HTTP_SERVER = data.server_unadorned;
23
24         JSAN.use('util.error'); error = new util.error();
25         JSAN.use('util.network'); net = new util.network();
26         JSAN.use('patron.util'); 
27         JSAN.use('util.list'); 
28         JSAN.use('util.functional'); 
29         JSAN.use('util.widgets');
30
31         init_list();
32         init_archived_list();
33         document.getElementById('date1').year = document.getElementById('date1').year - 1;
34         document.getElementById('cmd_apply_penalty').addEventListener('command', handle_apply_penalty, false);
35         document.getElementById('cmd_remove_penalty').addEventListener('command', handle_remove_penalty, false);
36         document.getElementById('cmd_edit_penalty').addEventListener('command', handle_edit_penalty, false);
37         document.getElementById('cmd_archive_penalty').addEventListener('command', handle_archive_penalty, false);
38         document.getElementById('cmd_retrieve_archived_penalties').addEventListener('command', handle_retrieve_archived_penalties, false);
39         populate_list();
40         default_focus();
41
42     } catch(E) {
43         var err_prefix = 'standing_penalties.js -> penalty_init() : ';
44         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
45     }
46 }
47
48 function init_list() {
49     try {
50
51         list = new util.list( 'ausp_list' );
52         list.init( 
53             {
54                 'columns' : patron.util.ausp_columns({}),
55                 'map_row_to_columns' : patron.util.std_map_row_to_columns(),
56                 'retrieve_row' : retrieve_row,
57                 'on_select' : generate_handle_selection(list)
58             } 
59         );
60
61     } catch(E) {
62         var err_prefix = 'standing_penalties.js -> init_list() : ';
63         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
64     }
65 }
66
67 function init_archived_list() {
68     try {
69
70         archived_list = new util.list( 'archived_ausp_list' );
71         archived_list.init( 
72             {
73                 'columns' : patron.util.ausp_columns({}),
74                 'map_row_to_columns' : patron.util.std_map_row_to_columns(),
75                 'retrieve_row' : retrieve_row, // We're getting fleshed objects for now, but if we move to just ausp.id's, then we'll need to put a per-id fetcher in here
76                 'on_select' : generate_handle_selection(archived_list)
77             } 
78         );
79
80     } catch(E) {
81         var err_prefix = 'standing_penalties.js -> init_archived_list() : ';
82         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
83     }
84 }
85
86
87 function retrieve_row (params) { // callback function for fleshing rows in a list
88     params.row_node.setAttribute('retrieve_id',params.row.my.ausp.id()); 
89     params.on_retrieve(params.row); 
90     return params.row; 
91 }
92
93 function generate_handle_selection(which_list) {
94     return function (ev) { // handler for list row selection event
95         var sel = which_list.retrieve_selection();
96         var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
97         if (which_list == list) { // top list
98             if (ids.length > 0) {
99                 document.getElementById('cmd_remove_penalty').setAttribute('disabled','false');
100                 document.getElementById('cmd_edit_penalty').setAttribute('disabled','false');
101                 document.getElementById('cmd_archive_penalty').setAttribute('disabled','false');
102             } else {
103                 document.getElementById('cmd_remove_penalty').setAttribute('disabled','true');
104                 document.getElementById('cmd_edit_penalty').setAttribute('disabled','true');
105                 document.getElementById('cmd_archive_penalty').setAttribute('disabled','true');
106             }
107         }
108     };
109 }
110
111 function populate_list() {
112     try {
113
114         rows = {};
115         list.clear();
116         for (var i = 0; i < xulG.patron.standing_penalties().length; i++) {
117             var row_params = {
118                 'row' : {
119                     'my' : {
120                         'ausp' : xulG.patron.standing_penalties()[i],
121                         'csp' : xulG.patron.standing_penalties()[i].standing_penalty(),
122                         'au' : xulG.patron,
123                     }
124                 }
125             };
126             rows[ xulG.patron.standing_penalties()[i].id() ] = list.append( row_params );
127         };
128
129     } catch(E) {
130         var err_prefix = 'standing_penalties.js -> populate_list() : ';
131         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
132     }
133 }
134
135 function handle_apply_penalty(ev) {
136     try {
137         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
138         JSAN.use('util.window');
139         var win = new util.window();
140         var my_xulG = win.open(
141             urls.XUL_NEW_STANDING_PENALTY,
142             'new_standing_penalty',
143             'chrome,resizable,modal',
144             {}
145         );
146
147         if (!my_xulG.id) { return 0; }
148
149         var penalty = new ausp();
150         penalty.usr( xulG.patron.id() );
151         penalty.isnew( 1 );
152         penalty.standing_penalty( my_xulG.id );
153         penalty.org_unit( ses('ws_ou') );
154         penalty.note( my_xulG.note );
155         net.simple_request(
156             'FM_AUSP_APPLY', 
157             [ ses(), penalty ],
158             generate_request_handler_for_penalty_apply( penalty, my_xulG.id )
159         );
160
161         document.getElementById('progress').hidden = false;
162
163     } catch(E) {
164         alert('error: ' + E);
165         var err_prefix = 'standing_penalties.js -> handle_apply_penalty() : ';
166         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
167     }
168 }
169
170 function generate_request_handler_for_penalty_apply(penalty,id) {
171     return function(reqobj) {
172         try {
173
174             var req = reqobj.getResultObject();
175             if (typeof req.ilsevent != 'undefined') {
176                 error.standard_unexpected_error_alert(
177                     patronStrings.getFormattedString('staff.patron.standing_penalty.apply_error',[data.hash.csp[id].name()]),
178                     req
179                 );
180             } else {
181                 penalty.id(req);
182                 JSAN.use('patron.util'); JSAN.use('util.functional');
183                 //xulG.patron.standing_penalties( xulG.patron.standing_penalties().concat( penalty ) ); // Not good enough for pcrud
184                 xulG.patron = patron.util.retrieve_fleshed_au_via_id( ses(), xulG.patron.id() ); // So get the real deal instead
185                 penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == req; } );
186
187                 var row_params = {
188                     'row' : {
189                         'my' : {
190                             'ausp' : penalty,
191                             'csp' : typeof penalty.standing_penalty() == 'object'
192                                 ? penalty.standing_penalty()
193                                 : data.hash.csp[ penalty.standing_penalty() ],
194                             'au' : xulG.patron,
195                         }
196                     }
197                 };
198                 rows[ penalty.id() ] = list.append( row_params );
199             }
200             /*
201             if (xulG && typeof xulG.refresh == 'function') {
202                 xulG.refresh();
203             }
204             */
205             document.getElementById('progress').hidden = true;
206
207         } catch(E) {
208             var err_prefix = 'standing_penalties.js -> request_handler_for_penalty_apply() : ';
209             if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
210         }
211     };
212 }
213  
214 function handle_remove_penalty(ev) {
215     try {
216
217         var sel = list.retrieve_selection();
218         var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
219         if (! ids.length > 0 ) return;
220
221         var funcs = [];
222         for (var i = 0; i < ids.length; i++) {
223             funcs.push( generate_penalty_remove_function(ids[i]) );
224         } 
225         funcs.push(
226             function() {
227                 /*
228                 if (xulG && typeof xulG.refresh == 'function') {
229                     xulG.refresh();
230                 }
231                 */
232                 document.getElementById('progress').hidden = true;
233             }
234         );
235         document.getElementById('progress').hidden = false;
236         JSAN.use('util.exec'); var exec = new util.exec();
237         exec.chain(funcs);
238
239     } catch(E) {
240         var err_prefix = 'standing_penalties.js -> request_handler_for_penalty_apply() : ';
241         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
242     }
243 }
244
245 function generate_penalty_remove_function(id) {
246     return function() {
247         try {
248
249             var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == id; } );
250             penalty.isdeleted(1);
251
252             var req = net.simple_request( 'FM_AUSP_REMOVE', [ ses(), penalty ] );
253             if (typeof req.ilsevent != 'undefined' || String(req) != '1') {
254                 error.standard_unexpected_error_alert(patronStrings.getFormattedString('staff.patron.standing_penalty.remove_error',[id]),req);
255             } else {
256                 var node = rows[ id ].my_node;
257                 var parentNode = node.parentNode;
258                 parentNode.removeChild( node );
259                 delete(rows[ id ]);
260             }
261
262         } catch(E) {
263             var err_prefix = 'standing_penalties.js -> penalty_remove_function() : ';
264             if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
265         }
266     }; 
267 }
268
269 function handle_edit_penalty(ev) {
270     try {
271
272         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
273         JSAN.use('util.window');
274         var win = new util.window();
275
276         var sel = list.retrieve_selection();
277         var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
278         if (ids.length > 0) {
279             for (var i = 0; i < ids.length; i++) {
280                 var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == ids[i]; } );
281                 var my_xulG = win.open(
282                     urls.XUL_EDIT_STANDING_PENALTY,
283                     'new_standing_penalty',
284                     'chrome,resizable,modal',
285                     { 
286                         'id' : typeof penalty.standing_penalty() == 'object' ? penalty.standing_penalty().id() : penalty.standing_penalty(), 
287                         'note' : penalty.note() 
288                     }
289                 );
290                 if (my_xulG.modify) {
291                     document.getElementById('progress').hidden = false;
292                     penalty.note( my_xulG.note ); /* this is for rendering, and propogates by reference to the object associated with the row in the GUI */
293                     penalty.standing_penalty( my_xulG.id );
294                     penalty.ischanged( 1 );
295                     dojo.require('openils.PermaCrud');
296                     var pcrud = new openils.PermaCrud( { authtoken :ses() });
297                     pcrud.update( penalty, {
298                         timeout : 10, // makes it synchronous
299                         onerror : function(r) {
300                             try {
301                                 document.getElementById('progress').hidden = true;
302                                 var res = openils.Util.readResponse(r,true);
303                                 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.update_error'),res);
304                             } catch(E) {
305                                 alert(E);
306                             }
307                         },
308                         oncomplete : function gen_func(p,row_id) {
309                             return function(r) {
310                                 try {
311                                     var res = openils.Util.readResponse(r,true);
312                                     /* FIXME - test for success */
313                                     var row_params = rows[row_id];
314                                     row_params.row.my.ausp = p;
315                                     row_params.row.my.csp = p.standing_penalty();
316                                     list.refresh_row( row_params );
317                                     document.getElementById('progress').hidden = true;
318                                 } catch(E) {
319                                     alert(E);
320                                 }
321                             }
322                         }(penalty,ids[i])
323                     });
324                 }
325             } 
326             /*
327             if (xulG && typeof xulG.refresh == 'function') {
328                 xulG.refresh();
329             }
330             */
331         }
332
333     } catch(E) {
334         var err_prefix = 'standing_penalties.js -> handle_edit_penalty() : ';
335         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
336     }
337 }
338
339 function handle_archive_penalty(ev) {
340     try {
341         var outstanding_requests = 0;
342         var sel = list.retrieve_selection();
343         var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
344         if (ids.length > 0) {
345             document.getElementById('progress').hidden = false;
346             for (var i = 0; i < ids.length; i++) {
347                 outstanding_requests++;
348                 var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == ids[i]; } );
349                 penalty.ischanged( 1 );
350                 penalty.stop_date( util.date.formatted_date(new Date(),'%F') );
351                 dojo.require('openils.PermaCrud');
352                 var pcrud = new openils.PermaCrud( { authtoken :ses() });
353                 pcrud.update( penalty, {
354                     onerror : function(r) {
355                         try {
356                             var res = openils.Util.readResponse(r,true);
357                             error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.update_error'),res);
358                         } catch(E) {
359                             alert(E);
360                         }
361                         if (--outstanding_requests==0) {
362                             document.getElementById('progress').hidden = true;
363                         }
364                     },
365                     oncomplete : function gen_func(row_id) {
366                         return function(r) {
367                             try {
368                                 var res = openils.Util.readResponse(r,true);
369                                 /* FIXME - test for success */
370                                 var node = rows[row_id].my_node;
371                                 var parentNode = node.parentNode;
372                                 parentNode.removeChild( node );
373                                 delete(rows[row_id]);
374                             } catch(E) {
375                                 alert(E);
376                             }
377                             if (--outstanding_requests==0) {
378                                 document.getElementById('progress').hidden = true;
379                             }
380                         }
381                     }(ids[i])
382                 });
383             } 
384             /*
385             if (xulG && typeof xulG.refresh == 'function') {
386                 xulG.refresh();
387             }
388             */
389         }
390
391     } catch(E) {
392         var err_prefix = 'standing_penalties.js -> handle_archive_penalty() : ';
393         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
394     }
395 }
396
397 function handle_retrieve_archived_penalties() {
398     try {
399         document.getElementById('archived_progress').hidden = false;
400         archived_list.clear(); archived_rows = {};
401         JSAN.use('util.date');
402         dojo.require('openils.PermaCrud');
403         var pcrud = new openils.PermaCrud( { authtoken :ses() });
404         var date2 = document.getElementById('date2').dateValue;
405         date2.setDate( date2.getDate() + 1 ); // Javascript will wrap into subsequent months
406         pcrud.search(
407             'ausp',
408             {
409                 usr : xulG.patron.id(),
410                 stop_date : {
411                     'between' : [ 
412                         document.getElementById('date1').value, 
413                         document.getElementById('date2').value == util.date.formatted_date(new Date(),'%F') ? 
414                             'now' : util.date.formatted_date( date2 ,'%F')
415                     ]
416                 }
417             },
418             {
419                 async : true,
420                 streaming : true,
421                 onerror : function(r) {
422                     try {
423                         var res = openils.Util.readResponse(r,true);
424                         error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),res);
425                     } catch(E) {
426                         error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),r);
427                     }
428                 },
429                 oncomplete : function() {
430                     document.getElementById('archived_progress').hidden = true;
431                 },
432                 onresponse : function(r) {
433                     try {
434                         var my_ausp = openils.Util.readResponse(r);
435                         var row_params = {
436                             'row' : {
437                                 'my' : {
438                                     'ausp' : my_ausp,
439                                     'csp' : my_ausp.standing_penalty(),
440                                     'au' : xulG.patron,
441                                 }
442                             }
443                         };
444                         archived_rows[ my_ausp.id() ] = archived_list.append( row_params );
445                     } catch(E) {
446                         error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),E);
447                     }
448                 }
449             }
450         );
451     } catch(E) {
452         var err_prefix = 'standing_penalties.js -> handle_retrieve_archived_penalties() : ';
453         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
454     }
455 }