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 ***********************************************************************/
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");
18 class FrontReport extends ExcelWriterXML
31 var $bottomMargin = 0;
51 function FrontReport($title, $filename, $size = 'A4', $fontsize = 9)
53 global $comp_path, $dateseps, $thoseps, $decseps;
56 $this->title = $title;
57 $this->lineHeight = 12;
58 $this->fontSize = $fontsize;
59 $this->oldFontSize = 0;
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);
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');
79 $how = user_date_format();
80 $sep = $dateseps[user_date_sep()];
85 $dateformat_long = "mm{$sep}dd{$sep}yyyy\ \ hh:mm\ am/pm";
86 $dateformat = "mm{$sep}dd{$sep}yyyy";
90 $dateformat_long = "dd{$sep}mm{$sep}yyyy\ \ hh:mm";
91 $dateformat = "dd{$sep}mm{$sep}yyyy";
95 $dateformat_long = "yyyy{$sep}mm{$sep}dd\ \ hh:mm";
96 $dateformat = "yyyy{$sep}mm{$sep}dd";
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');
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');
122 function NumFormat($dec)
124 global $thoseps,$decseps;
127 $stylename = 'formatAmount'.$dec;
128 if (!isset($this->styles[$stylename]))
130 $tsep = $thoseps[user_tho_sep()];
131 $dsep = $decseps[user_dec_sep()];
132 $format = "###{$tsep}###{$tsep}###{$tsep}##0";
134 $format .= "{$dsep}".str_repeat('0',$dec);
135 $style =& $this->addStyle($stylename);
136 $style->numberFormat($format);
137 $style->alignHorizontal('Right');
142 function Font($style = 'normal')
146 function Info($params, $cols, $headers, $aligns,
147 $cols2 = null, $headers2 = null, $aligns2 = null)
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)
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;
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]);
181 $tcol = $this->numcols - 1;
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);
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);
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++)
204 if ($this->params[$i]['from'] != '')
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);
215 $this->sheet->writeString($this->y, $tcol, $this->user);
216 $this->sheet->cellMerge($this->y, $tcol, 1, 0);
220 if ($this->params[0] != '') // Comments
223 $str = _("Comments") . ':';
224 $this->sheet->writeString($this->y, 1, $str);
225 $this->sheet->writeString($this->y, 2, $this->params[0]);
228 if ($this->headers2 != null)
230 for ($i = 0, $j = 0; $i < $this->numcols; $i++)
232 if ($this->cols2[$j] >= $this->cols[$i] && $this->cols2[$j] <= $this->cols[$i + 1])
234 if ($this->aligns2[$j] == "right")
235 $this->sheet->writeString($this->y, $i + 1, $this->headers2[$j], 'formatHeaderRight');
237 $this->sheet->writeString($this->y, $i + 1, $this->headers2[$j], 'formatHeaderLeft');
241 $this->sheet->writeString($this->y, $i + 1, "", 'formatHeaderLeft');
246 for ($i = 0; $i < $this->numcols; $i++)
248 if (!isset($this->headers[$i]))
251 $header = $this->headers[$i];
252 if ($this->aligns[$i] == "right")
253 $this->sheet->writeString($this->y, $i + 1, $header, 'formatHeaderRight');
255 $this->sheet->writeString($this->y, $i + 1, $header, 'formatHeaderLeft');
260 function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
265 function AddImage($logo, $x, $y, $w, $h)
270 function SetDrawColor($r, $g, $b)
275 function SetTextColor($r, $g, $b)
280 function Text($c, $txt, $n=0, $corr=0, $r=0)
285 function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
290 function TextCol($c, $n, $txt, $corr=0, $r=0)
292 if ($this->aligns[$c] == 'right')
293 $this->sheet->writeString($this->y, $c + 1, $txt, 'formatRight');
295 $this->sheet->writeString($this->y, $c + 1, $txt);
297 $this->sheet->cellMerge($this->y, $c + 1, $n - $c - 1, 0);
300 function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0)
302 if (!is_numeric($txt))
304 $this->sheet->writeNumber($this->y, $c + 1, $txt, $this->NumFormat($dec));
307 function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0)
310 $txt = date2sql($txt);
311 $date = $this->sheet->convertMysqlDate($txt);
312 $this->sheet->writeDateTime($this->y, $c + 1, $date, 'formatDate');
315 function TextCol2($c, $n, $txt, $corr=0, $r=0)
317 $this->sheet->writeString($this->y, $c + 1, $txt);
319 $this->sheet->cellMerge($this->y, $c + 1, $n - $c - 1, 0);
322 function TextColLines($c, $n, $txt, $corr=0, $r=0)
327 function TextWrapLines($c, $width, $txt, $align='left')
332 function LineTo($from, $row, $to, $row2)
337 function Line($row, $height = 0)
342 function NewLine($l=1, $np=0)
347 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
350 $this->sheet->cellMerge($this->y, 1, $this->numcols - 1, 0);
351 $this->sheet->writeString($this->y, 1, "", 'formatFooter');
353 $dir = $comp_path.'/'.user_company(). '/pdf_files';
355 if (!file_exists($dir))
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
370 $Ajax->redirect($fname); // otherwise use faster method
374 $this->sendHeaders();
377 // first have a look through the directory,
378 // and remove old temporary pdfs
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);