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