]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/bills.js
gather credit card/check info. Use stash for getting data from a modal window
[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                 alert('payment_blob = ' + payment_blob);
262                 var obj = this;
263                 try {
264                         var robj = obj.network.request(
265                                 api.bill_pay.app,       
266                                 api.bill_pay.method,
267                                 [ obj.session, payment_blob ]
268                         );
269                         if (robj && robj.ilsevent && robj.ilsevent == 0) {
270                                 return true;
271                         } else if (robj == 1) {
272                                 return true;
273                         } else {
274                                 throw robj;
275                         }
276                 } catch(E) {
277                         var error = 'patron.bills.pay: ' + js2JSON(E);
278                         obj.error.sdump('D_ERROR',error);
279                         alert(error);
280                         return false;
281                 }
282         },
283
284         'update_payment_applied' : function() {
285                 JSAN.use('util.money');
286                 var obj = this;
287                 var total_applied = 0;
288                 for (var i = 0; i < obj.current_payments.length; i++) {
289                         total_applied += util.money.dollars_float_to_cents_integer( obj.current_payments[ i ].textbox.value );
290                 }
291                 var total_payment = 0;
292                 if (obj.controller.view.bill_payment_amount.value) {
293                         try {
294                                 total_payment = util.money.dollars_float_to_cents_integer( obj.controller.view.bill_payment_amount.value );
295                         } catch(E) {
296                                 obj.error.sdump('D_ERROR',E + '\n');
297                         }
298                 }
299                 if ( total_applied > total_payment ) {
300                         total_payment = total_applied;
301                         obj.controller.view.bill_payment_amount.value = util.money.cents_as_dollars( total_applied );
302                 }
303                 obj.controller.view.bill_payment_applied.value = util.money.cents_as_dollars( total_applied );
304                 obj.controller.view.bill_payment_applied.setAttribute('value', obj.controller.view.bill_payment_applied.value )
305                 obj.controller.view.bill_credit_amount.value = '';
306                 if (total_payment > total_applied ) {
307                         obj.controller.view.bill_change_amount.value = util.money.cents_as_dollars( total_payment - total_applied);
308                         obj.controller.view.bill_credit_amount.value = '0.00';
309                 } else {
310                         obj.controller.view.bill_change_amount.value = '0.00';
311                         obj.controller.view.bill_credit_amount.value = '0.00';
312                 }
313                 var total_owed = util.money.dollars_float_to_cents_integer( obj.controller.view.bill_total_owed.value );
314                 obj.controller.view.bill_new_balance.value = util.money.cents_as_dollars( total_owed - total_applied );
315         },
316
317         'change_to_credit' : function() {
318                 JSAN.use('util.money');
319                 var obj = this;
320                 var tb = obj.controller.view.bill_change_amount;
321                 var proposed_change = 0;
322                 var proposed_credit = util.money.dollars_float_to_cents_integer( tb.value );
323                 obj.update_payment_applied();
324                 var real_change = util.money.dollars_float_to_cents_integer( tb.value );
325                 if ( proposed_change > real_change ) {
326                         obj.error.sdump('D_ERROR','Someone wanted more money than they deserved\n');
327                         proposed_change = real_change;
328                 } else if ( real_change > proposed_change ) {
329                         proposed_credit = real_change - proposed_change;
330                 }
331                 tb.value = util.money.cents_as_dollars( proposed_change );
332                 tb.setAttribute('value',tb.value);
333                 obj.controller.view.bill_credit_amount.value = util.money.cents_as_dollars( proposed_credit );
334                 obj.controller.view.bill_credit_amount.setAttribute('value',obj.controller.view.bill_credit_amount.value);
335         },
336
337         'retrieve' : function() {
338                 var obj = this;
339                 if (typeof window.bills != 'undefined') {
340                         obj.bills = window.bills;
341                 } else {
342                         obj.bills = obj.network.request(
343                                 api.fm_mobts_having_balance.app,
344                                 api.fm_mobts_having_balance.method,
345                                 [ obj.session, obj.patron_id ]
346                         );
347                 }
348         },
349
350         'xact_dates_box' : function ( mobts ) {
351                 var obj = this;
352                 function getString(s) { return obj.OpenILS.data.entities[s]; }
353                 var grid = document.createElement('grid');
354                         var cols = document.createElement('columns');
355                         grid.appendChild( cols );
356                                 cols.appendChild( document.createElement('column') );
357                                 cols.appendChild( document.createElement('column') );
358                         var rows = document.createElement('rows');
359                         grid.appendChild( rows );
360                                 var row0 = document.createElement('row');
361                                 rows.appendChild( row0 );
362                                         var cb_r0_0 = document.createElement('checkbox');
363                                         row0.appendChild( cb_r0_0 );
364                                         cb_r0_0.setAttribute('checked','true');
365                                         var hb_r0_1 = document.createElement('hbox');
366                                         row0.appendChild( hb_r0_1 );
367                                                 var label_r0_1 = document.createElement('label');
368                                                 hb_r0_1.appendChild( label_r0_1 );
369                                                 label_r0_1.setAttribute('value',getString('staff.mbts_id_label'));
370                                                 var label_r0_2 = document.createElement('label');
371                                                 hb_r0_1.appendChild( label_r0_2 );
372                                                 label_r0_2.setAttribute('value',mobts.id());
373                                 var row1 = document.createElement('row');
374                                 rows.appendChild( row1 );
375                                         var label_r1_1 = document.createElement('label');
376                                         row1.appendChild( label_r1_1 );
377                                         label_r1_1.setAttribute('value',getString('staff.mbts_xact_start_label'));
378                                         var label_r1_2 = document.createElement('label');
379                                         row1.appendChild( label_r1_2 );
380                                         label_r1_2.setAttribute('value',mobts.xact_start().toString().substr(0,10));
381                                 var row2 = document.createElement('row');
382                                 rows.appendChild( row2 );
383                                         var label_r2_1 = document.createElement('label');
384                                         row2.appendChild( label_r2_1 );
385                                         label_r2_1.setAttribute('value',getString('staff.mbts_xact_finish_label'));
386                                         var label_r2_2 = document.createElement('label');
387                                         row2.appendChild( label_r2_2 );
388                                         try { label_r2_2.setAttribute('value',mobts.xact_finish().toString().substr(0,10));
389                                         } catch(E) {}
390
391                 return grid;
392         },
393
394         'money_box' : function ( mobts ) {
395                 var obj = this;
396                 function getString(s) { return obj.OpenILS.data.entities[s]; }
397                 var grid = document.createElement('grid');
398                         var cols = document.createElement('columns');
399                         grid.appendChild( cols );
400                                 cols.appendChild( document.createElement('column') );
401                                 cols.appendChild( document.createElement('column') );
402                         var rows = document.createElement('rows');
403                         grid.appendChild( rows );
404                                 var row1 = document.createElement('row');
405                                 rows.appendChild( row1 );
406                                         var label_r1_1 = document.createElement('label');
407                                         row1.appendChild( label_r1_1 );
408                                         label_r1_1.setAttribute('value',getString('staff.mbts_total_owed_label'));
409                                         var label_r1_2 = document.createElement('label');
410                                         row1.appendChild( label_r1_2 );
411                                         label_r1_2.setAttribute('value',mobts.total_owed());
412                                 var row2 = document.createElement('row');
413                                 rows.appendChild( row2 );
414                                         var label_r2_1 = document.createElement('label');
415                                         row2.appendChild( label_r2_1 );
416                                         label_r2_1.setAttribute('value',getString('staff.mbts_total_paid_label'));
417                                         var label_r2_2 = document.createElement('label');
418                                         row2.appendChild( label_r2_2 );
419                                         label_r2_2.setAttribute('value',mobts.total_paid());
420                                 var row3 = document.createElement('row');
421                                 rows.appendChild( row3 );
422                                         var label_r3_1 = document.createElement('label');
423                                         row3.appendChild( label_r3_1 );
424                                         label_r3_1.setAttribute('value',getString('staff.mbts_balance_owed_label'));
425                                         label_r3_1.setAttribute('style','font-weight: bold');
426                                         var label_r3_2 = document.createElement('label');
427                                         row3.appendChild( label_r3_2 );
428                                         label_r3_2.setAttribute('value',mobts.balance_owed());
429                                         label_r3_2.setAttribute('style','font-weight: bold');
430
431                 return grid;
432         },
433
434         'info_box' : function ( mobts ) {
435                 var obj = this;
436                 function getString(s) { return obj.OpenILS.data.entities[s]; }
437                 var vbox = document.createElement('vbox');
438                         var grid = document.createElement('grid');
439                                 vbox.appendChild( grid );
440
441                                 var cols = document.createElement('columns');
442                                         grid.appendChild( cols );
443                                         cols.appendChild( document.createElement('column') );
444                                         cols.appendChild( document.createElement('column') );
445                                 var rows = document.createElement('rows');
446                                         grid.appendChild( rows );
447
448                         var xact_type = document.createElement('row');
449                         rows.appendChild( xact_type );
450
451                                 var xt_label = document.createElement('label');
452                                         xact_type.appendChild( xt_label );
453                                         xt_label.setAttribute( 'value', 'Type' );
454                                 var xt_value = document.createElement('label');
455                                         xact_type.appendChild( xt_value );
456                                         xt_value.setAttribute( 'value', mobts.xact_type() );
457
458                         var last_billing = document.createElement('row');
459                         rows.appendChild( last_billing );
460
461                                 var lb_label = document.createElement('label');
462                                         last_billing.appendChild( lb_label );
463                                         lb_label.setAttribute( 'value', 'Last Billing:' );
464
465                                 var lb_value = document.createElement('label');
466                                         last_billing.appendChild( lb_value );
467                                         if (mobts.last_billing_type()) 
468                                                 lb_value.setAttribute( 'value', mobts.last_billing_type() );
469
470                         var last_payment = document.createElement('row');
471                         rows.appendChild( last_payment );
472
473                                 var lp_label = document.createElement('label');
474                                         last_payment.appendChild( lp_label );
475                                         lp_label.setAttribute( 'value', 'Last Payment:' );
476
477                                 var lp_value = document.createElement('label');
478                                         last_payment.appendChild( lp_value );
479                                         if (mobts.last_payment_type()) 
480                                                 lp_value.setAttribute( 'value', mobts.last_payment_type() );
481
482                         var btn = document.createElement('button');
483                                 vbox.appendChild( btn );
484                                 btn.setAttribute( 'label', 'Full Details' );
485                                 btn.setAttribute( 'name', 'full_details' );
486                                 btn.setAttribute( 'mobts_id', mobts.id() );     
487                                 btn.addEventListener(
488                                         'command',
489                                         function(ev) {
490                                                 JSAN.use('util.window'); var w = new util.window();
491                                                 w.open(
492                                                         urls.remote_patron_bill_details 
493                                                         + '?session=' + window.escape(obj.session) 
494                                                         + '&mbts_id=' + window.escape(mobts.id()),
495                                                         'test' + mobts.id(),
496                                                         'modal,chrome,resizable'
497                                                 );
498                                         },
499                                         false
500                                 );
501
502                 return vbox;
503         },
504
505         'payment_box' : function() {
506                 var vb = document.createElement('vbox');
507                 var tb = document.createElement('textbox');
508                 tb.setAttribute('readonly','true');
509                 vb.appendChild(tb);
510                 return vb;
511         },
512         
513         'gen_map_row_to_column' : function() {
514                 var obj = this;
515
516                 return function(row,col) {
517                         // row contains { 'my' : { 'mobts' : ... } }
518                         // col contains one of the objects listed above in columns
519
520                         var my = row.my;
521                         var value;
522                         try {
523                                 value = eval( col.render );
524                         } catch(E) {
525                                 try{obj.error.sdump('D_ERROR','map_row_to_column: ' + E);}
526                                 catch(P){dump('?map_row_to_column: ' + E + '\n');}
527                                 value = '???';
528                         }
529                         dump('map_row_to_column: value = ' + value + '\n');
530                         return value;
531                 };
532         },
533
534 }
535
536 dump('exiting patron.bills.js\n');