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