]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/bills.js
fixes bug 569 : shove patron data into bill payment receipts for template use, so...
[Evergreen.git] / Open-ILS / xul / staff_client / server / patron / bills.js
1 dump('entering patron.bills.js\n');
2
3 if (typeof patron == 'undefined') patron = {};
4 patron.bills = function (params) {
5
6         var obj = this;
7         try { JSAN.use('util.error'); obj.error = new util.error(); } catch(E) { alert(E); }
8         try { JSAN.use('util.network'); obj.network = new util.network(); } catch(E) { alert(E); }
9         try { 
10                 obj.OpenILS = {}; JSAN.use('OpenILS.data'); obj.OpenILS.data = new OpenILS.data(); obj.OpenILS.data.init({'via':'stash'}); 
11                 obj.data = obj.OpenILS.data;
12                 obj.data.voided_billings = []; obj.data.stash('voided_billings');
13         } catch(E) { 
14                 alert(E); 
15         }
16 }
17
18 patron.bills.prototype = {
19
20         'bill_map' : {},
21
22         'current_payments' : [],
23
24         'SHOW_ME_THE_BILLS' : 'FM_MBTS_IDS_RETRIEVE_ALL_HAVING_BALANCE',
25         //'SHOW_ME_THE_BILLS' : 'FM_MBTS_IDS_RETRIEVE_ALL_STILL_OPEN',
26         //'SHOW_ME_THE_BILLS' : 'FM_MOBTS_HAVING_BALANCE',
27         /*'SHOW_ME_THE_BILLS' : 'FM_MOBTS_OPEN',*/
28
29         'refresh' : function(dont_show_me_the_money) {
30                 var obj = this;
31                 try {
32                                 obj.bills = obj.network.simple_request(
33                                         obj.SHOW_ME_THE_BILLS,
34                                         [ ses(), obj.patron_id ]
35                                 );
36                                 //alert('obj.bills = ' + js2JSON(obj.bills));
37
38                                 for (var i = 0; i < obj.bills.length; i++) {
39                                         if (instanceOf(obj.bills[i],mobts)) {
40                                                 obj.bills[i] = { 'transaction' : obj.bills[i] }
41                                         } else if (instanceOf(obj.bills[i],mbts)) {
42                                                 obj.bills[i] = { 'transaction' : obj.bills[i] }
43                                         } else {
44                                                 var robj = obj.network.simple_request('FM_MBTS_RETRIEVE',[ses(),obj.bills[i]]);
45                                                 //alert('refresh robj = ' + js2JSON(robj));
46                                                 obj.bills[i] = { 'transaction' : robj }
47                                         }
48                                 }
49
50                                 if (!dont_show_me_the_money) {
51                                         //alert('dont_show_me_the_money = ' + dont_show_me_the_money);
52                                         if (window.xulG && typeof window.xulG.on_money_change == 'function') {
53                                                 try { window.xulG.on_money_change(obj.bills); } catch(E) { this.error.sdump('D_ERROR',E); }
54                                         }
55                                 }
56
57                                 var tbs = document.getElementsByTagName('textbox');
58                                 for (var i = 0; i < tbs.length; i++) {
59                                         tbs[i].value = ''; tbs[i].setAttribute('value','');
60                                 }
61                                 obj.init();
62                                 obj.controller.view.bill_payment_amount.focus();
63                                 obj.distribute_payment(obj.controller.view.bill_payment_amount);
64                                 obj.tally_selected();
65                                 obj.tally_voided();
66                 } catch(E) {
67                         obj.error.standard_unexpected_error_alert('bills -> refresh',E);        
68                 }
69         },
70
71         'init' : function( params ) {
72         
73                 var obj = this;
74
75                 try {
76                                 obj.init_controller( params );
77
78                                 obj.retrieve();
79
80                                 var total_owed = 0;
81
82                                 JSAN.use('util.money');
83
84                                 obj.current_payments = []; obj.list.clear();
85                                 //FIXME//.bills virtual field
86                                 for (var i = 0; i < obj.bills.length; i++) {
87                                         var rparams = obj.list.append( 
88                                                 { 'row' : 
89                                                         { 'my' : 
90                                                                 { 'mobts' : obj.bills[i].transaction, 'circ' : obj.bills[i].circ, 'mvr' : obj.bills[i].record } 
91                                                         }, 
92                                                         'attributes' : { 'allowevents' : true } 
93                                                 } 
94                                         );
95                                         var rnode = rparams.my_node;
96                                         obj.bill_map[ obj.bills[i].transaction.id() ] = obj.bills[i];
97                                         var cb = rnode.getElementsByTagName('checkbox')[0];
98                                         var tb = rnode.getElementsByTagName('textbox')[0];
99                                         var bo = obj.bills[i].transaction.balance_owed();
100                                         total_owed += util.money.dollars_float_to_cents_integer( bo );
101                                         var id = obj.bills[i].transaction.id();
102                                         obj.current_payments.push( { 'mobts_id' : id, 'balance_owed' : bo, 'checkbox' : cb, 'textbox' : tb, } );
103                                 }
104                                 obj.controller.view.bill_total_owed.value = util.money.cents_as_dollars( total_owed );
105                                 obj.controller.view.bill_total_owed.setAttribute('value',obj.controller.view.bill_total_owed.value);
106                                 obj.distribute_payment(obj.controller.view.bill_payment_amount);
107                                 obj.controller.view.bill_payment_amount.select();
108                                 obj.tally_selected();
109                                 obj.tally_voided();
110                 } catch(E) {
111                         obj.error.standard_unexpected_error_alert('bills -> init',E);   
112                 }
113         },
114
115         'init_controller' : function( params ) {
116
117                 var obj = this;
118
119                 try {
120                                 if (obj._controller_inited) return;
121
122                                 obj.patron_id = obj.patron_id || params['patron_id'];
123
124                                 JSAN.use('util.list'); obj.list = new util.list('bill_list');
125
126                                 function getString(s) { return obj.OpenILS.data.entities[s]; }
127                                 obj.list.init(
128                                         {
129                                                 'columns' : [
130                                                 /*
131                                                                 {
132                                                                         'id' : 'xact_dates', 'label' : getString('staff.bills_xact_dates_label'), 'flex' : 1,
133                                                                         'primary' : false, 'hidden' : false, 'render' : 'obj.xact_dates_box(my.mobts)'
134                                                                 },
135                                                 */
136                                                                 {
137                                                                         'id' : 'notes', 'label' : getString('staff.bills_information'), 'flex' : 2,
138                                                                         'primary' : false, 'hidden' : false, 'render' : 'obj.info_box(my)'
139                                                                 },
140                                                                 {
141                                                                         'id' : 'money', 'label' : 'Money Summary', 'flex' : 1,
142                                                                         'primary' : false, 'hidden' : false, 'render' : 'obj.money_box(my.mobts)'
143                                                                 },
144                                                                 {
145                                                                         'id' : 'current_pay', 'label' : getString('staff.bills_current_payment_label'), 'flex' : 0, 
146                                                                         'render' : 'obj.payment_box()'
147                                                                 }
148                                                 ],
149                                                 'map_row_to_column' : obj.gen_map_row_to_column(),
150                                         }
151                                 );
152
153                                 JSAN.use('util.controller'); obj.controller = new util.controller();
154                                 obj.controller.init(
155                                         {
156                                                 'control_map' : {
157                                                         'cmd_broken' : [
158                                                                 ['command'],
159                                                                 function() { alert('Not Yet Implemented'); }
160                                                         ],
161                                                         'cmd_bill_wizard' : [
162                                                                 ['command'],
163                                                                 function() { 
164                                                                         try {
165                                                                                 JSAN.use('util.window');
166                                                                                 var win = new util.window();
167                                                                                 var my_xulG = win.open(
168                                                                                         urls.XUL_PATRON_BILL_WIZARD,
169                                                                                                 //+ '?patron_id=' + window.escape(obj.patron_id),
170                                                                                         'billwizard',
171                                                                                         'chrome,resizable,modal',
172                                                                                         { 'patron_id' : obj.patron_id }
173                                                                                 );
174                                                                                 obj.refresh();
175                                                                         } catch(E) {
176                                                                                 obj.error.standard_unexpected_error_alert('bills -> cmd_bill_wizard',E);        
177                                                                         }
178                                                                 }
179                                                         ],
180                                                         'cmd_bill_history' : [
181                                                                 ['command'],
182                                                                 function() { 
183                                                                         try {
184                                                                                 JSAN.use('util.window');
185                                                                                 var win = new util.window();
186                                                                                 //obj.OpenILS.data.init({'via':'stash'}); obj.OpenILS.data.temp = ''; obj.OpenILS.data.stash('temp');
187                                                                                 var my_xulG = win.open(
188                                                                                         urls.XUL_PATRON_BILL_HISTORY,
189                                                                                                 //+ '?patron_id=' + window.escape(obj.patron_id),
190                                                                                         'billhistory',
191                                                                                         //'chrome,resizable,modal'
192                                                                                         'chrome,resizable',
193                                                                                         {
194                                                                                                 'patron_id' : obj.patron_id,
195                                                                                                 'refresh' : function() { obj.refresh(); }
196                                                                                         }
197                                                                                 );
198                                                                         } catch(E) {
199                                                                                 obj.error.standard_unexpected_error_alert('bills -> cmd_bill_history',E);       
200                                                                         }
201                                                                 }
202                                                         ],
203                                                         'cmd_alternate_view' : [
204                                                                 ['command'],
205                                                                 function() { 
206                                                                         try {
207                                                                                 JSAN.use('util.window');
208                                                                                 var win = new util.window();
209                                                                                 //obj.OpenILS.data.init({'via':'stash'}); obj.OpenILS.data.temp = ''; obj.OpenILS.data.stash('temp');
210                                                                                 var my_xulG = win.open(
211                                                                                         urls.XUL_PATRON_BILL_HISTORY,
212                                                                                                 //+ '?current=1&patron_id=' + window.escape(obj.patron_id),
213                                                                                         'billhistory',
214                                                                                         //'chrome,resizable,modal'
215                                                                                         'chrome,resizable',
216                                                                                         {
217                                                                                                 'current' : 1,
218                                                                                                 'patron_id' : obj.patron_id,
219                                                                                                 'refresh' : function() { obj.refresh(); },
220                                                                                         }
221                                                                                 );
222                                                                         } catch(E) {
223                                                                                 obj.error.standard_unexpected_error_alert('bills -> cmd_alternate_view',E);     
224                                                                         }
225                                                                 }
226                                                         ],
227
228
229                                                         'cmd_change_to_credit' : [
230                                                                 ['command'],
231                                                                 function() {
232                                                                         try {
233                                                                                 obj.change_to_credit();
234                                                                         } catch(E) {
235                                                                                 obj.error.standard_unexpected_error_alert('bills -> cmd_change_to_credit',E);   
236                                                                         }
237                                                                 }
238                                                         ],
239                                                         'cmd_bill_apply_payment' : [
240                                                                 ['command'],
241                                                                 function() {
242                                                                         try { 
243                                                                                 obj.apply_payment(); 
244                                                                         } catch(E) { 
245                                                                                 obj.error.standard_unexpected_error_alert('bills -> cmd_bill_apply_payment',E); 
246                                                                         }
247                                                                 }
248                                                         ],
249                                                         'cmd_check_all' : [
250                                                                 ['command'],
251                                                                 function() {
252                                                                         for (var i = 0; i < obj.current_payments.length; i++) {
253                                                                                 obj.current_payments[i].checkbox.setAttribute('checked',true); //checked = true;
254                                                                         }
255                                                                         obj.distribute_payment(obj.controller.view.bill_payment_amount);
256                                                                         obj.tally_selected();
257                                                                 }
258                                                         ],
259                                                         'cmd_uncheck_all' : [
260                                                                 ['command'],
261                                                                 function() {
262                                                                         for (var i = 0; i < obj.current_payments.length; i++) {
263                                                                                 obj.current_payments[i].checkbox.setAttribute('checked',false); //checked = false;
264                                                                         }
265                                                                         obj.distribute_payment(obj.controller.view.bill_payment_amount);
266                                                                         obj.tally_selected();
267                                                                 }
268                                                         ],
269                                                         'voided_balance' : [
270                                                                 ['render'],
271                                                                 function(e) { return function() {}; }
272                                                         ],
273                                                         'selected_balance' : [
274                                                                 ['render'],
275                                                                 function(e) { return function() {}; }
276                                                         ],
277                                                         'unselected_balance' : [
278                                                                 ['render'],
279                                                                 function(e) { return function() {}; }
280                                                         ],
281                                                         'bill_total_owed' : [
282                                                                 ['render'],
283                                                                 function(e) { return function() {}; }
284                                                         ],
285                                                         'payment_type' : [
286                                                                 ['render'],
287                                                                 function(e) { return function() {}; }
288                                                         ],
289                                                         'bill_payment_amount' : [
290                                                                 ['change','keypress'],
291                                                                 function(ev) {
292                                                                         try {
293                                                                                         if (ev.type == 'keypress') {
294                                                                                                 if (! (ev.keyCode == 13 /* enter */ || ev.keyCode == 77 /* mac enter */) ) {
295                                                                                                         return; 
296                                                                                                 }
297                                                                                         }
298                                                                                         if (ev.type == 'change' && obj.controller.view.payment_type.value == 'credit_payment') {
299                                                                                                 JSAN.use('util.money');
300                                                                                                 JSAN.use('patron.util'); var au_obj = patron.util.retrieve_au_via_id(ses(),obj.patron_id);
301                                                                                                 var proposed = util.money.dollars_float_to_cents_integer(ev.target.value);
302                                                                                                 var available = util.money.dollars_float_to_cents_integer(au_obj.credit_forward_balance());
303                                                                                                 if (proposed > available) {
304                                                                                                         alert('Patron only has ' + au_obj.credit_forward_balance() + ' in credit.');
305                                                                                                         ev.target.value = util.money.cents_as_dollars( available );
306                                                                                                         ev.target.setAttribute('value',ev.target.value);
307                                                                                                 }
308                                                                                         }
309                                                                                         obj.distribute_payment(ev.target);
310                                                                                         ev.target.select();
311                                                                                         obj.tally_selected();
312                                                                         } catch(E) {
313                                                                                 obj.error.standard_unexpected_error_alert('bills -> bill_payment_amount',E);    
314                                                                         }
315                                                                 } 
316                                                         ],
317                                                         'bill_payment_applied' : [
318                                                                 ['render'],
319                                                                 function(e) { return function() {}; }
320                                                         ],
321                                                         'bill_change_amount' : [
322                                                                 ['change'],
323                                                                 function(ev) {
324                                                                         try {
325                                                                                         JSAN.use('util.money');
326                                                                                         var tb = ev.target;
327                                                                                         var proposed_change = util.money.dollars_float_to_cents_integer( tb.value );
328                                                                                         var proposed_credit = 0;
329                                                                                         obj.update_payment_applied();
330                                                                                         var real_change = util.money.dollars_float_to_cents_integer( tb.value );
331                                                                                         if ( proposed_change > real_change ) {
332                                                                                                 obj.error.sdump('D_ERROR','Someone wanted more money than they deserved\n');
333                                                                                                 proposed_change = real_change;
334                                                                                         } else if ( real_change > proposed_change ) {
335                                                                                                 proposed_credit = real_change - proposed_change;
336                                                                                         }
337                                                                                         tb.value = util.money.cents_as_dollars( proposed_change );
338                                                                                         tb.setAttribute('value',tb.value);
339                                                                                         obj.controller.view.bill_credit_amount.value = util.money.cents_as_dollars( proposed_credit );
340                                                                                         obj.controller.view.bill_credit_amount.setAttribute(
341                                                                                                 'value',
342                                                                                                 obj.controller.view.bill_credit_amount.value
343                                                                                         );
344                                                                         } catch(E) {
345                                                                                 obj.error.standard_unexpected_error_alert('bills -> bill_change_amount',E);     
346                                                                         }
347                                                                 }
348                                                         ],
349                                                         'bill_credit_amount' : [
350                                                                 ['render'],
351                                                                 function(e) { return function() {}; }
352                                                         ],
353                                                         'bill_new_balance' : [
354                                                                 ['render'],
355                                                                 function(e) { return function() {}; }
356                                                         ],
357                                                 }
358                                         }
359                                 );
360
361                                 obj.controller.render();
362                                 obj._controller_inited = true;
363                 } catch(E) {
364                         obj.error.standard_unexpected_error_alert('bills -> init_controller',E);        
365                 }
366
367         },
368
369         /*****************************************************************************************************************************/
370
371         'distribute_payment' : function(node) {
372                 try {
373                         var obj = this;
374                         JSAN.use('util.money');
375                         var tb = node;
376                         tb.value = util.money.cents_as_dollars( util.money.dollars_float_to_cents_integer( tb.value ) );
377                         tb.setAttribute('value', tb.value );
378                         var total = util.money.dollars_float_to_cents_integer( tb.value );
379                         if (total < 0) { tb.value = '0.00'; tb.setAttribute('value','0.00'); total = 0; }
380                         for (var i = 0; i < obj.current_payments.length; i++) {
381                                         var bill = obj.current_payments[i];
382                                         if (bill.checkbox.checked || bill.checkbox.getAttribute('checked') == 'true') {
383                                                 var bo = util.money.dollars_float_to_cents_integer( bill.balance_owed );
384                                                 if ( bo > total ) {
385                                                         bill.textbox.value = util.money.cents_as_dollars( total );
386                                                         total = 0;
387                                                 } else {
388                                                         bill.textbox.value = util.money.cents_as_dollars( bo );
389                                                         total = total - bo;
390                                                 }
391                                         } else {
392                                                 bill.textbox.value = '0.00';
393                                         }
394                                         bill.textbox.setAttribute('value',bill.textbox.value);
395                         }
396                         obj.update_payment_applied();
397                 } catch(E) {
398                         obj.error.standard_unexpected_error_alert('bills -> distribute payment',E);
399                 }
400         },
401
402         /*****************************************************************************************************************************/
403
404         'tally_selected' : function() {
405
406                         var obj = this;
407                         JSAN.use('util.money');
408                         var selected_total = 0;
409                         var unselected_total = 0;
410                         //var s = '';
411
412                         for (var i = 0; i < obj.current_payments.length; i++) {
413                                 var bill = obj.current_payments[i];
414                                 if (bill.checkbox.checked || bill.checkbox.getAttribute('checked') == 'true') {
415                                         var bo = util.money.dollars_float_to_cents_integer( bill.balance_owed );
416                                         selected_total += bo;
417                                         //s += ('tallying ' + i + ' : checked, bo = ' + bo + '  total = ' + selected_total + '\n');
418                                 } else {
419                                         var bo = util.money.dollars_float_to_cents_integer( bill.balance_owed );
420                                         unselected_total += bo;
421                                         //s += ('tallying ' + i + ' : not checked, total = ' + selected_total + '\n');
422                                 }
423                         }
424                         obj.controller.view.selected_balance.setAttribute('value', '$' + util.money.cents_as_dollars( selected_total ) );
425                         obj.controller.view.unselected_balance.setAttribute('value', '$' + util.money.cents_as_dollars( unselected_total ) );
426                         //dump(s); alert(s);
427         },
428
429         /*****************************************************************************************************************************/
430
431         'tally_voided' : function() {
432
433                         var obj = this;
434                         JSAN.use('util.money');
435                         var voided_total = 0;
436
437                         obj.data.stash_retrieve();
438
439                         for (var i = 0; i < obj.data.voided_billings.length; i++) {
440                                 var billing = obj.data.voided_billings[i];
441                                 var bv = util.money.dollars_float_to_cents_integer( billing.amount() );
442                                 voided_total += bv;
443                         }
444                         obj.controller.view.voided_balance.setAttribute('value', '$' + util.money.cents_as_dollars( voided_total ) );
445         },
446
447         /*****************************************************************************************************************************/
448
449         'apply_payment' : function() {
450
451                 var obj = this;
452                 try {
453                                 var payment_blob = {};
454                                 JSAN.use('util.window');
455                                 var win = new util.window();
456                                 switch(obj.controller.view.payment_type.value) {
457                                         case 'credit_card_payment' :
458                                                 obj.OpenILS.data.temp = '';
459                                                 obj.OpenILS.data.stash('temp');
460                                                 var my_xulG = win.open(
461                                                         urls.XUL_PATRON_BILL_CC_INFO,
462                                                         'billccinfo',
463                                                         'chrome,resizable,modal'
464                                                 );
465                                                 obj.OpenILS.data.stash_retrieve();
466                                                 //payment_blob = my_xulG.payment_blob; //JSON2js( obj.OpenILS.data.temp );
467                                                 payment_blob = JSON2js( obj.OpenILS.data.temp );
468                                         break;
469                                         case 'check_payment' :
470                                                 obj.OpenILS.data.temp = '';
471                                                 obj.OpenILS.data.stash('temp');
472                                                 var my_xulG = win.open(
473                                                         urls.XUL_PATRON_BILL_CHECK_INFO,
474                                                         'billcheckinfo',
475                                                         'chrome,resizable,modal'
476                                                 );
477                                                 obj.OpenILS.data.stash_retrieve();
478                                                 //payment_blob = my_xulG.payment_blob; //JSON2js( obj.OpenILS.data.temp );
479                                                 payment_blob = JSON2js( obj.OpenILS.data.temp );
480                                         break;
481                                 }
482                                 if ((typeof payment_blob == 'undefined') || payment_blob=='' || payment_blob.cancelled=='true') { alert('cancelled'); return; }
483                                 payment_blob.userid = obj.patron_id;
484                                 payment_blob.note = payment_blob.note || '';
485                                 //payment_blob.cash_drawer = 1; // FIXME: get new Config() to work
486                                 payment_blob.payment_type = obj.controller.view.payment_type.value;
487                                 payment_blob.payments = [];
488                                 payment_blob.patron_credit = obj.controller.view.bill_credit_amount.value;
489                                 for (var i = 0; i < obj.current_payments.length; i++) {
490                                         var tb = obj.current_payments[ i ].textbox;
491                                         if ( !(tb.value == '0.00' || tb.value == '') ) {
492                                                 payment_blob.payments.push( 
493                                                         [
494                                                                 obj.current_payments[ i ].mobts_id,
495                                                                 tb.value
496                                                         ]
497                                                 );
498                                         }
499                                 }
500                                 if ( obj.pay( payment_blob ) ) {
501
502                                         obj.data.voided_billings = []; obj.data.stash('voided_billings');
503                                         obj.refresh();
504                                         try {
505                                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
506                                                 obj.data.stash_retrieve();
507                                                 var template = 'bill_payment';
508                                                 JSAN.use('patron.util'); JSAN.use('util.functional');
509                                                 var params = { 
510                                                         'patron' : patron.util.retrieve_fleshed_au_via_id(ses(),obj.patron_id), 
511                                                         'lib' : obj.data.hash.aou[ obj.data.list.au[0].ws_ou() ],
512                                                         'staff' : obj.data.list.au[0],
513                                                         'header' : obj.data.print_list_templates[template].header,
514                                                         'line_item' : obj.data.print_list_templates[template].line_item,
515                                                         'footer' : obj.data.print_list_templates[template].footer,
516                                                         'type' : obj.data.print_list_templates[template].type,
517                                                         'list' : util.functional.map_list(
518                                                                 payment_blob.payments,
519                                                                 function(o) {
520                                                                         return {
521                                                                                 'bill_id' : o[0],
522                                                                                 'payment' : o[1],
523                                                                                 'last_billing_type' : obj.bill_map[ o[0] ].transaction.last_billing_type(),
524                                                                                 'last_billing_note' : obj.bill_map[ o[0] ].transaction.last_billing_note(),
525                                                                                 'title' : typeof obj.bill_map[ o[0] ].title != 'undefined' ? obj.bill_map[ o[0] ].title : '', 
526                                                                                 'barcode' : typeof obj.bill_map[ o[0] ].barcode != 'undefined' ? obj.bill_map[ o[0] ].barcode : '', 
527                                                                         };
528                                                                 }
529                                                         ),
530                                                         'data' : obj.previous_summary,
531                                                 };
532                                                 obj.error.sdump('D_DEBUG',js2JSON(params));
533                                                 if (document.getElementById('auto_print').checked) params.no_prompt = true;
534                                                 JSAN.use('util.print'); var print = new util.print();
535                                                 print.tree_list( params );
536                                         } catch(E) {
537                                                 obj.error.standard_unexpected_error_alert('bill receipt', E);
538                                         }
539                                 }
540                 } catch(E) {
541                         obj.error.standard_unexpected_error_alert('bills -> apply_payment',E);  
542                 }
543         },
544
545         'pay' : function(payment_blob) {
546                 var obj = this;
547                 try {
548             var x = document.getElementById('annotate_payment');
549             if (x && x.checked && (! payment_blob.note)) {
550                 payment_blob.note = window.prompt('Please annotate this payment:','','Annotate Payment');
551             }
552                         obj.previous_summary = {
553                                 original_balance : obj.controller.view.bill_total_owed.value,
554                                 voided_balance : obj.controller.view.voided_balance.value,
555                                 payment_received : obj.controller.view.bill_payment_amount.value,
556                                 payment_applied : obj.controller.view.bill_payment_applied.value,
557                                 change_given : obj.controller.view.bill_change_amount.value,
558                                 credit_given : obj.controller.view.bill_credit_amount.value,
559                                 new_balance : obj.controller.view.bill_new_balance.value,
560                                 payment_type : obj.controller.view.payment_type.getAttribute('label'),
561                                 note : payment_blob.note,
562                         }
563                         var robj = obj.network.request(
564                                 api.BILL_PAY.app,       
565                                 api.BILL_PAY.method,
566                                 [ ses(), payment_blob ]
567                         );
568                         if (robj == 1) { return true; } 
569                         if (typeof robj.ilsevent != 'undefined') {
570                                 switch(robj.ilsevent) {
571                                         case 0 /* SUCCESS */ : return true; break;
572                                         case 1226 /* REFUND_EXCEEDS_DESK_PAYMENTS */ : alert(robj.desc + '\n\nAnother way to "zero" this transaction is to use Add Billing and add a misc bill to counter the negative balance.'); return false; break;
573                                         default: throw(robj); break;
574                                 }
575                         }
576                 } catch(E) {
577                         obj.error.standard_unexpected_error_alert('Bill payment likely failed',E);
578                         return false;
579                 }
580         },
581
582         'update_payment_applied' : function() {
583                 var obj = this;
584                 try {
585                                 JSAN.use('util.money');
586                                 var total_applied = 0;
587                                 for (var i = 0; i < obj.current_payments.length; i++) {
588                                         total_applied += util.money.dollars_float_to_cents_integer( obj.current_payments[ i ].textbox.value );
589                                 }
590                                 var total_payment = 0;
591                                 if (obj.controller.view.bill_payment_amount.value) {
592                                         try {
593                                                 total_payment = util.money.dollars_float_to_cents_integer( obj.controller.view.bill_payment_amount.value );
594                                         } catch(E) {
595                                                 obj.error.sdump('D_ERROR',E + '\n');
596                                         }
597                                 }
598                                 if ( total_applied > total_payment ) {
599                                         total_payment = total_applied;
600                                         obj.controller.view.bill_payment_amount.value = util.money.cents_as_dollars( total_applied );
601                                 }
602                                 obj.controller.view.bill_payment_applied.value = util.money.cents_as_dollars( total_applied );
603                                 obj.controller.view.bill_payment_applied.setAttribute('value', obj.controller.view.bill_payment_applied.value )
604                                 obj.controller.view.bill_credit_amount.value = '';
605                                 if (total_payment > total_applied ) {
606                                         obj.controller.view.bill_change_amount.value = util.money.cents_as_dollars( total_payment - total_applied);
607                                         obj.controller.view.bill_credit_amount.value = '0.00';
608                                 } else {
609                                         obj.controller.view.bill_change_amount.value = '0.00';
610                                         obj.controller.view.bill_credit_amount.value = '0.00';
611                                 }
612                                 var total_owed = util.money.dollars_float_to_cents_integer( obj.controller.view.bill_total_owed.value );
613                                 obj.controller.view.bill_new_balance.value = util.money.cents_as_dollars( total_owed - total_applied );
614                 } catch(E) {
615                         obj.error.standard_unexpected_error_alert('bills -> update_payment_applied',E); 
616                 }
617         },
618
619         'change_to_credit' : function() {
620                 var obj = this;
621                 try {
622                                 JSAN.use('util.money');
623                                 var tb = obj.controller.view.bill_change_amount;
624                                 var proposed_change = 0;
625                                 var proposed_credit = util.money.dollars_float_to_cents_integer( tb.value );
626                                 obj.update_payment_applied();
627                                 var real_change = util.money.dollars_float_to_cents_integer( tb.value );
628                                 if ( proposed_change > real_change ) {
629                                         obj.error.sdump('D_ERROR','Someone wanted more money than they deserved\n');
630                                         proposed_change = real_change;
631                                 } else if ( real_change > proposed_change ) {
632                                         proposed_credit = real_change - proposed_change;
633                                 }
634                                 tb.value = util.money.cents_as_dollars( proposed_change );
635                                 tb.setAttribute('value',tb.value);
636                                 obj.controller.view.bill_credit_amount.value = util.money.cents_as_dollars( proposed_credit );
637                                 obj.controller.view.bill_credit_amount.setAttribute('value',obj.controller.view.bill_credit_amount.value);
638                 } catch(E) {
639                         obj.error.standard_unexpected_error_alert('bills -> change_to_credit',E);       
640                 }
641         },
642
643         'retrieve' : function() {
644                 var obj = this;
645                 try {
646                                 if (window.xulG && window.xulG.bills) {
647                                         obj.bills = window.xulG.bills;
648                                 } else {
649                                         obj.bills = obj.network.simple_request(
650                                                 obj.SHOW_ME_THE_BILLS,  
651                                                 [ ses(), obj.patron_id ]
652                                         );
653                                         for (var i = 0; i < obj.bills.length; i++) {
654                                                 if (instanceOf(obj.bills[i],mobts)) {
655                                                         obj.bills[i] = { 'transaction' : obj.bills[i] }
656                                                 } else if (instanceOf(obj.bills[i],mbts)) {
657                                                         obj.bills[i] = { 'transaction' : obj.bills[i] }
658                                                 } else {
659                                                         var robj = obj.network.simple_request('FM_MBTS_RETRIEVE',[ses(),obj.bills[i]]);
660                                                         //alert('robj = ' + js2JSON(robj));
661                                                         obj.bills[i] = { 'transaction' : robj }
662                                                 }
663                                         }
664                                 }
665                 } catch(E) {
666                         obj.error.standard_unexpected_error_alert('bills -> retrieve',E);       
667                 }
668         },
669
670         'xact_dates_box' : function ( mobts ) {
671                 var obj = this;
672                 try {
673                                 function getString(s) { return obj.OpenILS.data.entities[s]; }
674                                 var grid = document.createElement('grid');
675                                         var cols = document.createElement('columns');
676                                         grid.appendChild( cols );
677                                                 cols.appendChild( document.createElement('column') );
678                                                 cols.appendChild( document.createElement('column') );
679                                         var rows = document.createElement('rows');
680                                         grid.appendChild( rows );
681                                                 var row0 = document.createElement('row');
682                                                 rows.appendChild( row0 );
683                                                         var cb_r0_0 = document.createElement('checkbox');
684                                                         row0.appendChild( cb_r0_0 );
685                                                         cb_r0_0.setAttribute('checked',true);
686                                                         var hb_r0_1 = document.createElement('hbox');
687                                                         row0.appendChild( hb_r0_1 );
688                                                                 var label_r0_1 = document.createElement('label');
689                                                                 hb_r0_1.appendChild( label_r0_1 );
690                                                                 label_r0_1.setAttribute('value',getString('staff.mbts_id_label'));
691                                                                 var label_r0_2 = document.createElement('label');
692                                                                 hb_r0_1.appendChild( label_r0_2 );
693                                                                 label_r0_2.setAttribute('value',mobts.id());
694                                                 var row1 = document.createElement('row');
695                                                 rows.appendChild( row1 );
696                                                         var label_r1_1 = document.createElement('label');
697                                                         row1.appendChild( label_r1_1 );
698                                                         label_r1_1.setAttribute('value',getString('staff.mbts_xact_start_label'));
699                                                         var label_r1_2 = document.createElement('label');
700                                                         row1.appendChild( label_r1_2 );
701                                                         label_r1_2.setAttribute('value',mobts.xact_start().toString().substr(0,10));
702                                                 var row2 = document.createElement('row');
703                                                 rows.appendChild( row2 );
704                                                         var label_r2_1 = document.createElement('label');
705                                                         row2.appendChild( label_r2_1 );
706                                                         label_r2_1.setAttribute('value',getString('staff.mbts_xact_finish_label'));
707                                                         var label_r2_2 = document.createElement('label');
708                                                         row2.appendChild( label_r2_2 );
709                                                         try { label_r2_2.setAttribute('value',mobts.xact_finish().toString().substr(0,10));
710                                                         } catch(E) {}
711
712                                 return grid;
713                 } catch(E) {
714                         obj.error.standard_unexpected_error_alert('bills -> xact_dates_box',E); 
715                 }
716         },
717
718         'money_box' : function ( mobts ) {
719                 var obj = this;
720                 try {
721                                 JSAN.use('util.money');
722                                 function getString(s) { return obj.OpenILS.data.entities[s]; }
723                                 var grid = document.createElement('grid');
724                                         var cols = document.createElement('columns');
725                                         grid.appendChild( cols );
726                                                 cols.appendChild( document.createElement('column') );
727                                                 cols.appendChild( document.createElement('column') );
728                                         var rows = document.createElement('rows');
729                                         grid.appendChild( rows );
730                                                 var row1 = document.createElement('row');
731                                                 rows.appendChild( row1 );
732                                                         var label_r1_1 = document.createElement('label');
733                                                         row1.appendChild( label_r1_1 );
734                                                         label_r1_1.setAttribute('value',getString('staff.mbts_total_owed_label'));
735                                                         var label_r1_2 = document.createElement('label');
736                                                         row1.appendChild( label_r1_2 );
737                                                         label_r1_2.setAttribute('value','$' + util.money.sanitize(mobts.total_owed() || '0') );
738                                                 var row2 = document.createElement('row');
739                                                 rows.appendChild( row2 );
740                                                         var label_r2_1 = document.createElement('label');
741                                                         row2.appendChild( label_r2_1 );
742                                                         label_r2_1.setAttribute('value',getString('staff.mbts_total_paid_label'));
743                                                         var label_r2_2 = document.createElement('label');
744                                                         row2.appendChild( label_r2_2 );
745                                                         label_r2_2.setAttribute('value','$' + util.money.sanitize(mobts.total_paid() || '0') );
746                                                 var row3 = document.createElement('row');
747                                                 rows.appendChild( row3 );
748                                                         var label_r3_1 = document.createElement('label');
749                                                         row3.appendChild( label_r3_1 );
750                                                         label_r3_1.setAttribute('value',getString('staff.mbts_balance_owed_label'));
751                                                         label_r3_1.setAttribute('style','font-weight: bold');
752                                                         var label_r3_2 = document.createElement('label');
753                                                         row3.appendChild( label_r3_2 );
754                                                         label_r3_2.setAttribute('value','$' + util.money.sanitize(mobts.balance_owed() || '0') );
755                                                         label_r3_2.setAttribute('style','font-weight: bold');
756
757                                 return grid;
758                 } catch(E) {
759                         obj.error.standard_unexpected_error_alert('bills -> money_box',E);      
760                 }
761         },
762
763         'info_box' : function ( my ) {
764                 var obj = this;
765                 try {
766                                 function getString(s) { return obj.OpenILS.data.entities[s]; }
767                                 var vbox = document.createElement('vbox');
768
769                                         var hbox = document.createElement('hbox');
770                                                 vbox.appendChild( hbox ); hbox.flex = 1;
771
772                                                 var cb = document.createElement('checkbox');
773                                                 hbox.appendChild( cb ); 
774                                                 if ( my.mobts.balance_owed() == 0 ) { 
775                                                         cb.setAttribute('disabled', 'true'); 
776                                                 } else { 
777                                                         if (my.mobts.balance_owed() > 0) {
778                                                                 cb.setAttribute('checked', true); 
779                                                         }
780                                                 }
781                                                 cb.addEventListener(
782                                                         'command',
783                                                         function() {
784                                                                 setTimeout(
785                                                                         function() {
786                                                                                 obj.distribute_payment(obj.controller.view.bill_payment_amount);
787                                                                                 obj.tally_selected();
788                                                                         }, 0
789                                                                 );
790                                                         },
791                                                         false
792                                                 );
793
794
795                                         var grid = document.createElement('grid');
796                                                 hbox.appendChild( grid );
797
798                                                 var cols = document.createElement('columns');
799                                                         grid.appendChild( cols );
800                                                         cols.appendChild( document.createElement('column') );
801                                                         cols.appendChild( document.createElement('column') );
802                                                 var rows = document.createElement('rows');
803                                                         grid.appendChild( rows );
804
805                                         var xact_type = document.createElement('row');
806                                         rows.appendChild( xact_type );
807
808                                                 var xt_label = document.createElement('label');
809                                                         xact_type.appendChild( xt_label );
810                                                 var xt_value = document.createElement('description');
811                                                         xact_type.appendChild( xt_value );
812
813                                         try {
814                                         switch(my.mobts.xact_type()) {
815                                                 case 'circulation':
816                                                         xt_label.setAttribute( 'value', 'Title:' );
817                                                         obj.network.simple_request(
818                                                                 'FM_CIRC_RETRIEVE_VIA_ID',
819                                                                 [ ses(), my.mobts.id() ],
820                                                                 function (req) {
821                                                                         var r_circ = req.getResultObject();
822                                                                         if (instanceOf(r_circ,circ)) {
823                                                                                 /*
824                                                                                 xt_start.setAttribute('value','Checked Out: ' + r_circ.xact_start().toString().substr(0,10) );
825                                                                                 if (r_circ.checkin_time()) {
826                                                                                         xt_finish.setAttribute('value','Returned: ' + r_circ.checkin_time().toString().substr(0,10) );
827                                                                                 } else {
828                                                                                         xt_finish.setAttribute('value','Due: ' + r_circ.due_date().toString().substr(0,10) );
829                                                                                 }
830                                                                                 */
831                                                                                 if (! r_circ.checkin_time()) {
832                                                                                         xt_value.setAttribute('style','background: red; color: white');
833                                                                                         if (document.getElementById('circulating_hint')) {
834                                                                                                 document.getElementById('circulating_hint').hidden = false;
835                                                                                         }
836                                                                                 }
837                                                                                 obj.network.simple_request(
838                                                                                         'MODS_SLIM_RECORD_RETRIEVE_VIA_COPY',
839                                                                                         [ r_circ.target_copy() ],
840                                                                                         function (rreq) {
841                                                                                                 var r_mvr = rreq.getResultObject();
842                                                                                                 if (instanceOf(r_mvr,mvr)) {
843                                                                                                         xt_value.appendChild( document.createTextNode( String( r_mvr.title() ).substr(0,50) ) );
844                                                                                                         obj.bill_map[ my.mobts.id() ].title = r_mvr.title();
845                                                                                                 }
846                                                                                         }
847                                                                                 );
848                                                                                 obj.network.simple_request(
849                                                                                         'FM_ACP_RETRIEVE',
850                                                                                         [ r_circ.target_copy() ],
851                                                                                         function (rrreq) {
852                                                                                                 var r_acp = rrreq.getResultObject();
853                                                                                                 if (instanceOf(r_acp,acp)) {
854                                                                                                         xt_value.appendChild( document.createTextNode( r_acp.dummy_title() ) );
855                                                                                                         if (r_acp.dummy_title()) obj.bill_map[ my.mobts.id() ].title = r_acp.dummy_title();
856                                                                                                         obj.bill_map[ my.mobts.id() ].barcode = r_acp.barcode();
857                                                                                                 }
858                                                                                         }
859                                                                                 );
860                                                                         }
861                                                                 }
862                                                         );
863                                                 break;
864                                                 default:
865                                                                 xt_label.setAttribute( 'value', my.mvr ? 'Title' : 'Type' );
866                                                                 xt_value.appendChild( document.createTextNode( my.mvr ? my.mvr.title() : my.mobts.xact_type() ) );
867                                                 break;
868                                         }
869                                         } catch(E) { alert(E); }
870
871                                         var last_billing = document.createElement('row');
872                                         rows.appendChild( last_billing );
873
874                                                 var lb_label = document.createElement('label');
875                                                         last_billing.appendChild( lb_label );
876                                                         lb_label.setAttribute( 'value', 'Last Billing:' );
877
878                                                 var lb_value = document.createElement('label');
879                                                         last_billing.appendChild( lb_value );
880                                                         if (my.mobts.last_billing_type()) 
881                                                                 lb_value.setAttribute( 'value', my.mobts.last_billing_type() );
882                 /*
883                                         var last_payment = document.createElement('row');
884                                         rows.appendChild( last_payment );
885
886                                                 var lp_label = document.createElement('label');
887                                                         last_payment.appendChild( lp_label );
888                                                         lp_label.setAttribute( 'value', 'Last Payment:' );
889
890                                                 var lp_value = document.createElement('label');
891                                                         last_payment.appendChild( lp_value );
892                                                         if (my.mobts.last_payment_type()) 
893                                                                 lp_value.setAttribute( 'value', my.mobts.last_payment_type() );
894                 */
895                                         var btn_box = document.createElement('hbox');
896                                         vbox.appendChild( btn_box ); btn_box.flex = 1;
897                                                         var btn = document.createElement('button');
898                                                                 btn_box.appendChild( btn );
899                                                                 btn.setAttribute( 'label', 'Full Details' );
900                                                                 btn.setAttribute( 'name', 'full_details' );
901                                                                 btn.setAttribute( 'mobts_id', my.mobts.id() );  
902                                                                 btn.addEventListener(
903                                                                         'command',
904                                                                         function(ev) {
905                                                                                 JSAN.use('util.window'); var win = new util.window();
906                                                                                 var my_xulG = win.open(
907                                                                                         urls.XUL_PATRON_BILL_DETAILS,
908                                                                                         //+ '?patron_id=' + window.escape(obj.patron_id)
909                                                                                         //+ '&mbts_id=' + window.escape(my.mobts.id()),
910                                                                                         'test' + my.mobts.id(),
911                                                                                         'chrome,resizable',
912                                                                                         {
913                                                                                                 'patron_id' : obj.patron_id,
914                                                                                                 'mbts_id' : my.mobts.id(),
915                                                                                                 'refresh' : function() { obj.refresh(); },
916                                                                                         }
917                                                                                 );
918                                                                         },
919                                                                         false
920                                                                 );
921                                                         var btn2 = document.createElement('button');
922                                                                 btn_box.appendChild( btn2 );
923                                                                 btn2.setAttribute( 'label', 'Add Billing' );
924                                                                 btn2.setAttribute( 'mobts_id', my.mobts.id() ); 
925                                                                 btn2.addEventListener(
926                                                                         'command',
927                                                                         function(ev) {
928                                                                                 JSAN.use('util.window');
929                                                                                 var win = new util.window();
930                                                                                 var my_xulG = win.open(
931                                                                                         urls.XUL_PATRON_BILL_WIZARD,
932                                                                                                 //+ '?patron_id=' + window.escape(obj.patron_id)
933                                                                                                 //+ '&xact_id=' + window.escape( my.mobts.id() ),
934                                                                                         'billwizard',
935                                                                                         'chrome,resizable,modal',
936                                                                                         {
937                                                                                                 'patron_id' : obj.patron_id,
938                                                                                                 'xact_id' : my.mobts.id(),
939                                                                                         }
940                                                                                 );
941                                                                                 obj.refresh();
942                                                                         },
943                                                                         false
944                                                                 );
945                                 if (my.mobts.balance_owed() < 0) {
946                                         var btn3 = document.createElement('button');
947                                         btn_box.appendChild( btn3 );
948                                         btn3.setAttribute( 'label', 'Refund' );
949                                         btn3.setAttribute( 'mobts_id', my.mobts.id() ); 
950                                         btn3.addEventListener(
951                                                 'command',
952                                                 function(ev) {
953                                                         cb.setAttribute('checked',true);
954                                                         obj.distribute_payment(obj.controller.view.bill_payment_amount);
955                                                         obj.tally_selected();
956                                                 },
957                                                 false
958                                         );
959                                 }
960
961                                 var btn4 = document.createElement('button');
962                                 btn_box.appendChild( btn4 );
963                                 btn4.setAttribute( 'label', 'Void All Billings' );
964                                 btn4.setAttribute( 'mobts_id', my.mobts.id() );
965                                 btn4.addEventListener(
966                                         'command',
967                                         function(ev) {
968                                                 obj.void_all_billings( my.mobts.id() );
969                                         },
970                                         false
971                                 );
972
973                                 return vbox;
974                 } catch(E) {
975                         obj.error.standard_unexpected_error_alert('bills -> info_box',E);       
976                 }
977         },
978
979         'payment_box' : function() {
980                 try {
981                         var vb = document.createElement('vbox');
982                         var tb = document.createElement('textbox');
983                         tb.setAttribute('readonly','true');
984                         vb.appendChild(tb);
985                         return vb;
986                 } catch(E) {
987                         this.error.standard_unexpected_error_alert('bills -> payment_box',E);   
988                 }
989         },
990
991         'void_all_billings' : function(mobts_id) {
992                 try {
993                         var obj = this;
994                         JSAN.use('util.functional');
995                         
996                         var mb_list = obj.network.simple_request( 'FM_MB_RETRIEVE_VIA_MBTS_ID', [ ses(), mobts_id ] );
997                         if (typeof mb_list.ilsevent != 'undefined') throw(mb_list);
998
999                         mb_list = util.functional.filter_list( mb_list, function(o) { return ! get_bool( o.voided() ) });
1000
1001                         if (mb_list.length == 0) { alert('All billings already voided on this bill.'); return; }
1002
1003                         var sum = 0;
1004                         for (var i = 0; i < mb_list.length; i++) sum += util.money.dollars_float_to_cents_integer( mb_list[i].amount() );
1005                         sum = util.money.cents_as_dollars( sum );
1006
1007                         var msg = 'Are you sure you would like to void $' + sum + ' worth of line-item billings?';
1008                         var r = obj.error.yns_alert(msg,'Voiding Bills','Yes','No',null,'Check here to confirm this message');
1009                         if (r == 0) {
1010                                 var robj = obj.network.simple_request('FM_MB_VOID',[ses()].concat(util.functional.map_list(mb_list,function(o){return o.id();})));
1011                                 if (robj.ilsevent) {
1012                                         switch(robj.ilsevent) {
1013                                                 default: 
1014                                                         obj.error.standard_unexpected_error_alert('Error voiding bills.',robj); 
1015                                                         obj.refresh(); return; 
1016                                                 break;
1017                                         }
1018                                 }
1019
1020                                 obj.data.stash_retrieve(); if (! obj.data.voided_billings ) obj.data.voided_billings = []; 
1021                                 for (var i = 0; i < mb_list.length; i++) {
1022                                                 obj.data.voided_billings.push( mb_list[i] );
1023                                 }
1024                                 obj.data.stash('voided_billings');
1025                                 alert('Billings voided.');
1026                                 obj.refresh();
1027                         }
1028                 } catch(E) {
1029                         try { obj.error.standard_unexpected_error_alert('bills.js, void_all_billings():',E); } catch(F) { alert(E); }
1030                 }
1031
1032         },
1033         
1034         'gen_map_row_to_column' : function() {
1035                 var obj = this;
1036
1037                 try {
1038
1039                 return function(row,col) {
1040                         // row contains { 'my' : { 'mobts' : ... } }
1041                         // col contains one of the objects listed above in columns
1042
1043                         var my = row.my;
1044                         var value;
1045                         try {
1046                                 value = eval( col.render );
1047                         } catch(E) {
1048                                 try{obj.error.sdump('D_ERROR','map_row_to_column: ' + E);}
1049                                 catch(P){dump('?map_row_to_column: ' + E + '\n');}
1050                                 value = '???';
1051                         }
1052                         //dump('map_row_to_column: value = ' + value + '\n');
1053                         return value;
1054                 };
1055
1056                 } catch(E) {
1057                         obj.error.standard_unexpected_error_alert('bills -> gen_map_row_to_column',E);  
1058                 }
1059         },
1060
1061 }
1062
1063 dump('exiting patron.bills.js\n');