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