0ba2443fa876f9535a0492ded41bd34295ff9085
[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     date_cells(_("From:"), 'TransFromDate');
55         date_cells(_("To:"), 'TransToDate');
56         if ($dim >= 1)
57                 dimensions_list_cells(_("Dimension")." 1:", 'Dimension', null, true, " ", false, 1);
58         if ($dim > 1)
59                 dimensions_list_cells(_("Dimension")." 2:", 'Dimension2', null, true, " ", false, 2);
60         check_cells(_("No zero values"), 'NoZero', null);
61         check_cells(_("Only balances"), 'Balance', null);
62
63         submit_cells('Show',_("Show"),'','', 'default');
64     end_table();
65     end_form();
66 }
67
68 //----------------------------------------------------------------------------------------------------
69
70 function display_trial_balance($type, $typename)
71 {
72         global $path_to_root, $SysPrefs,
73                  $k, $pdeb, $pcre, $cdeb, $ccre, $tdeb, $tcre, $pbal, $cbal, $tbal;
74
75         $printtitle = 0; //Flag for printing type name
76
77         $k = 0;
78
79         //Get Accounts directly under this group/type
80         $accounts = get_gl_accounts(null, null, $type);
81
82         $begin = get_fiscalyear_begin_for_date($_POST['TransFromDate']);
83         if (date1_greater_date2($begin, $_POST['TransFromDate']))
84                 $begin = $_POST['TransFromDate'];
85         $begin = add_days($begin, -1);
86
87         while ($account = db_fetch($accounts))
88         {
89                 //Print Type Title if it has atleast one non-zero account       
90                 if (!$printtitle)
91                 {
92                         start_row("class='inquirybg' style='font-weight:bold'");
93                         label_cell(_("Group")." - ".$type ." - ".$typename, "colspan=8");
94                         end_row();
95                         $printtitle = 1;
96                 }
97
98                 // 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.
99                 // 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.
100                 if (@$SysPrefs->clear_trial_balance_opening)
101                 {
102                         $open = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin,  $begin, false, true);
103                         $offset = min($open['debit'], $open['credit']);
104                 } else
105                         $offset = 0;
106
107                 $prev = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $_POST['TransFromDate'], false, false);
108                 $curr = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $_POST['TransFromDate'], $_POST['TransToDate'], true, true);
109                 $tot = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $_POST['TransToDate'], false, true);
110                 if (check_value("NoZero") && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
111                         continue;
112                 alt_table_row_color($k);
113
114                 $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>";
115
116                 label_cell($url);
117                 label_cell($account["account_name"]);
118                 if (check_value('Balance'))
119                 {
120                         display_debit_or_credit_cells($prev['balance']);
121                         display_debit_or_credit_cells($curr['balance']);
122                         display_debit_or_credit_cells($tot['balance']);
123
124                 }
125                 else
126                 {
127                         amount_cell($prev['debit']-$offset);
128                         amount_cell($prev['credit']-$offset);
129                         amount_cell($curr['debit']);
130                         amount_cell($curr['credit']);
131                         amount_cell($tot['debit']-$offset);
132                         amount_cell($tot['credit']-$offset);
133                         $pdeb += $prev['debit'];
134                         $pcre += $prev['credit'];
135                         $cdeb += $curr['debit'];
136                         $ccre += $curr['credit'];
137                         $tdeb += $tot['debit'];
138                         $tcre += $tot['credit'];
139                 }       
140                 $pbal += $prev['balance'];
141                 $cbal += $curr['balance'];
142                 $tbal += $tot['balance'];
143                 end_row();
144         }
145
146         //Get Account groups/types under this group/type
147         $result = get_account_types(false, false, $type);
148         while ($accounttype=db_fetch($result))
149         {
150                 //Print Type Title if has sub types and not previously printed
151                 if (!$printtitle)
152                 {
153                         start_row("class='inquirybg' style='font-weight:bold'");
154                         label_cell(_("Group")." - ".$type ." - ".$typename, "colspan=8");
155                         end_row();
156                         $printtitle = 1;
157
158                 }
159                 display_trial_balance($accounttype["id"], $accounttype["name"].' ('.$typename.')');
160         }
161 }
162
163 //----------------------------------------------------------------------------------------------------
164
165 gl_inquiry_controls();
166
167 if (isset($_POST['TransFromDate']))
168 {
169         $row = get_current_fiscalyear();
170         if (date1_greater_date2($_POST['TransFromDate'], sql2date($row['end'])))
171         {
172                 display_error(_("The from date cannot be bigger than the fiscal year end."));
173                 set_focus('TransFromDate');
174                 return;
175         }
176 }
177 div_start('balance_tbl');
178 if (!isset($_POST['Dimension']))
179         $_POST['Dimension'] = 0;
180 if (!isset($_POST['Dimension2']))
181         $_POST['Dimension2'] = 0;
182 start_table(TABLESTYLE);
183 $tableheader =  "<tr>
184         <td rowspan=2 class='tableheader'>" . _("Account") . "</td>
185         <td rowspan=2 class='tableheader'>" . _("Account Name") . "</td>
186         <td colspan=2 class='tableheader'>" . _("Brought Forward") . "</td>
187         <td colspan=2 class='tableheader'>" . _("This Period") . "</td>
188         <td colspan=2 class='tableheader'>" . _("Balance") . "</td>
189         </tr><tr>
190         <td class='tableheader'>" . _("Debit") . "</td>
191         <td class='tableheader'>" . _("Credit") . "</td>
192         <td class='tableheader'>" . _("Debit") . "</td>
193         <td class='tableheader'>" . _("Credit") . "</td>
194         <td class='tableheader'>" . _("Debit") . "</td>
195         <td class='tableheader'>" . _("Credit") . "</td>
196         </tr>";
197
198 echo $tableheader;
199
200 //display_trial_balance();
201
202 $classresult = get_account_classes(false);
203 while ($class = db_fetch($classresult))
204 {
205         start_row("class='inquirybg' style='font-weight:bold'");
206         label_cell(_("Class")." - ".$class['cid'] ." - ".$class['class_name'], "colspan=8");
207         end_row();
208
209         //Get Account groups/types under this group/type with no parents
210         $typeresult = get_account_types(false, $class['cid'], -1);
211         while ($accounttype=db_fetch($typeresult))
212         {
213                 display_trial_balance($accounttype["id"], $accounttype["name"]);
214         }
215 }
216
217         if (!check_value('Balance'))
218         {
219                 start_row("class='inquirybg' style='font-weight:bold'");
220                 label_cell(_("Total") ." - ".$_POST['TransToDate'], "colspan=2");
221                 amount_cell($pdeb);
222                 amount_cell($pcre);
223                 amount_cell($cdeb);
224                 amount_cell($ccre);
225                 amount_cell($tdeb);
226                 amount_cell($tcre);
227                 end_row();
228         }       
229         start_row("class='inquirybg' style='font-weight:bold'");
230         label_cell(_("Ending Balance") ." - ".$_POST['TransToDate'], "colspan=2");
231         display_debit_or_credit_cells($pbal);
232         display_debit_or_credit_cells($cbal);
233         display_debit_or_credit_cells($tbal);
234         end_row();
235
236         end_table(1);
237         if (($pbal = round2($pbal, user_price_dec())) != 0 && $_POST['Dimension'] == 0 && $_POST['Dimension2'] == 0)
238                 display_warning(_("The Opening Balance is not in balance, probably due to a non closed Previous Fiscalyear."));
239         div_end();
240
241 //----------------------------------------------------------------------------------------------------
242
243 end_page();
244