33ac54a529448ee627444c4679e9dd357a41b567
[fa-stable.git] / reporting / includes / excel_report.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 include_once($path_to_root . "/reporting/includes/Workbook.php");
13 include_once($path_to_root . "/admin/db/company_db.inc");
14 include_once($path_to_root . "/admin/db/fiscalyears_db.inc");
15 include_once($path_to_root . "/config.php");
16 // xls version
17 class FrontReport extends Spreadsheet_Excel_Writer_Workbook
18 {
19         var $size;
20         var $company;
21         var $user;
22         var $host;
23         var $fiscal_year;
24         var $title;
25         var $filename;
26         var $unique_name;
27         var $path;
28         var $code;
29         var $bottomMargin = 0;
30         var $lineHeight;
31         var $leftMargin = 0;
32         var $pageWidth;         // dummy
33         var $rightMargin;       // dummy
34
35         var $cols;
36         var $params;
37         var $headers;
38         var $aligns;
39         var $headers2;
40         var $aligns2;
41         var $cols2;
42         var $fontSize;
43         var $oldFontSize;
44         var $currency;
45         var $row = 9999999;
46         var $y;
47         var $numcols;
48         var $excelColWidthFactor;
49         var $endLine;
50
51         var $formatTitle;
52         var $formatDateTime;
53         var $formatDate;
54         var $formatHeaderLeft;
55         var $formatHeaderRight;
56         var $formatFooter;
57         var $formatAmount = array();
58         var $headerFunc;
59         
60         var $sheet;
61
62         function FrontReport($title, $filename, $size = 'A4', $fontsize = 9, $orientation = 'P', $margins = NULL, $excelColWidthFactor = 6.5)
63         {
64                 global $dateseps, $page_security;
65                 if (!$_SESSION["wa_current_user"]->can_access_page($page_security))
66                 {
67                         display_error(_("The security settings on your account do not permit you to print this report"));
68                         end_page();
69                         exit;
70                 }
71                 $this->size = $size;
72                 $this->title = $title;
73                 $this->lineHeight = 12;
74                 $this->endLine = 760;
75                 $this->SetFontSize($fontsize);
76                 $this->oldFontSize = 0;
77                 $this->y = 0;
78                 $this->currency = '';
79                 $this->excelColWidthFactor = $excelColWidthFactor;
80                 $rtl = ($_SESSION['language']->dir == 'rtl');
81                 $this->code = strtolower($_SESSION['language']->encoding);
82                 $this->filename = $filename.".xls";
83                 $this->unique_name = uniqid('').".xls";
84                 $this->path = company_path(). '/pdf_files';
85                 $this->Spreadsheet_Excel_Writer_Workbook($this->path."/".$this->unique_name);
86                 //$this->setCountry(48);
87                 if ($this->code != "iso-8859-1")
88                         $this->setVersion(8); // set biff version to 8 (0x0006 internal)
89                 $this->sheet =& $this->addWorksheet($this->worksheetNameGenerator($this->title));
90                 if ($this->code != "iso-8859-1")
91                         $this->sheet->setInputEncoding($this->code); // set sheet encoding
92                 if ($rtl)
93                         $this->sheet->setRTL();
94                 $this->formatTitle =& $this->addFormat();       
95                 $this->formatTitle->setSize(16);
96                 $this->formatTitle->setBold();
97                 $this->formatTitle->setAlign($rtl ? 'right' : 'left');
98                 $this->formatTitle->setTop(2);
99                 $this->formatTitle->setTopColor('gray');
100
101                 $how = user_date_format();
102                 $sep = $dateseps[user_date_sep()];
103                 if ($sep == '.')
104                         $sep = "\\.";
105                 if ($how == 0)
106                 {
107                         $dateformat_long = "mm{$sep}dd{$sep}yyyy\ \ hh:mm\ am/pm";
108                         $dateformat = "mm{$sep}dd{$sep}yyyy";
109                 }       
110                 elseif ($how == 1)      
111                 {
112                         $dateformat_long = "dd{$sep}mm{$sep}yyyy\ \ hh:mm";
113                         $dateformat = "dd{$sep}mm{$sep}yyyy";
114                 }       
115                 else    
116                 {
117                         $dateformat_long = "yyyy{$sep}mm{$sep}dd\ \ hh:mm";
118                         $dateformat = "yyyy{$sep}mm{$sep}dd";
119                 }       
120                 $this->formatDateTime =& $this->addFormat();
121                 $this->formatDateTime->setNumFormat($dateformat_long);
122                 $this->formatDateTime->setAlign($rtl ? 'right' : 'left');
123                 $this->formatDate =& $this->addFormat();
124                 $this->formatDate->setNumFormat($dateformat);
125                 $this->formatDate->setAlign($rtl ? 'right' : 'left');
126                 $this->formatRight =& $this->addFormat();
127                 $this->formatRight->setAlign($rtl ? 'left' : 'right');
128                 $this->formatLeft =& $this->addFormat();
129                 $this->formatLeft->setAlign($rtl ? 'right' : 'left');
130                 
131                 $this->formatHeaderLeft =& $this->addFormat();
132                 $this->formatHeaderLeft->setItalic();
133                 $this->formatHeaderLeft->setTop(2);
134                 $this->formatHeaderLeft->setTopColor('gray');
135                 $this->formatHeaderLeft->setBottom(2);
136                 $this->formatHeaderLeft->setBottomColor('gray');
137                 $this->formatHeaderLeft->setAlign('vcenter');
138                 $this->formatTopHeaderLeft =& $this->addFormat();
139                 $this->formatTopHeaderLeft->setItalic();
140                 $this->formatTopHeaderLeft->setTop(2);
141                 $this->formatTopHeaderLeft->setTopColor('gray');
142                 $this->formatTopHeaderLeft->setAlign('vcenter');
143                 $this->formatBottomHeaderLeft =& $this->addFormat();
144                 $this->formatBottomHeaderLeft->setItalic();
145                 $this->formatBottomHeaderLeft->setBottom(2);
146                 $this->formatBottomHeaderLeft->setBottomColor('gray');
147                 $this->formatBottomHeaderLeft->setAlign('vcenter');
148                 $this->formatDate->setAlign($rtl ? 'right' : 'left');
149                 $this->formatHeaderRight =& $this->addFormat();
150                 $this->formatHeaderRight->setItalic();
151                 $this->formatHeaderRight->setTop(2);
152                 $this->formatHeaderRight->setTopColor('gray');
153                 $this->formatHeaderRight->setBottom(2);
154                 $this->formatHeaderRight->setBottomColor('gray');
155                 $this->formatHeaderRight->setAlign('vcenter');
156                 $this->formatHeaderRight->setAlign('right');
157                 $this->formatTopHeaderRight =& $this->addFormat();
158                 $this->formatTopHeaderRight->setItalic();
159                 $this->formatTopHeaderRight->setTop(2);
160                 $this->formatTopHeaderRight->setTopColor('gray');
161                 $this->formatTopHeaderRight->setAlign('vcenter');
162                 $this->formatTopHeaderRight->setAlign('right');
163                 $this->formatBottomHeaderRight =& $this->addFormat();
164                 $this->formatBottomHeaderRight->setItalic();
165                 $this->formatBottomHeaderRight->setBottom(2);
166                 $this->formatBottomHeaderRight->setBottomColor('gray');
167                 $this->formatBottomHeaderRight->setAlign('vcenter');
168                 $this->formatBottomHeaderRight->setAlign('right');
169                 $this->formatFooter =& $this->addFormat();
170                 $this->formatFooter->setTop(2);
171                 $this->formatFooter->setTopColor('gray');
172                 $this->SetHeaderType("header");
173         }
174         
175
176         /*
177                 Set header handler
178         */
179         function SetHeaderType($name) {
180                 $this->headerFunc = $name;
181         }
182         // Check a given name to see if it's a valid Excel worksheet name,
183         // and fix if necessary
184         function worksheetNameGenerator($name)
185         {
186                 // First, strip out characters which aren't allowed
187                 $illegal_chars = array(':', '\\', '/', '?', '*', '[', ']');
188                 for ($i = 0; $i < count($illegal_chars); $i++)
189                         $name = str_replace($illegal_chars[$i], '', $name);
190                 // Now, if name is longer than 31 chars, truncate it
191                 if (strlen($name) > 31)
192                         $name = substr($name, 0, 31);
193                 return $name;
194         }
195         
196         function NumFormat($dec) 
197         {
198                 if (!isset($this->formatAmount[$dec]))
199                 {
200                         //global $thoseps,$decseps;
201                         $dec = (int)$dec;
202                         //$tsep = $thoseps[user_tho_sep()];
203                         //$dsep = $decseps[user_dec_sep()];
204                         $tsep = ',';
205                         $dsep = '.';
206                         $format = "###{$tsep}###{$tsep}###{$tsep}##0";
207                         if ($dec>0)
208                                 $format .= "{$dsep}".str_repeat('0',$dec);
209                         $this->formatAmount[$dec] =& $this->addFormat();
210                         $this->formatAmount[$dec]->setNumFormat($format);
211                         $this->formatAmount[$dec]->setAlign('right');
212                 }
213                 return $this->formatAmount[$dec];
214         }
215
216         function Font($fontname = '', $style = 'normal')
217         {
218         }
219         
220         function SetFontSize($size)
221         {
222                 $this->fontSize = $size;
223         }
224
225         function Info($params, $cols, $headers, $aligns,
226                 $cols2 = null, $headers2 = null, $aligns2 = null)
227         {
228                 global $app_title, $version, $power_by, $power_url;
229                 $this->company = get_company_prefs();
230                 $year = get_current_fiscalyear();
231                 if ($year['closed'] == 0)
232                         $how = _("Active");
233                 else
234                         $how = _("Closed");
235                 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . "  " . "(" . $how . ")";
236                 $this->user = $_SESSION["wa_current_user"]->name;
237                 $this->host = $_SERVER['SERVER_NAME'];
238                 $this->params = $params;
239                 $this->cols = $cols;
240                 $this->headers = $headers;
241                 $this->aligns = $aligns;
242                 $this->cols2 = $cols2;
243                 $this->headers2 = $headers2;
244                 $this->aligns2 = $aligns2;
245                 $this->numcols = count($this->headers);
246                 $tcols = count($this->headers2);
247                 if ($tcols > $this->numcols)
248                         $this->numcols = $tcols;
249                 for ($i = 0; $i < $this->numcols; $i++)
250                         $this->sheet->setColumn($i, $i, $this->px2units($this->cols[$i + 1] - $this->cols[$i]));
251         }
252
253         function Header()
254         {
255                 $tcol = $this->numcols - 1;
256                 $this->sheet->setRow($this->y, 20);
257                 for ($i = 0; $i < $this->numcols; $i++)
258                         $this->sheet->writeBlank($this->y, $i, $this->formatTitle);
259                 $this->sheet->writeString($this->y, 0, $this->title, $this->formatTitle);
260                 $this->sheet->mergeCells($this->y, 0, $this->y, $tcol);
261                 $this->NewLine();
262                 $str = _("Print Out Date") . ':';
263                 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
264                 $this->sheet->writeString($this->y, 1, Today() . "  ".Now(), $this->formatLeft);
265                 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
266                 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
267                 $this->NewLine();
268                 $str = _("Fiscal Year") . ':';
269                 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
270                 $str = $this->fiscal_year;
271                 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
272                 $this->sheet->writeString($this->y, $tcol-1, $this->host, $this->formatLeft);
273                 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
274                 for ($i = 1; $i < count($this->params); $i++)
275                 {
276                         if ($this->params[$i]['from'] != '')
277                         {
278                                 $this->NewLine();
279                                 $str = $this->params[$i]['text'] . ':';
280                                 $this->sheet->writeString($this->y, 0, $str);
281                                 $str = $this->params[$i]['from'];
282                                 if ($this->params[$i]['to'] != '')
283                                         $str .= " - " . $this->params[$i]['to'];
284                                 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
285                                 if ($i == 1)
286                                 {
287                                         $this->sheet->writeString($this->y, $tcol-1, $this->user, $this->formatLeft);
288                                         $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
289                                 }       
290                         }
291                 }
292                 if ($this->params[0] != '') // Comments
293                 {
294                         $this->NewLine();
295                         $str = _("Comments") . ':';
296                         $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
297                         $this->sheet->writeString($this->y, 1, $this->params[0], $this->formatLeft);
298                 }
299                 $this->NewLine();
300                 if ($this->headers2 != null)
301                 {
302                         for ($i = 0, $j = 0; $i < $this->numcols; $i++)
303                         {
304                                 if ($this->cols2[$j] >= $this->cols[$i] && $this->cols2[$j] <= $this->cols[$i + 1])
305                                 {
306                                         if ($this->aligns2[$j] == "right")
307                                                 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatHeaderRight);
308                                         else    
309                                                 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatHeaderLeft);
310                                         $j++;   
311                                 }
312                                 else
313                                         $this->sheet->writeString($this->y, $i, "", $this->formatHeaderLeft);
314                         }               
315                         $this->NewLine();
316                 }
317
318                 for ($i = 0; $i < $this->numcols; $i++)
319                 {
320                         if (!isset($this->headers[$i]))
321                                 $header = "";
322                         else
323                                 $header = $this->headers[$i];
324                         if ($this->aligns[$i] == "right")
325                                 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderRight);
326                         else    
327                                 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderLeft);
328                 }
329                 $this->NewLine();
330         }
331
332         function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
333         {
334                 return;
335         }
336
337         // Alternate header style - primary differences are for PDFs
338         function Header3()
339         {
340                 // Flag to make sure we only print the company name once
341                 $companyNamePrinted = false;
342                 
343                 $this->y = 0;
344                 $tcol = $this->numcols - 1;
345                 $this->sheet->setRow($this->y, 20);
346                 // Title
347                 for ($i = 0; $i < $this->numcols; $i++)
348                         $this->sheet->writeBlank($this->y, $i, $this->formatTitle);
349                 $this->sheet->writeString($this->y, 0, $this->title, $this->formatTitle);
350                 $this->sheet->mergeCells($this->y, 0, $this->y, $tcol);
351
352                 // Dimension 1 - optional
353                 // - only print if available and not blank
354                 if (count($this->params) > 3)
355                         if ($this->params[3]['from'] != '')
356                         {
357                                 $this->NewLine();
358                                 $str = $this->params[3]['text'] . ':';
359                                 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
360                                 $this->sheet->writeString($this->y, 1, $this->params[3]['from'], $this->formatLeft);
361                                 // Company Name - at end of this row
362                                 if (!$companyNamePrinted)
363                                 {
364                                         $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
365                                         $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
366                                         $companyNamePrinted = true;
367                                 }
368                         }
369
370
371                 // Dimension 2 - optional
372                 // - only print if available and not blank
373                 if (count($this->params) > 4)
374                         if ($this->params[4]['from'] != '')
375                         {
376                                 $this->NewLine();
377                                 $str = $this->params[4]['text'] . ':';
378                                 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
379                                 $this->sheet->writeString($this->y, 1, $this->params[4]['from'], $this->formatLeft);
380                                 // Company Name - at end of this row
381                                 if (!$companyNamePrinted)
382                                 {
383                                         $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
384                                         $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
385                                         $companyNamePrinted = true;
386                                 }
387                         }
388                         
389                 // Tags - optional
390                 // TBD!!!               
391         
392                 // Report Date - time period covered
393                 // - can specify a range, or just the end date (and the report contents
394                 //   should make it obvious what the beginning date is)
395                 $this->NewLine();
396                 $str = _("Report Date") . ':';
397                 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
398                 $str = '';
399                 if ($this->params[1]['from'] != '')
400                         $str = $this->params[1]['from'] . ' - ';
401                 $str .= $this->params[1]['to'];
402                 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
403                 // Company Name - at end of this row
404                 if (!$companyNamePrinted)
405                 {
406                         $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
407                         $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
408                         $companyNamePrinted = true;
409                 }
410                         
411                 // Timestamp of when this copy of the report was generated 
412                 $this->NewLine();
413                 $str = _("Generated At") . ':';
414                 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
415                 $this->sheet->writeString($this->y, 1, Today() . "  ".Now(), $this->formatLeft);
416
417                 // Name of the user that generated this copy of the report
418                 $this->NewLine();
419                 $str = _("Generated By") . ':';
420                 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
421                 $str = $this->user;
422                 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
423
424                 // Comments - display any user-generated comments for this copy of the report
425                 if ($this->params[0] != '')
426                 {
427                         $this->NewLine();
428                         $str = _("Comments") . ':';
429                         $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
430                         $this->sheet->writeString($this->y, 1, $this->params[0], $this->formatLeft);
431                 }
432                 $this->NewLine();
433
434                 if ($this->headers2 != null)
435                 {
436                         for ($i = 0, $j = 0; $i < $this->numcols; $i++)
437                         {
438                                 if ($this->cols2[$j] >= $this->cols[$i] && $this->cols2[$j] <= $this->cols[$i + 1])
439                                 {
440                                         if ($this->aligns2[$j] == "right")
441                                                 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatTopHeaderRight);
442                                         else    
443                                                 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatTopHeaderLeft);
444                                         $j++;   
445                                 }
446                                 else
447                                         $this->sheet->writeString($this->y, $i, "", $this->formatTopHeaderLeft);
448                         }               
449                         $this->NewLine();
450                 }
451
452                 for ($i = 0; $i < $this->numcols; $i++)
453                 {
454                         if (!isset($this->headers[$i]))
455                                 $header = "";
456                         else
457                                 $header = $this->headers[$i];
458                         if ($this->aligns[$i] == "right")
459                                 if ($this->headers2 == null)
460                                         $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderRight);
461                                 else
462                                         $this->sheet->writeString($this->y, $i, $header, $this->formatBottomHeaderRight);
463                         else    
464                                 if ($this->headers2 == null)
465                                         $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderLeft);
466                                 else
467                                         $this->sheet->writeString($this->y, $i, $header, $this->formatBottomHeaderLeft);
468                 }
469                 $this->NewLine();
470         }
471
472         /**
473          * Format a numeric string date into something nicer looking.
474          *
475          * @param string $date Date string to be formatted.
476          * @param int $input_format Format of the input string.  Possible values are:<ul><li>0: user's default (default)</li></ul>
477          * @param int $output_format Format of the output string.  Possible values are:<ul><li>0: Month (word) Day (numeric), 4-digit Year - Example: January 1, 2000 (default)</li><li>1: Month 4-digit Year - Example: January 2000</li><li>2: Month Abbreviation 4-digit Year - Example: Jan 2000</li></ul>
478          * @access public
479          */
480         function DatePrettyPrint($date, $input_format = 0, $output_format = 0)
481         {
482                 if ($date != '')
483                 {
484                         $date = date2sql($date);
485                         $year = (int) (substr($date, 0, 4));
486                         $month = (int) (substr($date, 5, 2));
487                         $day = (int) (substr($date, 8, 2));
488                         if ($output_format == 0)
489                                 return(date('F j, Y', mktime(12, 0, 0, $month, $day, $year)));
490                         elseif ($output_format == 1)
491                                 return(date('F Y', mktime(12, 0, 0, $month, $day, $year)));
492                         elseif ($output_format == 2)
493                                 return(date('M Y', mktime(12, 0, 0, $month, $day, $year)));
494                 }
495                 else
496                         return $date;
497         }
498                 
499         function AddImage($logo, $x, $y, $w, $h)
500         {
501                 return;
502         }
503
504         function SetDrawColor($r, $g, $b)
505         {
506                 return;
507         }
508
509         function SetTextColor($r, $g, $b)
510         {
511                 return;
512         }
513
514         function SetFillColor($r, $g, $b)
515         {
516                 return;
517         }
518         
519         function GetCellPadding()
520         {
521                 return 0;
522         }
523         
524         function SetCellPadding($pad)
525         {
526                 return;
527         }
528         
529         function Text($c, $txt, $n=0, $corr=0, $r=0, $align='left', $border=0, $fill=0, $link=NULL, $stretch=0)
530         {
531                 return;
532         }
533
534         function TextWrap($xpos, $ypos, $len, $str, $align = 'left', $border = 0, $fill = 0, $link = NULL, $stretch = 0)
535         {
536                 return;
537         }
538
539         function TextCol($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
540         {
541                 $txt = html_entity_decode($txt);        
542                 if ($this->aligns[$c] == 'right')
543                         $this->sheet->writeString($this->y, $c, $txt, $this->formatRight);
544                 else    
545                         $this->sheet->writeString($this->y, $c, $txt, $this->formatLeft);
546                 if ($n - $c > 1)
547                         $this->sheet->mergeCells($this->y, $c, $this->y, $n - 1);
548         }
549
550         function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0, $color_red=false) 
551         { 
552                 if (!is_numeric($txt))
553                         $txt = 0;
554                 $this->sheet->writeNumber($this->y, $c, $txt, $this->NumFormat($dec)); 
555         }
556         
557         function AmountCol2($c, $n, $txt, $dec=0, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0, $color_red=false, $amount_locale = NULL, $amount_format = NULL) 
558         { 
559                 if (!is_numeric($txt))
560                         $txt = 0;
561                 $this->sheet->writeNumber($this->y, $c, $txt, $this->NumFormat($dec)); 
562         }
563         
564         function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0) 
565         {
566                 if (!$conv)
567                         $txt = date2sql($txt);
568                 list($year, $mo, $day) = explode("-", $txt);
569                 $date = $this->ymd2date((int)$year, (int)$mo, (int)$day);
570                 $this->sheet->writeNumber($this->y, $c, $date, $this->formatDate);
571         }
572
573         function TextCol2($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
574         {
575                 $txt = html_entity_decode($txt);        
576                 $this->sheet->writeString($this->y, $c, $txt, $this->formatLeft);
577                 if ($n - $c > 1)
578                         $this->sheet->mergeCells($this->y, $c, $this->y, $n - 1);
579         }
580
581         function TextColLines($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
582         {
583                 return;
584         }
585
586         function TextWrapLines($c, $width, $txt, $align='left', $border=0, $fill=0, $link=NULL, $stretch=0)
587         {
588                 return;
589         }
590
591         /**
592          * Crude text wrap calculator based on PDF version.
593          */
594         function TextWrapCalc($txt, $width, $spacebreak=false)
595         {
596                 // Assume an average character width
597                 $avg_char_width = 5;
598                 $ret = "";
599                 $txt2 = $txt;
600                 $w = strlen($txt) * $avg_char_width;
601                 if ($w > $width && $w > 0 && $width != 0)
602                 {
603                         $n = strlen($txt);
604                         $k = intval($n * $width / $w);
605                         if ($k > 0 && $k < $n)
606                         {
607                                 $txt2 = substr($txt, 0, $k);
608                                 if ($spacebreak && (($pos = strrpos($txt2, " ")) !== false))
609                                 {
610                                         $txt2 = substr($txt2, 0, $pos);
611                                         $ret = substr($txt, $pos+1);
612                                 }
613                                 else
614                                         $ret = substr($txt, $k);
615                         }
616                 }
617                 return array($txt2, $ret);
618         }
619
620         function SetLineStyle($style)
621         {
622                 return;
623         }
624
625         function SetLineWidth($width)
626         {
627                 return;
628         }
629         
630         function LineTo($from, $row, $to, $row2)
631         {
632                 return;
633         }
634
635         function Line($row, $height = 0)
636         {
637                 return;
638         }
639
640         function UnderlineCell($c, $r = 0, $type = 1, $linewidth = 0, $style = array())
641         {
642                 return;
643         }
644         
645         function NewLine($l=1, $np=0, $h=NULL)
646         {
647                 $this->y += $l;
648         }
649         
650         function NewPage()
651         {
652                 if (method_exists($this, $this->headerFunc))    // draw predefined page layout if any
653                         $this->{$this->headerFunc}();
654         }
655         
656         function ymd2Date($year, $mon, $day) // XLS internal date representation is a number between 1900-01-01 and 2078-12-31
657         {                                                                               // if we need the time part too, we have to add this value after a decimalpoint.
658         $mo = array(0,31,28,31,30,31,30,31,31,30,31,30,31);
659         $BASE = 1900;
660                 $MAXYEAR = 2075;
661                 if (($year % 4) == 0)
662                 $mo[2]++;
663         if ($mon < 1)
664             $mon = 1;
665         elseif ($mon > 12)
666             $mon = 12;
667         if ($day < 1)
668             $day = 1;
669         elseif ($day > $mo[$mon])
670             $day = $mo[$mon];
671         if ($year < $BASE)
672             $year = $BASE;
673         elseif ($year > $MAXYEAR)
674             $year = $MAXYEAR;
675         $jul = (int)$day;
676         for ($n = 1; $n < $mon; $n++)
677         {
678             $jul += $mo[$n];
679         }
680         for ($n = $BASE; $n < $year; $n++)
681         {
682             $jul += 365;
683             if (($n % 4) == 0)
684                 $jul++;
685         }
686         return $jul;
687         }
688   
689         function px2units($px) // XLS app conversion. Not bulletproof.
690         {
691                 $excel_column_width_factor = 256;
692                 $unit_offset_length = $this->excelColWidthFactor;
693                 return ($px / $unit_offset_length);
694         }       
695
696         function End($email=0, $subject='')
697         {
698                 global $path_to_root;
699                 ++$this->y;
700                 for ($i = 0; $i < $this->numcols; $i++)
701                         $this->sheet->writeBlank($this->y, $i, $this->formatFooter);
702                 $this->sheet->mergeCells($this->y, 0, $this->y, $this->numcols - 1);
703                 $this->close();
704                 // first have a look through the directory, 
705                 // and remove old temporary pdfs
706                 if ($d = @opendir($this->path)) {
707                         while (($file = readdir($d)) !== false) {
708                                 if (!is_file($this->path.'/'.$file) || $file == 'index.php') continue;
709                                 // then check to see if this one is too old
710                                 $ftime = filemtime($this->path.'/'.$file);
711                                 // seems 3 min is enough for any report download, isn't it?
712                                 if (time()-$ftime > 180){
713                                         unlink($this->path.'/'.$file);
714                                 }
715                         }
716                         closedir($d);
717                 }
718
719                 meta_forward($path_to_root.'/reporting/prn_redirect.php', "xls=1&filename=$this->filename&unique=$this->unique_name");
720                 exit();
721         }
722 }
723
724 ?>