]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/bill2.js
merge from my offline repo. Toward a billing interface replacement. Other things...
[working/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('patron.util');
13         JSAN.use('OpenILS.data'); g.data = new OpenILS.data(); g.data.init({'via':'stash'});
14         //g.data.temp = ''; g.data.stash('temp');
15
16         g.error.sdump('D_TRACE','my_init() for bill2.xul');
17
18         if (xul_param('current')) {
19             $('caption').setAttribute('label',$("patronStrings").getString('staff.patron.bill_history.my_init.current_bills'));
20             document.title = $("patronStrings").getString('staff.patron.bill_history.my_init.current_bills');
21         } else {
22             $('caption').setAttribute('label',$("patronStrings").getString('staff.patron.bill_history.my_init.bill_history'));
23             document.title = $("patronStrings").getString('staff.patron.bill_history.my_init.bill_history');
24         }
25
26         g.funcs = []; g.bill_map = {}; g.row_map = {}; g.check_map = {};
27
28         g.patron_id = xul_param('patron_id');
29
30         init_lists();
31
32         retrieve_mbts_for_list();
33
34         event_listeners();
35
36         JSAN.use('util.exec'); var exec = new util.exec(20); 
37         exec.on_error = function(E) { alert(E); return true; }
38         exec.timer(g.funcs,100);
39
40         $('credit_forward').setAttribute('value','???');
41         if (!g.patron) {
42             g.network.simple_request(
43                 'FM_AU_FLESHED_RETRIEVE_VIA_ID.authoritative',
44                 [ ses(), g.patron_id ],
45                 function(req) {
46                     try {
47                         g.patron = req.getResultObject();
48                         if (typeof g.patron.ilsevent != 'undefined') throw(g.patron);
49                         $('credit_forward').setAttribute('value','$' + util.money.sanitize( g.patron.credit_forward_balance() ));
50                     } catch(E) {
51                         alert('Error in bill2.js, retrieve patron callback: ' + E);
52                     }
53                 }
54             );
55         } else {
56             $('credit_forward').setAttribute('value','$' + util.money.sanitize( g.patron.credit_forward_balance() ));
57         }
58
59         default_focus();
60
61     } catch(E) {
62         var err_msg = $("commonStrings").getFormattedString('common.exception', ['patron/bill2.xul', E]);
63         try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); }
64         alert(err_msg);
65     }
66 }
67
68 function event_listeners() {
69     try {
70         $('details').addEventListener(
71             'command',
72             handle_details,
73             false
74         );
75
76         $('add').addEventListener(
77             'command',
78             handle_add,
79             false
80         );
81
82         $('payment').addEventListener(
83             'change',
84             function(ev) { distribute_payment(); },
85             false
86         );
87
88         $('payment').addEventListener(
89             'keypress',
90             function(ev) {
91                 if (! (ev.keyCode == 13 /* enter */ || ev.keyCode == 77 /* mac enter */) ) { return; }
92                 distribute_payment();
93                 $('apply_payment_btn').focus();
94             },
95             false
96         );
97
98         $('bill_patron_btn').addEventListener(
99             'command',
100             function() {
101                 JSAN.use('util.window'); var win = new util.window();
102                 var my_xulG = win.open(
103                     urls.XUL_PATRON_BILL_WIZARD,
104                     'billwizard',
105                     'chrome,resizable,modal',
106                     { 'patron_id' : g.patron_id }
107                 );
108                 if (my_xulG.xact_id) { g.funcs.push( gen_list_append_func( my_xulG.xact_id ) ); /* FIXME: do something to update summary sidebar */ }
109             },
110             false
111         );
112
113         $('convert_change_to_credit').addEventListener(
114             'command',
115             function(ev) {
116                 if (ev.target.checked) {
117                     addCSSClass( $('change_due'), 'change_to_credit' );
118                 } else {
119                     removeCSSClass( $('change_due'), 'change_to_credit' );
120                 }
121             },
122             false
123         );
124
125     } catch(E) {
126         alert('Error in bill2.js, event_listeners(): ' + E);
127     }
128 }
129
130 function $(id) { return document.getElementById(id); }
131
132 function default_focus() {
133     try { $('payment').focus(); } catch(E) { alert('Error in default_focus(): ' + E); }
134 }
135
136 function tally_pending() {
137     try {
138         var payments = [];
139         JSAN.use('util.money');
140         var tb = $('payment');
141         var payment_tendered = util.money.dollars_float_to_cents_integer( tb.value );
142         var payment_pending = 0;
143         var retrieve_ids = g.bill_list.dump_retrieve_ids();
144         for (var i = 0; i < retrieve_ids.length; i++) {
145             var row_params = g.row_map[retrieve_ids[i]];
146             if (g.check_map[retrieve_ids[i]]) { 
147                 var value = util.money.dollars_float_to_cents_integer( row_params.row.my.payment_pending );
148                 payment_pending += value;
149                 if (value != '0.00') { payments.push( [ retrieve_ids[i], value ] ); }
150             }
151         }
152         var change_pending = payment_tendered - payment_pending;
153         $('pending_payment').value = util.money.cents_as_dollars( payment_pending );
154         $('pending_change').value = util.money.cents_as_dollars( change_pending );
155         $('change_due').value = util.money.cents_as_dollars( change_pending );
156         return { 'payments' : payments, 'change' : change_pending };
157     } catch(E) {
158         alert('Error in bill2.js, tally_pending(): ' + E);
159     }
160 }
161
162 function tally_selected() {
163     try {
164         JSAN.use('util.money');
165         var selected_billed = 0;
166         var selected_paid = 0;
167         var selected_balance = 0;
168
169         for (var i = 0; i < g.bill_list_selection.length; i++) {
170             var bill = g.bill_map[g.bill_list_selection[i]];
171             if (!bill) {
172                 //$('checked_owed').setAttribute('value', '???');
173                 //$('checked_billed').setAttribute('value', '???');
174                 //$('checked_paid').setAttribute('value', '???');
175                 return;
176             }
177             var to = util.money.dollars_float_to_cents_integer( bill.transaction.total_owed() );
178             var tp = util.money.dollars_float_to_cents_integer( bill.transaction.total_paid() );
179             var bo = util.money.dollars_float_to_cents_integer( bill.transaction.balance_owed() );
180             selected_billed += to;
181             selected_paid += tp;
182             selected_balance += bo;
183         }
184         //$('checked_billed').setAttribute('value', '$' + util.money.cents_as_dollars( selected_billed ) );
185         //$('checked_paid').setAttribute('value', '$' + util.money.cents_as_dollars( selected_paid ) );
186         //$('checked_owed').setAttribute('value', '$' + util.money.cents_as_dollars( selected_balance ) );
187     } catch(E) {
188         alert('Error in bill2.js, tally_selected(): ' + E);
189     }
190 }
191
192 function tally_all() {
193     try {
194         JSAN.use('util.money');
195         var checked_billed = 0;
196         var checked_paid = 0;
197         var checked_balance = 0;
198         var total_billed = 0;
199         var total_paid = 0;
200         var total_balance = 0;
201         var refunds_owed = 0;
202
203         var retrieve_ids = g.bill_list.dump_retrieve_ids();
204         for (var i = 0; i < retrieve_ids.length; i++) {
205             var bill = g.bill_map[retrieve_ids[i]];
206             if (!bill) {
207                 $('checked_owed').setAttribute('value', '???');
208                 $('checked_owed2').setAttribute('value', '???');
209                 $('checked_billed').setAttribute('value', '???');
210                 $('checked_paid').setAttribute('value', '???');
211                 $('total_owed').setAttribute('value', '???');
212                 $('total_owed2').setAttribute('value', '???');
213                 $('total_billed').setAttribute('value', '???');
214                 $('total_paid').setAttribute('value', '???');
215                 $('refunds_owed').setAttribute('value', '???');
216                 return;
217             }
218             var to = util.money.dollars_float_to_cents_integer( bill.transaction.total_owed() );
219             var tp = util.money.dollars_float_to_cents_integer( bill.transaction.total_paid() );
220             var bo = util.money.dollars_float_to_cents_integer( bill.transaction.balance_owed() );
221             total_billed += to;
222             total_paid += tp;
223             total_balance += bo;
224             if ( bo < 0 ) refunds_owed += bo;
225             if (g.check_map[retrieve_ids[i]]) {
226                 checked_billed += to;
227                 checked_paid += tp;
228                 checked_balance += bo;
229             }
230         }
231         $('checked_billed').setAttribute('value', '$' + util.money.cents_as_dollars( checked_billed ) );
232         $('checked_paid').setAttribute('value', '$' + util.money.cents_as_dollars( checked_paid ) );
233         $('checked_owed').setAttribute('value', '$' + util.money.cents_as_dollars( checked_balance ) );
234         $('checked_owed2').setAttribute('value', '$' + util.money.cents_as_dollars( checked_balance ) );
235         $('total_billed').setAttribute('value', '$' + util.money.cents_as_dollars( total_billed ) );
236         $('total_paid').setAttribute('value', '$' + util.money.cents_as_dollars( total_paid ) );
237         $('total_owed').setAttribute('value', '$' + util.money.cents_as_dollars( total_balance ) );
238         $('total_owed2').setAttribute('value', '$' + util.money.cents_as_dollars( total_balance ) );
239         $('refunds_owed').setAttribute('value', '$' + util.money.cents_as_dollars( Math.abs( refunds_owed ) ) );
240         // tally_selected();
241     } catch(E) {
242         alert('Error in bill2.js, tally_all(): ' + E);
243     }
244 }
245
246 function check_all() {
247     try {
248         for (var i in g.bill_map) {
249             g.check_map[i] = true;
250             var row_params = g.row_map[i];
251             row_params.row.my.checked = true;
252             g.bill_list.refresh_row(row_params);
253         }
254         distribute_payment();
255     } catch(E) {
256         alert('Error in bill2.js, check_all(): ' + E);
257     }
258
259 }
260
261 function uncheck_all() {
262     try {
263         for (var i in g.bill_map) {
264             g.check_map[i] = false;
265             var row_params = g.row_map[i];
266             row_params.row.my.checked = false;
267             g.bill_list.refresh_row(row_params);
268         }
269         distribute_payment();
270     } catch(E) {
271         alert('Error in bill2.js, check_all(): ' + E);
272     }
273
274 }
275
276 function check_all_refunds() {
277     try {
278         for (var i in g.bill_map) {
279             g.check_map[i] = true;
280             if ( Number( g.bill_map[i].transaction.balance_owed() ) < 0 ) {
281                 var row_params = g.row_map[i];
282                 row_params.row.my.checked = true;
283                 g.bill_list.refresh_row(row_params);
284             }
285         }
286         distribute_payment();
287     } catch(E) {
288         alert('Error in bill2.js, check_all_refunds(): ' + E);
289     }
290 }
291
292 function gen_list_append_func(r) {
293     return function() {
294         if (typeof r == 'object') { g.row_map[ r.id() ] = g.bill_list.append( { 'retrieve_id' : r.id(), 'row' : { 'my' : { 'checked' : true, 'mbts' : r } } } );
295         } else { g.row_map[r] = g.bill_list.append( { 'retrieve_id' : r, 'row' : { 'my' : { 'checked' : true } } } ); }
296     }
297 }
298
299 function retrieve_mbts_for_list() {
300     var method = 'FM_MBTS_IDS_RETRIEVE_ALL_HAVING_BALANCE.authoritative';
301     g.mbts_ids = g.network.simple_request(method,[ses(),g.patron_id]);
302     if (g.mbts_ids.ilsevent) {
303         switch(Number(g.mbts_ids.ilsevent)) {
304             case -1: g.error.standard_network_error_alert($("patronStrings").getString('staff.patron.bill_history.retrieve_mbts_for_list.close_win_try_again')); break;
305             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;
306         }
307     } else if (g.mbts_ids == null) {
308         g.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.bill_history.retrieve_mbts_for_list.close_win_try_again'),null);
309     } else {
310    
311         g.mbts_ids.reverse();
312  
313         for (var i = 0; i < g.mbts_ids.length; i++) {
314             dump('i = ' + i + ' g.mbts_ids[i] = ' + g.mbts_ids[i] + '\n');
315             g.funcs.push( gen_list_append_func(g.mbts_ids[i]) );
316         }
317     }
318 }
319
320 function init_lists() {
321     JSAN.use('util.list'); JSAN.use('circ.util'); 
322
323     g.bill_list_selection = [];
324
325     g.bill_list = new util.list('bill_tree');
326
327     g.bill_list.init( {
328         'columns' : 
329             [
330                 {
331                     'id' : 'select', 'type' : 'checkbox', 'editable' : true, 'label' : '', 'render' : function(my) { return String( my.checked ) == 'true'; }, 
332                 }
333             ].concat(
334                 patron.util.mbts_columns({
335                     'xact_finish' : { 'hidden' : xul_param('current') ? true : false }
336                 }
337             ).concat( 
338                 circ.util.columns({ 
339                     'title' : { 'hidden' : false, 'flex' : '3' }
340                 }
341             ).concat( 
342                 [
343                     {
344                         'id' : 'payment_pending', 'editable' : false, 'label' : 'Payment Pending', 'sort_type' : 'money', 'render' : function(my) { return my.payment_pending || '0.00'; }, 
345                     }
346                 ]
347             ))),
348         'map_row_to_columns' : patron.util.std_map_row_to_columns(' '),
349         'on_select' : function(ev) {
350             JSAN.use('util.functional');
351             g.bill_list_selection = util.functional.map_list(
352                 g.bill_list.retrieve_selection(),
353                 function(o) { return o.getAttribute('retrieve_id'); }
354             );
355             //tally_selected();
356             $('details').setAttribute('disabled', g.bill_list_selection.length == 0);
357             $('add').setAttribute('disabled', g.bill_list_selection.length == 0);
358             $('voidall').setAttribute('disabled', g.bill_list_selection.length == 0);
359         },
360         'on_click' : function(ev) {
361             netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect UniversalBrowserRead');
362             var row = {}; var col = {}; var nobj = {};
363             g.bill_list.node.treeBoxObject.getCellAt(ev.clientX,ev.clientY,row,col,nobj);
364             if (row.value == -1) return;
365             var treeItem = g.bill_list.node.contentView.getItemAtIndex(row.value);
366             if (treeItem.nodeName != 'treeitem') return;
367             var treeRow = treeItem.firstChild;
368             var treeCell = treeRow.firstChild;
369             if (g.check_map[ treeItem.getAttribute('retrieve_id') ] != (treeCell.getAttribute('value') == 'true')) {
370                 g.check_map[ treeItem.getAttribute('retrieve_id') ] = treeCell.getAttribute('value') == 'true';
371                 g.row_map[ treeItem.getAttribute('retrieve_id') ].row.my.checked = treeCell.getAttribute('value') == 'true';
372                 tally_all();
373                 distribute_payment();
374             }
375         },
376         'on_sort' : function() {
377             tally_all();
378         },
379         'on_checkbox_toggle' : function(toggle) {
380             try {
381                 var retrieve_ids = g.bill_list.dump_retrieve_ids();
382                 for (var i = 0; i < retrieve_ids.length; i++) {
383                     g.check_map[ retrieve_ids[i] ] = (toggle=='on');
384                     g.row_map[ retrieve_ids[i] ].row.my.checked = (toggle=='on');
385                 }
386                 tally_all();
387             } catch(E) {
388                 alert('error in on_checkbox_toggle(): ' + E);
389             }
390         },
391         'retrieve_row' : function(params) {
392             try {
393                 var id = params.retrieve_id;
394                 var row = params.row;
395                 if (id) {
396                     if (typeof row.my == 'undefined') row.my = {};
397                     if (typeof row.my.mbts == 'undefined' ) {
398                         g.network.simple_request('BLOB_MBTS_DETAILS_RETRIEVE',[ses(),id], function(req) {
399                             var blob = req.getResultObject();
400                             row.my.mbts = blob.transaction;
401                             row.my.circ = blob.circ;
402                             row.my.acp = blob.copy;
403                             row.my.mvr = blob.record;
404                             if (typeof params.on_retrieve == 'function') {
405                                 if ( Number( row.my.mbts.balance_owed() ) < 0 ) {
406                                     params.row_node.firstChild.setAttribute('properties','refundable');
407                                     row.my.checked = false;
408                                 }
409                                 params.on_retrieve(row);
410                             };
411                             g.bill_map[ id ] = blob;
412                             g.check_map[ id ] = row.my.checked;
413                             tally_all();
414                         } );
415                     } else {
416                         if (typeof params.on_retrieve == 'function') { params.on_retrieve(row); }
417                     }
418                 } else {
419                     if (typeof params.on_retrieve == 'function') { params.on_retrieve(row); }
420                 }
421                 return row;
422             } catch(E) {
423                 alert('Error in bill2.js, retrieve_row(): ' + E);
424             }
425         }
426     } );
427
428     $('bill_list_actions').appendChild( g.bill_list.render_list_actions() );
429     g.bill_list.set_list_actions();
430 }
431
432 function handle_add() {
433     if(g.bill_list_selection.length > 1)
434         var msg = $("patronStrings").getFormattedString('staff.patron.bill_history.handle_add.message_plural', [g.bill_list_selection]);
435     else
436         var msg = $("patronStrings").getFormattedString('staff.patron.bill_history.handle_add.message_singular', [g.bill_list_selection]);
437         
438     var r = g.error.yns_alert(msg,
439         $("patronStrings").getString('staff.patron.bill_history.handle_add.title'),
440         $("patronStrings").getString('staff.patron.bill_history.handle_add.btn_yes'),
441         $("patronStrings").getString('staff.patron.bill_history.handle_add.btn_no'),null,
442         $("patronStrings").getString('staff.patron.bill_history.handle_add.confirm_message'));
443     if (r == 0) {
444         JSAN.use('util.window');
445         var win = new util.window();
446         for (var i = 0; i < g.bill_list_selection.length; i++) {
447             var w = win.open(
448                 urls.XUL_PATRON_BILL_WIZARD,
449                 'billwizard',
450                 'chrome,resizable,modal',
451                 { 'patron_id' : g.patron_id, 'xact_id' : g.bill_list_selection[i] }
452             );
453         }
454         g.bill_list.clear();
455         retrieve_mbts_for_list();
456         if (typeof window.refresh == 'function') window.refresh();
457         if (typeof window.xulG == 'object' && typeof window.xulG.refresh == 'function') window.xulG.refresh();
458     }
459 }
460
461 function handle_details() {
462     JSAN.use('util.window'); var win = new util.window();
463     for (var i = 0; i < g.bill_list_selection.length; i++) {
464         var my_xulG = win.open(
465             urls.XUL_PATRON_BILL_DETAILS,
466             'test_billdetails_' + g.bill_list_selection[i],
467             'chrome,resizable',
468             {
469                 'patron_id' : g.patron_id,
470                 'mbts_id' : g.bill_list_selection[i],
471                 'refresh' : function() { 
472                     if (typeof window.refresh == 'function') window.refresh();
473                     if (typeof window.xulG == 'object' && typeof window.xulG.refresh == 'function') window.xulG.refresh();
474                 }, 
475             }
476         );
477     }
478 }
479
480 function print_bills() {
481     try {
482         var template = 'bills_historical'; if (xul_param('current')) template = 'bills_current';
483         JSAN.use('patron.util');
484         var params = { 
485             'patron' : patron.util.retrieve_au_via_id(ses(),g.patron_id), 
486             'template' : template
487         };
488         g.bill_list.print(params);
489     } catch(E) {
490         g.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.bill_history.print_bills.print_error'), E);
491     }
492 }
493
494 function distribute_payment() {
495     try {
496         JSAN.use('util.money');
497         var tb = $('payment');
498         tb.value = util.money.cents_as_dollars( util.money.dollars_float_to_cents_integer( tb.value ) );
499         tb.setAttribute('value', tb.value );
500         var total = util.money.dollars_float_to_cents_integer( tb.value );
501         if (total < 0) { tb.value = '0.00'; tb.setAttribute('value','0.00'); total = 0; }
502         var retrieve_ids = g.bill_list.dump_retrieve_ids();
503         for (var i = 0; i < retrieve_ids.length; i++) {
504             var row_params = g.row_map[retrieve_ids[i]];
505             if (g.check_map[retrieve_ids[i]]) { 
506                 var bill = g.bill_map[retrieve_ids[i]].transaction;
507                 var bo = util.money.dollars_float_to_cents_integer( bill.balance_owed() );
508                 if ( bo > total ) {
509                     row_params.row.my.payment_pending = util.money.cents_as_dollars( total );
510                     total = 0;
511                 } else {
512                     row_params.row.my.payment_pending = util.money.cents_as_dollars( bo );
513                     total = total - bo;
514                 }
515             } else {
516                 row_params.row.my.payment_pending = '0.00';
517             }
518             g.bill_list.refresh_row(row_params);
519         }
520         tally_pending();
521     } catch(E) {
522         alert('Error in bill2.js, distribute_payment(): ' + E);
523     }
524 }
525
526 function apply_payment() {
527     try {
528         var payment_blob = {};
529         JSAN.use('util.window');
530         var win = new util.window();
531         switch($('payment_type').value) {
532             case 'credit_card_payment' :
533                 g.data.temp = '';
534                 g.data.stash('temp');
535                 var my_xulG = win.open(
536                     urls.XUL_PATRON_BILL_CC_INFO,
537                     'billccinfo',
538                     'chrome,resizable,modal',
539                     {'patron_id': g.patron_id}
540                 );
541                 g.data.stash_retrieve();
542                 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
543             break;
544             case 'check_payment' :
545                 g.data.temp = '';
546                 g.data.stash('temp');
547                 var my_xulG = win.open(
548                     urls.XUL_PATRON_BILL_CHECK_INFO,
549                     'billcheckinfo',
550                     'chrome,resizable,modal'
551                 );
552                 g.data.stash_retrieve();
553                 payment_blob = JSON2js( g.data.temp );
554             break;
555         }
556         if (
557             (typeof payment_blob == 'undefined') || 
558             payment_blob=='' || 
559             payment_blob.cancelled=='true'
560         ) { 
561             alert( $('commonStrings').getString('common.cancelled') ); 
562             return; 
563         }
564         payment_blob.userid = g.patron_id;
565         payment_blob.note = payment_blob.note || '';
566         //payment_blob.cash_drawer = 1; // FIXME: get new Config() to work
567         payment_blob.payment_type = $('payment_type').value;
568         var tally_blob = tally_pending();
569         payment_blob.payments = tally_blob.payments;
570         payment_blob.patron_credit = $('convert_change_to_credit').checked ? tally_blob.change : '0.00'; 
571         if ( payment_blob.payments.length == 0 && payment_blob.patron_credit == '0.00' ) {
572             alert($("patronStrings").getString('staff.patron.bills.apply_payment.nothing_applied'));
573             return;
574         }
575         if ( pay( payment_blob ) ) {
576
577             g.data.voided_billings = []; g.data.stash('voided_billings');
578             g.bill_list.clear();
579             retrieve_mbts_for_list();
580             if (typeof window.refresh == 'function') window.refresh();
581             if (typeof window.xulG == 'object' && typeof window.xulG.refresh == 'function') window.xulG.refresh();
582             try {
583                 var no_print_prompting = g.data.hash.aous['circ.staff_client.do_not_auto_attempt_print'];
584                 if (no_print_prompting) {
585                     if (no_print_prompting.indexOf( "Bill Pay" ) > -1) return; // Skip print attempt
586                 }
587                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
588                 g.data.stash_retrieve();
589                 var template = 'bill_payment';
590                 JSAN.use('patron.util'); JSAN.use('util.functional');
591                 var params = { 
592                     'patron' : g.patron,
593                     'lib' : data.hash.aou[ ses('ws_ou') ],
594                     'staff' : ses('staff'),
595                     'header' : data.print_list_templates[template].header,
596                     'line_item' : data.print_list_templates[template].line_item,
597                     'footer' : data.print_list_templates[template].footer,
598                     'type' : data.print_list_templates[template].type,
599                     'list' : util.functional.map_list(
600                         payment_blob.payments,
601                         function(o) {
602                             return {
603                                 'bill_id' : o[0],
604                                 'payment' : o[1],
605                                 'last_billing_type' : g.bill_map[ o[0] ].transaction.last_billing_type(),
606                                 'last_billing_note' : g.bill_map[ o[0] ].transaction.last_billing_note(),
607                                 'title' : typeof g.bill_map[ o[0] ].title != 'undefined' ? g.bill_map[ o[0] ].title : '', 
608                                 'barcode' : typeof g.bill_map[ o[0] ].barcode != 'undefined' ? g.bill_map[ o[0] ].barcode : ''
609                             };
610                         }
611                     ),
612                     'data' : g.previous_summary
613                 };
614                 g.error.sdump('D_DEBUG',js2JSON(params));
615                 if ($('auto_print').checked) params.no_prompt = true;
616                 JSAN.use('util.print'); var print = new util.print();
617                 print.tree_list( params );
618             } catch(E) {
619                 g.standard_unexpected_error_alert('bill receipt', E);
620             }
621         }
622     } catch(E) {
623         alert('Error in bill2.js, apply_payment(): ' + E);
624     }
625 }
626
627 function pay(payment_blob) {
628     try {
629         var x = $('annotate_payment');
630         if (x && x.checked && (! payment_blob.note)) {
631             payment_blob.note = window.prompt(
632                 $("patronStrings").getString('staff.patron.bills.pay.annotate_payment'),
633                 '', 
634                 $("patronStrings").getString('staff.patron.bills.pay.annotate_payment.title')
635             );
636         }
637         previous_summary = {
638             original_balance : obj.controller.view.bill_total_owed.value,
639             voided_balance : obj.controller.view.voided_balance.value,
640             payment_received : obj.controller.view.bill_payment_amount.value,
641             payment_applied : obj.controller.view.bill_payment_applied.value,
642             change_given : obj.controller.view.bill_change_amount.value,
643             credit_given : obj.controller.view.bill_credit_amount.value,
644             new_balance : obj.controller.view.bill_new_balance.value,
645             payment_type : obj.controller.view.payment_type.getAttribute('label'),
646             note : payment_blob.note
647         }
648         var robj = g.network.request(
649             api.BILL_PAY.app,    
650             api.BILL_PAY.method,
651             [ ses(), payment_blob ]
652         );
653         if (robj == 1) { return true; } 
654         if (typeof robj.ilsevent != 'undefined') {
655             switch(Number(robj.ilsevent)) {
656                 case 0 /* SUCCESS */ : return true; break;
657                 case 1226 /* REFUND_EXCEEDS_DESK_PAYMENTS */ : alert($("patronStrings").getFormattedString('staff.patron.bills.pay.refund_exceeds_desk_payment', [robj.desc])); return false; break;
658                 default: throw(robj); break;
659             }
660         }
661     } catch(E) {
662         obj.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.bills.pay.payment_failed'),E);
663         return false;
664     }
665 }
666
667