Bad thousand/decimal formatting in XLS reports.(OpenOffice)
[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 /* $Revision$ */
13 $page_security = 8;
14 include_once($path_to_root . "/reporting/includes/Workbook.php");
15 include_once($path_to_root . "/admin/db/company_db.inc");
16 include_once($path_to_root . "/config.php");
17 // xls version
18 class FrontReport extends Spreadsheet_Excel_Writer_Workbook
19 {
20         var $size;
21         var $company;
22         var $user;
23         var $host;
24         var $fiscal_year;
25         var $title;
26         var $filename;
27         var $unique_name;
28         var $path;
29         var $code;
30         var $bottomMargin = 0;
31         var $lineHeight;
32         var $leftMargin = 0;
33
34         var $cols;
35         var $params;
36         var $headers;
37         var $aligns;
38         var $headers2;
39         var $aligns2;
40         var $cols2;
41         var $fontSize;
42         var $oldFontSize;
43         var $currency;
44         var $row = 9999999;
45         var $y;
46         var $numcols;
47         
48         var $formatTitle;
49         var $formatDateTime;
50         var $formatDate;
51         var $formatHeaderLeft;
52         var $formatHeaderRight;
53         var $formatFooter;
54         var $formatAmount = array();
55         
56         var $sheet;
57
58         function FrontReport($title, $filename, $size = 'A4', $fontsize = 9)
59         {
60                 global $comp_path, $dateseps;
61                 
62                 $this->size = $size;
63                 $this->title = $title;
64                 $this->lineHeight = 12;
65                 $this->fontSize = $fontsize;
66                 $this->oldFontSize = 0;
67                 $this->y = 1;
68                 $this->currency = '';
69                 $rtl = ($_SESSION['language']->dir == 'rtl');
70                 $this->code = strtolower($_SESSION['language']->encoding);
71                 $this->filename = $filename.".xls";
72                 $this->unique_name = uniqid('').".xls";
73                 $this->path = $comp_path.'/'.user_company(). '/pdf_files';
74                 $this->Spreadsheet_Excel_Writer_Workbook($this->path."/".$this->unique_name);
75                 //$this->setCountry(48);
76                 if ($this->code != "iso-8859-1")
77                         $this->setVersion(8); // set biff version to 8 (0x0006 internal)
78                 $this->sheet =& $this->addWorksheet($this->title);
79                 if ($this->code != "iso-8859-1")
80                         $this->sheet->setInputEncoding($this->code); // set sheet encoding
81                 if ($rtl)
82                         $this->sheet->setRTL();
83                 $this->formatTitle =& $this->addFormat();       
84                 $this->formatTitle->setSize(16);
85                 $this->formatTitle->setBold();
86                 $this->formatTitle->setAlign($rtl ? 'right' : 'left');
87                 $this->formatTitle->setTop(2);
88                 $this->formatTitle->setTopColor('gray');
89
90                 $how = user_date_format();
91                 $sep = $dateseps[user_date_sep()];
92                 if ($sep == '.')
93                         $sep = "\\.";
94                 if ($how == 0)
95                 {
96                         $dateformat_long = "mm{$sep}dd{$sep}yyyy\ \ hh:mm\ am/pm";
97                         $dateformat = "mm{$sep}dd{$sep}yyyy";
98                 }       
99                 elseif ($how == 1)      
100                 {
101                         $dateformat_long = "dd{$sep}mm{$sep}yyyy\ \ hh:mm";
102                         $dateformat = "dd{$sep}mm{$sep}yyyy";
103                 }       
104                 else    
105                 {
106                         $dateformat_long = "yyyy{$sep}mm{$sep}dd\ \ hh:mm";
107                         $dateformat = "yyyy{$sep}mm{$sep}dd";
108                 }       
109                 $this->formatDateTime =& $this->addFormat();
110                 $this->formatDateTime->setNumFormat($dateformat_long);
111                 $this->formatDateTime->setAlign($rtl ? 'right' : 'left');
112                 $this->formatDate =& $this->addFormat();
113                 $this->formatDate->setNumFormat($dateformat);
114                 $this->formatDate->setAlign($rtl ? 'right' : 'left');
115                 $this->formatRight =& $this->addFormat();
116                 $this->formatRight->setAlign($rtl ? 'left' : 'right');
117                 $this->formatLeft =& $this->addFormat();
118                 $this->formatLeft->setAlign($rtl ? 'right' : 'left');
119                 
120                 $this->formatHeaderLeft =& $this->addFormat();
121                 $this->formatHeaderLeft->setItalic();
122                 $this->formatHeaderLeft->setTop(2);
123                 $this->formatHeaderLeft->setTopColor('gray');
124                 $this->formatHeaderLeft->setBottom(2);
125                 $this->formatHeaderLeft->setBottomColor('gray');
126                 $this->formatHeaderLeft->setAlign('vcenter');
127                 $this->formatDate->setAlign($rtl ? 'right' : 'left');
128                 $this->formatHeaderRight =& $this->addFormat();
129                 $this->formatHeaderRight->setItalic();
130                 $this->formatHeaderRight->setTop(2);
131                 $this->formatHeaderRight->setTopColor('gray');
132                 $this->formatHeaderRight->setBottom(2);
133                 $this->formatHeaderRight->setBottomColor('gray');
134                 $this->formatHeaderRight->setAlign('vcenter');
135                 $this->formatHeaderRight->setAlign('right');
136                 $this->formatFooter =& $this->addFormat();
137                 $this->formatFooter->setTop(2);
138                 $this->formatFooter->setTopColor('gray');
139         }
140         
141         function NumFormat($dec) 
142         {
143                 if (!isset($this->formatAmount[$dec]))
144                 {
145                         //global $thoseps,$decseps;
146                         $dec = (int)$dec;
147                         //$tsep = $thoseps[user_tho_sep()];
148                         //$dsep = $decseps[user_dec_sep()];
149                         $tsep = ',';
150                         $dsep = '.';
151                         $format = "###{$tsep}###{$tsep}###{$tsep}##0";
152                         if ($dec>0)
153                                 $format .= "{$dsep}".str_repeat('0',$dec);
154                         $this->formatAmount[$dec] =& $this->addFormat();
155                         $this->formatAmount[$dec]->setNumFormat($format);
156                         $this->formatAmount[$dec]->setAlign('right');
157                 }
158                 return $this->formatAmount[$dec];
159         }
160
161         function Font($style = 'normal')
162         {
163         }
164
165         function Info($params, $cols, $headers, $aligns,
166                 $cols2 = null, $headers2 = null, $aligns2 = null)
167         {
168                 global $app_title, $version, $power_by, $power_url;
169                 $this->company = get_company_prefs();
170                 $year = get_current_fiscalyear();
171                 if ($year['closed'] == 0)
172                         $how = _("Active");
173                 else
174                         $how = _("Closed");
175                 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . "  " . "(" . $how . ")";
176                 $this->user = $_SESSION["wa_current_user"]->name;
177                 $this->host = $_SERVER['SERVER_NAME'];
178                 $this->params = $params;
179                 $this->cols = $cols;
180                 $this->headers = $headers;
181                 $this->aligns = $aligns;
182                 $this->cols2 = $cols2;
183                 $this->headers2 = $headers2;
184                 $this->aligns2 = $aligns2;
185                 $this->numcols = count($this->headers);
186                 $tcols = count($this->headers2);
187                 if ($tcols > $this->numcols)
188                         $this->numcols = $tcols;
189                 for ($i = 0; $i < $this->numcols; $i++)
190                         $this->sheet->setColumn($i, $i, $this->px2units($this->cols[$i + 1] - $this->cols[$i]));
191         }
192
193         function Header()
194         {
195                 $this->y = 0;
196                 $tcol = $this->numcols - 1;
197                 $this->sheet->setRow($this->y, 20);
198                 for ($i = 0; $i < $this->numcols; $i++)
199                         $this->sheet->writeBlank($this->y, $i, $this->formatTitle);
200                 $this->sheet->writeString($this->y, 0, $this->title, $this->formatTitle);
201                 $this->sheet->mergeCells($this->y, 0, $this->y, $tcol);
202                 $this->NewLine();
203                 $str = _("Print Out Date") . ':';
204                 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
205                 $this->sheet->writeString($this->y, 1, Today() . "  ".Now(), $this->formatLeft);
206                 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
207                 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
208                 $this->NewLine();
209                 $str = _("Fiscal Year") . ':';
210                 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
211                 $str = $this->fiscal_year;
212                 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
213                 $this->sheet->writeString($this->y, $tcol-1, $this->host, $this->formatLeft);
214                 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
215                 for ($i = 1; $i < count($this->params); $i++)
216                 {
217                         if ($this->params[$i]['from'] != '')
218                         {
219                                 $this->NewLine();
220                                 $str = $this->params[$i]['text'] . ':';
221                                 $this->sheet->writeString($this->y, 0, $str);
222                                 $str = $this->params[$i]['from'];
223                                 if ($this->params[$i]['to'] != '')
224                                         $str .= " - " . $this->params[$i]['to'];
225                                 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
226                                 if ($i == 1)
227                                 {
228                                         $this->sheet->writeString($this->y, $tcol-1, $this->user, $this->formatLeft);
229                                         $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
230                                 }       
231                         }
232                 }
233                 if ($this->params[0] != '') // Comments
234                 {
235                         $this->NewLine();
236                         $str = _("Comments") . ':';
237                         $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
238                         $this->sheet->writeString($this->y, 1, $this->params[0], $this->formatLeft);
239                 }
240                 $this->NewLine();
241                 if ($this->headers2 != null)
242                 {
243                         for ($i = 0, $j = 0; $i < $this->numcols; $i++)
244                         {
245                                 if ($this->cols2[$j] >= $this->cols[$i] && $this->cols2[$j] <= $this->cols[$i + 1])
246                                 {
247                                         if ($this->aligns2[$j] == "right")
248                                                 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatHeaderRight);
249                                         else    
250                                                 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatHeaderLeft);
251                                         $j++;   
252                                 }
253                                 else
254                                         $this->sheet->writeString($this->y, $i, "", $this->formatHeaderLeft);
255                         }               
256                         $this->NewLine();
257                 }
258
259                 for ($i = 0; $i < $this->numcols; $i++)
260                 {
261                         if (!isset($this->headers[$i]))
262                                 $header = "";
263                         else
264                                 $header = $this->headers[$i];
265                         if ($this->aligns[$i] == "right")
266                                 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderRight);
267                         else    
268                                 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderLeft);
269                 }
270                 $this->NewLine();
271         }
272
273         function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
274         {
275                 return;
276         }
277
278         function AddImage($logo, $x, $y, $w, $h)
279         {
280                 return;
281         }
282
283         function SetDrawColor($r, $g, $b)
284         {
285                 return;
286         }
287
288         function SetTextColor($r, $g, $b)
289         {
290                 return;
291         }
292
293         function Text($c, $txt, $n=0, $corr=0, $r=0)
294         {
295                 return;
296         }
297
298         function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
299         {
300                 return;
301         }
302
303         function TextCol($c, $n, $txt, $corr=0, $r=0)
304         {
305                 if ($this->aligns[$c] == 'right')
306                         $this->sheet->writeString($this->y, $c, $txt, $this->formatRight);
307                 else    
308                         $this->sheet->writeString($this->y, $c, $txt, $this->formatLeft);
309                 if ($n - $c > 1)
310                         $this->sheet->mergeCells($this->y, $c, $this->y, $n - 1);
311         }
312
313         function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0) 
314         { 
315                 if (!is_numeric($txt))
316                         $txt = 0;
317                 $this->sheet->writeNumber($this->y, $c, $txt, $this->NumFormat($dec)); 
318         }
319         
320         function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0) 
321         {
322                 if (!$conv)
323                         $txt = date2sql($txt);
324                 list($year, $mo, $day) = explode("-", $txt);    
325                 $date = $this->ymd2date((int)$year, (int)$mo, (int)$day);
326                 $this->sheet->writeNumber($this->y, $c, $date, $this->formatDate);
327         }
328
329         function TextCol2($c, $n, $txt, $corr=0, $r=0)
330         {
331                 $this->sheet->writeString($this->y, $c, $txt, $this->formatLeft);
332                 if ($n - $c > 1)
333                         $this->sheet->mergeCells($this->y, $c, $this->y, $n - 1);
334         }
335
336         function TextColLines($c, $n, $txt, $corr=0, $r=0)
337         {
338                 return;
339         }
340
341         function TextWrapLines($c, $width, $txt, $align='left')
342         {
343                 return;
344         }
345
346         function LineTo($from, $row, $to, $row2)
347         {
348                 return;
349         }
350
351         function Line($row, $height = 0)
352         {
353                 return;
354         }
355
356         function NewLine($l=1, $np=0)
357         {
358                 $this->y += $l;
359         }
360
361         function ymd2Date($year, $mon, $day) // XLS internal date representation is a number between 1900-01-01 and 2078-12-31
362         {                                                                               // if we need the time part too, we have to add this value after a decimalpoint.
363         $mo = array(0,31,28,31,30,31,30,31,31,30,31,30,31);
364         $BASE = 1900;
365                 $MAXYEAR = 2075;
366                 if (($year % 4) == 0)
367                 $mo[2]++;
368         if ($mon < 1)
369             $mon = 1;
370         elseif ($mon > 12)
371             $mon = 12;
372         if ($day < 1)
373             $day = 1;
374         elseif ($day > $mo[$mon])
375             $day = $mo[$mon];
376         if ($year < $BASE)
377             $year = $BASE;
378         elseif ($year > $MAXYEAR)
379             $year = $MAXYEAR;
380         $jul = (int)$day;
381         for ($n = 1; $n < $mon; $n++)
382         {
383             $jul += $mo[$n];
384         }
385         for ($n = $BASE; $n < $year; $n++)
386         {
387             $jul += 365;
388             if (($n % 4) == 0)
389                 $jul++;
390         }
391         return $jul;
392         }
393   
394         function px2units($px) // XLS app conversion. Not bulletproof.
395         {
396                 $excel_column_width_factor = 256;
397                 $unit_offset_length = 6.5;
398                 return ($px / $unit_offset_length);
399         }       
400
401         function End($email=0, $subject=null, $myrow=null, $doctype = 0)
402         {
403                 for ($i = 0; $i < $this->numcols; $i++)
404                         $this->sheet->writeBlank($this->y, $i, $this->formatFooter);
405                 $this->sheet->mergeCells($this->y, 0, $this->y, $this->numcols - 1);
406                 $this->close();
407                 // first have a look through the directory, 
408                 // and remove old temporary pdfs
409                 if ($d = @opendir($this->path)) {
410                         while (($file = readdir($d)) !== false) {
411                                 if (!is_file($this->path.'/'.$file) || $file == 'index.php') continue;
412                                 // then check to see if this one is too old
413                                 $ftime = filemtime($this->path.'/'.$file);
414                                 // seems 3 min is enough for any report download, isn't it?
415                                 if (time()-$ftime > 180){
416                                         unlink($this->path.'/'.$file);
417                                 }
418                         }
419                         closedir($d);
420                 }
421                 meta_forward($_SERVER['PHP_SELF'], "xls=1&filename=$this->filename&unique=$this->unique_name");
422                 exit();
423         }
424 }
425
426 ?>