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