Group Total in Trial Balance on Screen.
[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/ui.inc");
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/admin/db/fiscalyears_db.inc");
20 include_once($path_to_root . "/includes/data_checks.inc");
21
22 include_once($path_to_root . "/gl/includes/gl_db.inc");
23
24 $js = "";
25 if (user_use_date_picker())
26         $js = get_js_date_picker();
27
28 page(_($help_context = "Trial Balance"), false, false, "", $js);
29
30 $k = 0;
31 $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;
32
33 //----------------------------------------------------------------------------------------------------
34 // Ajax updates
35 //
36 if (get_post('Show'))
37 {
38         $Ajax->activate('balance_tbl');
39 }
40
41
42 function gl_inquiry_controls()
43 {
44         $dim = get_company_pref('use_dimension');
45     start_form();
46
47     start_table(TABLESTYLE_NOBORDER);
48
49         $date = today();
50         if (!isset($_POST['TransToDate']))
51                 $_POST['TransToDate'] = end_month($date);
52         if (!isset($_POST['TransFromDate']))
53                 $_POST['TransFromDate'] = add_days(end_month($date), -user_transaction_days());
54         start_row();    
55     date_cells(_("From:"), 'TransFromDate');
56         date_cells(_("To:"), 'TransToDate');
57         if ($dim >= 1)
58                 dimensions_list_cells(_("Dimension")." 1:", 'Dimension', null, true, " ", false, 1);
59         if ($dim > 1)
60                 dimensions_list_cells(_("Dimension")." 2:", 'Dimension2', null, true, " ", false, 2);
61         check_cells(_("No zero values"), 'NoZero', null);
62         check_cells(_("Only balances"), 'Balance', null);
63         submit_cells('Show',_("Show"),'','', 'default');
64         end_row();
65     end_table();
66     end_form();
67 }
68
69 //----------------------------------------------------------------------------------------------------
70
71 function display_trial_balance($type, $typename)
72 {
73         global $path_to_root, $SysPrefs,
74                  $k, $pdeb, $pcre, $cdeb, $ccre, $tdeb, $tcre, $pbal, $cbal, $tbal;
75
76         $printtitle = 0; //Flag for printing type name
77
78         $k = 0;
79
80         //Get Accounts directly under this group/type
81         $accounts = get_gl_accounts(null, null, $type);
82
83         $begin = get_fiscalyear_begin_for_date($_POST['TransFromDate']);
84         if (date1_greater_date2($begin, $_POST['TransFromDate']))
85                 $begin = $_POST['TransFromDate'];
86         $begin = add_days($begin, -1);
87
88         $Apdeb=$pdeb;
89         $Apcre=$pcre;
90         $Acdeb=$cdeb;
91         $Accre=$ccre;
92         $Atdeb=$tdeb;
93         $Atcre=$tcre;
94         $Apbal=$pbal;
95         $Acbal=$cbal;
96         $Atbal=$tbal;
97
98         while ($account = db_fetch($accounts))
99         {
100                 //Print Type Title if it has atleast one non-zero account
101                 if (!$printtitle)
102                 {
103                         start_row("class='inquirybg' style='font-weight:bold'");
104                         label_cell(_("Group")." - ".$type ." - ".$typename, "colspan=8");
105                         end_row();
106                         $printtitle = 1;
107                 }
108
109                 // FA doesn't really clear the closed year, therefore the brought forward balance includes all the transactions from the past, even though the balance is null.
110                 // If we want to remove the balanced part for the past years, this option removes the common part from from the prev and tot figures.
111                 if (@$SysPrefs->clear_trial_balance_opening)
112                 {
113                         $open = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin,  $begin, false, true);
114                         $offset = min($open['debit'], $open['credit']);
115                 } else
116                         $offset = 0;
117
118                 $prev = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $_POST['TransFromDate'], false, false);
119                 $curr = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $_POST['TransFromDate'], $_POST['TransToDate'], true, true);
120                 $tot = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $_POST['TransToDate'], false, true);
121                 if (check_value("NoZero") && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
122                         continue;
123                 alt_table_row_color($k);
124
125                 $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>";
126
127                 label_cell($url);
128                 label_cell($account["account_name"]);
129                 if (check_value('Balance'))
130                 {
131                         display_debit_or_credit_cells($prev['balance']);
132                         display_debit_or_credit_cells($curr['balance']);
133                         display_debit_or_credit_cells($tot['balance']);
134
135                 }
136                 else
137                 {
138                         amount_cell($prev['debit']-$offset);
139                         amount_cell($prev['credit']-$offset);
140                         amount_cell($curr['debit']);
141                         amount_cell($curr['credit']);
142                         amount_cell($tot['debit']-$offset);
143                         amount_cell($tot['credit']-$offset);
144                         $pdeb += $prev['debit'];
145                         $pcre += $prev['credit'];
146                         $cdeb += $curr['debit'];
147                         $ccre += $curr['credit'];
148                         $tdeb += $tot['debit'];
149                         $tcre += $tot['credit'];
150                 }
151                 $pbal += $prev['balance'];
152                 $cbal += $curr['balance'];
153                 $tbal += $tot['balance'];
154                 end_row();
155         }
156
157         //Get Account groups/types under this group/type
158         $result = get_account_types(false, false, $type);
159         while ($accounttype=db_fetch($result))
160         {
161                 //Print Type Title if has sub types and not previously printed
162                 if (!$printtitle)
163                 {
164                         start_row("class='inquirybg' style='font-weight:bold'");
165                         label_cell(_("Group")." - ".$type ." - ".$typename, "colspan=8");
166                         end_row();
167                         $printtitle = 1;
168
169                 }
170                 display_trial_balance($accounttype["id"], $accounttype["name"].' ('.$typename.')');
171         }
172
173         start_row("class='inquirybg' style='font-weight:bold'");
174         label_cell(_("Total") ." - ".$typename, "colspan=2");
175
176         if (!check_value('Balance'))
177         {
178                 amount_cell($pdeb-$Apdeb );
179                 amount_cell($pcre-$Apcre);
180                 amount_cell($cdeb-$Acdeb );
181                 amount_cell($ccre-$Accre );
182                 amount_cell($tdeb-$Atdeb );
183                 amount_cell($tcre-$Atcre);
184         }
185         else
186         {
187                 display_debit_or_credit_cells($pbal-$Apbal);
188                 display_debit_or_credit_cells($cbal-$Acbal );
189                 display_debit_or_credit_cells($tbal-$Atbal);
190         }
191         end_row();
192 }
193
194 //----------------------------------------------------------------------------------------------------
195
196 gl_inquiry_controls();
197
198 if (isset($_POST['TransFromDate']))
199 {
200         $row = get_current_fiscalyear();
201         if (date1_greater_date2($_POST['TransFromDate'], sql2date($row['end'])))
202         {
203                 display_error(_("The from date cannot be bigger than the fiscal year end."));
204                 set_focus('TransFromDate');
205                 return;
206         }
207 }
208 div_start('balance_tbl');
209 if (!isset($_POST['Dimension']))
210         $_POST['Dimension'] = 0;
211 if (!isset($_POST['Dimension2']))
212         $_POST['Dimension2'] = 0;
213 start_table(TABLESTYLE);
214 $tableheader =  "<tr>
215         <td rowspan=2 class='tableheader'>" . _("Account") . "</td>
216         <td rowspan=2 class='tableheader'>" . _("Account Name") . "</td>
217         <td colspan=2 class='tableheader'>" . _("Brought Forward") . "</td>
218         <td colspan=2 class='tableheader'>" . _("This Period") . "</td>
219         <td colspan=2 class='tableheader'>" . _("Balance") . "</td>
220         </tr><tr>
221         <td class='tableheader'>" . _("Debit") . "</td>
222         <td class='tableheader'>" . _("Credit") . "</td>
223         <td class='tableheader'>" . _("Debit") . "</td>
224         <td class='tableheader'>" . _("Credit") . "</td>
225         <td class='tableheader'>" . _("Debit") . "</td>
226         <td class='tableheader'>" . _("Credit") . "</td>
227         </tr>";
228
229 echo $tableheader;
230
231 //display_trial_balance();
232
233 $classresult = get_account_classes(false);
234 while ($class = db_fetch($classresult))
235 {
236         start_row("class='inquirybg' style='font-weight:bold'");
237         label_cell(_("Class")." - ".$class['cid'] ." - ".$class['class_name'], "colspan=8");
238         end_row();
239
240         //Get Account groups/types under this group/type with no parents
241         $typeresult = get_account_types(false, $class['cid'], -1);
242         while ($accounttype=db_fetch($typeresult))
243         {
244                 display_trial_balance($accounttype["id"], $accounttype["name"]);
245         }
246 }
247
248 if (!check_value('Balance'))
249 {
250         start_row("class='inquirybg' style='font-weight:bold'");
251         label_cell(_("Total") ." - ".$_POST['TransToDate'], "colspan=2");
252         amount_cell($pdeb);
253         amount_cell($pcre);
254         amount_cell($cdeb);
255         amount_cell($ccre);
256         amount_cell($tdeb);
257         amount_cell($tcre);
258         end_row();
259 }
260 start_row("class='inquirybg' style='font-weight:bold'");
261 label_cell(_("Ending Balance") ." - ".$_POST['TransToDate'], "colspan=2");
262 display_debit_or_credit_cells($pbal);
263 display_debit_or_credit_cells($cbal);
264 display_debit_or_credit_cells($tbal);
265 end_row();
266
267 end_table(1);
268 if (($pbal = round2($pbal, user_price_dec())) != 0 && $_POST['Dimension'] == 0 && $_POST['Dimension2'] == 0)
269         display_warning(_("The Opening Balance is not in balance, probably due to a non closed Previous Fiscalyear."));
270 div_end();
271
272 //----------------------------------------------------------------------------------------------------
273
274 end_page();
275