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