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 ***********************************************************************/
14 include_once($path_to_root . "/includes/session.inc");
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");
21 $js .= get_js_date_picker();
22 page(_("Fiscal Years"), false, false, "", $js);
24 simple_page_mode(true);
25 //---------------------------------------------------------------------------------------------
27 function is_date_in_fiscalyears($date)
29 $date = date2sql($date);
30 $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE '$date' >= begin AND '$date' <= end";
32 $result = db_query($sql, "could not get all fiscal years");
33 return db_fetch($result) !== false;
36 function is_bad_begin_date($date)
38 $bdate = date2sql($date);
39 $sql = "SELECT MAX(end) FROM ".TB_PREF."fiscal_year WHERE begin < '$bdate'";
41 $result = db_query($sql, "could not retrieve last fiscal years");
42 $row = db_fetch_row($result);
45 $max = add_days(sql2date($row[0]), 1);
46 return ($max !== $date);
49 function check_open_before($date)
51 $date = date2sql($date);
52 $sql = "SELECT COUNT(*) FROM ".TB_PREF."fiscal_year WHERE begin < '$date' AND closed=0";
54 $result = db_query($sql, "could not check open fiscal years");
55 $row = db_fetch_row($result);
61 if (!is_date($_POST['from_date']) || is_date_in_fiscalyears($_POST['from_date']) || is_bad_begin_date($_POST['from_date']))
63 display_error( _("Invalid BEGIN date in fiscal year."));
64 set_focus('from_date');
67 if (!is_date($_POST['to_date']) || is_date_in_fiscalyears($_POST['to_date']))
69 display_error( _("Invalid END date in fiscal year."));
73 if (date1_greater_date2($_POST['from_date'], $_POST['to_date']))
75 display_error( _("BEGIN date bigger than END date."));
76 set_focus('from_date');
81 //---------------------------------------------------------------------------------------------
82 function close_year($year)
84 $myrow = get_fiscalyear($year);
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");
92 $row = db_fetch_row($result);
93 $balance = round2($row[0], user_price_dec());
100 $co = get_company_prefs();
102 $trans_type = systypes::journal_entry();
103 $trans_id = get_next_trans_no($trans_type);
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);
111 close_transactions($to);
112 commit_transaction();
115 function open_year($year)
117 $myrow = get_fiscalyear($year);
118 $from = sql2date($myrow['begin']);
121 open_transactions($from);
122 commit_transaction();
125 function handle_submit()
127 global $selected_id, $Mode;
129 if ($selected_id != -1)
131 if ($_POST['closed'] == 1)
133 if (check_open_before($_POST['from_date']))
135 display_error( _("Cannot CLOSE this year because there are open fiscal years before"));
139 close_year($selected_id);
142 open_year($selected_id);
143 update_fiscalyear($selected_id, $_POST['closed']);
144 display_notification(_('Selected fiscal year has been updated'));
150 add_fiscalyear($_POST['from_date'], $_POST['to_date'], $_POST['closed']);
151 display_notification(_('New fiscal year has been added'));
156 //---------------------------------------------------------------------------------------------
158 function check_can_delete($selected_id)
160 $myrow = get_fiscalyear($selected_id);
161 // PREVENT DELETES IF DEPENDENT RECORDS IN gl_trans
162 $from = $myrow['begin'];
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);
169 display_error(_("Cannot delete this fiscal year because items have been created referring to it."));
176 //---------------------------------------------------------------------------------------------
178 function handle_delete()
180 global $selected_id, $Mode;
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'));
190 //---------------------------------------------------------------------------------------------
192 function display_fiscalyears()
196 $company_year = get_company_pref('f_year');
198 $result = get_all_fiscalyears();
200 start_table($table_style);
202 $th = array(_("Fiscal Year Begin"), _("Fiscal Year End"), _("Closed"), "", "");
206 while ($myrow=db_fetch($result))
208 if ($myrow['id'] == $company_year)
210 start_row("class='stockmankobg'");
213 alt_table_row_color($k);
215 $from = sql2date($myrow["begin"]);
216 $to = sql2date($myrow["end"]);
217 if ($myrow["closed"] == 0)
219 $closed_text = _("No");
223 $closed_text = _("Yes");
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"));
238 display_note(_("The marked fiscal year is the current fiscal year which cannot be deleted."), 0, 0, "class='currentfg'");
241 //---------------------------------------------------------------------------------------------
243 function display_fiscalyear_edit($selected_id)
245 global $table_style2, $Mode;
248 start_table($table_style2);
250 if ($selected_id != -1)
254 $myrow = get_fiscalyear($selected_id);
256 $_POST['from_date'] = sql2date($myrow["begin"]);
257 $_POST['to_date'] = sql2date($myrow["end"]);
258 $_POST['closed'] = $myrow["closed"];
262 label_row(_("Fiscal Year Begin:"), $_POST['from_date']);
263 label_row(_("Fiscal Year End:"), $_POST['to_date']);
267 date_row(_("Fiscal Year Begin:"), 'from_date', '', null, 0, 0, 1001);
268 date_row(_("Fiscal Year End:"), 'to_date', '', null, 0, 0, 1001);
270 hidden('selected_id', $selected_id);
272 yesno_list_row(_("Is Closed:"), 'closed', null, "", "", false);
276 submit_add_or_update_center($selected_id == -1, '', 'both');
281 //---------------------------------------------------------------------------------------------
283 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
288 //---------------------------------------------------------------------------------------------
290 if ($Mode == 'Delete')
293 handle_delete($selected_id);
296 if ($Mode == 'RESET')
300 //---------------------------------------------------------------------------------------------
302 display_fiscalyears();
306 display_fiscalyear_edit($selected_id);
308 //---------------------------------------------------------------------------------------------