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