]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/bill2.js
lp797321: Disable the Apply Payment button during payment processing to mitigate...
[Evergreen.git] / Open-ILS / xul / staff_client / server / patron / bill2.js
1 function my_init() {
2     try {
3         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
4         if (typeof JSAN == 'undefined') { throw( $("commonStrings").getString('common.jsan.missing') ); }
5         JSAN.errorLevel = "die"; // none, warn, or die
6         JSAN.addRepository('/xul/server/');
7
8         JSAN.use('util.error'); g.error = new util.error();
9         JSAN.use('util.network'); g.network = new util.network();
10         JSAN.use('util.date');
11         JSAN.use('util.money');
12         JSAN.use('util.widgets');
13         JSAN.use('patron.util');
14         JSAN.use('OpenILS.data'); g.data = new OpenILS.data(); g.data.init({'via':'stash'});
15         g.data.voided_billings = []; g.data.stash('voided_billings');
16
17         g.error.sdump('D_TRACE','my_init() for bill2.xul');
18
19         if (xul_param('current')) {
20             $('caption').setAttribute('label',$("patronStrings").getString('staff.patron.bill_history.my_init.current_bills'));
21             document.title = $("patronStrings").getString('staff.patron.bill_history.my_init.current_bills');
22         } else {
23             $('caption').setAttribute('label',$("patronStrings").getString('staff.patron.bill_history.my_init.bill_history'));
24             document.title = $("patronStrings").getString('staff.patron.bill_history.my_init.bill_history');
25         }
26
27         g.funcs = []; g.bill_map = {}; g.row_map = {}; g.check_map = {};
28
29         g.patron_id = xul_param('patron_id');
30
31         $('circulating_hint').hidden = true;
32
33         init_lists();
34
35         retrieve_mbts_for_list();
36
37         event_listeners();
38
39         JSAN.use('util.exec'); var exec = new util.exec(20); 
40         exec.on_error = function(E) { alert(E); return true; }
41         exec.timer(g.funcs,100);
42
43         $('credit_forward').setAttribute('value','???');
44         if (!g.patron) {
45             refresh_patron();
46         } else {
47             $('credit_forward').setAttribute('value',util.money.sanitize( g.patron.credit_forward_balance() ));
48         }
49
50         default_focus();
51
52         tally_all();
53
54     } catch(E) {
55         var err_msg = $("commonStrings").getFormattedString('common.exception', ['patron/bill2.xul', E]);
56         try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); }
57         alert(err_msg);
58     }
59 }
60
61 function event_listeners() {
62     try {
63         $('details').addEventListener(
64             'command',
65             handle_details,
66             false
67         );
68
69         $('add').addEventListener(
70             'command',
71             handle_add,
72             false
73         );
74
75         $('voidall').addEventListener(
76             'command',
77             handle_void_all,
78             false
79         );
80
81         $('refund').addEventListener(
82             'command',
83             handle_refund,
84             false
85         );
86
87         $('opac').addEventListener(
88             'command',
89             handle_opac,
90             false
91         );
92
93         $('copy_details').addEventListener(
94             'command',
95             handle_copy_details,
96             false
97         );
98
99         $('payment').addEventListener(
100             'change',
101             function(ev) {
102                 if ($('payment_type').value == 'credit_payment') {
103                     JSAN.use('util.money');
104                     JSAN.use('patron.util'); g.patron = patron.util.retrieve_fleshed_au_via_id(ses(),g.patron_id,null);
105                     var proposed = util.money.dollars_float_to_cents_integer(ev.target.value);
106                     var available = util.money.dollars_float_to_cents_integer(g.patron.credit_forward_balance());
107                     if (proposed > available) {
108                         alert($("patronStrings").getFormattedString('staff.patron.bills.bill_payment_amount.credit_amount', [g.patron.credit_forward_balance()]));
109                         ev.target.value = util.money.cents_as_dollars( available );
110                         ev.target.setAttribute('value',ev.target.value);
111                     }
112                 }
113                 distribute_payment(); 
114             },
115             false
116         );
117
118         $('payment').addEventListener(
119             'focus',
120             function(ev) { ev.target.select(); },
121             false
122         );
123
124         $('payment').addEventListener(
125             'keypress',
126             function(ev) {
127                 if (! (ev.keyCode == 13 /* enter */ || ev.keyCode == 77 /* mac enter */) ) { return; }
128                 distribute_payment();
129                 $('apply_payment_btn').focus();
130             },
131             false
132         );
133
134         $('bill_patron_btn').addEventListener(
135             'command',
136             function() {
137                 JSAN.use('util.window'); var win = new util.window();
138                 var my_xulG = win.open(
139                     urls.XUL_PATRON_BILL_WIZARD,
140                     'billwizard',
141                     'chrome,resizable,modal',
142                     { 'patron_id' : g.patron_id }
143                 );
144                 if (my_xulG.xact_id) { g.funcs.push( gen_list_append_func( my_xulG.xact_id ) ); /* FIXME: do something to update summary sidebar */ }
145             },
146             false
147         );
148
149         $('bill_history_btn').addEventListener(
150             'command',
151             function() {
152                 xulG.display_window.g.patron.right_deck.reset_iframe( 
153                     urls.XUL_PATRON_BILL_HISTORY,
154                     {},
155                     {
156                         'patron_id' : g.patron_id,
157                         'refresh' : function() { refresh(); },
158                         'new_tab' : xulG.new_tab
159                     }
160                 );
161             },
162             false
163         );
164
165         $('convert_change_to_credit').addEventListener(
166             'command',
167             function(ev) {
168                 if (ev.target.checked) {
169                     addCSSClass( $('change_due'), 'change_to_credit' );
170                 } else {
171                     removeCSSClass( $('change_due'), 'change_to_credit' );
172                 }
173             },
174             false
175         );
176
177         $('apply_payment_btn').addEventListener(
178             'command',
179             function(ev) {
180                 try {
181                     $('apply_payment_btn').disabled = true;
182                     apply_payment();
183                     $('apply_payment_btn').disabled = false;
184                 } catch(E) {
185                     alert('Error in bill2.js, apply_payment_btn: ' + E);
186                 }
187             },
188             false
189         );
190
191     } catch(E) {
192         alert('Error in bill2.js, event_listeners(): ' + E);
193     }
194 }
195
196 function $(id) { return document.getElementById(id); }
197
198 function default_focus() {
199     try { $('payment').focus(); } catch(E) { alert('Error in default_focus(): ' + E); }
200 }
201
202 function tally_pending() {
203     try {
204         var payments = [];
205         JSAN.use('util.money');
206         var tb = $('payment');
207         var payment_tendered = util.money.dollars_float_to_cents_integer( tb.value );
208         var payment_pending = 0;
209         var retrieve_ids = g.bill_list.dump_retrieve_ids();
210         for (var i = 0; i < retrieve_ids.length; i++) {
211             var row_params = g.row_map[retrieve_ids[i]];
212             if (g.check_map[retrieve_ids[i]]) { 
213                 var value = util.money.dollars_float_to_cents_integer( row_params.row.my.payment_pending );
214                 payment_pending += value;
215                 if (value != '0.00') { payments.push( [ retrieve_ids[i], util.money.cents_as_dollars(value) ] ); }
216             }
217         }
218         var change_pending = payment_tendered - payment_pending;
219         $('pending_payment').value = util.money.cents_as_dollars( payment_pending );
220         $('pending_change').value = util.money.cents_as_dollars( change_pending );
221         $('change_due').value = util.money.cents_as_dollars( change_pending );
222         return { 'payments' : payments, 'change' : util.money.cents_as_dollars( change_pending ) };
223     } catch(E) {
224         alert('Error in bill2.js, tally_pending(): ' + E);
225     }
226 }
227
228 function tally_selected() {
229     try {
230         JSAN.use('util.money');
231         var selected_billed = 0;
232         var selected_paid = 0;
233         var selected_balance = 0;
234
235         for (var i = 0; i < g.bill_list_selection.length; i++) {
236             var bill = g.bill_map[g.bill_list_selection[i]];
237             if (!bill) {
238                 //$('checked_owed').setAttribute('value', '???');
239                 //$('checked_billed').setAttribute('value', '???');
240                 //$('checked_paid').setAttribute('value', '???');
241                 return;
242             }
243             var to = util.money.dollars_float_to_cents_integer( bill.transaction.total_owed() );
244             var tp = util.money.dollars_float_to_cents_integer( bill.transaction.total_paid() );
245             var bo = util.money.dollars_float_to_cents_integer( bill.transaction.balance_owed() );
246             selected_billed += to;
247             selected_paid += tp;
248             selected_balance += bo;
249         }
250         //$('checked_billed').setAttribute('value', util.money.cents_as_dollars( selected_billed ) );
251         //$('checked_paid').setAttribute('value', util.money.cents_as_dollars( selected_paid ) );
252         //$('checked_owed').setAttribute('value', util.money.cents_as_dollars( selected_balance ) );
253     } catch(E) {
254         alert('Error in bill2.js, tally_selected(): ' + E);
255     }
256 }
257
258 function tally_voided() {
259     try {
260         JSAN.use('util.money');
261         var voided_total = 0;
262
263         g.data.stash_retrieve();
264
265         for (var i = 0; i < g.data.voided_billings.length; i++) {
266             var billing = g.data.voided_billings[i];
267             var bv = util.money.dollars_float_to_cents_integer( billing.amount() );
268             voided_total += bv;
269         }
270         $('currently_voided').setAttribute('value', util.money.cents_as_dollars( voided_total ) );
271     } catch(E) {
272         alert('Error in bill2.js, tally_voided(): ' + E);
273     }
274 }
275
276 function tally_all() {
277     try {
278         JSAN.use('util.money');
279         var checked_billed = 0;
280         var checked_paid = 0;
281         var checked_balance = 0;
282         var total_billed = 0;
283         var total_paid = 0;
284         var total_balance = 0;
285         var refunds_owed = 0;
286
287         var retrieve_ids = g.bill_list.dump_retrieve_ids();
288         for (var i = 0; i < retrieve_ids.length; i++) {
289             var bill = g.bill_map[retrieve_ids[i]];
290             if (!bill) {
291                 $('checked_owed').value = '???';
292                 $('checked_owed2').setAttribute('value', '???');
293                 $('checked_billed').value = '???';
294                 $('checked_paid').value = '???';
295                 $('tb_total_owed').value = '???';
296                 $('total_owed2').setAttribute('value', '???');
297                 $('total_billed').value = '???';
298                 $('tb_total_paid').value = '???';
299                 $('refunds_owed').setAttribute('value', '???');
300                 return;
301             }
302             var to = util.money.dollars_float_to_cents_integer( bill.transaction.total_owed() );
303             var tp = util.money.dollars_float_to_cents_integer( bill.transaction.total_paid() );
304             var bo = util.money.dollars_float_to_cents_integer( bill.transaction.balance_owed() );
305             total_billed += to;
306             total_paid += tp;
307             total_balance += bo;
308             if ( bo < 0 ) refunds_owed += bo;
309             if (g.check_map[retrieve_ids[i]]) {
310                 checked_billed += to;
311                 checked_paid += tp;
312                 checked_balance += bo;
313             }
314         }
315         $('checked_billed').value = util.money.cents_as_dollars( checked_billed );
316         $('checked_paid').value = util.money.cents_as_dollars( checked_paid );
317         $('checked_owed').value = util.money.cents_as_dollars( checked_balance );
318         $('checked_owed2').setAttribute('value', util.money.cents_as_dollars( checked_balance ) );
319         $('total_billed').value = util.money.cents_as_dollars( total_billed );
320         $('tb_total_paid').value = util.money.cents_as_dollars( total_paid );
321         $('tb_total_owed').value = util.money.cents_as_dollars( total_balance );
322         $('total_owed2').setAttribute('value', util.money.cents_as_dollars( total_balance ) );
323         $('refunds_owed').setAttribute('value', util.money.cents_as_dollars( Math.abs( refunds_owed ) ) );
324         // tally_selected();
325     } catch(E) {
326         alert('Error in bill2.js, tally_all(): ' + E);
327     }
328 }
329
330 function handle_refund() {
331     if(g.bill_list_selection.length > 1) {
332         var msg = $("patronStrings").getFormattedString('staff.patron.bills.handle_refund.message_plural', [g.bill_list_selection]);
333     } else {
334         var msg = $("patronStrings").getFormattedString('staff.patron.bills.handle_refund.message_singular', [g.bill_list_selection]);
335     }
336         
337     var r = g.error.yns_alert(msg,
338         $("patronStrings").getString('staff.patron.bills.handle_refund.title'),
339         $("patronStrings").getString('staff.patron.bills.handle_refund.btn_yes'),
340         $("patronStrings").getString('staff.patron.bills.handle_refund.btn_no'),null,
341         $("patronStrings").getString('staff.patron.bills.handle_refund.confirm_message'));
342     if (r == 0) {
343         for (var i = 0; i < g.bill_list_selection.length; i++) {
344             var bill_id = g.bill_list_selection[i];
345             //alert('g.check_map['+bill_id+'] = '+g.check_map[bill_id]+' bill_map['+bill_id+'] = ' + js2JSON(g.bill_map[bill_id]));
346             g.check_map[bill_id] = true;
347             var row_params = g.row_map[bill_id];
348             row_params.row.my.checked = true;
349             g.bill_list.refresh_row(row_params);
350         }
351     }
352     tally_all();
353     distribute_payment();
354 }
355
356
357 function check_all() {
358     try {
359         for (var i in g.bill_map) {
360             g.check_map[i] = true;
361             var row_params = g.row_map[i];
362             row_params.row.my.checked = true;
363             g.bill_list.refresh_row(row_params);
364         }
365         tally_all();
366         distribute_payment();
367     } catch(E) {
368         alert('Error in bill2.js, check_all(): ' + E);
369     }
370
371 }
372
373 function uncheck_all() {
374     try {
375         for (var i in g.bill_map) {
376             g.check_map[i] = false;
377             var row_params = g.row_map[i];
378             row_params.row.my.checked = false;
379             g.bill_list.refresh_row(row_params);
380         }
381         tally_all();
382         distribute_payment();
383     } catch(E) {
384         alert('Error in bill2.js, check_all(): ' + E);
385     }
386
387 }
388
389 function check_all_refunds() {
390     try {
391         for (var i in g.bill_map) {
392             if ( Number( g.bill_map[i].transaction.balance_owed() ) < 0 ) {
393                 g.check_map[i] = true;
394                 var row_params = g.row_map[i];
395                 row_params.row.my.checked = true;
396                 g.bill_list.refresh_row(row_params);
397             }
398         }
399         tally_all();
400         distribute_payment();
401     } catch(E) {
402         alert('Error in bill2.js, check_all_refunds(): ' + E);
403     }
404 }
405
406 function gen_list_append_func(r) {
407     return function() {
408         if (typeof r == 'object') { g.row_map[ r.id() ] = g.bill_list.append( { 'retrieve_id' : r.id(), 'flesh_immediately' : true, 'row' : { 'my' : { 'checked' : true, 'mbts' : r } } } );
409         } else { g.row_map[r] = g.bill_list.append( { 'retrieve_id' : r, 'flesh_immediately' : true, 'row' : { 'my' : { 'checked' : true } } } ); }
410     }
411 }
412
413 function retrieve_mbts_for_list() {
414     var method = 'FM_MBTS_IDS_RETRIEVE_ALL_HAVING_BALANCE.authoritative';
415     g.mbts_ids = g.network.simple_request(method,[ses(),g.patron_id]);
416     if (g.mbts_ids.ilsevent) {
417         switch(Number(g.mbts_ids.ilsevent)) {
418             case -1: g.error.standard_network_error_alert($("patronStrings").getString('staff.patron.bill_history.retrieve_mbts_for_list.close_win_try_again')); break;
419             default: g.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.bill_history.retrieve_mbts_for_list.close_win_try_again'),g.mbts_ids); break;
420         }
421     } else if (g.mbts_ids == null) {
422         g.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.bill_history.retrieve_mbts_for_list.close_win_try_again'),null);
423     } else {
424    
425         g.mbts_ids.reverse();
426  
427         for (var i = 0; i < g.mbts_ids.length; i++) {
428             dump('i = ' + i + ' g.mbts_ids[i] = ' + g.mbts_ids[i] + '\n');
429             g.funcs.push( gen_list_append_func(g.mbts_ids[i]) );
430         }
431     }
432 }
433
434 function init_lists() {
435     JSAN.use('util.list'); JSAN.use('circ.util'); 
436
437     g.bill_list_selection = [];
438
439     g.bill_list = new util.list('bill_tree');
440
441     g.bill_list.init( {
442         'columns' : 
443             [
444                 {
445                     'id' : 'select', 'primary' : true, 'type' : 'checkbox', 'editable' : true, 'label' : '', 'style' : 'min-width: 3em;',
446                     'render' : function(my) { return String( my.checked ) == 'true'; }, 
447                 }
448             ].concat(
449                 patron.util.mbts_columns({
450                     'mbts_xact_finish' : { 'hidden' : xul_param('current') ? true : false }
451                 }
452             ).concat( 
453                 circ.util.columns({ 
454                     'title' : { 'hidden' : false, 'flex' : '3' }
455                 }
456             ).concat( 
457                 [
458                     {
459                         'id' : 'payment_pending', 'editable' : false, 'sort_type' : 'money', 
460                         'label' : $('patronStrings').getString('staff.patron.bill_interface.payment_pending.column_header'),
461                         'render' : function(my) { return my.payment_pending || '0.00'; }, 
462                     }
463                 ]
464             ))),
465         'map_row_to_columns' : patron.util.std_map_row_to_columns(' '),
466         'on_select' : function(ev) {
467             JSAN.use('util.functional');
468             g.bill_list_selection = util.functional.map_list(
469                 g.bill_list.retrieve_selection(),
470                 function(o) { return o.getAttribute('retrieve_id'); }
471             );
472             //tally_selected();
473             $('details').setAttribute('disabled', g.bill_list_selection.length == 0);
474             $('add').setAttribute('disabled', g.bill_list_selection.length == 0);
475             $('voidall').setAttribute('disabled', g.bill_list_selection.length == 0);
476             $('refund').setAttribute('disabled', g.bill_list_selection.length == 0);
477             $('opac').setAttribute('disabled', g.bill_list_selection.length == 0);
478             $('copy_details').setAttribute('disabled', g.bill_list_selection.length == 0);
479         },
480         'on_click' : function(ev) {
481             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserRead');
482             var row = {}; var col = {}; var nobj = {};
483             g.bill_list.node.treeBoxObject.getCellAt(ev.clientX,ev.clientY,row,col,nobj);
484             if (row.value == -1) return;
485             var treeItem = g.bill_list.node.contentView.getItemAtIndex(row.value);
486             if (treeItem.nodeName != 'treeitem') return;
487             var treeRow = treeItem.firstChild;
488             var treeCell = treeRow.firstChild;
489             if (g.check_map[ treeItem.getAttribute('retrieve_id') ] != (treeCell.getAttribute('value') == 'true')) {
490                 g.check_map[ treeItem.getAttribute('retrieve_id') ] = treeCell.getAttribute('value') == 'true';
491                 g.row_map[ treeItem.getAttribute('retrieve_id') ].row.my.checked = treeCell.getAttribute('value') == 'true';
492                 tally_all();
493                 distribute_payment();
494             }
495         },
496         'on_sort' : function() {
497             tally_all();
498         },
499         'on_checkbox_toggle' : function(toggle) {
500             try {
501                 var retrieve_ids = g.bill_list.dump_retrieve_ids();
502                 for (var i = 0; i < retrieve_ids.length; i++) {
503                     g.check_map[ retrieve_ids[i] ] = (toggle=='on');
504                     g.row_map[ retrieve_ids[i] ].row.my.checked = (toggle=='on');
505                 }
506                 tally_all();
507             } catch(E) {
508                 alert('error in on_checkbox_toggle(): ' + E);
509             }
510         },
511         'retrieve_row' : function(params) {
512             try {
513                 var id = params.retrieve_id;
514                 var row = params.row;
515
516                 function handle_props(row) {
517                     try {
518                         if ( row && row.my && row.my.mbts && Number( row.my.mbts.balance_owed() ) < 0 ) {
519                             util.widgets.addProperty(params.row_node.firstChild,'refundable');
520                             util.widgets.addProperty(params.row_node.firstChild.childNodes[ g.payment_pending_column_idx ],'refundable');
521                         }
522                         if ( row && row.my && row.my.circ && ! row.my.circ.checkin_time() ) {
523                             $('circulating_hint').hidden = false;
524                             util.widgets.addProperty(params.row_node.firstChild,'circulating');
525                             util.widgets.addProperty(params.row_node.firstChild.childNodes[ g.title_column_idx ],'circulating');
526                         }
527                     } catch(E) {
528                         g.error.sdump('D_WARN','Error setting list properties in bill2.js: ' + E);
529                         alert('Error setting list properties in bill2.js: ' + E);
530                     }
531                 }
532
533                 if (id) {
534                     if (typeof row.my == 'undefined') row.my = {};
535                     if (typeof row.my.mbts == 'undefined' ) {
536                         g.network.simple_request('BLOB_MBTS_DETAILS_RETRIEVE',[ses(),id], function(req) {
537                             var blob = req.getResultObject();
538                             row.my.mbts = blob.transaction;
539                             row.my.circ = blob.circ;
540                             row.my.acp = blob.copy;
541                             row.my.mvr = blob.record;
542                             if (typeof params.on_retrieve == 'function') {
543                                 if ( row.my.mbts && Number( row.my.mbts.balance_owed() ) < 0 ) {
544                                     row.my.checked = false;
545                                 }
546                                 handle_props(row);
547                                 params.on_retrieve(row);
548                             };
549                             g.bill_map[ id ] = blob;
550                             g.check_map[ id ] = row.my.checked;
551                             tally_all();
552                         } );
553                     } else {
554                         if (typeof params.on_retrieve == 'function') { 
555                             handle_props(row);
556                             params.on_retrieve(row); 
557                         }
558                     }
559                 } else {
560                     if (typeof params.on_retrieve == 'function') { 
561                         params.on_retrieve(row); 
562                     }
563                 }
564
565                 return row;
566             } catch(E) {
567                 alert('Error in bill2.js, retrieve_row(): ' + E);
568             }
569         }
570     } );
571
572     g.title_column_idx = util.functional.map_list( g.bill_list.columns, function(o) { return o.id; } ).indexOf( 'title' );
573     g.payment_pending_column_idx = util.functional.map_list( g.bill_list.columns, function(o) { return o.id; } ).indexOf( 'payment_pending' );
574     $('bill_list_actions').appendChild( g.bill_list.render_list_actions() );
575     g.bill_list.set_list_actions();
576 }
577
578 function handle_add() {
579     if(g.bill_list_selection.length > 1) {
580         var msg = $("patronStrings").getFormattedString('staff.patron.bill_history.handle_add.message_plural', [g.bill_list_selection]);
581     } else {
582         var msg = $("patronStrings").getFormattedString('staff.patron.bill_history.handle_add.message_singular', [g.bill_list_selection]);
583     }
584         
585     var r = g.error.yns_alert(msg,
586         $("patronStrings").getString('staff.patron.bill_history.handle_add.title'),
587         $("patronStrings").getString('staff.patron.bill_history.handle_add.btn_yes'),
588         $("patronStrings").getString('staff.patron.bill_history.handle_add.btn_no'),null,
589         $("patronStrings").getString('staff.patron.bill_history.handle_add.confirm_message'));
590     if (r == 0) {
591         JSAN.use('util.window');
592         var win = new util.window();
593         for (var i = 0; i < g.bill_list_selection.length; i++) {
594             var w = win.open(
595                 urls.XUL_PATRON_BILL_WIZARD,
596                 'billwizard',
597                 'chrome,resizable,modal',
598                 { 'patron_id' : g.patron_id, 'xact_id' : g.bill_list_selection[i] }
599             );
600         }
601         refresh();
602         if (typeof window.xulG == 'object' && typeof window.xulG.refresh == 'function') window.xulG.refresh();
603     }
604 }
605
606 function handle_void_all() {
607     if(g.bill_list_selection.length > 1) {
608         var msg = $("patronStrings").getFormattedString('staff.patron.bill_history.handle_void.message_plural', [g.bill_list_selection]);
609     } else {
610         var msg = $("patronStrings").getFormattedString('staff.patron.bill_history.handle_void.message_singular', [g.bill_list_selection]);
611     }
612         
613     var r = g.error.yns_alert(msg,
614         $("patronStrings").getString('staff.patron.bill_history.handle_void.title'),
615         $("patronStrings").getString('staff.patron.bill_history.handle_void.btn_yes'),
616         $("patronStrings").getString('staff.patron.bill_history.handle_void.btn_no'),null,
617         $("patronStrings").getString('staff.patron.bill_history.handle_void.confirm_message'));
618     if (r == 0) {
619         for (var i = 0; i < g.bill_list_selection.length; i++) {
620             void_all_billings( g.bill_list_selection[i] );
621         }
622         refresh();
623         if (typeof window.xulG == 'object' && typeof window.xulG.refresh == 'function') window.xulG.refresh();
624         if (typeof window.xulG == 'object' && typeof window.xulG.on_money_change == 'function') window.xulG.on_money_change();
625     }
626 }
627
628 function handle_opac() {
629     try {
630         var ids = [];
631         for (var i = 0; i < g.bill_list_selection.length; i++) {
632             var my_mvr = g.bill_map[ g.bill_list_selection[i] ].record;
633             var my_acp = g.bill_map[ g.bill_list_selection[i] ].copy;
634             if (typeof my_mvr != 'undefined' && my_mvr != null) {
635                 ids.push( { 'barcode' : my_acp.barcode(), 'doc_id' : my_mvr.doc_id() } );
636             }
637         }
638         JSAN.use('cat.util');
639         cat.util.show_in_opac( ids );
640     } catch(E) {
641         alert('Error in bill2.js, handle_opac: ' + E);
642     }
643 }
644
645 function handle_copy_details() {
646     try {
647         var ids = [];
648         for (var i = 0; i < g.bill_list_selection.length; i++) {
649             var my_acp = g.bill_map[ g.bill_list_selection[i] ].copy;
650             if (typeof my_acp != 'undefined' && my_acp != null) {
651                 ids.push( my_acp.barcode() );
652             }
653         }
654         JSAN.use('circ.util');
655         circ.util.item_details_new( ids );
656     } catch(E) {
657         alert('Error in bill2.js, handle_opac: ' + E);
658     }
659 }
660
661 function handle_details() {
662     JSAN.use('util.window'); var win = new util.window();
663     for (var i = 0; i < g.bill_list_selection.length; i++) {
664         var my_xulG = win.open(
665             urls.XUL_PATRON_BILL_DETAILS,
666             'test_billdetails_' + g.bill_list_selection[i],
667             'chrome,resizable',
668             {
669                 'patron_id' : g.patron_id,
670                 'mbts_id' : g.bill_list_selection[i],
671                 'refresh' : function() {
672                     refresh(); 
673                     if (typeof window.xulG == 'object' && typeof window.xulG.refresh == 'function') window.xulG.refresh();
674                 }, 
675                 'new_tab' : xulG.new_tab,
676                 'url_prefix' : xulG.url_prefix
677             }
678         );
679     }
680 }
681
682 function print_bills() {
683     try {
684         var template = 'bills_historical'; if (xul_param('current')) template = 'bills_current';
685         JSAN.use('patron.util');
686         g.patron = patron.util.retrieve_fleshed_au_via_id(ses(),g.patron_id,null); 
687         var params = { 
688             'patron' : g.patron,
689             'printer_context' : 'receipt',
690             'template' : template
691         };
692         g.bill_list.print(params);
693     } catch(E) {
694         g.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.bill_history.print_bills.print_error'), E);
695     }
696 }
697
698 function distribute_payment() {
699     try {
700         JSAN.use('util.money');
701         var tb = $('payment');
702         tb.value = util.money.cents_as_dollars( util.money.dollars_float_to_cents_integer( tb.value ) );
703         tb.setAttribute('value', tb.value );
704         var total = util.money.dollars_float_to_cents_integer( tb.value );
705         if (total < 0) { tb.value = '0.00'; tb.setAttribute('value','0.00'); total = 0; }
706         var retrieve_ids = g.bill_list.dump_retrieve_ids();
707         for (var i = 0; i < retrieve_ids.length; i++) {
708             var row_params = g.row_map[retrieve_ids[i]];
709             if (g.check_map[retrieve_ids[i]]) { 
710                 var bill = g.bill_map[retrieve_ids[i]].transaction;
711                 var bo = util.money.dollars_float_to_cents_integer( bill.balance_owed() );
712                 if ( bo > total ) {
713                     row_params.row.my.payment_pending = util.money.cents_as_dollars( total );
714                     total = 0;
715                 } else {
716                     row_params.row.my.payment_pending = util.money.cents_as_dollars( bo );
717                     total = total - bo;
718                 }
719             } else {
720                 row_params.row.my.payment_pending = '0.00';
721             }
722             g.bill_list.refresh_row(row_params);
723         }
724         tally_pending();
725     } catch(E) {
726         alert('Error in bill2.js, distribute_payment(): ' + E);
727     }
728 }
729
730 function apply_payment() {
731     try {
732         var payment_blob = {};
733         JSAN.use('util.window');
734         var win = new util.window();
735         switch($('payment_type').value) {
736             case 'credit_card_payment' :
737                 g.data.temp = '';
738                 g.data.stash('temp');
739                 var my_xulG = win.open(
740                     urls.XUL_PATRON_BILL_CC_INFO,
741                     'billccinfo',
742                     'chrome,resizable,modal',
743                     {'patron_id': g.patron_id}
744                 );
745                 g.data.stash_retrieve();
746                 payment_blob = JSON2js( g.data.temp ); // FIXME - replace with my_xulG and update_modal_xulG, though it looks like we were using that before and moved away from it
747             break;
748             case 'check_payment' :
749                 g.data.temp = '';
750                 g.data.stash('temp');
751                 var my_xulG = win.open(
752                     urls.XUL_PATRON_BILL_CHECK_INFO,
753                     'billcheckinfo',
754                     'chrome,resizable,modal'
755                 );
756                 g.data.stash_retrieve();
757                 payment_blob = JSON2js( g.data.temp );
758             break;
759         }
760         if (
761             (typeof payment_blob == 'undefined') || 
762             payment_blob=='' || 
763             payment_blob.cancelled=='true'
764         ) { 
765             alert( $('commonStrings').getString('common.cancelled') ); 
766             return; 
767         }
768         payment_blob.userid = g.patron_id;
769         payment_blob.note = payment_blob.note || '';
770         //payment_blob.cash_drawer = 1; // FIXME: get new Config() to work
771         payment_blob.payment_type = $('payment_type').value;
772         var tally_blob = tally_pending();
773         payment_blob.payments = tally_blob.payments;
774         // Handle patron credit
775         if ( payment_blob.payment_type == 'credit_payment' ) { // paying with patron credit
776             if ( $('convert_change_to_credit').checked ) {
777                 // No need to convert credit into credit, handled automatically
778                 payment_blob.patron_credit = '0.00';
779             } else {
780                 // Cashing out extra credit as change
781                 payment_blob.patron_credit = 0 - tally_blob.change;
782             }
783         } else if ( $('convert_change_to_credit').checked ) {
784             // Saving change from a non-credit payment as patron credit on server
785             payment_blob.patron_credit = tally_blob.change;
786         } else {
787             payment_blob.patron_credit = '0.00';
788         }
789         if ( payment_blob.payments.length == 0 && payment_blob.patron_credit == '0.00' ) {
790             alert($("patronStrings").getString('staff.patron.bills.apply_payment.nothing_applied'));
791             return;
792         }
793         if ( pay( payment_blob ) ) {
794
795             $('payment').value = ''; $('payment').select(); $('payment').focus();
796             refresh({'clear_voided_summary':true});
797             if (typeof window.xulG == 'object' && typeof window.xulG.refresh == 'function') window.xulG.refresh();
798             if (typeof window.xulG == 'object' && typeof window.xulG.on_money_change == 'function') window.xulG.on_money_change();
799             if ( $('payment_type').value == 'credit_payment' || $('convert_change_to_credit').checked ) {
800                 refresh_patron();
801             }
802             try {
803                 if ( ! $('receipt_upon_payment').hasAttribute('checked') ) { return; } // Skip print attempt
804                 if ( ! $('receipt_upon_payment').getAttribute('checked') ) { return; } // Skip print attempt
805                 var no_print_prompting = g.data.hash.aous['circ.staff_client.do_not_auto_attempt_print'];
806                 if (no_print_prompting) {
807                     if (no_print_prompting.indexOf( "Bill Pay" ) > -1) { return; } // Skip print attempt
808                 }
809                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
810                 g.data.stash_retrieve();
811                 var template = 'bill_payment';
812                 JSAN.use('patron.util'); JSAN.use('util.functional');
813                 var params = { 
814                     'patron' : g.patron,
815                     'lib' : g.data.hash.aou[ ses('ws_ou') ],
816                     'staff' : ses('staff'),
817                     'header' : g.data.print_list_templates[template].header,
818                     'line_item' : g.data.print_list_templates[template].line_item,
819                     'footer' : g.data.print_list_templates[template].footer,
820                     'type' : g.data.print_list_templates[template].type,
821                     'list' : util.functional.map_list(
822                         payment_blob.payments,
823                         function(o) {
824                             return {
825                                 'bill_id' : o[0],
826                                 'payment' : o[1],
827                                 'last_billing_type' : g.bill_map[ o[0] ].transaction.last_billing_type(),
828                                 'last_billing_note' : g.bill_map[ o[0] ].transaction.last_billing_note(),
829                                 'title' : typeof g.bill_map[ o[0] ].record != 'undefined' ? g.bill_map[ o[0] ].record.title() : '', 
830                                 'barcode' : typeof g.bill_map[ o[0] ].copy != 'undefined' ? g.bill_map[ o[0] ].copy.barcode() : ''
831                             };
832                         }
833                     ),
834                     'data' : g.previous_summary
835                 };
836                 g.error.sdump('D_DEBUG',js2JSON(params));
837                 if ($('printer_prompt').hasAttribute('checked')) {
838                     if ($('printer_prompt').getAttribute('checked')) {
839                             params.no_prompt = false;
840                     } else {
841                             params.no_prompt = true;
842                     }
843                 } else {
844                     params.no_prompt = true;
845                 }
846                 JSAN.use('util.print'); var print = new util.print('receipt');
847                 for (var i = 0; i < $('num_of_receipts').value; i++) {
848                     print.tree_list( params );
849                 }
850             } catch(E) {
851                 g.error.standard_unexpected_error_alert('bill receipt', E);
852             }
853         }
854     } catch(E) {
855         alert('Error in bill2.js, apply_payment(): ' + E);
856     }
857 }
858
859 function pay(payment_blob) {
860     try {
861         var x = $('annotate_payment');
862         if (x && x.checked && (! payment_blob.note)) {
863             payment_blob.note = window.prompt(
864                 $("patronStrings").getString('staff.patron.bills.pay.annotate_payment'),
865                 '', 
866                 $("patronStrings").getString('staff.patron.bills.pay.annotate_payment.title')
867             );
868         }
869         g.previous_summary = {
870             original_balance : $('tb_total_owed').value,
871             voided_balance : $('currently_voided').value,
872             payment_received : $('payment').value,
873             payment_applied : $('pending_payment').value,
874             change_given : $('convert_change_to_credit').checked ? 0 : $('pending_change').value,
875             credit_given : $('convert_change_to_credit').checked ? $('pending_change').value : 0,
876             new_balance : util.money.cents_as_dollars( 
877                 util.money.dollars_float_to_cents_integer( $('tb_total_owed').value ) - 
878                 util.money.dollars_float_to_cents_integer( $('pending_payment').value )
879             ),
880             payment_type : $('payment_type').getAttribute('label'),
881             note : payment_blob.note
882         }
883         var robj = g.network.simple_request( 'BILL_PAY', [ ses(), payment_blob, g.patron.last_xact_id() ]);
884         if (typeof robj.ilsevent != 'undefined') {
885             switch(robj.textcode) {
886                 case 'SUCCESS' : return true; break;
887                 case 'REFUND_EXCEEDS_DESK_PAYMENTS' : alert($("patronStrings").getFormattedString('staff.patron.bills.pay.refund_exceeds_desk_payment', [robj.desc])); return false; break;
888                 case 'INVALID_USER_XACT_ID' :
889                     refresh(); default_focus();
890                     alert($("patronStrings").getFormattedString('staff.patron.bills.pay.invalid_user_xact_id', [robj.desc])); return false; break;
891                 default: throw(robj); break;
892             }
893         }
894         return true;
895     } catch(E) {
896         g.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.bills.pay.payment_failed'),E);
897         return false;
898     }
899 }
900
901 function refresh(params) {
902     try {
903         if (params && params.clear_voided_summary) {
904             g.data.voided_billings = []; g.data.stash('voided_billings');
905         }
906         refresh_patron();
907         g.bill_list.clear();
908         retrieve_mbts_for_list();
909         tally_voided();
910         distribute_payment(); 
911     } catch(E) {
912         alert('Error in bill2.js, refresh(): ' + E);
913     }
914 }
915
916 function void_all_billings(mobts_id) {
917     try {
918         JSAN.use('util.functional');
919         
920         var mb_list = g.network.simple_request( 'FM_MB_RETRIEVE_VIA_MBTS_ID.authoritative', [ ses(), mobts_id ] );
921         if (typeof mb_list.ilsevent != 'undefined') throw(mb_list);
922
923         mb_list = util.functional.filter_list( mb_list, function(o) { return ! get_bool( o.voided() ) });
924
925         if (mb_list.length == 0) { alert($("patronStrings").getString('staff.patron.bills.void_all_billings.all_voided')); return; }
926
927         var sum = 0;
928         for (var i = 0; i < mb_list.length; i++) sum += util.money.dollars_float_to_cents_integer( mb_list[i].amount() );
929         sum = util.money.cents_as_dollars( sum );
930
931         var msg = $("patronStrings").getFormattedString('staff.patron.bills.void_all_billings.void.message', [sum]);
932         var r = g.error.yns_alert(msg,
933             $("patronStrings").getString('staff.patron.bills.void_all_billings.void.title'),
934             $("patronStrings").getString('staff.patron.bills.void_all_billings.void.yes'),
935             $("patronStrings").getString('staff.patron.bills.void_all_billings.void.no'), null,
936             $("patronStrings").getString('staff.patron.bills.void_all_billings.void.confirm_message'));
937         if (r == 0) {
938             var robj = g.network.simple_request('FM_MB_VOID',[ses()].concat(util.functional.map_list(mb_list,function(o){return o.id();})));
939             if (robj.ilsevent) {
940                 switch(Number(robj.ilsevent)) {
941                     case 5000 /* PERM_FAILURE */:
942                         return;
943                     break;
944                     default: 
945                         g.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.bills.void_all_billings.error_voiding_bills'),robj); 
946                         return; 
947                     break;
948                 }
949             }
950
951             g.data.stash_retrieve(); if (! g.data.voided_billings ) g.data.voided_billings = []; 
952             for (var i = 0; i < mb_list.length; i++) {
953                     g.data.voided_billings.push( mb_list[i] );
954             }
955             g.data.stash('voided_billings');
956         }
957     } catch(E) {
958         try { g.error.standard_unexpected_error_alert('bill2.js, void_all_billings():',E); } catch(F) { alert(E); }
959     }
960 }
961
962 function refresh_patron() {
963     JSAN.use('patron.util'); JSAN.use('util.money');
964     patron.util.retrieve_fleshed_au_via_id(ses(),g.patron_id,null,function(req) {
965         var au_obj = req.getResultObject();
966         if (typeof au_obj.ilsevent == 'undefined') {
967             g.patron = au_obj;
968             $('credit_forward').setAttribute('value',util.money.sanitize( g.patron.credit_forward_balance() ));
969         }
970     });
971 }