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();
60 function FrontReport($title, $filename, $size = 'A4', $fontsize = 9, $orientation = 'P', $margins = NULL, $excelColWidthFactor = 6.5)
62 global $dateseps, $page_security;
63 if (!$_SESSION["wa_current_user"]->can_access_page($page_security))
65 display_error(_("The security settings on your account do not permit you to print this report"));
70 $this->title = $title;
71 $this->lineHeight = 12;
73 $this->fontSize = $fontsize;
74 $this->oldFontSize = 0;
77 $this->excelColWidthFactor = $excelColWidthFactor;
78 $rtl = ($_SESSION['language']->dir == 'rtl');
79 $this->code = strtolower($_SESSION['language']->encoding);
80 $this->filename = $filename.".xls";
81 $this->unique_name = uniqid('').".xls";
82 $this->path = company_path(). '/pdf_files';
83 $this->Spreadsheet_Excel_Writer_Workbook($this->path."/".$this->unique_name);
84 //$this->setCountry(48);
85 if ($this->code != "iso-8859-1")
86 $this->setVersion(8); // set biff version to 8 (0x0006 internal)
87 $this->sheet =& $this->addWorksheet($this->worksheetNameGenerator($this->title));
88 if ($this->code != "iso-8859-1")
89 $this->sheet->setInputEncoding($this->code); // set sheet encoding
91 $this->sheet->setRTL();
92 $this->formatTitle =& $this->addFormat();
93 $this->formatTitle->setSize(16);
94 $this->formatTitle->setBold();
95 $this->formatTitle->setAlign($rtl ? 'right' : 'left');
96 $this->formatTitle->setTop(2);
97 $this->formatTitle->setTopColor('gray');
99 $how = user_date_format();
100 $sep = $dateseps[user_date_sep()];
105 $dateformat_long = "mm{$sep}dd{$sep}yyyy\ \ hh:mm\ am/pm";
106 $dateformat = "mm{$sep}dd{$sep}yyyy";
110 $dateformat_long = "dd{$sep}mm{$sep}yyyy\ \ hh:mm";
111 $dateformat = "dd{$sep}mm{$sep}yyyy";
115 $dateformat_long = "yyyy{$sep}mm{$sep}dd\ \ hh:mm";
116 $dateformat = "yyyy{$sep}mm{$sep}dd";
118 $this->formatDateTime =& $this->addFormat();
119 $this->formatDateTime->setNumFormat($dateformat_long);
120 $this->formatDateTime->setAlign($rtl ? 'right' : 'left');
121 $this->formatDate =& $this->addFormat();
122 $this->formatDate->setNumFormat($dateformat);
123 $this->formatDate->setAlign($rtl ? 'right' : 'left');
124 $this->formatRight =& $this->addFormat();
125 $this->formatRight->setAlign($rtl ? 'left' : 'right');
126 $this->formatLeft =& $this->addFormat();
127 $this->formatLeft->setAlign($rtl ? 'right' : 'left');
129 $this->formatHeaderLeft =& $this->addFormat();
130 $this->formatHeaderLeft->setItalic();
131 $this->formatHeaderLeft->setTop(2);
132 $this->formatHeaderLeft->setTopColor('gray');
133 $this->formatHeaderLeft->setBottom(2);
134 $this->formatHeaderLeft->setBottomColor('gray');
135 $this->formatHeaderLeft->setAlign('vcenter');
136 $this->formatTopHeaderLeft =& $this->addFormat();
137 $this->formatTopHeaderLeft->setItalic();
138 $this->formatTopHeaderLeft->setTop(2);
139 $this->formatTopHeaderLeft->setTopColor('gray');
140 $this->formatTopHeaderLeft->setAlign('vcenter');
141 $this->formatBottomHeaderLeft =& $this->addFormat();
142 $this->formatBottomHeaderLeft->setItalic();
143 $this->formatBottomHeaderLeft->setBottom(2);
144 $this->formatBottomHeaderLeft->setBottomColor('gray');
145 $this->formatBottomHeaderLeft->setAlign('vcenter');
146 $this->formatDate->setAlign($rtl ? 'right' : 'left');
147 $this->formatHeaderRight =& $this->addFormat();
148 $this->formatHeaderRight->setItalic();
149 $this->formatHeaderRight->setTop(2);
150 $this->formatHeaderRight->setTopColor('gray');
151 $this->formatHeaderRight->setBottom(2);
152 $this->formatHeaderRight->setBottomColor('gray');
153 $this->formatHeaderRight->setAlign('vcenter');
154 $this->formatHeaderRight->setAlign('right');
155 $this->formatTopHeaderRight =& $this->addFormat();
156 $this->formatTopHeaderRight->setItalic();
157 $this->formatTopHeaderRight->setTop(2);
158 $this->formatTopHeaderRight->setTopColor('gray');
159 $this->formatTopHeaderRight->setAlign('vcenter');
160 $this->formatTopHeaderRight->setAlign('right');
161 $this->formatBottomHeaderRight =& $this->addFormat();
162 $this->formatBottomHeaderRight->setItalic();
163 $this->formatBottomHeaderRight->setBottom(2);
164 $this->formatBottomHeaderRight->setBottomColor('gray');
165 $this->formatBottomHeaderRight->setAlign('vcenter');
166 $this->formatBottomHeaderRight->setAlign('right');
167 $this->formatFooter =& $this->addFormat();
168 $this->formatFooter->setTop(2);
169 $this->formatFooter->setTopColor('gray');
170 $this->SetHeaderType("header");
177 function SetHeaderType($name) {
178 $this->headerFunc = $name;
180 // Check a given name to see if it's a valid Excel worksheet name,
181 // and fix if necessary
182 function worksheetNameGenerator($name)
184 // First, strip out characters which aren't allowed
185 $illegal_chars = array(':', '\\', '/', '?', '*', '[', ']');
186 for ($i = 0; $i < count($illegal_chars); $i++)
187 $name = str_replace($illegal_chars[$i], '', $name);
188 // Now, if name is longer than 31 chars, truncate it
189 if (strlen($name) > 31)
190 $name = substr($name, 0, 31);
194 function NumFormat($dec)
196 if (!isset($this->formatAmount[$dec]))
198 //global $thoseps,$decseps;
200 //$tsep = $thoseps[user_tho_sep()];
201 //$dsep = $decseps[user_dec_sep()];
204 $format = "###{$tsep}###{$tsep}###{$tsep}##0";
206 $format .= "{$dsep}".str_repeat('0',$dec);
207 $this->formatAmount[$dec] =& $this->addFormat();
208 $this->formatAmount[$dec]->setNumFormat($format);
209 $this->formatAmount[$dec]->setAlign('right');
211 return $this->formatAmount[$dec];
214 function Font($fontname = '', $style = 'normal')
218 function Info($params, $cols, $headers, $aligns,
219 $cols2 = null, $headers2 = null, $aligns2 = null)
221 global $app_title, $version, $power_by, $power_url;
222 $this->company = get_company_prefs();
223 $year = get_current_fiscalyear();
224 if ($year['closed'] == 0)
228 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . " " . "(" . $how . ")";
229 $this->user = $_SESSION["wa_current_user"]->name;
230 $this->host = $_SERVER['SERVER_NAME'];
231 $this->params = $params;
233 $this->headers = $headers;
234 $this->aligns = $aligns;
235 $this->cols2 = $cols2;
236 $this->headers2 = $headers2;
237 $this->aligns2 = $aligns2;
238 $this->numcols = count($this->headers);
239 $tcols = count($this->headers2);
240 if ($tcols > $this->numcols)
241 $this->numcols = $tcols;
242 for ($i = 0; $i < $this->numcols; $i++)
243 $this->sheet->setColumn($i, $i, $this->px2units($this->cols[$i + 1] - $this->cols[$i]));
248 $tcol = $this->numcols - 1;
249 $this->sheet->setRow($this->y, 20);
250 for ($i = 0; $i < $this->numcols; $i++)
251 $this->sheet->writeBlank($this->y, $i, $this->formatTitle);
252 $this->sheet->writeString($this->y, 0, $this->title, $this->formatTitle);
253 $this->sheet->mergeCells($this->y, 0, $this->y, $tcol);
255 $str = _("Print Out Date") . ':';
256 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
257 $this->sheet->writeString($this->y, 1, Today() . " ".Now(), $this->formatLeft);
258 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
259 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
261 $str = _("Fiscal Year") . ':';
262 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
263 $str = $this->fiscal_year;
264 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
265 $this->sheet->writeString($this->y, $tcol-1, $this->host, $this->formatLeft);
266 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
267 for ($i = 1; $i < count($this->params); $i++)
269 if ($this->params[$i]['from'] != '')
272 $str = $this->params[$i]['text'] . ':';
273 $this->sheet->writeString($this->y, 0, $str);
274 $str = $this->params[$i]['from'];
275 if ($this->params[$i]['to'] != '')
276 $str .= " - " . $this->params[$i]['to'];
277 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
280 $this->sheet->writeString($this->y, $tcol-1, $this->user, $this->formatLeft);
281 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
285 if ($this->params[0] != '') // Comments
288 $str = _("Comments") . ':';
289 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
290 $this->sheet->writeString($this->y, 1, $this->params[0], $this->formatLeft);
293 if ($this->headers2 != null)
295 for ($i = 0, $j = 0; $i < $this->numcols; $i++)
297 if ($this->cols2[$j] >= $this->cols[$i] && $this->cols2[$j] <= $this->cols[$i + 1])
299 if ($this->aligns2[$j] == "right")
300 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatHeaderRight);
302 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatHeaderLeft);
306 $this->sheet->writeString($this->y, $i, "", $this->formatHeaderLeft);
311 for ($i = 0; $i < $this->numcols; $i++)
313 if (!isset($this->headers[$i]))
316 $header = $this->headers[$i];
317 if ($this->aligns[$i] == "right")
318 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderRight);
320 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderLeft);
325 function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
330 // Alternate header style - primary differences are for PDFs
333 // Flag to make sure we only print the company name once
334 $companyNamePrinted = false;
337 $tcol = $this->numcols - 1;
338 $this->sheet->setRow($this->y, 20);
340 for ($i = 0; $i < $this->numcols; $i++)
341 $this->sheet->writeBlank($this->y, $i, $this->formatTitle);
342 $this->sheet->writeString($this->y, 0, $this->title, $this->formatTitle);
343 $this->sheet->mergeCells($this->y, 0, $this->y, $tcol);
345 // Dimension 1 - optional
346 // - only print if available and not blank
347 if (count($this->params) > 3)
348 if ($this->params[3]['from'] != '')
351 $str = $this->params[3]['text'] . ':';
352 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
353 $this->sheet->writeString($this->y, 1, $this->params[3]['from'], $this->formatLeft);
354 // Company Name - at end of this row
355 if (!$companyNamePrinted)
357 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
358 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
359 $companyNamePrinted = true;
364 // Dimension 2 - optional
365 // - only print if available and not blank
366 if (count($this->params) > 4)
367 if ($this->params[4]['from'] != '')
370 $str = $this->params[4]['text'] . ':';
371 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
372 $this->sheet->writeString($this->y, 1, $this->params[4]['from'], $this->formatLeft);
373 // Company Name - at end of this row
374 if (!$companyNamePrinted)
376 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
377 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
378 $companyNamePrinted = true;
385 // Report Date - time period covered
386 // - can specify a range, or just the end date (and the report contents
387 // should make it obvious what the beginning date is)
389 $str = _("Report Date") . ':';
390 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
392 if ($this->params[1]['from'] != '')
393 $str = $this->params[1]['from'] . ' - ';
394 $str .= $this->params[1]['to'];
395 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
396 // Company Name - at end of this row
397 if (!$companyNamePrinted)
399 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
400 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
401 $companyNamePrinted = true;
404 // Timestamp of when this copy of the report was generated
406 $str = _("Generated At") . ':';
407 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
408 $this->sheet->writeString($this->y, 1, Today() . " ".Now(), $this->formatLeft);
410 // Name of the user that generated this copy of the report
412 $str = _("Generated By") . ':';
413 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
415 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
417 // Comments - display any user-generated comments for this copy of the report
418 if ($this->params[0] != '')
421 $str = _("Comments") . ':';
422 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
423 $this->sheet->writeString($this->y, 1, $this->params[0], $this->formatLeft);
427 if ($this->headers2 != null)
429 for ($i = 0, $j = 0; $i < $this->numcols; $i++)
431 if ($this->cols2[$j] >= $this->cols[$i] && $this->cols2[$j] <= $this->cols[$i + 1])
433 if ($this->aligns2[$j] == "right")
434 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatTopHeaderRight);
436 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatTopHeaderLeft);
440 $this->sheet->writeString($this->y, $i, "", $this->formatTopHeaderLeft);
445 for ($i = 0; $i < $this->numcols; $i++)
447 if (!isset($this->headers[$i]))
450 $header = $this->headers[$i];
451 if ($this->aligns[$i] == "right")
452 if ($this->headers2 == null)
453 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderRight);
455 $this->sheet->writeString($this->y, $i, $header, $this->formatBottomHeaderRight);
457 if ($this->headers2 == null)
458 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderLeft);
460 $this->sheet->writeString($this->y, $i, $header, $this->formatBottomHeaderLeft);
466 * Format a numeric string date into something nicer looking.
468 * @param string $date Date string to be formatted.
469 * @param int $input_format Format of the input string. Possible values are:<ul><li>0: user's default (default)</li></ul>
470 * @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>
473 function DatePrettyPrint($date, $input_format = 0, $output_format = 0)
477 $date = date2sql($date);
478 $year = (int) (substr($date, 0, 4));
479 $month = (int) (substr($date, 5, 2));
480 $day = (int) (substr($date, 8, 2));
481 if ($output_format == 0)
482 return(date('F j, Y', mktime(12, 0, 0, $month, $day, $year)));
483 elseif ($output_format == 1)
484 return(date('F Y', mktime(12, 0, 0, $month, $day, $year)));
485 elseif ($output_format == 2)
486 return(date('M Y', mktime(12, 0, 0, $month, $day, $year)));
492 function AddImage($logo, $x, $y, $w, $h)
497 function SetDrawColor($r, $g, $b)
502 function SetTextColor($r, $g, $b)
507 function SetFillColor($r, $g, $b)
512 function GetCellPadding()
517 function SetCellPadding($pad)
522 function Text($c, $txt, $n=0, $corr=0, $r=0, $align='left', $border=0, $fill=0, $link=NULL, $stretch=0)
527 function TextWrap($xpos, $ypos, $len, $str, $align = 'left', $border = 0, $fill = 0, $link = NULL, $stretch = 0)
532 function TextCol($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
534 $txt = html_entity_decode($txt);
535 if ($this->aligns[$c] == 'right')
536 $this->sheet->writeString($this->y, $c, $txt, $this->formatRight);
538 $this->sheet->writeString($this->y, $c, $txt, $this->formatLeft);
540 $this->sheet->mergeCells($this->y, $c, $this->y, $n - 1);
543 function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0, $color_red=false)
545 if (!is_numeric($txt))
547 $this->sheet->writeNumber($this->y, $c, $txt, $this->NumFormat($dec));
550 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)
552 if (!is_numeric($txt))
554 $this->sheet->writeNumber($this->y, $c, $txt, $this->NumFormat($dec));
557 function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
560 $txt = date2sql($txt);
561 list($year, $mo, $day) = explode("-", $txt);
562 $date = $this->ymd2date((int)$year, (int)$mo, (int)$day);
563 $this->sheet->writeNumber($this->y, $c, $date, $this->formatDate);
566 function TextCol2($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
568 $txt = html_entity_decode($txt);
569 $this->sheet->writeString($this->y, $c, $txt, $this->formatLeft);
571 $this->sheet->mergeCells($this->y, $c, $this->y, $n - 1);
574 function TextColLines($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
579 function TextWrapLines($c, $width, $txt, $align='left', $border=0, $fill=0, $link=NULL, $stretch=0)
585 * Crude text wrap calculator based on PDF version.
587 function TextWrapCalc($txt, $width, $spacebreak=false)
589 // Assume an average character width
593 $w = strlen($txt) * $avg_char_width;
594 if ($w > $width && $w > 0 && $width != 0)
597 $k = intval($n * $width / $w);
598 if ($k > 0 && $k < $n)
600 $txt2 = substr($txt, 0, $k);
601 if ($spacebreak && (($pos = strrpos($txt2, " ")) !== false))
603 $txt2 = substr($txt2, 0, $pos);
604 $ret = substr($txt, $pos+1);
607 $ret = substr($txt, $k);
610 return array($txt2, $ret);
613 function SetLineStyle($style)
618 function SetLineWidth($width)
623 function LineTo($from, $row, $to, $row2)
628 function Line($row, $height = 0)
633 function UnderlineCell($c, $r = 0, $type = 1, $linewidth = 0, $style = array())
638 function NewLine($l=1, $np=0, $h=NULL)
645 if (method_exists($this, $this->headerFunc)) // draw predefined page layout if any
646 $this->{$this->headerFunc}();
649 function ymd2Date($year, $mon, $day) // XLS internal date representation is a number between 1900-01-01 and 2078-12-31
650 { // if we need the time part too, we have to add this value after a decimalpoint.
651 $mo = array(0,31,28,31,30,31,30,31,31,30,31,30,31);
654 if (($year % 4) == 0)
662 elseif ($day > $mo[$mon])
666 elseif ($year > $MAXYEAR)
669 for ($n = 1; $n < $mon; $n++)
673 for ($n = $BASE; $n < $year; $n++)
682 function px2units($px) // XLS app conversion. Not bulletproof.
684 $excel_column_width_factor = 256;
685 $unit_offset_length = $this->excelColWidthFactor;
686 return ($px / $unit_offset_length);
689 function End($email=0, $subject=null, $myrow=null, $doctype = 0)
692 for ($i = 0; $i < $this->numcols; $i++)
693 $this->sheet->writeBlank($this->y, $i, $this->formatFooter);
694 $this->sheet->mergeCells($this->y, 0, $this->y, $this->numcols - 1);
696 // first have a look through the directory,
697 // and remove old temporary pdfs
698 if ($d = @opendir($this->path)) {
699 while (($file = readdir($d)) !== false) {
700 if (!is_file($this->path.'/'.$file) || $file == 'index.php') continue;
701 // then check to see if this one is too old
702 $ftime = filemtime($this->path.'/'.$file);
703 // seems 3 min is enough for any report download, isn't it?
704 if (time()-$ftime > 180){
705 unlink($this->path.'/'.$file);
710 meta_forward($_SERVER['PHP_SELF'], "xls=1&filename=$this->filename&unique=$this->unique_name");