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 Workbook
31 var $bottomMargin = 0;
52 var $formatHeaderLeft;
53 var $formatHeaderRight;
55 var $formatAmount = array();
59 function FrontReport($title, $filename, $size = 'A4', $fontsize = 9)
61 global $comp_path, $dateseps;
64 $this->title = $title;
65 $this->lineHeight = 12;
66 $this->fontSize = $fontsize;
67 $this->oldFontSize = 0;
70 $this->rtl = ($_SESSION['language']->dir === 'rtl' ? 'rtl' : 'ltr');
71 $this->code = $_SESSION['language']->encoding;
72 $this->filename = $filename.".xls";
73 $this->unique_name = uniqid('').".xls";
74 $this->path = $comp_path.'/'.user_company(). '/pdf_files';
75 $this->Workbook($this->path."/".$this->unique_name);
77 $this->sheet =& $this->add_worksheet($this->title);
78 if ($this->rtl === 'rtl')
79 $this->sheet->set_rtl();
80 $this->formatTitle =& $this->add_format();
81 $this->formatTitle->set_size(16);
82 $this->formatTitle->set_bold();
83 $this->formatTitle->set_top(2);
84 $this->formatTitle->set_top_color('gray');
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->add_format();
106 $this->formatDateTime->set_num_format($dateformat_long);
107 $this->formatDateTime->set_align('left');
108 $this->formatDate =& $this->add_format();
109 $this->formatDate->set_num_format($dateformat);
110 $this->formatDate->set_align('left');
111 $this->formatRight =& $this->add_format();
112 $this->formatRight->set_align('right');
114 $this->formatHeaderLeft =& $this->add_format();
115 $this->formatHeaderLeft->set_italic();
116 $this->formatHeaderLeft->set_top(2);
117 $this->formatHeaderLeft->set_top_color('gray');
118 $this->formatHeaderLeft->set_bottom(2);
119 $this->formatHeaderLeft->set_bottom_color('gray');
120 $this->formatHeaderLeft->set_align('vcenter');
121 $this->formatHeaderLeft->set_align('left');
122 $this->formatHeaderRight =& $this->add_format();
123 $this->formatHeaderRight->set_italic();
124 $this->formatHeaderRight->set_top(2);
125 $this->formatHeaderRight->set_top_color('gray');
126 $this->formatHeaderRight->set_bottom(2);
127 $this->formatHeaderRight->set_bottom_color('gray');
128 $this->formatHeaderRight->set_align('vcenter');
129 $this->formatHeaderRight->set_align('right');
130 $this->formatFooter =& $this->add_format();
131 $this->formatFooter->set_top(2);
132 $this->formatFooter->set_top_color('gray');
135 function NumFormat($dec)
137 if (!isset($this->formatAmount[$dec]))
139 global $thoseps,$decseps;
141 $tsep = $thoseps[user_tho_sep()];
142 $dsep = $decseps[user_dec_sep()];
143 $format = "###{$tsep}###{$tsep}###{$tsep}##0";
145 $format .= "{$dsep}".str_repeat('0',$dec);
146 $this->formatAmount[$dec] =& $this->add_format();
147 $this->formatAmount[$dec]->set_num_format($format);
148 $this->formatAmount[$dec]->set_align('right');
150 return $this->formatAmount[$dec];
153 function Font($style = 'normal')
157 function Info($params, $cols, $headers, $aligns,
158 $cols2 = null, $headers2 = null, $aligns2 = null)
160 global $app_title, $version, $power_by, $power_url;
161 $this->company = get_company_prefs();
162 //$this->docTitle($this->title);
163 //$this->docAuthor($app_title . ' ' . $version);
164 //$this->docCompany($this->company['coy_name']);
165 //$this->docManager($power_by . ' - ' . $power_url);
166 $year = get_current_fiscalyear();
167 if ($year['closed'] == 0)
171 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . " " . "(" . $how . ")";
172 $this->user = $_SESSION["wa_current_user"]->name;
173 $this->host = $_SERVER['SERVER_NAME'];
174 $this->params = $params;
176 $this->headers = $headers;
177 $this->aligns = $aligns;
178 $this->cols2 = $cols2;
179 $this->headers2 = $headers2;
180 $this->aligns2 = $aligns2;
181 $this->numcols = count($this->headers);
182 $tcols = count($this->headers2);
183 if ($tcols > $this->numcols)
184 $this->numcols = $tcols;
185 for ($i = 0; $i < $this->numcols; $i++)
186 $this->sheet->set_column($i, $i, $this->px2units($this->cols[$i + 1] - $this->cols[$i]));
192 $tcol = $this->numcols - 1;
193 $this->sheet->set_row($this->y, 20);
194 for ($i = 0; $i < $this->numcols; $i++)
195 $this->sheet->write_blank($this->y, $i, $this->formatTitle);
196 $this->sheet->write_string($this->y, 0, $this->title, $this->formatTitle);
197 $this->sheet->merge_cells($this->y, 0, $this->y, $tcol);
200 $str = _("Print Out Date") . ':';
201 $this->sheet->write_string($this->y, 0, $str);
202 $this->sheet->write_string($this->y, 1, Today() . " ".Now());
203 $this->sheet->write_string($this->y, $tcol-1, $this->company['coy_name']);
204 $this->sheet->merge_cells($this->y, $tcol-1, $this->y, $tcol);
206 $str = _("Fiscal Year") . ':';
207 $this->sheet->write_string($this->y, 0, $str);
208 $str = $this->fiscal_year;
209 $this->sheet->write_string($this->y, 1, $str);
210 $this->sheet->write_string($this->y, $tcol-1, $this->host);
211 $this->sheet->merge_cells($this->y, $tcol-1, $this->y, $tcol);
212 for ($i = 1; $i < count($this->params); $i++)
214 if ($this->params[$i]['from'] != '')
217 $str = $this->params[$i]['text'] . ':';
218 $this->sheet->write_string($this->y, 0, $str);
219 $str = $this->params[$i]['from'];
220 if ($this->params[$i]['to'] != '')
221 $str .= " - " . $this->params[$i]['to'];
222 $this->sheet->write_string($this->y, 1, $str);
225 $this->sheet->write_string($this->y, $tcol-1, $this->user);
226 $this->sheet->merge_cells($this->y, $tcol-1, $this->y, $tcol);
230 if ($this->params[0] != '') // Comments
233 $str = _("Comments") . ':';
234 $this->sheet->write_string($this->y, 0, $str);
235 $this->sheet->write_string($this->y, 1, $this->params[0]);
238 if ($this->headers2 != null)
240 for ($i = 0, $j = 0; $i < $this->numcols; $i++)
242 if ($this->cols2[$j] >= $this->cols[$i] && $this->cols2[$j] <= $this->cols[$i + 1])
244 if ($this->aligns2[$j] == "right")
245 $this->sheet->write_string($this->y, $i, $this->headers2[$j], $this->formatHeaderRight);
247 $this->sheet->write_string($this->y, $i, $this->headers2[$j], $this->formatHeaderLeft);
251 $this->sheet->write_string($this->y, $i, "", $this->formatHeaderLeft);
256 for ($i = 0; $i < $this->numcols; $i++)
258 if (!isset($this->headers[$i]))
261 $header = $this->headers[$i];
262 if ($this->aligns[$i] == "right")
263 $this->sheet->write_string($this->y, $i, $header, $this->formatHeaderRight);
265 $this->sheet->write_string($this->y, $i, $header, $this->formatHeaderLeft);
270 function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
275 function AddImage($logo, $x, $y, $w, $h)
280 function SetDrawColor($r, $g, $b)
285 function SetTextColor($r, $g, $b)
290 function Text($c, $txt, $n=0, $corr=0, $r=0)
295 function TextWrap($xpos, $ypos, $len, $str, $align = 'left')
300 function TextCol($c, $n, $txt, $corr=0, $r=0)
302 if ($this->aligns[$c] == 'right')
303 $this->sheet->write_string($this->y, $c, $txt, $this->formatRight);
305 $this->sheet->write_string($this->y, $c, $txt);
307 $this->sheet->merge_cells($this->y, $c, $this->y, $n - 1);
310 function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0)
312 if (!is_numeric($txt))
314 $this->sheet->write_number($this->y, $c, $txt, $this->NumFormat($dec));
317 function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0)
320 $txt = date2sql($txt);
321 list($year, $mo, $day) = explode("-", $txt);
322 $date = $this->ymd2date((int)$year, (int)$mo, (int)$day);
323 //$date = $this->ymd2date(2009, 3, 2);
324 $this->sheet->write_number($this->y, $c, $date, $this->formatDate);
325 //$this->sheet->write_string($this->y, $c, $txt);
328 function TextCol2($c, $n, $txt, $corr=0, $r=0)
330 $this->sheet->write_string($this->y, $c, $txt);
332 $this->sheet->merge_cells($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;
398 $unit_offset_map = array(0, 36, 73, 109, 146, 182, 219);
399 $width_units = $excel_column_width_factor * ($px / $unit_offset_length);
400 $width_units += $unit_offset_map[($px % $unit_offset_length)];
402 $pixels = (float)((float)$px / (float)$excel_column_width_factor) * $unit_offset_length;
403 $ofs = $px % $excel_column_width_factor;
404 $pixels += round((float)$ofs / ((float) $excel_column_width_factor / $unit_offset_length));
407 return ($px / $unit_offset_length);
410 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
412 for ($i = 0; $i < $this->numcols; $i++)
413 $this->sheet->write_blank($this->y, $i, $this->formatFooter);
414 $this->sheet->merge_cells($this->y, 0, $this->y, $this->numcols - 1);
416 // first have a look through the directory,
417 // and remove old temporary pdfs
418 if ($d = @opendir($this->path)) {
419 while (($file = readdir($d)) !== false) {
420 if (!is_file($this->path.'/'.$file) || $file == 'index.php') continue;
421 // then check to see if this one is too old
422 $ftime = filemtime($this->path.'/'.$file);
423 // seems 3 min is enough for any report download, isn't it?
424 if (time()-$ftime > 180){
425 unlink($this->path.'/'.$file);
430 meta_forward($_SERVER['PHP_SELF'], "xls=1&filename=$this->filename&unique=$this->unique_name");