]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/bill_history.js
tally amounts for selected payments in payment history interface
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / patron / bill_history.js
1 function $(id) { return document.getElementById(id); }
2 var payment_history_fetched = false;
3
4 function tally_selected() {
5     try {
6         JSAN.use('util.money');
7         var selected_billed = 0;
8         var selected_paid = 0;
9
10         for (var i = 0; i < g.bill_list_selection.length; i++) {
11             var bill = g.bill_map[g.bill_list_selection[i]];
12             if (!bill) {
13                 $('billed_tally').setAttribute('value', '???');
14                 $('paid_tally').setAttribute('value', '???');
15                 return;
16             }
17             var to = util.money.dollars_float_to_cents_integer( bill.transaction.total_owed() );
18             var tp = util.money.dollars_float_to_cents_integer( bill.transaction.total_paid() );
19             selected_billed += to;
20             selected_paid += tp;
21         }
22         $('billed_tally').setAttribute('value', util.money.cents_as_dollars( selected_billed ) );
23         $('paid_tally').setAttribute('value', util.money.cents_as_dollars( selected_paid ) );
24     } catch(E) {
25         alert('Error in bill_history.js, tally_selected(): ' + E);
26     }
27 }
28
29 function payments_tally_selected() {
30     try {
31         JSAN.use('util.money');
32         var selected_paid = 0;
33
34         for (var i = 0; i < g.payments_list_selection.length; i++) {
35             var payment = g.payments_map[g.payments_list_selection[i].id];
36             if (!payment) {
37                 $('payments_paid_tally').setAttribute('value', '???');
38                 return;
39             }
40             var amount = util.money.dollars_float_to_cents_integer( payment.amount() );
41             selected_paid += amount;
42         }
43         $('payments_paid_tally').setAttribute('value', util.money.cents_as_dollars( selected_paid ) );
44     } catch(E) {
45         alert('Error in bill_history.js, payments_tally_selected(): ' + E);
46     }
47 }
48
49
50 function retrieve_mbts_for_list() {
51     //var method = 'FM_MBTS_IDS_RETRIEVE_ALL_HAVING_CHARGE';
52     var method = 'FM_MBTS_IDS_RETRIEVE_FOR_HISTORY.authoritative';
53     if (xul_param('current')) method = 'FM_MBTS_IDS_RETRIEVE_ALL_HAVING_BALANCE.authoritative';
54     var date2 = $('bills_date2').dateValue;
55     date2.setDate( date2.getDate() + 1 ); // Javascript will wrap into subsequent months
56     var filter = {
57         'xact_start' : {
58             'between' : [
59                 $('bills_date1').value,
60                 $('bills_date2').value == util.date.formatted_date(new Date(),'%F') ?
61                     'now' : util.date.formatted_date( date2 ,'%F')
62             ]
63         }
64     }
65     g.mbts_ids = g.network.simple_request(method,[ses(),g.patron_id, null, filter]);
66     if (g.mbts_ids.ilsevent) {
67         switch(Number(g.mbts_ids.ilsevent)) {
68             case -1: g.error.standard_network_error_alert($("patronStrings").getString('staff.patron.bill_history.retrieve_mbts_for_list.close_win_try_again')); break;
69             default: g.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.bill_history.retrieve_mbts_for_list.close_win_try_again'),g.mbts_ids); break;
70         }
71     } else if (g.mbts_ids == null) {
72         g.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.bill_history.retrieve_mbts_for_list.close_win_try_again'),null);
73     } else {
74         //g.mbts_ids.reverse();
75     
76         function gen_func(r) {
77             return function() {
78                 if (typeof r == 'object') {
79                     g.bill_list.append( 
80                         { 
81                             'retrieve_id' : r.id(), 
82                             'row' : { 
83                                 'my' : { 
84                                     'mbts' : r 
85                                 } 
86                             } 
87                         } 
88                     );
89                 } else {
90                     g.bill_list.append( 
91                         { 
92                             'retrieve_id' : r, 
93                             'row' : { 
94                                 'my' : {} 
95                             } 
96                         } 
97                     );
98                 }
99             }
100         }
101
102         g.bill_list.clear(); $('bills_meter').hidden = false;
103         for (var i = 0; i < g.mbts_ids.length; i++) {
104             dump('i = ' + i + ' g.mbts_ids[i] = ' + g.mbts_ids[i] + '\n');
105             g.funcs.push( gen_func(g.mbts_ids[i]) );
106         }
107         g.funcs.push( function() { $('bills_meter').hidden = true; } );
108     }
109 }
110
111 function init_lists() {
112     JSAN.use('util.list'); JSAN.use('circ.util'); 
113
114     init_main_list();
115     init_payments_list();
116 }
117
118 function init_main_list() {
119     g.bill_list_selection = [];
120
121     g.bill_list = new util.list('bill_tree');
122
123     g.bill_list.init( {
124         'columns' : 
125             patron.util.mbts_columns({
126                 'xact_finish' : { 'hidden' : xul_param('current') ? true : false }
127             }).concat( 
128             circ.util.columns({ 
129                 'title' : { 'hidden' : false, 'flex' : '3' }
130             }) 
131         ),
132         'map_row_to_columns' : patron.util.std_map_row_to_columns(' '),
133         'on_select' : function(ev) {
134             JSAN.use('util.functional');
135             g.bill_list_selection = util.functional.map_list(
136                 g.bill_list.retrieve_selection(),
137                 function(o) { return o.getAttribute('retrieve_id'); }
138             );
139             tally_selected();
140             $('details').disabled = g.bill_list_selection.length == 0;
141             $('add').disabled = g.bill_list_selection.length == 0;
142             $('summary').hidden = g.bill_list_selection.length == 0;
143             $('copy_summary').hidden = g.bill_list_selection.length == 0;
144         },
145         'retrieve_row' : function(params) {
146             var id = params.retrieve_id;
147             var row = params.row;
148             if (id) {
149                 if (typeof row.my == 'undefined') row.my = {};
150                 if (typeof row.my.mbts == 'undefined' ) {
151                     g.network.simple_request('BLOB_MBTS_DETAILS_RETRIEVE',[ses(),id], function(req) {
152                         var blob = req.getResultObject();
153                         row.my.mbts = blob.transaction;
154                         row.my.circ = blob.circ;
155                         row.my.acp = blob.copy;
156                         row.my.mvr = blob.record;
157                         g.bill_map[ id ] = blob;
158                         if (typeof params.on_retrieve == 'function') {
159                             params.on_retrieve(row);
160                         };
161                         tally_selected();
162                     } );
163                 }
164             }
165             return row;
166         },
167     } );
168
169     $('bill_list_actions').appendChild( g.bill_list.render_list_actions() );
170     g.bill_list.set_list_actions();
171 }
172
173 function init_payments_list() {
174     g.payments_list_selection = [];
175
176     g.payments_list = new util.list('payments_tree');
177
178     g.payments_list.init( {
179         'columns' : g.payments_list.fm_columns('mp').concat( [
180             {
181                 'id' : 'payments_blob_xact_type', 'flex' : 0,
182                 'label' : $('patronStrings').getString('staff.patron.bill_history.column.xact_type.label'),
183                 'render' : function(my) { return my.xact_type; }
184             },
185             {
186                 'id' : 'payments_blob_last_billing_type', 'flex' : 0,
187                 'label' : $('patronStrings').getString('staff.patron.bill_history.column.last_billing_type.label'),
188                 'render' : function(my) { return my.last_billing_type; }
189             },
190             {
191                 'id' : 'payments_blob_title', 'flex' : 1,
192                 'label' : $('patronStrings').getString('staff.patron.bill_history.column.title.label'),
193                 'render' : function(my) { return my.title; }
194             }
195         ] ),
196         'on_select' : function(ev) {
197             JSAN.use('util.functional');
198             g.payments_list_selection = util.functional.map_list(
199                 g.payments_list.retrieve_selection(),
200                 function(o) { return JSON2js( o.getAttribute('retrieve_id') ); }
201             );
202             payments_tally_selected();
203             $('payments_details').disabled = g.payments_list_selection.length == 0;
204         },
205         'retrieve_row' : function(params) {
206             var id = params.retrieve_id;
207             var row = params.row;
208             if (typeof params.on_retrieve == 'function') {
209                 params.on_retrieve(row);
210             };
211             return row;
212         },
213     } );
214
215     $('payments_list_actions').appendChild( g.payments_list.render_list_actions() );
216     g.payments_list.set_list_actions();
217 }
218
219 function my_init() {
220     try {
221         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
222         if (typeof JSAN == 'undefined') { throw( $("commonStrings").getString('common.jsan.missing') ); }
223         JSAN.errorLevel = "die"; // none, warn, or die
224         JSAN.addRepository('/xul/server/');
225
226         JSAN.use('util.error'); g.error = new util.error();
227         JSAN.use('util.network'); g.network = new util.network();
228         JSAN.use('util.date');
229         JSAN.use('util.money');
230         JSAN.use('patron.util');
231         JSAN.use('OpenILS.data'); g.data = new OpenILS.data(); g.data.init({'via':'stash'});
232         //g.data.temp = ''; g.data.stash('temp');
233
234         g.error.sdump('D_TRACE','my_init() for bill_history.xul');
235
236         if (xul_param('current')) {
237             $('caption').setAttribute('label',$("patronStrings").getString('staff.patron.bill_history.my_init.current_bills'));
238             document.title = $("patronStrings").getString('staff.patron.bill_history.my_init.current_bills');
239         } else {
240             $('caption').setAttribute('label',$("patronStrings").getString('staff.patron.bill_history.my_init.bill_history'));
241             document.title = $("patronStrings").getString('staff.patron.bill_history.my_init.bill_history');
242         }
243
244         g.funcs = []; g.bill_map = {}; g.payments_map = {};
245
246         g.patron_id = xul_param('patron_id');
247
248         init_lists();
249
250         $('bills_date1').year = $('bills_date1').year - 1;
251
252         retrieve_mbts_for_list();
253
254         $('details').addEventListener(
255             'command',
256             gen_handle_details('bills'),
257             false
258         );
259
260         $('payments_details').addEventListener(
261             'command',
262             gen_handle_details('payments'),
263             false
264         );
265
266         $('add').addEventListener(
267             'command',
268             handle_add,
269             false
270         );
271
272         JSAN.use('util.exec'); var exec = new util.exec(20); 
273         exec.on_error = function(E) { alert(E); return true; }
274         exec.timer(g.funcs,100);
275     } catch(E) {
276         var err_msg = $("commonStrings").getFormattedString('common.exception', ['patron/bill_history.xul', E]);
277         try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); }
278         alert(err_msg);
279     }
280 }
281
282 function handle_add() {
283     if(g.bill_list_selection.length > 1)
284         var msg = $("patronStrings").getFormattedString('staff.patron.bill_history.handle_add.message_plural', [g.bill_list_selection]);
285     else
286         var msg = $("patronStrings").getFormattedString('staff.patron.bill_history.handle_add.message_singular', [g.bill_list_selection]);
287         
288     var r = g.error.yns_alert(msg,
289         $("patronStrings").getString('staff.patron.bill_history.handle_add.title'),
290         $("patronStrings").getString('staff.patron.bill_history.handle_add.btn_yes'),
291         $("patronStrings").getString('staff.patron.bill_history.handle_add.btn_no'),null,
292         $("patronStrings").getString('staff.patron.bill_history.handle_add.confirm_message'));
293     if (r == 0) {
294         JSAN.use('util.window');
295         var win = new util.window();
296         for (var i = 0; i < g.bill_list_selection.length; i++) {
297             var w = win.open(
298                 urls.XUL_PATRON_BILL_WIZARD,
299                 'billwizard',
300                 'chrome,resizable,modal',
301                 { 'patron_id' : g.patron_id, 'xact_id' : g.bill_list_selection[i] }
302             );
303         }
304         g.bill_list.clear();
305         retrieve_mbts_for_list();
306         if (typeof window.refresh == 'function') window.refresh();
307         if (typeof window.xulG == 'object' && typeof window.xulG.refresh == 'function') window.xulG.refresh();
308     }
309 }
310
311 function gen_handle_details(which_list) {
312     return function() {
313         JSAN.use('util.functional');
314         var selection;
315         switch(which_list) {
316             case 'payments': selection = util.functional.map_list( g.payments_list_selection, function(o) { return o.xact; } ); break;
317             default: selection = g.bill_list_selection; break;
318         }
319         JSAN.use('util.window'); var win = new util.window();
320         for (var i = 0; i < selection.length; i++) {
321             var my_xulG = win.open(
322                 urls.XUL_PATRON_BILL_DETAILS,
323                 'test_billdetails_' + selection[i],
324                 'chrome,resizable',
325                 {
326                     'patron_id' : g.patron_id,
327                     'mbts_id' : selection[i],
328                     'refresh' : function() { 
329                         if (typeof window.refresh == 'function') window.refresh();
330                         if (typeof window.xulG == 'object' && typeof window.xulG.refresh == 'function') window.xulG.refresh();
331                     }, 
332                 }
333             );
334         }
335     };
336 }
337
338 function print_bills() {
339     try {
340         var template = 'bills_historical'; if (xul_param('current')) template = 'bills_current';
341         JSAN.use('patron.util');
342         var params = { 
343             'patron' : patron.util.retrieve_au_via_id(ses(),g.patron_id), 
344             'template' : template
345         };
346         g.bill_list.print(params);
347     } catch(E) {
348         g.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.bill_history.print_bills.print_error'), E);
349     }
350 }
351
352 function payment_history_init() {
353     try {
354         if (payment_history_fetched) { return; } else { payment_history_fetched = true; }
355
356         $('payments_date1').year = $('payments_date1').year - 1;
357
358         retrieve_payments();
359
360     } catch(E) {
361         alert('Error in bill_history.js, payment_history_init(): ' + E);
362     }
363 }
364
365 function retrieve_payments() {
366     try {
367
368         g.payments_list.clear();
369
370         $('payments_meter').hidden = false;
371
372         var date2 = $('payments_date2').dateValue;
373         date2.setDate( date2.getDate() + 1 ); // Javascript will wrap into subsequent months
374         var filters = {
375             'where' : {
376                 'payment_ts' : {
377                     'between' : [
378                         $('payments_date1').value,
379                         $('payments_date2').value == util.date.formatted_date(new Date(),'%F') ? 
380                             'now' : util.date.formatted_date( date2 ,'%F')
381                     ]
382                 }
383             }
384         };
385
386         fieldmapper.standardRequest(
387             [ api.FM_MP_RETRIEVE_VIA_USER.app, api.FM_MP_RETRIEVE_VIA_USER.method ],
388             {   async: true,
389                 params: [ses(), g.patron_id, filters],
390                 onresponse: function(r) {
391                     try {
392                         var result = r.recv().content();
393
394                         if (result && typeof result.ilsevent == 'undefined') {
395                             g.payments_list.append( 
396                                 { 
397                                     'retrieve_id' : js2JSON( { 'id' : result.mp.id(), 'xact' : result.mp.xact() } ),
398                                     'row' : { 
399                                         'my' : { 
400                                             'mp' : result.mp,
401                                             'xact_type' : result.xact_type,
402                                             'last_billing_type' : result.last_billing_type,
403                                             'title' : result.title
404                                         } 
405                                     } 
406                                 } 
407                             );
408                             g.payments_map[ result.mp.id() ] = result.mp;
409                         } else {
410                             throw( js2JSON(result) );
411                         }
412                     } catch(E) {
413                         alert('Error retrieving payment in bill_history.js, onresponse: ' + E);                        
414                     }
415                 },
416                 oncomplete: function() {
417                     $('payments_meter').hidden = true;
418                 },
419                 onerror: function(r) {
420                     var result = r.recv().content();
421                     alert('Error retrieving payment in bill_history.js, onerror: ' + js2JSON(result));                        
422                 }
423             }
424         );
425
426     } catch(E) {
427         alert('Error in bill_history.js, retrieve_payments(): ' + E);
428     }
429 }