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