c0a3e56ab1c15108466eb271e9c8250a5bf252eb
[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 //----------------------------------------------------------------------------------------------------
31 // Ajax updates
32 //
33 if (get_post('Show')) 
34 {
35         $Ajax->activate('balance_tbl');
36 }
37
38
39 function gl_inquiry_controls()
40 {
41         $dim = get_company_pref('use_dimension');
42     start_form();
43
44     start_table(TABLESTYLE_NOBORDER);
45
46     date_cells(_("From:"), 'TransFromDate', '', null, -30);
47         date_cells(_("To:"), 'TransToDate');
48         if ($dim >= 1)
49                 dimensions_list_cells(_("Dimension")." 1:", 'Dimension', null, true, " ", false, 1);
50         if ($dim > 1)
51                 dimensions_list_cells(_("Dimension")." 2:", 'Dimension2', null, true, " ", false, 2);
52         check_cells(_("No zero values"), 'NoZero', null);
53         check_cells(_("Only balances"), 'Balance', null);
54
55         submit_cells('Show',_("Show"),'','', 'default');
56     end_table();
57     end_form();
58 }
59
60 //----------------------------------------------------------------------------------------------------
61
62 function display_trial_balance()
63 {
64         global $path_to_root;
65
66         if (isset($_POST['TransFromDate']))
67         {
68                 $row = get_current_fiscalyear();
69                 if (date1_greater_date2($_POST['TransFromDate'], sql2date($row['end'])))
70                 {
71                         display_error(_("The from date cannot be bigger than the fiscal year end."));
72                         set_focus('TransFromDate');
73                         return;
74                 }       
75         }       
76         div_start('balance_tbl');
77         if (!isset($_POST['Dimension']))
78                 $_POST['Dimension'] = 0;
79         if (!isset($_POST['Dimension2']))
80                 $_POST['Dimension2'] = 0;
81         start_table(TABLESTYLE);
82         $tableheader =  "<tr>
83         <td rowspan=2 class='tableheader'>" . _("Account") . "</td>
84         <td rowspan=2 class='tableheader'>" . _("Account Name") . "</td>
85                 <td colspan=2 class='tableheader'>" . _("Brought Forward") . "</td>
86                 <td colspan=2 class='tableheader'>" . _("This Period") . "</td>
87                 <td colspan=2 class='tableheader'>" . _("Balance") . "</td>
88                 </tr><tr>
89                 <td class='tableheader'>" . _("Debit") . "</td>
90         <td class='tableheader'>" . _("Credit") . "</td>
91                 <td class='tableheader'>" . _("Debit") . "</td>
92                 <td class='tableheader'>" . _("Credit") . "</td>
93         <td class='tableheader'>" . _("Debit") . "</td>
94         <td class='tableheader'>" . _("Credit") . "</td>
95         </tr>";
96
97     echo $tableheader;
98
99         $k = 0;
100
101         $accounts = get_gl_accounts();
102         $pdeb = $pcre = $cdeb = $ccre = $tdeb = $tcre = $pbal = $cbal = $tbal = 0;
103         $begin = begin_fiscalyear();
104         if (date1_greater_date2($begin, $_POST['TransFromDate']))
105                 $begin = $_POST['TransFromDate'];
106         $begin = add_days($begin, -1);
107         
108         while ($account = db_fetch($accounts))
109         {
110                 $prev = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $_POST['TransFromDate'], false, false);
111                 $curr = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $_POST['TransFromDate'], $_POST['TransToDate'], true, true);
112                 $tot = get_balance($account["account_code"], $_POST['Dimension'], $_POST['Dimension2'], $begin, $_POST['TransToDate'], false, true);
113                 if (check_value("NoZero") && !$prev['balance'] && !$curr['balance'] && !$tot['balance'])
114                         continue;
115                 alt_table_row_color($k);
116
117                 $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>";
118
119                 label_cell($url);
120                 label_cell($account["account_name"]);
121                 if (check_value('Balance'))
122                 {
123                         display_debit_or_credit_cells($prev['balance']);
124                         display_debit_or_credit_cells($curr['balance']);
125                         display_debit_or_credit_cells($tot['balance']);
126                         
127                 }
128                 else
129                 {
130                         amount_cell($prev['debit']);
131                         amount_cell($prev['credit']);
132                         amount_cell($curr['debit']);
133                         amount_cell($curr['credit']);
134                         amount_cell($tot['debit']);
135                         amount_cell($tot['credit']);
136                         $pdeb += $prev['debit'];
137                         $pcre += $prev['credit'];
138                         $cdeb += $curr['debit'];
139                         $ccre += $curr['credit'];
140                         $tdeb += $tot['debit'];
141                         $tcre += $tot['credit'];
142                 }       
143                 $pbal += $prev['balance'];
144                 $cbal += $curr['balance'];
145                 $tbal += $tot['balance'];
146                 end_row();
147         }
148
149         //$prev = get_balance(null, $begin, $_POST['TransFromDate'], false, false);
150         //$curr = get_balance(null, $_POST['TransFromDate'], $_POST['TransToDate'], true, true);
151         //$tot = get_balance(null, $begin, $_POST['TransToDate'], false, true);
152         if (!check_value('Balance'))
153         {
154                 start_row("class='inquirybg' style='font-weight:bold'");
155                 label_cell(_("Total") ." - ".$_POST['TransToDate'], "colspan=2");
156                 amount_cell($pdeb);
157                 amount_cell($pcre);
158                 amount_cell($cdeb);
159                 amount_cell($ccre);
160                 amount_cell($tdeb);
161                 amount_cell($tcre);
162                 end_row();
163         }       
164         start_row("class='inquirybg' style='font-weight:bold'");
165         label_cell(_("Ending Balance") ." - ".$_POST['TransToDate'], "colspan=2");
166         display_debit_or_credit_cells($pbal);
167         display_debit_or_credit_cells($cbal);
168         display_debit_or_credit_cells($tbal);
169         end_row();
170
171         end_table(1);
172         if (($pbal = round2($pbal, user_price_dec()))  != 0)
173                 display_warning(_("The Opening Balance is not in balance, probably due to a non closed Previous Fiscalyear."));
174         div_end();
175 }
176
177 //----------------------------------------------------------------------------------------------------
178
179 gl_inquiry_controls();
180
181 display_trial_balance();
182
183 //----------------------------------------------------------------------------------------------------
184
185 end_page();
186
187 ?>
188