Again fixed nested subtypes in account types correctly in COA, Monthly Breakdown...
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Tue, 26 May 2009 16:03:24 +0000 (16:03 +0000)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Tue, 26 May 2009 16:03:24 +0000 (16:03 +0000)
Fixed wider combobox for backup-files in company backup

CHANGELOG.txt
admin/backups.php
gl/includes/db/gl_db_account_types.inc
gl/includes/db/gl_db_accounts.inc
reporting/rep701.php
reporting/rep705.php
reporting/rep706.php
reporting/rep707.php

index 3574c50be3fae91f005b3ebc100ba5dc60dc039d..d22d272c8b7d3b1651c0ffed3deb2c2d7aa88546 100644 (file)
@@ -19,13 +19,24 @@ Legend:
 ! -> Note
 $ -> Affected files
 
+26-May-2009 Joe Hunt
+# Again fixed nested subtypes in account types correctly in COA, Monthly Breakdown, Balance Sheet and P&L Statement
+$ /gl/includes/db/gl_db_accounts.inc
+  /gl/includes/db/gl_db_account_types.inc
+  /reporting/rep701.php
+  /reporting/rep705.php
+  /reporting/rep706.php
+  /reporting/rep707.php
+# fixed wider combobox for backup-files in company backup
+$ /admin/backups.php
+
 25-May-2009 Joe Hunt
 # Fixed Supplier Credit so Purchase Order Outstanding is decreased too.
 $ /purchasing/includes/db/grn_db.inc
 # Fixed so nested subtypes in account types are nested correctly in Balance Sheet and P&L Statement
 $ /gl/includes/db/gl_db_accounts.inc
-$ /reporting/rep706.php
-$ /reporting/rep707.php
+  /reporting/rep706.php
+  /reporting/rep707.php
 ! Extra column in Stock Check Sheet, On Order and option for selecting Only Shortage.
 $ /reporting/rep303.php
   /reporting/reports_main.php
index f78e1d5a710eae40b83295c7531020f544c3589e..728d3f29dde29877efd2528e5cb453a122fcd0f2 100644 (file)
@@ -84,7 +84,7 @@ function get_backup_file_combo()
                if (preg_match("/.sql(.zip|.gz)?$/", $file))
                $opt_files .= "<option value='$file'>$file</option>";
 
-       $selector = "<select name='cmb_backups' size=2 style='height:160px;width:230px'>$opt_files</select>";
+       $selector = "<select name='cmb_backups' size=2 style='height:160px;min-width:230px'>$opt_files</select>";
 
        $Ajax->addUpdate('cmd_backups', "_cmd_backups_sel", $selector);
        $selector = "<span id='_cmd_backups_sel'>".$selector."</span>\n";
index 21a8c2ce81eead5437a5c266e1435da92dc73cf4..39af247fb8b078085c9ae40da8e27aaab8b3d0a4 100644 (file)
@@ -52,6 +52,19 @@ function get_account_type_name($id)
 }
 
 
+function get_account_types_all($balance=-1)
+{
+       $sql = "SELECT ".TB_PREF."chart_types.name AS AccountTypeName,".TB_PREF."chart_types.parent,".TB_PREF."chart_types.id AS AccountType,
+               ".TB_PREF."chart_class.class_name AS AccountClassName
+               FROM ".TB_PREF."chart_types, ".TB_PREF."chart_class
+               WHERE ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid";
+       if ($balance != -1)
+               $sql .= " AND ".TB_PREF."chart_class.balance_sheet=$balance";
+       $sql .= " ORDER BY ".TB_PREF."chart_class.cid, IF(parent > -1,parent,".TB_PREF."chart_types.id), parent";
+
+       return db_query($sql, "could not get gl types");
+}
+
 function delete_account_type($id)
 {
        $sql = "DELETE FROM ".TB_PREF."chart_types WHERE id = $id";
index 695743d8af6818878cdd63ef0996fc9a0cfce303..380d5b442e1cca62585f39c0566c975ddb365da6 100644 (file)
@@ -49,20 +49,22 @@ function get_gl_accounts($from=null, $to=null)
        return db_query($sql, "could not get gl accounts");
 }
 
-function get_gl_accounts_all($balance=-1)
+function get_gl_accounts_in_type($type)
 {
-       $sql = "SELECT ".TB_PREF."chart_master.*,".TB_PREF."chart_types.name AS AccountTypeName,".TB_PREF."chart_types.parent,
-               ".TB_PREF."chart_class.class_name AS AccountClassName
-               FROM ".TB_PREF."chart_master,".TB_PREF."chart_types, ".TB_PREF."chart_class
-               WHERE ".TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id AND
-               ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid";
-       if ($balance != -1)
-               $sql .= " AND ".TB_PREF."chart_class.balance_sheet=$balance";
-       $sql .= " ORDER BY ".TB_PREF."chart_class.cid, ".TB_PREF."chart_types.id, ".TB_PREF."chart_master.account_code";
+       $sql = "SELECT * FROM ".TB_PREF."chart_master WHERE account_type=$type ORDER BY account_code";
 
        return db_query($sql, "could not get gl accounts");
 }
 
+function num_accounts_in_type($type, $parent)
+{
+       $sql = "SELECT COUNT(*) FROM ".TB_PREF."chart_master WHERE account_type=$type OR account_type=$parent";
+
+       $result = db_query($sql, "could not get gl accounts");
+       $row = db_fetch_row($result);
+       return $row[0];
+}
+
 function get_gl_account($code)
 {
        $sql = "SELECT * FROM ".TB_PREF."chart_master WHERE account_code='$code'";
index 63c90c39ede42b88b85206d4c4224389b1e33793..6a44328efee127c786589cc51f49b31be3ea64ff 100644 (file)
@@ -60,49 +60,55 @@ function print_Chart_of_Accounts()
        $classname = '';
        $group = '';
 
-       $accounts = get_gl_accounts_all();
+       $types = get_account_types_all();
 
-       while ($account=db_fetch($accounts))
+       while ($type=db_fetch($types))
        {
-               if ($showbalance == 1)
-               {
-                       $begin = begin_fiscalyear();
-                       if (is_account_balancesheet($account["account_code"]))
-                               $begin = "";
-                       $balance = get_gl_trans_from_to($begin, ToDay(), $account["account_code"], 0);
-               }
-               if ($account['AccountTypeName'] != $group)
+               if (!num_accounts_in_type($type['AccountType'], $type['parent']))
+                       continue;
+               if ($type['AccountTypeName'] != $group)
                {
                        if ($classname != '')
                                $rep->row -= 4;
-                       if ($account['AccountClassName'] != $classname)
+                       if ($type['AccountClassName'] != $classname)
                        {
                                $rep->Font('bold');
-                               $rep->TextCol(0, 4, $account['AccountClassName']);
+                               $rep->TextCol(0, 4, $type['AccountClassName']);
                                $rep->Font();
                                //$rep->row -= ($rep->lineHeight + 4);
                                $rep->NewLine();
                        }
-                       $group = $account['AccountTypeName'];
-                       $rep->TextCol(0, 4, $account['AccountTypeName']);
+                       $group = $type['AccountTypeName'];
+                       $rep->TextCol(0, 4, $type['AccountTypeName']);
                        //$rep->Line($rep->row - 4);
                        //$rep->row -= ($rep->lineHeight + 4);
                        $rep->NewLine();
                }
-               $classname = $account['AccountClassName'];
-
-               $rep->TextCol(0, 1,     $account['account_code']);
-               $rep->TextCol(1, 2,     $account['account_name']);
-               $rep->TextCol(2, 3,     $account['account_code2']);
-               if ($showbalance == 1)  
-                       $rep->AmountCol(3, 4, $balance, $dec);
-
-               $rep->NewLine();
-               if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
+               $classname = $type['AccountClassName'];
+               
+               $accounts = get_gl_accounts_in_type($type['AccountType']);
+               while ($account=db_fetch($accounts))
                {
-                       $rep->Line($rep->row - 2);
-                       $rep->Header();
-               }
+                       if ($showbalance == 1)
+                       {
+                               $begin = begin_fiscalyear();
+                               if (is_account_balancesheet($account["account_code"]))
+                                       $begin = "";
+                               $balance = get_gl_trans_from_to($begin, ToDay(), $account["account_code"], 0);
+                       }
+                       $rep->TextCol(0, 1,     $account['account_code']);
+                       $rep->TextCol(1, 2,     $account['account_name']);
+                       $rep->TextCol(2, 3,     $account['account_code2']);
+                       if ($showbalance == 1)  
+                               $rep->AmountCol(3, 4, $balance, $dec);
+
+                       $rep->NewLine();
+                       if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
+                       {
+                               $rep->Line($rep->row - 2);
+                               $rep->Header();
+                       }
+               }       
        }
        $rep->Line($rep->row);
        $rep->End();
index 699cc28a063b258a87d339ed603e40e29e96c40f..8516da4f1ec4e6458dc95357bc0d905077dd4150 100644 (file)
@@ -179,24 +179,32 @@ function print_annual_expense_breakdown()
        $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);
-       $accounts = get_gl_accounts_all(0);
+       $typename = array('','','','','','','','','','');
+       $closing = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
+       $level = 0;
+       $last = -1;
 
-       while ($account = db_fetch($accounts))
+       $types = get_account_types_all(0);
+
+       while ($type = db_fetch($types))
        {
-               $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 (!num_accounts_in_type($type['AccountType'], $type['parent']))
                        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['AccountClassName'] != $classname)
+               if ($type['AccountClassName'] != $classname)
                {
                        if ($classname != '')
                        {
@@ -204,18 +212,28 @@ function print_annual_expense_breakdown()
                        }
                }
 
-               if ($account['AccountTypeName'] != $group)
+               if ($type['AccountTypeName'] != $typename[$level])
                {
-                       if ($group != '')
+                       if ($typename[$level] != '')
                        {
-                               $rep->row += 6;
-                               $rep->Line($rep->row);
-                               $rep->NewLine();
-                               $rep->TextCol(0, 2,     _('Total') . " " . $group);
-                               for ($i = 1; $i <= 12; $i++)
-                                       $rep->AmountCol($i + 1, $i + 2, $total[$i], $dec);
-                               $total = Array(1 => 0,0,0,0,0,0,0,0,0,0,0,0);
-                               $rep->NewLine();
+                               for ( ; $level >= 0, $typename[$level] != ''; $level--) 
+                               {
+                                       if ($type['parent'] == $closing[$level] || $type['parent'] == $last || $type['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], $dec);
+                                                       $total[$level][$i] = 0.0;
+                                               }
+                                       }
+                                       else
+                                               break;
+                                       $rep->NewLine();
+                               }       
                                if ($closeclass)
                                {
                                        $rep->row += 6;
@@ -234,56 +252,88 @@ function print_annual_expense_breakdown()
                                        $closeclass = false;
                                }
                        }
-                       if ($account['AccountClassName'] != $classname)
+                       if ($type['AccountClassName'] != $classname)
                        {
                                $rep->Font('bold');
-                               $rep->TextCol(0, 5, $account['AccountClassName']);
+                               $rep->TextCol(0, 5, $type['AccountClassName']);
                                $rep->Font();
                                $rep->NewLine();
                        }
-                       $group = $account['AccountTypeName'];
+                       $level++;
+                       if ($type['parent'] != $last)
+                               $last = $type['parent'];
+                       $typename[$level] = $type['AccountTypeName'];
+                       $closing[$level] = $type['parent'];
                        $rep->row -= 4;
-                       $rep->TextCol(0, 5, $account['AccountTypeName']);
+                       $rep->TextCol(0, 5, $type['AccountTypeName']);
                        $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++)
+               $classname = $type['AccountClassName'];
+
+               $accounts = get_gl_accounts_in_type($type['AccountType']);
+               while ($account=db_fetch($accounts))
                {
-                       $rep->AmountCol($i + 1, $i + 2, $balance[$i], $dec);
-                       $total[$i] += $balance[$i];
-                       $total2[$i] += $balance[$i];
-               }
+                       $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;
+                       $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], $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)
+       if ($type['AccountClassName'] != $classname)
        {
                if ($classname != '')
                {
                        $closeclass = true;
                }
        }
-       if ($account['AccountTypeName'] != $group)
+       if ($type['AccountTypeName'] != $typename[$level])
        {
-               if ($group != '')
+               if ($typename[$level] != '')
                {
-                       $rep->row += 6;
-                       $rep->Line($rep->row);
-                       $rep->NewLine();
-                       $rep->TextCol(0, 2,     _('Total') . " " . $group);
-                       for ($i = 1; $i <= 12; $i++)
-                               $rep->AmountCol($i + 1, $i + 2, $total[$i], $dec);
-                       $rep->NewLine();
+                       for ( ; $level >= 0, $typename[$level] != ''; $level--) 
+                       {
+                               if ($type['parent'] == $closing[$level] || $type['parent'] == $last || $type['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], $dec);
+                                               $total[$level][$i] = 0.0;
+                                       }
+                               }
+                               else
+                                       break;
+                               $rep->NewLine();
+                       }       
                        if ($closeclass)
                        {
                                $rep->row += 6;
index f490e08d4ee942287f1630402490b8c141723a50..e69fc0dba48854aeaa3a6c09b6d8ea5f2e4c95bf 100644 (file)
@@ -117,24 +117,20 @@ function print_balance_sheet()
        $typeperiod = array(0,0,0,0,0,0,0,0,0,0);
        $typeclose = array(0,0,0,0,0,0,0,0,0,0);
        $typename = array('','','','','','','','','','');
-       $parent = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
+       $closing = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
        $level = 0;
+       $last = -1;
        
        $closeclass = false;
        $rep->NewLine();
 
-       $accounts = get_gl_accounts_all(1);
+       $types = get_account_types_all(1);
 
-       while ($account=db_fetch($accounts))
+       while ($type=db_fetch($types))
        {
-               $prev_balance = get_gl_balance_from_to("", $from, $account["account_code"], $dimension, $dimension2);
-
-               $curr_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
-
-               if (!$prev_balance && !$curr_balance)
+               if (!num_accounts_in_type($type['AccountType'], $type['parent']))
                        continue;
-
-               if ($account['AccountClassName'] != $classname)
+               if ($type['AccountClassName'] != $classname)
                {
                        if ($classname != '')
                        {
@@ -142,30 +138,36 @@ function print_balance_sheet()
                        }
                }
 
-               if ($account['AccountTypeName'] != $typename[$level])
+               if ($type['AccountTypeName'] != $typename[$level])
                {
-                       if ($typename[$level] != '' && $account['parent'] == -1)
+                       //$rep->NewLine();
+                       //$rep->TextCol(0, 5,   "level = $level, closing[level] = ".$closing[$level].", type[parent] = ".$type['parent']." last = ".$last);
+                       //$rep->NewLine();
+                       if ($typename[$level] != '')
                        {
-                               for ( ; $level >= 0; $level--)
+                               for ( ; $level >= 0, $typename[$level] != ''; $level--) 
                                {
-                                       $rep->row += 6;
-                                       $rep->Line($rep->row);
-                                       $rep->NewLine();
-                                       $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
-                                       $rep->AmountCol(2, 3, $typeopen[$level], $dec);
-                                       $rep->AmountCol(3, 4, $typeperiod[$level], $dec);
-                                       $rep->AmountCol(4, 5, $typeclose[$level], $dec);
-                                       $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
-                                       if ($parent[$level] == -1)
+                                       if ($type['parent'] == $closing[$level] || $type['parent'] == $last || $type['parent'] <= 0)
+                                       {
+                                               $rep->row += 6;
+                                               $rep->Line($rep->row);
+                                               $rep->NewLine();
+                                               $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
+                                               $rep->AmountCol(2, 3, $typeopen[$level], $dec);
+                                               $rep->AmountCol(3, 4, $typeperiod[$level], $dec);
+                                               $rep->AmountCol(4, 5, $typeclose[$level], $dec);
+                                               if ($graphics)
+                                               {
+                                                       $pg->x[] = $$typename[$level];
+                                                       $pg->y[] = abs($typeclose[$level]);
+                                               }
+                                               $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
+                                       }       
+                                       else
                                                break;
                                        $rep->NewLine();
                                }
-                               if ($graphics)
-                               {
-                                       $pg->x[] = $$typename[$level];
-                                       $pg->y[] = abs($typeclose[$level]);
-                               }
-                               $rep->NewLine();
+                               //$rep->NewLine();
                                if ($closeclass)
                                {
                                        $rep->row += 6;
@@ -185,80 +187,95 @@ function print_balance_sheet()
                                        $closeclass = false;
                                }
                        }
-                       if ($account['AccountClassName'] != $classname)
+                       if ($type['AccountClassName'] != $classname)
                        {
                                $rep->Font('bold');
-                               $rep->TextCol(0, 5, $account['AccountClassName']);
+                               $rep->TextCol(0, 5, $type['AccountClassName']);
                                $rep->Font();
                                $rep->NewLine();
                        }
-                       if ($account['parent'] != -1 && $account['parent'] != $parent[$level])
-                               $level++;
-                       $typename[$level] = $account['AccountTypeName'];
+                       $level++;
+                       if ($type['parent'] != $last)
+                               $last = $type['parent'];
+                       $typename[$level] = $type['AccountTypeName'];
+                       $closing[$level] = $type['parent'];
                        $rep->row -= 4;
-                       $rep->TextCol(0, 5, $account['AccountTypeName']);
+                       $rep->TextCol(0, 5, $type['AccountTypeName']);
                        $rep->row -= 4;
                        $rep->Line($rep->row);
                        $rep->NewLine();
                }
-               $classname = $account['AccountClassName'];
-               $parent[$level] = $account['parent'];   
+               $classname = $type['AccountClassName'];
 
-               for ($i = 0; $i <= $level; $i++)
+               $accounts = get_gl_accounts_in_type($type['AccountType']);
+               while ($account=db_fetch($accounts))
                {
-                       $typeopen[$i] += $prev_balance;
-                       $typeperiod[$i] += $curr_balance;
-                       $typeclose[$i] = $typeopen[$i] + $typeperiod[$i];
-               }
-               $classopen += $prev_balance;
-               $classperiod += $curr_balance;
-               $classclose = $classopen + $classperiod;
-               $rep->TextCol(0, 1,     $account['account_code']);
-               $rep->TextCol(1, 2,     $account['account_name']);
+                       $prev_balance = get_gl_balance_from_to("", $from, $account["account_code"], $dimension, $dimension2);
 
-               $rep->AmountCol(2, 3, $prev_balance, $dec);
-               $rep->AmountCol(3, 4, $curr_balance, $dec);
-               $rep->AmountCol(4, 5, $curr_balance + $prev_balance, $dec);
+                       $curr_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
 
-               $rep->NewLine();
+                       if (!$prev_balance && !$curr_balance)
+                               continue;
 
-               if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
-               {
-                       $rep->Line($rep->row - 2);
-                       $rep->Header();
-               }
+                       for ($i = 0; $i <= $level; $i++)
+                       {
+                               $typeopen[$i] += $prev_balance;
+                               $typeperiod[$i] += $curr_balance;
+                               $typeclose[$i] = $typeopen[$i] + $typeperiod[$i];
+                       }
+                       $classopen += $prev_balance;
+                       $classperiod += $curr_balance;
+                       $classclose = $classopen + $classperiod;
+                       $rep->TextCol(0, 1,     $account['account_code']);
+                       $rep->TextCol(1, 2,     $account['account_name']);
+
+                       $rep->AmountCol(2, 3, $prev_balance, $dec);
+                       $rep->AmountCol(3, 4, $curr_balance, $dec);
+                       $rep->AmountCol(4, 5, $curr_balance + $prev_balance, $dec);
+
+                       $rep->NewLine();
+
+                       if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
+                       {
+                               $rep->Line($rep->row - 2);
+                               $rep->Header();
+                       }
+               }       
        }
-       if ($account['AccountClassName'] != $classname)
+       if ($type['AccountClassName'] != $classname)
        {
                if ($classname != '')
                {
                        $closeclass = true;
                }
        }
-       if ($account['AccountTypeName'] != $typename[$level])
+       if ($type['AccountTypeName'] != $typename[$level])
        {
                if ($typename[$level] != '')
                {
-                       for ( ; $level >= 0; $level--)
+                       for ( ; $level >= 0, $typename[$level] != ''; $level--) 
                        {
-                               $rep->row += 6;
-                               $rep->Line($rep->row);
-                               $rep->NewLine();
-                               $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
-                               $rep->AmountCol(2, 3, $typeopen[$level], $dec);
-                               $rep->AmountCol(3, 4, $typeperiod[$level], $dec);
-                               $rep->AmountCol(4, 5, $typeclose[$level], $dec);
-                               $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
-                               if ($parent[$level] == -1)
+                               if ($type['parent'] == $closing[$level] || $type['parent'] == $last || $type['parent'] <= 0)
+                               {
+                                       $rep->row += 6;
+                                       $rep->Line($rep->row);
+                                       $rep->NewLine();
+                                       $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
+                                       $rep->AmountCol(2, 3, $typeopen[$level], $dec);
+                                       $rep->AmountCol(3, 4, $typeperiod[$level], $dec);
+                                       $rep->AmountCol(4, 5, $typeclose[$level], $dec);
+                                       if ($graphics)
+                                       {
+                                               $pg->x[] = $$typename[$level];
+                                               $pg->y[] = abs($typeclose[$level]);
+                                       }
+                                       $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
+                               }
+                               else
                                        break;
                                $rep->NewLine();
                        }
-                       if ($graphics)
-                       {
-                               $pg->x[] = $$typename[$level];
-                               $pg->y[] = abs($typeclose[$level]);
-                       }
-                       $rep->NewLine();
+                       //$rep->NewLine();
                        if ($closeclass)
                        {
                                $calculateopen = -$assetsopen - $classopen;
index d562218b5d5e2896cef02b6f13db6e3fbaffe06e..7799ffeaf573bd88ea98d12f3457c01617ddcfa0 100644 (file)
@@ -145,28 +145,23 @@ function print_profit_and_loss_statement()
        $typeper = array(0,0,0,0,0,0,0,0,0,0);
        $typeacc = array(0,0,0,0,0,0,0,0,0,0);
        $typename = array('','','','','','','','','','');
-       $parent = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
+       $closing = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
        $level = 0;
 
        $classper = 0.0;
        $classacc = 0.0;
        $salesper = 0.0;
        $salesacc = 0.0;
+       $last = -1;
 
-       $accounts = get_gl_accounts_all(0);
+       $types = get_account_types_all(0);
 
-       while ($account=db_fetch($accounts))
+       while ($type=db_fetch($types))
        {
-               $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
-
-               if ($compare == 2)
-                       $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
-               else
-                       $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
-               if (!$per_balance && !$acc_balance)
+               if (!num_accounts_in_type($type['AccountType'], $type['parent']))
                        continue;
 
-               if ($account['AccountClassName'] != $classname)
+               if ($type['AccountClassName'] != $classname)
                {
                        if ($classname != '')
                        {
@@ -174,31 +169,34 @@ function print_profit_and_loss_statement()
                        }
                }
 
-               if ($account['AccountTypeName'] != $typename[$level])
+               if ($type['AccountTypeName'] != $typename[$level])
                {
-                       if ($typename[$level] != '' && $account['parent'] == -1)
+                       if ($typename[$level] != '')
                        {
-                               for ( ; $level >= 0; $level--)
+                               for ( ; $level >= 0, $typename[$level] != ''; $level--) 
                                {
-                                       $rep->row += 6;
-                                       $rep->Line($rep->row);
-                                       $rep->NewLine();
-                                       $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
-                                       $rep->AmountCol(2, 3, $typeper[$level], $dec);
-                                       $rep->AmountCol(3, 4, $typeacc[$level], $dec);
-                                       $rep->AmountCol(4, 5, Achieve($typeper[$level], $typeacc[$level]), $pdec);
-                                       $typeper[$level] = $typeacc[$level] = 0.0;
-                                       if ($parent[$level] == -1)
+                                       if ($type['parent'] == $closing[$level] || $type['parent'] == $last || $type['parent'] <= 0)
+                                       {
+                                               $rep->row += 6;
+                                               $rep->Line($rep->row);
+                                               $rep->NewLine();
+                                               $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
+                                               $rep->AmountCol(2, 3, $typeper[$level], $dec);
+                                               $rep->AmountCol(3, 4, $typeacc[$level], $dec);
+                                               $rep->AmountCol(4, 5, Achieve($typeper[$level], $typeacc[$level]), $pdec);
+                                               if ($graphics)
+                                               {
+                                                       $pg->x[] = $typename[$level];
+                                                       $pg->y[] = abs($typeper[$level]);
+                                                       $pg->z[] = abs($typeacc[$level]);
+                                               }
+                                               $typeper[$level] = $typeacc[$level] = 0.0;
+                                       }
+                                       else
                                                break;
                                        $rep->NewLine();
                                }
-                               if ($graphics)
-                               {
-                                       $pg->x[] = $typename[$level];
-                                       $pg->y[] = abs($typeper[$level]);
-                                       $pg->z[] = abs($typeacc[$level]);
-                               }
-                               $rep->NewLine();
+                               //$rep->NewLine();
                                if ($closeclass)
                                {
                                        $rep->row += 6;
@@ -217,82 +215,98 @@ function print_profit_and_loss_statement()
                                        $closeclass = false;
                                }
                        }
-                       if ($account['AccountClassName'] != $classname)
+                       if ($type['AccountClassName'] != $classname)
                        {
                                $rep->Font('bold');
-                               $rep->TextCol(0, 5, $account['AccountClassName']);
+                               $rep->TextCol(0, 5, $type['AccountClassName']);
                                $rep->Font();
                                $rep->NewLine();
                        }
-                       if ($account['parent'] != -1 && $account['parent'] != $parent[$level])
-                               $level++;
-                       $typename[$level] = $account['AccountTypeName'];
+                       $level++;
+                       if ($type['parent'] != $last)
+                               $last = $type['parent'];
+                       $typename[$level] = $type['AccountTypeName'];
+                       $closing[$level] = $type['parent'];
                        $rep->row -= 4;
-                       $rep->TextCol(0, 5, $account['AccountTypeName']);
+                       $rep->TextCol(0, 5, $type['AccountTypeName']);
                        $rep->row -= 4;
                        $rep->Line($rep->row);
                        $rep->NewLine();
                }
-               $classname = $account['AccountClassName'];
-               $parent[$level] = $account['parent'];   
+               $classname = $type['AccountClassName'];
 
-               $per_balance *= -1;
-               $acc_balance *= -1;
-               
-               for ($i = 0; $i <= $level; $i++)
+               $accounts = get_gl_accounts_in_type($type['AccountType']);
+               while ($account=db_fetch($accounts))
                {
-                       $typeper[$i] += $per_balance;
-                       $typeacc[$i] += $acc_balance;
-               }
-               $classper += $per_balance;
-               $classacc += $acc_balance;
-               $rep->TextCol(0, 1,     $account['account_code']);
-               $rep->TextCol(1, 2,     $account['account_name']);
+                       $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
+
+                       if ($compare == 2)
+                               $acc_balance = get_budget_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
+                       else
+                               $acc_balance = get_gl_trans_from_to($begin, $end, $account["account_code"], $dimension, $dimension2);
+                       if (!$per_balance && !$acc_balance)
+                               continue;
+                       $per_balance *= -1;
+                       $acc_balance *= -1;
+               
+                       for ($i = 0; $i <= $level; $i++)
+                       {
+                               $typeper[$i] += $per_balance;
+                               $typeacc[$i] += $acc_balance;
+                       }
+                       $classper += $per_balance;
+                       $classacc += $acc_balance;
+                       $rep->TextCol(0, 1,     $account['account_code']);
+                       $rep->TextCol(1, 2,     $account['account_name']);
 
-               $rep->AmountCol(2, 3, $per_balance, $dec);
-               $rep->AmountCol(3, 4, $acc_balance, $dec);
-               $rep->AmountCol(4, 5, Achieve($per_balance, $acc_balance), $pdec);
+                       $rep->AmountCol(2, 3, $per_balance, $dec);
+                       $rep->AmountCol(3, 4, $acc_balance, $dec);
+                       $rep->AmountCol(4, 5, Achieve($per_balance, $acc_balance), $pdec);
 
-               $rep->NewLine();
+                       $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)
+       if ($type['AccountClassName'] != $classname)
        {
                if ($classname != '')
                {
                        $closeclass = true;
                }
        }
-       if ($account['AccountTypeName'] != $typename[$level])
+       if ($type['AccountTypeName'] != $typename[$level])
        {
                if ($typename[$level] != '')
                {
-                       for ( ; $level >= 0; $level--)
+                       for ( ; $level >= 0, $typename[$level] != ''; $level--) 
                        {
-                               $rep->row += 6;
-                               $rep->Line($rep->row);
-                               $rep->NewLine();
-                               $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
-                               $rep->AmountCol(2, 3, $typeper[$level], $dec);
-                               $rep->AmountCol(3, 4, $typeacc[$level], $dec);
-                               $rep->AmountCol(4, 5, Achieve($typeper[$level], $typeacc[$level]), $pdec);
-                               $typeper[$level] = $typeacc[$level] = 0.0;
-                               if ($parent[$level] == -1)
+                               if ($type['parent'] == $closing[$level] || $type['parent'] == $last || $type['parent'] <= 0)
+                               {
+                                       $rep->row += 6;
+                                       $rep->Line($rep->row);
+                                       $rep->NewLine();
+                                       $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
+                                       $rep->AmountCol(2, 3, $typeper[$level], $dec);
+                                       $rep->AmountCol(3, 4, $typeacc[$level], $dec);
+                                       $rep->AmountCol(4, 5, Achieve($typeper[$level], $typeacc[$level]), $pdec);
+                                       if ($graphics)
+                                       {
+                                               $pg->x[] = $typename[$level];
+                                               $pg->y[] = abs($typeper[$level]);
+                                               $pg->z[] = abs($typeacc[$level]);
+                                       }
+                                       $typeper[$level] = $typeacc[$level] = 0.0;
+                               }
+                               else
                                        break;
                                $rep->NewLine();
                        }
-                       if ($graphics)
-                       {
-                               $pg->x[] = $typename[$level];
-                               $pg->y[] = abs($typeper[$level]);
-                               $pg->z[] = abs($typeacc[$level]);
-                       }
-                       $rep->NewLine();
+                       //$rep->NewLine();
                        if ($closeclass)
                        {
                                $rep->Line($rep->row + 6);