48deeb972da8b019164266b2422b52b7161280ed
[fa-stable.git] / admin / fiscalyears.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 = 9;
13 $path_to_root="..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 include_once($path_to_root . "/includes/date_functions.inc");
17 include_once($path_to_root . "/admin/db/company_db.inc");
18 include_once($path_to_root . "/includes/ui.inc");
19 $js = "";
20 if ($use_date_picker)
21         $js .= get_js_date_picker();
22 page(_("Fiscal Years"), false, false, "", $js);
23
24 simple_page_mode(true);
25 //---------------------------------------------------------------------------------------------
26
27 function is_date_in_fiscalyears($date)
28 {
29         $date = date2sql($date);
30         $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE '$date' >= begin AND '$date' <= end";
31
32         $result = db_query($sql, "could not get all fiscal years");
33         return db_fetch($result) !== false;
34 }
35
36 function is_bad_begin_date($date)
37 {
38         $bdate = date2sql($date);
39         $sql = "SELECT MAX(end) FROM ".TB_PREF."fiscal_year WHERE begin < '$bdate'";
40
41         $result = db_query($sql, "could not retrieve last fiscal years");
42         $row = db_fetch_row($result);
43         if ($row[0] === null)
44                 return false;
45         $max = add_days(sql2date($row[0]), 1);
46         return ($max !== $date);
47 }
48
49 function check_open_before($date)
50 {
51         $date = date2sql($date);
52         $sql = "SELECT COUNT(*) FROM ".TB_PREF."fiscal_year WHERE begin < '$date' AND closed=0";
53
54         $result = db_query($sql, "could not check open fiscal years");
55         $row = db_fetch_row($result);
56         return ($row[0] > 0);
57 }
58
59 function check_data()
60 {
61         if (!is_date($_POST['from_date']) || is_date_in_fiscalyears($_POST['from_date']) || is_bad_begin_date($_POST['from_date']))
62         {
63                 display_error( _("Invalid BEGIN date in fiscal year."));
64                 set_focus('from_date');
65                 return false;
66         }
67         if (!is_date($_POST['to_date']) || is_date_in_fiscalyears($_POST['to_date']))
68         {
69                 display_error( _("Invalid END date in fiscal year."));
70                 set_focus('to_date');
71                 return false;
72         }
73         if (date1_greater_date2($_POST['from_date'], $_POST['to_date']))
74         {
75                 display_error( _("BEGIN date bigger than END date."));
76                 set_focus('from_date');
77                 return false;
78         }
79         return true;
80 }
81 //---------------------------------------------------------------------------------------------
82 function close_year($year)
83 {
84         $myrow = get_fiscalyear($year);
85         $to = $myrow['end'];
86         // retrieve total balances from balance sheet accounts
87     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans INNER JOIN ".TB_PREF."chart_master ON account=account_code
88         INNER JOIN ".TB_PREF."chart_types ON account_type=id INNER JOIN ".TB_PREF."chart_class ON class_id=cid 
89                 WHERE balance_sheet=1 AND tran_date <= '$to'";
90         $result = db_query($sql, "The total balance could not be calculated");
91
92         $row = db_fetch_row($result);
93         $balance = round2($row[0], user_price_dec());
94
95         begin_transaction();
96         $to = sql2date($to);
97
98         if ($balance != 0.0)
99         {
100                 $co = get_company_prefs();
101
102                 $trans_type = systypes::journal_entry();
103                 $trans_id = get_next_trans_no($trans_type);
104
105                 add_gl_trans($trans_type, $trans_id, $to, $co['retained_earnings_act'],
106                         0, 0, _("Closing Year"), -$balance);
107                 add_gl_trans($trans_type, $trans_id, $to, $co['profit_loss_year_act'],
108                         0, 0, _("Closing Year"), $balance);
109
110         }       
111                 close_transactions($to);
112                 commit_transaction();
113 }
114
115 function open_year($year)
116 {
117         $myrow = get_fiscalyear($year);
118         $from = sql2date($myrow['begin']);
119
120         begin_transaction();
121         open_transactions($from);
122         commit_transaction();
123 }
124
125 function handle_submit()
126 {
127         global $selected_id, $Mode;
128
129         if ($selected_id != -1)
130         {
131                 if ($_POST['closed'] == 1)
132                 {
133                         if (check_open_before($_POST['from_date']))
134                         {
135                                 display_error( _("Cannot CLOSE this year because there are open fiscal years before"));
136                                 set_focus('closed');
137                                 return false;
138                         }       
139                         close_year($selected_id);
140                 }       
141                 else
142                         open_year($selected_id);
143                 update_fiscalyear($selected_id, $_POST['closed']);
144                 display_notification(_('Selected fiscal year has been updated'));
145         }
146         else
147         {
148                 if (!check_data())
149                         return false;
150                 add_fiscalyear($_POST['from_date'], $_POST['to_date'], $_POST['closed']);
151                 display_notification(_('New fiscal year has been added'));
152         }
153         $Mode = 'RESET';
154 }
155
156 //---------------------------------------------------------------------------------------------
157
158 function check_can_delete($selected_id)
159 {
160         $myrow = get_fiscalyear($selected_id);
161         // PREVENT DELETES IF DEPENDENT RECORDS IN gl_trans
162         $from = $myrow['begin'];
163         $to = $myrow['end'];
164         $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE tran_date >= '$from' AND tran_date <= '$to'";
165         $result = db_query($sql, "could not query gl_trans master");
166         $myrow = db_fetch_row($result);
167         if ($myrow[0] > 0)
168         {
169                 display_error(_("Cannot delete this fiscal year because items have been created referring to it."));
170                 return false;
171         }
172
173         return true;
174 }
175
176 //---------------------------------------------------------------------------------------------
177
178 function handle_delete()
179 {
180         global $selected_id, $Mode;
181
182         if (check_can_delete($selected_id)) {
183         //only delete if used in neither customer or supplier, comp prefs, bank trans accounts
184                 delete_fiscalyear($selected_id);
185                 display_notification(_('Selected fiscal year has been deleted'));
186         }
187         $Mode = 'RESET';
188 }
189
190 //---------------------------------------------------------------------------------------------
191
192 function display_fiscalyears()
193 {
194         global $table_style;
195
196         $company_year = get_company_pref('f_year');
197
198         $result = get_all_fiscalyears();
199         start_form();
200         start_table($table_style);
201
202         $th = array(_("Fiscal Year Begin"), _("Fiscal Year End"), _("Closed"), "", "");
203         table_header($th);
204
205         $k=0;
206         while ($myrow=db_fetch($result))
207         {
208         if ($myrow['id'] == $company_year)
209         {
210                 start_row("class='stockmankobg'");
211         }
212         else
213                 alt_table_row_color($k);
214
215                 $from = sql2date($myrow["begin"]);
216                 $to = sql2date($myrow["end"]);
217                 if ($myrow["closed"] == 0)
218                 {
219                         $closed_text = _("No");
220                 }
221                 else
222                 {
223                         $closed_text = _("Yes");
224                 }
225                 label_cell($from);
226                 label_cell($to);
227                 label_cell($closed_text);
228                 edit_button_cell("Edit".$myrow['id'], _("Edit"));
229                 if ($myrow["id"] != $company_year)
230                         delete_button_cell("Delete".$myrow['id'], _("Delete"));
231                 else
232                         label_cell('');
233                 end_row();
234         }
235
236         end_table();
237         end_form();
238         display_note(_("The marked fiscal year is the current fiscal year which cannot be deleted."), 0, 0, "class='currentfg'");
239 }
240
241 //---------------------------------------------------------------------------------------------
242
243 function display_fiscalyear_edit($selected_id)
244 {
245         global $table_style2, $Mode;
246
247         start_form();
248         start_table($table_style2);
249
250         if ($selected_id != -1)
251         {
252                 if($Mode =='Edit')
253                 {
254                         $myrow = get_fiscalyear($selected_id);
255
256                         $_POST['from_date'] = sql2date($myrow["begin"]);
257                         $_POST['to_date']  = sql2date($myrow["end"]);
258                         $_POST['closed']  = $myrow["closed"];
259                 }
260                 hidden('from_date');
261                 hidden('to_date');
262                 label_row(_("Fiscal Year Begin:"), $_POST['from_date']);
263                 label_row(_("Fiscal Year End:"), $_POST['to_date']);
264         }
265         else
266         {
267                 date_row(_("Fiscal Year Begin:"), 'from_date', '', null, 0, 0, 1001);
268                 date_row(_("Fiscal Year End:"), 'to_date', '', null, 0, 0, 1001);
269         }
270         hidden('selected_id', $selected_id);
271
272         yesno_list_row(_("Is Closed:"), 'closed', null, "", "", false);
273
274         end_table(1);
275
276         submit_add_or_update_center($selected_id == -1, '', 'both');
277
278         end_form();
279 }
280
281 //---------------------------------------------------------------------------------------------
282
283 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
284 {
285         handle_submit();
286 }
287
288 //---------------------------------------------------------------------------------------------
289
290 if ($Mode == 'Delete')
291 {
292         global $selected_id;
293         handle_delete($selected_id);
294 }
295
296 if ($Mode == 'RESET')
297 {
298         $selected_id = -1;
299 }
300 //---------------------------------------------------------------------------------------------
301
302 display_fiscalyears();
303
304 echo '<br>';
305
306 display_fiscalyear_edit($selected_id);
307
308 //---------------------------------------------------------------------------------------------
309
310 end_page();
311
312 ?>