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