8a6ba2f9c1c65ec251fb70a53a52d6549fb91f6f
[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
22
23 function gl_inquiry_controls()
24 {
25     start_form();
26
27     start_table("class='tablestyle_noborder'");
28
29     date_cells(_("From:"), 'TransFromDate', null, -30);
30         date_cells(_("To:"), 'TransToDate');
31         check_cells(_("No zero values"), 'NoZero', null);
32
33     submit_cells('Show',_("Show"));
34     end_table();
35     end_form();
36 }
37
38 //----------------------------------------------------------------------------------------------------
39
40 function get_balance($account, $from, $to, $from_incl=true, $to_incl=true) {
41
42         $sql = "SELECT SUM(amount) As TransactionSum FROM ".TB_PREF."gl_trans
43                 WHERE account='$account'";
44
45         if ($from)
46         {
47                 $from_date = date2sql($from);
48                 if ($from_incl)
49                         $sql .= " AND tran_date >= '$from_date'";
50                 else
51                         $sql .= " AND tran_date > '$from_date'";
52         }
53
54         if ($to)
55         {
56                 $to_date = date2sql($to);
57                 if ($to_incl)
58                         $sql .= " AND tran_date <= '$to_date' ";
59                 else
60                         $sql .= " AND tran_date < '$to_date' ";
61         }
62
63         $result = db_query($sql,"No general ledger accounts were returned");
64
65         $row = db_fetch_row($result);
66         return $row[0];
67 }
68
69 //----------------------------------------------------------------------------------------------------
70
71 function display_trial_balance()
72 {
73         global $table_style, $path_to_root;
74
75         start_table($table_style);
76         $tableheader =  "<tr>
77         <td rowspan=2 class='tableheader'>" . _("Account") . "</td>
78         <td rowspan=2 class='tableheader'>" . _("Account Name") . "</td>
79                 <td colspan=2 class='tableheader'>" . _("Brought Forward") . "</td>
80                 <td colspan=2 class='tableheader'>" . _("This Period") . "</td>
81                 <td colspan=2 class='tableheader'>" . _("Balance") . "</td>
82                 </tr><tr>
83                 <td class='tableheader'>" . _("Debit") . "</td>
84         <td class='tableheader'>" . _("Credit") . "</td>
85                 <td class='tableheader'>" . _("Debit") . "</td>
86                 <td class='tableheader'>" . _("Credit") . "</td>
87         <td class='tableheader'>" . _("Debit") . "</td>
88         <td class='tableheader'>" . _("Credit") . "</td>
89         </tr>";
90
91     echo $tableheader;
92
93         $k = 0;
94
95         $accounts = get_gl_accounts();
96
97         while ($account = db_fetch($accounts))
98         {
99                 if (is_account_balancesheet($account["account_code"]))
100                         $begin = null;
101                 else
102                 {
103                         $begin = begin_fiscalyear();
104                         if ($_POST['TransFromDate'] < $begin)
105                                 $begin = $_POST['TransFromDate'];
106                         $begin = add_days($begin, -1);
107                 }
108                 $prev_balance = get_balance($account["account_code"], $begin, $_POST['TransFromDate'], false, false);
109
110                 $curr_balance = get_balance($account["account_code"], $_POST['TransFromDate'], $_POST['TransToDate']);
111                 if (check_value("NoZero") && !$prev_balance && !$curr_balance)
112                         continue;
113                 alt_table_row_color($k);
114
115                 $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>";
116
117                 label_cell($url);
118                 label_cell($account["account_name"]);
119
120                 display_debit_or_credit_cells($prev_balance);
121                 display_debit_or_credit_cells($curr_balance);
122                 display_debit_or_credit_cells($prev_balance + $curr_balance);
123                 end_row();
124         }
125
126         end_table(1);
127
128 }
129
130 //----------------------------------------------------------------------------------------------------
131
132 gl_inquiry_controls();
133
134 display_trial_balance();
135
136 //----------------------------------------------------------------------------------------------------
137
138 end_page();
139
140 ?>
141