Changed Balance Sheets/PL Statements to be recursive
authorJoe Hunt <joe.hunt.consulting@gmail.com>
Sat, 6 Feb 2010 14:56:44 +0000 (14:56 +0000)
committerJoe Hunt <joe.hunt.consulting@gmail.com>
Sat, 6 Feb 2010 14:56:44 +0000 (14:56 +0000)
CHANGELOG.txt
gl/inquiry/balance_sheet.php
gl/inquiry/profit_loss.php
reporting/rep701.php
reporting/rep705.php
reporting/rep706.php
reporting/rep707.php

index d6b10a90bb0c62c93eac28ec42862d579a731f4d..1cc580da7067d2c09f8feccda504901ad63e9a8e 100644 (file)
@@ -19,6 +19,15 @@ Legend:
 ! -> Note
 $ -> Affected files
 
+06-Feb-2010 Joe Hunt/Chaitanya
+! Changed Balance Sheets/PL Statements to be recursive
+$ /gl/inquiry/balance_sheet.php
+  /gl/inquiry/profit_loss.php
+  /reporting/rep701.php
+  /reporting/rep705.php
+  /reporting/rep706.php
+  /reporting/rep707.php
+  
 05-Feb-2010 Joe Hunt/Kalido
 + Placing a print link in all view windows (without menus).
 $ /includes/main.inc
index a550ebc8e67c8c77f3799d4f342b5706d8eb07bb..c08183d74d5a47d915d628087795c1b60ce03af9 100644 (file)
@@ -43,43 +43,77 @@ if (isset($_GET["AccGrp"]))
 
 //----------------------------------------------------------------------------------------------------
 
-function is_of_account_type($accttype,$typeslist)
+function display_type ($type, $typename, $from, $to, $convert, $drilldown, $path_to_root)
 {
-       return in_array($accttype, $typeslist);
-}
-
-function get_child_account_types($acctype)
-{
-       global $parentsarr;
-       $parentsarr = array();
-       $childernsarr = array();
-       $list = '';
-       array_push($parentsarr, $acctype);
-    while (sizeof($parentsarr)>0)
-    {
-               $parent = array_pop($parentsarr);
-               array_push($childernsarr,$parent);
-               pushchilds($parent);
+       global $levelptr, $k;
+       
+       $dimension = $dimension2 = 0;
+       $acctstotal = 0;
+       $typestotal = 0;
+       
+       //Get Accounts directly under this group/type
+       $result = get_gl_accounts(null, null, $type);   
+               
+       while ($account=db_fetch($result))
+       {
+               $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)
+                       continue;
+               
+               if ($drilldown && $levelptr == 0)
+               {
+                       $url = "<a href='$path_to_root/gl/inquiry/gl_account_inquiry.php?TransFromDate=" 
+                               . $from . "&TransToDate=" . $to 
+                               . "&account=" . $account['account_code'] . "'>" . $account['account_code'] 
+                               ." ". $account['account_name'] ."</a>";                         
+                               
+                       start_row("class='stockmankobg'");
+                       label_cell($url);
+                       amount_cell(($curr_balance + $prev_balance) * $convert);
+                       end_row();
+               }
+               
+               $acctstotal += $curr_balance + $prev_balance;
+       }
+       
+       $levelptr = 1;
+
+       //Get Account groups/types under this group/type
+       $result = get_account_types(false, false, $type);
+       while ($accounttype=db_fetch($result))
+       {                       
+               $typestotal += display_type($accounttype["id"], $accounttype["name"], $from, $to, 
+                       $convert, $drilldown, $path_to_root);   
        }
-       $list = substr($list,0,-1);
-       return $childernsarr;
-}
-
-function pushchilds($parent)
-{
-       global $parentsarr;
 
-       $sql = "SELECT id FROM  ".TB_PREF."chart_types WHERE parent=".$parent;
-       $result = db_query($sql,"Query failed");   
-       while ($myrow=db_fetch($result))
+       //Display Type Summary if total is != 0  
+       if (($acctstotal + $typestotal) != 0)
        {
-               array_push($parentsarr, $myrow['id']);
+               if ($drilldown && $type == $_POST["AccGrp"])
+               {               
+                       start_row("class='inquirybg' style='font-weight:bold'");
+                       label_cell(_('Total') . " " . $typename);
+                       amount_cell(($acctstotal + $typestotal) * $convert);
+                       end_row();
+               }
+               elseif ($drilldown && $type != $_POST["AccGrp"])
+               {
+                       $url = "<a href='$path_to_root/gl/inquiry/balance_sheet.php?TransFromDate=" 
+                               . $from . "&TransToDate=" . $to 
+                               . "&AccGrp=" . $type ."'>" . $typename ."</a>";
+                               
+                       alt_table_row_color($k);
+                       label_cell($url);
+                       amount_cell(($acctstotal + $typestotal) * $convert);
+                       end_row();
+               }
        }
-}
-
+       return ($acctstotal + $typestotal);
+}      
+       
 function inquiry_controls()
 {
-   
     start_table("class='tablestyle_noborder'");
        date_cells(_("As at:"), 'TransToDate');
        submit_cells('Show',_("Show"),'','', 'default');
@@ -87,10 +121,9 @@ function inquiry_controls()
 
        hidden('TransFromDate');
        hidden('AccGrp');
-
 }
 
-function print_balance_sheet()
+function display_balance_sheet()
 {
        global $comp_path, $path_to_root, $table_style;
        
@@ -98,249 +131,113 @@ function print_balance_sheet()
        $to = $_POST['TransToDate'];
        
        $dim = get_company_pref('use_dimension');
-       $dimension = $dimension2 = 0;   
-       
-       $classname = '';
-       $classopen = 0.0;
-       $classperiod = 0.0;
-       $classclose = 0.0;
-       $assetsopen = 0.0;
-       $assetsperiod = 0.0;
-       $assetsclose = 0.0;
-       $equityopen = 0.0;
-       $equityperiod = 0.0;
-       $equityclose = 0.0;
-       $lopen = 0.0;
-       $lperiod = 0.0;
-       $lclose = 0.0;
-       
-       $typeopen = array(0,0,0,0,0,0,0,0,0,0);
-       $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('','','','','','','','','','');
-       $acctype = array('','','','','','','','','','');
-       $closing = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
-       //$parent = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
-       $level = 0;
-       $last = -1;
-       
-       $closeclass = false;
-       $ctype = 0;
-       $convert = 1;   
+       $dimension = $dimension2 = 0;
+       $lconvert = $econvert = 1;
+       if (isset($_POST["AccGrp"]) && (strlen($_POST['AccGrp']) > 0))
+               $drilldown = 1; // Deeper Level
+       else
+               $drilldown = 0; // Root level   
 
-       $accounts = get_gl_accounts_all(1);
-       
        div_start('balance_tbl');
-
-       start_table("width=30% $table_style");
        
-       while ($account=db_fetch($accounts))
-       {
-               if ($account['account_code'] == null && $account['parent'] > 0)
-                       continue;
-
-               //Check for confirming the account type
-               if (isset($_POST['AccGrp']) && (strlen($_POST['AccGrp']) > 0) )
+       start_table("width=30% $table_style");                  
+               
+       if (!$drilldown) //Root Level
+       {               
+               $equityclose = 0.0;
+               $lclose = 0.0; 
+               $calculateclose = 0.0;          
+
+               $parent = -1;
+
+               //Get classes for BS
+               $classresult = get_account_classes(false, 1);
+       
+               while ($class = db_fetch($classresult))
                {       
-                       $sub_types_arr = get_child_account_types($_POST['AccGrp']);
-                       if (!is_of_account_type($account['AccountType'], $sub_types_arr))
-                               continue;
-               }
-                                                       
-               if ($account['account_code'] != null)
-               {
-                       $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);
+                       $classclose = 0.0;
+                       $convert = get_class_type_convert($class["ctype"]);             
+                       $ctype = $class["ctype"];
+                       $classname = $class["class_name"];      
 
-                       if (!$prev_balance && !$curr_balance)
-                               continue;
-               }
-               if ($account['AccountClassName'] != $classname)
-               {
-                       if ($classname != '')
-                       {
-                               $closeclass = true;
-                       }
-               }
-               if ($account['AccountTypeName'] != $typename[$level])
-               {
-                       if ($typename[$level] != '')
+                       //Print Class Name      
+                       table_section_title($class["class_name"]);
+                       
+                       //Get Account groups/types under this group/type
+                       $typeresult = get_account_types(false, $class['cid'], -1);
+                               
+                       while ($accounttype=db_fetch($typeresult))
                        {
-                               for ( ; $level >= 0, $typename[$level] != ''; $level--) 
+                               $TypeTotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, 
+                                               $convert, $drilldown, $path_to_root);   
+                               //Print Summary 
+                               if ($TypeTotal != 0 )
                                {
-                                       if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0 || $closeclass)
-                                       {       
-                                       
-                                               $url = "<a href='$path_to_root/gl/inquiry/balance_sheet.php?TransFromDate=" 
-                                                       . $from . "&TransToDate=" . $to 
-                                                       . "&AccGrp=" . $acctype[$level] . "'>" . $typename[$level] . "</a>";
-                                                                               
-                                               alt_table_row_color($k);
-                                               label_cell($url);
-                                               amount_cell($typeclose[$level] * $convert);
-                                               end_row();                                              
-
-                                               $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
-                                       }       
-                                       else
-                                               break;
-                               }
-
-                               if ($closeclass)
-                               {       
-                                       start_row("class='inquirybg' style='font-weight:bold'");
-                                       label_cell(_('Total') . " " . $classname);
-                                       amount_cell($classclose * $convert, true);
-                                       end_row();                                              
-                                                               
-                                       if ($ctype == CL_EQUITY)
-                                       {
-                                               $equityopen += $classopen;
-                                               $equityperiod += $classperiod;
-                                               $equityclose += $classclose;
-                                       }
-                                       if ($ctype == CL_LIABILITIES)
-                                       {
-                                               $lopen += $classopen;
-                                               $lperiod += $classperiod;
-                                               $lclose += $classclose;
-                                       }
-                                       $assetsopen += $classopen;
-                                       $assetsperiod += $classperiod;
-                                       $assetsclose += $classclose;
-                                       $classopen = $classperiod = $classclose = 0.0;
-
-                                       $closeclass = false;
+                                       $url = "<a href='$path_to_root/gl/inquiry/balance_sheet.php?TransFromDate=" 
+                                               . $from . "&TransToDate=" . $to . "&AccGrp=" . $accounttype['id'] ."'>" . $accounttype['name'] ."</a>"; 
+                                       alt_table_row_color($k);
+                                       label_cell($url);
+                                       amount_cell($TypeTotal * $convert);
+                                       end_row();
                                }
-                       }
-                       if ($account['AccountClassName'] != $classname)
-                       {
-                               if (isset($_POST['AccGrp']) && (strlen($_POST['AccGrp']) > 0))
-                                       table_section_title($account['AccountTypeName']);
-                               else 
-                                       table_section_title($account['AccountClassName']);
-                       }
-                       $level++;
-                       if ($account['parent'] != $last)
-                               $last = $account['parent'];
-                       $typename[$level] = $account['AccountTypeName'];
-                       
-                       $acctype[$level] = $account['AccountType'];
+                               $classclose += $TypeTotal;
+                       }                               
+
+                       //Print Class Summary
+                       start_row("class='inquirybg' style='font-weight:bold'");
+                       label_cell(_('Total') . " " . $class["class_name"]);
+                       amount_cell($classclose * $convert);
+                       end_row();              
                        
-                       $closing[$level] = $account['parent'];
-
-               }
-               $classname = $account['AccountClassName'];
-               $classtype = $account['AccountType'];
-               $ctype = $account['ClassType'];
-               $convert = get_class_type_convert($ctype); 
-
-               if ($account['account_code'] != null)
-               {
-                       for ($i = 0; $i <= $level; $i++)
+                       if ($ctype == CL_EQUITY)
                        {
-                               $typeopen[$i] += $prev_balance;
-                               $typeperiod[$i] += $curr_balance;
-                               $typeclose[$i] = $typeopen[$i] + $typeperiod[$i];
+                               $equityclose += $classclose;
+                               $econvert = $convert;
                        }
-                       $classopen += $prev_balance;
-                       $classperiod += $curr_balance;
-                       $classclose = $classopen + $classperiod;
-
-                       //Show accounts details only for drill down and direct child of Account Group
-                       if ( isset($_POST['AccGrp']) && ($account['AccountType'] == $_POST['AccGrp']))
+                       if ($ctype == CL_LIABILITIES)
                        {
-                               $url = "<a href='$path_to_root/gl/inquiry/gl_account_inquiry.php?TransFromDate=" 
-                                       . $from . "&TransToDate=" . $to 
-                                       . "&account=" . $account['account_code'] . "'>" . $account['account_code'] 
-                                       ." ". $account['account_name'] ."</a>";                         
-                                       
-                               start_row("class='stockmankobg'");
-                               label_cell($url);
-                               amount_cell(($curr_balance + $prev_balance) * $convert);
-                               end_row();
+                               $lclose += $classclose;
+                               $lconvert = $convert;
                        }
-               }       
-       }
        
-       if ($account['AccountClassName'] != $classname)
-       {
-               if ($classname != '')
-               {
-                       $closeclass = true;
+                       $calculateclose += $classclose;
                }
+               
+               if ($lconvert == 1)
+                       $calculateclose *= -1;
+               //Final Report Summary
+               $url = "<a href='$path_to_root/gl/inquiry/profit_loss.php?TransFromDate=" 
+                               . $from."&TransToDate=".$to
+                       ."&Compare=0'>"._('Calculated Return')."</a>";          
+               
+               start_row("class='inquirybg' style='font-weight:bold'");
+               label_cell($url);
+               amount_cell($calculateclose);
+               end_row();              
+               
+               start_row("class='inquirybg' style='font-weight:bold'");
+               label_cell(_('Total') . " " . _('Liabilities') . _(' and ') . _('Equities'));
+               amount_cell($lclose * $lconvert + $equityclose * $econvert + $calculateclose);
+               end_row();
        }
-       if ($account['AccountTypeName'] != $typename[$level])
+       else //Drill Down
        {
-
-               if ($typename[$level] != '')
-               {
-                       for ( ; $level >= 0, $typename[$level] != ''; $level--) 
-                       {
-                               if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0 || $closeclass)
-                               {
-                                       //Inside drill down, no hyperlink
-                                       if (isset($_POST['AccGrp']) && (strlen($_POST['AccGrp']) > 0) && ($acctype[$level] == $_POST['AccGrp']))
-                                       {
-                                               start_row("class='inquirybg' style='font-weight:bold'");
-                                               label_cell(_('Total') . " " .$typename[$level]);
-                                               amount_cell($typeclose[$level] * $convert);
-                                               end_row();                                                      
-                                       }
-                                       else
-                                       {
-                                               $url = "<a href='$path_to_root/gl/inquiry/balance_sheet.php?TransFromDate=" 
-                                                       . $from . "&TransToDate=" . $to 
-                                                       . "&AccGrp=" . $acctype[$level] . "'>" . $typename[$level] . "</a>";                                    
-                                               
-                                               alt_table_row_color($k);
-                                               label_cell($url);
-                                               amount_cell($typeclose[$level] * $convert);
-                                               end_row();                                                      
-                                       }
-                                       
-                                       $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;                                     
-                                       
-                               }
-                               else
-                                       break;
-
-                       }
-
-                       if (($closeclass) && !(isset($_POST['AccGrp']) && (strlen($_POST['AccGrp']) > 0)) )
-                       {
-                               $calculateopen = -$assetsopen - $classopen;
-                               $calculateperiod = -$assetsperiod - $classperiod;
-                               $calculateclose = -$assetsclose  - $classclose;
-                               if ($ctype == CL_EQUITY)
-                               {
-                                       $equityopen += $classopen;
-                                       $equityperiod += $classperiod;
-                                       $equityclose += $classclose;
-                               }
-                       
-                               alt_table_row_color($k);
-                               label_cell(_('Calculated Return'));
-                               amount_cell($calculateclose * $convert);
-                               end_row();      
-                                                       
-                               start_row("class='inquirybg' style='font-weight:bold'");
-                               label_cell(_('Total') . " " . $classname);
-                               amount_cell(-$assetsclose * $convert);
-                               end_row();                                      
-                               
-                               if ($equityopen != 0.0 || $equityperiod != 0.0 || $equityclose != 0.0 ||
-                                       $lopen != 0.0 || $lperiod != 0.0 || $lclose != 0.0)
-                               {
-                                       alt_table_row_color($k);
-                                       label_cell(_('Total') . " " . _('Liabilities') . _(' and ') . _('Equities'));
-                                       amount_cell(($lclose + $equityclose + $calculateclose) * -1);
-                                       end_row();      
-                               }
-                       }
-               }
+               //Level Pointer : Global variable defined in order to control display of root 
+               global $levelptr;
+               $levelptr = 0;
+               
+               $accounttype = get_account_type($_POST["AccGrp"]);
+               $classid = $accounttype["class_id"];
+               $class = get_account_class($classid);
+               $convert = get_class_type_convert($class["ctype"]); 
+               
+               //Print Class Name      
+               table_section_title(get_account_type_name($_POST["AccGrp"]));   
+               
+               $classclose = display_type($accounttype["id"], $accounttype["name"], $from, $to, 
+                       $convert, $drilldown, $path_to_root);
        }
+       
        end_table(1); // outer table
        div_end();
 }
@@ -351,7 +248,7 @@ start_form();
 
 inquiry_controls();
 
-print_balance_sheet();
+display_balance_sheet();
 
 end_form();
 
index 7254ea4e763fbb41c62d23fb9c1b9e01be2b6908..f7e6ea6958a914ec8bb7d664adf7602227e4deee 100644 (file)
@@ -39,49 +39,101 @@ if (isset($_GET["TransFromDate"]))
 if (isset($_GET["TransToDate"]))
        $_POST["TransToDate"] = $_GET["TransToDate"];
 if (isset($_GET["Compare"]))
-       $_POST["Compare"] = $_GET["Compare"];   
+       $_POST["Compare"] = $_GET["Compare"];
 if (isset($_GET["AccGrp"]))
-       $_POST["AccGrp"] = $_GET["AccGrp"];     
-if (isset($_GET["DrillLevel"]))
-       $_POST["DrillLevel"] = $_GET["DrillLevel"];     
-else   
-       $_POST["DrillLevel"] = 1; // Root level
-       
+       $_POST["AccGrp"] = $_GET["AccGrp"];
+
 //----------------------------------------------------------------------------------------------------
 
-function is_of_account_type($accttype,$typeslist)
+function display_type ($type, $typename, $from, $to, $begin, $end, $compare, $convert,
+       &$dec, &$pdec, &$rep, $dimension=0, $dimension2=0, $drilldown, $path_to_root)
 {
-       return in_array($accttype, $typeslist);
-}
+       global $levelptr, $k;
+               
+       $code_per_balance = 0;
+       $code_acc_balance = 0;
+       $per_balance_total = 0;
+       $acc_balance_total = 0;
+       unset($totals_arr);
+       $totals_arr = array();
+       
+       //Get Accounts directly under this group/type
+       $result = get_gl_accounts(null, null, $type);   
+               
+       while ($account=db_fetch($result))
+       {
+               $per_balance = get_gl_trans_from_to($from, $to, $account["account_code"], $dimension, $dimension2);
 
-function get_child_account_types($acctype)
-{
-       global $parentsarr;
-       $parentsarr = array();
-       $childernsarr = array();
-       $list = '';
-       array_push($parentsarr, $acctype);
-    while (sizeof($parentsarr)>0)
-    {
-               $parent = array_pop($parentsarr);
-               array_push($childernsarr,$parent);
-               pushchilds($parent);
+               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;
+               
+               if ($drilldown && $levelptr == 0)
+               {
+                       $url = "<a href='$path_to_root/gl/inquiry/gl_account_inquiry.php?TransFromDate=" 
+                               . $from . "&TransToDate=" . $to 
+                               . "&account=" . $account['account_code'] . "'>" . $account['account_code'] 
+                               ." ". $account['account_name'] ."</a>";                         
+                               
+                       start_row("class='stockmankobg'");
+                       label_cell($url);
+                       amount_cell($per_balance * $convert);
+                       amount_cell($acc_balance * $convert);
+                       amount_cell(Achieve($per_balance, $acc_balance));
+                       end_row();
+               }
+                       
+               $code_per_balance += $per_balance;
+               $code_acc_balance += $acc_balance;
        }
-       $list = substr($list,0,-1);
-       return $childernsarr;
-}
 
-function pushchilds($parent)
-{
-       global $parentsarr;
+       $levelptr = 1;
+       
+       //Get Account groups/types under this group/type
+       $result = get_account_types(false, false, $type);
+       while ($accounttype=db_fetch($result))
+       {       
+               $totals_arr = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, 
+                       $compare, $convert, $dec, $pdec, $rep, $dimension, $dimension2, $drilldown, $path_to_root);
+               $per_balance_total += $totals_arr[0];
+               $acc_balance_total += $totals_arr[1];
+       }
 
-       $sql = "SELECT id FROM  ".TB_PREF."chart_types WHERE parent=".$parent;
-       $result = db_query($sql,"Query failed");   
-       while ($myrow=db_fetch($result))
+       //Display Type Summary if total is != 0 
+       if (($code_per_balance + $per_balance_total + $code_acc_balance + $acc_balance_total) != 0)
        {
-               array_push($parentsarr, $myrow['id']);
+               if ($drilldown && $type == $_POST["AccGrp"])
+               {               
+                       start_row("class='inquirybg' style='font-weight:bold'");
+                       label_cell(_('Total') . " " . $typename);
+                       amount_cell(($code_per_balance + $per_balance_total) * $convert);
+                       amount_cell(($code_acc_balance + $acc_balance_total) * $convert);
+                       amount_cell(Achieve(($code_per_balance + $per_balance_total), ($code_acc_balance + $acc_balance_total)));
+                       end_row();
+               }
+               elseif ($drilldown && $type != $_POST["AccGrp"])
+               {
+                       $url = "<a href='$path_to_root/gl/inquiry/profit_loss.php?TransFromDate=" 
+                               . $from . "&TransToDate=" . $to . "&Compare=" . $compare
+                               . "&AccGrp=" . $type ."'>" . $typename ."</a>";
+                               
+                       alt_table_row_color($k);
+                       label_cell($url);
+                       amount_cell(($code_per_balance + $per_balance_total) * $convert);
+                       amount_cell(($code_acc_balance + $acc_balance_total) * $convert);
+                       amount_cell(Achieve(($code_per_balance + $per_balance_total), ($code_acc_balance + $acc_balance_total)));
+                       end_row();
+               }
        }
-}
+       
+       $totals_arr[0] = $code_per_balance + $per_balance_total;
+       $totals_arr[1] = $code_acc_balance + $acc_balance_total;
+       return $totals_arr;
+}      
+       
 function Achieve($d1, $d2)
 {
        if ($d1 == 0 && $d2 == 0)
@@ -117,7 +169,7 @@ function inquiry_controls()
 
 //----------------------------------------------------------------------------------------------------
 
-function print_profit_and_loss()
+function display_profit_and_loss()
 {
        global $comp_path, $path_to_root, $table_style, $sel;
 
@@ -127,9 +179,12 @@ function print_profit_and_loss()
        $from = $_POST['TransFromDate'];
        $to = $_POST['TransToDate'];
        $compare = $_POST['Compare'];
-       $drilllevel = $_POST["DrillLevel"];
-       $nextDrillLevel = $drilllevel + 1;
-
+       
+       if (isset($_POST["AccGrp"]) && (strlen($_POST['AccGrp']) > 0))
+               $drilldown = 1; // Deeper Level
+       else
+               $drilldown = 0; // Root level
+       
        $dec = 0;
        $pdec = user_percent_dec();
 
@@ -148,31 +203,6 @@ function print_profit_and_loss()
                $begin = add_months($from, -12);
                $end = add_months($to, -12);
        }
-
-       $classname = '';
-       
-       $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('','','','','','','','','','');
-       $acctype = array('','','','','','','','','','');        
-       $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;
-
-       $closeclass = false;
-       $convert = 1;
-       $ctype = 0;
-
-       //For drill down, prepare the list of sub account types
-       if (isset($_POST['AccGrp']) && (strlen($_POST['AccGrp']) > 0) )
-               $sub_types_arr = get_child_account_types($_POST['AccGrp']);     
-               
-       $accounts = get_gl_accounts_all(0);
        
        div_start('pl_tbl');
 
@@ -183,203 +213,94 @@ function print_profit_and_loss()
         <td class='tableheader'>" . _("Period") . "</td>
                <td class='tableheader'>" . $sel[$compare] . "</td>
                <td class='tableheader'>" . _("Achieved %") . "</td>
-        </tr>";
-
-       while ($account=db_fetch($accounts))
+        </tr>";        
+       
+       if (!$drilldown) //Root Level
        {
-               if ($account['account_code'] == null && $account['parent'] > 0)
-                       continue;                       
-                       
-               //Check for confirming the account type
-               if (isset($_POST['AccGrp']) && (strlen($_POST['AccGrp']) > 0) )
-               {       
-                       if (!is_of_account_type($account['AccountType'], $sub_types_arr))
-                               continue;
-               }                       
-
-               if ($account['account_code'] != null)
-               {
-                       $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;
-               }
-
-               if ($account['AccountClassName'] != $classname)
+               $parent = -1;
+               $classper = 0.0;
+               $classacc = 0.0;
+               $salesper = 0.0;
+               $salesacc = 0.0;        
+       
+               //Get classes for PL
+               $classresult = get_account_classes(false, 0);
+               while ($class = db_fetch($classresult))
                {
-                       if ($classname != '')
-                       {
-                               $closeclass = true;
-                       }
-               }
-               
-               if ($account['AccountTypeName'] != $typename[$level] || $closeclass )
-               {                               
-                       if ($typename[$level] != '')
+                       $class_per_total = 0;
+                       $class_acc_total = 0;
+                       $convert = get_class_type_convert($class["ctype"]);             
+                       
+                       //Print Class Name      
+                       table_section_title($class["class_name"],4);    
+                       echo $tableheader;
+                       
+                       //Get Account groups/types under this group/type
+                       $typeresult = get_account_types(false, $class['cid'], -1);
+                       while ($accounttype=db_fetch($typeresult))
                        {
-                               for ( ; $level >= 0, $typename[$level] != ''; $level--) 
+                               $TypeTotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert, 
+                                       $dec, $pdec, $rep, $dimension, $dimension2, $drilldown, $path_to_root);
+                               $class_per_total += $TypeTotal[0];
+                               $class_acc_total += $TypeTotal[1];      
+
+                               if ($TypeTotal[0] != 0 || $TypeTotal[1] != 0 )
                                {
-                                       if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0 || $closeclass)
-                                       {       
-                                               // Display Groups only of Specific Drill Level
-                                               if ($level == $drilllevel && ($typeper[$level] != 0 || $typeacc[$level] != 0 )) 
-                                               {
-                                                       $url = "<a href='$path_to_root/gl/inquiry/profit_loss.php?TransFromDate=" 
-                                                               . $from . "&TransToDate=" . $to . "&Compare=" . $compare
-                                                               . "&AccGrp=" . $acctype[$level] . "&DrillLevel=".$nextDrillLevel."'>" . $typename[$level] ."</a>";
-                                                                                       
-                                                       alt_table_row_color($k);
-                                                       label_cell($url);
-                                                       amount_cell($typeper[$level] * $convert);
-                                                       amount_cell($typeacc[$level] * $convert);
-                                                       amount_cell(Achieve($typeper[$level], $typeacc[$level]));
-                                                       end_row();
-                                               }                                               
+                                       $url = "<a href='$path_to_root/gl/inquiry/profit_loss.php?TransFromDate=" 
+                                               . $from . "&TransToDate=" . $to . "&Compare=" . $compare
+                                               . "&AccGrp=" . $accounttype['id'] ."'>" . $accounttype['name'] ."</a>";
                                                
-                                               $typeper[$level] = $typeacc[$level] = 0.0;
-                                       }
-                                       else
-                                               break;
+                                       alt_table_row_color($k);
+                                       label_cell($url);
+                                       amount_cell($TypeTotal[0] * $convert);
+                                       amount_cell($TypeTotal[1] * $convert);
+                                       amount_cell(Achieve($TypeTotal[0], $TypeTotal[1]));
+                                       end_row();
                                }
-                               if ($closeclass)
-                               {
-                                       start_row("class='inquirybg' style='font-weight:bold'");
-                                       label_cell(_('Total') . " " . $classname);
-                                       amount_cell($classper * $convert);
-                                       amount_cell($classacc * $convert);
-                                       amount_cell(Achieve($classper, $classacc));
-                                       end_row();                                      
-                                       
-                                       $salesper += $classper;
-                                       $salesacc += $classacc;
-                                       $classper = $classacc = 0.0;
-
-                                       $closeclass = false;
-                               }
-                       }
-                       if ($account['AccountClassName'] != $classname)
-                       {
-                               if (isset($_POST['AccGrp']) && (strlen($_POST['AccGrp']) > 0))
-                                       table_section_title($account['AccountTypeName'],4);
-                               else 
-                                       table_section_title($account['AccountClassName'],4);
-                                       
-                               echo $tableheader;
-                       }
-                       $level++;
-                       if ($account['parent'] != $last)
-                               $last = $account['parent'];
-                       $typename[$level] = $account['AccountTypeName'];
-                       $acctype[$level] = $account['AccountType'];
-                       $closing[$level] = $account['parent'];
-               }
-               $classname = $account['AccountClassName'];
-               $ctype = $account['ClassType'];
-               $convert = get_class_type_convert($ctype); 
-               
-               if ($account['account_code'] != null)
-               {
-                       //$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;              
                        
-                       //Show accounts details only for drill down and direct child of Account Group
-                       if ( isset($_POST['AccGrp']) && ($account['AccountType'] == $_POST['AccGrp']))
-                       {
-                               $url = "<a href='$path_to_root/gl/inquiry/gl_account_inquiry.php?TransFromDate=" 
-                                       . $from . "&TransToDate=" . $to 
-                                       . "&account=" . $account['account_code'] . "'>" . $account['account_code'] 
-                                       ." ". $account['account_name'] ."</a>";                         
-                                       
-                               start_row("class='stockmankobg'");
-                               label_cell($url);
-                               amount_cell($per_balance * $convert);
-                               amount_cell($acc_balance * $convert);
-                               amount_cell(Achieve($per_balance, $acc_balance));
-                               end_row();
-                       }                       
+                       //Print Class Summary
+                       
+                       start_row("class='inquirybg' style='font-weight:bold'");
+                       label_cell(_('Total') . " " . $class["class_name"]);
+                       amount_cell($class_per_total * $convert);
+                       amount_cell($class_acc_total * $convert);
+                       amount_cell(Achieve($class_per_total, $class_acc_total));
+                       end_row();                      
+                       
+                       $salesper += $class_per_total;
+                       $salesacc += $class_acc_total;
                }
+               
+               start_row("class='inquirybg' style='font-weight:bold'");
+               label_cell(_('Calculated Return'));
+               amount_cell($salesper *-1);
+               amount_cell($salesacc * -1);
+               amount_cell(achieve($salesper, $salesacc));
+               end_row();              
+
        }
-       if ($account['AccountClassName'] != $classname)
+       else 
        {
-               if ($classname != '')
-               {
-                       $closeclass = true;
-               }
+               //Level Pointer : Global variable defined in order to control display of root 
+               global $levelptr;
+               $levelptr = 0;
+               
+               $accounttype = get_account_type($_POST["AccGrp"]);
+               $classid = $accounttype["class_id"];
+               $class = get_account_class($classid);
+               $convert = get_class_type_convert($class["ctype"]); 
+               
+               //Print Class Name      
+               table_section_title(get_account_type_name($_POST["AccGrp"]),4); 
+               echo $tableheader;
+               
+               $classtotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $begin, $end, $compare, $convert, 
+                       $dec, $pdec, $rep, $dimension, $dimension2, $drilldown, $path_to_root);
+               
        }
-       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 || $closeclass)
-                               {
-                                       
-                                       //Inside drill down, Show summary of the specific account group
-                                       if (isset($_POST['AccGrp']) && (strlen($_POST['AccGrp']) > 0) && ($acctype[$level] == $_POST['AccGrp']))
-                                       {
-                                               start_row("class='inquirybg' style='font-weight:bold'");
-                                               label_cell(_('Total') . " " .$typename[$level]);
-                                               amount_cell($typeper[$level] * $convert);
-                                               amount_cell($typeacc[$level] * $convert);
-                                               amount_cell(Achieve($typeper[$level], $typeacc[$level]));
-                                               end_row();                                                      
-                                       }
-                                        // Display Groups only of Specific Drill Level
-                                       elseif ($level == $drilllevel  && ($typeper[$level] != 0 || $typeacc[$level] != 0 )) 
-                                       {
-                                               $url = "<a href='$path_to_root/gl/inquiry/profit_loss.php?TransFromDate=" 
-                                                       . $from . "&TransToDate=" . $to . "&Compare=" . $compare
-                                                       . "&AccGrp=" . $acctype[$level] . "&DrillLevel=".$nextDrillLevel."'>" . $typename[$level] ."</a>";
-                                                                               
-                                               alt_table_row_color($k);
-                                               label_cell($url);
-                                               amount_cell($typeper[$level] * $convert);
-                                               amount_cell($typeacc[$level] * $convert);
-                                               amount_cell(Achieve($typeper[$level], $typeacc[$level]));
-                                               end_row();
-                                       }                                       
-                                       
-                                       $typeper[$level] = $typeacc[$level] = 0.0;
-                               }
-                               else
-                                       break;
-                       }
+               
 
-                       if (($closeclass) && !(isset($_POST['AccGrp']) && (strlen($_POST['AccGrp']) > 0)) )
-                       {
-                               $calculateper = $salesper + $classper;
-                               $calculateacc = $salesacc + $classacc;
-
-                               start_row("class='inquirybg' style='font-weight:bold'");
-                               label_cell(_('Total') . " " . $classname);
-                               amount_cell($classper * $convert);
-                               amount_cell($classacc * $convert);
-                               amount_cell(Achieve($classper, $classacc));
-                               end_row();      
-                               
-                               start_row("class='inquirybg' style='font-weight:bold'");
-                               label_cell(_('Calculated Return'));
-                               amount_cell($calculateper *-1);
-                               amount_cell($calculateacc * -1);
-                               amount_cell(Achieve($calculateper, $calculateacc));
-                               end_row();                                      
-                               
-                       }
-               }
-       }
        end_table(1); // outer table
        div_end();
 }
@@ -390,11 +311,10 @@ start_form();
 
 inquiry_controls();
 
-print_profit_and_loss();
+display_profit_and_loss();
 
 end_form();
 
 end_page();
 
-?>
-
+?>
\ No newline at end of file
index fc7b8a43006f1149fb0f0b9b51abf576ef471811..82bb0390ddc8e8db1c3a460458258992d1422a99 100644 (file)
@@ -25,6 +25,62 @@ include_once($path_to_root . "/gl/includes/gl_db.inc");
 
 //----------------------------------------------------------------------------------------------------
 
+function display_type ($type, $typename, &$dec, &$rep, $showbalance)
+{
+       $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))
+       {
+               //Print Type Title if it has atleast one non-zero account       
+               if (!$printtitle)
+               {
+                       $printtitle = 1;
+                       $rep->row -= 4;
+                       $rep->TextCol(0, 1, $type);
+                       $rep->TextCol(1, 4, $typename);
+                       $rep->row -= 4;
+                       $rep->Line($rep->row);
+                       $rep->NewLine();                
+               }                       
+               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();
+       }
+               
+       //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, 1, $type);
+                       $rep->TextCol(1, 4, $typename);
+                       $rep->row -= 4;
+                       $rep->Line($rep->row);
+                       $rep->NewLine();                
+               }
+
+               display_type($accounttype["id"], $accounttype["name"], $dec, $rep, $showbalance);
+       }
+}
+
+//----------------------------------------------------------------------------------------------------
+
 print_Chart_of_Accounts();
 
 //----------------------------------------------------------------------------------------------------
@@ -57,61 +113,24 @@ function print_Chart_of_Accounts()
        $rep->Info($params, $cols, $headers, $aligns);
        $rep->Header();
 
-       $classname = '';
-       $group = '';
-
-       $accounts = get_gl_accounts_all();
-
-       while ($account=db_fetch($accounts))
+       $classresult = get_account_classes(false);
+       while ($class = db_fetch($classresult))
        {
-               if ($account['AccountTypeName'] != $group)
+               $rep->Font('bold');
+               $rep->TextCol(0, 1, $class['cid']);
+               $rep->TextCol(1, 4, $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))
                {
-                       if ($classname != '')
-                               $rep->row -= 4;
-                       if ($account['AccountClassName'] != $classname)
-                       {
-                               $rep->Font('bold');
-                               //$rep->TextCol(0, 4, $account['AccountClassName']);
-                               $rep->TextCol(0, 1, $account['ClassID']);
-                               $rep->TextCol(1, 4, $account['AccountClassName']);
-                               $rep->Font();
-                               //$rep->row -= ($rep->lineHeight + 4);
-                               $rep->NewLine();
-                       }
-                       $group = $account['AccountTypeName'];
-                       //$rep->TextCol(0, 4, $account['AccountTypeName']);
-                       $rep->TextCol(0, 1, $account['AccountType']);
-                       $rep->TextCol(1, 4, $account['AccountTypeName']);
-                       //$rep->Line($rep->row - 4);
-                       //$rep->row -= ($rep->lineHeight + 4);
-                       $rep->NewLine();
+                       display_type($accounttype["id"], $accounttype["name"], $dec, $rep, $showbalance);
                }
-               $classname = $account['AccountClassName'];
-               
-               if ($account['account_code'] != null)
-               {
-                       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->NewLine();
        }
-       $rep->Line($rep->row);
+       $rep->Line($rep->row + 10);
        $rep->End();
 }
 
index 607b446c1da5ddc916cc8cbaece65e3e10655c70..2c14360d8aa08920bebafec8f9cafb916f631bbc 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
 // ----------------------------------------------------------------
@@ -29,10 +29,8 @@ print_annual_expense_breakdown();
 
 //----------------------------------------------------------------------------------------------------
 
-function getPeriods($row, $account, $dimension, $dimension2)
+function getPeriods($yr, $mo, $account, $dimension, $dimension2)
 {
-       $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));
@@ -73,6 +71,90 @@ function getPeriods($row, $account, $dimension, $dimension2)
 
 //----------------------------------------------------------------------------------------------------
 
+function display_type ($type, $typename, $yr, $mo, $convert, &$dec, &$rep, $dimension, $dimension2)
+{
+       $ctotal = array(1 => 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);
+       $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))
+       {
+               $bal = getPeriods($yr, $mo, $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;
+       
+               //Print Type Title if it has atleast 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']);
+               $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);
+                       $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);
+               for ($i = 1; $i <= 12; $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 <= 12; $i++)
+                       $rep->AmountCol($i + 1, $i + 2, ($total[$i] + $ctotal[$i]) * $convert, $dec);
+               $rep->NewLine();
+       }
+       for ($i = 1; $i <= 12; $i++)
+               $totals_arr[$i] = $total[$i] + $ctotal[$i];     
+       return $totals_arr;
+}
+
+//----------------------------------------------------------------------------------------------------
+
 function print_annual_expense_breakdown()
 {
        global $path_to_root, $date_system;
@@ -183,196 +265,51 @@ function print_annual_expense_breakdown()
        $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))
+       $sales = Array(1 => 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 ($account['account_code'] == null && $account['parent'] > 0)
-                       continue;
-
-               if ($account['account_code'] != null)
+               $ctotal = Array(1 => 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))
                {
-                       $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;
-                       }
-               }
-
-               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 || $closeclass)
-                                       {
-                                               $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']);
-
+                       $classtotal = display_type($accounttype["id"], $accounttype["name"], $yr, $mo, $convert, $dec, $rep, $dimension, $dimension2);
                        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)
-       {
-               if ($classname != '')
-               {
-                       $closeclass = true;
+                               $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 <= 12; $i++)
                {
-                       for ( ; $level >= 0, $typename[$level] != ''; $level--) 
-                       {
-                               if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0 || $closeclass)
-                               {
-                                       $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 <= 12; $i++)
+               $rep->AmountCol($i + 1, $i + 2, $sales[$i] * -1, $dec);
+       $rep->Font();
+       $rep->NewLine();
        $rep->Line($rep->row);
+       $rep->NewLine(2);
        $rep->End();
 }
 
index 58d605992164e6dd5e9faf0bbf4196fcfb6ee09e..281c806b0fe424e6c2b19e103e4aa71c824c746a 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:      Balance Sheet
 // ----------------------------------------------------------------
@@ -25,8 +25,98 @@ include_once($path_to_root . "/gl/includes/gl_db.inc");
 
 //----------------------------------------------------------------------------------------------------
 
-print_balance_sheet();
+function display_type ($type, $typename, $from, $to, $convert, &$dec, &$rep, $dimension, $dimension2, &$pg, $graphics)
+{
+       $code_open_balance = 0;
+       $code_period_balance = 0;
+       $open_balance_total = 0;
+       $period_balance_total = 0;
+       unset($totals_arr);
+       $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))
+       {
+               $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)
+                       continue;
+               
+               //Print Type Title if it has atleast 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();                
+               }                       
 
+               $rep->TextCol(0, 1,     $account['account_code']);
+               $rep->TextCol(1, 2,     $account['account_name']);
+
+               $rep->AmountCol(2, 3, $prev_balance * $convert, $dec);
+               $rep->AmountCol(3, 4, $curr_balance * $convert, $dec);
+               $rep->AmountCol(4, 5, ($prev_balance + $curr_balance) * $convert, $dec);
+
+               $rep->NewLine();
+
+               $code_open_balance += $prev_balance;
+               $code_period_balance += $curr_balance;
+       }
+               
+       //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"], $from, $to, $convert, $dec, 
+                       $rep, $dimension, $dimension2, $pg, $graphics);
+               $open_balance_total += $totals_arr[0];
+               $period_balance_total += $totals_arr[1];
+       }
+
+       //Display Type Summary if total is != 0 OR head is printed (Needed in case of unused hierarchical COA) 
+       if (($code_open_balance + $open_balance_total + $code_period_balance + $period_balance_total) != 0 || $printtitle)
+       {
+               $rep->row += 6;
+               $rep->Line($rep->row);
+               $rep->NewLine();
+               $rep->TextCol(0, 2,     _('Total') . " " . $typename);
+               $rep->AmountCol(2, 3, ($code_open_balance + $open_balance_total) * $convert, $dec);
+               $rep->AmountCol(3, 4, ($code_period_balance + $period_balance_total) * $convert, $dec);
+               $rep->AmountCol(4, 5, ($code_open_balance + $open_balance_total + $code_period_balance + $period_balance_total) * $convert, $dec);              
+               if ($graphics)
+               {
+                       $pg->x[] = $typename;
+                       $pg->y[] = abs($code_open_balance + $open_balance_total);
+                       $pg->z[] = abs($code_period_balance + $period_balance_total);
+               }
+               $rep->NewLine();
+       }
+       
+       $totals_arr[0] = $code_open_balance + $open_balance_total;
+       $totals_arr[1] = $code_period_balance + $period_balance_total;
+       return $totals_arr;
+}
+
+print_balance_sheet();
 
 //----------------------------------------------------------------------------------------------------
 
@@ -105,263 +195,103 @@ function print_balance_sheet()
        $rep->Font();
        $rep->Info($params, $cols, $headers, $aligns);
        $rep->Header();
-       $classname = '';
-       $classopen = 0.0;
-       $classperiod = 0.0;
-       $classclose = 0.0;
-       $assetsopen = 0.0;
-       $assetsperiod = 0.0;
-       $assetsclose = 0.0;
-       $equityopen = 0.0;
-       $equityperiod = 0.0;
-       $equityclose = 0.0;
-       $lopen = 0.0;
-       $lperiod = 0.0;
-       $lclose = 0.0;
-       
-       $typeopen = array(0,0,0,0,0,0,0,0,0,0);
-       $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('','','','','','','','','','');
-       $closing = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
-       //$parent = array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1);
-       $level = 0;
-       $last = -1;
-       
-       $closeclass = false;
-       $ctype = 0;
-       $convert = 1;
-       $rep->NewLine();
-
-       $accounts = get_gl_accounts_all(1);
 
-       while ($account=db_fetch($accounts))
+       $calc_open = $calc_period = 0.0;
+       $equity_open = $equity_period = 0.0;
+       $liability_open = $liability_period = 0.0;
+       $econvert = $lconvert = 0;
+       
+       $classresult = get_account_classes(false, 1);
+       while ($class = db_fetch($classresult))
        {
-               if ($account['account_code'] == null && $account['parent'] > 0)
-                       continue;
-               if ($account['account_code'] != null)
+               $class_open_total = 0;
+               $class_period_total = 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))
                {
-                       $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)
-                               continue;
-               }
-               if ($account['AccountClassName'] != $classname)
-               {
-                       if ($classname != '')
-                       {
-                               $closeclass = true;
-                       }
-               }
-               if ($account['AccountTypeName'] != $typename[$level])
-               {
-                       //$rep->NewLine();
-                       //$rep->TextCol(0, 5,   "type = ".$account['AccountType'].", level = $level, closing[0]-[1]-[2]-[3] = ".$closing[0]." ".$closing[1]." ".$closing[2]." ".$closing[3]." type[parent] = ".$account['parent']." last = ".$last);
-                       //$rep->NewLine();
-                       if ($typename[$level] != '')
-                       {
-                               for ( ; $level >= 0, $typename[$level] != ''; $level--) 
-                               {
-                                       if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0 || $closeclass)
-                                       {
-                                               $rep->row += 6;
-                                               $rep->Line($rep->row);
-                                               $rep->NewLine();
-                                               $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
-                                               $rep->AmountCol(2, 3, $typeopen[$level] * $convert, $dec);
-                                               $rep->AmountCol(3, 4, $typeperiod[$level] * $convert, $dec);
-                                               $rep->AmountCol(4, 5, $typeclose[$level] * $convert, $dec);
-                                               if ($graphics)
-                                               {
-                                                       $pg->x[] = $typename[$level];
-                                                       $pg->y[] = abs($typeclose[$level]);
-                                               }
-                                               $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
-                                       }       
-                                       else
-                                               break;
-                                       $rep->NewLine();
-                               }
-                               //$rep->NewLine();
-                               if ($closeclass)
-                               {
-                                       $rep->row += 6;
-                                       $rep->Line($rep->row);
-                                       $rep->NewLine();
-                                       $rep->Font('bold');
-                                       $rep->TextCol(0, 2,     _('Total') . " " . $classname);
-                                       $rep->AmountCol(2, 3, $classopen * $convert, $dec);
-                                       $rep->AmountCol(3, 4, $classperiod * $convert, $dec);
-                                       $rep->AmountCol(4, 5, $classclose * $convert, $dec);
-                                       $rep->Font();
-                                       if ($ctype == CL_EQUITY)
-                                       {
-                                               $equityopen += $classopen;
-                                               $equityperiod += $classperiod;
-                                               $equityclose += $classclose;
-                                       }
-                                       if ($ctype == CL_LIABILITIES)
-                                       {
-                                               $lopen += $classopen;
-                                               $lperiod += $classperiod;
-                                               $lclose += $classclose;
-                                       }
-                                       $assetsopen += $classopen;
-                                       $assetsperiod += $classperiod;
-                                       $assetsclose += $classclose;
-                                       $classopen = $classperiod = $classclose = 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();
+                       $classtotal = display_type($accounttype["id"], $accounttype["name"], $from, $to, $convert, $dec, 
+                               $rep, $dimension, $dimension2, $pg, $graphics);
+                       $class_open_total += $classtotal[0];
+                       $class_period_total += $classtotal[1];                  
                }
-               $classname = $account['AccountClassName'];
-               $ctype = $account['ClassType'];
-               $convert = get_class_type_convert($ctype); 
-
-               if ($account['account_code'] != null)
-               {
-                       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']);
+               
+               //Print Class Summary   
+               $rep->row += 6;
+               $rep->Line($rep->row);
+               $rep->NewLine();
+               $rep->Font('bold');
+               $rep->TextCol(0, 2,     _('Total') . " " . $class["class_name"]);
+               $rep->AmountCol(2, 3, $class_open_total * $convert, $dec);
+               $rep->AmountCol(3, 4, $class_period_total * $convert, $dec);
+               $rep->AmountCol(4, 5, ($class_open_total + $class_period_total) * $convert, $dec);
+               $rep->Font();
+               $rep->NewLine(2);       
 
-                       $rep->AmountCol(2, 3, $prev_balance * $convert, $dec);
-                       $rep->AmountCol(3, 4, $curr_balance * $convert, $dec);
-                       $rep->AmountCol(4, 5, ($curr_balance + $prev_balance) * $convert, $dec);
-
-                       $rep->NewLine();
-
-                       if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
-                       {
-                               $rep->Line($rep->row - 2);
-                               $rep->Header();
-                       }
-               }       
-       }
-       if ($account['AccountClassName'] != $classname)
-       {
-               if ($classname != '')
+               $calc_open += $class_open_total;
+               $calc_period += $class_period_total;
+               if ($class['ctype'] == CL_EQUITY)
                {
-                       $closeclass = true;
+                       $equity_open += $class_open_total;
+                       $equity_period += $class_period_total;
+                       $econvert = $convert;
                }
-       }
-       if ($account['AccountTypeName'] != $typename[$level])
-       {
-               //$rep->NewLine();
-               //$rep->TextCol(0, 5,   "type = ".$account['AccountType'].", level = $level, closing[0]-[1]-[2]-[3] = ".$closing[0]." ".$closing[1]." ".$closing[2]." ".$closing[3]." type[parent] = ".$account['parent']." last = ".$last);
-               //$rep->NewLine();
-               if ($typename[$level] != '')
+               elseif ($class['ctype'] == CL_LIABILITIES)
                {
-                       for ( ; $level >= 0, $typename[$level] != ''; $level--) 
-                       {
-                               if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0 || $closeclass)
-                               {
-                                       $rep->row += 6;
-                                       $rep->Line($rep->row);
-                                       $rep->NewLine();
-                                       $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
-                                       $rep->AmountCol(2, 3, $typeopen[$level] * $convert, $dec);
-                                       $rep->AmountCol(3, 4, $typeperiod[$level] * $convert, $dec);
-                                       $rep->AmountCol(4, 5, $typeclose[$level] * $convert, $dec);
-                                       if ($graphics)
-                                       {
-                                               $pg->x[] = $typename[$level];
-                                               $pg->y[] = abs($typeclose[$level]);
-                                       }
-                                       $typeopen[$level] = $typeperiod[$level] = $typeclose[$level] = 0.0;
-                               }
-                               else
-                                       break;
-                               $rep->NewLine();
-                       }
-                       //$rep->NewLine();
-                       if ($closeclass)
-                       {
-                               $calculateopen = -$assetsopen - $classopen;
-                               $calculateperiod = -$assetsperiod - $classperiod;
-                               $calculateclose = -$assetsclose  - $classclose;
-                               if ($ctype == CL_EQUITY)
-                               {
-                                       $equityopen += $classopen;
-                                       $equityperiod += $classperiod;
-                                       $equityclose += $classclose;
-                               }
-                               $rep->row += 6;
-                               $rep->Line($rep->row);
-                               $rep->NewLine();
-                               $rep->TextCol(0, 2,     _('Calculated Return'));
-                               $rep->AmountCol(2, 3, $calculateopen * $convert, $dec);
-                               $rep->AmountCol(3, 4, $calculateperiod * $convert, $dec);
-                               $rep->AmountCol(4, 5, $calculateclose * $convert, $dec);
-                               if ($graphics)
-                               {
-                                       $pg->x[] = _('Calculated Return');
-                                       $pg->y[] = abs($calculateclose);
-                               }
-                               $rep->NewLine(2);
-                               $rep->Font('bold');
-                               $rep->TextCol(0, 2,     _('Total') . " " . $classname);
-                               $rep->AmountCol(2, 3, -$assetsopen * $convert, $dec);
-                               $rep->AmountCol(3, 4, -$assetsperiod * $convert, $dec);
-                               $rep->AmountCol(4, 5, -$assetsclose * $convert, $dec);
-                               $rep->Font();
-                               $rep->NewLine();
-                               if ($equityopen != 0.0 || $equityperiod != 0.0 || $equityclose != 0.0 ||
-                                       $lopen != 0.0 || $lperiod != 0.0 || $lclose != 0.0)
-                               {
-                                       $rep->NewLine();
-                                       $rep->Font('bold');
-                                       $rep->TextCol(0, 2,     _('Total') . " " . _('Liabilities') . _(' and ') . _('Equities'));
-                                       $rep->AmountCol(2, 3, ($lopen + $equityopen + $calculateopen) * -1, $dec);
-                                       $rep->AmountCol(3, 4, ($lperiod + $equityperiod + $calculateperiod) * -1, $dec);
-                                       $rep->AmountCol(4, 5, ($lclose + $equityclose + $calculateclose) * -1, $dec);
-                                       $rep->Font();
-                                       $rep->NewLine();
-                               }
-                       }
+                       $liability_open += $class_open_total;
+                       $liability_period += $class_period_total;
+                       $lconvert = $convert;
                }
        }
+       $rep->Font();   
+       $rep->TextCol(0, 2,     _('Calculated Return'));
+       if ($lconvert == 1)
+       {
+               $calc_open *= -1;
+               $calc_period *= -1;
+       }       
+       $rep->AmountCol(2, 3, $calc_open, $dec); // never convert
+       $rep->AmountCol(3, 4, $calc_period, $dec);
+       $rep->AmountCol(4, 5, $calc_open + $calc_period, $dec);
+       $rep->NewLine(2);
+
+       $rep->Font('bold');     
+       $rep->TextCol(0, 2,     _('Total') . " " . _('Liabilities') . _(' and ') . _('Equities'));
+       $topen = $equity_open * $econvert + $liability_open * $lconvert + $calc_open;
+       $tperiod = $equity_period * $econvert + $liability_period * $lconvert + $calc_period;
+       $tclose = $topen + $tperiod;
+       $rep->AmountCol(2, 3, $topen, $dec);
+       $rep->AmountCol(3, 4, $tperiod, $dec);
+       $rep->AmountCol(4, 5, $tclose, $dec);
+       
+       $rep->Font();
+       $rep->NewLine();
        $rep->Line($rep->row);
        if ($graphics)
        {
+               $pg->x[] = _('Calculated Return');
+               $pg->y[] = abs($calc_open);
+               $pg->z[] = abs($calc_period);
                global $decseps, $graph_skin;
                $pg->title     = $rep->title;
                $pg->axis_x    = _("Group");
                $pg->axis_y    = _("Amount");
-               $pg->graphic_1 = $to;
+               $pg->graphic_1 = $headers[2];
+               $pg->graphic_2 = $headers[3];
                $pg->type      = $graphics;
                $pg->skin      = $graph_skin;
                $pg->built_in  = false;
                $pg->fontfile  = $path_to_root . "/reporting/fonts/Vera.ttf";
                $pg->latin_notation = ($decseps[$_SESSION["wa_current_user"]->prefs->dec_sep()] != ".");
-               $filename =  $comp_path.'/'.user_company()."/pdf_files/test.png";
+               $filename = $comp_path.'/'.user_company(). "/pdf_files/test.png";
                $pg->display($filename, true);
                $w = $pg->width / 1.5;
                $h = $pg->height / 1.5;
index 0516c17ec53e8e6250a7f438052bad2cb0950414..0fa067a3993b667f7050bb1a7ad24cbb7233480c 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:      Profit and Loss Statement
 // ----------------------------------------------------------------
@@ -25,6 +25,105 @@ include_once($path_to_root . "/gl/includes/gl_db.inc");
 
 //----------------------------------------------------------------------------------------------------
 
+function display_type ($type, $typename, $from, $to, $begin, $end, $compare, $convert, &$dec, &$pdec, &$rep, $dimension, $dimension2, &$pg, $graphics)
+{
+       $code_per_balance = 0;
+       $code_acc_balance = 0;
+       $per_balance_total = 0;
+       $acc_balance_total = 0;
+       unset($totals_arr);
+       $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))
+       {
+               $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;
+               
+               //Print Type Title if it has atleast 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();                
+               }                       
+
+               $rep->TextCol(0, 1,     $account['account_code']);
+               $rep->TextCol(1, 2,     $account['account_name']);
+
+               $rep->AmountCol(2, 3, $per_balance * $convert, $dec);
+               $rep->AmountCol(3, 4, $acc_balance * $convert, $dec);
+               $rep->AmountCol(4, 5, Achieve($per_balance, $acc_balance), $pdec);
+
+               $rep->NewLine();
+
+               if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
+               {
+                       $rep->Line($rep->row - 2);
+                       $rep->Header();
+               }
+
+               $code_per_balance += $per_balance;
+               $code_acc_balance += $acc_balance;
+       }
+               
+       //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"], $from, $to, $begin, $end, $compare, $convert, $dec, 
+                       $pdec, $rep, $dimension, $dimension2, $pg, $graphics);
+               $per_balance_total += $totals_arr[0];
+               $acc_balance_total += $totals_arr[1];
+       }
+
+       //Display Type Summary if total is != 0 OR head is printed (Needed in case of unused hierarchical COA) 
+       if (($code_per_balance + $per_balance_total + $code_acc_balance + $acc_balance_total) != 0 || $printtitle)
+       {
+               $rep->row += 6;
+               $rep->Line($rep->row);
+               $rep->NewLine();
+               $rep->TextCol(0, 2,     _('Total') . " " . $typename);
+               $rep->AmountCol(2, 3, ($code_per_balance + $per_balance_total) * $convert, $dec);
+               $rep->AmountCol(3, 4, ($code_acc_balance + $acc_balance_total) * $convert, $dec);
+               $rep->AmountCol(4, 5, Achieve(($code_per_balance + $per_balance_total), ($code_acc_balance + $acc_balance_total)), $pdec);              
+               if ($graphics)
+               {
+                       $pg->x[] = $typename;
+                       $pg->y[] = abs($code_per_balance + $per_balance_total);
+                       $pg->z[] = abs($code_acc_balance + $acc_balance_total);
+               }
+               $rep->NewLine();
+       }
+       
+       $totals_arr[0] = $code_per_balance + $per_balance_total;
+       $totals_arr[1] = $code_acc_balance + $acc_balance_total;
+       return $totals_arr;
+}
+
 print_profit_and_loss_statement();
 
 //----------------------------------------------------------------------------------------------------
@@ -140,216 +239,63 @@ function print_profit_and_loss_statement()
        $rep->Info($params, $cols, $headers, $aligns);
        $rep->Header();
 
-       $classname = '';
-       
-       $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('','','','','','','','','','');
-       $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;
-
-       $closeclass = false;
-       $convert = 1;
-       $ctype = 0;
-
-       $accounts = get_gl_accounts_all(0);
+       $salesacc = 0.0;        
 
-       while ($account=db_fetch($accounts))
+       $classresult = get_account_classes(false, 0);
+       while ($class = db_fetch($classresult))
        {
-               if ($account['account_code'] == null && $account['parent'] > 0)
-                       continue;
-
-               if ($account['account_code'] != null)
-               {
-                       $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;
-               }
-
-               if ($account['AccountClassName'] != $classname)
-               {
-                       if ($classname != '')
-                       {
-                               $closeclass = true;
-                       }
-               }
-
-               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 || $closeclass)
-                                       {
-                                               $rep->row += 6;
-                                               $rep->Line($rep->row);
-                                               $rep->NewLine();
-                                               $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
-                                               $rep->AmountCol(2, 3, $typeper[$level] * $convert, $dec);
-                                               $rep->AmountCol(3, 4, $typeacc[$level] * $convert, $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();
-                               }
-                               //$rep->NewLine();
-                               if ($closeclass)
-                               {
-                                       $rep->row += 6;
-                                       $rep->Line($rep->row);
-                                       $rep->NewLine();
-                                       $rep->Font('bold');
-                                       $rep->TextCol(0, 2,     _('Total') . " " . $classname);
-                                       $rep->AmountCol(2, 3, $classper * $convert, $dec);
-                                       $rep->AmountCol(3, 4, $classacc * $convert, $dec);
-                                       $rep->AmountCol(4, 5, Achieve($classper, $classacc), $pdec);
-                                       $rep->Font();
-                                       $salesper += $classper;
-                                       $salesacc += $classacc;
-                                       $classper = $classacc = 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); 
+               $class_per_total = 0;
+               $class_acc_total = 0;
+               $convert = get_class_type_convert($class["ctype"]);             
                
-               if ($account['account_code'] != null)
-               {
-                       //$per_balance *= -1;
-                       //$acc_balance *= -1;
+               //Print Class Name      
+               $rep->Font('bold');
+               $rep->TextCol(0, 5, $class["class_name"]);
+               $rep->Font();
+               $rep->NewLine();
                
-                       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 * $convert, $dec);
-                       $rep->AmountCol(3, 4, $acc_balance * $convert, $dec);
-                       $rep->AmountCol(4, 5, Achieve($per_balance, $acc_balance), $pdec);
-
-                       $rep->NewLine();
-
-                       if ($rep->row < $rep->bottomMargin + 3 * $rep->lineHeight)
-                       {
-                               $rep->Line($rep->row - 2);
-                               $rep->Header();
-                       }
-               }       
-       }
-       if ($account['AccountClassName'] != $classname)
-       {
-               if ($classname != '')
+               //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"], $from, $to, $begin, $end, $compare, $convert, $dec, 
+                               $pdec, $rep, $dimension, $dimension2, $pg, $graphics);
+                       $class_per_total += $classtotal[0];
+                       $class_acc_total += $classtotal[1];                     
                }
+               
+               //Print Class Summary   
+               $rep->row += 6;
+               $rep->Line($rep->row);
+               $rep->NewLine();
+               $rep->Font('bold');
+               $rep->TextCol(0, 2,     _('Total') . " " . $class["class_name"]);
+               $rep->AmountCol(2, 3, $class_per_total * $convert, $dec);
+               $rep->AmountCol(3, 4, $class_acc_total * $convert, $dec);
+               $rep->AmountCol(4, 5, Achieve($class_per_total, $class_acc_total), $pdec);
+               $rep->Font();
+               $rep->NewLine(2);       
+
+               $salesper += $class_per_total;
+               $salesacc += $class_acc_total;
        }
-       if ($account['AccountTypeName'] != $typename[$level])
+       
+       $rep->Font('bold');     
+       $rep->TextCol(0, 2,     _('Calculated Return'));
+       $rep->AmountCol(2, 3, $salesper *-1, $dec); // always convert
+       $rep->AmountCol(3, 4, $salesacc * -1, $dec);
+       $rep->AmountCol(4, 5, Achieve($salesper, $salesacc), $pdec);
+       if ($graphics)
        {
-               if ($typename[$level] != '')
-               {
-                       for ( ; $level >= 0, $typename[$level] != ''; $level--) 
-                       {
-                               if ($account['parent'] == $closing[$level] || $account['parent'] < $last || $account['parent'] <= 0 || $closeclass)
-                               {
-                                       $rep->row += 6;
-                                       $rep->Line($rep->row);
-                                       $rep->NewLine();
-                                       $rep->TextCol(0, 2,     _('Total') . " " . $typename[$level]);
-                                       $rep->AmountCol(2, 3, $typeper[$level] * $convert, $dec);
-                                       $rep->AmountCol(3, 4, $typeacc[$level] * $convert, $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();
-                       }
-                       //$rep->NewLine();
-                       if ($closeclass)
-                       {
-                               $rep->Line($rep->row + 6);
-                               $calculateper = $salesper + $classper;
-                               $calculateacc = $salesacc + $classacc;
-                               $rep->row += 6;
-                               $rep->Line($rep->row);
-                               $rep->NewLine();
-
-                               $rep->Font('bold');
-                               $rep->TextCol(0, 2,     _('Total') . " " . $classname);
-                               $rep->AmountCol(2, 3, $classper * $convert, $dec);
-                               $rep->AmountCol(3, 4, $classacc * $convert, $dec);
-                               $rep->AmountCol(4, 5, Achieve($classper, $classacc), $pdec);
-
-                               $rep->NewLine(2);
-                               $rep->TextCol(0, 2,     _('Calculated Return'));
-                               $rep->AmountCol(2, 3, $calculateper *-1, $dec); // always convert
-                               $rep->AmountCol(3, 4, $calculateacc * -1, $dec);
-                               $rep->AmountCol(4, 5, Achieve($calculateper, $calculateacc), $pdec);
-                               if ($graphics)
-                               {
-                                       $pg->x[] = _('Calculated Return');
-                                       $pg->y[] = abs($calculateper);
-                                       $pg->z[] = abs($calculateacc);
-                               }
-
-                               $rep->Font();
-
-                               $rep->NewLine();
-                       }
-               }
+               $pg->x[] = _('Calculated Return');
+               $pg->y[] = abs($salesper);
+               $pg->z[] = abs($salesacc);
        }
+       $rep->Font();
+       $rep->NewLine();
        $rep->Line($rep->row);
        if ($graphics)
        {
@@ -374,6 +320,7 @@ function print_profit_and_loss_statement()
                        $rep->Header();
                $rep->AddImage($filename, $x, $rep->row - $h, $w, $h);
        }
+               
        $rep->End();
 }