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