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