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;
38 if (!is_date($_POST['from_date']) || is_date_in_fiscalyears($_POST['from_date']))
40 display_error( _("Invalid BEGIN date in fiscal year."));
41 set_focus('from_date');
44 if (!is_date($_POST['to_date']) || is_date_in_fiscalyears($_POST['to_date']))
46 display_error( _("Invalid END date in fiscal year."));
50 if (date1_greater_date2($_POST['from_date'], $_POST['to_date']))
52 display_error( _("BEGIN date bigger than END date."));
53 set_focus('from_date');
58 //---------------------------------------------------------------------------------------------
59 function close_year($year)
61 $myrow = get_fiscalyear($year);
63 // retrieve total balances from balance sheet accounts
64 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans INNER JOIN ".TB_PREF."chart_master ON account=account_code
65 INNER JOIN ".TB_PREF."chart_types ON account_type=id INNER JOIN ".TB_PREF."chart_class ON class_id=cid
66 WHERE balance_sheet=1 AND tran_date <= '$to'";
67 $result = db_query($sql, "The total balance could not be calculated");
69 $row = db_fetch_row($result);
70 $balance = round2($row[0], user_price_dec());
73 $co = get_company_prefs();
78 $trans_type = systypes::journal_entry();
79 $trans_id = get_next_trans_no($trans_type);
81 add_gl_trans($trans_type, $trans_id, $to, $co['retained_earnings_act'],
82 0, 0, _("Closing Year"), -$balance);
83 add_gl_trans($trans_type, $trans_id, $to, $co['profit_loss_year_act'],
84 0, 0, _("Closing Year"), $balance);
90 function handle_submit()
92 global $selected_id, $Mode;
94 if ($selected_id != -1)
96 if ($_POST['closed'] == 1)
97 close_year($selected_id);
98 update_fiscalyear($selected_id, $_POST['closed']);
99 display_notification(_('Selected fiscal year has been updated'));
105 add_fiscalyear($_POST['from_date'], $_POST['to_date'], $_POST['closed']);
106 display_notification(_('New fiscal year has been added'));
111 //---------------------------------------------------------------------------------------------
113 function check_can_delete($selected_id)
115 $myrow = get_fiscalyear($selected_id);
116 // PREVENT DELETES IF DEPENDENT RECORDS IN gl_trans
117 $from = $myrow['begin'];
119 $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE tran_date >= '$from' AND tran_date <= '$to'";
120 $result = db_query($sql, "could not query gl_trans master");
121 $myrow = db_fetch_row($result);
124 display_error(_("Cannot delete this fiscal year because items have been created referring to it."));
131 //---------------------------------------------------------------------------------------------
133 function handle_delete()
135 global $selected_id, $Mode;
137 if (check_can_delete($selected_id)) {
138 //only delete if used in neither customer or supplier, comp prefs, bank trans accounts
139 delete_fiscalyear($selected_id);
140 display_notification(_('Selected fiscal year has been deleted'));
145 //---------------------------------------------------------------------------------------------
147 function display_fiscalyears()
151 $company_year = get_company_pref('f_year');
153 $result = get_all_fiscalyears();
155 start_table($table_style);
157 $th = array(_("Fiscal Year Begin"), _("Fiscal Year End"), _("Closed"), "", "");
161 while ($myrow=db_fetch($result))
163 if ($myrow['id'] == $company_year)
165 start_row("class='stockmankobg'");
168 alt_table_row_color($k);
170 $from = sql2date($myrow["begin"]);
171 $to = sql2date($myrow["end"]);
172 if ($myrow["closed"] == 0)
174 $closed_text = _("No");
178 $closed_text = _("Yes");
182 label_cell($closed_text);
183 edit_button_cell("Edit".$myrow['id'], _("Edit"));
184 if ($myrow["id"] != $company_year)
185 delete_button_cell("Delete".$myrow['id'], _("Delete"));
193 display_note(_("The marked fiscal year is the current fiscal year which cannot be deleted."), 0, 0, "class='currentfg'");
196 //---------------------------------------------------------------------------------------------
198 function display_fiscalyear_edit($selected_id)
200 global $table_style2, $Mode;
203 start_table($table_style2);
205 if ($selected_id != -1)
209 $myrow = get_fiscalyear($selected_id);
211 $_POST['from_date'] = sql2date($myrow["begin"]);
212 $_POST['to_date'] = sql2date($myrow["end"]);
213 $_POST['closed'] = $myrow["closed"];
217 label_row(_("Fiscal Year Begin:"), $_POST['from_date']);
218 label_row(_("Fiscal Year End:"), $_POST['to_date']);
222 date_row(_("Fiscal Year Begin:"), 'from_date', '', null, 0, 0, 1001);
223 date_row(_("Fiscal Year End:"), 'to_date', '', null, 0, 0, 1001);
225 hidden('selected_id', $selected_id);
227 yesno_list_row(_("Is Closed:"), 'closed', null, "", "", false);
231 submit_add_or_update_center($selected_id == -1, '', 'both');
236 //---------------------------------------------------------------------------------------------
238 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
243 //---------------------------------------------------------------------------------------------
245 if ($Mode == 'Delete')
248 handle_delete($selected_id);
251 if ($Mode == 'RESET')
255 //---------------------------------------------------------------------------------------------
257 display_fiscalyears();
261 display_fiscalyear_edit($selected_id);
263 //---------------------------------------------------------------------------------------------