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