]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/server/patron/bill_details.js
LP 2061136 follow-up: ng lint --fix
[working/Evergreen.git] / Open-ILS / xul / staff_client / server / patron / bill_details.js
1 function $(id) { return document.getElementById(id); }
2
3 function retrieve_patron() {
4     g.patron_id = xul_param('patron_id');
5
6     if (g.patron_id) {
7         JSAN.use('patron.util'); 
8         g.au_obj = patron.util.retrieve_fleshed_au_via_id( ses(), g.patron_id, null );
9         
10         $('patron_name').setAttribute('value', 
11             patron.util.format_name( g.au_obj ) + ' : ' + g.au_obj.card().barcode() 
12         );
13     }
14
15 }
16
17 function retrieve_mbts() {
18     g.network.simple_request('FM_MBTS_RETRIEVE.authoritative',[ses(),g.mbts_id],
19         function(req) {
20             try {
21                 g.mbts = req.getResultObject();
22                 $('mbts_id').value = g.mbts.id();
23                 $('mbts_xact_type').value = g.mbts.xact_type();
24                 $('mbts_xact_start').value = util.date.formatted_date( g.mbts.xact_start(), '%{localized}' );
25                 $('mbts_xact_finish').value = g.mbts.xact_finish() ? util.date.formatted_date( g.mbts.xact_finish(), '%{localized}' ) : '';
26                 $('mbts_total_owed').value = g.mbts.total_owed() ? util.money.sanitize( g.mbts.total_owed() ) : '';
27                 $('mbts_total_paid').value = g.mbts.total_paid() ? util.money.sanitize( g.mbts.total_paid() ) : '';
28                 $('mbts_balance_owed').value = g.mbts.balance_owed() ? util.money.sanitize( g.mbts.balance_owed() ) : '';
29
30                 switch(g.mbts.xact_type()) {
31                     case 'circulation' : retrieve_circ(); break;
32                     case 'grocery' : retrieve_grocery(); $('copy_summary_vbox').hidden = true; $('copy_summary_splitter').hidden = true; break;
33                     case 'reservation' : retrieve_reservation(); $('copy_summary_vbox').hidden = true; $('copy_summary_splitter').hidden = true; break;
34                     default: $('copy_summary_vbox').hidden = true; $('copy_summary_splitter').hidden = true; break;
35                 }
36
37             } catch(E) {
38                 g.error.sdump('D_ERROR',E);
39             }
40         }
41     );
42 }
43
44 function retrieve_grocery() {
45     JSAN.use('util.widgets');
46     g.network.simple_request('FM_MG_RETRIEVE', [ ses(), g.mbts_id ],
47         function (req) {
48             var r_mg = req.getResultObject();
49             if (instanceOf(r_mg,mg)) {
50                 $('billing_location').value = g.data.hash.aou[ r_mg.billing_location() ].shortname() + ' : ' + g.data.hash.aou[ r_mg.billing_location() ].name();
51             }
52         }
53     );
54 }
55
56 function retrieve_reservation() {
57     JSAN.use('util.widgets');
58     g.network.simple_request('FM_BRESV_RETRIEVE', [ ses(), g.mbts_id ],
59         function (req) {
60             var r_bresv = req.getResultObject();
61             if (instanceOf(r_bresv,bresv)) {
62                 $('billing_location').value = g.data.hash.aou[ r_bresv.pickup_lib() ].shortname() + ' : ' + g.data.hash.aou[ r_bresv.pickup_lib() ].name();
63             }
64         }
65     );
66 }
67
68 function retrieve_circ() {
69     JSAN.use('util.widgets');
70     g.network.simple_request('FM_CIRC_RETRIEVE_VIA_ID', [ ses(), g.mbts_id ],
71         function (req) {
72             var r_circ = req.getResultObject();
73             if (instanceOf(r_circ,circ)) {
74
75                 $('title_label').hidden = false;
76                 $('checked_out_label').hidden = false;
77                 $('due_label').hidden = false;
78                 $('checked_in_label').hidden = false;
79                 $('checked_out').value = r_circ.xact_start() ? util.date.formatted_date( r_circ.xact_start(), '%{localized}' ) : '';
80                 $('checked_in').value = r_circ.checkin_time() ? util.date.formatted_date( r_circ.checkin_time(), '%{localized}' ) : '';
81                 $('due').value = r_circ.due_date() ? util.date.formatted_date( r_circ.due_date(), '%{localized}' ) : '';
82                 $('billing_location').value = g.data.hash.aou[ r_circ.circ_lib() ].shortname() + ' : ' + g.data.hash.aou[ r_circ.circ_lib() ].name();
83                 var r = '';
84                 if (get_bool( r_circ.desk_renewal() ) ) r += 'DESK ';
85                 if (get_bool(r_circ.opac_renewal() ) ) r += 'OPAC ';
86                 if (get_bool(r_circ.phone_renewal() ) ) r += 'PHONE ';
87                 $('renewal').value = r || 'No';
88
89                 var csb = $('copy_summary_vbox'); while (csb.firstChild) csb.removeChild(csb.lastChild);
90                 var copy_summary = document.createElement('iframe'); csb.appendChild(copy_summary);
91                 copy_summary.setAttribute('src',urls.XUL_COPY_SUMMARY); // + '?copy_id=' + r_circ.target_copy());
92                 copy_summary.setAttribute('flex','1');
93                 get_contentWindow(copy_summary).xulG = { 'circ' : r_circ, 'copy_id' : r_circ.target_copy(), 'new_tab' : xulG.new_tab, 'url_prefix' : xulG.url_prefix };
94
95                 g.network.simple_request(
96                     'MODS_SLIM_RECORD_RETRIEVE_VIA_COPY.authoritative',
97                     [ r_circ.target_copy() ],
98                     function (rreq) {
99                         var r_mvr = rreq.getResultObject();
100                         if (instanceOf(r_mvr,mvr)) {
101                             util.widgets.remove_children('title');
102                             $('title').appendChild( document.createTextNode( String(r_mvr.title()).substr(0,50) ) );
103                         } else {
104                             g.network.simple_request(
105                                 'FM_ACP_RETRIEVE',
106                                 [ r_circ.target_copy() ],
107                                 function (rrreq) {
108                                     var r_acp = rrreq.getResultObject();
109                                     if (instanceOf(r_acp,acp)) {
110                                         util.widgets.remove_children('title');
111                                         $('title').appendChild( document.createTextNode( r_acp.dummy_title() ) );
112                                     }
113                                 }
114                             );
115                         }
116                     }
117                 );
118
119             }
120         }
121     );
122 }
123
124 function init_lists() {
125     JSAN.use('util.list'); 
126     g.bill_list = new util.list('bill_tree');
127     g.payment_list = new util.list('payment_tree');
128
129     g.bill_list.init( {
130         'columns' : patron.util.mb_columns({}),
131         'on_select' : function(ev) {
132             JSAN.use('util.functional');
133             g.bill_list_selection = util.functional.map_list(
134                 g.bill_list.retrieve_selection(),
135                 function(o) { return o.getAttribute('retrieve_id'); }
136             );
137             $('void').disabled = g.bill_list_selection.length == 0;
138             $('edit_bill_note').disabled = g.bill_list_selection.length == 0;
139         },
140     } );
141
142     $('bill_list_actions').appendChild( g.bill_list.render_list_actions() );
143     g.bill_list.set_list_actions();
144
145     g.payment_list.init( {
146         'columns' : patron.util.mp_columns({}),
147         'on_select' : function(ev) {
148             JSAN.use('util.functional');
149             g.payment_list_selection = util.functional.map_list(
150                 g.payment_list.retrieve_selection(),
151                 function(o) { return o.getAttribute('retrieve_id'); }
152             );
153             $('edit_payment_note').disabled = g.payment_list_selection.length == 0;
154         },
155     } );
156
157     $('payment_list_actions').appendChild( g.payment_list.render_list_actions() );
158     g.payment_list.set_list_actions();
159 }
160
161 function retrieve_mb() {
162     g.mb_list = g.network.simple_request( 'FM_MB_RETRIEVE_VIA_MBTS_ID.authoritative', [ ses(), g.mbts_id ] );
163     //g.error.sdump('D_DEBUG',g.error.pretty_print( js2JSON(g.mb_list) ));
164
165     var mb_funcs = [];
166
167     function gen_mb_func(i,r) {
168         return function() {
169             g.bill_list.append( { 'retrieve_id' : i, 'row' : { my : { 'mb' : r } } } );
170         }
171     }
172
173     for (var i = 0; i < g.mb_list.length; i++) {
174         mb_funcs.push( gen_mb_func(i,g.mb_list[i]) );
175     }
176
177     JSAN.use('util.exec');
178     var mb_exec = new util.exec(4); mb_exec.chain(mb_funcs);
179 }
180
181 function retrieve_mp() {
182     g.mp_list = g.network.simple_request( 'FM_MP_RETRIEVE_VIA_MBTS_ID.authoritative', [ ses(), g.mbts_id ]);
183     //g.error.sdump('D_DEBUG',g.error.pretty_print( js2JSON(mp_list) ));
184
185     var mp_funcs = [];
186
187     function gen_mp_func(i,r) {
188         return function() {
189             g.payment_list.append( { 'retrieve_id' : i, 'row' : { my : { 'mp' : r } } } );
190         }
191     }
192
193     for (var i = 0; i < g.mp_list.length; i++) {
194         mp_funcs.push( gen_mp_func(i,g.mp_list[i]) );
195     }
196
197     JSAN.use('util.exec');
198     var mp_exec = new util.exec(4); mp_exec.chain(mp_funcs);
199 }
200
201 function my_init() {
202     try {
203         if (typeof JSAN == 'undefined') { throw( $("commonStrings").getString('common.jsan.missing') ); }
204         JSAN.errorLevel = "die"; // none, warn, or die
205         JSAN.addRepository('/xul/server/');
206
207         JSAN.use('util.error'); g.error = new util.error();
208         JSAN.use('util.network'); g.network = new util.network();
209         JSAN.use('util.date');
210         JSAN.use('util.money');
211         JSAN.use('patron.util');
212         JSAN.use('OpenILS.data'); g.data = new OpenILS.data(); g.data.init({'via':'stash'});
213
214         g.error.sdump('D_TRACE','my_init() for bill_details.xul');
215
216         g.mbts_id = xul_param('mbts_id');
217
218         window.bill_details_event_listeners = new EventListenerList();
219
220         retrieve_patron();
221
222         retrieve_mbts();
223
224         init_lists();
225
226         check_void_button();
227
228         retrieve_mb();
229         retrieve_mp();
230
231         window.bill_details_event_listeners.add($('void'), 
232             'command',
233             handle_void,
234             false
235         );
236
237         window.bill_details_event_listeners.add($('edit_bill_note'), 
238             'command',
239             handle_edit_bill_note,
240             false
241         );
242
243         window.bill_details_event_listeners.add($('edit_payment_note'), 
244             'command',
245             handle_edit_payment_note,
246             false
247         );
248
249     } catch(E) {
250         try { g.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.bill_details.my_init.error'),E); } catch(F) { alert(E); }
251     }
252 }
253
254 function my_cleanup() {
255     try {
256         g.bill_list.cleanup();
257         g.bill_list.clear();
258         g.payment_list.cleanup();
259         g.payment_list.clear();
260         window.bill_details_event_listeners.removeAll();
261     } catch(E) {
262         try { g.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.bill_details.my_cleanup.error'),E); } catch(F) { alert(E); }
263     }
264 }
265
266 function handle_edit_bill_note() {
267     try {
268         var mb_list = util.functional.map_list(g.bill_list_selection, function(o){return g.mb_list[o].id();}); 
269         if (mb_list.length == 0) return;
270         var new_note = window.prompt(
271             $("patronStrings").getString('staff.patron.bill_details.handle_edit_bill_note.note_dialog.prompt'),
272             util.functional.map_list(g.bill_list_selection, function(o){return g.mb_list[o].note();}).join(", "),           
273             $("patronStrings").getString('staff.patron.bill_details.handle_edit_bill_note.note_dialog.title')
274         );
275         if (new_note) {
276             var r = g.network.simple_request('FM_MB_NOTE_EDIT',[ ses(), new_note ].concat(mb_list));
277             if (r == 1 /* success */) {
278                 g.bill_list.clear();
279                 retrieve_mb();
280             } else {
281                 if (r.ilsevent != 5000 /* PERM_FAILURE */) {
282                     alert( $("patronStrings").getString('staff.patron.bill_details.handle_edit_bill_note.failure') );
283                 }
284             } 
285         }
286     } catch(E) {
287         try { g.error.standard_unexpected_error_alert('bill_details.xul, handle_edit_bill_note:',E); } catch(F) { alert(E); }
288     }
289 };
290
291 function handle_edit_payment_note() {
292     try {
293         var mp_list = util.functional.map_list(g.payment_list_selection, function(o){return g.mp_list[o].id();}); 
294         if (mp_list.length == 0) return;
295         var new_note = window.prompt(
296             $("patronStrings").getString('staff.patron.bill_details.handle_edit_payment_note.note_dialog.prompt'),
297             util.functional.map_list(g.payment_list_selection, function(o){return g.mp_list[o].note();}).join(", "),           
298             $("patronStrings").getString('staff.patron.bill_details.handle_edit_payment_note.note_dialog.title')
299         );
300         if (new_note) {
301             var r = g.network.simple_request('FM_MP_NOTE_EDIT',[ ses(), new_note ].concat(mp_list));
302             if (r == 1 /* success */) {
303                 g.payment_list.clear();
304                 retrieve_mp();
305             } else {
306                 if (r.ilsevent != 5000 /* PERM_FAILURE */) {
307                     alert( $("patronStrings").getString('staff.patron.bill_details.handle_edit_payment_note.failure') );
308                 }
309             } 
310         }
311     } catch(E) {
312         try { g.error.standard_unexpected_error_alert('bill_details.xul, handle_edit_payment_note:',E); } catch(F) { alert(E); }
313     }
314 };
315
316 function handle_void() {
317     try {
318         var mb_list = util.functional.map_list(g.bill_list_selection, function(o){return g.mb_list[o];}); 
319         mb_list = util.functional.filter_list( mb_list, function(o) { return ! get_bool( o.voided() ) });
320
321         if (mb_list.length == 0) { alert($("patronStrings").getString('staff.patron.bill_details.handle_void.voided_billings.alert')); return; }
322
323         var sum = 0;
324         var has_overdue = false;
325         var has_lost_or_lo = false;
326         for (var i = 0; i < mb_list.length; i++) {
327             var bill = mb_list[i];
328             sum += util.money.dollars_float_to_cents_integer( bill.amount() );
329             var btype = bill.btype();
330             if (!has_overdue && btype == 1) {
331                 has_overdue = true;
332             } else if (!has_lost_or_lo && (btype == 3 || btype == 4 || btype == 10 || btype == 11)) {
333                 has_lost_or_lo = true;
334             }
335         }
336         var prohibit_default = g.data.hash.aous['bill.prohibit_negative_balance_default'];
337         var prohibit_on_overdues = g.data.hash.aous['bill.prohibit_negative_balance_on_overdues'];
338         if (prohibit_on_overdues === undefined) prohibit_on_overdues = prohibit_default;
339         var prohibit_on_lost = g.data.hash.aous['bill.prohibit_negative_balance_on_lost'];
340         if (prohibit_on_lost === undefined) prohibit_on_lost = prohibit_default;
341         if (has_overdue && prohibit_on_overdues || has_lost_or_lo && prohibit_on_lost) {
342             var choice = g.error.yns_alert_original(
343                 $("patronStrings").getString('staff.patron.bills.void_warning.message'),
344                 $("patronStrings").getString('staff.patron.bills.void_warning.title'),
345                 $('commonStrings').getString('common.yes'),
346                 $('commonStrings').getString('common.no'),
347                 null,
348                 $('commonStrings').getString('common.confirm')
349             );
350             if (choice != 0) return;
351         }
352
353         sum = util.money.cents_as_dollars( sum );
354         var msg = $("patronStrings").getFormattedString('staff.patron.bill_details.handle_void.confirm_void_billing', sum);
355         var r = g.error.yns_alert(msg,
356             $("patronStrings").getString('staff.patron.bill_details.handle_void.confirm_void_billing_title'),
357             $("patronStrings").getString('staff.patron.bill_details.handle_void.confirm_void_billing_yes'),
358             $("patronStrings").getString('staff.patron.bill_details.handle_void.confirm_void_billing_no'),null,
359             $("patronStrings").getString('staff.patron.bill_details.handle_void.confirm_void_billing_confirm_message'));
360         if (r == 0) {
361             var robj = g.network.simple_request('FM_MB_VOID',[ses()].concat(util.functional.map_list(mb_list,function(o){return o.id();})));
362             if (robj.ilsevent) {
363                 switch(Number(robj.ilsevent)) {
364                     default: 
365                         g.error.standard_unexpected_error_alert($("patronStrings").getString('staff.patron.bill_details.handle_void.voiding_error'),robj); 
366                         retrieve_mbts();
367                         g.bill_list.clear();
368                         retrieve_mb();
369                         if (typeof window.xulG == 'object' && typeof window.xulG.refresh == 'function') { window.xulG.refresh(); }
370                         return; 
371                     break;
372                 }
373             }
374
375             g.data.stash_retrieve(); if (! g.data.voided_billings ) g.data.voided_billings = []; 
376             for (var i = 0; i < mb_list.length; i++) {
377                     g.data.voided_billings.push( mb_list[i] );
378             }
379             g.data.stash('voided_billings');
380             retrieve_mbts();
381             g.bill_list.clear();
382             retrieve_mb();
383             if (typeof window.xulG == 'object' && typeof window.xulG.refresh == 'function') { window.xulG.refresh(); }
384         }
385
386     } catch(E) {
387         try { g.error.standard_unexpected_error_alert('bill_details.xul, handle_void:',E); } catch(F) { alert(E); }
388     }
389 }
390
391 function check_void_button() {
392     try {
393         var check = g.network.simple_request('PERM_CHECK',[ses(),ses('staff_id'),g.au_obj.home_ou(),['VOID_BILLING']]);
394         if (typeof check.ilsevent != 'undefined') {
395             g.error.standard_unexpected_error_alert('check_void_button()',check);
396             return;
397         }
398         if (check.length != 0) $('void').setAttribute('hidden', true);
399     } catch(E) {
400         g.error.standard_unexpected_error_alert('check_void_button()',E);
401     }
402 }