handle object or id for mbts
[Evergreen.git] / Open-ILS / xul / staff_client / server / patron / bill_history.xul
1 <?xml version="1.0"?>
2 <!-- Application: Evergreen Staff Client -->
3 <!-- Screen: Patron Display -->
4
5 <!-- ///////////////////////////////////////////////////////////////////////////////////////////////////////////// -->
6 <!-- STYLESHEETS -->
7 <?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
8 <?xml-stylesheet href="chrome://open_ils_staff_client/skin/global.css" type="text/css"?>
9 <?xml-stylesheet href="/xul/server/skin/global.css" type="text/css"?>
10 <?xml-stylesheet href="/xul/server/skin/circ.css" type="text/css"?>
11 <?xml-stylesheet href="/xul/server/skin/patron_display.css" type="text/css"?>
12
13 <!-- ///////////////////////////////////////////////////////////////////////////////////////////////////////////// -->
14 <!-- LOCALIZATION -->
15 <!DOCTYPE window PUBLIC "" ""[
16         <!--#include virtual="/opac/locale/en-US/lang.dtd"-->
17 ]>
18
19 <!-- ///////////////////////////////////////////////////////////////////////////////////////////////////////////// -->
20 <!-- OVERLAYS -->
21 <?xul-overlay href="/xul/server/OpenILS/util_overlay.xul"?>
22 <?xul-overlay href="/xul/server/patron/bill_summary_overlay.xul"?>
23
24 <window id="bill_history_win" width="700" height="550"
25         onload="try{ my_init(); font_helper(); } catch(E) { alert(E); }"
26         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
27
28         <!-- ///////////////////////////////////////////////////////////////////////////////////////////////////////////// -->
29         <!-- BEHAVIOR -->
30         <script type="text/javascript">var myPackageDir = 'open_ils_staff_client'; var IAMXUL = true; var g = {};</script>
31         <scripts id="openils_util_scripts"/>
32
33         <script type="text/javascript" src="/xul/server/main/JSAN.js"/>
34         <script>
35         <![CDATA[
36
37                 function $(id) { return document.getElementById(id); }
38
39                 function retrieve_patron() {
40                         g.patron_id = g.cgi.param('patron_id');
41
42                         if (g.patron_id) {
43                                 JSAN.use('patron.util'); 
44                                 g.au_obj = patron.util.retrieve_fleshed_au_via_id( ses(), g.patron_id );
45                                 
46                                 $('patron_name').setAttribute('value', g.au_obj.family_name() + ', ' + g.au_obj.first_given_name() + ' : ' + g.au_obj.card().barcode() );
47                         }
48
49                 }
50
51                 function retrieve_mbts_for_list() {
52                         //var method = 'FM_MBTS_IDS_RETRIEVE_ALL_HAVING_CHARGE';
53                         var method = 'FM_MBTS_IDS_RETRIEVE_ALL_HAVING_BILL';
54                         if (g.cgi.param('current')) method = 'FM_MBTS_IDS_RETRIEVE_ALL_HAVING_BALANCE';
55                         g.mbts_ids = g.network.simple_request(method,[ses(),g.patron_id]);
56                         if (g.mbts_ids.ilsevent) {
57                                 switch(g.mbts_ids.ilsevent) {
58                                         case -1: g.error.standard_network_error_alert('Please close this window and try again.'); break;
59                                         default: g.error.standard_unexpected_error_alert('Please close this window and try again.',g.mbts_ids); break;
60                                 }
61                         } else if (g.mbts_ids == null) {
62                                 g.error.standard_unexpected_error_alert('Please close this window and try again.',null);
63                         } else {
64                                 //g.mbts_ids.reverse();
65                                 var funcs = [];
66                         
67                                                 function gen_func(r) {
68                                                         return function() {
69                                                                 if (typeof r == 'object') {
70                                                                         g.bill_list.append( 
71                                                                                 { 
72                                                                                         'retrieve_id' : r.id(), 
73                                                                                         'row' : { 
74                                                                                                 'my' : { 
75                                                                                                         'mbts' : r 
76                                                                                                 } 
77                                                                                         } 
78                                                                                 } 
79                                                                         );
80                                                                 } else {
81                                                                         g.bill_list.append( 
82                                                                                 { 
83                                                                                         'retrieve_id' : r, 
84                                                                                         'row' : { 
85                                                                                                 'my' : {} 
86                                                                                         } 
87                                                                                 } 
88                                                                         );
89                                                                 }
90                                                         }
91                                                 }
92                         
93                                 for (var i = 0; i < g.mbts_ids.length; i++) {
94                                         dump('i = ' + i + ' g.mbts_ids[i] = ' + g.mbts_ids[i] + '\n');
95                                         funcs.push( gen_func(g.mbts_ids[i]) );
96                                 }
97                                 JSAN.use('util.exec'); var exec = new util.exec(4);
98                                 exec.on_error = function(E) { alert(E); return true; }
99                                 exec.chain(funcs);
100                         }
101                 }
102
103                 function retrieve_specific_mbts() {
104                         if (g.mbts_id) g.network.simple_request('FM_MBTS_RETRIEVE',[ses(),g.mbts_id],
105                                 function(req) {
106                                         try {
107                                                 g.mbts = req.getResultObject();
108                                                 if (g.mbts.ilsevent) {
109                                                         switch(g.mbts.ilsevent) {
110                                                                 case -1: g.error.standard_network_error_alert('mbts_id = ' + g.mbts_id); break;
111                                                                 default: g.error.standard_unexpected_error_alert('mbts_id = ' + g.mbts_id,g.mbts); break;
112                                                         }
113                                                 } else {
114                                                         $('mbts_id').value = g.mbts_id;
115                                                         $('mbts_xact_type').value = g.mbts.xact_type();
116                                                         $('mbts_xact_start').value = g.mbts.xact_start().toString().substr(0,19);
117                                                         $('mbts_xact_finish').value = g.mbts.xact_finish() ? g.mbts.xact_finish().toString().substr(0,19) : '';
118                                                         $('mbts_total_owed').value = g.mbts.total_owed() ? util.money.sanitize( g.mbts.total_owed() ) : '';
119                                                         $('mbts_total_paid').value = g.mbts.total_paid() ? util.money.sanitize( g.mbts.total_paid() ) : '';
120                                                         $('mbts_balance_owed').value = g.mbts.balance_owed() ? util.money.sanitize( g.mbts.balance_owed() ) : '';
121                                                 }
122                                         } catch(E) {
123                                                 g.error.sdump('D_ERROR',E);
124                                         }
125                                 }
126                         );
127                 }
128
129                 function retrieve_circ() {
130                         JSAN.use('util.widgets');
131                         util.widgets.remove_children('title');
132                         $('title_label').hidden = true;
133                         $('checked_out_label').hidden = true;
134                         $('due_label').hidden = true;
135                         $('checked_in_label').hidden = true;
136                         $('checked_out').value = '';
137                         $('checked_in').value = '';
138                         $('due').value = '';
139                         $('copy_summary').hidden=true;
140
141                         g.network.simple_request('FM_CIRC_RETRIEVE_VIA_ID', [ ses(), g.mbts_id ],
142                                 function (req) {
143                                         var r_circ = req.getResultObject();
144                                         if (instanceOf(r_circ,circ)) {
145
146                                                 $('title_label').hidden = false;
147                                                 $('checked_out_label').hidden = false;
148                                                 $('due_label').hidden = false;
149                                                 $('checked_in_label').hidden = false;
150                                                 $('checked_out').value = r_circ.xact_start() ? r_circ.xact_start().toString().substr(0,10) : '';
151                                                 $('checked_in').value = r_circ.checkin_time() ? r_circ.checkin_time().toString().substr(0,10) : '';
152                                                 $('due').value = r_circ.due_date() ? r_circ.due_date().toString().substr(0,10) : '';
153
154                                                 $('copy_summary').setAttribute('src',urls.XUL_COPY_SUMMARY + '?copy_id=' + r_circ.target_copy());
155                                                 $('copy_summary').hidden=false;
156
157                                                 g.network.simple_request(
158                                                         'MODS_SLIM_RECORD_RETRIEVE_VIA_COPY',
159                                                         [ r_circ.target_copy() ],
160                                                         function (rreq) {
161                                                                 var r_mvr = rreq.getResultObject();
162                                                                 if (instanceOf(r_mvr,mvr)) {
163                                                                         $('title').appendChild( document.createTextNode( r_mvr.title().substr(0,50) ) );
164                                                                 } else {
165                                                                         g.network.simple_request(
166                                                                                 'FM_ACP_RETRIEVE',
167                                                                                 [ r_circ.target_copy() ],
168                                                                                 function (rrreq) {
169                                                                                         var r_acp = rrreq.getResultObject();
170                                                                                         if (instanceOf(r_acp,acp)) {
171                                                                                                 $('title').appendChild( document.createTextNode( r_acp.dummy_title() ) );
172                                                                                         }
173                                                                                 }
174                                                                         );
175                                                                 }
176                                                         }
177                                                 );
178
179                                         }
180                                 }
181                         );
182                 }
183
184                 function init_lists() {
185                         JSAN.use('util.list'); 
186                         g.bill_list = new util.list('bill_tree');
187
188                         g.bill_list.init( {
189                                 'columns' : patron.util.mbts_columns({}),
190                                 'map_row_to_column' : patron.util.std_map_row_to_column(' '),
191                                 'on_select' : function(ev) {
192                                         JSAN.use('util.functional');
193                                         g.bill_list_selection = util.functional.map_list(
194                                                 g.bill_list.retrieve_selection(),
195                                                 function(o) { return o.getAttribute('retrieve_id'); }
196                                         );
197                                         $('details').disabled = g.bill_list_selection.length == 0;
198                                         $('add').disabled = g.bill_list_selection.length == 0;
199                                         $('summary').hidden = g.bill_list_selection.length == 0;
200                                         $('copy_summary').hidden = g.bill_list_selection.length == 0;
201                                         g.mbts_id = g.bill_list_selection[0];
202                                         retrieve_specific_mbts();
203                                         retrieve_circ();
204                                 },
205                 'retrieve_row' : function(params) {
206                     var id = params.retrieve_id;
207                                         var row = params.row;
208                                         if (id) {
209                                                 if (typeof row.my == 'undefined') row.my = {};
210                                                 if ( typeof row.my.mbts == 'undefined' ) {
211                                                         var mbts_obj = g.network.simple_request('FM_MBTS_RETRIEVE',[ses(),id]);
212                                                         row.my.mbts = mbts_obj;
213                                                 }
214                                         }
215                                         if (typeof params.on_retrieve == 'function') {
216                                                 params.on_retrieve(row);
217                                         }
218                     return row;
219                 },
220                         } );
221
222                 }
223
224                 function my_init() {
225                         try {
226                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
227                                 if (typeof JSAN == 'undefined') { throw( "The JSAN library object is missing."); }
228                                 JSAN.errorLevel = "die"; // none, warn, or die
229                                 JSAN.addRepository('/xul/server/');
230
231                                 JSAN.use('util.error'); g.error = new util.error();
232                                 JSAN.use('util.network'); g.network = new util.network();
233                                 JSAN.use('util.date');
234                                 JSAN.use('util.money');
235                                 JSAN.use('patron.util');
236                                 JSAN.use('OpenILS.data'); g.data = new OpenILS.data(); g.data.init({'via':'stash'});
237                                 //g.data.temp = ''; g.data.stash('temp');
238
239                                 g.error.sdump('D_TRACE','my_init() for bill_history.xul');
240
241                                 g.cgi = new CGI();
242
243                                 retrieve_patron();
244
245                                 init_lists();
246
247                                 retrieve_mbts_for_list();
248
249                                 $('details').addEventListener(
250                                         'command',
251                                         handle_details,
252                                         false
253                                 );
254
255                                 $('add').addEventListener(
256                                         'command',
257                                         handle_add,
258                                         false
259                                 );
260
261                                 if (g.cgi.param('current')) {
262                                         $('caption').setAttribute('label','Current Bills');
263                                         document.title = 'Current Bills';
264                                 } else {
265                                         $('caption').setAttribute('label','Bill History');
266                                         document.title = 'Bill History';
267                                 }
268
269                         } catch(E) {
270                                 var err_msg = "!! This software has encountered an error.  Please tell your friendly " +
271                                         "system administrator or software developer the following:\nbill_history.xul\n" + E + '\n';
272                                 try { g.error.sdump('D_ERROR',err_msg); } catch(E) { dump(err_msg); }
273                                 alert(err_msg);
274                         }
275                 }
276
277                 function handle_add() {
278                         var msg = 'Are you sure you would like to add a billing to bill' + ( g.bill_list_selection.length > 1 ? 's ' : ' ') + g.bill_list_selection + '?';
279                         var r = g.error.yns_alert(msg,'Bill Patron','Yes','No',null,'Check here to confirm this message');
280                         if (r == 0) {
281                                 JSAN.use('util.window');
282                                 var win = new util.window();
283                                 var w = win.open(
284                                         urls.XUL_PATRON_BILL_WIZARD
285                                                 + '?patron_id=' + window.escape(g.patron_id)
286                                                 + '&xact_id=' + window.escape( g.bill_list_selection[0] ),
287                                         'billwizard',
288                                         'chrome,resizable,modal'
289                                 );
290                                 g.bill_list.clear();
291                                 retrieve_mbts_for_list();
292                                 if (typeof window.refresh == 'function') window.refresh();
293                                 if (typeof window.xulG == 'object' && typeof window.xulG.refresh == 'function') window.xulG.refresh();
294                         }
295                 }
296
297                 function handle_details() {
298                         JSAN.use('util.window'); var win = new util.window();
299                         var w = win.open(
300                                 urls.XUL_PATRON_BILL_DETAILS 
301                                 + '?patron_id=' + window.escape(g.patron_id)
302                                 + '&mbts_id=' + window.escape( g.bill_list_selection[0] ),
303                                 'test_billdetails',
304                                 'chrome,resizable'
305                         );
306                         w.xulG = { 'refresh' : function() { 
307                                 if (typeof window.refresh == 'function') window.refresh();
308                                 if (typeof window.xulG == 'object' && typeof window.xulG.refresh == 'function') window.xulG.refresh();
309                         } };
310                 }
311
312                 function print_bills() {
313                         try {
314                                 g.bill_list.on_all_fleshed = function() {
315                                         try {
316                                                 var template = 'bills_historical'; if (g.cgi.param('current')) template = 'bills_current';
317                                                 JSAN.use('patron.util');
318                                                 var params = { 
319                                                         'patron' : patron.util.retrieve_au_via_id(ses(),g.patron_id), 
320                                                         'lib' : g.data.hash.aou[ g.data.list.au[0].ws_ou() ],
321                                                         'staff' : g.data.list.au[0],
322                                                         'header' : g.data.print_list_templates[template].header,
323                                                         'line_item' : g.data.print_list_templates[template].line_item,
324                                                         'footer' : g.data.print_list_templates[template].footer,
325                                                         'type' : g.data.print_list_templates[template].type,
326                                                         'list' : g.bill_list.dump_with_keys(),
327                                                         //'modal' : true,
328                                                 };
329                                                 JSAN.use('util.print'); var print = new util.print();
330                                                 print.tree_list( params );
331                                                 setTimeout(function(){ g.bill_list.on_all_fleshed = null; },0);
332                                         } catch(E) {
333                                                 g.error.standard_unexpected_error_alert('printing bills', E);
334                                         }
335                                 }
336                                 g.bill_list.full_retrieve();
337                         } catch(E) {
338                                 g.error.standard_unexpected_error_alert('printing bills', E);
339                         }
340                 }
341
342         ]]>
343         </script>
344
345         <vbox flex="1" class="my_overflow">
346                 <label id="patron_name" class="patronNameLarge"/>
347
348                 <vbox flex="0">
349                         <groupbox orient="vertical" flex="0" id="summary" hidden="true"/>
350                         <iframe id="copy_summary" hidden="true" flex="0"/>
351                 </vbox>
352
353                 <splitter><grippy/></splitter>
354
355                 <groupbox orient="vertical" flex="1">
356                         <caption id="caption" label="Bill History"/>
357                         <tree id="bill_tree" flex="1" enableColumnDrag="true" seltype="single"/>
358                         <hbox>
359                                 <spacer flex="1"/>
360                                 <button id="add" label="Add Billing" disabled="true"/>
361                                 <button id="details" label="Full Details" disabled="true" accesskey="D"/>
362                         </hbox>
363                 </groupbox>
364
365                 <hbox flex="0">
366                         <button id="opac" label="Show in Catalog" accesskey="S" hidden="true" oncommand="alert('Not Yet Implemented');"/>
367                         <button id="print" label="Print" accesskey="P" hidden="false" oncommand="print_bills();"/>
368                         <spacer flex="1"/>
369                         <button label="Close Window" oncommand="window.close()" accesskey="c"/>
370                 </hbox>
371         </vbox>
372
373
374 </window>
375