[0000266] Added dimension selections in Profit Loss and Balance drilldown and Trial...
[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 = 'SA_GLANALYTIC';
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(_($help_context = "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         $dim = get_company_pref('use_dimension');
41     start_form();
42
43     start_table(TABLESTYLE_NOBORDER);
44
45     date_cells(_("From:"), 'TransFromDate', '', null, -30);
46         date_cells(_("To:"), 'TransToDate');
47         if ($dim >= 1)
48                 dimensions_list_cells(_("Dimension")." 1:", 'Dimension', null, true, " ", false, 1);
49         if ($dim > 1)
50                 dimensions_list_cells(_("Dimension")." 2:", 'Dimension2', null, true, " ", false, 2);
51         check_cells(_("No zero values"), 'NoZero', null);
52         check_cells(_("Only balances"), 'Balance', null);
53
54         submit_cells('Show',_("Show"),'','', 'default');
55     end_table();
56     end_form();
57 }
58
59 //----------------------------------------------------------------------------------------------------
60
61 function display_trial_balance()
62 {
63         global $path_to_root;
64
65         div_start('balance_tbl');
66         if (!isset($_POST['Dimension']))
67                 $_POST['Dimension'] = 0;
68         if (!isset($_POST['Dimension2']))
69                 $_POST['Dimension2'] = 0;
70         start_table(TABLESTYLE);
71         $tableheader =  "<tr>
72         <td rowspan=2 class='tableheader'>" . _("Account") . "</td>
73         <td rowspan=2 class='tableheader'>" . _("Account Name") . "</td>
74                 <td colspan=2 class='tableheader'>" . _("Brought Forward") . "</td>
75                 <td colspan=2 class='tableheader'>" . _("This Period") . "</td>
76                 <td colspan=2 class='tableheader'>" . _("Balance") . "</td>
77                 </tr><tr>
78                 <td class='tableheader'>" . _("Debit") . "</td>
79         <td class='tableheader'>" . _("Credit") . "</td>
80                 <td class='tableheader'>" . _("Debit") . "</td>
81                 <td class='tableheader'>" . _("Credit") . "</td>
82         <td class='tableheader'>" . _("Debit") . "</td>
83         <td class='tableheader'>" . _("Credit") . "</td>
84         </tr>";
85
86     echo $tableheader;
87
88         $k = 0;
89
90         $accounts = get_gl_accounts();
91         $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;
92         $begin = begin_fiscalyear();
93         if (date1_greater_date2($begin, $_POST['TransFromDate']))
94                 $begin = $_POST['TransFromDate'];
95         $begin = add_days($begin, -1);
96         
97         while ($account = db_fetch($accounts))
98         {
99                 $prev = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $_POST['TransFromDate'], false, false);
100                 $curr = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $_POST['TransFromDate'], $_POST['TransToDate'], true, true);
101                 $tot = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $_POST['TransToDate'], false, true);
102                 if (check_value("NoZero") && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
103                         continue;
104                 alt_table_row_color($k);
105
106                 $url = "<a href='$path_to_root/gl/inquiry/gl_account_inquiry.php?TransFromDate=" . $_POST["TransFromDate"] . "&TransToDate=" . $_POST["TransToDate"] . "&account=" . $account["account_code"] . "&Dimension=" . $_POST["Dimension"] . "&Dimension2=" . $_POST["Dimension2"] . "'>" . $account["account_code"] . "</a>";
107
108                 label_cell($url);
109                 label_cell($account["account_name"]);
110                 if (check_value('Balance'))
111                 {
112                         display_debit_or_credit_cells($prev['balance']);
113                         display_debit_or_credit_cells($curr['balance']);
114                         display_debit_or_credit_cells($tot['balance']);
115                         
116                 }
117                 else
118                 {
119                         amount_cell($prev['debit']);
120                         amount_cell($prev['credit']);
121                         amount_cell($curr['debit']);
122                         amount_cell($curr['credit']);
123                         amount_cell($tot['debit']);
124                         amount_cell($tot['credit']);
125                         $pdeb += $prev['debit'];
126                         $pcre += $prev['credit'];
127                         $cdeb += $curr['debit'];
128                         $ccre += $curr['credit'];
129                         $tdeb += $tot['debit'];
130                         $tcre += $tot['credit'];
131                 }       
132                 $pbal += $prev['balance'];
133                 $cbal += $curr['balance'];
134                 $tbal += $tot['balance'];
135                 end_row();
136         }
137
138         //$prev = get_balance(null, $begin, $_POST['TransFromDate'], false, false);
139         //$curr = get_balance(null, $_POST['TransFromDate'], $_POST['TransToDate'], true, true);
140         //$tot = get_balance(null, $begin, $_POST['TransToDate'], false, true);
141         if (!check_value('Balance'))
142         {
143                 start_row("class='inquirybg' style='font-weight:bold'");
144                 label_cell(_("Total") ." - ".$_POST['TransToDate'], "colspan=2");
145                 amount_cell($pdeb);
146                 amount_cell($pcre);
147                 amount_cell($cdeb);
148                 amount_cell($ccre);
149                 amount_cell($tdeb);
150                 amount_cell($tcre);
151                 end_row();
152         }       
153         start_row("class='inquirybg' style='font-weight:bold'");
154         label_cell(_("Ending Balance") ." - ".$_POST['TransToDate'], "colspan=2");
155         display_debit_or_credit_cells($pbal);
156         display_debit_or_credit_cells($cbal);
157         display_debit_or_credit_cells($tbal);
158         end_row();
159
160         end_table(1);
161         div_end();
162 }
163
164 //----------------------------------------------------------------------------------------------------
165
166 gl_inquiry_controls();
167
168 display_trial_balance();
169
170 //----------------------------------------------------------------------------------------------------
171
172 end_page();
173
174 ?>
175