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]));
239 $tcol = $this->numcols - 1;
240 $this->sheet->setRow($this->y, 20);
241 for ($i = 0; $i < $this->numcols; $i++)
242 $this->sheet->writeBlank($this->y, $i, $this->formatTitle);
243 $this->sheet->writeString($this->y, 0, $this->title, $this->formatTitle);
244 $this->sheet->mergeCells($this->y, 0, $this->y, $tcol);
246 $str = _("Print Out Date") . ':';
247 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
248 $this->sheet->writeString($this->y, 1, Today() . " ".Now(), $this->formatLeft);
249 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
250 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
252 $str = _("Fiscal Year") . ':';
253 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
254 $str = $this->fiscal_year;
255 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
256 $this->sheet->writeString($this->y, $tcol-1, $this->host, $this->formatLeft);
257 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
258 for ($i = 1; $i < count($this->params); $i++)
260 if ($this->params[$i]['from'] != '')
263 $str = $this->params[$i]['text'] . ':';
264 $this->sheet->writeString($this->y, 0, $str);
265 $str = $this->params[$i]['from'];
266 if ($this->params[$i]['to'] != '')
267 $str .= " - " . $this->params[$i]['to'];
268 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
271 $this->sheet->writeString($this->y, $tcol-1, $this->user, $this->formatLeft);
272 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
276 if ($this->params[0] != '') // Comments
279 $str = _("Comments") . ':';
280 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
281 $this->sheet->writeString($this->y, 1, $this->params[0], $this->formatLeft);
284 if ($this->headers2 != null)
286 for ($i = 0, $j = 0; $i < $this->numcols; $i++)
288 if ($this->cols2[$j] >= $this->cols[$i] && $this->cols2[$j] <= $this->cols[$i + 1])
290 if ($this->aligns2[$j] == "right")
291 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatHeaderRight);
293 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatHeaderLeft);
297 $this->sheet->writeString($this->y, $i, "", $this->formatHeaderLeft);
302 for ($i = 0; $i < $this->numcols; $i++)
304 if (!isset($this->headers[$i]))
307 $header = $this->headers[$i];
308 if ($this->aligns[$i] == "right")
309 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderRight);
311 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderLeft);
316 function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
321 // Alternate header style - primary differences are for PDFs
324 // Flag to make sure we only print the company name once
325 $companyNamePrinted = false;
328 $tcol = $this->numcols - 1;
329 $this->sheet->setRow($this->y, 20);
331 for ($i = 0; $i < $this->numcols; $i++)
332 $this->sheet->writeBlank($this->y, $i, $this->formatTitle);
333 $this->sheet->writeString($this->y, 0, $this->title, $this->formatTitle);
334 $this->sheet->mergeCells($this->y, 0, $this->y, $tcol);
336 // Dimension 1 - optional
337 // - only print if available and not blank
338 if (count($this->params) > 3)
339 if ($this->params[3]['from'] != '')
342 $str = $this->params[3]['text'] . ':';
343 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
344 $this->sheet->writeString($this->y, 1, $this->params[3]['from'], $this->formatLeft);
345 // Company Name - at end of this row
346 if (!$companyNamePrinted)
348 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
349 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
350 $companyNamePrinted = true;
355 // Dimension 2 - optional
356 // - only print if available and not blank
357 if (count($this->params) > 4)
358 if ($this->params[4]['from'] != '')
361 $str = $this->params[4]['text'] . ':';
362 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
363 $this->sheet->writeString($this->y, 1, $this->params[4]['from'], $this->formatLeft);
364 // Company Name - at end of this row
365 if (!$companyNamePrinted)
367 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
368 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
369 $companyNamePrinted = true;
376 // Report Date - time period covered
377 // - can specify a range, or just the end date (and the report contents
378 // should make it obvious what the beginning date is)
380 $str = _("Report Date") . ':';
381 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
383 if ($this->params[1]['from'] != '')
384 $str = $this->params[1]['from'] . ' - ';
385 $str .= $this->params[1]['to'];
386 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
387 // Company Name - at end of this row
388 if (!$companyNamePrinted)
390 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
391 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
392 $companyNamePrinted = true;
395 // Timestamp of when this copy of the report was generated
397 $str = _("Generated At") . ':';
398 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
399 $this->sheet->writeString($this->y, 1, Today() . " ".Now(), $this->formatLeft);
401 // Name of the user that generated this copy of the report
403 $str = _("Generated By") . ':';
404 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
406 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
408 // Comments - display any user-generated comments for this copy of the report
409 if ($this->params[0] != '')
412 $str = _("Comments") . ':';
413 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
414 $this->sheet->writeString($this->y, 1, $this->params[0], $this->formatLeft);
418 if ($this->headers2 != null)
420 for ($i = 0, $j = 0; $i < $this->numcols; $i++)
422 if ($this->cols2[$j] >= $this->cols[$i] && $this->cols2[$j] <= $this->cols[$i + 1])
424 if ($this->aligns2[$j] == "right")
425 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatTopHeaderRight);
427 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatTopHeaderLeft);
431 $this->sheet->writeString($this->y, $i, "", $this->formatTopHeaderLeft);
436 for ($i = 0; $i < $this->numcols; $i++)
438 if (!isset($this->headers[$i]))
441 $header = $this->headers[$i];
442 if ($this->aligns[$i] == "right")
443 if ($this->headers2 == null)
444 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderRight);
446 $this->sheet->writeString($this->y, $i, $header, $this->formatBottomHeaderRight);
448 if ($this->headers2 == null)
449 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderLeft);
451 $this->sheet->writeString($this->y, $i, $header, $this->formatBottomHeaderLeft);
457 * Format a numeric string date into something nicer looking.
459 * @param string $date Date string to be formatted.
460 * @param int $input_format Format of the input string. Possible values are:<ul><li>0: user's default (default)</li></ul>
461 * @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>
464 function DatePrettyPrint($date, $input_format = 0, $output_format = 0)
468 $date = date2sql($date);
469 $year = (int) (substr($date, 0, 4));
470 $month = (int) (substr($date, 5, 2));
471 $day = (int) (substr($date, 8, 2));
472 if ($output_format == 0)
473 return(date('F j, Y', mktime(12, 0, 0, $month, $day, $year)));
474 elseif ($output_format == 1)
475 return(date('F Y', mktime(12, 0, 0, $month, $day, $year)));
476 elseif ($output_format == 2)
477 return(date('M Y', mktime(12, 0, 0, $month, $day, $year)));
483 function AddImage($logo, $x, $y, $w, $h)
488 function SetDrawColor($r, $g, $b)
493 function SetTextColor($r, $g, $b)
498 function SetFillColor($r, $g, $b)
503 function GetCellPadding()
508 function SetCellPadding($pad)
513 function Text($c, $txt, $n=0, $corr=0, $r=0, $align='left', $border=0, $fill=0, $link=NULL, $stretch=0)
518 function TextWrap($xpos, $ypos, $len, $str, $align = 'left', $border = 0, $fill = 0, $link = NULL, $stretch = 0)
523 function TextCol($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
525 $txt = htmlspecialchars_decode($txt);
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 $txt = htmlspecialchars_decode($txt);
560 $this->sheet->writeString($this->y, $c, $txt, $this->formatLeft);
562 $this->sheet->mergeCells($this->y, $c, $this->y, $n - 1);
565 function TextColLines($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
570 function TextWrapLines($c, $width, $txt, $align='left', $border=0, $fill=0, $link=NULL, $stretch=0)
576 * Crude text wrap calculator based on PDF version.
578 function TextWrapCalc($txt, $width, $spacebreak=false)
580 // Assume an average character width
584 $w = strlen($txt) * $avg_char_width;
585 if ($w > $width && $w > 0 && $width != 0)
588 $k = intval($n * $width / $w);
589 if ($k > 0 && $k < $n)
591 $txt2 = substr($txt, 0, $k);
592 if ($spacebreak && (($pos = strrpos($txt2, " ")) !== false))
594 $txt2 = substr($txt2, 0, $pos);
595 $ret = substr($txt, $pos+1);
598 $ret = substr($txt, $k);
601 return array($txt2, $ret);
604 function SetLineStyle($style)
609 function SetLineWidth($width)
614 function LineTo($from, $row, $to, $row2)
619 function Line($row, $height = 0)
624 function UnderlineCell($c, $r = 0, $type = 1, $linewidth = 0, $style = array())
629 function NewLine($l=1, $np=0, $h=NULL)
634 function ymd2Date($year, $mon, $day) // XLS internal date representation is a number between 1900-01-01 and 2078-12-31
635 { // if we need the time part too, we have to add this value after a decimalpoint.
636 $mo = array(0,31,28,31,30,31,30,31,31,30,31,30,31);
639 if (($year % 4) == 0)
647 elseif ($day > $mo[$mon])
651 elseif ($year > $MAXYEAR)
654 for ($n = 1; $n < $mon; $n++)
658 for ($n = $BASE; $n < $year; $n++)
667 function px2units($px) // XLS app conversion. Not bulletproof.
669 $excel_column_width_factor = 256;
670 $unit_offset_length = $this->excelColWidthFactor;
671 return ($px / $unit_offset_length);
674 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
676 for ($i = 0; $i < $this->numcols; $i++)
677 $this->sheet->writeBlank($this->y, $i, $this->formatFooter);
678 $this->sheet->mergeCells($this->y, 0, $this->y, $this->numcols - 1);
680 // first have a look through the directory,
681 // and remove old temporary pdfs
682 if ($d = @opendir($this->path)) {
683 while (($file = readdir($d)) !== false) {
684 if (!is_file($this->path.'/'.$file) || $file == 'index.php') continue;
685 // then check to see if this one is too old
686 $ftime = filemtime($this->path.'/'.$file);
687 // seems 3 min is enough for any report download, isn't it?
688 if (time()-$ftime > 180){
689 unlink($this->path.'/'.$file);
694 meta_forward($_SERVER['PHP_SELF'], "xls=1&filename=$this->filename&unique=$this->unique_name");