[0000077] Added a total before ending balance in trial balance (also in report)
[fa-stable.git] / gl / inquiry / gl_trial_balance.php
1 <?php
2
3 $page_security = 8;
4 $path_to_root="../..";
5
6 include_once($path_to_root . "/includes/session.inc");
7
8 include_once($path_to_root . "/includes/date_functions.inc");
9 include_once($path_to_root . "/includes/ui.inc");
10 include_once($path_to_root . "/includes/data_checks.inc");
11
12 include_once($path_to_root . "/gl/includes/gl_db.inc");
13
14 $js = "";
15 if ($use_date_picker)
16         $js = get_js_date_picker();
17
18 page(_("Trial Balance"), false, false, "", $js);
19
20 //----------------------------------------------------------------------------------------------------
21 // Ajax updates
22 //
23 if (get_post('Show')) 
24 {
25         $Ajax->activate('balance_tbl');
26 }
27
28
29 function gl_inquiry_controls()
30 {
31     start_form();
32
33     start_table("class='tablestyle_noborder'");
34
35     date_cells(_("From:"), 'TransFromDate', '', null, -30);
36         date_cells(_("To:"), 'TransToDate');
37         check_cells(_("No zero values"), 'NoZero', null);
38
39         submit_cells('Show',_("Show"),'','', true);
40     end_table();
41     end_form();
42 }
43
44 //----------------------------------------------------------------------------------------------------
45
46 function get_balance($account, $from, $to, $from_incl=true, $to_incl=true) {
47
48         $sql = "SELECT SUM(amount) As TransactionSum FROM ".TB_PREF."gl_trans
49                 WHERE account='$account'";
50
51         if ($from)
52         {
53                 $from_date = date2sql($from);
54                 if ($from_incl)
55                         $sql .= " AND tran_date >= '$from_date'";
56                 else
57                         $sql .= " AND tran_date > '$from_date'";
58         }
59
60         if ($to)
61         {
62                 $to_date = date2sql($to);
63                 if ($to_incl)
64                         $sql .= " AND tran_date <= '$to_date' ";
65                 else
66                         $sql .= " AND tran_date < '$to_date' ";
67         }
68
69         $result = db_query($sql,"No general ledger accounts were returned");
70
71         $row = db_fetch_row($result);
72         return $row[0];
73 }
74
75 //----------------------------------------------------------------------------------------------------
76
77 function display_trial_balance()
78 {
79         global $table_style, $path_to_root;
80
81         div_start('balance_tbl');
82         start_table($table_style);
83         $tableheader =  "<tr>
84         <td rowspan=2 class='tableheader'>" . _("Account") . "</td>
85         <td rowspan=2 class='tableheader'>" . _("Account Name") . "</td>
86                 <td colspan=2 class='tableheader'>" . _("Brought Forward") . "</td>
87                 <td colspan=2 class='tableheader'>" . _("This Period") . "</td>
88                 <td colspan=2 class='tableheader'>" . _("Balance") . "</td>
89                 </tr><tr>
90                 <td class='tableheader'>" . _("Debit") . "</td>
91         <td class='tableheader'>" . _("Credit") . "</td>
92                 <td class='tableheader'>" . _("Debit") . "</td>
93                 <td class='tableheader'>" . _("Credit") . "</td>
94         <td class='tableheader'>" . _("Debit") . "</td>
95         <td class='tableheader'>" . _("Credit") . "</td>
96         </tr>";
97
98     echo $tableheader;
99
100         $k = 0;
101
102         $totprevd = $totprevc = $totcurrd = $totcurrc = 0.0;
103         $accounts = get_gl_accounts();
104
105         while ($account = db_fetch($accounts))
106         {
107                 if (is_account_balancesheet($account["account_code"]))
108                         $begin = null;
109                 else
110                 {
111                         $begin = begin_fiscalyear();
112                         if (date1_greater_date2($begin, $_POST['TransFromDate']))
113                                 $begin = $_POST['TransFromDate'];
114                         $begin = add_days($begin, -1);
115                 }
116                 $prev_balance = get_balance($account["account_code"], $begin, $_POST['TransFromDate'], false, false);
117
118                 $curr_balance = get_balance($account["account_code"], $_POST['TransFromDate'], $_POST['TransToDate']);
119                 if (check_value("NoZero") && !$prev_balance && !$curr_balance)
120                         continue;
121                 if ($prev_balance >= 0.0)
122                         $totprevd += $prev_balance;
123                 else
124                         $totprevc += $prev_balance;
125                 if ($curr_balance >= 0.0)
126                         $totcurrd += $curr_balance;
127                 else
128                         $totcurrc += $curr_balance;
129                 alt_table_row_color($k);
130
131                 $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>";
132
133                 label_cell($url);
134                 label_cell($account["account_name"]);
135
136                 display_debit_or_credit_cells($prev_balance);
137                 display_debit_or_credit_cells($curr_balance);
138                 display_debit_or_credit_cells($prev_balance + $curr_balance);
139                 end_row();
140         }
141         start_row("class='inquirybg' style='font-weight:bold'");
142         label_cell(_("Total") ." - ".$_POST['TransToDate'], "colspan=2");
143         amount_cell($totprevd);
144         amount_cell(abs($totprevc));
145         amount_cell($totcurrd);
146         amount_cell(abs($totcurrc));
147         amount_cell($totprevd + $totcurrd);
148         amount_cell(abs($totprevc + $totcurrc));
149         end_row();
150         $totprev = $totprevd + $totprevc;
151         $totcurr = $totcurrd + $totcurrc;
152         start_row("class='inquirybg' style='font-weight:bold'");
153         label_cell(_("Ending Balance") ." - ".$_POST['TransToDate'], "colspan=2");
154         display_debit_or_credit_cells($totprev);
155         display_debit_or_credit_cells($totcurr);
156         display_debit_or_credit_cells($totprev + $totcurr);
157         end_row();
158
159         end_table(1);
160         div_end();
161 }
162
163 //----------------------------------------------------------------------------------------------------
164
165 gl_inquiry_controls();
166
167 display_trial_balance();
168
169 //----------------------------------------------------------------------------------------------------
170
171 end_page();
172
173 ?>
174