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