6bee1314fe7ccd9e8685cebfb7131973018f1889
[Evergreen.git] / Evergreen / staff_client / chrome / content / evergreen / patron / patron_bills.js
1 sdump('D_TRACE','Loading patron_bills.js\n');
2
3 function patron_bills_init(p) {
4         sdump('D_PATRON_BILLS',"TESTING: patron_bills.js: " + mw.G['main_test_variable'] + '\n');
5         sdump('D_CONSTRUCTOR',arg_dump(arguments));
6
7         patron_bills_list_box_init( p );
8
9         patron_bills_control_box_init( p );
10
11         p.current_payments = [];
12
13         p.update_payment_applied = function () {
14                 sdump('D_PATRON_BILLS','p.update_payment_applied()\n');
15                 var total_applied = 0;
16                 for (var i = 0; i < p.current_payments.length; i++) {
17                         total_applied += dollars_float_to_cents_integer( p.current_payments[ i ].textbox.value );
18                 }
19                 var total_payment = 0;
20                 if (p.control_box.bill_payment_amount.value) {
21                         try {
22                                 total_payment = dollars_float_to_cents_integer( p.control_box.bill_payment_amount.value );
23                         } catch(E) {
24                                 sdump('D_ERROR',E + '\n');
25                         }
26                 }
27                 if ( total_applied > total_payment ) {
28                         total_payment = total_applied;
29                         p.control_box.bill_payment_amount.value = cents_as_dollars( total_applied );
30                 }
31                 p.control_box.bill_payment_applied.setAttribute('value', cents_as_dollars( total_applied ));
32                 p.control_box.bill_payment_applied.value = cents_as_dollars( total_applied );
33                 p.control_box.bill_credit_amount.value = '';
34                 if (total_payment > total_applied ) {
35                         p.control_box.bill_change_amount.value = cents_as_dollars( total_payment - total_applied);
36                         p.control_box.bill_credit_amount.value = '0.00';
37                 } else {
38                         p.control_box.bill_change_amount.value = '0.00';
39                         p.control_box.bill_credit_amount.value = '0.00';
40                 }
41                 var total_owed = dollars_float_to_cents_integer( p.control_box.bill_total_owed.value );
42                 p.control_box.bill_new_balance.value = cents_as_dollars( total_owed - total_applied );
43         }
44
45         p.list_box.apply_to_each_listitem = function (idx, listitem) {
46                 sdump('D_PATRON_BILLS','p.list_box.apply_to_each_listitem()\n');
47                 p.current_payments[ idx ] = {};
48                 p.current_payments[ idx ].listitem = listitem;
49                 p.current_payments[ idx ].checkbox = listitem.getElementsByTagName('checkbox')[0];
50                 p.current_payments[ idx ].textbox = listitem.getElementsByTagName('textbox')[0];
51                 p.current_payments[ idx ].mbts_id = listitem.getAttribute('record_id');
52                 p.current_payments[ idx ].balance_owed = listitem.getAttribute('balance_owed');
53
54                 p.current_payments[ idx ].textbox.addEventListener(
55                         'change',
56                         function () {
57                                 sdump('D_PATRON_BILLS','listitem textbox onchange handler()\n');
58                                 var tb = p.current_payments[ idx ].textbox;
59                                 var bo = p.current_payments[ idx ].balance_owed;
60                                 tb.value = cents_as_dollars( dollars_float_to_cents_integer( tb.value ) ); // show user what we think the number is
61                                 sdump('D_PATRON_BILLS','bo = ' + bo + '\ntb.value = ' + tb.value + '\n');
62                                 if ( dollars_float_to_cents_integer( tb.value ) > dollars_float_to_cents_integer( bo ) ) {
63                                         sdump('D_PATRON_BILLS','Tried to overpay bill\n');
64                                         tb.value = bo;
65                                 }
66                                 p.update_payment_applied();
67                         },
68                         false
69                 );
70         }
71
72         p.control_box.bill_payment_amount.addEventListener(
73                 'change',
74                 function () {
75                         var tb = p.control_box.bill_payment_amount;
76                         tb.value = cents_as_dollars( dollars_float_to_cents_integer( tb.value ) );
77                         var total = dollars_float_to_cents_integer( tb.value );
78                         for (var i = 0; i < p.current_payments.length; i++) {
79                                 var bill = p.current_payments[i];
80                                 if (bill.checkbox.checked) {
81                                         var bo = dollars_float_to_cents_integer( bill.balance_owed );
82                                         if ( bo > total ) {
83                                                 bill.textbox.value = cents_as_dollars( total );
84                                                 total = 0;
85                                         } else {
86                                                 bill.textbox.value = cents_as_dollars( bo );
87                                                 total = total - bo;
88                                         }
89                                 } else {
90                                         bill.textbox.value = '0.00';
91                                 }
92                         }
93                         p.update_payment_applied();
94                 },
95                 false
96         );
97
98         p.control_box.bill_change_amount.addEventListener(
99                 'change',
100                 function() {
101                         var tb = p.control_box.bill_change_amount;
102                         var proposed_change = dollars_float_to_cents_integer( tb.value );
103                         var proposed_credit = 0;
104                         p.update_payment_applied();
105                         var real_change = dollars_float_to_cents_integer( tb.value );
106                         if ( proposed_change > real_change ) {
107                                 sdump('D_ERROR','Someone wanted more money than they deserved\n');
108                                 proposed_change = real_change;
109                         } else if ( real_change > proposed_change ) {
110                                 proposed_credit = real_change - proposed_change;
111                         }
112                         tb.value = cents_as_dollars( proposed_change );
113                         p.control_box.bill_credit_amount.value = cents_as_dollars( proposed_credit );
114                 },
115                 false
116         );
117
118         p.control_box.change_to_credit.addEventListener(
119                 'command',
120                 function() {
121                         var tb = p.control_box.bill_change_amount;
122                         var proposed_change = 0;
123                         var proposed_credit = dollars_float_to_cents_integer( tb.value );
124                         p.update_payment_applied();
125                         var real_change = dollars_float_to_cents_integer( tb.value );
126                         if ( proposed_change > real_change ) {
127                                 sdump('D_ERROR','Someone wanted more money than they deserved\n');
128                                 proposed_change = real_change;
129                         } else if ( real_change > proposed_change ) {
130                                 proposed_credit = real_change - proposed_change;
131                         }
132                         tb.value = cents_as_dollars( proposed_change );
133                         p.control_box.bill_credit_amount.value = cents_as_dollars( proposed_credit );
134
135                 },
136                 false
137         );
138
139         p.control_box.bill_apply_payment.addEventListener(
140                 'command',
141                 function() { 
142                         var payment_blob = {};
143                         payment_blob.cash_drawer = 1; // FIXME: get new Config() to work
144                         payment_blob.payment_type = p.control_box.payment_type.value;
145                         payment_blob.payments = [];
146                         payment_blob.patron_credit = p.control_box.bill_credit_amount.value;
147                         for (var i = 0; i < p.current_payments.length; i++) {
148                                 var tb = p.current_payments[ i ].textbox;
149                                 if ( !(tb.value == '0.00' || tb.value == '') ) {
150                                         payment_blob.payments.push( 
151                                                 [
152                                                         p.current_payments[ i ].mbts_id,
153                                                         tb.value
154                                                 ]
155                                         );
156                                 }
157                         }
158                         try {
159                                 if ( patron_pay_bills( payment_blob ) ) {
160
161                                         if (p.refresh) p.refresh();
162
163                                 }
164
165                         } catch(E) {
166
167                                 handle_error(E);
168                         }
169                 },
170                 false
171         );
172
173         sdump('D_TRACE_EXIT',arg_dump(arguments));
174         return p;
175 }
176
177 function patron_bills_control_box_init( p ) {
178         p.control_box = {};
179         p.control_box.node = p.node.previousSibling;
180         p.control_box.node2 = p.node.nextSibling;
181         p.control_box.bill_total_owed = p.control_box.node.getElementsByAttribute('id','bill_total_owed')[0];
182         p.control_box.payment_type = p.control_box.node.getElementsByAttribute('id','payment_type_menulist')[0];
183         p.control_box.bill_payment_amount = p.control_box.node.getElementsByAttribute('id','bill_payment_amount_textbox')[0];
184         p.control_box.bill_payment_applied = p.control_box.node.getElementsByAttribute('id','bill_payment_applied_textbox')[0];
185         p.control_box.bill_change_amount = p.control_box.node.getElementsByAttribute('id','bill_change_amount_textbox')[0];
186         p.control_box.bill_credit_amount = p.control_box.node.getElementsByAttribute('id','bill_credit_amount_textbox')[0];
187         p.control_box.change_to_credit = p.control_box.node.getElementsByAttribute('id','change_to_credit')[0];
188         p.control_box.bill_apply_payment = p.control_box.node.getElementsByAttribute('id','bill_apply_payment')[0];
189         p.control_box.bill_new_balance = p.control_box.node.getElementsByAttribute('id','bill_new_balance_textbox')[0];
190 }
191
192 function patron_bills_list_box_init( p ) {
193         p.patron_bills_cols = [
194                 {
195                         'id' : 'checkbox', 'label' : '', 'flex' : 0, 'primary' : false, 'hidden' : false,
196                         'render_xul' : 'checkbox'
197                 },
198                 {
199                         'id' : 'xact_dates', 'label' : getString('bills_xact_dates_label'), 'flex' : 0,
200                         'primary' : false, 'hidden' : false, 'fm_class' : 'mbts', 
201                         'fm_field_render' : 'xact_dates_box($$)'
202                 },
203                 {
204                         'id' : 'notes', 'label' : getString('bills_information'), 'flex' : 1,
205                         'primary' : false, 'hidden' : false, 'fm_class' : 'mbts',
206                         'fm_field_render' : '.last_billing_note()'
207                 },
208                 {
209                         'id' : 'money', 'label' : getString('bills_money_label'), 'flex' : 0,
210                         'primary' : false, 'hidden' : false, 'fm_class' : 'mbts', 
211                         'fm_field_render' : 'money_box($$)'
212                 },
213                 {
214                         'id' : 'current_pay', 'label' : getString('bills_current_payment_label'), 'flex' : 0, 
215                         'render_xul' : 'textbox'
216                 }
217         ];
218
219         p.list_box = list_box_init( { 'w' : p.w, 'node' : p.node, 'cols' : p.patron_bills_cols, 'debug' : p.app } );
220         p.clear_patron_bills = function () { 
221                 p.current_payments = []; 
222                 p.control_box.bill_total_owed.setAttribute('value', 'Calculating...');
223                 p.control_box.bill_total_owed.value = 'Calculating...';
224                 p.control_box.bill_payment_amount.setAttribute('value', '');
225                 p.control_box.bill_payment_amount.value = '';
226                 p.control_box.bill_payment_applied.setAttribute('value', '0.00');
227                 p.control_box.bill_payment_applied.value = '0.00';
228                 p.control_box.bill_change_amount.setAttribute('value', '0.00');
229                 p.control_box.bill_change_amount.value = '0.00';
230                 p.control_box.bill_credit_amount.setAttribute('value', '0.00');
231                 p.control_box.bill_credit_amount.value = '0.00';
232                 p.control_box.bill_new_balance.setAttribute('value', 'Calculating...');
233                 p.control_box.bill_new_balance.value = 'Calculating...';
234                 p.list_box.clear_rows(); 
235         };
236         p.add_patron_bills = function (bills) {
237                 sdump('D_PATRON_BILLS','p.add_patron_bills(' + bills + ')\n');
238                 return patron_bills_add_patron_bills(p,bills);
239         }
240 }
241
242 function patron_bills_add_patron_bills(p, bills) {
243         sdump('D_PATRON_BILLS',arg_dump(arguments,{1:true}));
244
245         p.control_box.bill_total_owed.setAttribute('value',get_bills_total( bills ));
246         p.control_box.bill_total_owed.value = get_bills_total( bills );
247         p.control_box.bill_new_balance.setAttribute('value',get_bills_total( bills ));
248         p.control_box.bill_new_balance.value = get_bills_total( bills );
249
250         function xact_dates_box( mbts ) {
251                 var grid = p.w.document.createElement('grid');
252                         var cols = p.w.document.createElement('columns');
253                         grid.appendChild( cols );
254                                 cols.appendChild( p.w.document.createElement('column') );
255                                 cols.appendChild( p.w.document.createElement('column') );
256                         var rows = p.w.document.createElement('rows');
257                         grid.appendChild( rows );
258                                 var row0 = p.w.document.createElement('row');
259                                 rows.appendChild( row0 );
260                                         var label_r0_1 = p.w.document.createElement('label');
261                                         row0.appendChild( label_r0_1 );
262                                         label_r0_1.setAttribute('value',getString('mbts_id_label'));
263                                         var label_r0_2 = p.w.document.createElement('label');
264                                         row0.appendChild( label_r0_2 );
265                                         label_r0_2.setAttribute('value',mbts.id());
266                                 var row1 = p.w.document.createElement('row');
267                                 rows.appendChild( row1 );
268                                         var label_r1_1 = p.w.document.createElement('label');
269                                         row1.appendChild( label_r1_1 );
270                                         label_r1_1.setAttribute('value',getString('mbts_xact_start_label'));
271                                         var label_r1_2 = p.w.document.createElement('label');
272                                         row1.appendChild( label_r1_2 );
273                                         label_r1_2.setAttribute('value',mbts.xact_start().toString().substr(0,10));
274                                 var row2 = p.w.document.createElement('row');
275                                 rows.appendChild( row2 );
276                                         var label_r2_1 = p.w.document.createElement('label');
277                                         row2.appendChild( label_r2_1 );
278                                         label_r2_1.setAttribute('value',getString('mbts_xact_finish_label'));
279                                         var label_r2_2 = p.w.document.createElement('label');
280                                         row2.appendChild( label_r2_2 );
281                                         try { label_r2_2.setAttribute('value',mbts.xact_finish().toString().substr(0,10));
282                                         } catch(E) {}
283
284                 return grid;
285         }
286
287         function money_box( mbts ) {
288                 var grid = p.w.document.createElement('grid');
289                         var cols = p.w.document.createElement('columns');
290                         grid.appendChild( cols );
291                                 cols.appendChild( p.w.document.createElement('column') );
292                                 cols.appendChild( p.w.document.createElement('column') );
293                         var rows = p.w.document.createElement('rows');
294                         grid.appendChild( rows );
295                                 var row1 = p.w.document.createElement('row');
296                                 rows.appendChild( row1 );
297                                         var label_r1_1 = p.w.document.createElement('label');
298                                         row1.appendChild( label_r1_1 );
299                                         label_r1_1.setAttribute('value',getString('mbts_total_owed_label'));
300                                         var label_r1_2 = p.w.document.createElement('label');
301                                         row1.appendChild( label_r1_2 );
302                                         label_r1_2.setAttribute('value',mbts.total_owed());
303                                 var row2 = p.w.document.createElement('row');
304                                 rows.appendChild( row2 );
305                                         var label_r2_1 = p.w.document.createElement('label');
306                                         row2.appendChild( label_r2_1 );
307                                         label_r2_1.setAttribute('value',getString('mbts_total_paid_label'));
308                                         var label_r2_2 = p.w.document.createElement('label');
309                                         row2.appendChild( label_r2_2 );
310                                         label_r2_2.setAttribute('value',mbts.total_paid());
311                                 var row3 = p.w.document.createElement('row');
312                                 rows.appendChild( row3 );
313                                         var label_r3_1 = p.w.document.createElement('label');
314                                         row3.appendChild( label_r3_1 );
315                                         label_r3_1.setAttribute('value',getString('mbts_balance_owed_label'));
316                                         label_r3_1.setAttribute('style','font-weight: bold');
317                                         var label_r3_2 = p.w.document.createElement('label');
318                                         row3.appendChild( label_r3_2 );
319                                         label_r3_2.setAttribute('value',mbts.balance_owed());
320                                         label_r3_2.setAttribute('style','font-weight: bold');
321
322                 return grid;
323         }
324
325         var obj_string ='mbts';
326
327         setTimeout(
328                 function() {
329                         //p.list_box.clear_rows();
330
331                         for (var i = 0; i < bills.length; i++) {
332
333                                 var mbts = bills[i];
334
335                                 var cols = [];
336
337                                 for (var j = 0; j < p.patron_bills_cols.length; j++) {
338                                         var hash = p.patron_bills_cols[j];
339                                         sdump('D_PATRON_BILLS','Considering ' + js2JSON(hash) + '\n');
340                                         var listcell = p.w.document.createElement('listcell');
341                                         listcell.setAttribute('pack','start');
342                                         listcell.setAttribute('align','start');
343                                         listcell.setAttribute('style','border-left: black solid thin');
344                                         var col = '';
345                                         if (hash.fm_field_render) {
346
347                                                 var obj = 'mbts';
348                                                 switch( hash.fm_class ) {
349                                                         case 'mvr' : obj_string = 'mvr'; break;
350                                                 }
351                                                 var cmd = parse_render_string( obj_string, hash.fm_field_render );
352                                                 sdump('D_PATRON_BILLS','cmd = ' + cmd + '\n');
353                                                 try {
354                                                         col = eval( cmd );
355                                                         sdump('D_PATRON_BILLS','eval = ' + col + '\n');
356                                                 } catch(E) {
357                                                         sdump('D_ERROR',E + '\n');
358                                                 }
359                                                 if (typeof(col) == 'string') {
360                                                         listcell.setAttribute('label',col);
361                                                 } else {
362                                                         listcell.appendChild( col );
363                                                 }
364                                         }
365                                         if (hash.render_xul) {
366                                                 var xul = p.w.document.createElement( hash.render_xul );
367                                                 listcell.appendChild( xul );
368                                                 if (hash.render_xul == 'checkbox') xul.setAttribute('checked', 'true');
369                                         }
370                                         cols.push( listcell );
371                                 }
372
373                                 var listitem = p.list_box.add_row( 
374                                         cols, { 
375                                                 'record_id' : mbts.id(),
376                                                 'balance_owed' : mbts.balance_owed()
377                                         } 
378                                 ); 
379                                 if (p.list_box.apply_to_each_listitem) {
380                                         p.list_box.apply_to_each_listitem( i, listitem );
381                                 }
382                         }
383                 }, 0
384         );
385 }