Merged changes from main CVS up to 2.1.5
[fa-stable.git] / reporting / rep705.php
index c26a1ebb15ff6d795edc74546fe5de7cb88384fc..a9ca35ea20d92f124668b15a7280ddb376ac8741 100644 (file)
@@ -1,13 +1,13 @@
 <?php
 /**********************************************************************
     Copyright (C) FrontAccounting, LLC.
-       Released under the terms of the GNU Affero General Public License,
-       AGPL, as published by the Free Software Foundation, either version 
-       of the License, or (at your option) any later version.
+       Released under the terms of the GNU General Public License, GPL, 
+       as published by the Free Software Foundation, either version 3 
+       of the License, or (at your option) any later version.
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
-    See the License here <http://www.gnu.org/licenses/agpl-3.0.html>.
+    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
 ***********************************************************************/
 $page_security = 2;
 // ----------------------------------------------------------------
@@ -25,16 +25,12 @@ include_once($path_to_root . "/gl/includes/gl_db.inc");
 
 //----------------------------------------------------------------------------------------------------
 
-// trial_inquiry_controls();
 print_annual_expense_breakdown();
 
 //----------------------------------------------------------------------------------------------------
 
 function getPeriods($year, $account, $dimension, $dimension2)
 {
-       //$yr = date('Y');
-       //$mo = date('m'):
-       // from now
        $yr = $year;
        $mo = 12;
        $date13 = date('Y-m-d',mktime(0,0,0,$mo+1,1,$yr));
@@ -51,18 +47,18 @@ function getPeriods($year, $account, $dimension, $dimension2)
        $date02 = date('Y-m-d',mktime(0,0,0,$mo-10,1,$yr));
        $date01 = date('Y-m-d',mktime(0,0,0,$mo-11,1,$yr));
 
-    $sql = "SELECT SUM(CASE WHEN tran_date >= '$date01' AND tran_date < '$date02' THEN -amount / 1000 ELSE 0 END) AS per01,
-                               SUM(CASE WHEN tran_date >= '$date02' AND tran_date < '$date03' THEN -amount / 1000 ELSE 0 END) AS per02,
-                               SUM(CASE WHEN tran_date >= '$date03' AND tran_date < '$date04' THEN -amount / 1000 ELSE 0 END) AS per03,
-                               SUM(CASE WHEN tran_date >= '$date04' AND tran_date < '$date05' THEN -amount / 1000 ELSE 0 END) AS per04,
-                               SUM(CASE WHEN tran_date >= '$date05' AND tran_date < '$date06' THEN -amount / 1000 ELSE 0 END) AS per05,
-                               SUM(CASE WHEN tran_date >= '$date06' AND tran_date < '$date07' THEN -amount / 1000 ELSE 0 END) AS per06,
-                               SUM(CASE WHEN tran_date >= '$date07' AND tran_date < '$date08' THEN -amount / 1000 ELSE 0 END) AS per07,
-                               SUM(CASE WHEN tran_date >= '$date08' AND tran_date < '$date09' THEN -amount / 1000 ELSE 0 END) AS per08,
-                               SUM(CASE WHEN tran_date >= '$date09' AND tran_date < '$date10' THEN -amount / 1000 ELSE 0 END) AS per09,
-                               SUM(CASE WHEN tran_date >= '$date10' AND tran_date < '$date11' THEN -amount / 1000 ELSE 0 END) AS per10,
-                               SUM(CASE WHEN tran_date >= '$date11' AND tran_date < '$date12' THEN -amount / 1000 ELSE 0 END) AS per11,
-                               SUM(CASE WHEN tran_date >= '$date12' AND tran_date < '$date13' THEN -amount / 1000 ELSE 0 END) AS per12
+    $sql = "SELECT SUM(CASE WHEN tran_date >= '$date01' AND tran_date < '$date02' THEN amount / 1000 ELSE 0 END) AS per01,
+                               SUM(CASE WHEN tran_date >= '$date02' AND tran_date < '$date03' THEN amount / 1000 ELSE 0 END) AS per02,
+                               SUM(CASE WHEN tran_date >= '$date03' AND tran_date < '$date04' THEN amount / 1000 ELSE 0 END) AS per03,
+                               SUM(CASE WHEN tran_date >= '$date04' AND tran_date < '$date05' THEN amount / 1000 ELSE 0 END) AS per04,
+                               SUM(CASE WHEN tran_date >= '$date05' AND tran_date < '$date06' THEN amount / 1000 ELSE 0 END) AS per05,
+                               SUM(CASE WHEN tran_date >= '$date06' AND tran_date < '$date07' THEN amount / 1000 ELSE 0 END) AS per06,
+                               SUM(CASE WHEN tran_date >= '$date07' AND tran_date < '$date08' THEN amount / 1000 ELSE 0 END) AS per07,
+                               SUM(CASE WHEN tran_date >= '$date08' AND tran_date < '$date09' THEN amount / 1000 ELSE 0 END) AS per08,
+                               SUM(CASE WHEN tran_date >= '$date09' AND tran_date < '$date10' THEN amount / 1000 ELSE 0 END) AS per09,
+                               SUM(CASE WHEN tran_date >= '$date10' AND tran_date < '$date11' THEN amount / 1000 ELSE 0 END) AS per10,
+                               SUM(CASE WHEN tran_date >= '$date11' AND tran_date < '$date12' THEN amount / 1000 ELSE 0 END) AS per11,
+                               SUM(CASE WHEN tran_date >= '$date12' AND tran_date < '$date13' THEN amount / 1000 ELSE 0 END) AS per12
                        FROM ".TB_PREF."gl_trans
                                WHERE account='$account'";
        if ($dimension > 0)
@@ -81,7 +77,6 @@ function print_annual_expense_breakdown()
 {
        global $path_to_root, $date_system;
 
-       include_once($path_to_root . "/reporting/includes/pdf_report.inc");
        $dim = get_company_pref('use_dimension');
        $dimension = $dimension2 = 0;
 
@@ -91,18 +86,26 @@ function print_annual_expense_breakdown()
                $dimension = $_POST['PARAM_1'];
                $dimension2 = $_POST['PARAM_2'];
                $comments = $_POST['PARAM_3'];
+               $destination = $_POST['PARAM_4'];
        }
        else if ($dim == 1)
        {
                $year = $_POST['PARAM_0'];
                $dimension = $_POST['PARAM_1'];
                $comments = $_POST['PARAM_2'];
+               $destination = $_POST['PARAM_3'];
        }
        else
        {
                $year = $_POST['PARAM_0'];
                $comments = $_POST['PARAM_1'];
+               $destination = $_POST['PARAM_2'];
        }
+       if ($destination)
+               include_once($path_to_root . "/reporting/includes/excel_report.inc");
+       else
+               include_once($path_to_root . "/reporting/includes/pdf_report.inc");
+
        $dec = 1;
        //$pdec = user_percent_dec();
 
@@ -169,30 +172,51 @@ function print_annual_expense_breakdown()
                                'to' => ''));
     }
 
-       $rep = new FrontReport(_('Annual Expense Breakdown'), "AnnualBreakDown.pdf", user_pagesize());
+       $rep = new FrontReport(_('Annual Expense Breakdown'), "AnnualBreakDown", user_pagesize());
 
        $rep->Font();
        $rep->Info($params, $cols, $headers, $aligns);
        $rep->Header();
 
        $classname = '';
-       $group = '';
-       $total = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
+       $total = Array(
+               0 => Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
+                       Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
+                       Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
+                       Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
+                       Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
+                       Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
+                       Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
+                       Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
+                       Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0),
+                       Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0));
        $total2 = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
        $sales = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
        $calc = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
+       $typename = array('','','','','','','','','','');
+       $closing = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
+       $level = 0;
+       $last = -1;
+
+       $closeclass = false;
+       $convert = 1;
+       $ctype = 0;
+
        $accounts = get_gl_accounts_all(0);
 
-       while ($account = db_fetch($accounts))
+       while ($account=db_fetch($accounts))
        {
-               $bal = getPeriods($year, $account["account_code"], $dimension, $dimension2);
-               if (!$bal['per01'] && !$bal['per02'] && !$bal['per03'] && !$bal['per04'] &&
-                       !$bal['per05'] && !$bal['per06'] && !$bal['per07'] && !$bal['per08'] &&
-                       !$bal['per09'] && !$bal['per10'] && !$bal['per11'] && !$bal['per12'])
+               if ($account['account_code'] == null && $account['parent'] > 0)
                        continue;
-               $balance = array(1 => $bal['per01'], $bal['per02'], $bal['per03'], $bal['per04'],
-                       $bal['per05'], $bal['per06'], $bal['per07'], $bal['per08'],
-                       $bal['per09'], $bal['per10'], $bal['per11'], $bal['per12']);
+
+               if ($account['account_code'] != null)
+               {
+                       $bal = getPeriods($year, $account["account_code"], $dimension, $dimension2);
+                       if (!$bal['per01'] && !$bal['per02'] && !$bal['per03'] && !$bal['per04'] &&
+                               !$bal['per05'] && !$bal['per06'] && !$bal['per07'] && !$bal['per08'] &&
+                               !$bal['per09'] && !$bal['per10'] && !$bal['per11'] && !$bal['per12'])
+                               continue;
+               }
                if ($account['AccountClassName'] != $classname)
                {
                        if ($classname != '')
@@ -201,31 +225,43 @@ function print_annual_expense_breakdown()
                        }
                }
 
-               if ($account['AccountTypeName'] != $group)
+               if ($account['AccountTypeName'] != $typename[$level])
                {
-                       if ($group != '')
+                       if ($typename[$level] != '')
                        {
-                               $rep->Line($rep->row + 6);
-                               $rep->row -= 6;
-                               $rep->TextCol(0, 2,     _('Total') . " " . $group);
-                               for ($i = 1; $i <= 12; $i++)
-                                       $rep->TextCol($i + 1, $i + 2, number_format2($total[$i], $dec));
-                               $total = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
-                               $rep->row -= ($rep->lineHeight + 4);
+                               for ( ; $level >= 0, $typename[$level] != ''; $level--) 
+                               {
+                                       if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0)
+                                       {
+                                               $rep->row += 6;
+                                               $rep->Line($rep->row);
+                                               $rep->NewLine();
+                                               $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
+                                               for ($i = 1; $i <= 12; $i++)
+                                               {
+                                                       $rep->AmountCol($i + 1, $i + 2, $total[$level][$i] * $convert, $dec);
+                                                       $total[$level][$i] = 0.0;
+                                               }
+                                       }
+                                       else
+                                               break;
+                                       $rep->NewLine();
+                               }       
                                if ($closeclass)
                                {
-                                       $rep->Line($rep->row + 6);
-                                       $rep->row -= 6;
+                                       $rep->row += 6;
+                                       $rep->Line($rep->row);
+                                       $rep->NewLine();
                                        $rep->Font('bold');
                                        $rep->TextCol(0, 2,     _('Total') . " " . $classname);
                                        for ($i = 1; $i <= 12; $i++)
                                        {
-                                               $rep->TextCol($i + 1, $i + 2, number_format2($total2[$i], $dec));
+                                               $rep->AmountCol($i + 1, $i + 2, $total2[$i] * $convert, $dec);
                                                $sales[$i] += $total2[$i];
                                        }
                                        $rep->Font();
                                        $total2 = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
-                                       $rep->NewLine(3);
+                                       $rep->NewLine(2);
                                        $closeclass = false;
                                }
                        }
@@ -234,30 +270,49 @@ function print_annual_expense_breakdown()
                                $rep->Font('bold');
                                $rep->TextCol(0, 5, $account['AccountClassName']);
                                $rep->Font();
-                               $rep->row -= ($rep->lineHeight + 4);
+                               $rep->NewLine();
                        }
-                       $group = $account['AccountTypeName'];
+                       $level++;
+                       if ($account['parent'] != $last)
+                               $last = $account['parent'];
+                       $typename[$level] = $account['AccountTypeName'];
+                       $closing[$level] = $account['parent'];
+                       $rep->row -= 4;
                        $rep->TextCol(0, 5, $account['AccountTypeName']);
-                       $rep->Line($rep->row - 4);
-                       $rep->row -= ($rep->lineHeight + 4);
+                       $rep->row -= 4;
+                       $rep->Line($rep->row);
+                       $rep->NewLine();
                }
                $classname = $account['AccountClassName'];
-               $rep->TextCol(0, 1,     $account['account_code']);
-               $rep->TextCol(1, 2,     $account['account_name']);
-               for ($i = 1; $i <= 12; $i++)
+               $ctype = $account['ClassType'];
+               $convert = get_class_type_convert($ctype); 
+
+               if ($account['account_code'] != null)
                {
-                       $rep->TextCol($i + 1, $i + 2, number_format2($balance[$i], $dec));
-                       $total[$i] += $balance[$i];
-                       $total2[$i] += $balance[$i];
-               }
+                       $balance = array(1 => $bal['per01'], $bal['per02'], $bal['per03'], $bal['per04'],
+                               $bal['per05'], $bal['per06'], $bal['per07'], $bal['per08'],
+                               $bal['per09'], $bal['per10'], $bal['per11'], $bal['per12']);
+                       $rep->TextCol(0, 1,     $account['account_code']);
+                       $rep->TextCol(1, 2,     $account['account_name']);
 
-               $rep->NewLine();
+                       for ($i = 1; $i <= 12; $i++)
+                       {
+                               $rep->AmountCol($i + 1, $i + 2, $balance[$i] * $convert, $dec);
+                               $total2[$i] += $balance[$i];
+                       }
+                       for ($j = 0; $j <= $level; $j++)
+                       {
+                               for ($i = 1; $i <= 12; $i++)
+                                       $total[$j][$i] += $balance[$i];
+                       }
+                       $rep->NewLine();
 
-               if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
-               {
-                       $rep->Line($rep->row - 2);
-                       $rep->Header();
-               }
+                       if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
+                       {
+                               $rep->Line($rep->row - 2);
+                               $rep->Header();
+                       }
+               }       
        }
        if ($account['AccountClassName'] != $classname)
        {
@@ -266,33 +321,46 @@ function print_annual_expense_breakdown()
                        $closeclass = true;
                }
        }
-       if ($account['AccountTypeName'] != $group)
+       if ($account['AccountTypeName'] != $typename[$level])
        {
-               if ($group != '')
+               if ($typename[$level] != '')
                {
-                       $rep->Line($rep->row + 6);
-                       $rep->row -= 6;
-                       $rep->TextCol(0, 2,     _('Total') . " " . $group);
-                       for ($i = 1; $i <= 12; $i++)
-                               $rep->TextCol($i + 1, $i + 2, number_format2($total[$i], $dec));
-                       $rep->row -= ($rep->lineHeight + 4);
+                       for ( ; $level >= 0, $typename[$level] != ''; $level--) 
+                       {
+                               if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0)
+                               {
+                                       $rep->row += 6;
+                                       $rep->Line($rep->row);
+                                       $rep->NewLine();
+                                       $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
+                                       for ($i = 1; $i <= 12; $i++)
+                                       {
+                                               $rep->AmountCol($i + 1, $i + 2, $total[$level][$i] * $convert, $dec);
+                                               $total[$level][$i] = 0.0;
+                                       }
+                               }
+                               else
+                                       break;
+                               $rep->NewLine();
+                       }       
                        if ($closeclass)
                        {
-                               $rep->Line($rep->row + 6);
-                               $rep->row -= 6;
+                               $rep->row += 6;
+                               $rep->Line($rep->row);
+                               $rep->NewLine();
 
                                $rep->Font('bold');
                                $rep->TextCol(0, 2,     _('Total') . " " . $classname);
                                for ($i = 1; $i <= 12; $i++)
                                {
-                                       $rep->TextCol($i + 1, $i + 2, number_format2($total2[$i], $dec));
+                                       $rep->AmountCol($i + 1, $i + 2, $total2[$i] * $convert, $dec);
                                        $calc[$i] = $sales[$i] + $total2[$i];
                                }
 
-                               $rep->row -= ($rep->lineHeight + 8);
+                               $rep->NewLine(2);
                                $rep->TextCol(0, 2,     _('Calculated Return'));
                                for ($i = 1; $i <= 12; $i++)
-                                       $rep->TextCol($i + 1, $i + 2, number_format2($calc[$i], $dec));
+                                       $rep->AmountCol($i + 1, $i + 2, $calc[$i] * -1, $dec); // always convert
                                $rep->Font();
 
                                $rep->NewLine();