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 . "/admin/db/fiscalyears_db.inc");
15 include_once($path_to_root . "/config.php");
17 class FrontReport extends Spreadsheet_Excel_Writer_Workbook
29 var $bottomMargin = 0;
46 var $excelColWidthFactor;
52 var $formatHeaderLeft;
53 var $formatHeaderRight;
55 var $formatAmount = array();
59 function FrontReport($title, $filename, $size = 'A4', $fontsize = 9, $orientation = 'P', $margins = NULL, $excelColWidthFactor = 6.5)
61 global $dateseps, $page_security;
62 if (!$_SESSION["wa_current_user"]->can_access_page($page_security))
64 display_error(_("The security settings on your account do not permit you to print this report"));
69 $this->title = $title;
70 $this->lineHeight = 12;
72 $this->fontSize = $fontsize;
73 $this->oldFontSize = 0;
76 $this->excelColWidthFactor = $excelColWidthFactor;
77 $rtl = ($_SESSION['language']->dir == 'rtl');
78 $this->code = strtolower($_SESSION['language']->encoding);
79 $this->filename = $filename.".xls";
80 $this->unique_name = uniqid('').".xls";
81 $this->path = company_path(). '/pdf_files';
82 $this->Spreadsheet_Excel_Writer_Workbook($this->path."/".$this->unique_name);
83 //$this->setCountry(48);
84 if ($this->code != "iso-8859-1")
85 $this->setVersion(8); // set biff version to 8 (0x0006 internal)
86 $this->sheet =& $this->addWorksheet($this->worksheetNameGenerator($this->title));
87 if ($this->code != "iso-8859-1")
88 $this->sheet->setInputEncoding($this->code); // set sheet encoding
90 $this->sheet->setRTL();
91 $this->formatTitle =& $this->addFormat();
92 $this->formatTitle->setSize(16);
93 $this->formatTitle->setBold();
94 $this->formatTitle->setAlign($rtl ? 'right' : 'left');
95 $this->formatTitle->setTop(2);
96 $this->formatTitle->setTopColor('gray');
98 $how = user_date_format();
99 $sep = $dateseps[user_date_sep()];
104 $dateformat_long = "mm{$sep}dd{$sep}yyyy\ \ hh:mm\ am/pm";
105 $dateformat = "mm{$sep}dd{$sep}yyyy";
109 $dateformat_long = "dd{$sep}mm{$sep}yyyy\ \ hh:mm";
110 $dateformat = "dd{$sep}mm{$sep}yyyy";
114 $dateformat_long = "yyyy{$sep}mm{$sep}dd\ \ hh:mm";
115 $dateformat = "yyyy{$sep}mm{$sep}dd";
117 $this->formatDateTime =& $this->addFormat();
118 $this->formatDateTime->setNumFormat($dateformat_long);
119 $this->formatDateTime->setAlign($rtl ? 'right' : 'left');
120 $this->formatDate =& $this->addFormat();
121 $this->formatDate->setNumFormat($dateformat);
122 $this->formatDate->setAlign($rtl ? 'right' : 'left');
123 $this->formatRight =& $this->addFormat();
124 $this->formatRight->setAlign($rtl ? 'left' : 'right');
125 $this->formatLeft =& $this->addFormat();
126 $this->formatLeft->setAlign($rtl ? 'right' : 'left');
128 $this->formatHeaderLeft =& $this->addFormat();
129 $this->formatHeaderLeft->setItalic();
130 $this->formatHeaderLeft->setTop(2);
131 $this->formatHeaderLeft->setTopColor('gray');
132 $this->formatHeaderLeft->setBottom(2);
133 $this->formatHeaderLeft->setBottomColor('gray');
134 $this->formatHeaderLeft->setAlign('vcenter');
135 $this->formatTopHeaderLeft =& $this->addFormat();
136 $this->formatTopHeaderLeft->setItalic();
137 $this->formatTopHeaderLeft->setTop(2);
138 $this->formatTopHeaderLeft->setTopColor('gray');
139 $this->formatTopHeaderLeft->setAlign('vcenter');
140 $this->formatBottomHeaderLeft =& $this->addFormat();
141 $this->formatBottomHeaderLeft->setItalic();
142 $this->formatBottomHeaderLeft->setBottom(2);
143 $this->formatBottomHeaderLeft->setBottomColor('gray');
144 $this->formatBottomHeaderLeft->setAlign('vcenter');
145 $this->formatDate->setAlign($rtl ? 'right' : 'left');
146 $this->formatHeaderRight =& $this->addFormat();
147 $this->formatHeaderRight->setItalic();
148 $this->formatHeaderRight->setTop(2);
149 $this->formatHeaderRight->setTopColor('gray');
150 $this->formatHeaderRight->setBottom(2);
151 $this->formatHeaderRight->setBottomColor('gray');
152 $this->formatHeaderRight->setAlign('vcenter');
153 $this->formatHeaderRight->setAlign('right');
154 $this->formatTopHeaderRight =& $this->addFormat();
155 $this->formatTopHeaderRight->setItalic();
156 $this->formatTopHeaderRight->setTop(2);
157 $this->formatTopHeaderRight->setTopColor('gray');
158 $this->formatTopHeaderRight->setAlign('vcenter');
159 $this->formatTopHeaderRight->setAlign('right');
160 $this->formatBottomHeaderRight =& $this->addFormat();
161 $this->formatBottomHeaderRight->setItalic();
162 $this->formatBottomHeaderRight->setBottom(2);
163 $this->formatBottomHeaderRight->setBottomColor('gray');
164 $this->formatBottomHeaderRight->setAlign('vcenter');
165 $this->formatBottomHeaderRight->setAlign('right');
166 $this->formatFooter =& $this->addFormat();
167 $this->formatFooter->setTop(2);
168 $this->formatFooter->setTopColor('gray');
172 // Check a given name to see if it's a valid Excel worksheet name,
173 // and fix if necessary
174 function worksheetNameGenerator($name)
176 // First, strip out characters which aren't allowed
177 $illegal_chars = array(':', '\\', '/', '?', '*', '[', ']');
178 for ($i = 0; $i < count($illegal_chars); $i++)
179 $name = str_replace($illegal_chars[$i], '', $name);
180 // Now, if name is longer than 31 chars, truncate it
181 if (strlen($name) > 31)
182 $name = substr($name, 0, 31);
186 function NumFormat($dec)
188 if (!isset($this->formatAmount[$dec]))
190 //global $thoseps,$decseps;
192 //$tsep = $thoseps[user_tho_sep()];
193 //$dsep = $decseps[user_dec_sep()];
196 $format = "###{$tsep}###{$tsep}###{$tsep}##0";
198 $format .= "{$dsep}".str_repeat('0',$dec);
199 $this->formatAmount[$dec] =& $this->addFormat();
200 $this->formatAmount[$dec]->setNumFormat($format);
201 $this->formatAmount[$dec]->setAlign('right');
203 return $this->formatAmount[$dec];
206 function Font($fontname = '', $style = 'normal')
210 function Info($params, $cols, $headers, $aligns,
211 $cols2 = null, $headers2 = null, $aligns2 = null)
213 global $app_title, $version, $power_by, $power_url;
214 $this->company = get_company_prefs();
215 $year = get_current_fiscalyear();
216 if ($year['closed'] == 0)
220 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . " " . "(" . $how . ")";
221 $this->user = $_SESSION["wa_current_user"]->name;
222 $this->host = $_SERVER['SERVER_NAME'];
223 $this->params = $params;
225 $this->headers = $headers;
226 $this->aligns = $aligns;
227 $this->cols2 = $cols2;
228 $this->headers2 = $headers2;
229 $this->aligns2 = $aligns2;
230 $this->numcols = count($this->headers);
231 $tcols = count($this->headers2);
232 if ($tcols > $this->numcols)
233 $this->numcols = $tcols;
234 for ($i = 0; $i < $this->numcols; $i++)
235 $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 $txt = html_entity_decode($txt);
527 if ($this->aligns[$c] == 'right')
528 $this->sheet->writeString($this->y, $c, $txt, $this->formatRight);
530 $this->sheet->writeString($this->y, $c, $txt, $this->formatLeft);
532 $this->sheet->mergeCells($this->y, $c, $this->y, $n - 1);
535 function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0, $color_red=false)
537 if (!is_numeric($txt))
539 $this->sheet->writeNumber($this->y, $c, $txt, $this->NumFormat($dec));
542 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)
544 if (!is_numeric($txt))
546 $this->sheet->writeNumber($this->y, $c, $txt, $this->NumFormat($dec));
549 function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
552 $txt = date2sql($txt);
553 list($year, $mo, $day) = explode("-", $txt);
554 $date = $this->ymd2date((int)$year, (int)$mo, (int)$day);
555 $this->sheet->writeNumber($this->y, $c, $date, $this->formatDate);
558 function TextCol2($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
560 $txt = html_entity_decode($txt);
561 $this->sheet->writeString($this->y, $c, $txt, $this->formatLeft);
563 $this->sheet->mergeCells($this->y, $c, $this->y, $n - 1);
566 function TextColLines($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
571 function TextWrapLines($c, $width, $txt, $align='left', $border=0, $fill=0, $link=NULL, $stretch=0)
577 * Crude text wrap calculator based on PDF version.
579 function TextWrapCalc($txt, $width, $spacebreak=false)
581 // Assume an average character width
585 $w = strlen($txt) * $avg_char_width;
586 if ($w > $width && $w > 0 && $width != 0)
589 $k = intval($n * $width / $w);
590 if ($k > 0 && $k < $n)
592 $txt2 = substr($txt, 0, $k);
593 if ($spacebreak && (($pos = strrpos($txt2, " ")) !== false))
595 $txt2 = substr($txt2, 0, $pos);
596 $ret = substr($txt, $pos+1);
599 $ret = substr($txt, $k);
602 return array($txt2, $ret);
605 function SetLineStyle($style)
610 function SetLineWidth($width)
615 function LineTo($from, $row, $to, $row2)
620 function Line($row, $height = 0)
625 function UnderlineCell($c, $r = 0, $type = 1, $linewidth = 0, $style = array())
630 function NewLine($l=1, $np=0, $h=NULL)
640 function ymd2Date($year, $mon, $day) // XLS internal date representation is a number between 1900-01-01 and 2078-12-31
641 { // if we need the time part too, we have to add this value after a decimalpoint.
642 $mo = array(0,31,28,31,30,31,30,31,31,30,31,30,31);
645 if (($year % 4) == 0)
653 elseif ($day > $mo[$mon])
657 elseif ($year > $MAXYEAR)
660 for ($n = 1; $n < $mon; $n++)
664 for ($n = $BASE; $n < $year; $n++)
673 function px2units($px) // XLS app conversion. Not bulletproof.
675 $excel_column_width_factor = 256;
676 $unit_offset_length = $this->excelColWidthFactor;
677 return ($px / $unit_offset_length);
680 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
682 for ($i = 0; $i < $this->numcols; $i++)
683 $this->sheet->writeBlank($this->y, $i, $this->formatFooter);
684 $this->sheet->mergeCells($this->y, 0, $this->y, $this->numcols - 1);
686 // first have a look through the directory,
687 // and remove old temporary pdfs
688 if ($d = @opendir($this->path)) {
689 while (($file = readdir($d)) !== false) {
690 if (!is_file($this->path.'/'.$file) || $file == 'index.php') continue;
691 // then check to see if this one is too old
692 $ftime = filemtime($this->path.'/'.$file);
693 // seems 3 min is enough for any report download, isn't it?
694 if (time()-$ftime > 180){
695 unlink($this->path.'/'.$file);
700 meta_forward($_SERVER['PHP_SELF'], "xls=1&filename=$this->filename&unique=$this->unique_name");