Release 1.0.1 established on SourceForge, fixing the bugs and including a Date Picker...
[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
100                 $prev_balance = get_balance($account["account_code"], null, $_POST['TransFromDate'], false, false);
101
102                 $curr_balance = get_balance($account["account_code"], $_POST['TransFromDate'], $_POST['TransToDate']);
103                 if (check_value("NoZero") && !$prev_balance && !$curr_balance)
104                         continue;
105                 alt_table_row_color($k);
106
107                 $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>";
108
109                 label_cell($url);
110                 label_cell($account["account_name"]);
111
112                 display_debit_or_credit_cells($prev_balance);
113                 display_debit_or_credit_cells($curr_balance);
114                 display_debit_or_credit_cells($prev_balance + $curr_balance);
115                 end_row();
116         }
117
118         end_table(1);
119
120 }
121
122 //----------------------------------------------------------------------------------------------------
123
124 gl_inquiry_controls();
125
126 display_trial_balance();
127
128 //----------------------------------------------------------------------------------------------------
129
130 end_page();
131
132 ?>
133