908b1ac93dfd4fadd412c6bc1fb9e97c6ddfec85
[Evergreen.git] / Evergreen / staff_client / chrome / content / evergreen / util / print.js
1 sdump('D_TRACE','Loading print.js\n');
2
3 var print_crlf = '<br />\r\n';
4
5 // Higher-level
6
7 function print_itemsout_receipt(params,sample_view) {
8         print_circ_receipt('itemsout',params,sample_view);
9 }
10
11 function print_checkout_receipt(params,sample_view) {
12         print_circ_receipt('checkout',params,sample_view);
13 }
14
15 function print_circ_receipt(circ_type,params,sample_view) {
16         sdump('D_PRINT',arg_dump(arguments));
17         var s = ''; params.current_circ = new circ(); params.current_copy = new acp(); params.current_mvr = new mvr();
18         if (params.header) { s += print_template_replace(params.header, params); }
19         var circs;
20         switch(circ_type) {
21                 case 'itemsout' : circs = params.au.checkouts(); break;
22                 case 'checkout' : circs = params.au._current_checkouts; break;
23                 default: alert('bad!'); break;
24         }
25         for (var i = 0; i < circs.length; i++) {
26                 params.current_circ = circs[i].circ;
27                 params.current_copy = circs[i].copy;
28                 params.current_mvr = circs[i].record;
29                 params.current_index = i;
30                 s += print_template_replace(params.line_item, params); 
31         }
32         if (params.footer) { s += print_template_replace(params.footer, params); }
33         s = s.replace( /\n/g, print_crlf );
34         if (sample_view) {
35                 sample_view.setAttribute( 'src', 'data:text/html,<html>' + s + '</html>\r\n' );
36         } else {
37                 sPrint( s, true );
38         }
39 }
40
41 function print_template_replace(s,params) {
42
43                 function trunc(t) {
44                         if (params.truncate) {
45                                 try {
46                                         return t.toString().substr(0,params.truncate);
47                                 } catch(E) {
48                                         return t;
49                                 }
50                         } else {
51                                 return t;
52                         }
53                 }
54                 function ttrunc(t) {
55                         if (params.title_truncate) {
56                                 try {
57                                         return t.toString().substr(0,params.title_truncate);
58                                 } catch(E) {
59                                         return t;
60                                 }
61                         } else {
62                                 return t;
63                         }
64                 }
65                 function atrunc(t) {
66                         if (params.author_truncate) {
67                                 try {
68                                         return t.toString().substr(0,params.author_truncate);
69                                 } catch(E) {
70                                         return t;
71                                 }
72                         } else {
73                                 return t;
74                         }
75                 }
76
77
78
79                 var b = s.match( /%TRUNC.{0,3}:\s*(\d+)%/ );
80                 if (b) params.truncate = b[1];
81
82                 try{s=s.replace(/%TRUNC.{0,3}:\s*\d+%/g,'');}
83                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
84                 try{s=s.replace(/%LIBRARY%/g,trunc(params.lib.name()));}
85                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
86                 try{s=s.replace(/%PINES_CODE%/g,trunc(params.lib.shortname()));}
87                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
88
89                 try{s=s.replace(/%PATRON_LASTNAME%/g,trunc(params.au.family_name()));}
90                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
91                 try{s=s.replace(/%PATRON_FIRSTNAME%/g,trunc(params.au.first_given_name()));}
92                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
93                 try{s=s.replace(/%PATRON_MIDDLENAME%/g,trunc(params.au.second_given_name()));}
94                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
95                 try{s=s.replace(/%PATRON_BARCODE%/g,trunc(params.au.card().barcode()));}
96                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
97
98                 try{s=s.replace(/%STAFF_LASTNAME%/g,trunc(params.staff.family_name()));}
99                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
100                 try{s=s.replace(/%STAFF_FIRSTNAME%/g,trunc(params.staff.first_given_name()));}
101                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
102                 try{s=s.replace(/%STAFF_MIDDLENAME%/g,trunc(params.staff.second_given_name()));}
103                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
104                 //try{s=s.replace(/%STAFF_BARCODE%/g,trunc(params.staff.card().barcode()));}
105                 //      catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
106
107                 try{s=s.replace(/%TODAY%/g,trunc(new Date()));}
108                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
109                 try{s=s.replace(/%TODAY_m%/g,trunc(formatted_date(new Date(),'%m')));}
110                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
111                 try{s=s.replace(/%TODAY_d%/g,trunc(formatted_date(new Date(),'%d')));}
112                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
113                 try{s=s.replace(/%TODAY_Y%/g,trunc(formatted_date(new Date(),'%Y')));}
114                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
115                 try{s=s.replace(/%TODAY_H%/g,trunc(formatted_date(new Date(),'%H')));}
116                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
117                 try{s=s.replace(/%TODAY_I%/g,trunc(formatted_date(new Date(),'%I')));}
118                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
119                 try{s=s.replace(/%TODAY_M%/g,trunc(formatted_date(new Date(),'%M')));}
120                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
121                 try{s=s.replace(/%TODAY_D%/g,trunc(formatted_date(new Date(),'%D')));}
122                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
123                 try{s=s.replace(/%TODAY_F%/g,trunc(formatted_date(new Date(),'%F')));}
124                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
125                 
126                 try{s=s.replace(/%OUT%/g,trunc(params.current_circ.xact_start()));}
127                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
128                 try{s=s.replace(/%OUT_m%/g,trunc(formatted_date(params.current_circ.xact_start(),'%m')));}
129                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
130                 try{s=s.replace(/%OUT_d%/g,trunc(formatted_date(params.current_circ.xact_start(),'%d')));}
131                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
132                 try{s=s.replace(/%OUT_Y%/g,trunc(formatted_date(params.current_circ.xact_start(),'%Y')));}
133                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
134                 try{s=s.replace(/%OUT_H%/g,trunc(formatted_date(params.current_circ.xact_start(),'%H')));}
135                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
136                 try{s=s.replace(/%OUT_I%/g,trunc(formatted_date(params.current_circ.xact_start(),'%I')));}
137                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
138                 try{s=s.replace(/%OUT_M%/g,trunc(formatted_date(params.current_circ.xact_start(),'%M')));}
139                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
140                 try{s=s.replace(/%OUT_D%/g,trunc(formatted_date(params.current_circ.xact_start(),'%D')));}
141                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
142                 try{s=s.replace(/%OUT_F%/g,trunc(formatted_date(params.current_circ.xact_start(),'%F')));}
143                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
144
145                 try{s=s.replace(/%DUE%/g,trunc(params.current_circ.due_date()));}
146                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
147                 try{s=s.replace(/%DUE_m%/g,trunc(formatted_date(params.current_circ.due_date(),'%m')));}
148                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
149                 try{s=s.replace(/%DUE_d%/g,trunc(formatted_date(params.current_circ.due_date(),'%d')));}
150                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
151                 try{s=s.replace(/%DUE_Y%/g,trunc(formatted_date(params.current_circ.due_date(),'%Y')));}
152                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
153                 try{s=s.replace(/%DUE_H%/g,trunc(formatted_date(params.current_circ.due_date(),'%H')));}
154                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
155                 try{s=s.replace(/%DUE_I%/g,trunc(formatted_date(params.current_circ.due_date(),'%I')));}
156                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
157                 try{s=s.replace(/%DUE_M%/g,trunc(formatted_date(params.current_circ.due_date(),'%M')));}
158                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
159                 try{s=s.replace(/%DUE_D%/g,trunc(formatted_date(params.current_circ.due_date(),'%D')));}
160                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
161                 try{s=s.replace(/%DUE_F%/g,trunc(formatted_date(params.current_circ.due_date(),'%F')));}
162                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
163
164                 try{s=s.replace(/%DURATION%/g,trunc(params.current_circ.duration()));}
165                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
166                 
167                 try{s=s.replace(/%COPY_BARCODE%/g,trunc(params.current_copy.barcode()));}
168                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
169                 
170                 var tb = s.match( /%TITLE:?\s*(\d*)%/ );
171                 if (tb) params.title_truncate = tb[1];
172
173                 try{s=s.replace(/%TITLE:?\s*\d*%/g,ttrunc(params.current_mvr.title()));}
174                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
175
176                 var ab = s.match( /%AUTHOR:?\s*(\d*)%/ );
177                 if (ab) params.author_truncate = ab[1];
178
179                 try{s=s.replace(/%AUTHOR%/g,atrunc(params.current_mvr.author()));}
180                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
181                 try{s=s.replace(/%PUBLISHER%/g,trunc(params.current_mvr.publisher()));}
182                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
183                 try{s=s.replace(/%PUBDATE%/g,trunc(params.current_mvr.pubdate()));}
184                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
185
186                 try{s=s.replace(/%NUMBER%/g,(params.current_index+1));}
187                         catch(E){sdump('D_ERROR','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
188
189                 return s;
190 }
191
192 // Lower-level
193
194 function last_sPrint() {
195         if (mw.G.last_receipt) sPrint( mw.G.last_receipt, false );
196 }
197
198 function sPrint(s,silent,params) {
199         sdump('D_PRINT',arg_dump(arguments));
200         mw.G.last_receipt = s;
201         var w = new_window('data:text/html,<html>' + s + '</html>\r\n', { 'window_name':'LastPrint' });
202         w.minimize(); mw.minimize();
203         setTimeout(
204                 function() {
205                         if (!mw.printDebug) {
206                                 NSPrint(w,silent,params); w.minimize(); w.close(); mw.minimize();
207                         } else {
208                                 sdump('D_PRINT','Not printing due to printDebug flag.');
209                         }
210                 },0
211         );
212 }
213
214 function NSPrint(w,silent,params)
215 {
216         sdump('D_PRINT',arg_dump(arguments));
217         if (!w) { w = this; }
218         try {
219                 var webBrowserPrint = w
220                         .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
221                         .getInterface(Components.interfaces.nsIWebBrowserPrint);
222                 sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
223                 if (webBrowserPrint) {
224                         var gPrintSettings = GetPrintSettings();
225                         if (silent) gPrintSettings.printSilent = true;
226                         else gPrintSettings.printSilent = false;
227                         /*
228                         gPrintSettings.marginTop = 0;
229                         gPrintSettings.marginLeft = 0;
230                         gPrintSettings.marginBottom = 0;
231                         gPrintSettings.marginRight = 0;
232                         */
233                         if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
234                         gPrintSettings.headerStrLeft = '';
235                         gPrintSettings.headerStrCenter = '';
236                         gPrintSettings.headerStrRight = '';
237                         gPrintSettings.footerStrLeft = '';
238                         gPrintSettings.footerStrCenter = '';
239                         gPrintSettings.footerStrRight = '';
240                         sdump('D_PRINT','gPrintSettings = ' + js2JSON(gPrintSettings));
241                         webBrowserPrint.print(gPrintSettings, null);
242                         //alert('Should be printing\n');
243                         sdump('D_PRINT','Should be printing\n');
244                 } else {
245                         //alert('Should not be printing\n');
246                         sdump('D_PRINT','Should not be printing\n');
247                 }
248         } catch (e) {
249                 //alert('Probably not printing: ' + e);
250         // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
251         // causing an exception to be thrown which we catch here.
252         // Unfortunately this will also consume helpful failures, so add a
253                 sdump('D_PRINT','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
254         // if you need to debug
255         }
256 }
257
258 var gPrintSettings = null;
259
260 function GetPrintSettings()
261  {
262         sdump('D_PRINT',arg_dump(arguments));
263    try {
264      if (gPrintSettings == null) {
265        var pref = Components.classes["@mozilla.org/preferences-service;1"]
266                             .getService(Components.interfaces.nsIPrefBranch);
267        if (pref) {
268          gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
269          gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
270        }
271  
272        var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
273                                          .getService(Components.interfaces.nsIPrintSettingsService);
274        if (gPrintSettingsAreGlobal) {
275          gPrintSettings = printService.globalPrintSettings;
276          setPrinterDefaultsForSelectedPrinter(printService);
277        } else {
278          gPrintSettings = printService.newPrintSettings;
279        }
280      }
281    } catch (e) {
282      sdump('D_PRINT',"GetPrintSettings() "+e+"\n");
283      alert("GetPrintSettings() "+e+"\n");
284    }
285  
286    return gPrintSettings;
287  }
288
289 function setPrinterDefaultsForSelectedPrinter(aPrintService)
290  {
291         sdump('D_PRINT',arg_dump(arguments));
292    if (gPrintSettings.printerName == "") {
293      gPrintSettings.printerName = aPrintService.defaultPrinterName;
294    }
295  
296    // First get any defaults from the printer 
297    aPrintService.initPrintSettingsFromPrinter(gPrintSettings.printerName, gPrintSettings);
298  
299    // now augment them with any values from last time
300    aPrintService.initPrintSettingsFromPrefs(gPrintSettings, true, gPrintSettings.kInitSaveAll);
301  }
302