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 $comp_path, $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 = $comp_path.'/'.user_company(). '/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]));
241 $tcol = $this->numcols - 1;
242 $this->sheet->setRow($this->y, 20);
243 for ($i = 0; $i < $this->numcols; $i++)
244 $this->sheet->writeBlank($this->y, $i, $this->formatTitle);
245 $this->sheet->writeString($this->y, 0, $this->title, $this->formatTitle);
246 $this->sheet->mergeCells($this->y, 0, $this->y, $tcol);
248 $str = _("Print Out Date") . ':';
249 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
250 $this->sheet->writeString($this->y, 1, Today() . " ".Now(), $this->formatLeft);
251 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
252 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
254 $str = _("Fiscal Year") . ':';
255 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
256 $str = $this->fiscal_year;
257 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
258 $this->sheet->writeString($this->y, $tcol-1, $this->host, $this->formatLeft);
259 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
260 for ($i = 1; $i < count($this->params); $i++)
262 if ($this->params[$i]['from'] != '')
265 $str = $this->params[$i]['text'] . ':';
266 $this->sheet->writeString($this->y, 0, $str);
267 $str = $this->params[$i]['from'];
268 if ($this->params[$i]['to'] != '')
269 $str .= " - " . $this->params[$i]['to'];
270 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
273 $this->sheet->writeString($this->y, $tcol-1, $this->user, $this->formatLeft);
274 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
278 if ($this->params[0] != '') // Comments
281 $str = _("Comments") . ':';
282 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
283 $this->sheet->writeString($this->y, 1, $this->params[0], $this->formatLeft);
286 if ($this->headers2 != null)
288 for ($i = 0, $j = 0; $i < $this->numcols; $i++)
290 if ($this->cols2[$j] >= $this->cols[$i] && $this->cols2[$j] <= $this->cols[$i + 1])
292 if ($this->aligns2[$j] == "right")
293 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatHeaderRight);
295 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatHeaderLeft);
299 $this->sheet->writeString($this->y, $i, "", $this->formatHeaderLeft);
304 for ($i = 0; $i < $this->numcols; $i++)
306 if (!isset($this->headers[$i]))
309 $header = $this->headers[$i];
310 if ($this->aligns[$i] == "right")
311 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderRight);
313 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderLeft);
318 function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype)
323 // Alternate header style - primary differences are for PDFs
326 // Flag to make sure we only print the company name once
327 $companyNamePrinted = false;
330 $tcol = $this->numcols - 1;
331 $this->sheet->setRow($this->y, 20);
333 for ($i = 0; $i < $this->numcols; $i++)
334 $this->sheet->writeBlank($this->y, $i, $this->formatTitle);
335 $this->sheet->writeString($this->y, 0, $this->title, $this->formatTitle);
336 $this->sheet->mergeCells($this->y, 0, $this->y, $tcol);
338 // Dimension 1 - optional
339 // - only print if available and not blank
340 if (count($this->params) > 3)
341 if ($this->params[3]['from'] != '')
344 $str = $this->params[3]['text'] . ':';
345 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
346 $this->sheet->writeString($this->y, 1, $this->params[3]['from'], $this->formatLeft);
347 // Company Name - at end of this row
348 if (!$companyNamePrinted)
350 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
351 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
352 $companyNamePrinted = true;
357 // Dimension 2 - optional
358 // - only print if available and not blank
359 if (count($this->params) > 4)
360 if ($this->params[4]['from'] != '')
363 $str = $this->params[4]['text'] . ':';
364 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
365 $this->sheet->writeString($this->y, 1, $this->params[4]['from'], $this->formatLeft);
366 // Company Name - at end of this row
367 if (!$companyNamePrinted)
369 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
370 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
371 $companyNamePrinted = true;
378 // Report Date - time period covered
379 // - can specify a range, or just the end date (and the report contents
380 // should make it obvious what the beginning date is)
382 $str = _("Report Date") . ':';
383 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
385 if ($this->params[1]['from'] != '')
386 $str = $this->params[1]['from'] . ' - ';
387 $str .= $this->params[1]['to'];
388 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
389 // Company Name - at end of this row
390 if (!$companyNamePrinted)
392 $this->sheet->writeString($this->y, $tcol-1, $this->company['coy_name'], $this->formatLeft);
393 $this->sheet->mergeCells($this->y, $tcol-1, $this->y, $tcol);
394 $companyNamePrinted = true;
397 // Timestamp of when this copy of the report was generated
399 $str = _("Generated At") . ':';
400 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
401 $this->sheet->writeString($this->y, 1, Today() . " ".Now(), $this->formatLeft);
403 // Name of the user that generated this copy of the report
405 $str = _("Generated By") . ':';
406 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
408 $this->sheet->writeString($this->y, 1, $str, $this->formatLeft);
410 // Comments - display any user-generated comments for this copy of the report
411 if ($this->params[0] != '')
414 $str = _("Comments") . ':';
415 $this->sheet->writeString($this->y, 0, $str, $this->formatLeft);
416 $this->sheet->writeString($this->y, 1, $this->params[0], $this->formatLeft);
420 if ($this->headers2 != null)
422 for ($i = 0, $j = 0; $i < $this->numcols; $i++)
424 if ($this->cols2[$j] >= $this->cols[$i] && $this->cols2[$j] <= $this->cols[$i + 1])
426 if ($this->aligns2[$j] == "right")
427 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatTopHeaderRight);
429 $this->sheet->writeString($this->y, $i, $this->headers2[$j], $this->formatTopHeaderLeft);
433 $this->sheet->writeString($this->y, $i, "", $this->formatTopHeaderLeft);
438 for ($i = 0; $i < $this->numcols; $i++)
440 if (!isset($this->headers[$i]))
443 $header = $this->headers[$i];
444 if ($this->aligns[$i] == "right")
445 if ($this->headers2 == null)
446 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderRight);
448 $this->sheet->writeString($this->y, $i, $header, $this->formatBottomHeaderRight);
450 if ($this->headers2 == null)
451 $this->sheet->writeString($this->y, $i, $header, $this->formatHeaderLeft);
453 $this->sheet->writeString($this->y, $i, $header, $this->formatBottomHeaderLeft);
459 * Format a numeric string date into something nicer looking.
461 * @param string $date Date string to be formatted.
462 * @param int $input_format Format of the input string. Possible values are:<ul><li>0: user's default (default)</li></ul>
463 * @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>
466 function DatePrettyPrint($date, $input_format = 0, $output_format = 0)
470 $date = date2sql($date);
471 $year = (int) (substr($date, 0, 4));
472 $month = (int) (substr($date, 5, 2));
473 $day = (int) (substr($date, 8, 2));
474 if ($output_format == 0)
475 return(date('F j, Y', mktime(12, 0, 0, $month, $day, $year)));
476 elseif ($output_format == 1)
477 return(date('F Y', mktime(12, 0, 0, $month, $day, $year)));
478 elseif ($output_format == 2)
479 return(date('M Y', mktime(12, 0, 0, $month, $day, $year)));
485 function AddImage($logo, $x, $y, $w, $h)
490 function SetDrawColor($r, $g, $b)
495 function SetTextColor($r, $g, $b)
500 function SetFillColor($r, $g, $b)
505 function GetCellPadding()
510 function SetCellPadding($pad)
515 function Text($c, $txt, $n=0, $corr=0, $r=0, $align='left', $border=0, $fill=0, $link=NULL, $stretch=0)
520 function TextWrap($xpos, $ypos, $len, $str, $align = 'left', $border = 0, $fill = 0, $link = NULL, $stretch = 0)
525 function TextCol($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
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 $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");