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