Better Trial Balance Inquiry and minor bug in date_functions.inc
[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;\r
31 $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;\r
32 $cls_pdeb = $cls_pcre = $cls_cdeb = $cls_ccre = $cls_tdeb = $cls_tcre = $cls_pbal = $cls_cbal = $cls_tbal = 0;\r
33 $grp_pdeb = $grp_pcre = $grp_cdeb = $grp_ccre = $grp_tdeb = $grp_tcre = $grp_pbal = $grp_cbal = $grp_tbal = 0;\r
34 \r
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)\r
68 {
69         global $path_to_root;
70         \r
71         global $k, $pdeb, $pcre, $cdeb, $ccre, $tdeb, $tcre, $pbal, $cbal, $tbal;\r
72         $printtitle = 0; //Flag for printing type name          \r
73
74         $k = 0;
75
76         //$accounts = get_gl_accounts();\r
77         //Get Accounts directly under this group/type\r
78         $accounts = get_gl_accounts(null, null, $type);         \r
79         \r
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       \r
89                 if (!$printtitle)\r
90                 {       \r
91                         start_row("class='inquirybg' style='font-weight:bold'");\r
92                         label_cell("Group - ".$type ." - ".$typename, "colspan=8");\r
93                         end_row();              \r
94                         $printtitle = 1;                \r
95                 }       \r
96         \r
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\r
137         $result = get_account_types(false, false, $type);\r
138         while ($accounttype=db_fetch($result))\r
139         {\r
140                 //Print Type Title if has sub types and not previously printed\r
141                 if (!$printtitle)\r
142                 {\r
143                         start_row("class='inquirybg' style='font-weight:bold'");\r
144                         label_cell("Group - ".$type ." - ".$typename, "colspan=8");\r
145                         end_row();              \r
146                         $printtitle = 1;                \r
147                 }\r
148                 display_trial_balance($accounttype["id"], $accounttype["name"].' ('.$typename.')');\r
149         }\r
150 }\r
151 \r
152 //----------------------------------------------------------------------------------------------------\r
153 \r
154 gl_inquiry_controls();\r
155 \r
156 if (isset($_POST['TransFromDate']))\r
157 {\r
158         $row = get_current_fiscalyear();\r
159         if (date1_greater_date2($_POST['TransFromDate'], sql2date($row['end'])))\r
160         {\r
161                 display_error(_("The from date cannot be bigger than the fiscal year end."));\r
162                 set_focus('TransFromDate');\r
163                 return;\r
164         }       \r
165 }       \r
166 div_start('balance_tbl');\r
167 if (!isset($_POST['Dimension']))\r
168         $_POST['Dimension'] = 0;\r
169 if (!isset($_POST['Dimension2']))\r
170         $_POST['Dimension2'] = 0;\r
171 start_table(TABLESTYLE);\r
172 $tableheader =  "<tr>\r
173         <td rowspan=2 class='tableheader'>" . _("Account") . "</td>\r
174         <td rowspan=2 class='tableheader'>" . _("Account Name") . "</td>\r
175         <td colspan=2 class='tableheader'>" . _("Brought Forward") . "</td>\r
176         <td colspan=2 class='tableheader'>" . _("This Period") . "</td>\r
177         <td colspan=2 class='tableheader'>" . _("Balance") . "</td>\r
178         </tr><tr>\r
179         <td class='tableheader'>" . _("Debit") . "</td>\r
180         <td class='tableheader'>" . _("Credit") . "</td>\r
181         <td class='tableheader'>" . _("Debit") . "</td>\r
182         <td class='tableheader'>" . _("Credit") . "</td>\r
183         <td class='tableheader'>" . _("Debit") . "</td>\r
184         <td class='tableheader'>" . _("Credit") . "</td>\r
185         </tr>";\r
186 \r
187 echo $tableheader;\r
188 \r
189 //display_trial_balance();\r
190 \r
191 $classresult = get_account_classes(false);\r
192 while ($class = db_fetch($classresult))\r
193 {\r
194         start_row("class='inquirybg' style='font-weight:bold'");\r
195         label_cell("Class - ".$class['cid'] ." - ".$class['class_name'], "colspan=8");\r
196         end_row();\r
197 \r
198         //Get Account groups/types under this group/type with no parents\r
199         $typeresult = get_account_types(false, $class['cid'], -1);\r
200         while ($accounttype=db_fetch($typeresult))\r
201         {\r
202                 display_trial_balance($accounttype["id"], $accounttype["name"]);\r
203         }\r
204 }\r
205 \r
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."));\r
231         div_end();
232
233 //----------------------------------------------------------------------------------------------------
234
235 end_page();
236
237 ?>
238