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