Fixed more rows when reporting to Excel Reports. Greater then 65535 rows.
[fa-stable.git] / reporting / includes / excel_report.inc
index aca0e2c7bfa7f004ca4ff1c1e39619e75090091e..3786a91aaf9d92bcba4522a2096473deac9dc390 100644 (file)
@@ -13,6 +13,7 @@ include_once($path_to_root . "/reporting/includes/Workbook.php");
 include_once($path_to_root . "/admin/db/company_db.inc");
 include_once($path_to_root . "/admin/db/fiscalyears_db.inc");
 include_once($path_to_root . "/config.php");
+define('MAX_ROW_SHEET', 65530);
 // xls version
 class FrontReport extends Spreadsheet_Excel_Writer_Workbook
 {
@@ -29,6 +30,8 @@ class FrontReport extends Spreadsheet_Excel_Writer_Workbook
        var $bottomMargin = 0;
        var $lineHeight;
        var $leftMargin = 0;
+       var $pageWidth;         // dummy
+       var $rightMargin;       // dummy
 
        var $cols;
        var $params;
@@ -53,12 +56,14 @@ class FrontReport extends Spreadsheet_Excel_Writer_Workbook
        var $formatHeaderRight;
        var $formatFooter;
        var $formatAmount = array();
+       var $headerFunc;
        
        var $sheet;
 
        function FrontReport($title, $filename, $size = 'A4', $fontsize = 9, $orientation = 'P', $margins = NULL, $excelColWidthFactor = 6.5)
        {
-               global $comp_path, $dateseps, $page_security;
+               global $SysPrefs, $page_security;
+
                if (!$_SESSION["wa_current_user"]->can_access_page($page_security))
                {
                        display_error(_("The security settings on your account do not permit you to print this report"));
@@ -69,21 +74,20 @@ class FrontReport extends Spreadsheet_Excel_Writer_Workbook
                $this->title = $title;
                $this->lineHeight = 12;
                $this->endLine = 760;
-               $this->fontSize = $fontsize;
+               $this->SetFontSize($fontsize);
                $this->oldFontSize = 0;
-               $this->y = 1;
+               $this->y = 0;
                $this->currency = '';
                $this->excelColWidthFactor = $excelColWidthFactor;
                $rtl = ($_SESSION['language']->dir == 'rtl');
                $this->code = strtolower($_SESSION['language']->encoding);
                $this->filename = $filename.".xls";
-               $this->unique_name = uniqid('').".xls";
-               $this->path = $comp_path.'/'.user_company(). '/pdf_files';
+               $this->unique_name = random_id().".xls";
+               $this->path = company_path(). '/pdf_files';
                $this->Spreadsheet_Excel_Writer_Workbook($this->path."/".$this->unique_name);
-               //$this->setCountry(48);
                if ($this->code != "iso-8859-1")
                        $this->setVersion(8); // set biff version to 8 (0x0006 internal)
-               $this->sheet =& $this->addWorksheet($this->worksheetNameGenerator($this->title));
+               $this->sheet = $this->addWorksheet($this->worksheetNameGenerator($this->title));
                if ($this->code != "iso-8859-1")
                        $this->sheet->setInputEncoding($this->code); // set sheet encoding
                if ($rtl)
@@ -96,7 +100,7 @@ class FrontReport extends Spreadsheet_Excel_Writer_Workbook
                $this->formatTitle->setTopColor('gray');
 
                $how = user_date_format();
-               $sep = $dateseps[user_date_sep()];
+               $sep = $SysPrefs->dateseps[user_date_sep()];
                if ($sep == '.')
                        $sep = "\\.";
                if ($how == 0)
@@ -166,9 +170,16 @@ class FrontReport extends Spreadsheet_Excel_Writer_Workbook
                $this->formatFooter =& $this->addFormat();
                $this->formatFooter->setTop(2);
                $this->formatFooter->setTopColor('gray');
+               $this->SetHeaderType("header");
        }
        
 
+       /*
+               Set header handler
+       */
+       function SetHeaderType($name) {
+               $this->headerFunc = $name;
+       }
        // Check a given name to see if it's a valid Excel worksheet name,
        // and fix if necessary
        function worksheetNameGenerator($name)
@@ -187,10 +198,7 @@ class FrontReport extends Spreadsheet_Excel_Writer_Workbook
        {
                if (!isset($this->formatAmount[$dec]))
                {
-                       //global $thoseps,$decseps;
                        $dec = (int)$dec;
-                       //$tsep = $thoseps[user_tho_sep()];
-                       //$dsep = $decseps[user_dec_sep()];
                        $tsep = ',';
                        $dsep = '.';
                        $format = "###{$tsep}###{$tsep}###{$tsep}##0";
@@ -206,11 +214,15 @@ class FrontReport extends Spreadsheet_Excel_Writer_Workbook
        function Font($fontname = '', $style = 'normal')
        {
        }
+       
+       function SetFontSize($size)
+       {
+               $this->fontSize = $size;
+       }
 
        function Info($params, $cols, $headers, $aligns,
                $cols2 = null, $headers2 = null, $aligns2 = null)
        {
-               global $app_title, $version, $power_by, $power_url;
                $this->company = get_company_prefs();
                $year = get_current_fiscalyear();
                if ($year['closed'] == 0)
@@ -237,7 +249,6 @@ class FrontReport extends Spreadsheet_Excel_Writer_Workbook
 
        function Header()
        {
-               $this->y = 0;
                $tcol = $this->numcols - 1;
                $this->sheet->setRow($this->y, 20);
                for ($i = 0; $i < $this->numcols; $i++)
@@ -524,6 +535,7 @@ class FrontReport extends Spreadsheet_Excel_Writer_Workbook
 
        function TextCol($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
        {
+               $txt = html_entity_decode($txt);        
                if ($this->aligns[$c] == 'right')
                        $this->sheet->writeString($this->y, $c, $txt, $this->formatRight);
                else    
@@ -550,13 +562,14 @@ class FrontReport extends Spreadsheet_Excel_Writer_Workbook
        {
                if (!$conv)
                        $txt = date2sql($txt);
-               list($year, $mo, $day) = explode("-", $txt);    
+               list($year, $mo, $day) = explode("-", $txt);
                $date = $this->ymd2date((int)$year, (int)$mo, (int)$day);
                $this->sheet->writeNumber($this->y, $c, $date, $this->formatDate);
        }
 
        function TextCol2($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
        {
+               $txt = html_entity_decode($txt);        
                $this->sheet->writeString($this->y, $c, $txt, $this->formatLeft);
                if ($n - $c > 1)
                        $this->sheet->mergeCells($this->y, $c, $this->y, $n - 1);
@@ -629,11 +642,23 @@ class FrontReport extends Spreadsheet_Excel_Writer_Workbook
        function NewLine($l=1, $np=0, $h=NULL)
        {
                $this->y += $l;
+               if ($this->y > MAX_ROW_SHEET)
+               {
+                       $this->y = 0;
+                       $this->sheet = $this->addWorksheet();
+                       if ($this->code != "iso-8859-1")
+                               $this->sheet->setInputEncoding($this->code); // set sheet encoding
+                       if ($_SESSION['language']->dir == 'rtl')
+                               $this->sheet->setRTL();
+                       for ($i = 0; $i < $this->numcols; $i++)
+                               $this->sheet->setColumn($i, $i, $this->px2units($this->cols[$i + 1] - $this->cols[$i]));
+               }
        }
        
        function NewPage()
        {
-               return;
+               if (method_exists($this, $this->headerFunc))    // draw predefined page layout if any
+                       $this->{$this->headerFunc}();
        }
        
        function ymd2Date($year, $mon, $day) // XLS internal date representation is a number between 1900-01-01 and 2078-12-31
@@ -676,8 +701,10 @@ class FrontReport extends Spreadsheet_Excel_Writer_Workbook
                return ($px / $unit_offset_length);
        }       
 
-       function End($email=0, $subject=null, $myrow=null, $doctype = 0)
+       function End($email=0, $subject='')
        {
+               global $path_to_root;
+               ++$this->y;
                for ($i = 0; $i < $this->numcols; $i++)
                        $this->sheet->writeBlank($this->y, $i, $this->formatFooter);
                $this->sheet->mergeCells($this->y, 0, $this->y, $this->numcols - 1);
@@ -696,9 +723,9 @@ class FrontReport extends Spreadsheet_Excel_Writer_Workbook
                        }
                        closedir($d);
                }
-               meta_forward($_SERVER['PHP_SELF'], "xls=1&filename=$this->filename&unique=$this->unique_name");
+
+               meta_forward($path_to_root.'/reporting/prn_redirect.php', "xls=1&filename=$this->filename&unique=$this->unique_name");
                exit();
        }
 }
 
-?>
\ No newline at end of file