Merging version 2.1 RC to main trunk.
[fa-stable.git] / gl / inquiry / gl_trial_balance.php
1 <?php
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 ***********************************************************************/
12 $page_security = 8;
13 $path_to_root="../..";
14
15 include_once($path_to_root . "/includes/session.inc");
16
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");
20
21 include_once($path_to_root . "/gl/includes/gl_db.inc");
22
23 $js = "";
24 if ($use_date_picker)
25         $js = get_js_date_picker();
26
27 page(_("Trial Balance"), false, false, "", $js);
28
29 //----------------------------------------------------------------------------------------------------
30 // Ajax updates
31 //
32 if (get_post('Show')) 
33 {
34         $Ajax->activate('balance_tbl');
35 }
36
37
38 function gl_inquiry_controls()
39 {
40     start_form();
41
42     start_table("class='tablestyle_noborder'");
43
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);
48
49         submit_cells('Show',_("Show"),'','', true);
50     end_table();
51     end_form();
52 }
53
54 //----------------------------------------------------------------------------------------------------
55
56 function get_balance($account, $from, $to, $from_incl=true, $to_incl=true) 
57 {
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";
62                 
63         if ($account != null)
64                 $sql .= " account='$account' AND";
65         $from_date = date2sql($from);
66         if ($from_incl)
67                 $sql .= " tran_date >= '$from_date'  AND";
68         else
69                 $sql .= " tran_date > IF(".TB_PREF."chart_class.balance_sheet=1, '0000-00-00', '$from_date') AND";
70         $to_date = date2sql($to);
71         if ($to_incl)
72                 $sql .= " tran_date <= '$to_date' ";
73         else
74                 $sql .= " tran_date < '$to_date' ";
75
76         $result = db_query($sql,"No general ledger accounts were returned");
77
78         return db_fetch($result);
79 }
80
81 //----------------------------------------------------------------------------------------------------
82
83 function display_trial_balance()
84 {
85         global $table_style, $path_to_root;
86
87         div_start('balance_tbl');
88         start_table($table_style);
89         $tableheader =  "<tr>
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>
95                 </tr><tr>
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>
102         </tr>";
103
104     echo $tableheader;
105
106         $k = 0;
107
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);
114         
115         while ($account = db_fetch($accounts))
116         {
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'])
121                         continue;
122                 alt_table_row_color($k);
123
124                 $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>";
125
126                 label_cell($url);
127                 label_cell($account["account_name"]);
128                 if (check_value('Balance'))
129                 {
130                         display_debit_or_credit_cells($prev['balance']);
131                         display_debit_or_credit_cells($curr['balance']);
132                         display_debit_or_credit_cells($tot['balance']);
133                         
134                 }
135                 else
136                 {
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'];
149                 }       
150                 $pbal += $prev['balance'];
151                 $cbal += $curr['balance'];
152                 $tbal += $tot['balance'];
153                 end_row();
154         }
155
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'))
160         {
161                 start_row("class='inquirybg' style='font-weight:bold'");
162                 label_cell(_("Total") ." - ".$_POST['TransToDate'], "colspan=2");
163                 amount_cell($pdeb);
164                 amount_cell($pcre);
165                 amount_cell($cdeb);
166                 amount_cell($ccre);
167                 amount_cell($tdeb);
168                 amount_cell($tcre);
169                 end_row();
170         }       
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);
176         end_row();
177
178         end_table(1);
179         div_end();
180 }
181
182 //----------------------------------------------------------------------------------------------------
183
184 gl_inquiry_controls();
185
186 display_trial_balance();
187
188 //----------------------------------------------------------------------------------------------------
189
190 end_page();
191
192 ?>
193