2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU Affero General Public License,
5 AGPL, as published by the Free Software Foundation, either version
6 3 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/agpl-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 //---------------------------------------------------------------------------------------------
60 function handle_submit()
62 global $selected_id, $Mode;
64 if ($selected_id != -1)
66 update_fiscalyear($selected_id, $_POST['closed']);
67 display_notification(_('Selected fiscal year has been updated'));
73 add_fiscalyear($_POST['from_date'], $_POST['to_date'], $_POST['closed']);
74 display_notification(_('New fiscal year has been added'));
79 //---------------------------------------------------------------------------------------------
81 function check_can_delete($selected_id)
83 $myrow = get_fiscalyear($selected_id);
84 // PREVENT DELETES IF DEPENDENT RECORDS IN gl_trans
85 $from = $myrow['begin'];
87 $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE tran_date >= '$from' AND tran_date <= '$to'";
88 $result = db_query($sql, "could not query gl_trans master");
89 $myrow = db_fetch_row($result);
92 display_error(_("Cannot delete this fiscal year because items have been created referring to it."));
99 //---------------------------------------------------------------------------------------------
101 function handle_delete()
103 global $selected_id, $Mode;
105 if (check_can_delete($selected_id)) {
106 //only delete if used in neither customer or supplier, comp prefs, bank trans accounts
107 delete_fiscalyear($selected_id);
108 display_notification(_('Selected fiscal year has been deleted'));
113 //---------------------------------------------------------------------------------------------
115 function display_fiscalyears()
119 $company_year = get_company_pref('f_year');
121 $result = get_all_fiscalyears();
123 start_table($table_style);
125 $th = array(_("Fiscal Year Begin"), _("Fiscal Year End"), _("Closed"), "", "");
129 while ($myrow=db_fetch($result))
131 if ($myrow['id'] == $company_year)
133 start_row("class='stockmankobg'");
136 alt_table_row_color($k);
138 $from = sql2date($myrow["begin"]);
139 $to = sql2date($myrow["end"]);
140 if ($myrow["closed"] == 0)
142 $closed_text = _("No");
146 $closed_text = _("Yes");
150 label_cell($closed_text);
151 edit_button_cell("Edit".$myrow['id'], _("Edit"));
152 if ($myrow["id"] != $company_year)
153 delete_button_cell("Delete".$myrow['id'], _("Delete"));
161 display_note(_("The marked fiscal year is the current fiscal year which cannot be deleted."), 0, 0, "class='currentfg'");
164 //---------------------------------------------------------------------------------------------
166 function display_fiscalyear_edit($selected_id)
168 global $table_style2, $Mode;
171 start_table($table_style2);
173 if ($selected_id != -1)
177 $myrow = get_fiscalyear($selected_id);
179 $_POST['from_date'] = sql2date($myrow["begin"]);
180 $_POST['to_date'] = sql2date($myrow["end"]);
181 $_POST['closed'] = $myrow["closed"];
185 label_row(_("Fiscal Year Begin:"), $_POST['from_date']);
186 label_row(_("Fiscal Year End:"), $_POST['to_date']);
190 date_row(_("Fiscal Year Begin:"), 'from_date', '', null, 0, 0, 1001);
191 date_row(_("Fiscal Year End:"), 'to_date', '', null, 0, 0, 1001);
193 hidden('selected_id', $selected_id);
195 yesno_list_row(_("Is Closed:"), 'closed', null, "", "", false);
199 submit_add_or_update_center($selected_id == -1, '', true);
204 //---------------------------------------------------------------------------------------------
206 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
211 //---------------------------------------------------------------------------------------------
213 if ($Mode == 'Delete')
216 handle_delete($selected_id);
219 if ($Mode == 'RESET')
223 //---------------------------------------------------------------------------------------------
225 display_fiscalyears();
229 display_fiscalyear_edit($selected_id);
231 //---------------------------------------------------------------------------------------------