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_FISCALYEARS';
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 . "/admin/db/fiscalyears_db.inc");
19 include_once($path_to_root . "/includes/ui.inc");
20 include_once($path_to_root . "/sales/includes/db/cust_trans_db.inc");
21 include_once($path_to_root . "/admin/db/maintenance_db.inc");
23 if (user_use_date_picker())
24 $js .= get_js_date_picker();
25 page(_($help_context = "Fiscal Years"), false, false, "", $js);
27 simple_page_mode(true);
28 //---------------------------------------------------------------------------------------------
32 if (!is_date($_POST['from_date']) || is_date_in_fiscalyears($_POST['from_date']))
34 display_error( _("Invalid BEGIN date in fiscal year."));
35 set_focus('from_date');
38 if (!is_date($_POST['to_date']) || is_date_in_fiscalyears($_POST['to_date']))
40 display_error( _("Invalid END date in fiscal year."));
44 if (!check_begin_end_date($_POST['from_date'], $_POST['to_date']))
46 display_error( _("Invalid BEGIN or END date in fiscal year."));
47 set_focus('from_date');
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');
59 function handle_submit()
61 global $selected_id, $Mode;
64 if ($selected_id != -1)
66 if ($_POST['closed'] == 1)
68 if (check_years_before($_POST['from_date'], false))
70 display_error( _("Cannot CLOSE this year because there are open fiscal years before"));
74 $ok = close_year($selected_id);
77 open_year($selected_id);
80 update_fiscalyear($selected_id, $_POST['closed']);
81 display_notification(_('Selected fiscal year has been updated'));
88 add_fiscalyear($_POST['from_date'], $_POST['to_date'], $_POST['closed']);
89 display_notification(_('New fiscal year has been added'));
94 //---------------------------------------------------------------------------------------------
96 function check_can_delete($selected_id)
98 $myrow = get_fiscalyear($selected_id);
99 // PREVENT DELETES IF DEPENDENT RECORDS IN gl_trans
100 if (check_years_before(sql2date($myrow['begin']), true))
102 display_error(_("Cannot delete this fiscal year because there are fiscal years before."));
105 if ($myrow['closed'] == 0)
107 display_error(_("Cannot delete this fiscal year because the fiscal year is not closed."));
113 function handle_delete()
115 global $selected_id, $Mode;
117 if (check_can_delete($selected_id)) {
118 //only delete if used in neither customer or supplier, comp prefs, bank trans accounts
119 delete_this_fiscalyear($selected_id);
120 display_notification(_('Selected fiscal year has been deleted'));
125 //---------------------------------------------------------------------------------------------
127 function display_fiscalyears()
129 $company_year = get_company_pref('f_year');
131 $result = get_all_fiscalyears();
133 display_note(_("Warning: Deleting a fiscal year all transactions
134 are removed and converted into relevant balances. This process is irreversible!"),
135 0, 1, "class='currentfg'");
136 start_table(TABLESTYLE);
138 $th = array(_("Fiscal Year Begin"), _("Fiscal Year End"), _("Closed"), "", "");
142 while ($myrow=db_fetch($result))
144 if ($myrow['id'] == $company_year)
146 start_row("class='stockmankobg'");
149 alt_table_row_color($k);
151 $from = sql2date($myrow["begin"]);
152 $to = sql2date($myrow["end"]);
153 if ($myrow["closed"] == 0)
155 $closed_text = _("No");
159 $closed_text = _("Yes");
163 label_cell($closed_text);
164 edit_button_cell("Edit".$myrow['id'], _("Edit"));
165 if ($myrow["id"] != $company_year) {
166 delete_button_cell("Delete".$myrow['id'], _("Delete"));
167 submit_js_confirm("Delete".$myrow['id'],
168 sprintf(_("Are you sure you want to delete fiscal year %s - %s? All transactions are deleted and converted into relevant balances. Do you want to continue ?"), $from, $to));
176 display_note(_("The marked fiscal year is the current fiscal year which cannot be deleted."), 0, 0, "class='currentfg'");
179 //---------------------------------------------------------------------------------------------
181 function display_fiscalyear_edit($selected_id)
186 start_table(TABLESTYLE2);
188 if ($selected_id != -1)
192 $myrow = get_fiscalyear($selected_id);
194 $_POST['from_date'] = sql2date($myrow["begin"]);
195 $_POST['to_date'] = sql2date($myrow["end"]);
196 $_POST['closed'] = $myrow["closed"];
200 label_row(_("Fiscal Year Begin:"), $_POST['from_date']);
201 label_row(_("Fiscal Year End:"), $_POST['to_date']);
205 $begin = next_begin_date();
206 if ($begin && $Mode != 'ADD_ITEM')
208 $_POST['from_date'] = $begin;
209 $_POST['to_date'] = end_month(add_months($begin, 11));
211 date_row(_("Fiscal Year Begin:"), 'from_date', '', null, 0, 0, 1001);
212 date_row(_("Fiscal Year End:"), 'to_date', '', null, 0, 0, 1001);
214 hidden('selected_id', $selected_id);
216 yesno_list_row(_("Is Closed:"), 'closed', null, "", "", false);
220 submit_add_or_update_center($selected_id == -1, '', 'both');
225 //---------------------------------------------------------------------------------------------
227 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
232 //---------------------------------------------------------------------------------------------
234 if ($Mode == 'Delete')
237 handle_delete($selected_id);
240 if ($Mode == 'RESET')
244 //---------------------------------------------------------------------------------------------
246 display_fiscalyears();
250 display_fiscalyear_edit($selected_id);
252 //---------------------------------------------------------------------------------------------