Merged changes from main branch up to 2.1.3.
[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"),'','', 'default');
50     end_table();
51     end_form();
52 }
53
54 //----------------------------------------------------------------------------------------------------
55
56 function display_trial_balance()
57 {
58         global $table_style, $path_to_root;
59
60         div_start('balance_tbl');
61         start_table($table_style);
62         $tableheader =  "<tr>
63         <td rowspan=2 class='tableheader'>" . _("Account") . "</td>
64         <td rowspan=2 class='tableheader'>" . _("Account Name") . "</td>
65                 <td colspan=2 class='tableheader'>" . _("Brought Forward") . "</td>
66                 <td colspan=2 class='tableheader'>" . _("This Period") . "</td>
67                 <td colspan=2 class='tableheader'>" . _("Balance") . "</td>
68                 </tr><tr>
69                 <td class='tableheader'>" . _("Debit") . "</td>
70         <td class='tableheader'>" . _("Credit") . "</td>
71                 <td class='tableheader'>" . _("Debit") . "</td>
72                 <td class='tableheader'>" . _("Credit") . "</td>
73         <td class='tableheader'>" . _("Debit") . "</td>
74         <td class='tableheader'>" . _("Credit") . "</td>
75         </tr>";
76
77     echo $tableheader;
78
79         $k = 0;
80
81         $accounts = get_gl_accounts();
82         $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;
83         $begin = begin_fiscalyear();
84         if (date1_greater_date2($begin, $_POST['TransFromDate']))
85                 $begin = $_POST['TransFromDate'];
86         $begin = add_days($begin, -1);
87         
88         while ($account = db_fetch($accounts))
89         {
90                 $prev = get_balance($account["account_code"], 0, 0, $begin, $_POST['TransFromDate'], false, false);
91                 $curr = get_balance($account["account_code"], 0, 0, $_POST['TransFromDate'], $_POST['TransToDate'], true, true);
92                 $tot = get_balance($account["account_code"], 0, 0, $begin, $_POST['TransToDate'], false, true);
93                 if (check_value("NoZero") && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
94                         continue;
95                 alt_table_row_color($k);
96
97                 $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>";
98
99                 label_cell($url);
100                 label_cell($account["account_name"]);
101                 if (check_value('Balance'))
102                 {
103                         display_debit_or_credit_cells($prev['balance']);
104                         display_debit_or_credit_cells($curr['balance']);
105                         display_debit_or_credit_cells($tot['balance']);
106                         
107                 }
108                 else
109                 {
110                         amount_cell($prev['debit']);
111                         amount_cell($prev['credit']);
112                         amount_cell($curr['debit']);
113                         amount_cell($curr['credit']);
114                         amount_cell($tot['debit']);
115                         amount_cell($tot['credit']);
116                         $pdeb += $prev['debit'];
117                         $pcre += $prev['credit'];
118                         $cdeb += $curr['debit'];
119                         $ccre += $curr['credit'];
120                         $tdeb += $tot['debit'];
121                         $tcre += $tot['credit'];
122                 }       
123                 $pbal += $prev['balance'];
124                 $cbal += $curr['balance'];
125                 $tbal += $tot['balance'];
126                 end_row();
127         }
128
129         //$prev = get_balance(null, $begin, $_POST['TransFromDate'], false, false);
130         //$curr = get_balance(null, $_POST['TransFromDate'], $_POST['TransToDate'], true, true);
131         //$tot = get_balance(null, $begin, $_POST['TransToDate'], false, true);
132         if (!check_value('Balance'))
133         {
134                 start_row("class='inquirybg' style='font-weight:bold'");
135                 label_cell(_("Total") ." - ".$_POST['TransToDate'], "colspan=2");
136                 amount_cell($pdeb);
137                 amount_cell($pcre);
138                 amount_cell($cdeb);
139                 amount_cell($ccre);
140                 amount_cell($tdeb);
141                 amount_cell($tcre);
142                 end_row();
143         }       
144         start_row("class='inquirybg' style='font-weight:bold'");
145         label_cell(_("Ending Balance") ." - ".$_POST['TransToDate'], "colspan=2");
146         display_debit_or_credit_cells($pbal);
147         display_debit_or_credit_cells($cbal);
148         display_debit_or_credit_cells($tbal);
149         end_row();
150
151         end_table(1);
152         div_end();
153 }
154
155 //----------------------------------------------------------------------------------------------------
156
157 gl_inquiry_controls();
158
159 display_trial_balance();
160
161 //----------------------------------------------------------------------------------------------------
162
163 end_page();
164
165 ?>
166