a6efe3ceb9b603ecdf5e07020d801652f179625a
[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         } catch(E) { 
12                 alert(E); 
13         }
14 }
15
16 patron.bills.prototype = {
17
18         'version' : 'test123',
19
20         'current_payments' : [],
21
22         'refresh' : function() {
23                 location.href = location.href;
24         },
25
26         'init' : function( params ) {
27
28                 var obj = this;
29
30                 obj.session = obj.session || params['session'];
31                 obj.patron_id = obj.patron_id || params['patron_id'];
32
33                 JSAN.use('util.list'); obj.list = new util.list('bill_list');
34
35                 function getString(s) { return obj.OpenILS.data.entities[s]; }
36                 obj.list.init(
37                         {
38                                 'columns' : [
39                                                 {
40                                                         'id' : 'xact_dates', 'label' : getString('staff.bills_xact_dates_label'), 'flex' : 1,
41                                                         'primary' : false, 'hidden' : false, 'render' : 'obj.xact_dates_box(my.mobts)'
42                                                 },
43                                                 {
44                                                         'id' : 'notes', 'label' : getString('staff.bills_information'), 'flex' : 2,
45                                                         'primary' : false, 'hidden' : false, 'render' : 'obj.info_box(my.mobts)'
46                                                 },
47                                                 {
48                                                         'id' : 'money', 'label' : getString('staff.bills_money_label'), 'flex' : 1,
49                                                         'primary' : false, 'hidden' : false, 'render' : 'obj.money_box(my.mobts)'
50                                                 },
51                                                 {
52                                                         'id' : 'current_pay', 'label' : getString('staff.bills_current_payment_label'), 'flex' : 0, 
53                                                         'render' : 'obj.payment_box()'
54                                                 }
55                                 ],
56                                 'map_row_to_column' : obj.gen_map_row_to_column(),
57                         }
58                 );
59
60                 JSAN.use('util.controller'); obj.controller = new util.controller();
61                 obj.controller.init(
62                         {
63                                 'control_map' : {
64                                         'cmd_broken' : [
65                                                 ['command'],
66                                                 function() { alert('Not Yet Implemented'); }
67                                         ],
68                                         'cmd_bill_wizard' : [
69                                                 ['command'],
70                                                 function() { 
71                                                         try {
72                                                                 JSAN.use('util.window');
73                                                                 var win = new util.window();
74                                                                 var w = win.open(
75                                                                         urls.remote_patron_bill_wizard
76                                                                                 + '?session=' + window.escape(obj.session)
77                                                                                 + '&patron_id=' + window.escape(obj.patron_id),
78                                                                         'billwizard',
79                                                                         'chrome,resizable,modal'
80                                                                 );
81                                                                 if (typeof window.display_refresh == 'function') {
82                                                                         try { window.display_refresh(); } catch(E) { obj.error.sdump('D_ERROR',E); }
83                                                                 }
84                                                                 obj.refresh();
85                                                         } catch(E) {
86                                                                 obj.error.sdump('D_ERROR',E);
87                                                                 alert(E);
88                                                         }
89                                                 }
90                                         ],
91                                         'cmd_change_to_credit' : [
92                                                 ['command'],
93                                                 function() {
94                                                         obj.change_to_credit();
95                                                 }
96                                         ],
97                                         'cmd_bill_apply_payment' : [
98                                                 ['command'],
99                                                 function() {
100                                                         try { obj.apply_payment(); } catch(E) { alert(E); }
101                                                 }
102                                         ],
103                                         'bill_total_owed' : [
104                                                 ['render'],
105                                                 function(e) { return function() {}; }
106                                         ],
107                                         'payment_type' : [
108                                                 ['render'],
109                                                 function(e) { return function() {}; }
110                                         ],
111                                         'bill_payment_amount' : [
112                                                 ['change'],
113                                                 function(ev) {
114                                                         JSAN.use('util.money');
115                                                         var tb = ev.target;
116                                                         tb.value = util.money.cents_as_dollars( util.money.dollars_float_to_cents_integer( tb.value ) );
117                                                         tb.setAttribute('value', tb.value );
118                                                         var total = util.money.dollars_float_to_cents_integer( tb.value );
119                                                         for (var i = 0; i < obj.current_payments.length; i++) {
120                                                                 var bill = obj.current_payments[i];
121                                                                 if (bill.checkbox.checked) {
122                                                                         var bo = util.money.dollars_float_to_cents_integer( bill.balance_owed );
123                                                                         if ( bo > total ) {
124                                                                                 bill.textbox.value = util.money.cents_as_dollars( total );
125                                                                                 total = 0;
126                                                                         } else {
127                                                                                 bill.textbox.value = util.money.cents_as_dollars( bo );
128                                                                                 total = total - bo;
129                                                                         }
130                                                                 } else {
131                                                                         bill.textbox.value = '0.00';
132                                                                 }
133                                                                 bill.textbox.setAttribute('value',bill.textbox.value);
134                                                         }
135                                                         obj.update_payment_applied();
136                                                 } 
137                                         ],
138                                         'bill_payment_applied' : [
139                                                 ['render'],
140                                                 function(e) { return function() {}; }
141                                         ],
142                                         'bill_change_amount' : [
143                                                 ['change'],
144                                                 function(ev) {
145                                                         JSAN.use('util.money');
146                                                         var tb = ev.target;
147                                                         var proposed_change = util.money.dollars_float_to_cents_integer( tb.value );
148                                                         var proposed_credit = 0;
149                                                         obj.update_payment_applied();
150                                                         var real_change = util.money.dollars_float_to_cents_integer( tb.value );
151                                                         if ( proposed_change > real_change ) {
152                                                                 obj.error.sdump('D_ERROR','Someone wanted more money than they deserved\n');
153                                                                 proposed_change = real_change;
154                                                         } else if ( real_change > proposed_change ) {
155                                                                 proposed_credit = real_change - proposed_change;
156                                                         }
157                                                         tb.value = util.money.cents_as_dollars( proposed_change );
158                                                         tb.setAttribute('value',tb.value);
159                                                         obj.controller.view.bill_credit_amount.value = util.money.cents_as_dollars( proposed_credit );
160                                                         obj.controller.view.bill_credit_amount.setAttribute(
161                                                                 'value',
162                                                                 obj.controller.view.bill_credit_amount.value
163                                                         );
164                                                 }
165                                         ],
166                                         'bill_credit_amount' : [
167                                                 ['render'],
168                                                 function(e) { return function() {}; }
169                                         ],
170                                         'bill_new_balance' : [
171                                                 ['render'],
172                                                 function(e) { return function() {}; }
173                                         ],
174                                 }
175                         }
176                 );
177
178                 obj.retrieve();
179
180                 var total_owed = 0;
181
182                 JSAN.use('util.money');
183
184                 obj.current_payments = [];
185                 //FIXME//.bills virtual field
186                 for (var i = 0; i < obj.bills.length; i++) {
187                         var rnode = obj.list.append( { 'row' : { 'my' : { 'mobts' : obj.bills[i] } }, 'attributes' : { 'allowevents' : true } } );
188                         var cb = rnode.getElementsByTagName('checkbox')[0];
189                         var tb = rnode.getElementsByTagName('textbox')[0];
190                         var bo = obj.bills[i].balance_owed();
191                         total_owed += util.money.dollars_float_to_cents_integer( bo );
192                         var id = obj.bills[i].id();
193                         obj.current_payments.push( { 'mobts_id' : id, 'balance_owed' : bo, 'checkbox' : cb, 'textbox' : tb, } );
194                 }
195                 obj.controller.view.bill_total_owed.value = util.money.cents_as_dollars( total_owed );
196                 obj.controller.view.bill_total_owed.setAttribute('value',obj.controller.view.bill_total_owed.value);
197         },
198
199         /*****************************************************************************************************************************/
200
201         'apply_payment' : function() {
202                 var obj = this;
203                 var payment_blob = {};
204                 JSAN.use('util.window');
205                 var win = new util.window();
206                 switch(obj.controller.view.payment_type.value) {
207                         case 'credit_card_payment' :
208                                 var w = win.open(
209                                         urls.remote_patron_bill_cc_info,
210                                         'billccinfo',
211                                         'chrome,resizable,modal'
212                                 );
213                                 obj.OpenILS.data.stash_retrieve();
214                                 payment_blob = JSON2js( obj.OpenILS.data.temp );
215                         break;
216                         case 'check_payment' :
217                                 var w = win.open(
218                                         urls.remote_patron_bill_check_info,
219                                         'billccinfo',
220                                         'chrome,resizable,modal'
221                                 );
222                                 obj.OpenILS.data.stash_retrieve();
223                                 payment_blob = JSON2js( obj.OpenILS.data.temp );
224                         break;
225                 }
226                 if (payment_blob.cancelled == 'true') { alert('cancelled'); return; }
227                 payment_blob.userid = obj.patron_id;
228                 payment_blob.note = payment_blob.note || '';
229                 payment_blob.cash_drawer = 1; // FIXME: get new Config() to work
230                 payment_blob.payment_type = obj.controller.view.payment_type.value;
231                 payment_blob.payments = [];
232                 payment_blob.patron_credit = obj.controller.view.bill_credit_amount.value;
233                 for (var i = 0; i < obj.current_payments.length; i++) {
234                         var tb = obj.current_payments[ i ].textbox;
235                         if ( !(tb.value == '0.00' || tb.value == '') ) {
236                                 payment_blob.payments.push( 
237                                         [
238                                                 obj.current_payments[ i ].mobts_id,
239                                                 tb.value
240                                         ]
241                                 );
242                         }
243                 }
244                 try {
245                         if ( obj.pay( payment_blob ) ) {
246
247                                 obj.refresh();
248
249                         } else {
250
251                                 alert('oops');
252                         }
253
254                 } catch(E) {
255
256                         obj.error.sdump('D_ERROR',E);
257                 }
258         },
259
260         'pay' : function(payment_blob) {
261                 var obj = this;
262                 try {
263                         var robj = obj.network.request(
264                                 api.bill_pay.app,       
265                                 api.bill_pay.method,
266                                 [ obj.session, payment_blob ]
267                         );
268                         if (robj && robj.ilsevent && robj.ilsevent == 0) {
269                                 return true;
270                         } else if (robj == 1) {
271                                 return true;
272                         } else {
273                                 throw robj;
274                         }
275                 } catch(E) {
276                         var error = 'patron.bills.pay: ' + js2JSON(E);
277                         obj.error.sdump('D_ERROR',error);
278                         alert(error);
279                         return false;
280                 }
281         },
282
283         'update_payment_applied' : function() {
284                 JSAN.use('util.money');
285                 var obj = this;
286                 var total_applied = 0;
287                 for (var i = 0; i < obj.current_payments.length; i++) {
288                         total_applied += util.money.dollars_float_to_cents_integer( obj.current_payments[ i ].textbox.value );
289                 }
290                 var total_payment = 0;
291                 if (obj.controller.view.bill_payment_amount.value) {
292                         try {
293                                 total_payment = util.money.dollars_float_to_cents_integer( obj.controller.view.bill_payment_amount.value );
294                         } catch(E) {
295                                 obj.error.sdump('D_ERROR',E + '\n');
296                         }
297                 }
298                 if ( total_applied > total_payment ) {
299                         total_payment = total_applied;
300                         obj.controller.view.bill_payment_amount.value = util.money.cents_as_dollars( total_applied );
301                 }
302                 obj.controller.view.bill_payment_applied.value = util.money.cents_as_dollars( total_applied );
303                 obj.controller.view.bill_payment_applied.setAttribute('value', obj.controller.view.bill_payment_applied.value )
304                 obj.controller.view.bill_credit_amount.value = '';
305                 if (total_payment > total_applied ) {
306                         obj.controller.view.bill_change_amount.value = util.money.cents_as_dollars( total_payment - total_applied);
307                         obj.controller.view.bill_credit_amount.value = '0.00';
308                 } else {
309                         obj.controller.view.bill_change_amount.value = '0.00';
310                         obj.controller.view.bill_credit_amount.value = '0.00';
311                 }
312                 var total_owed = util.money.dollars_float_to_cents_integer( obj.controller.view.bill_total_owed.value );
313                 obj.controller.view.bill_new_balance.value = util.money.cents_as_dollars( total_owed - total_applied );
314         },
315
316         'change_to_credit' : function() {
317                 JSAN.use('util.money');
318                 var obj = this;
319                 var tb = obj.controller.view.bill_change_amount;
320                 var proposed_change = 0;
321                 var proposed_credit = util.money.dollars_float_to_cents_integer( tb.value );
322                 obj.update_payment_applied();
323                 var real_change = util.money.dollars_float_to_cents_integer( tb.value );
324                 if ( proposed_change > real_change ) {
325                         obj.error.sdump('D_ERROR','Someone wanted more money than they deserved\n');
326                         proposed_change = real_change;
327                 } else if ( real_change > proposed_change ) {
328                         proposed_credit = real_change - proposed_change;
329                 }
330                 tb.value = util.money.cents_as_dollars( proposed_change );
331                 tb.setAttribute('value',tb.value);
332                 obj.controller.view.bill_credit_amount.value = util.money.cents_as_dollars( proposed_credit );
333                 obj.controller.view.bill_credit_amount.setAttribute('value',obj.controller.view.bill_credit_amount.value);
334         },
335
336         'retrieve' : function() {
337                 var obj = this;
338                 if (typeof window.bills != 'undefined') {
339                         obj.bills = window.bills;
340                 } else {
341                         obj.bills = obj.network.request(
342                                 api.fm_mobts_having_balance.app,
343                                 api.fm_mobts_having_balance.method,
344                                 [ obj.session, obj.patron_id ]
345                         );
346                 }
347         },
348
349         'xact_dates_box' : function ( mobts ) {
350                 var obj = this;
351                 function getString(s) { return obj.OpenILS.data.entities[s]; }
352                 var grid = document.createElement('grid');
353                         var cols = document.createElement('columns');
354                         grid.appendChild( cols );
355                                 cols.appendChild( document.createElement('column') );
356                                 cols.appendChild( document.createElement('column') );
357                         var rows = document.createElement('rows');
358                         grid.appendChild( rows );
359                                 var row0 = document.createElement('row');
360                                 rows.appendChild( row0 );
361                                         var cb_r0_0 = document.createElement('checkbox');
362                                         row0.appendChild( cb_r0_0 );
363                                         cb_r0_0.setAttribute('checked','true');
364                                         var hb_r0_1 = document.createElement('hbox');
365                                         row0.appendChild( hb_r0_1 );
366                                                 var label_r0_1 = document.createElement('label');
367                                                 hb_r0_1.appendChild( label_r0_1 );
368                                                 label_r0_1.setAttribute('value',getString('staff.mbts_id_label'));
369                                                 var label_r0_2 = document.createElement('label');
370                                                 hb_r0_1.appendChild( label_r0_2 );
371                                                 label_r0_2.setAttribute('value',mobts.id());
372                                 var row1 = document.createElement('row');
373                                 rows.appendChild( row1 );
374                                         var label_r1_1 = document.createElement('label');
375                                         row1.appendChild( label_r1_1 );
376                                         label_r1_1.setAttribute('value',getString('staff.mbts_xact_start_label'));
377                                         var label_r1_2 = document.createElement('label');
378                                         row1.appendChild( label_r1_2 );
379                                         label_r1_2.setAttribute('value',mobts.xact_start().toString().substr(0,10));
380                                 var row2 = document.createElement('row');
381                                 rows.appendChild( row2 );
382                                         var label_r2_1 = document.createElement('label');
383                                         row2.appendChild( label_r2_1 );
384                                         label_r2_1.setAttribute('value',getString('staff.mbts_xact_finish_label'));
385                                         var label_r2_2 = document.createElement('label');
386                                         row2.appendChild( label_r2_2 );
387                                         try { label_r2_2.setAttribute('value',mobts.xact_finish().toString().substr(0,10));
388                                         } catch(E) {}
389
390                 return grid;
391         },
392
393         'money_box' : function ( mobts ) {
394                 var obj = this;
395                 function getString(s) { return obj.OpenILS.data.entities[s]; }
396                 var grid = document.createElement('grid');
397                         var cols = document.createElement('columns');
398                         grid.appendChild( cols );
399                                 cols.appendChild( document.createElement('column') );
400                                 cols.appendChild( document.createElement('column') );
401                         var rows = document.createElement('rows');
402                         grid.appendChild( rows );
403                                 var row1 = document.createElement('row');
404                                 rows.appendChild( row1 );
405                                         var label_r1_1 = document.createElement('label');
406                                         row1.appendChild( label_r1_1 );
407                                         label_r1_1.setAttribute('value',getString('staff.mbts_total_owed_label'));
408                                         var label_r1_2 = document.createElement('label');
409                                         row1.appendChild( label_r1_2 );
410                                         label_r1_2.setAttribute('value',mobts.total_owed());
411                                 var row2 = document.createElement('row');
412                                 rows.appendChild( row2 );
413                                         var label_r2_1 = document.createElement('label');
414                                         row2.appendChild( label_r2_1 );
415                                         label_r2_1.setAttribute('value',getString('staff.mbts_total_paid_label'));
416                                         var label_r2_2 = document.createElement('label');
417                                         row2.appendChild( label_r2_2 );
418                                         label_r2_2.setAttribute('value',mobts.total_paid());
419                                 var row3 = document.createElement('row');
420                                 rows.appendChild( row3 );
421                                         var label_r3_1 = document.createElement('label');
422                                         row3.appendChild( label_r3_1 );
423                                         label_r3_1.setAttribute('value',getString('staff.mbts_balance_owed_label'));
424                                         label_r3_1.setAttribute('style','font-weight: bold');
425                                         var label_r3_2 = document.createElement('label');
426                                         row3.appendChild( label_r3_2 );
427                                         label_r3_2.setAttribute('value',mobts.balance_owed());
428                                         label_r3_2.setAttribute('style','font-weight: bold');
429
430                 return grid;
431         },
432
433         'info_box' : function ( mobts ) {
434                 var obj = this;
435                 function getString(s) { return obj.OpenILS.data.entities[s]; }
436                 var vbox = document.createElement('vbox');
437                         var grid = document.createElement('grid');
438                                 vbox.appendChild( grid );
439
440                                 var cols = document.createElement('columns');
441                                         grid.appendChild( cols );
442                                         cols.appendChild( document.createElement('column') );
443                                         cols.appendChild( document.createElement('column') );
444                                 var rows = document.createElement('rows');
445                                         grid.appendChild( rows );
446
447                         var xact_type = document.createElement('row');
448                         rows.appendChild( xact_type );
449
450                                 var xt_label = document.createElement('label');
451                                         xact_type.appendChild( xt_label );
452                                         xt_label.setAttribute( 'value', 'Type' );
453                                 var xt_value = document.createElement('label');
454                                         xact_type.appendChild( xt_value );
455                                         xt_value.setAttribute( 'value', mobts.xact_type() );
456
457                         var last_billing = document.createElement('row');
458                         rows.appendChild( last_billing );
459
460                                 var lb_label = document.createElement('label');
461                                         last_billing.appendChild( lb_label );
462                                         lb_label.setAttribute( 'value', 'Last Billing:' );
463
464                                 var lb_value = document.createElement('label');
465                                         last_billing.appendChild( lb_value );
466                                         if (mobts.last_billing_type()) 
467                                                 lb_value.setAttribute( 'value', mobts.last_billing_type() );
468
469                         var last_payment = document.createElement('row');
470                         rows.appendChild( last_payment );
471
472                                 var lp_label = document.createElement('label');
473                                         last_payment.appendChild( lp_label );
474                                         lp_label.setAttribute( 'value', 'Last Payment:' );
475
476                                 var lp_value = document.createElement('label');
477                                         last_payment.appendChild( lp_value );
478                                         if (mobts.last_payment_type()) 
479                                                 lp_value.setAttribute( 'value', mobts.last_payment_type() );
480
481                         var btn = document.createElement('button');
482                                 vbox.appendChild( btn );
483                                 btn.setAttribute( 'label', 'Full Details' );
484                                 btn.setAttribute( 'name', 'full_details' );
485                                 btn.setAttribute( 'mobts_id', mobts.id() );     
486                                 btn.addEventListener(
487                                         'command',
488                                         function(ev) {
489                                                 JSAN.use('util.window'); var w = new util.window();
490                                                 w.open(
491                                                         urls.remote_patron_bill_details 
492                                                         + '?session=' + window.escape(obj.session) 
493                                                         + '&mbts_id=' + window.escape(mobts.id()),
494                                                         'test' + mobts.id(),
495                                                         'modal,chrome,resizable'
496                                                 );
497                                         },
498                                         false
499                                 );
500
501                 return vbox;
502         },
503
504         'payment_box' : function() {
505                 var vb = document.createElement('vbox');
506                 var tb = document.createElement('textbox');
507                 tb.setAttribute('readonly','true');
508                 vb.appendChild(tb);
509                 return vb;
510         },
511         
512         'gen_map_row_to_column' : function() {
513                 var obj = this;
514
515                 return function(row,col) {
516                         // row contains { 'my' : { 'mobts' : ... } }
517                         // col contains one of the objects listed above in columns
518
519                         var my = row.my;
520                         var value;
521                         try {
522                                 value = eval( col.render );
523                         } catch(E) {
524                                 try{obj.error.sdump('D_ERROR','map_row_to_column: ' + E);}
525                                 catch(P){dump('?map_row_to_column: ' + E + '\n');}
526                                 value = '???';
527                         }
528                         dump('map_row_to_column: value = ' + value + '\n');
529                         return value;
530                 };
531         },
532
533 }
534
535 dump('exiting patron.bills.js\n');