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 ***********************************************************************/
13 include_once($path_to_root . "/reporting/includes/Workbook.php");
14 include_once($path_to_root . "/admin/db/company_db.inc");
15 include_once($path_to_root . "/config.php");
17 class FrontReport extends Spreadsheet_Excel_Writer_Workbook
29 var $bottomMargin = 0;
50 var $formatHeaderLeft;
51 var $formatHeaderRight;
53 var $formatAmount = array();
57 function FrontReport($title, $filename, $size = 'A4', $fontsize = 9)
59 global $comp_path, $dateseps;
62 $this->title = $title;
63 $this->lineHeight = 12;
64 $this->fontSize = $fontsize;
65 $this->oldFontSize = 0;
68 $rtl = ($_SESSION['language']->dir == 'rtl');
69 $this->code = strtolower($_SESSION['language']->encoding);
70 $this->filename = $filename.".xls";
71 $this->unique_name = uniqid('').".xls";
72 $this->path = $comp_path.'/'.user_company(). '/pdf_files';
73 $this->Spreadsheet_Excel_Writer_Workbook($this->path."/".$this->unique_name);
74 //$this->setCountry(48);
75 if ($this->code != "iso-8859-1")
76 $this->setVersion(8); // set biff version to 8 (0x0006 internal)
77 $this->sheet =& $this->addWorksheet($this->title);
78 if ($this->code != "iso-8859-1")
79 $this->sheet->setInputEncoding($this->code); // set sheet encoding
81 $this->sheet->setRTL();
82 $this->formatTitle =& $this->addFormat();
83 $this->formatTitle->setSize(16);
84 $this->formatTitle->setBold();
85 $this->formatTitle->setAlign($rtl ? 'right' : 'left');
86 $this->formatTitle->setTop(2);
87 $this->formatTitle->setTopColor('gray');
89 $how = user_date_format();
90 $sep = $dateseps[user_date_sep()];
95 $dateformat_long = "mm{$sep}dd{$sep}yyyy\ \ hh:mm\ am/pm";
96 $dateformat = "mm{$sep}dd{$sep}yyyy";
100 $dateformat_long = "dd{$sep}mm{$sep}yyyy\ \ hh:mm";
101 $dateformat = "dd{$sep}mm{$sep}yyyy";
105 $dateformat_long = "yyyy{$sep}mm{$sep}dd\ \ hh:mm";
106 $dateformat = "yyyy{$sep}mm{$sep}dd";
108 $this->formatDateTime =& $this->addFormat();
109 $this->formatDateTime->setNumFormat($dateformat_long);
110 $this->formatDateTime->setAlign($rtl ? 'right' : 'left');
111 $this->formatDate =& $this->addFormat();
112 $this->formatDate->setNumFormat($dateformat);
113 $this->formatDate->setAlign($rtl ? 'right' : 'left');
114 $this->formatRight =& $this->addFormat();
115 $this->formatRight->setAlign($rtl ? 'left' : 'right');
116 $this->formatLeft =& $this->addFormat();
117 $this->formatLeft->setAlign($rtl ? 'right' : 'left');
119 $this->formatHeaderLeft =& $this->addFormat();
120 $this->formatHeaderLeft->setItalic();
121 $this->formatHeaderLeft->setTop(2);
122 $this->formatHeaderLeft->setTopColor('gray');
123 $this->formatHeaderLeft->setBottom(2);
124 $this->formatHeaderLeft->setBottomColor('gray');
125 $this->formatHeaderLeft->setAlign('vcenter');
126 $this->formatDate->setAlign($rtl ? 'right' : 'left');
127 $this->formatHeaderRight =& $this->addFormat();
128 $this->formatHeaderRight->setItalic();
129 $this->formatHeaderRight->setTop(2);
130 $this->formatHeaderRight->setTopColor('gray');
131 $this->formatHeaderRight->setBottom(2);
132 $this->formatHeaderRight->setBottomColor('gray');
133 $this->formatHeaderRight->setAlign('vcenter');
134 $this->formatHeaderRight->setAlign('right');
135 $this->formatFooter =& $this->addFormat();
136 $this->formatFooter->setTop(2);
137 $this->formatFooter->setTopColor('gray');
140 function NumFormat($dec)
142 if (!isset($this->formatAmount[$dec]))
144 //global $thoseps,$decseps;
146 //$tsep = $thoseps[user_tho_sep()];
147 //$dsep = $decseps[user_dec_sep()];
150 $format = "###{$tsep}###{$tsep}###{$tsep}##0";
152 $format .= "{$dsep}".str_repeat('0',$dec);
153 $this->formatAmount[$dec] =& $this->addFormat();
154 $this->formatAmount[$dec]->setNumFormat($format);
155 $this->formatAmount[$dec]->setAlign('right');
157 return $this->formatAmount[$dec];
160 function Font($style = 'normal')
164 function Info($params, $cols, $headers, $aligns,
165 $cols2 = null, $headers2 = null, $aligns2 = null)
167 global $app_title, $version, $power_by, $power_url;
168 $this->company = get_company_prefs();
169 $year = get_current_fiscalyear();
170 if ($year['closed'] == 0)
174 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . " " . "(" . $how . ")";
175 $this->user = $_SESSION["wa_current_user"]->name;
176 $this->host = $_SERVER['SERVER_NAME'];
177 $this->params = $params;
179 $this->headers = $headers;
180 $this->aligns = $aligns;
181 $this->cols2 = $cols2;
182 $this->headers2 = $headers2;
183 $this->aligns2 = $aligns2;
184 $this->numcols = count($this->headers);
185 $tcols = count($this->headers2);
186 if ($tcols > $this->numcols)
187 $this->numcols = $tcols;
188 for ($i = 0; $i < $this->numcols; $i++)
189 $this->sheet->setColumn($i, $i, $this->px2units($this->cols[$i + 1] - $this->cols[$i]));
195 $tcol = $this->numcols - 1;
196 $this->sheet->setRow($this->y, 20);
197 for ($i = 0; $i < $this->numcols; $i++)
198 $this->sheet->writeBlank($this->y, $i, $this->formatTitle);
199 $this->sheet->writeString($this->y, 0, $this->title, $this->formatTitle);
200 $this->sheet->mergeCells($this->y, 0, $this->y, $tcol);
202 $str = _("Print Out Date") . ':';
203 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
204 $this->sheet->writeString($this->y, 1, Today() . " ".Now(), $this->formatLeft);
205 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
206 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
208 $str = _("Fiscal Year") . ':';
209 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
210 $str = $this->fiscal_year;
211 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
212 $this->sheet->writeString($this->y, $tcol-1, $this->host, $this->formatLeft);
213 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
214 for ($i = 1; $i < count($this->params); $i++)
216 if ($this->params[$i]['from'] != '')
219 $str = $this->params[$i]['text'] . ':';
220 $this->sheet->writeString($this->y, 0, $str);
221 $str = $this->params[$i]['from'];
222 if ($this->params[$i]['to'] != '')
223 $str .= " - " . $this->params[$i]['to'];
224 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
227 $this->sheet->writeString($this->y, $tcol-1, $this->user, $this->formatLeft);
228 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
232 if ($this->params[0] != '') // Comments
235 $str = _("Comments") . ':';
236 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
237 $this->sheet->writeString($this->y, 1, $this->params[0], $this->formatLeft);
240 if ($this->headers2 != null)
242 for ($i = 0, $j = 0; $i < $this->numcols; $i++)
244 if ($this->cols2[$j] >= $this->cols[$i] && $this->cols2[$j] <= $this->cols[$i + 1])
246 if ($this->aligns2[$j] == "right")
247 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatHeaderRight);
249 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatHeaderLeft);
253 $this->sheet->writeString($this->y, $i, "", $this->formatHeaderLeft);
258 for ($i = 0; $i < $this->numcols; $i++)
260 if (!isset($this->headers[$i]))
263 $header = $this->headers[$i];
264 if ($this->aligns[$i] == "right")
265 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderRight);
267 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderLeft);
272 function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
277 function AddImage($logo, $x, $y, $w, $h)
282 function SetDrawColor($r, $g, $b)
287 function SetTextColor($r, $g, $b)
292 function Text($c, $txt, $n=0, $corr=0, $r=0)
297 function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
302 function TextCol($c, $n, $txt, $corr=0, $r=0)
304 if ($this->aligns[$c] == 'right')
305 $this->sheet->writeString($this->y, $c, $txt, $this->formatRight);
307 $this->sheet->writeString($this->y, $c, $txt, $this->formatLeft);
309 $this->sheet->mergeCells($this->y, $c, $this->y, $n - 1);
312 function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0)
314 if (!is_numeric($txt))
316 $this->sheet->writeNumber($this->y, $c, $txt, $this->NumFormat($dec));
319 function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0)
322 $txt = date2sql($txt);
323 list($year, $mo, $day) = explode("-", $txt);
324 $date = $this->ymd2date((int)$year, (int)$mo, (int)$day);
325 $this->sheet->writeNumber($this->y, $c, $date, $this->formatDate);
328 function TextCol2($c, $n, $txt, $corr=0, $r=0)
330 $this->sheet->writeString($this->y, $c, $txt, $this->formatLeft);
332 $this->sheet->mergeCells($this->y, $c, $this->y, $n - 1);
335 function TextColLines($c, $n, $txt, $corr=0, $r=0)
340 function TextWrapLines($c, $width, $txt, $align='left')
345 function LineTo($from, $row, $to, $row2)
350 function Line($row, $height = 0)
355 function NewLine($l=1, $np=0)
360 function ymd2Date($year, $mon, $day) // XLS internal date representation is a number between 1900-01-01 and 2078-12-31
361 { // if we need the time part too, we have to add this value after a decimalpoint.
362 $mo = array(0,31,28,31,30,31,30,31,31,30,31,30,31);
365 if (($year % 4) == 0)
373 elseif ($day > $mo[$mon])
377 elseif ($year > $MAXYEAR)
380 for ($n = 1; $n < $mon; $n++)
384 for ($n = $BASE; $n < $year; $n++)
393 function px2units($px) // XLS app conversion. Not bulletproof.
395 $excel_column_width_factor = 256;
396 $unit_offset_length = 6.5;
397 return ($px / $unit_offset_length);
400 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
402 for ($i = 0; $i < $this->numcols; $i++)
403 $this->sheet->writeBlank($this->y, $i, $this->formatFooter);
404 $this->sheet->mergeCells($this->y, 0, $this->y, $this->numcols - 1);
406 // first have a look through the directory,
407 // and remove old temporary pdfs
408 if ($d = @opendir($this->path)) {
409 while (($file = readdir($d)) !== false) {
410 if (!is_file($this->path.'/'.$file) || $file == 'index.php') continue;
411 // then check to see if this one is too old
412 $ftime = filemtime($this->path.'/'.$file);
413 // seems 3 min is enough for any report download, isn't it?
414 if (time()-$ftime > 180){
415 unlink($this->path.'/'.$file);
420 meta_forward($_SERVER['PHP_SELF'], "xls=1&filename=$this->filename&unique=$this->unique_name");