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/Workbook.php");
15 include_once($path_to_root . "/admin/db/company_db.inc");
16 include_once($path_to_root . "/config.php");
18 class FrontReport extends Spreadsheet_Excel_Writer_Workbook
30 var $bottomMargin = 0;
51 var $formatHeaderLeft;
52 var $formatHeaderRight;
54 var $formatAmount = array();
58 function FrontReport($title, $filename, $size = 'A4', $fontsize = 9)
60 global $comp_path, $dateseps;
63 $this->title = $title;
64 $this->lineHeight = 12;
65 $this->fontSize = $fontsize;
66 $this->oldFontSize = 0;
69 $rtl = ($_SESSION['language']->dir == 'rtl');
70 $this->code = strtolower($_SESSION['language']->encoding);
71 $this->filename = $filename.".xls";
72 $this->unique_name = uniqid('').".xls";
73 $this->path = $comp_path.'/'.user_company(). '/pdf_files';
74 $this->Spreadsheet_Excel_Writer_Workbook($this->path."/".$this->unique_name);
75 //$this->setCountry(48);
76 if ($this->code != "iso-8859-1")
77 $this->setVersion(8); // set biff version to 8 (0x0006 internal)
78 $this->sheet =& $this->addWorksheet($this->title);
79 if ($this->code != "iso-8859-1")
80 $this->sheet->setInputEncoding($this->code); // set sheet encoding
82 $this->sheet->setRTL();
83 $this->formatTitle =& $this->addFormat();
84 $this->formatTitle->setSize(16);
85 $this->formatTitle->setBold();
86 $this->formatTitle->setAlign($rtl ? 'right' : 'left');
87 $this->formatTitle->setTop(2);
88 $this->formatTitle->setTopColor('gray');
90 $how = user_date_format();
91 $sep = $dateseps[user_date_sep()];
96 $dateformat_long = "mm{$sep}dd{$sep}yyyy\ \ hh:mm\ am/pm";
97 $dateformat = "mm{$sep}dd{$sep}yyyy";
101 $dateformat_long = "dd{$sep}mm{$sep}yyyy\ \ hh:mm";
102 $dateformat = "dd{$sep}mm{$sep}yyyy";
106 $dateformat_long = "yyyy{$sep}mm{$sep}dd\ \ hh:mm";
107 $dateformat = "yyyy{$sep}mm{$sep}dd";
109 $this->formatDateTime =& $this->addFormat();
110 $this->formatDateTime->setNumFormat($dateformat_long);
111 $this->formatDateTime->setAlign($rtl ? 'right' : 'left');
112 $this->formatDate =& $this->addFormat();
113 $this->formatDate->setNumFormat($dateformat);
114 $this->formatDate->setAlign($rtl ? 'right' : 'left');
115 $this->formatRight =& $this->addFormat();
116 $this->formatRight->setAlign($rtl ? 'left' : 'right');
117 $this->formatLeft =& $this->addFormat();
118 $this->formatLeft->setAlign($rtl ? 'right' : 'left');
120 $this->formatHeaderLeft =& $this->addFormat();
121 $this->formatHeaderLeft->setItalic();
122 $this->formatHeaderLeft->setTop(2);
123 $this->formatHeaderLeft->setTopColor('gray');
124 $this->formatHeaderLeft->setBottom(2);
125 $this->formatHeaderLeft->setBottomColor('gray');
126 $this->formatHeaderLeft->setAlign('vcenter');
127 $this->formatDate->setAlign($rtl ? 'right' : 'left');
128 $this->formatHeaderRight =& $this->addFormat();
129 $this->formatHeaderRight->setItalic();
130 $this->formatHeaderRight->setTop(2);
131 $this->formatHeaderRight->setTopColor('gray');
132 $this->formatHeaderRight->setBottom(2);
133 $this->formatHeaderRight->setBottomColor('gray');
134 $this->formatHeaderRight->setAlign('vcenter');
135 $this->formatHeaderRight->setAlign('right');
136 $this->formatFooter =& $this->addFormat();
137 $this->formatFooter->setTop(2);
138 $this->formatFooter->setTopColor('gray');
141 function NumFormat($dec)
143 if (!isset($this->formatAmount[$dec]))
145 global $thoseps,$decseps;
147 $tsep = $thoseps[user_tho_sep()];
148 $dsep = $decseps[user_dec_sep()];
149 $format = "###{$tsep}###{$tsep}###{$tsep}##0";
151 $format .= "{$dsep}".str_repeat('0',$dec);
152 $this->formatAmount[$dec] =& $this->addFormat();
153 $this->formatAmount[$dec]->setNumFormat($format);
154 $this->formatAmount[$dec]->setAlign('right');
156 return $this->formatAmount[$dec];
159 function Font($style = 'normal')
163 function Info($params, $cols, $headers, $aligns,
164 $cols2 = null, $headers2 = null, $aligns2 = null)
166 global $app_title, $version, $power_by, $power_url;
167 $this->company = get_company_prefs();
168 //$this->docTitle($this->title);
169 //$this->docAuthor($app_title . ' ' . $version);
170 //$this->docCompany($this->company['coy_name']);
171 //$this->docManager($power_by . ' - ' . $power_url);
172 $year = get_current_fiscalyear();
173 if ($year['closed'] == 0)
177 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . " " . "(" . $how . ")";
178 $this->user = $_SESSION["wa_current_user"]->name;
179 $this->host = $_SERVER['SERVER_NAME'];
180 $this->params = $params;
182 $this->headers = $headers;
183 $this->aligns = $aligns;
184 $this->cols2 = $cols2;
185 $this->headers2 = $headers2;
186 $this->aligns2 = $aligns2;
187 $this->numcols = count($this->headers);
188 $tcols = count($this->headers2);
189 if ($tcols > $this->numcols)
190 $this->numcols = $tcols;
191 for ($i = 0; $i < $this->numcols; $i++)
192 $this->sheet->setColumn($i, $i, $this->px2units($this->cols[$i + 1] - $this->cols[$i]));
198 $tcol = $this->numcols - 1;
199 $this->sheet->setRow($this->y, 20);
200 for ($i = 0; $i < $this->numcols; $i++)
201 $this->sheet->writeBlank($this->y, $i, $this->formatTitle);
202 $this->sheet->writeString($this->y, 0, $this->title, $this->formatTitle);
203 $this->sheet->mergeCells($this->y, 0, $this->y, $tcol);
205 $str = _("Print Out Date") . ':';
206 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
207 $this->sheet->writeString($this->y, 1, Today() . " ".Now(), $this->formatLeft);
208 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
209 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
211 $str = _("Fiscal Year") . ':';
212 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
213 $str = $this->fiscal_year;
214 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
215 $this->sheet->writeString($this->y, $tcol-1, $this->host, $this->formatLeft);
216 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
217 for ($i = 1; $i < count($this->params); $i++)
219 if ($this->params[$i]['from'] != '')
222 $str = $this->params[$i]['text'] . ':';
223 $this->sheet->writeString($this->y, 0, $str);
224 $str = $this->params[$i]['from'];
225 if ($this->params[$i]['to'] != '')
226 $str .= " - " . $this->params[$i]['to'];
227 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
230 $this->sheet->writeString($this->y, $tcol-1, $this->user, $this->formatLeft);
231 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
235 if ($this->params[0] != '') // Comments
238 $str = _("Comments") . ':';
239 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
240 $this->sheet->writeString($this->y, 1, $this->params[0], $this->formatLeft);
243 if ($this->headers2 != null)
245 for ($i = 0, $j = 0; $i < $this->numcols; $i++)
247 if ($this->cols2[$j] >= $this->cols[$i] && $this->cols2[$j] <= $this->cols[$i + 1])
249 if ($this->aligns2[$j] == "right")
250 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatHeaderRight);
252 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatHeaderLeft);
256 $this->sheet->writeString($this->y, $i, "", $this->formatHeaderLeft);
261 for ($i = 0; $i < $this->numcols; $i++)
263 if (!isset($this->headers[$i]))
266 $header = $this->headers[$i];
267 if ($this->aligns[$i] == "right")
268 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderRight);
270 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderLeft);
275 function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
280 function AddImage($logo, $x, $y, $w, $h)
285 function SetDrawColor($r, $g, $b)
290 function SetTextColor($r, $g, $b)
295 function Text($c, $txt, $n=0, $corr=0, $r=0)
300 function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
305 function TextCol($c, $n, $txt, $corr=0, $r=0)
307 if ($this->aligns[$c] == 'right')
308 $this->sheet->writeString($this->y, $c, $txt, $this->formatRight);
310 $this->sheet->writeString($this->y, $c, $txt, $this->formatLeft);
312 $this->sheet->mergeCells($this->y, $c, $this->y, $n - 1);
315 function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0)
317 if (!is_numeric($txt))
319 $this->sheet->writeNumber($this->y, $c, $txt, $this->NumFormat($dec));
322 function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0)
325 $txt = date2sql($txt);
326 list($year, $mo, $day) = explode("-", $txt);
327 $date = $this->ymd2date((int)$year, (int)$mo, (int)$day);
328 //$date = $this->ymd2date(2009, 3, 2);
329 $this->sheet->writeNumber($this->y, $c, $date, $this->formatDate);
330 //$this->sheet->write_string($this->y, $c, $txt);
333 function TextCol2($c, $n, $txt, $corr=0, $r=0)
335 $this->sheet->writeString($this->y, $c, $txt, $this->formatLeft);
337 $this->sheet->mergeCells($this->y, $c, $this->y, $n - 1);
340 function TextColLines($c, $n, $txt, $corr=0, $r=0)
345 function TextWrapLines($c, $width, $txt, $align='left')
350 function LineTo($from, $row, $to, $row2)
355 function Line($row, $height = 0)
360 function NewLine($l=1, $np=0)
365 function ymd2Date($year, $mon, $day) // XLS internal date representation is a number between 1900-01-01 and 2078-12-31
366 { // if we need the time part too, we have to add this value after a decimalpoint.
367 $mo = array(0,31,28,31,30,31,30,31,31,30,31,30,31);
370 if (($year % 4) == 0)
378 elseif ($day > $mo[$mon])
382 elseif ($year > $MAXYEAR)
385 for ($n = 1; $n < $mon; $n++)
389 for ($n = $BASE; $n < $year; $n++)
398 function px2units($px) // XLS app conversion. Not bulletproof.
400 $excel_column_width_factor = 256;
401 $unit_offset_length = 6.5;
403 $unit_offset_map = array(0, 36, 73, 109, 146, 182, 219);
404 $width_units = $excel_column_width_factor * ($px / $unit_offset_length);
405 $width_units += $unit_offset_map[($px % $unit_offset_length)];
407 $pixels = (float)((float)$px / (float)$excel_column_width_factor) * $unit_offset_length;
408 $ofs = $px % $excel_column_width_factor;
409 $pixels += round((float)$ofs / ((float) $excel_column_width_factor / $unit_offset_length));
412 return ($px / $unit_offset_length);
415 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
417 for ($i = 0; $i < $this->numcols; $i++)
418 $this->sheet->writeBlank($this->y, $i, $this->formatFooter);
419 $this->sheet->mergeCells($this->y, 0, $this->y, $this->numcols - 1);
421 // first have a look through the directory,
422 // and remove old temporary pdfs
423 if ($d = @opendir($this->path)) {
424 while (($file = readdir($d)) !== false) {
425 if (!is_file($this->path.'/'.$file) || $file == 'index.php') continue;
426 // then check to see if this one is too old
427 $ftime = filemtime($this->path.'/'.$file);
428 // seems 3 min is enough for any report download, isn't it?
429 if (time()-$ftime > 180){
430 unlink($this->path.'/'.$file);
435 meta_forward($_SERVER['PHP_SELF'], "xls=1&filename=$this->filename&unique=$this->unique_name");