08ddb047ffcd29f1c217f73ffb86017a526fbd61
[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         p.control_box.bill_apply_payment.addEventListener(
174                 'command',
175                 function() {
176                         spawn_bill_wizard(
177                                 p.w.document, 'new_window', '', {}
178                         );
179                 },
180                 false
181         );
182
183         sdump('D_TRACE_EXIT',arg_dump(arguments));
184         return p;
185 }
186
187 function patron_bills_control_box_init( p ) {
188         p.control_box = {};
189         p.control_box.node = p.node.previousSibling;
190         p.control_box.node2 = p.node.nextSibling;
191         p.control_box.bill_wizard = p.control_box.node.getElementsByAttribute('id','bill_wizard')[0];
192         p.control_box.bill_total_owed = p.control_box.node.getElementsByAttribute('id','bill_total_owed')[0];
193         p.control_box.payment_type = p.control_box.node.getElementsByAttribute('id','payment_type_menulist')[0];
194         p.control_box.bill_payment_amount = p.control_box.node.getElementsByAttribute('id','bill_payment_amount_textbox')[0];
195         p.control_box.bill_payment_applied = p.control_box.node.getElementsByAttribute('id','bill_payment_applied_textbox')[0];
196         p.control_box.bill_change_amount = p.control_box.node.getElementsByAttribute('id','bill_change_amount_textbox')[0];
197         p.control_box.bill_credit_amount = p.control_box.node.getElementsByAttribute('id','bill_credit_amount_textbox')[0];
198         p.control_box.change_to_credit = p.control_box.node.getElementsByAttribute('id','change_to_credit')[0];
199         p.control_box.bill_apply_payment = p.control_box.node.getElementsByAttribute('id','bill_apply_payment')[0];
200         p.control_box.bill_new_balance = p.control_box.node.getElementsByAttribute('id','bill_new_balance_textbox')[0];
201 }
202
203 function patron_bills_list_box_init( p ) {
204         p.patron_bills_cols = [
205                 {
206                         'id' : 'checkbox', 'label' : '', 'flex' : 0, 'primary' : false, 'hidden' : false,
207                         'render_xul' : 'checkbox'
208                 },
209                 {
210                         'id' : 'xact_dates', 'label' : getString('bills_xact_dates_label'), 'flex' : 0,
211                         'primary' : false, 'hidden' : false, 'fm_class' : 'mbts', 
212                         'fm_field_render' : 'xact_dates_box($$)'
213                 },
214                 {
215                         'id' : 'notes', 'label' : getString('bills_information'), 'flex' : 1,
216                         'primary' : false, 'hidden' : false, 'fm_class' : 'mbts',
217                         'fm_field_render' : '.last_billing_note()'
218                 },
219                 {
220                         'id' : 'money', 'label' : getString('bills_money_label'), 'flex' : 0,
221                         'primary' : false, 'hidden' : false, 'fm_class' : 'mbts', 
222                         'fm_field_render' : 'money_box($$)'
223                 },
224                 {
225                         'id' : 'current_pay', 'label' : getString('bills_current_payment_label'), 'flex' : 0, 
226                         'render_xul' : 'textbox'
227                 }
228         ];
229
230         p.list_box = list_box_init( { 'w' : p.w, 'node' : p.node, 'cols' : p.patron_bills_cols, 'debug' : p.app } );
231         p.clear_patron_bills = function () { 
232                 p.current_payments = []; 
233                 p.control_box.bill_total_owed.setAttribute('value', 'Calculating...');
234                 p.control_box.bill_total_owed.value = 'Calculating...';
235                 p.control_box.bill_payment_amount.setAttribute('value', '');
236                 p.control_box.bill_payment_amount.value = '';
237                 p.control_box.bill_payment_applied.setAttribute('value', '0.00');
238                 p.control_box.bill_payment_applied.value = '0.00';
239                 p.control_box.bill_change_amount.setAttribute('value', '0.00');
240                 p.control_box.bill_change_amount.value = '0.00';
241                 p.control_box.bill_credit_amount.setAttribute('value', '0.00');
242                 p.control_box.bill_credit_amount.value = '0.00';
243                 p.control_box.bill_new_balance.setAttribute('value', 'Calculating...');
244                 p.control_box.bill_new_balance.value = 'Calculating...';
245                 p.list_box.clear_rows(); 
246         };
247         p.add_patron_bills = function (bills) {
248                 sdump('D_PATRON_BILLS','p.add_patron_bills(' + bills + ')\n');
249                 return patron_bills_add_patron_bills(p,bills);
250         }
251 }
252
253 function patron_bills_add_patron_bills(p, bills) {
254         sdump('D_PATRON_BILLS',arg_dump(arguments,{1:true}));
255
256         p.control_box.bill_total_owed.setAttribute('value',get_bills_total( bills ));
257         p.control_box.bill_total_owed.value = get_bills_total( bills );
258         p.control_box.bill_new_balance.setAttribute('value',get_bills_total( bills ));
259         p.control_box.bill_new_balance.value = get_bills_total( bills );
260
261         function xact_dates_box( mbts ) {
262                 var grid = p.w.document.createElement('grid');
263                         var cols = p.w.document.createElement('columns');
264                         grid.appendChild( cols );
265                                 cols.appendChild( p.w.document.createElement('column') );
266                                 cols.appendChild( p.w.document.createElement('column') );
267                         var rows = p.w.document.createElement('rows');
268                         grid.appendChild( rows );
269                                 var row0 = p.w.document.createElement('row');
270                                 rows.appendChild( row0 );
271                                         var label_r0_1 = p.w.document.createElement('label');
272                                         row0.appendChild( label_r0_1 );
273                                         label_r0_1.setAttribute('value',getString('mbts_id_label'));
274                                         var label_r0_2 = p.w.document.createElement('label');
275                                         row0.appendChild( label_r0_2 );
276                                         label_r0_2.setAttribute('value',mbts.id());
277                                 var row1 = p.w.document.createElement('row');
278                                 rows.appendChild( row1 );
279                                         var label_r1_1 = p.w.document.createElement('label');
280                                         row1.appendChild( label_r1_1 );
281                                         label_r1_1.setAttribute('value',getString('mbts_xact_start_label'));
282                                         var label_r1_2 = p.w.document.createElement('label');
283                                         row1.appendChild( label_r1_2 );
284                                         label_r1_2.setAttribute('value',mbts.xact_start().toString().substr(0,10));
285                                 var row2 = p.w.document.createElement('row');
286                                 rows.appendChild( row2 );
287                                         var label_r2_1 = p.w.document.createElement('label');
288                                         row2.appendChild( label_r2_1 );
289                                         label_r2_1.setAttribute('value',getString('mbts_xact_finish_label'));
290                                         var label_r2_2 = p.w.document.createElement('label');
291                                         row2.appendChild( label_r2_2 );
292                                         try { label_r2_2.setAttribute('value',mbts.xact_finish().toString().substr(0,10));
293                                         } catch(E) {}
294
295                 return grid;
296         }
297
298         function money_box( mbts ) {
299                 var grid = p.w.document.createElement('grid');
300                         var cols = p.w.document.createElement('columns');
301                         grid.appendChild( cols );
302                                 cols.appendChild( p.w.document.createElement('column') );
303                                 cols.appendChild( p.w.document.createElement('column') );
304                         var rows = p.w.document.createElement('rows');
305                         grid.appendChild( rows );
306                                 var row1 = p.w.document.createElement('row');
307                                 rows.appendChild( row1 );
308                                         var label_r1_1 = p.w.document.createElement('label');
309                                         row1.appendChild( label_r1_1 );
310                                         label_r1_1.setAttribute('value',getString('mbts_total_owed_label'));
311                                         var label_r1_2 = p.w.document.createElement('label');
312                                         row1.appendChild( label_r1_2 );
313                                         label_r1_2.setAttribute('value',mbts.total_owed());
314                                 var row2 = p.w.document.createElement('row');
315                                 rows.appendChild( row2 );
316                                         var label_r2_1 = p.w.document.createElement('label');
317                                         row2.appendChild( label_r2_1 );
318                                         label_r2_1.setAttribute('value',getString('mbts_total_paid_label'));
319                                         var label_r2_2 = p.w.document.createElement('label');
320                                         row2.appendChild( label_r2_2 );
321                                         label_r2_2.setAttribute('value',mbts.total_paid());
322                                 var row3 = p.w.document.createElement('row');
323                                 rows.appendChild( row3 );
324                                         var label_r3_1 = p.w.document.createElement('label');
325                                         row3.appendChild( label_r3_1 );
326                                         label_r3_1.setAttribute('value',getString('mbts_balance_owed_label'));
327                                         label_r3_1.setAttribute('style','font-weight: bold');
328                                         var label_r3_2 = p.w.document.createElement('label');
329                                         row3.appendChild( label_r3_2 );
330                                         label_r3_2.setAttribute('value',mbts.balance_owed());
331                                         label_r3_2.setAttribute('style','font-weight: bold');
332
333                 return grid;
334         }
335
336         var obj_string ='mbts';
337
338         setTimeout(
339                 function() {
340                         //p.list_box.clear_rows();
341
342                         for (var i = 0; i < bills.length; i++) {
343
344                                 var mbts = bills[i];
345
346                                 var cols = [];
347
348                                 for (var j = 0; j < p.patron_bills_cols.length; j++) {
349                                         var hash = p.patron_bills_cols[j];
350                                         sdump('D_PATRON_BILLS','Considering ' + js2JSON(hash) + '\n');
351                                         var listcell = p.w.document.createElement('listcell');
352                                         listcell.setAttribute('pack','start');
353                                         listcell.setAttribute('align','start');
354                                         listcell.setAttribute('style','border-left: black solid thin');
355                                         var col = '';
356                                         if (hash.fm_field_render) {
357
358                                                 var obj = 'mbts';
359                                                 switch( hash.fm_class ) {
360                                                         case 'mvr' : obj_string = 'mvr'; break;
361                                                 }
362                                                 var cmd = parse_render_string( obj_string, hash.fm_field_render );
363                                                 sdump('D_PATRON_BILLS','cmd = ' + cmd + '\n');
364                                                 try {
365                                                         col = eval( cmd );
366                                                         sdump('D_PATRON_BILLS','eval = ' + col + '\n');
367                                                 } catch(E) {
368                                                         sdump('D_ERROR',E + '\n');
369                                                 }
370                                                 if (typeof(col) == 'string') {
371                                                         listcell.setAttribute('label',col);
372                                                 } else {
373                                                         listcell.appendChild( col );
374                                                 }
375                                         }
376                                         if (hash.render_xul) {
377                                                 var xul = p.w.document.createElement( hash.render_xul );
378                                                 listcell.appendChild( xul );
379                                                 if (hash.render_xul == 'checkbox') xul.setAttribute('checked', 'true');
380                                         }
381                                         cols.push( listcell );
382                                 }
383
384                                 var listitem = p.list_box.add_row( 
385                                         cols, { 
386                                                 'record_id' : mbts.id(),
387                                                 'balance_owed' : mbts.balance_owed()
388                                         } 
389                                 ); 
390                                 if (p.list_box.apply_to_each_listitem) {
391                                         p.list_box.apply_to_each_listitem( i, listitem );
392                                 }
393                         }
394                 }, 0
395         );
396 }