]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/standing_penalties.js
Address date sorting in Item Status and Copy Buckets interfaces, too
[working/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                 'retrieve_row' : retrieve_row,
56                 'on_select' : generate_handle_selection(list)
57             } 
58         );
59
60     } catch(E) {
61         var err_prefix = 'standing_penalties.js -> init_list() : ';
62         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
63     }
64 }
65
66 function init_archived_list() {
67     try {
68
69         archived_list = new util.list( 'archived_ausp_list' );
70         archived_list.init( 
71             {
72                 'columns' : patron.util.ausp_columns({}),
73                 '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
74                 'on_select' : generate_handle_selection(archived_list)
75             } 
76         );
77
78     } catch(E) {
79         var err_prefix = 'standing_penalties.js -> init_archived_list() : ';
80         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
81     }
82 }
83
84
85 function retrieve_row (params) { // callback function for fleshing rows in a list
86     params.treeitem_node.setAttribute('retrieve_id',params.row.my.ausp.id()); 
87     params.on_retrieve(params.row); 
88     return params.row; 
89 }
90
91 function generate_handle_selection(which_list) {
92     return function (ev) { // handler for list row selection event
93         var sel = which_list.retrieve_selection();
94         var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
95         if (which_list == list) { // top list
96             if (ids.length > 0) {
97                 document.getElementById('cmd_remove_penalty').setAttribute('disabled','false');
98                 document.getElementById('cmd_edit_penalty').setAttribute('disabled','false');
99                 document.getElementById('cmd_archive_penalty').setAttribute('disabled','false');
100             } else {
101                 document.getElementById('cmd_remove_penalty').setAttribute('disabled','true');
102                 document.getElementById('cmd_edit_penalty').setAttribute('disabled','true');
103                 document.getElementById('cmd_archive_penalty').setAttribute('disabled','true');
104             }
105         }
106     };
107 }
108
109 function populate_list() {
110     try {
111
112         rows = {};
113         list.clear();
114         for (var i = 0; i < xulG.patron.standing_penalties().length; i++) {
115             var row_params = {
116                 'row' : {
117                     'my' : {
118                         'ausp' : xulG.patron.standing_penalties()[i],
119                         'csp' : xulG.patron.standing_penalties()[i].standing_penalty(),
120                         'au' : xulG.patron,
121                     }
122                 }
123             };
124             rows[ xulG.patron.standing_penalties()[i].id() ] = list.append( row_params );
125         };
126
127     } catch(E) {
128         var err_prefix = 'standing_penalties.js -> populate_list() : ';
129         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
130     }
131 }
132
133 function handle_apply_penalty(ev) {
134     try {
135         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
136         JSAN.use('util.window');
137         var win = new util.window();
138         var my_xulG = win.open(
139             urls.XUL_NEW_STANDING_PENALTY,
140             'new_standing_penalty',
141             'chrome,resizable,modal',
142             {}
143         );
144
145         if (!my_xulG.id) { return 0; }
146
147         var penalty = new ausp();
148         penalty.usr( xulG.patron.id() );
149         penalty.isnew( 1 );
150         penalty.standing_penalty( my_xulG.id );
151         penalty.org_unit( ses('ws_ou') );
152         penalty.note( my_xulG.note );
153         net.simple_request(
154             'FM_AUSP_APPLY', 
155             [ ses(), penalty ],
156             generate_request_handler_for_penalty_apply( penalty, my_xulG.id )
157         );
158
159         document.getElementById('progress').hidden = false;
160
161     } catch(E) {
162         alert('error: ' + E);
163         var err_prefix = 'standing_penalties.js -> handle_apply_penalty() : ';
164         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
165     }
166 }
167
168 function generate_request_handler_for_penalty_apply(penalty,id) {
169     return function(reqobj) {
170         try {
171
172             var req = reqobj.getResultObject();
173             if (typeof req.ilsevent != 'undefined') {
174                 error.standard_unexpected_error_alert(
175                     patronStrings.getFormattedString('staff.patron.standing_penalty.apply_error',[data.hash.csp[id].name()]),
176                     req
177                 );
178             } else {
179                 penalty.id(req);
180                 JSAN.use('patron.util'); JSAN.use('util.functional');
181                 //xulG.patron.standing_penalties( xulG.patron.standing_penalties().concat( penalty ) ); // Not good enough for pcrud
182                 xulG.patron = patron.util.retrieve_fleshed_au_via_id( ses(), xulG.patron.id() ); // So get the real deal instead
183                 penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == req; } );
184
185                 var row_params = {
186                     'row' : {
187                         'my' : {
188                             'ausp' : penalty,
189                             'csp' : typeof penalty.standing_penalty() == 'object'
190                                 ? penalty.standing_penalty()
191                                 : data.hash.csp[ penalty.standing_penalty() ],
192                             'au' : xulG.patron,
193                         }
194                     }
195                 };
196                 rows[ penalty.id() ] = list.append( row_params );
197             }
198             /*
199             if (xulG && typeof xulG.refresh == 'function') {
200                 xulG.refresh();
201             }
202             */
203             document.getElementById('progress').hidden = true;
204
205         } catch(E) {
206             var err_prefix = 'standing_penalties.js -> request_handler_for_penalty_apply() : ';
207             if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
208         }
209     };
210 }
211  
212 function handle_remove_penalty(ev) {
213     try {
214
215         var sel = list.retrieve_selection();
216         var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
217         if (! ids.length > 0 ) return;
218
219         var funcs = [];
220         for (var i = 0; i < ids.length; i++) {
221             funcs.push( generate_penalty_remove_function(ids[i]) );
222         } 
223         funcs.push(
224             function() {
225                 /*
226                 if (xulG && typeof xulG.refresh == 'function') {
227                     xulG.refresh();
228                 }
229                 */
230                 document.getElementById('progress').hidden = true;
231
232                 patron.util.set_penalty_css(xulG.patron, patron.display.w.document.documentElement);
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 ].treeitem_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
318                                     patron.util.set_penalty_css(xulG.patron, patron.display.w.document.documentElement);
319                                     document.getElementById('progress').hidden = true;
320                                 } catch(E) {
321                                     alert(E);
322                                 }
323                             }
324                         }(penalty,ids[i])
325                     });
326                 }
327             } 
328             /*
329             if (xulG && typeof xulG.refresh == 'function') {
330                 xulG.refresh();
331             }
332             */
333         }
334
335     } catch(E) {
336         var err_prefix = 'standing_penalties.js -> handle_edit_penalty() : ';
337         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
338     }
339 }
340
341 function handle_archive_penalty(ev) {
342     try {
343         var outstanding_requests = 0;
344         var sel = list.retrieve_selection();
345         var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
346         if (ids.length > 0) {
347             document.getElementById('progress').hidden = false;
348             for (var i = 0; i < ids.length; i++) {
349                 outstanding_requests++;
350                 var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == ids[i]; } );
351                 penalty.ischanged( 1 );
352                 penalty.stop_date( util.date.formatted_date(new Date(),'%F') );
353                 dojo.require('openils.PermaCrud');
354                 var pcrud = new openils.PermaCrud( { authtoken :ses() });
355                 pcrud.update( penalty, {
356                     onerror : function(r) {
357                         try {
358                             var res = openils.Util.readResponse(r,true);
359                             error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.update_error'),res);
360                         } catch(E) {
361                             alert(E);
362                         }
363                         if (--outstanding_requests==0) {
364                             document.getElementById('progress').hidden = true;
365                         }
366                     },
367                     oncomplete : function gen_func(row_id) {
368                         return function(r) {
369                             try {
370                                 var res = openils.Util.readResponse(r,true);
371                                 /* FIXME - test for success */
372                                 var node = rows[row_id].treeitem_node;
373                                 var parentNode = node.parentNode;
374                                 parentNode.removeChild( node );
375                                 delete(rows[row_id]);
376                             } catch(E) {
377                                 alert(E);
378                             }
379                             if (--outstanding_requests==0) {
380                                 document.getElementById('progress').hidden = true;
381
382                                 patron.util.set_penalty_css(xulG.patron, patron.display.w.document.documentElement);
383                             }
384                         }
385                     }(ids[i])
386                 });
387             } 
388             /*
389             if (xulG && typeof xulG.refresh == 'function') {
390                 xulG.refresh();
391             }
392             */
393         }
394
395     } catch(E) {
396         var err_prefix = 'standing_penalties.js -> handle_archive_penalty() : ';
397         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
398     }
399 }
400
401 function handle_retrieve_archived_penalties() {
402     try {
403         document.getElementById('archived_progress').hidden = false;
404         archived_list.clear(); archived_rows = {};
405         JSAN.use('util.date');
406         dojo.require('openils.PermaCrud');
407         var pcrud = new openils.PermaCrud( { authtoken :ses() });
408         var date2 = document.getElementById('date2').dateValue;
409         date2.setDate( date2.getDate() + 1 ); // Javascript will wrap into subsequent months
410         pcrud.search(
411             'ausp',
412             {
413                 usr : xulG.patron.id(),
414                 stop_date : {
415                     'between' : [ 
416                         document.getElementById('date1').value, 
417                         document.getElementById('date2').value == util.date.formatted_date(new Date(),'%F') ? 
418                             'now' : util.date.formatted_date( date2 ,'%F')
419                     ]
420                 }
421             },
422             {
423                 async : true,
424                 streaming : true,
425                 onerror : function(r) {
426                     try {
427                         var res = openils.Util.readResponse(r,true);
428                         error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),res);
429                     } catch(E) {
430                         error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),r);
431                     }
432                 },
433                 oncomplete : function() {
434                     document.getElementById('archived_progress').hidden = true;
435                 },
436                 onresponse : function(r) {
437                     try {
438                         var my_ausp = openils.Util.readResponse(r);
439                         var row_params = {
440                             'row' : {
441                                 'my' : {
442                                     'ausp' : my_ausp,
443                                     'csp' : my_ausp.standing_penalty(),
444                                     'au' : xulG.patron,
445                                 }
446                             }
447                         };
448                         archived_rows[ my_ausp.id() ] = archived_list.append( row_params );
449                     } catch(E) {
450                         error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),E);
451                     }
452                 }
453             }
454         );
455     } catch(E) {
456         var err_prefix = 'standing_penalties.js -> handle_retrieve_archived_penalties() : ';
457         if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
458     }
459 }