Bad balance in GL trans for chart en_US-demo.sql
[fa-stable.git] / gl / inquiry / gl_trial_balance.php
index 28653ff0e57d6327ebff246fa2a093d19b98d284..969cfcca0dd6c8030c6877e3afe950de87456d16 100644 (file)
@@ -5,15 +5,25 @@ $path_to_root="../..";
 
 include_once($path_to_root . "/includes/session.inc");
 
-page(_("Trial Balance"));
-
 include_once($path_to_root . "/includes/date_functions.inc");
 include_once($path_to_root . "/includes/ui.inc");
 include_once($path_to_root . "/includes/data_checks.inc");
 
 include_once($path_to_root . "/gl/includes/gl_db.inc");
 
+$js = "";
+if ($use_date_picker)
+       $js = get_js_date_picker();
+
+page(_("Trial Balance"), false, false, "", $js);
+
 //----------------------------------------------------------------------------------------------------
+// Ajax updates
+//
+if (get_post('Show')) 
+{
+       $Ajax->activate('balance_tbl');
+}
 
 
 function gl_inquiry_controls()
@@ -22,11 +32,11 @@ function gl_inquiry_controls()
 
     start_table("class='tablestyle_noborder'");
 
-    date_cells(_("From:"), 'TransFromDate', null, -30);
+    date_cells(_("From:"), 'TransFromDate', '', null, -30);
        date_cells(_("To:"), 'TransToDate');
        check_cells(_("No zero values"), 'NoZero', null);
 
-    submit_cells('Show',_("Show"));
+       submit_cells('Show',_("Show"),'','', true);
     end_table();
     end_form();
 }
@@ -36,9 +46,9 @@ function gl_inquiry_controls()
 function get_balance($account, $from, $to, $from_incl=true, $to_incl=true) {
 
        $sql = "SELECT SUM(amount) As TransactionSum FROM ".TB_PREF."gl_trans
-               WHERE account=$account";
+               WHERE account='$account'";
 
-       if ($from) 
+       if ($from)
        {
                $from_date = date2sql($from);
                if ($from_incl)
@@ -47,7 +57,7 @@ function get_balance($account, $from, $to, $from_incl=true, $to_incl=true) {
                        $sql .= " AND tran_date > '$from_date'";
        }
 
-       if ($to) 
+       if ($to)
        {
                $to_date = date2sql($to);
                if ($to_incl)
@@ -68,6 +78,7 @@ function display_trial_balance()
 {
        global $table_style, $path_to_root;
 
+       div_start('balance_tbl');
        start_table($table_style);
        $tableheader =  "<tr>
         <td rowspan=2 class='tableheader'>" . _("Account") . "</td>
@@ -88,16 +99,27 @@ function display_trial_balance()
 
        $k = 0;
 
+       $totprev = $totcurr = 0.0;
        $accounts = get_gl_accounts();
 
-       while ($account = db_fetch($accounts)) 
+       while ($account = db_fetch($accounts))
        {
-
-               $prev_balance = get_balance($account["account_code"], null, $_POST['TransFromDate'], false, false);
+               if (is_account_balancesheet($account["account_code"]))
+                       $begin = null;
+               else
+               {
+                       $begin = begin_fiscalyear();
+                       if (date1_greater_date2($begin, $_POST['TransFromDate']))
+                               $begin = $_POST['TransFromDate'];
+                       $begin = add_days($begin, -1);
+               }
+               $prev_balance = get_balance($account["account_code"], $begin, $_POST['TransFromDate'], false, false);
 
                $curr_balance = get_balance($account["account_code"], $_POST['TransFromDate'], $_POST['TransToDate']);
                if (check_value("NoZero") && !$prev_balance && !$curr_balance)
                        continue;
+               $totprev += $prev_balance;
+               $totcurr += $curr_balance;
                alt_table_row_color($k);
 
                $url = "<a href='$path_to_root/gl/inquiry/gl_account_inquiry.php?" . SID . "TransFromDate=" . $_POST["TransFromDate"] . "&TransToDate=" . $_POST["TransToDate"] . "&account=" . $account["account_code"] . "'>" . $account["account_code"] . "</a>";
@@ -110,9 +132,15 @@ function display_trial_balance()
                display_debit_or_credit_cells($prev_balance + $curr_balance);
                end_row();
        }
+       start_row("class='inquirybg'");
+       label_cell("<b>" . _("Ending Balance") ." - ".$_POST['TransToDate']. "</b>", "colspan=2");
+       display_debit_or_credit_cells($totprev);
+       display_debit_or_credit_cells($totcurr);
+       display_debit_or_credit_cells($totprev + $totcurr);
+       end_row();
 
        end_table(1);
-
+       div_end();
 }
 
 //----------------------------------------------------------------------------------------------------