2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
13 $path_to_root="../..";
15 include_once($path_to_root . "/includes/session.inc");
17 include_once($path_to_root . "/includes/date_functions.inc");
18 include_once($path_to_root . "/includes/ui.inc");
19 include_once($path_to_root . "/includes/data_checks.inc");
21 include_once($path_to_root . "/gl/includes/gl_db.inc");
25 $js = get_js_date_picker();
27 page(_("Trial Balance"), false, false, "", $js);
29 //----------------------------------------------------------------------------------------------------
34 $Ajax->activate('balance_tbl');
38 function gl_inquiry_controls()
42 start_table("class='tablestyle_noborder'");
44 date_cells(_("From:"), 'TransFromDate', '', null, -30);
45 date_cells(_("To:"), 'TransToDate');
46 check_cells(_("No zero values"), 'NoZero', null);
47 check_cells(_("Only balances"), 'Balance', null);
49 submit_cells('Show',_("Show"),'','', 'default');
54 //----------------------------------------------------------------------------------------------------
56 function get_balance($account, $from, $to, $from_incl=true, $to_incl=true)
58 $sql = "SELECT SUM(IF(amount >= 0, amount, 0)) AS debit, SUM(IF(amount < 0, -amount, 0)) AS credit, SUM(amount) AS balance
59 FROM ".TB_PREF."gl_trans,".TB_PREF."chart_master,".TB_PREF."chart_types, ".TB_PREF."chart_class
60 WHERE ".TB_PREF."gl_trans.account=".TB_PREF."chart_master.account_code AND ".TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id
61 AND ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid AND";
64 $sql .= " account='$account' AND";
65 $from_date = date2sql($from);
67 $sql .= " tran_date >= '$from_date' AND";
69 $sql .= " tran_date > IF(".TB_PREF."chart_class.balance_sheet=1, '0000-00-00', '$from_date') AND";
70 $to_date = date2sql($to);
72 $sql .= " tran_date <= '$to_date' ";
74 $sql .= " tran_date < '$to_date' ";
76 $result = db_query($sql,"No general ledger accounts were returned");
78 return db_fetch($result);
81 //----------------------------------------------------------------------------------------------------
83 function display_trial_balance()
85 global $table_style, $path_to_root;
87 div_start('balance_tbl');
88 start_table($table_style);
90 <td rowspan=2 class='tableheader'>" . _("Account") . "</td>
91 <td rowspan=2 class='tableheader'>" . _("Account Name") . "</td>
92 <td colspan=2 class='tableheader'>" . _("Brought Forward") . "</td>
93 <td colspan=2 class='tableheader'>" . _("This Period") . "</td>
94 <td colspan=2 class='tableheader'>" . _("Balance") . "</td>
96 <td class='tableheader'>" . _("Debit") . "</td>
97 <td class='tableheader'>" . _("Credit") . "</td>
98 <td class='tableheader'>" . _("Debit") . "</td>
99 <td class='tableheader'>" . _("Credit") . "</td>
100 <td class='tableheader'>" . _("Debit") . "</td>
101 <td class='tableheader'>" . _("Credit") . "</td>
108 $accounts = get_gl_accounts();
109 $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;
110 $begin = begin_fiscalyear();
111 if (date1_greater_date2($begin, $_POST['TransFromDate']))
112 $begin = $_POST['TransFromDate'];
113 $begin = add_days($begin, -1);
115 while ($account = db_fetch($accounts))
117 $prev = get_balance($account["account_code"], $begin, $_POST['TransFromDate'], false, false);
118 $curr = get_balance($account["account_code"], $_POST['TransFromDate'], $_POST['TransToDate'], true, true);
119 $tot = get_balance($account["account_code"], $begin, $_POST['TransToDate'], false, true);
120 if (check_value("NoZero") && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
122 alt_table_row_color($k);
124 $url = "<a href='$path_to_root/gl/inquiry/gl_account_inquiry.php?TransFromDate=" . $_POST["TransFromDate"] . "&TransToDate=" . $_POST["TransToDate"] . "&account=" . $account["account_code"] . "'>" . $account["account_code"] . "</a>";
127 label_cell($account["account_name"]);
128 if (check_value('Balance'))
130 display_debit_or_credit_cells($prev['balance']);
131 display_debit_or_credit_cells($curr['balance']);
132 display_debit_or_credit_cells($tot['balance']);
137 amount_cell($prev['debit']);
138 amount_cell($prev['credit']);
139 amount_cell($curr['debit']);
140 amount_cell($curr['credit']);
141 amount_cell($tot['debit']);
142 amount_cell($tot['credit']);
143 $pdeb += $prev['debit'];
144 $pcre += $prev['credit'];
145 $cdeb += $curr['debit'];
146 $ccre += $curr['credit'];
147 $tdeb += $tot['debit'];
148 $tcre += $tot['credit'];
150 $pbal += $prev['balance'];
151 $cbal += $curr['balance'];
152 $tbal += $tot['balance'];
156 //$prev = get_balance(null, $begin, $_POST['TransFromDate'], false, false);
157 //$curr = get_balance(null, $_POST['TransFromDate'], $_POST['TransToDate'], true, true);
158 //$tot = get_balance(null, $begin, $_POST['TransToDate'], false, true);
159 if (!check_value('Balance'))
161 start_row("class='inquirybg' style='font-weight:bold'");
162 label_cell(_("Total") ." - ".$_POST['TransToDate'], "colspan=2");
171 start_row("class='inquirybg' style='font-weight:bold'");
172 label_cell(_("Ending Balance") ." - ".$_POST['TransToDate'], "colspan=2");
173 display_debit_or_credit_cells($pbal);
174 display_debit_or_credit_cells($cbal);
175 display_debit_or_credit_cells($tbal);
182 //----------------------------------------------------------------------------------------------------
184 gl_inquiry_controls();
186 display_trial_balance();
188 //----------------------------------------------------------------------------------------------------