]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/OpenILS/data.js
default print templates for historical bills and current bills
[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                                 'holds', 
106                                 'patrons' 
107                         ];
108                         obj.print_list_templates = { 
109                                 'item_status' : {
110                                         'type' : 'items',
111                                         'header' : 'The following items have been examined:<hr/><ol>',
112                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%\r\n',
113                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
114                                 }, 
115                                 'items_out' : {
116                                         'type' : 'items',
117                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>\r\nYou have the following items:<hr/><ol>',
118                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
119                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%<br/>\r\n<br/>\r\n',
120                                 }, 
121                                 'checkout' : {
122                                         'type' : 'items',
123                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>\r\nYou checked out the following items:<hr/><ol>',
124                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode% Due: %due_date%\r\n',
125                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%<br/>\r\n<br/>\r\n',
126                                 }, 
127                                 'offline_checkout' : {
128                                         'type' : 'offline_checkout',
129                                         'header' : 'Patron %patron_barcode%<br/>\r\nYou checked out the following items:<hr/><ol>',
130                                         'line_item' : '<li>Barcode: %barcode%<br/>\r\nDue: %due_date%\r\n',
131                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
132                                 },
133                                 'checkin' : {
134                                         'type' : 'items',
135                                         'header' : 'You checked in the following items:<hr/><ol>',
136                                         'line_item' : '<li>%title%<br/>\r\nBarcode: %barcode%  Call Number: %call_number%\r\n',
137                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
138                                 }, 
139                                 'bills_historical' : {
140                                         'type' : 'bills',
141                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>You had the following bills:<hr/><ol>',
142                                         '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/>',
143                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
144                                 }, 
145                                 'bills_current' : {
146                                         'type' : 'bills',
147                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>You have the following bills:<hr/><ol>',
148                                         '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/>',
149                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\n<br/>\r\n',
150                                 }, 
151                                 'offline_checkin' : {
152                                         'type' : 'offline_checkin',
153                                         'header' : 'You checked in the following items:<hr/><ol>',
154                                         'line_item' : '<li>Barcode: %barcode%\r\n',
155                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
156                                 },
157                                 'offline_renew' : {
158                                         'type' : 'offline_renew',
159                                         'header' : 'You renewed the following items:<hr/><ol>',
160                                         'line_item' : '<li>Barcode: %barcode%\r\n',
161                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
162                                 },
163                                 'offline_inhouse_use' : {
164                                         'type' : 'offline_inhouse_use',
165                                         'header' : 'You marked the following in-house items used:<hr/><ol>',
166                                         'line_item' : '<li>Barcode: %barcode%\r\nUses: %count%',
167                                         'footer' : '</ol><hr />%TODAY_TRIM%<br/>\r\n<br/>\r\n',
168                                 },
169                                 'holds' : {
170                                         'type' : 'holds',
171                                         'header' : 'Welcome %PATRON_FIRSTNAME%, to %LIBRARY%!<br/>\r\nYou have the following titles on hold:<hr/><ol>',
172                                         'line_item' : '<li>%title%\r\n',
173                                         'footer' : '</ol><hr />%PINES_CODE% %TODAY_TRIM%<br/>\r\nYou were helped by %STAFF_FIRSTNAME% %STAFF_LASTNAME%<br/>\r\n<br/>\r\n',
174                                 } 
175                         }; 
176
177                         obj.stash( 'print_list_templates', 'print_list_types' );
178                 }
179         },
180
181         'network_retrieve' : function() {
182                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
183                 var obj = this;
184
185
186                 JSAN.use('util.file'); var file = new util.file('print_list_templates');
187                 obj.print_list_defaults();
188                 if (file._file.exists()) {
189                         try {
190                                 var x = file.get_object();
191                                 if (x) {
192                                         for (var i in x) {
193                                                 obj.print_list_templates[i] = x[i];
194                                         }
195                                         obj.stash('print_list_templates');
196                                 }
197                         } catch(E) {
198                                 alert(E);
199                         }
200                 }
201                 file.close();
202
203                 JSAN.use('util.file');
204                 JSAN.use('util.functional');
205                 JSAN.use('util.fm_utils');
206
207                 function gen_fm_retrieval_func(classname,data) {
208                         var app = data[0]; var method = data[1]; var params = data[2]; var cacheable = data[3];
209                         return function () {
210                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
211
212                                 function convert() {
213                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
214                                         try {
215                                                 if (obj.list[classname].constructor.name == 'Array') {
216                                                         obj.hash[classname] = 
217                                                                 util.functional.convert_object_list_to_hash(
218                                                                         obj.list[classname]
219                                                                 );
220                                                 }
221                                         } catch(E) {
222
223                                                 obj.error.sdump('D_ERROR',E + '\n');
224                                         }
225
226                                 }
227
228                                 try {
229                                         var level = obj.error.sdump_levels.D_SES_RESULT;
230                                         if (classname == 'aou' || classname == 'my_aou')
231                                                 obj.error.sdump_levels.D_SES_RESULT = false;
232                                         var robj = obj.network.request( app, method, params);
233                                         if (!robj || robj.ilsevent) {
234                                                 obj.error.standard_unexpected_error_alert('The staff client failed to retrieve expected data from this call, "' + method + '"',robj);
235                                                 throw(robj);
236                                         }
237                                         obj.list[classname] = robj;
238                                         obj.error.sdump_levels.D_SES_RESULT = level;
239                                         convert();
240                                         // if cacheable, store an offline copy
241                                         /* FIXME -- we're going to revisit caching and do it differently
242                                         if (cacheable) {
243                                                 var file = new util.file( classname );
244                                                 file.set_object( obj.list[classname] );
245                                         }
246                                         */
247
248                                 } catch(E) {
249                                         // if cacheable, try offline
250                                         if (cacheable) {
251                                                 /* FIXME -- we're going to revisit caching and do it differently
252                                                 try {
253                                                         var file = new util.file( classname );
254                                                         obj.list[classname] = file.get_object(); file.close();
255                                                         convert();
256                                                 } catch(E) {
257                                                         throw(E);
258                                                 }
259                                                 */
260                                                 throw(E); // for now
261                                         } else {
262                                                 throw(E); // for now
263                                         }
264                                 }
265                         }
266                 }
267
268                 this.chain = [];
269
270                 this.chain.push(
271                         function() {
272                                 var f = gen_fm_retrieval_func(
273                                         'au',
274                                         [
275                                                 api.FM_AU_RETRIEVE_VIA_SESSION.app,
276                                                 api.FM_AU_RETRIEVE_VIA_SESSION.method,
277                                                 [ obj.session.key ],
278                                                 false
279                                         ]
280                                 );
281                                 try {
282                                         f();
283                                 } catch(E) {
284                                         var error = 'Error: ' + js2JSON(E);
285                                         obj.error.sdump('D_ERROR',error);
286                                         throw(E);
287                                 }
288                                 obj.list.au = [ obj.list.au ];
289                         }
290                 );
291
292                 this.chain.push(
293                         function() {
294                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
295                                 var f = gen_fm_retrieval_func(
296                                         'my_asv',
297                                         [
298                                                 api.FM_ASV_RETRIEVE_REQUIRED.app,
299                                                 api.FM_ASV_RETRIEVE_REQUIRED.method,
300                                                 [ obj.session.key ],
301                                                 true
302                                         ]
303                                 );
304                                 try {
305                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
306                                         f();
307                                 } catch(E) {
308                                         var error = 'Error: ' + js2JSON(E);
309                                         obj.error.sdump('D_ERROR',error);
310                                         throw(E);
311                                 }
312                         }
313                 );
314
315                 this.chain.push(
316                         function() {
317                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
318                                 var f = gen_fm_retrieval_func(
319                                         'asv',
320                                         [
321                                                 api.FM_ASV_RETRIEVE.app,
322                                                 api.FM_ASV_RETRIEVE.method,
323                                                 [ obj.session.key ],
324                                                 true
325                                         ]
326                                 );
327                                 try {
328                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
329                                         f();
330                                 } catch(E) {
331                                         var error = 'Error: ' + js2JSON(E);
332                                         obj.error.sdump('D_ERROR',error);
333                                         throw(E);
334                                 }
335                         }
336                 );
337
338                 obj.error.sdump('D_DEBUG','_fm_objects = ' + js2JSON(this._fm_objects) + '\n');
339
340                 for (var i in this._fm_objects) {
341                         this.chain.push( gen_fm_retrieval_func(i,this._fm_objects[i]) );
342                 }
343
344                 // The previous org_tree call returned a tree, not a list or hash.
345                 this.chain.push(
346                         function () {
347                                 obj.tree.aou = obj.list.aou;
348                                 obj.list.aou = util.fm_utils.flatten_ou_branch( obj.tree.aou );
349                                 obj.hash.aou = util.functional.convert_object_list_to_hash( obj.list.aou );
350                         }
351                 );
352
353                 // Do this after we get the user object
354                 this.chain.push(
355
356                         function() {
357
358                                 gen_fm_retrieval_func('my_aou', 
359                                         [ 
360                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.app,
361                                                 api.FM_AOU_RETRIEVE_RELATED_VIA_SESSION.method,
362                                                 [ obj.session.key, obj.list.au[0].ws_ou() ], /* use ws_ou and not home_ou */
363                                                 true
364                                         ]
365                                 )();
366                         }
367                 );
368
369                 this.chain.push(
370
371                         function () {
372
373                                 gen_fm_retrieval_func( 'my_actsc', 
374                                         [ 
375                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.app,
376                                                 api.FM_ACTSC_RETRIEVE_VIA_AOU.method,
377                                                 [ obj.session.key, obj.list.au[0].ws_ou() ],
378                                                 true
379                                         ]
380                                 )();
381                         }
382                 );
383
384                 this.chain.push(
385
386                         function () {
387
388                                 gen_fm_retrieval_func( 'my_asc', 
389                                         [ 
390                                                 api.FM_ASC_RETRIEVE_VIA_AOU.app,
391                                                 api.FM_ASC_RETRIEVE_VIA_AOU.method,
392                                                 [ obj.session.key, obj.list.au[0].ws_ou() ],
393                                                 true
394                                         ]
395                                 )();
396                         }
397                 );
398
399
400                 this.chain.push(
401                         function() {
402                                 var f = gen_fm_retrieval_func(
403                                         'cnct',
404                                         [
405                                                 api.FM_CNCT_RETRIEVE.app,
406                                                 api.FM_CNCT_RETRIEVE.method,
407                                                 [ obj.list.au[0].ws_ou() ], 
408                                                 false
409                                         ]
410                                 );
411                                 try {
412                                         f();
413                                 } catch(E) {
414                                         var error = 'Error: ' + js2JSON(E);
415                                         obj.error.sdump('D_ERROR',error);
416                                         throw(E);
417                                 }
418                         }
419                 );
420
421
422                 if (typeof this.on_complete == 'function') {
423
424                         this.chain.push( this.on_complete );
425                 }
426                 JSAN.use('util.exec'); this.exec = new util.exec();
427                 this.exec.on_error = function(E) { 
428                 
429                         if (typeof obj.on_error == 'function') {
430                                 obj.on_error();
431                         } else {
432                                 alert('oops: ' + E ); 
433                         }
434
435                         return false; /* break chain */
436                 }
437
438                 this.exec.chain( this.chain );
439
440         }
441 }
442
443 dump('exiting OpenILS/data.js\n');