b3dee997d722db482fe18f4b376840f60d91a780
[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         $accounts = get_gl_accounts();
103
104         while ($account = db_fetch($accounts))
105         {
106                 if (is_account_balancesheet($account["account_code"]))
107                         $begin = null;
108                 else
109                 {
110                         $begin = begin_fiscalyear();
111                         if ($_POST['TransFromDate'] < $begin)
112                                 $begin = $_POST['TransFromDate'];
113                         $begin = add_days($begin, -1);
114                 }
115                 $prev_balance = get_balance($account["account_code"], $begin, $_POST['TransFromDate'], false, false);
116
117                 $curr_balance = get_balance($account["account_code"], $_POST['TransFromDate'], $_POST['TransToDate']);
118                 if (check_value("NoZero") && !$prev_balance && !$curr_balance)
119                         continue;
120                 alt_table_row_color($k);
121
122                 $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>";
123
124                 label_cell($url);
125                 label_cell($account["account_name"]);
126
127                 display_debit_or_credit_cells($prev_balance);
128                 display_debit_or_credit_cells($curr_balance);
129                 display_debit_or_credit_cells($prev_balance + $curr_balance);
130                 end_row();
131         }
132
133         end_table(1);
134         div_end();
135 }
136
137 //----------------------------------------------------------------------------------------------------
138
139 gl_inquiry_controls();
140
141 display_trial_balance();
142
143 //----------------------------------------------------------------------------------------------------
144
145 end_page();
146
147 ?>
148