]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/data.js
default template for bill_payment
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / OpenILS / data.js
1 dump('entering OpenILS/data.js\n');
2
3 if (typeof OpenILS == 'undefined') OpenILS = {};
4 OpenILS.data = function () {
5
6         JSAN.use('util.error'); this.error = new util.error();
7         JSAN.use('util.network'); this.network = new util.network();
8
9         return this;
10 }
11
12 OpenILS.data.prototype = {
13
14         'list' : {},
15         'hash' : {},
16         'tree' : {},
17
18         'temp' : '',
19
20         'init' : function (params) {
21
22                 try {
23                         if (params && params.via == 'stash') {  
24                                 this.stash_retrieve();
25                         } else {
26                                 this.network_retrieve();
27                         }
28                 
29                 } catch(E) {
30                         this.error.sdump('D_ERROR','Error in OpenILS.data.init('
31                                 +js2JSON(params)+'): ' + js2JSON(E) );
32                 }
33
34
35         },
36
37         'stash' : function () {
38                 try {
39                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
40                         const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
41                         var data_cache=new OpenILS( );
42                         for (var i = 0; i < arguments.length; i++) {
43                                 this.error.sdump('D_DATA','stashing ' + arguments[i] + ' : ' + this[arguments[i]] + '\n');
44                                 data_cache.wrappedJSObject.OpenILS.prototype.data[arguments[i]] = this[arguments[i]];
45                         }
46                 } catch(E) {
47                         this.error.sdump('D_ERROR','Error in OpenILS.data.stash(): ' + js2JSON(E) );
48                 }
49         },
50
51         '_debug_stash' : function() {
52                 try {
53                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
54                         const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
55                         var data_cache=new OpenILS( );
56                         for (var i in data_cache.wrappedJSObject.OpenILS.prototype.data) {
57                                 dump('_debug_stash ' + i + '\n');
58                         }
59                 } catch(E) {
60                         this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
61                 }
62         },
63
64         '_fm_objects' : {
65
66                 'pgt' : [ api.FM_PGT_RETRIEVE.app, api.FM_PGT_RETRIEVE.method, [], true ],
67                 'cit' : [ api.FM_CIT_RETRIEVE.app, api.FM_CIT_RETRIEVE.method, [], true ],
68                 'cst' : [ api.FM_CST_RETRIEVE.app, api.FM_CST_RETRIEVE.method, [], true ],
69                 'acpl' : [ api.FM_ACPL_RETRIEVE.app, api.FM_ACPL_RETRIEVE.method, [], true ],
70                 'ccs' : [ api.FM_CCS_RETRIEVE.app, api.FM_CCS_RETRIEVE.method, [], true ],
71                 'aou' : [ api.FM_AOU_RETRIEVE.app, api.FM_AOU_RETRIEVE.method, [], true ],
72                 'aout' : [ api.FM_AOUT_RETRIEVE.app, api.FM_AOUT_RETRIEVE.method, [], true ]    
73         },
74
75         'stash_retrieve' : function() {
76                 try {
77                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
78                         const OpenILS=new Components.Constructor("@mozilla.org/openils_data_cache;1", "nsIOpenILS");
79                         var data_cache=new OpenILS( );
80                         var dc = data_cache.wrappedJSObject.OpenILS.prototype.data;
81                         for (var i in dc) {
82                                 this.error.sdump('D_DATA','Retrieving ' + i + ' : ' + dc[i] + '\n');
83                                 this[i] = dc[i];
84                         }
85                         if (typeof this.on_complete == 'function') {
86
87                                 this.on_complete();
88                         }
89                 } catch(E) {
90                         this.error.sdump('D_ERROR','Error in OpenILS.data._debug_stash(): ' + js2JSON(E) );
91                 }
92         },
93
94         'print_list_defaults' : function() {
95                 var obj = this;
96                 //if (typeof obj.print_list_templates == 'undefined') {
97                 {
98                         obj.print_list_types = [ 
99                                 'offline_checkout', 
100                                 'offline_checkin', 
101                                 'offline_renew', 
102                                 'offline_inhouse_use', 
103                                 'items', 
104                                 'bills', 
105                                 'payment', 
106                                 'holds', 
107                                 /* 'patrons' */
108                         ];
109                         obj.print_list_templates = { 
110                                 'item_status' : {
111                                         'type' : 'items',
112                                         'header' : 'The following items have been examined:<hr/><ol>',
113                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%\r\n',
114                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
115                                 }, 
116                                 'items_out' : {
117                                         'type' : 'items',
118                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>\r\nYou have the following items:<hr/><ol>',
119                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
120                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%<br/>\r\n<br/>\r\n',
121                                 }, 
122                                 'checkout' : {
123                                         'type' : 'items',
124                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>\r\nYou checked out the following items:<hr/><ol>',
125                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
126                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%<br/>\r\n<br/>\r\n',
127                                 }, 
128                                 'offline_checkout' : {
129                                         'type' : 'offline_checkout',
130                                         'header' : 'Patron %patron_barcode%<br/>\r\nYou checked out the following items:<hr/><ol>',
131                                         'line_item' : '<li>Barcode: %barcode%<br/>\r\nDue: %due_date%\r\n',
132                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
133                                 },
134                                 'checkin' : {
135                                         'type' : 'items',
136                                         'header' : 'You checked in the following items:<hr/><ol>',
137                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%  Call Number: %call_number%\r\n',
138                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
139                                 }, 
140                                 'bill_payment' : {
141                                         'type' : 'payment',
142                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>A receipt of your  transaction:<hr/> <table width="100%"> <tr> <td>Original Balance:</td> <td align="right">$%original_balance%</td> </tr> <tr> <td>Payment Method:</td> <td align="right">%payment_type%</td> </tr> <tr> <td>Payment Received:</td> <td align="right">$%payment_received%</td> </tr> <tr> <td>Payment Applied:</td> <td align="right">$%payment_applied%</td> </tr> <tr> <td>Change Given:</td> <td align="right">$%change_given%</td> </tr> <tr> <td>Credit Given:</td> <td align="right">$%credit_given%</td> </tr> <tr> <td>New Balance:</td> <td align="right">$%new_balance%</td> </tr> </table> <p> Note: %note% </p> <p> Specific bills: <blockquote>',
143                                         'line_item' : 'Bill #%bill_id%  Received: $%payment%<br />',
144                                         'footer' : '</blockquote> </p> <hr />%PINES_CODE% %TODAY_TRIM%<br/> <br/> ',
145                                 },
146                                 'bills_historical' : {
147                                         'type' : 'bills',
148                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>You had the following bills:<hr/><ol>',
149                                         'line_item' : '<dt><b>Bill #%id%</b></dt> <dd> <table> <tr valign="top"><td>Date:</td><td>%xact_start%</td></tr> <tr valign="top"><td>Type:</td><td>%xact_type%</td></tr> <tr valign="top"><td>Last Billing:</td><td>%last_billing_type%<br/>%last_billing_note%</td></tr> <tr valign="top"><td>Total Billed:</td><td>$%total_owed%</td></tr> <tr valign="top"><td>Last Payment:</td><td>%last_payment_type%<br/>%last_payment_note%</td></tr> <tr valign="top"><td>Total Paid:</td><td>$%total_paid%</td></tr> <tr valign="top"><td><b>Balance:</b></td><td><b>$%balance_owed%</b></td></tr> </table><br/>',
150                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
151                                 }, 
152                                 'bills_current' : {
153                                         'type' : 'bills',
154                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>You have the following bills:<hr/><ol>',
155                                         'line_item' : '<dt><b>Bill #%id%</b></dt> <dd> <table> <tr valign="top"><td>Date:</td><td>%xact_start%</td></tr> <tr valign="top"><td>Type:</td><td>%xact_type%</td></tr> <tr valign="top"><td>Last Billing:</td><td>%last_billing_type%<br/>%last_billing_note%</td></tr> <tr valign="top"><td>Total Billed:</td><td>$%total_owed%</td></tr> <tr valign="top"><td>Last Payment:</td><td>%last_payment_type%<br/>%last_payment_note%</td></tr> <tr valign="top"><td>Total Paid:</td><td>$%total_paid%</td></tr> <tr valign="top"><td><b>Balance:</b></td><td><b>$%balance_owed%</b></td></tr> </table><br/>',
156                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
157                                 }, 
158                                 'offline_checkin' : {
159                                         'type' : 'offline_checkin',
160                                         'header' : 'You checked in the following items:<hr/><ol>',
161                                         'line_item' : '<li>Barcode: %barcode%\r\n',
162                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
163                                 },
164                                 'offline_renew' : {
165                                         'type' : 'offline_renew',
166                                         'header' : 'You renewed the following items:<hr/><ol>',
167                                         'line_item' : '<li>Barcode: %barcode%\r\n',
168                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
169                                 },
170                                 'offline_inhouse_use' : {
171                                         'type' : 'offline_inhouse_use',
172                                         'header' : 'You marked the following in-house items used:<hr/><ol>',
173                                         'line_item' : '<li>Barcode: %barcode%\r\nUses: %count%',
174                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
175                                 },
176                                 'holds' : {
177                                         'type' : 'holds',
178                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>\r\nYou have the following titles on hold:<hr/><ol>',
179                                         'line_item' : '<li>%title%\r\n',
180                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%<br/>\r\n<br/>\r\n',
181                                 } 
182                         }; 
183
184                         obj.stash( 'print_list_templates', 'print_list_types' );
185                 }
186         },
187
188         'network_retrieve' : function() {
189                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
190                 var obj = this;
191
192
193                 JSAN.use('util.file'); var file = new util.file('print_list_templates');
194                 obj.print_list_defaults();
195                 if (file._file.exists()) {
196                         try {
197                                 var x = file.get_object();
198                                 if (x) {
199                                         for (var i in x) {
200                                                 obj.print_list_templates[i] = x[i];
201                                         }
202                                         obj.stash('print_list_templates');
203                                 }
204                         } catch(E) {
205                                 alert(E);
206                         }
207                 }
208                 file.close();
209
210                 JSAN.use('util.file');
211                 JSAN.use('util.functional');
212                 JSAN.use('util.fm_utils');
213
214                 function gen_fm_retrieval_func(classname,data) {
215                         var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
216                         return function () {
217                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
218
219                                 function convert() {
220                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
221                                         try {
222                                                 if (obj.list[classname].constructor.name == 'Array') {
223                                                         obj.hash[classname] = 
224                                                                 util.functional.convert_object_list_to_hash(
225                                                                         obj.list[classname]
226                                                                 );
227                                                 }
228                                         } catch(E) {
229
230                                                 obj.error.sdump('D_ERROR',E + '\n');
231                                         }
232
233                                 }
234
235                                 try {
236                                         var level = obj.error.sdump_levels.D_SES_RESULT;
237                                         if (classname == 'aou' || classname == 'my_aou')
238                                                 obj.error.sdump_levels.D_SES_RESULT = false;
239                                         var robj = obj.network.request( app, method, params);
240                                         if (!robj || robj.ilsevent) {
241                                                 obj.error.standard_unexpected_error_alert('The staff client failed to retrieve expected data from this call, "' + method + '"',robj);
242                                                 throw(robj);
243                                         }
244                                         obj.list[classname] = robj;
245                                         obj.error.sdump_levels.D_SES_RESULT = level;
246                                         convert();
247                                         // if cacheable, store an offline copy
248                                         /* FIXME -- we're going to revisit caching and do it differently
249                                         if (cacheable) {
250                                                 var file = new util.file( classname );
251                                                 file.set_object( obj.list[classname] );
252                                         }
253                                         */
254
255                                 } catch(E) {
256                                         // if cacheable, try offline
257                                         if (cacheable) {
258                                                 /* FIXME -- we're going to revisit caching and do it differently
259                                                 try {
260                                                         var file = new util.file( classname );
261                                                         obj.list[classname] = file.get_object(); file.close();
262                                                         convert();
263                                                 } catch(E) {
264                                                         throw(E);
265                                                 }
266                                                 */
267                                                 throw(E); // for now
268                                         } else {
269                                                 throw(E); // for now
270                                         }
271                                 }
272                         }
273                 }
274
275                 this.chain = [];
276
277                 this.chain.push(
278                         function() {
279                                 var f = gen_fm_retrieval_func(
280                                         'au',
281                                         [
282                                                 api.FM_AU_RETRIEVE_VIA_SESSION.app,
283                                                 api.FM_AU_RETRIEVE_VIA_SESSION.method,
284                                                 [ obj.session.key ],
285                                                 false
286                                         ]
287                                 );
288                                 try {
289                                         f();
290                                 } catch(E) {
291                                         var error = 'Error: ' + js2JSON(E);
292                                         obj.error.sdump('D_ERROR',error);
293                                         throw(E);
294                                 }
295                                 obj.list.au = [ obj.list.au ];
296                         }
297                 );
298
299                 this.chain.push(
300                         function() {
301                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
302                                 var f = gen_fm_retrieval_func(
303                                         'my_asv',
304                                         [
305                                                 api.FM_ASV_RETRIEVE_REQUIRED.app,
306                                                 api.FM_ASV_RETRIEVE_REQUIRED.method,
307                                                 [ obj.session.key ],
308                                                 true
309                                         ]
310                                 );
311                                 try {
312                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
313                                         f();
314                                 } catch(E) {
315                                         var error = 'Error: ' + js2JSON(E);
316                                         obj.error.sdump('D_ERROR',error);
317                                         throw(E);
318                                 }
319                         }
320                 );
321
322                 this.chain.push(
323                         function() {
324                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
325                                 var f = gen_fm_retrieval_func(
326                                         'asv',
327                                         [
328                                                 api.FM_ASV_RETRIEVE.app,
329                                                 api.FM_ASV_RETRIEVE.method,
330                                                 [ obj.session.key ],
331                                                 true
332                                         ]
333                                 );
334                                 try {
335                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
336                                         f();
337                                 } catch(E) {
338                                         var error = 'Error: ' + js2JSON(E);
339                                         obj.error.sdump('D_ERROR',error);
340                                         throw(E);
341                                 }
342                         }
343                 );
344
345                 obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
346
347                 for (var i in this._fm_objects) {
348                         this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
349                 }
350
351                 // The previous org_tree call returned a tree, not a list or hash.
352                 this.chain.push(
353                         function () {
354                                 obj.tree.aou = obj.list.aou;
355                                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
356                                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
357                         }
358                 );
359
360                 // Do this after we get the user object
361                 this.chain.push(
362
363                         function() {
364
365                                 gen_fm_retrieval_func('my_aou', 
366                                         [ 
367                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
368                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
369                                                 [ obj.session.key, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
370                                                 true
371                                         ]
372                                 )();
373                         }
374                 );
375
376                 this.chain.push(
377
378                         function () {
379
380                                 gen_fm_retrieval_func( 'my_actsc', 
381                                         [ 
382                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
383                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
384                                                 [ obj.session.key, obj.list.au[0].ws_ou() ],
385                                                 true
386                                         ]
387                                 )();
388                         }
389                 );
390
391                 this.chain.push(
392
393                         function () {
394
395                                 gen_fm_retrieval_func( 'my_asc', 
396                                         [ 
397                                                 api.FM_ASC_RETRIEVE_VIA_AOU.app,
398                                                 api.FM_ASC_RETRIEVE_VIA_AOU.method,
399                                                 [ obj.session.key, obj.list.au[0].ws_ou() ],
400                                                 true
401                                         ]
402                                 )();
403                         }
404                 );
405
406
407                 this.chain.push(
408                         function() {
409                                 var f = gen_fm_retrieval_func(
410                                         'cnct',
411                                         [
412                                                 api.FM_CNCT_RETRIEVE.app,
413                                                 api.FM_CNCT_RETRIEVE.method,
414                                                 [ obj.list.au[0].ws_ou() ], 
415                                                 false
416                                         ]
417                                 );
418                                 try {
419                                         f();
420                                 } catch(E) {
421                                         var error = 'Error: ' + js2JSON(E);
422                                         obj.error.sdump('D_ERROR',error);
423                                         throw(E);
424                                 }
425                         }
426                 );
427
428
429                 if (typeof this.on_complete == 'function') {
430
431                         this.chain.push( this.on_complete );
432                 }
433                 JSAN.use('util.exec'); this.exec = new util.exec();
434                 this.exec.on_error = function(E) { 
435                 
436                         if (typeof obj.on_error == 'function') {
437                                 obj.on_error();
438                         } else {
439                                 alert('oops: ' + E ); 
440                         }
441
442                         return false; /* break chain */
443                 }
444
445                 this.exec.chain( this.chain );
446
447         }
448 }
449
450 dump('exiting OpenILS/data.js\n');