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 ***********************************************************************/
12 include_once($path_to_root . "/reporting/includes/Workbook.php");
13 include_once($path_to_root . "/admin/db/company_db.inc");
14 include_once($path_to_root . "/config.php");
16 class FrontReport extends Spreadsheet_Excel_Writer_Workbook
28 var $bottomMargin = 0;
45 var $excelColWidthFactor;
51 var $formatHeaderLeft;
52 var $formatHeaderRight;
54 var $formatAmount = array();
58 function FrontReport($title, $filename, $size = 'A4', $fontsize = 9, $orientation = 'P', $margins = NULL, $excelColWidthFactor = 6.5)
60 global $comp_path, $dateseps, $page_security;
61 if (!$_SESSION["wa_current_user"]->can_access_page($page_security))
63 display_error(_("The security settings on your account do not permit you to print this report"));
68 $this->title = $title;
69 $this->lineHeight = 12;
71 $this->fontSize = $fontsize;
72 $this->oldFontSize = 0;
75 $this->excelColWidthFactor = $excelColWidthFactor;
76 $rtl = ($_SESSION['language']->dir == 'rtl');
77 $this->code = strtolower($_SESSION['language']->encoding);
78 $this->filename = $filename.".xls";
79 $this->unique_name = uniqid('').".xls";
80 $this->path = $comp_path.'/'.user_company(). '/pdf_files';
81 $this->Spreadsheet_Excel_Writer_Workbook($this->path."/".$this->unique_name);
82 //$this->setCountry(48);
83 if ($this->code != "iso-8859-1")
84 $this->setVersion(8); // set biff version to 8 (0x0006 internal)
85 $this->sheet =& $this->addWorksheet($this->worksheetNameGenerator($this->title));
86 if ($this->code != "iso-8859-1")
87 $this->sheet->setInputEncoding($this->code); // set sheet encoding
89 $this->sheet->setRTL();
90 $this->formatTitle =& $this->addFormat();
91 $this->formatTitle->setSize(16);
92 $this->formatTitle->setBold();
93 $this->formatTitle->setAlign($rtl ? 'right' : 'left');
94 $this->formatTitle->setTop(2);
95 $this->formatTitle->setTopColor('gray');
97 $how = user_date_format();
98 $sep = $dateseps[user_date_sep()];
103 $dateformat_long = "mm{$sep}dd{$sep}yyyy\ \ hh:mm\ am/pm";
104 $dateformat = "mm{$sep}dd{$sep}yyyy";
108 $dateformat_long = "dd{$sep}mm{$sep}yyyy\ \ hh:mm";
109 $dateformat = "dd{$sep}mm{$sep}yyyy";
113 $dateformat_long = "yyyy{$sep}mm{$sep}dd\ \ hh:mm";
114 $dateformat = "yyyy{$sep}mm{$sep}dd";
116 $this->formatDateTime =& $this->addFormat();
117 $this->formatDateTime->setNumFormat($dateformat_long);
118 $this->formatDateTime->setAlign($rtl ? 'right' : 'left');
119 $this->formatDate =& $this->addFormat();
120 $this->formatDate->setNumFormat($dateformat);
121 $this->formatDate->setAlign($rtl ? 'right' : 'left');
122 $this->formatRight =& $this->addFormat();
123 $this->formatRight->setAlign($rtl ? 'left' : 'right');
124 $this->formatLeft =& $this->addFormat();
125 $this->formatLeft->setAlign($rtl ? 'right' : 'left');
127 $this->formatHeaderLeft =& $this->addFormat();
128 $this->formatHeaderLeft->setItalic();
129 $this->formatHeaderLeft->setTop(2);
130 $this->formatHeaderLeft->setTopColor('gray');
131 $this->formatHeaderLeft->setBottom(2);
132 $this->formatHeaderLeft->setBottomColor('gray');
133 $this->formatHeaderLeft->setAlign('vcenter');
134 $this->formatTopHeaderLeft =& $this->addFormat();
135 $this->formatTopHeaderLeft->setItalic();
136 $this->formatTopHeaderLeft->setTop(2);
137 $this->formatTopHeaderLeft->setTopColor('gray');
138 $this->formatTopHeaderLeft->setAlign('vcenter');
139 $this->formatBottomHeaderLeft =& $this->addFormat();
140 $this->formatBottomHeaderLeft->setItalic();
141 $this->formatBottomHeaderLeft->setBottom(2);
142 $this->formatBottomHeaderLeft->setBottomColor('gray');
143 $this->formatBottomHeaderLeft->setAlign('vcenter');
144 $this->formatDate->setAlign($rtl ? 'right' : 'left');
145 $this->formatHeaderRight =& $this->addFormat();
146 $this->formatHeaderRight->setItalic();
147 $this->formatHeaderRight->setTop(2);
148 $this->formatHeaderRight->setTopColor('gray');
149 $this->formatHeaderRight->setBottom(2);
150 $this->formatHeaderRight->setBottomColor('gray');
151 $this->formatHeaderRight->setAlign('vcenter');
152 $this->formatHeaderRight->setAlign('right');
153 $this->formatTopHeaderRight =& $this->addFormat();
154 $this->formatTopHeaderRight->setItalic();
155 $this->formatTopHeaderRight->setTop(2);
156 $this->formatTopHeaderRight->setTopColor('gray');
157 $this->formatTopHeaderRight->setAlign('vcenter');
158 $this->formatTopHeaderRight->setAlign('right');
159 $this->formatBottomHeaderRight =& $this->addFormat();
160 $this->formatBottomHeaderRight->setItalic();
161 $this->formatBottomHeaderRight->setBottom(2);
162 $this->formatBottomHeaderRight->setBottomColor('gray');
163 $this->formatBottomHeaderRight->setAlign('vcenter');
164 $this->formatBottomHeaderRight->setAlign('right');
165 $this->formatFooter =& $this->addFormat();
166 $this->formatFooter->setTop(2);
167 $this->formatFooter->setTopColor('gray');
171 // Check a given name to see if it's a valid Excel worksheet name,
172 // and fix if necessary
173 function worksheetNameGenerator($name)
175 // First, strip out characters which aren't allowed
176 $illegal_chars = array(':', '\\', '/', '?', '*', '[', ']');
177 for ($i = 0; $i < count($illegal_chars); $i++)
178 $name = str_replace($illegal_chars[$i], '', $name);
179 // Now, if name is longer than 31 chars, truncate it
180 if (strlen($name) > 31)
181 $name = substr($name, 0, 31);
185 function NumFormat($dec)
187 if (!isset($this->formatAmount[$dec]))
189 //global $thoseps,$decseps;
191 //$tsep = $thoseps[user_tho_sep()];
192 //$dsep = $decseps[user_dec_sep()];
195 $format = "###{$tsep}###{$tsep}###{$tsep}##0";
197 $format .= "{$dsep}".str_repeat('0',$dec);
198 $this->formatAmount[$dec] =& $this->addFormat();
199 $this->formatAmount[$dec]->setNumFormat($format);
200 $this->formatAmount[$dec]->setAlign('right');
202 return $this->formatAmount[$dec];
205 function Font($fontname = '', $style = 'normal')
209 function Info($params, $cols, $headers, $aligns,
210 $cols2 = null, $headers2 = null, $aligns2 = null)
212 global $app_title, $version, $power_by, $power_url;
213 $this->company = get_company_prefs();
214 $year = get_current_fiscalyear();
215 if ($year['closed'] == 0)
219 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . " " . "(" . $how . ")";
220 $this->user = $_SESSION["wa_current_user"]->name;
221 $this->host = $_SERVER['SERVER_NAME'];
222 $this->params = $params;
224 $this->headers = $headers;
225 $this->aligns = $aligns;
226 $this->cols2 = $cols2;
227 $this->headers2 = $headers2;
228 $this->aligns2 = $aligns2;
229 $this->numcols = count($this->headers);
230 $tcols = count($this->headers2);
231 if ($tcols > $this->numcols)
232 $this->numcols = $tcols;
233 for ($i = 0; $i < $this->numcols; $i++)
234 $this->sheet->setColumn($i, $i, $this->px2units($this->cols[$i + 1] - $this->cols[$i]));
240 $tcol = $this->numcols - 1;
241 $this->sheet->setRow($this->y, 20);
242 for ($i = 0; $i < $this->numcols; $i++)
243 $this->sheet->writeBlank($this->y, $i, $this->formatTitle);
244 $this->sheet->writeString($this->y, 0, $this->title, $this->formatTitle);
245 $this->sheet->mergeCells($this->y, 0, $this->y, $tcol);
247 $str = _("Print Out Date") . ':';
248 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
249 $this->sheet->writeString($this->y, 1, Today() . " ".Now(), $this->formatLeft);
250 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
251 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
253 $str = _("Fiscal Year") . ':';
254 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
255 $str = $this->fiscal_year;
256 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
257 $this->sheet->writeString($this->y, $tcol-1, $this->host, $this->formatLeft);
258 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
259 for ($i = 1; $i < count($this->params); $i++)
261 if ($this->params[$i]['from'] != '')
264 $str = $this->params[$i]['text'] . ':';
265 $this->sheet->writeString($this->y, 0, $str);
266 $str = $this->params[$i]['from'];
267 if ($this->params[$i]['to'] != '')
268 $str .= " - " . $this->params[$i]['to'];
269 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
272 $this->sheet->writeString($this->y, $tcol-1, $this->user, $this->formatLeft);
273 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
277 if ($this->params[0] != '') // Comments
280 $str = _("Comments") . ':';
281 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
282 $this->sheet->writeString($this->y, 1, $this->params[0], $this->formatLeft);
285 if ($this->headers2 != null)
287 for ($i = 0, $j = 0; $i < $this->numcols; $i++)
289 if ($this->cols2[$j] >= $this->cols[$i] && $this->cols2[$j] <= $this->cols[$i + 1])
291 if ($this->aligns2[$j] == "right")
292 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatHeaderRight);
294 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatHeaderLeft);
298 $this->sheet->writeString($this->y, $i, "", $this->formatHeaderLeft);
303 for ($i = 0; $i < $this->numcols; $i++)
305 if (!isset($this->headers[$i]))
308 $header = $this->headers[$i];
309 if ($this->aligns[$i] == "right")
310 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderRight);
312 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderLeft);
317 function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
322 // Alternate header style - primary differences are for PDFs
325 // Flag to make sure we only print the company name once
326 $companyNamePrinted = false;
329 $tcol = $this->numcols - 1;
330 $this->sheet->setRow($this->y, 20);
332 for ($i = 0; $i < $this->numcols; $i++)
333 $this->sheet->writeBlank($this->y, $i, $this->formatTitle);
334 $this->sheet->writeString($this->y, 0, $this->title, $this->formatTitle);
335 $this->sheet->mergeCells($this->y, 0, $this->y, $tcol);
337 // Dimension 1 - optional
338 // - only print if available and not blank
339 if (count($this->params) > 3)
340 if ($this->params[3]['from'] != '')
343 $str = $this->params[3]['text'] . ':';
344 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
345 $this->sheet->writeString($this->y, 1, $this->params[3]['from'], $this->formatLeft);
346 // Company Name - at end of this row
347 if (!$companyNamePrinted)
349 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
350 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
351 $companyNamePrinted = true;
356 // Dimension 2 - optional
357 // - only print if available and not blank
358 if (count($this->params) > 4)
359 if ($this->params[4]['from'] != '')
362 $str = $this->params[4]['text'] . ':';
363 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
364 $this->sheet->writeString($this->y, 1, $this->params[4]['from'], $this->formatLeft);
365 // Company Name - at end of this row
366 if (!$companyNamePrinted)
368 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
369 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
370 $companyNamePrinted = true;
377 // Report Date - time period covered
378 // - can specify a range, or just the end date (and the report contents
379 // should make it obvious what the beginning date is)
381 $str = _("Report Date") . ':';
382 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
384 if ($this->params[1]['from'] != '')
385 $str = $this->params[1]['from'] . ' - ';
386 $str .= $this->params[1]['to'];
387 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
388 // Company Name - at end of this row
389 if (!$companyNamePrinted)
391 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
392 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
393 $companyNamePrinted = true;
396 // Timestamp of when this copy of the report was generated
398 $str = _("Generated At") . ':';
399 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
400 $this->sheet->writeString($this->y, 1, Today() . " ".Now(), $this->formatLeft);
402 // Name of the user that generated this copy of the report
404 $str = _("Generated By") . ':';
405 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
407 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
409 // Comments - display any user-generated comments for this copy of the report
410 if ($this->params[0] != '')
413 $str = _("Comments") . ':';
414 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
415 $this->sheet->writeString($this->y, 1, $this->params[0], $this->formatLeft);
419 if ($this->headers2 != null)
421 for ($i = 0, $j = 0; $i < $this->numcols; $i++)
423 if ($this->cols2[$j] >= $this->cols[$i] && $this->cols2[$j] <= $this->cols[$i + 1])
425 if ($this->aligns2[$j] == "right")
426 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatTopHeaderRight);
428 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatTopHeaderLeft);
432 $this->sheet->writeString($this->y, $i, "", $this->formatTopHeaderLeft);
437 for ($i = 0; $i < $this->numcols; $i++)
439 if (!isset($this->headers[$i]))
442 $header = $this->headers[$i];
443 if ($this->aligns[$i] == "right")
444 if ($this->headers2 == null)
445 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderRight);
447 $this->sheet->writeString($this->y, $i, $header, $this->formatBottomHeaderRight);
449 if ($this->headers2 == null)
450 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderLeft);
452 $this->sheet->writeString($this->y, $i, $header, $this->formatBottomHeaderLeft);
458 * Format a numeric string date into something nicer looking.
460 * @param string $date Date string to be formatted.
461 * @param int $input_format Format of the input string. Possible values are:<ul><li>0: user's default (default)</li></ul>
462 * @param int $output_format Format of the output string. Possible values are:<ul><li>0: Month (word) Day (numeric), 4-digit Year - Example: January 1, 2000 (default)</li><li>1: Month 4-digit Year - Example: January 2000</li><li>2: Month Abbreviation 4-digit Year - Example: Jan 2000</li></ul>
465 function DatePrettyPrint($date, $input_format = 0, $output_format = 0)
469 $date = date2sql($date);
470 $year = (int) (substr($date, 0, 4));
471 $month = (int) (substr($date, 5, 2));
472 $day = (int) (substr($date, 8, 2));
473 if ($output_format == 0)
474 return(date('F j, Y', mktime(12, 0, 0, $month, $day, $year)));
475 elseif ($output_format == 1)
476 return(date('F Y', mktime(12, 0, 0, $month, $day, $year)));
477 elseif ($output_format == 2)
478 return(date('M Y', mktime(12, 0, 0, $month, $day, $year)));
484 function AddImage($logo, $x, $y, $w, $h)
489 function SetDrawColor($r, $g, $b)
494 function SetTextColor($r, $g, $b)
499 function SetFillColor($r, $g, $b)
504 function GetCellPadding()
509 function SetCellPadding($pad)
514 function Text($c, $txt, $n=0, $corr=0, $r=0, $align='left', $border=0, $fill=0, $link=NULL, $stretch=0)
519 function TextWrap($xpos, $ypos, $len, $str, $align = 'left', $border = 0, $fill = 0, $link = NULL, $stretch = 0)
524 function TextCol($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
526 if ($this->aligns[$c] == 'right')
527 $this->sheet->writeString($this->y, $c, $txt, $this->formatRight);
529 $this->sheet->writeString($this->y, $c, $txt, $this->formatLeft);
531 $this->sheet->mergeCells($this->y, $c, $this->y, $n - 1);
534 function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0, $color_red=false)
536 if (!is_numeric($txt))
538 $this->sheet->writeNumber($this->y, $c, $txt, $this->NumFormat($dec));
541 function AmountCol2($c, $n, $txt, $dec=0, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0, $color_red=false, $amount_locale = NULL, $amount_format = NULL)
543 if (!is_numeric($txt))
545 $this->sheet->writeNumber($this->y, $c, $txt, $this->NumFormat($dec));
548 function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
551 $txt = date2sql($txt);
552 list($year, $mo, $day) = explode("-", $txt);
553 $date = $this->ymd2date((int)$year, (int)$mo, (int)$day);
554 $this->sheet->writeNumber($this->y, $c, $date, $this->formatDate);
557 function TextCol2($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
559 $this->sheet->writeString($this->y, $c, $txt, $this->formatLeft);
561 $this->sheet->mergeCells($this->y, $c, $this->y, $n - 1);
564 function TextColLines($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
569 function TextWrapLines($c, $width, $txt, $align='left', $border=0, $fill=0, $link=NULL, $stretch=0)
575 * Crude text wrap calculator based on PDF version.
577 function TextWrapCalc($txt, $width, $spacebreak=false)
579 // Assume an average character width
583 $w = strlen($txt) * $avg_char_width;
584 if ($w > $width && $w > 0 && $width != 0)
587 $k = intval($n * $width / $w);
588 if ($k > 0 && $k < $n)
590 $txt2 = substr($txt, 0, $k);
591 if ($spacebreak && (($pos = strrpos($txt2, " ")) !== false))
593 $txt2 = substr($txt2, 0, $pos);
594 $ret = substr($txt, $pos+1);
597 $ret = substr($txt, $k);
600 return array($txt2, $ret);
603 function SetLineStyle($style)
608 function SetLineWidth($width)
613 function LineTo($from, $row, $to, $row2)
618 function Line($row, $height = 0)
623 function UnderlineCell($c, $r = 0, $type = 1, $linewidth = 0, $style = array())
628 function NewLine($l=1, $np=0, $h=NULL)
633 function ymd2Date($year, $mon, $day) // XLS internal date representation is a number between 1900-01-01 and 2078-12-31
634 { // if we need the time part too, we have to add this value after a decimalpoint.
635 $mo = array(0,31,28,31,30,31,30,31,31,30,31,30,31);
638 if (($year % 4) == 0)
646 elseif ($day > $mo[$mon])
650 elseif ($year > $MAXYEAR)
653 for ($n = 1; $n < $mon; $n++)
657 for ($n = $BASE; $n < $year; $n++)
666 function px2units($px) // XLS app conversion. Not bulletproof.
668 $excel_column_width_factor = 256;
669 $unit_offset_length = $this->excelColWidthFactor;
670 return ($px / $unit_offset_length);
673 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
675 for ($i = 0; $i < $this->numcols; $i++)
676 $this->sheet->writeBlank($this->y, $i, $this->formatFooter);
677 $this->sheet->mergeCells($this->y, 0, $this->y, $this->numcols - 1);
679 // first have a look through the directory,
680 // and remove old temporary pdfs
681 if ($d = @opendir($this->path)) {
682 while (($file = readdir($d)) !== false) {
683 if (!is_file($this->path.'/'.$file) || $file == 'index.php') continue;
684 // then check to see if this one is too old
685 $ftime = filemtime($this->path.'/'.$file);
686 // seems 3 min is enough for any report download, isn't it?
687 if (time()-$ftime > 180){
688 unlink($this->path.'/'.$file);
693 meta_forward($_SERVER['PHP_SELF'], "xls=1&filename=$this->filename&unique=$this->unique_name");