Annual Expense Breakdown Report: additional bugfix.
[fa-stable.git] / reporting / rep705.php
index ad10656cdc0f5f142359a2c5ad67a539e76fba34..f32e5674c2f1337010b880d846880ea1d09b237e 100644 (file)
@@ -12,7 +12,7 @@
 $page_security = 'SA_GLANALYTIC';
 // ----------------------------------------------------------------
 // $ Revision: 2.0 $
-// Creator:    Joe Hunt
+// Creator:    Joe Hunt, Chaitanya for the recursive version 2009-02-05.
 // date_:      2005-05-19
 // Title:      Annual expense breakdown
 // ----------------------------------------------------------------
@@ -22,6 +22,7 @@ include_once($path_to_root . "/includes/session.inc");
 include_once($path_to_root . "/includes/date_functions.inc");
 include_once($path_to_root . "/includes/data_checks.inc");
 include_once($path_to_root . "/gl/includes/gl_db.inc");
+include_once($path_to_root . "/admin/db/tags_db.inc");
 
 //----------------------------------------------------------------------------------------------------
 
@@ -29,10 +30,8 @@ print_annual_expense_breakdown();
 
 //----------------------------------------------------------------------------------------------------
 
-function getPeriods($row, $account, $dimension, $dimension2)
+function getPeriods($yr, $mo, $account, $dimension, $dimension2, $thousands)
 {
-       $yr = $row['yr'];
-       $mo = $row['mo'];
        $date13 = date('Y-m-d',mktime(0,0,0,$mo+1,1,$yr));
        $date12 = date('Y-m-d',mktime(0,0,0,$mo,1,$yr));
        $date11 = date('Y-m-d',mktime(0,0,0,$mo-1,1,$yr));
@@ -47,18 +46,19 @@ function getPeriods($row, $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 / $thousands ELSE 0 END) AS per01,
+                               SUM(CASE WHEN tran_date >= '$date02' AND tran_date < '$date03' THEN amount / $thousands ELSE 0 END) AS per02,
+                               SUM(CASE WHEN tran_date >= '$date03' AND tran_date < '$date04' THEN amount / $thousands ELSE 0 END) AS per03,
+                               SUM(CASE WHEN tran_date >= '$date04' AND tran_date < '$date05' THEN amount / $thousands ELSE 0 END) AS per04,
+                               SUM(CASE WHEN tran_date >= '$date05' AND tran_date < '$date06' THEN amount / $thousands ELSE 0 END) AS per05,
+                               SUM(CASE WHEN tran_date >= '$date06' AND tran_date < '$date07' THEN amount / $thousands ELSE 0 END) AS per06,
+                               SUM(CASE WHEN tran_date >= '$date07' AND tran_date < '$date08' THEN amount / $thousands ELSE 0 END) AS per07,
+                               SUM(CASE WHEN tran_date >= '$date08' AND tran_date < '$date09' THEN amount / $thousands ELSE 0 END) AS per08,
+                               SUM(CASE WHEN tran_date >= '$date09' AND tran_date < '$date10' THEN amount / $thousands ELSE 0 END) AS per09,
+                               SUM(CASE WHEN tran_date >= '$date10' AND tran_date < '$date11' THEN amount / $thousands ELSE 0 END) AS per10,
+                               SUM(CASE WHEN tran_date >= '$date11' AND tran_date < '$date12' THEN amount / $thousands ELSE 0 END) AS per11,
+                               SUM(CASE WHEN tran_date >= '$date12' AND tran_date < '$date13' THEN amount / $thousands ELSE 0 END) AS per12,
+                               SUM(CASE WHEN tran_date >= '$date01' AND tran_date < '$date13' THEN amount / $thousands ELSE 0 END) AS pertotal
                        FROM ".TB_PREF."gl_trans
                                WHERE account='$account'";
        if ($dimension != 0)
@@ -73,47 +73,159 @@ function getPeriods($row, $account, $dimension, $dimension2)
 
 //----------------------------------------------------------------------------------------------------
 
+function display_type ($type, $typename, $yr, $mo, $convert, &$dec, &$rep, $dimension, $dimension2, 
+                                                               $tags, $thousands)
+{
+       $ctotal = array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
+       $total = array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
+       $totals_arr = array();
+
+       $printtitle = 0; //Flag for printing type name  
+
+       //Get Accounts directly under this group/type
+       $result = get_gl_accounts(null, null, $type);   
+       while ($account=db_fetch($result))
+       {
+               if ($tags != -1 && is_array($tags) && $tags[0] != false)
+               {
+                       if (!is_record_in_tags($tags, TAG_ACCOUNT, $account['account_code']))
+                               continue;
+               }       
+               $bal = getPeriods($yr, $mo, $account["account_code"], $dimension, $dimension2, $thousands);
+               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;
+       
+               //Print Type Title if it has at least one non-zero account      
+               if (!$printtitle)
+               {
+                       $printtitle = 1;
+                       $rep->row -= 4;
+                       $rep->TextCol(0, 5, $typename);
+                       $rep->row -= 4;
+                       $rep->Line($rep->row);
+                       $rep->NewLine();
+               }                       
+
+               $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'], $bal[pertotal]);
+               $rep->TextCol(0, 1,     $account['account_code']);
+               $rep->TextCol(1, 2,     $account['account_name']);
+
+               for ($i = 1; $i <= 13; $i++)
+               {
+                       $rep->AmountCol($i + 1, $i + 2, $balance[$i] * $convert, $dec);
+                       $ctotal[$i] += $balance[$i];
+               }
+
+               $rep->NewLine();
+       }
+               
+       //Get Account groups/types under this group/type
+       $result = get_account_types(false, false, $type);
+       while ($accounttype=db_fetch($result))
+       {
+               //Print Type Title if has sub types and not previously printed
+               if (!$printtitle)
+               {
+                       $printtitle = 1;
+                       $rep->row -= 4;
+                       $rep->TextCol(0, 5, $typename);
+                       $rep->row -= 4;
+                       $rep->Line($rep->row);
+                       $rep->NewLine();
+               }
+
+               $totals_arr = display_type($accounttype["id"], $accounttype["name"], $yr, $mo, $convert, $dec, $rep,
+                                                                                       $dimension, $dimension2, $tags, $thousands);
+               for ($i = 1; $i <= 13; $i++)
+               {
+                       $total[$i] += $totals_arr[$i];
+               }
+       }
+
+       //Display Type Summary if total is != 0 OR head is printed (Needed in case of unused hierarchical COA) 
+       if ($printtitle)
+       {
+               $rep->row += 6;
+               $rep->Line($rep->row);
+               $rep->NewLine();
+               $rep->TextCol(0, 2,     _('Total') . " " . $typename);
+               for ($i = 1; $i <= 13; $i++)
+                       $rep->AmountCol($i + 1, $i + 2, ($total[$i] + $ctotal[$i]) * $convert, $dec);
+               $rep->NewLine();
+       }
+       for ($i = 1; $i <= 13; $i++)
+               $totals_arr[$i] = $total[$i] + $ctotal[$i];     
+       return $totals_arr;
+}
+
+//----------------------------------------------------------------------------------------------------
+
 function print_annual_expense_breakdown()
 {
-       global $path_to_root, $date_system;
+       global $path_to_root, $SysPrefs;
 
        $dim = get_company_pref('use_dimension');
        $dimension = $dimension2 = 0;
+       $thousands = 0;
 
        if ($dim == 2)
        {
                $year = $_POST['PARAM_0'];
                $dimension = $_POST['PARAM_1'];
                $dimension2 = $_POST['PARAM_2'];
-               $comments = $_POST['PARAM_3'];
-               $destination = $_POST['PARAM_4'];
+               $tags = (isset($_POST['PARAM_3']) ? $_POST['PARAM_3'] : -1);
+               $comments = $_POST['PARAM_4'];
+               $orientation = $_POST['PARAM_5'];
+               $thousands = $_POST['PARAM_6'];
+               $destination = $_POST['PARAM_7'];
        }
-       else if ($dim == 1)
+       elseif ($dim == 1)
        {
                $year = $_POST['PARAM_0'];
                $dimension = $_POST['PARAM_1'];
-               $comments = $_POST['PARAM_2'];
-               $destination = $_POST['PARAM_3'];
+               $tags = (isset($_POST['PARAM_2']) ? $_POST['PARAM_2'] : -1);
+               $comments = $_POST['PARAM_3'];
+               $orientation = $_POST['PARAM_4'];
+               $thousands = $_POST['PARAM_5'];
+               $destination = $_POST['PARAM_6'];
        }
        else
        {
                $year = $_POST['PARAM_0'];
-               $comments = $_POST['PARAM_1'];
-               $destination = $_POST['PARAM_2'];
+               $tags = (isset($_POST['PARAM_1']) ? $_POST['PARAM_1'] : -1);
+               $comments = $_POST['PARAM_2'];
+               $orientation = $_POST['PARAM_3'];
+               $thousands = $_POST['PARAM_4'];
+               $destination = $_POST['PARAM_5'];
        }
        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();
+       $orientation = ($orientation ? 'L' : 'P');
+       if ($thousands)
+       {
+               $dec = 1;
+               $thousands = 1000;
+               $amts_thousands = _("Amounts in thousands");
+               $fontSize = 9;
+       }
+       else
+       {
+               $dec = 2;
+               $thousands = 1;
+               $amts_thousands = "";
+               $fontSize = ($orientation == 'P' ? 7 : 8);
+       }
 
-       $cols = array(0, 40, 150, 180, 210, 240, 270, 300, 330, 360, 390, 420, 450, 480, 510);
-       //------------0--1---2----3----4----5----6----7----8----10---11---12---13---14---15-
+       $cols = array(0, 34, 130, 162, 194, 226, 258, 290, 322, 354, 386, 418, 450, 482, 514, 546);
+       //------------0--1---2----3----4----5----6----7----8----10---11---12---13---14---15---16-
 
-       //$yr = date('Y');
-       //$mo = date('m'):
        // from now
        $sql = "SELECT begin, end, YEAR(end) AS yr, MONTH(end) AS mo FROM ".TB_PREF."fiscal_year WHERE id=".db_escape($year);
        $result = db_query($sql, "could not get fiscal year");
@@ -123,9 +235,9 @@ function print_annual_expense_breakdown()
        $yr = $row['yr'];
        $mo = $row['mo'];
        $da = 1;
-       if ($date_system == 1)
+       if ($SysPrefs->date_system == 1)
                list($yr, $mo, $da) = jalali_to_gregorian($yr, $mo, $da);
-       elseif ($date_system == 2)
+       elseif ($SysPrefs->date_system == 2)
                list($yr, $mo, $da) = islamic_to_gregorian($yr, $mo, $da);
        $per12 = strftime('%b',mktime(0,0,0,$mo,$da,$yr));
        $per11 = strftime('%b',mktime(0,0,0,$mo-1,$da,$yr));
@@ -141,10 +253,10 @@ function print_annual_expense_breakdown()
        $per01 = strftime('%b',mktime(0,0,0,$mo-11,$da,$yr));
 
        $headers = array(_('Account'), _('Account Name'), $per01, $per02, $per03, $per04,
-               $per05, $per06, $per07, $per08, $per09, $per10, $per11, $per12);
+               $per05, $per06, $per07, $per08, $per09, $per10, $per11, $per12, _('Total'));
 
        $aligns = array('left', 'left', 'right', 'right', 'right',      'right', 'right', 'right',
-               'right', 'right', 'right',      'right', 'right', 'right');
+               'right', 'right', 'right',      'right', 'right', 'right', 'right');
 
     if ($dim == 2)
     {
@@ -155,225 +267,89 @@ function print_annual_expense_breakdown()
                                'from' => get_dimension_string($dimension), 'to' => ''),
                        3 => array('text' => _("Dimension")." 2",
                                'from' => get_dimension_string($dimension2), 'to' => ''),
-                       4 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
-                               'to' => ''));
+                       4 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''),    
+                       5 => array('text' => _('Info'), 'from' => $amts_thousands, 'to' => ''));
     }
-    else if ($dim == 1)
+    elseif ($dim == 1)
     {
        $params =   array(      0 => $comments,
                        1 => array('text' => _("Year"),
                                'from' => $year, 'to' => ''),
                        2 => array('text' => _('Dimension'),
                                'from' => get_dimension_string($dimension), 'to' => ''),
-                       3 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
-                               'to' => ''));
+                       3 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''),    
+                       4 => array('text' => _('Info'), 'from' => $amts_thousands, 'to' => ''));
     }
     else
     {
        $params =   array(      0 => $comments,
                        1 => array('text' => _("Year"),
                                'from' => $year, 'to' => ''),
-                       2 => array('text' => _('Info'), 'from' => _('Amounts in thousands'),
-                               'to' => ''));
+                       2 => array('text' => _('Tags'), 'from' => get_tag_names($tags), 'to' => ''),    
+                       3 => array('text' => _('Info'), 'from' => $amts_thousands, 'to' => ''));
     }
 
-       $rep = new FrontReport(_('Annual Expense Breakdown'), "AnnualBreakDown", user_pagesize());
+       $rep = new FrontReport(_('Annual Expense Breakdown'), "AnnualBreakDown", user_pagesize(), $fontSize,
+                                                                               $orientation);
+   if ($orientation == 'L')
+       recalculate_cols($cols);
+       elseif (user_pagesize() == 'A4')
+       {
+               // Portrait, so adjust columns for A4, 16.7 pts narrower than Letter
+               for ($i = 2; $i < sizeof($cols); $i++)
+                       $cols[$i] -= 17;
+       }
 
        $rep->Font();
        $rep->Info($params, $cols, $headers, $aligns);
-       $rep->Header();
-
-       $classname = '';
-       $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))
-       {
-               if ($account['account_code'] == null && $account['parent'] > 0)
-                       continue;
-
-               if ($account['account_code'] != null)
-               {
-                       $bal = getPeriods($row, $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 != '')
-                       {
-                               $closeclass = true;
-                       }
-               }
+       $rep->NewPage();
 
-               if ($account['AccountTypeName'] != $typename[$level])
-               {
-                       if ($typename[$level] != '')
-                       {
-                               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->row += 6;
-                                       $rep->Line($rep->row);
-                                       $rep->NewLine();
-                                       $rep->Font('bold');
-                                       $rep->TextCol(0, 2,     _('Total') . " " . $classname);
-                                       for ($i = 1; $i <= 12; $i++)
-                                       {
-                                               $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(2);
-                                       $closeclass = false;
-                               }
-                       }
-                       if ($account['AccountClassName'] != $classname)
-                       {
-                               $rep->Font('bold');
-                               $rep->TextCol(0, 5, $account['AccountClassName']);
-                               $rep->Font();
-                               $rep->NewLine();
-                       }
-                       $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->row -= 4;
-                       $rep->Line($rep->row);
-                       $rep->NewLine();
-               }
-               $classname = $account['AccountClassName'];
-               $ctype = $account['ClassType'];
-               $convert = get_class_type_convert($ctype); 
-
-               if ($account['account_code'] != null)
-               {
-                       $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']);
-
-                       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 ($account['AccountClassName'] != $classname)
+       $sales = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
+       
+       $classresult = get_account_classes(false, 0);
+       while ($class = db_fetch($classresult))
        {
-               if ($classname != '')
+               $ctotal = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
+               $convert = get_class_type_convert($class["ctype"]);             
+               
+               //Print Class Name      
+               $rep->Font('bold');
+               $rep->TextCol(0, 5, $class["class_name"]);
+               $rep->Font();
+               $rep->NewLine();
+               
+               //Get Account groups/types under this group/type with no parents
+               $typeresult = get_account_types(false, $class['cid'], -1);
+               while ($accounttype=db_fetch($typeresult))
                {
-                       $closeclass = true;
+                       $classtotal = display_type($accounttype["id"], $accounttype["name"], $yr, $mo, $convert, $dec, $rep,
+                                                                                               $dimension,     $dimension2, $tags, $thousands);
+                       for ($i = 1; $i <= 13; $i++)
+                               $ctotal[$i] += $classtotal[$i];
                }
-       }
-       if ($account['AccountTypeName'] != $typename[$level])
-       {
-               if ($typename[$level] != '')
+               
+               //Print Class Summary   
+               $rep->row += 6;
+               $rep->Line($rep->row);
+               $rep->NewLine();
+               $rep->Font('bold');
+               $rep->TextCol(0, 2,     _('Total') . " " . $class["class_name"]);
+               for ($i = 1; $i <= 13; $i++)
                {
-                       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->row += 6;
-                               $rep->Line($rep->row);
-                               $rep->NewLine();
-
-                               $rep->Font('bold');
-                               $rep->TextCol(0, 2,     _('Total') . " " . $classname);
-                               for ($i = 1; $i <= 12; $i++)
-                               {
-                                       $rep->AmountCol($i + 1, $i + 2, $total2[$i] * $convert, $dec);
-                                       $calc[$i] = $sales[$i] + $total2[$i];
-                               }
-
-                               $rep->NewLine(2);
-                               $rep->TextCol(0, 2,     _('Calculated Return'));
-                               for ($i = 1; $i <= 12; $i++)
-                                       $rep->AmountCol($i + 1, $i + 2, $calc[$i] * -1, $dec); // always convert
-                               $rep->Font();
-
-                               $rep->NewLine();
-                       }
+                       $rep->AmountCol($i + 1, $i + 2, $ctotal[$i] * $convert, $dec);
+                       $sales[$i] += $ctotal[$i];
                }
+               $rep->Font();
+               $rep->NewLine(2);
        }
+       $rep->Font('bold');     
+       $rep->TextCol(0, 2,     _("Calculated Return"));
+       for ($i = 1; $i <= 13; $i++)
+               $rep->AmountCol($i + 1, $i + 2, $sales[$i] * -1, $dec);
+       $rep->Font();
+       $rep->NewLine();
        $rep->Line($rep->row);
+       $rep->NewLine(2);
        $rep->End();
 }
 
-?>
\ No newline at end of file