1 var list; var archived_list; var data; var error; var net; var rows; var archived_rows;
3 function default_focus() { document.getElementById('apply_btn').focus(); } // parent interfaces often call this
5 function penalty_init() {
7 commonStrings = document.getElementById('commonStrings');
8 patronStrings = document.getElementById('patronStrings');
10 if (typeof JSAN == 'undefined') {
12 commonStrings.getString('common.jsan.missing')
16 JSAN.errorLevel = "die"; // none, warn, or die
17 JSAN.addRepository('..');
19 JSAN.use('OpenILS.data'); data = new OpenILS.data(); data.stash_retrieve();
20 XML_HTTP_SERVER = data.server_unadorned;
22 JSAN.use('util.error'); error = new util.error();
23 JSAN.use('util.network'); net = new util.network();
24 JSAN.use('patron.util');
25 JSAN.use('util.list');
26 JSAN.use('util.functional');
27 JSAN.use('util.widgets');
31 window.standing_penalties_event_listeners = new EventListenerList();
32 document.getElementById('date1').year = document.getElementById('date1').year - 1;
33 window.standing_penalties_event_listeners.add(document.getElementById('cmd_apply_penalty'), 'command', handle_apply_penalty, false);
34 window.standing_penalties_event_listeners.add(document.getElementById('cmd_remove_penalty'), 'command', handle_remove_penalty, false);
35 window.standing_penalties_event_listeners.add(document.getElementById('cmd_edit_penalty'), 'command', handle_edit_penalty, false);
36 window.standing_penalties_event_listeners.add(document.getElementById('cmd_archive_penalty'), 'command', handle_archive_penalty, false);
37 window.standing_penalties_event_listeners.add(document.getElementById('cmd_retrieve_archived_penalties'), 'command', handle_retrieve_archived_penalties, false);
42 var err_prefix = 'standing_penalties.js -> penalty_init() : ';
43 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
47 function penalty_cleanup() {
49 window.standing_penalties_event_listeners.removeAll();
51 archived_list.cleanup();
53 var err_prefix = 'standing_penalties.js -> penalty_cleanup() : ';
54 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
58 function init_list() {
61 list = new util.list( 'ausp_list' );
64 'columns' : patron.util.ausp_columns({}),
65 'retrieve_row' : retrieve_row,
66 'on_select' : generate_handle_selection(list)
71 var err_prefix = 'standing_penalties.js -> init_list() : ';
72 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
76 function init_archived_list() {
79 archived_list = new util.list( 'archived_ausp_list' );
82 'columns' : patron.util.ausp_columns({}),
83 '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
84 'on_select' : generate_handle_selection(archived_list)
89 var err_prefix = 'standing_penalties.js -> init_archived_list() : ';
90 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
95 function retrieve_row (params) { // callback function for fleshing rows in a list
96 params.treeitem_node.setAttribute('retrieve_id',params.row.my.ausp.id());
97 params.on_retrieve(params.row);
101 function generate_handle_selection(which_list) {
102 return function (ev) { // handler for list row selection event
103 var sel = which_list.retrieve_selection();
104 var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
105 if (which_list == list) { // top list
106 if (ids.length > 0) {
107 document.getElementById('cmd_remove_penalty').setAttribute('disabled','false');
108 document.getElementById('cmd_edit_penalty').setAttribute('disabled','false');
109 document.getElementById('cmd_archive_penalty').setAttribute('disabled','false');
111 document.getElementById('cmd_remove_penalty').setAttribute('disabled','true');
112 document.getElementById('cmd_edit_penalty').setAttribute('disabled','true');
113 document.getElementById('cmd_archive_penalty').setAttribute('disabled','true');
119 function populate_list() {
124 for (var i = 0; i < xulG.patron.standing_penalties().length; i++) {
128 'ausp' : xulG.patron.standing_penalties()[i],
129 'csp' : xulG.patron.standing_penalties()[i].standing_penalty(),
134 rows[ xulG.patron.standing_penalties()[i].id() ] = list.append( row_params );
138 var err_prefix = 'standing_penalties.js -> populate_list() : ';
139 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
143 function handle_apply_penalty(ev) {
145 JSAN.use('util.window');
146 var win = new util.window();
147 var my_xulG = win.open(
148 urls.XUL_NEW_STANDING_PENALTY,
149 'new_standing_penalty',
150 'chrome,resizable,modal',
154 if (!my_xulG.id) { return 0; }
156 var penalty = new ausp();
157 penalty.usr( xulG.patron.id() );
159 penalty.standing_penalty( my_xulG.id );
160 penalty.org_unit( ses('ws_ou') );
161 penalty.note( my_xulG.note );
165 generate_request_handler_for_penalty_apply( penalty, my_xulG.id )
168 document.getElementById('progress').hidden = false;
171 alert('error: ' + E);
172 var err_prefix = 'standing_penalties.js -> handle_apply_penalty() : ';
173 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
177 function generate_request_handler_for_penalty_apply(penalty,id) {
178 return function(reqobj) {
181 var req = reqobj.getResultObject();
182 if (typeof req.ilsevent != 'undefined') {
183 error.standard_unexpected_error_alert(
184 patronStrings.getFormattedString('staff.patron.standing_penalty.apply_error',[data.hash.csp[id].name()]),
189 JSAN.use('patron.util'); JSAN.use('util.functional');
190 //xulG.patron.standing_penalties( xulG.patron.standing_penalties().concat( penalty ) ); // Not good enough for pcrud
191 xulG.patron = patron.util.retrieve_fleshed_au_via_id( ses(), xulG.patron.id() ); // So get the real deal instead
192 penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == req; } );
198 'csp' : typeof penalty.standing_penalty() == 'object'
199 ? penalty.standing_penalty()
200 : data.hash.csp[ penalty.standing_penalty() ],
205 rows[ penalty.id() ] = list.append( row_params );
208 if (xulG && typeof xulG.refresh == 'function') {
212 document.getElementById('progress').hidden = true;
215 var err_prefix = 'standing_penalties.js -> request_handler_for_penalty_apply() : ';
216 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
221 function handle_remove_penalty(ev) {
224 var sel = list.retrieve_selection();
225 var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
226 if (! ids.length > 0 ) return;
229 for (var i = 0; i < ids.length; i++) {
230 funcs.push( generate_penalty_remove_function(ids[i]) );
235 if (xulG && typeof xulG.refresh == 'function') {
239 document.getElementById('progress').hidden = true;
241 //patron.util.set_penalty_css(xulG.patron);
244 document.getElementById('progress').hidden = false;
245 JSAN.use('util.exec'); var exec = new util.exec();
249 var err_prefix = 'standing_penalties.js -> request_handler_for_penalty_apply() : ';
250 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
254 function generate_penalty_remove_function(id) {
258 var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == id; } );
259 penalty.isdeleted(1);
261 var req = net.simple_request( 'FM_AUSP_REMOVE', [ ses(), penalty ] );
262 if (typeof req.ilsevent != 'undefined' || String(req) != '1') {
263 error.standard_unexpected_error_alert(patronStrings.getFormattedString('staff.patron.standing_penalty.remove_error',[id]),req);
265 var node = rows[ id ].treeitem_node;
266 var parentNode = node.parentNode;
267 parentNode.removeChild( node );
272 var err_prefix = 'standing_penalties.js -> penalty_remove_function() : ';
273 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
278 function handle_edit_penalty(ev) {
281 JSAN.use('util.window');
282 var win = new util.window();
284 var sel = list.retrieve_selection();
285 var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
286 if (ids.length > 0) {
287 for (var i = 0; i < ids.length; i++) {
288 var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == ids[i]; } );
289 var my_xulG = win.open(
290 urls.XUL_EDIT_STANDING_PENALTY,
291 'new_standing_penalty',
292 'chrome,resizable,modal',
294 'id' : typeof penalty.standing_penalty() == 'object' ? penalty.standing_penalty().id() : penalty.standing_penalty(),
295 'note' : penalty.note()
298 if (my_xulG.modify) {
299 document.getElementById('progress').hidden = false;
300 penalty.note( my_xulG.note ); /* this is for rendering, and propogates by reference to the object associated with the row in the GUI */
301 penalty.standing_penalty( my_xulG.id );
302 penalty.ischanged( 1 );
303 dojo.require('openils.PermaCrud');
304 var pcrud = new openils.PermaCrud( { authtoken :ses() });
305 pcrud.update( penalty, {
306 timeout : 10, // makes it synchronous
307 onerror : function(r) {
309 document.getElementById('progress').hidden = true;
310 var res = openils.Util.readResponse(r,true);
311 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.update_error'),res);
316 oncomplete : function gen_func(p,row_id) {
319 var res = openils.Util.readResponse(r,true);
320 /* FIXME - test for success */
321 var row_params = rows[row_id];
322 row_params.row.my.ausp = p;
323 row_params.row.my.csp = p.standing_penalty();
324 list.refresh_row( row_params );
326 //patron.util.set_penalty_css(xulG.patron);
327 document.getElementById('progress').hidden = true;
337 if (xulG && typeof xulG.refresh == 'function') {
344 var err_prefix = 'standing_penalties.js -> handle_edit_penalty() : ';
345 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
349 function handle_archive_penalty(ev) {
351 var outstanding_requests = 0;
352 var sel = list.retrieve_selection();
353 var ids = util.functional.map_list( sel, function(o) { return JSON2js( o.getAttribute('retrieve_id') ); } );
354 if (ids.length > 0) {
355 document.getElementById('progress').hidden = false;
356 for (var i = 0; i < ids.length; i++) {
357 outstanding_requests++;
358 var penalty = util.functional.find_list( xulG.patron.standing_penalties(), function(o) { return o.id() == ids[i]; } );
359 penalty.ischanged( 1 );
360 penalty.stop_date( util.date.formatted_date(new Date(),'%F') );
361 dojo.require('openils.PermaCrud');
362 var pcrud = new openils.PermaCrud( { authtoken :ses() });
363 pcrud.update( penalty, {
364 onerror : function(r) {
366 var res = openils.Util.readResponse(r,true);
367 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.update_error'),res);
371 if (--outstanding_requests==0) {
372 document.getElementById('progress').hidden = true;
375 oncomplete : function gen_func(row_id) {
378 var res = openils.Util.readResponse(r,true);
379 /* FIXME - test for success */
380 var node = rows[row_id].treeitem_node;
381 var parentNode = node.parentNode;
382 parentNode.removeChild( node );
383 delete(rows[row_id]);
387 if (--outstanding_requests==0) {
388 document.getElementById('progress').hidden = true;
390 //patron.util.set_penalty_css(xulG.patron);
397 if (xulG && typeof xulG.refresh == 'function') {
404 var err_prefix = 'standing_penalties.js -> handle_archive_penalty() : ';
405 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);
409 function handle_retrieve_archived_penalties() {
411 document.getElementById('archived_progress').hidden = false;
412 archived_list.clear(); archived_rows = {};
413 JSAN.use('util.date');
414 dojo.require('openils.PermaCrud');
415 var pcrud = new openils.PermaCrud( { authtoken :ses() });
416 var date2 = document.getElementById('date2').dateValue;
417 date2.setDate( date2.getDate() + 1 ); // Javascript will wrap into subsequent months
421 usr : xulG.patron.id(),
424 document.getElementById('date1').value,
425 document.getElementById('date2').value == util.date.formatted_date(new Date(),'%F') ?
426 'now' : util.date.formatted_date( date2 ,'%F')
433 onerror : function(r) {
435 var res = openils.Util.readResponse(r,true);
436 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),res);
438 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),r);
441 oncomplete : function() {
442 document.getElementById('archived_progress').hidden = true;
444 onresponse : function(r) {
446 var my_ausp = openils.Util.readResponse(r);
451 'csp' : my_ausp.standing_penalty(),
456 archived_rows[ my_ausp.id() ] = archived_list.append( row_params );
458 error.standard_unexpected_error_alert(patronStrings.getString('staff.patron.standing_penalty.retrieve_error'),E);
464 var err_prefix = 'standing_penalties.js -> handle_retrieve_archived_penalties() : ';
465 if (error) error.standard_unexpected_error_alert(err_prefix,E); else alert(err_prefix + E);