3d543e86dc91c786ab1af36bac80b68af898d6ae
[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/ExcelWriterXML.php");
15 include_once($path_to_root . "/admin/db/company_db.inc");
16 include_once($path_to_root . "/config.php");
17
18 class FrontReport extends ExcelWriterXML
19 {
20         var $xml;
21         var $size;
22         var $company;
23         var $user;
24         var $host;
25         var $fiscal_year;
26         var $title;
27         var $filename;
28         var $path;
29         var $rtl;
30         var $code;
31         var $bottomMargin = 0;
32         var $lineHeight;
33         var $leftMargin = 0;
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         
49         var $sheet;
50
51         function FrontReport($title, $filename, $size = 'A4', $fontsize = 9)
52         {
53                 global $comp_path, $dateseps, $thoseps, $decseps;
54                 
55                 $this->size = $size;
56                 $this->title = $title;
57                 $this->lineHeight = 12;
58                 $this->fontSize = $fontsize;
59                 $this->oldFontSize = 0;
60                 $this->y = 1;
61                 $this->currency = '';
62                 $this->rtl = ($_SESSION['language']->dir === 'rtl' ? 'rtl' : 'ltr');
63                 $this->code = $_SESSION['language']->encoding;
64                 $this->filename = $filename;
65                 $this->path = $comp_path.'/'.user_company(). '/pdf_files/';
66                 $this->ExcelWriterXML($this->filename);
67                 $this->setCharSet($this->code);
68                 $this->overwriteFile();
69                 $this->showErrorSheet(true);
70                 
71                 $this->sheet =& $this->addSheet($this->title);
72                 if ($this->rtl === 'rtl')
73                         $this->sheet->displayRightToLeft();
74                 $formatTitle =& $this->addStyle('formatTitle'); 
75                 $formatTitle->fontSize('16');
76                 $formatTitle->fontBold();
77                 $formatTitle->border('Top', '2', 'darkgray');
78         
79                 $how = user_date_format();
80                 $sep = $dateseps[user_date_sep()];
81                 if ($sep == '.')
82                         $sep = "\\.";
83                 if ($how == 0)
84                 {
85                         $dateformat_long = "mm{$sep}dd{$sep}yyyy\ \ hh:mm\ am/pm";
86                         $dateformat = "mm{$sep}dd{$sep}yyyy";
87                 }       
88                 elseif ($how == 1)      
89                 {
90                         $dateformat_long = "dd{$sep}mm{$sep}yyyy\ \ hh:mm";
91                         $dateformat = "dd{$sep}mm{$sep}yyyy";
92                 }       
93                 else    
94                 {
95                         $dateformat_long = "yyyy{$sep}mm{$sep}dd\ \ hh:mm";
96                         $dateformat = "yyyy{$sep}mm{$sep}dd";
97                 }       
98                 $formatDateTime =& $this->addStyle('formatDateTime');
99                 $formatDateTime->numberFormatDateTime($dateformat_long);
100                 $formatDateTime->alignHorizontal('Left');
101                 $formatDate =& $this->addStyle('formatDate');
102                 $formatDate->numberFormatDateTime($dateformat);
103                 $formatDate->alignHorizontal('Left');
104                 $formatRight =& $this->addStyle('formatRight');
105                 $formatRight->alignHorizontal('Right');
106                 
107                 $formatHeaderLeft =& $this->addStyle("formatHeaderLeft");
108                 $formatHeaderLeft->fontItalic();
109                 $formatHeaderLeft->border('Top', '2', 'darkgray');
110                 $formatHeaderLeft->border('Bottom', '2', 'darkgray');
111                 $formatHeaderLeft->alignVertical('Center');
112                 $formatHeaderRight =& $this->addStyle("formatHeaderRight");
113                 $formatHeaderRight->fontItalic();
114                 $formatHeaderRight->alignHorizontal('Right');
115                 $formatHeaderRight->border('Top', '2', 'darkgray');
116                 $formatHeaderRight->border('Bottom', '2', 'darkgray');
117                 $formatHeaderRight->alignVertical('Center');
118                 $formatFooter =& $this->addStyle("formatFooter");
119                 $formatFooter->border('Top', '2', 'darkgray');
120         }
121         
122         function NumFormat($dec) 
123         {
124                 global $thoseps,$decseps;
125                 
126                 $dec = (int)$dec;
127                 $stylename = 'formatAmount'.$dec;
128                 if (!isset($this->styles[$stylename])) 
129                 {
130                         $tsep = $thoseps[user_tho_sep()];
131                         $dsep = $decseps[user_dec_sep()];
132                         $format = "###{$tsep}###{$tsep}###{$tsep}##0";
133                         if ($dec>0)
134                                 $format .= "{$dsep}".str_repeat('0',$dec);
135                         $style =& $this->addStyle($stylename);
136                         $style->numberFormat($format);
137                         $style->alignHorizontal('Right');
138                 }
139                 return $stylename;
140         }
141
142         function Font($style = 'normal')
143         {
144         }
145
146         function Info($params, $cols, $headers, $aligns,
147                 $cols2 = null, $headers2 = null, $aligns2 = null)
148         {
149                 global $app_title, $version, $power_by, $power_url;
150                 $this->company = get_company_prefs();
151                 $this->docTitle($this->title);
152                 $this->docAuthor($app_title . ' ' . $version);
153                 $this->docCompany($this->company['coy_name']);
154                 $this->docManager($power_by . ' - ' . $power_url);
155                 $year = get_current_fiscalyear();
156                 if ($year['closed'] == 0)
157                         $how = _("Active");
158                 else
159                         $how = _("Closed");
160                 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . "  " . "(" . $how . ")";
161                 $this->user = $_SESSION["wa_current_user"]->name;
162                 $this->host = $_SERVER['SERVER_NAME'];
163                 $this->params = $params;
164                 $this->cols = $cols;
165                 $this->headers = $headers;
166                 $this->aligns = $aligns;
167                 $this->cols2 = $cols2;
168                 $this->headers2 = $headers2;
169                 $this->aligns2 = $aligns2;
170                 $this->numcols = count($this->headers);
171                 $tcols = count($this->headers2);
172                 if ($tcols > $this->numcols)
173                         $this->numcols = $tcols;
174                 for ($i = 0; $i < $this->numcols; $i++)
175                         $this->sheet->columnWidth($i+1, $this->cols[$i + 1] - $this->cols[$i]);
176         }
177
178         function Header()
179         {
180                 $this->y = 0;
181                 $tcol = $this->numcols - 1;
182                 $this->NewLine();
183                 $this->sheet->rowHeight($this->y, 20);
184                 $this->sheet->writeString($this->y, 1, $this->title, 'formatTitle');
185                 $this->sheet->cellMerge($this->y, 1, $tcol, 0);
186
187                 $this->NewLine();
188
189                 $str = _("Print Out Date") . ':';
190                 $this->sheet->writeString($this->y, 1, $str);
191                 $date = $this->sheet->convertMysqlDateTime(date('Y-m-d H:i:s'));
192                 $this->sheet->writeDateTime($this->y, 2, $date, 'formatDateTime');
193                 $this->sheet->writeString($this->y, $tcol, $this->company['coy_name']);
194                 $this->sheet->cellMerge($this->y, $tcol, 1, 0);
195                 $this->NewLine();
196                 $str = _("Fiscal Year") . ':';
197                 $this->sheet->writeString($this->y, 1, $str);
198                 $str = $this->fiscal_year;
199                 $this->sheet->writeString($this->y, 2, $str);
200                 $this->sheet->writeString($this->y, $tcol, $this->host);
201                 $this->sheet->cellMerge($this->y, $tcol, 1, 0);
202                 for ($i = 1; $i < count($this->params); $i++)
203                 {
204                         if ($this->params[$i]['from'] != '')
205                         {
206                                 $this->NewLine();
207                                 $str = $this->params[$i]['text'] . ':';
208                                 $this->sheet->writeString($this->y, 1, $str);
209                                 $str = $this->params[$i]['from'];
210                                 if ($this->params[$i]['to'] != '')
211                                         $str .= " - " . $this->params[$i]['to'];
212                                 $this->sheet->writeString($this->y, 2, $str);
213                                 if ($i == 1)
214                                 {
215                                         $this->sheet->writeString($this->y, $tcol, $this->user);
216                                         $this->sheet->cellMerge($this->y, $tcol, 1, 0);
217                                 }       
218                         }
219                 }
220                 if ($this->params[0] != '') // Comments
221                 {
222                         $this->NewLine();
223                         $str = _("Comments") . ':';
224                         $this->sheet->writeString($this->y, 1, $str);
225                         $this->sheet->writeString($this->y, 2, $this->params[0]);
226                 }
227                 $this->NewLine();
228                 if ($this->headers2 != null)
229                 {
230                         for ($i = 0, $j = 0; $i < $this->numcols; $i++)
231                         {
232                                 if ($this->cols2[$j] >= $this->cols[$i] && $this->cols2[$j] <= $this->cols[$i + 1])
233                                 {
234                                         if ($this->aligns2[$j] == "right")
235                                                 $this->sheet->writeString($this->y, $i + 1, $this->headers2[$j], 'formatHeaderRight');
236                                         else    
237                                                 $this->sheet->writeString($this->y, $i + 1, $this->headers2[$j], 'formatHeaderLeft');
238                                         $j++;   
239                                 }
240                                 else
241                                         $this->sheet->writeString($this->y, $i + 1, "", 'formatHeaderLeft');
242                         }               
243                         $this->NewLine();
244                 }
245
246                 for ($i = 0; $i < $this->numcols; $i++)
247                 {
248                         if (!isset($this->headers[$i]))
249                                 $header = "";
250                         else
251                                 $header = $this->headers[$i];
252                         if ($this->aligns[$i] == "right")
253                                 $this->sheet->writeString($this->y, $i + 1, $header, 'formatHeaderRight');
254                         else    
255                                 $this->sheet->writeString($this->y, $i + 1, $header, 'formatHeaderLeft');
256                 }               
257                 $this->NewLine();
258         }
259
260         function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
261         {
262                 return;
263         }
264
265         function AddImage($logo, $x, $y, $w, $h)
266         {
267                 return;
268         }
269
270         function SetDrawColor($r, $g, $b)
271         {
272                 return;
273         }
274
275         function SetTextColor($r, $g, $b)
276         {
277                 return;
278         }
279
280         function Text($c, $txt, $n=0, $corr=0, $r=0)
281         {
282                 return;
283         }
284
285         function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
286         {
287                 return;
288         }
289
290         function TextCol($c, $n, $txt, $corr=0, $r=0)
291         {
292                 if ($this->aligns[$c] == 'right')
293                         $this->sheet->writeString($this->y, $c + 1, $txt, 'formatRight');
294                 else    
295                         $this->sheet->writeString($this->y, $c + 1, $txt);
296                 if ($n - $c > 1)
297                         $this->sheet->cellMerge($this->y, $c + 1, $n - $c - 1, 0);
298         }
299
300         function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0) 
301         { 
302                 if (!is_numeric($txt))
303                         $txt = 0;
304                 $this->sheet->writeNumber($this->y, $c + 1, $txt, $this->NumFormat($dec)); 
305         }
306         
307         function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0) 
308         {
309                 if (!$conv)
310                         $txt = date2sql($txt);
311                 $date = $this->sheet->convertMysqlDate($txt);
312                 $this->sheet->writeDateTime($this->y, $c + 1, $date, 'formatDate');
313         }
314
315         function TextCol2($c, $n, $txt, $corr=0, $r=0)
316         {
317                 $this->sheet->writeString($this->y, $c + 1, $txt);
318                 if ($n - $c > 1)
319                         $this->sheet->cellMerge($this->y, $c + 1, $n - $c - 1, 0);
320         }
321
322         function TextColLines($c, $n, $txt, $corr=0, $r=0)
323         {
324                 return;
325         }
326
327         function TextWrapLines($c, $width, $txt, $align='left')
328         {
329                 return;
330         }
331
332         function LineTo($from, $row, $to, $row2)
333         {
334                 return;
335         }
336
337         function Line($row, $height = 0)
338         {
339                 return;
340         }
341
342         function NewLine($l=1, $np=0)
343         {
344                 $this->y += $l;
345         }
346
347         function End($email=0, $subject=null, $myrow=null, $doctype = 0)
348         {
349                 global $comp_path;
350                 $this->sheet->cellMerge($this->y, 1, $this->numcols - 1, 0);
351                 $this->sheet->writeString($this->y, 1, "", 'formatFooter');
352                 
353                 $dir =  $comp_path.'/'.user_company(). '/pdf_files';
354                 //save the file
355                 if (!file_exists($dir))
356                 {
357                         mkdir ($dir,0777);
358                 }
359                 if(in_ajax()) 
360                 {
361                         global $Ajax;
362                         // do not use standard filenames or your sensitive company data 
363                         // are world readable
364                         //$fname = $dir.'/'.uniqid('').'.xml';
365                         $fname = $dir.'/'."test".'.xml';
366                         $this->writeData($fname);
367                         if (user_rep_popup()) 
368                                 $Ajax->popup($fname); // when embeded pdf viewer used
369                         else
370                                 $Ajax->redirect($fname); // otherwise use faster method
371                 } 
372                 else 
373                 {
374                         $this->sendHeaders();
375                         $this->writeData();
376                 }       
377                 // first have a look through the directory, 
378                 // and remove old temporary pdfs
379                 /*
380                 if ($d = @opendir($dir)) {
381                         while (($file = readdir($d)) !== false) {
382                                 if (!is_file($dir.'/'.$file) || $file == 'index.php') continue;
383                         // then check to see if this one is too old
384                                 $ftime = filemtime($dir.'/'.$file);
385                          // seems 3 min is enough for any report download, isn't it?
386                                 if (time()-$ftime > 180){
387                                         unlink($dir.'/'.$file);
388                                 }
389                         }
390                         closedir($d);
391                 }
392                 */
393         }
394 }
395
396 ?>