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
33 var $bottomMargin = 0;
53 var $formatHeaderLeft;
54 var $formatHeaderRight;
55 var $formatAmount = array();
58 function FrontReport($title, $filename, $size = 'A4', $fontsize = 9)
60 global $comp_path, $dateseps, $thoseps, $decseps;
63 $this->title = $title;
64 $this->lineHeight = 12;
65 $this->fontSize = $fontsize;
66 $this->oldFontSize = 0;
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');
86 $how = user_date_format();
87 $sep = $dateseps[user_date_sep()];
92 $dateformat_long = "mm{$sep}dd{$sep}yyyy\ \ hh:mm\ am/pm";
93 $dateformat = "mm{$sep}dd{$sep}yyyy";
97 $dateformat_long = "dd{$sep}mm{$sep}yyyy\ \ hh:mm";
98 $dateformat = "dd{$sep}mm{$sep}yyyy";
102 $dateformat_long = "yyyy{$sep}mm{$sep}dd\ \ hh:mm";
103 $dateformat = "yyyy{$sep}mm{$sep}dd";
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');
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');
147 function Font($style = 'normal')
151 function Info($params, $cols, $headers, $aligns,
152 $cols2 = null, $headers2 = null, $aligns2 = null)
154 global $app_title, $version, $power_by, $power_url;
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)
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;
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]);
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);
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);
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++)
205 if ($this->params[$i]['from'] != '')
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);
216 $this->sheet->writeString($this->y, 4, $this->user);
217 $this->sheet->cellMerge($this->y, 4, 1, 0);
221 if ($this->params[0] != '') // Comments
224 $str = _("Comments") . ':';
225 $this->sheet->writeString($this->y, 1, $str);
226 $this->sheet->writeString($this->y, 2, $this->params[0]);
229 if ($this->headers2 != null)
231 $count = count($this->headers2);
232 for ($i = 0; $i < $count; $i++)
234 if ($this->aligns2[$i] == "right")
235 $this->sheet->writeString($this->y, $i + 1, $this->headers2[$i], $this->formatHeaderRight);
237 $this->sheet->writeString($this->y, $i + 1, $this->headers2[$i], $this->formatHeaderLeft);
242 $count = count($this->headers);
243 for ($i = 0; $i < $count; $i++)
245 if ($this->aligns[$i] == "right")
246 $this->sheet->writeString($this->y, $i + 1, $this->headers[$i], $this->formatHeaderRight);
248 $this->sheet->writeString($this->y, $i + 1, $this->headers[$i], $this->formatHeaderLeft);
253 function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
258 function AddImage($logo, $x, $y, $w, $h)
263 function SetDrawColor($r, $g, $b)
268 function SetTextColor($r, $g, $b)
273 function Text($c, $txt, $n=0, $corr=0, $r=0)
278 function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
283 function TextCol($c, $n, $txt, $corr=0, $r=0)
285 if ($this->aligns[$c] == 'right')
286 $this->sheet->writeString($this->y, $c + 1, $txt, $this->formatRight);
288 $this->sheet->writeString($this->y, $c + 1, $txt);
290 $this->sheet->cellMerge($this->y, $c + 1, $n - $c - 1, 0);
293 function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0)
295 if (!is_numeric($txt))
297 $this->sheet->writeNumber($this->y, $c + 1, $txt, $this->formatAmount[$dec]);
300 function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0)
303 $txt = date2sql($txt);
304 $date = $this->sheet->convertMysqlDate($txt);
305 $this->sheet->writeDate($this->y, $c + 1, $date, $this->formatDate);
308 function TextCol2($c, $n, $txt, $corr=0, $r=0)
310 $this->sheet->writeString($this->y, $c + 1, $txt);
312 $this->sheet->cellMerge($this->y, $c + 1, $n - $c - 1, 0);
315 function TextColLines($c, $n, $txt, $corr=0, $r=0)
320 function TextWrapLines($c, $width, $txt, $align='left')
325 function LineTo($from, $row, $to, $row2)
330 function Line($row, $height = 0)
335 function NewLine($l=1, $np=0)
340 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
343 $this->sheet->cellMerge($this->y, 1, count($this->headers) - 1, 0);
344 $this->sheet->writeString($this->y, 1, "", $this->formatFooter);
346 $dir = $comp_path.'/'.user_company(). '/pdf_files';
348 if (!file_exists($dir))
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);
360 if (user_rep_popup())
361 $Ajax->popup($fname); // when embeded pdf viewer used
363 $Ajax->redirect($fname); // otherwise use faster method
367 $this->xml->sendHeaders();
368 $this->xml->writeData();
370 // first have a look through the directory,
371 // and remove old temporary pdfs
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);