5 include_once($path_to_root . "/includes/session.inc");
7 include_once($path_to_root . "/includes/date_functions.inc");
8 include_once($path_to_root . "/admin/db/company_db.inc");
9 include_once($path_to_root . "/includes/ui.inc");
12 $js .= get_js_date_picker();
13 page(_("Fiscal Years"), false, false, "", $js);
15 simple_page_mode(true);
16 //---------------------------------------------------------------------------------------------
18 function is_date_in_fiscalyears($date)
20 $date = date2sql($date);
21 $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE '$date' >= begin AND '$date' <= end";
23 $result = db_query($sql, "could not get all fiscal years");
24 return db_fetch($result) !== false;
29 if (!is_date($_POST['from_date']) || is_date_in_fiscalyears($_POST['from_date']))
31 display_error( _("Invalid BEGIN date in fiscal year."));
32 set_focus('from_date');
35 if (!is_date($_POST['to_date']) || is_date_in_fiscalyears($_POST['to_date']))
37 display_error( _("Invalid END date in fiscal year."));
41 if (date1_greater_date2($_POST['from_date'], $_POST['to_date']))
43 display_error( _("BEGIN date bigger than END date."));
44 set_focus('from_date');
49 //---------------------------------------------------------------------------------------------
51 function handle_submit()
53 global $selected_id, $Mode;
55 if ($selected_id != -1)
57 update_fiscalyear($selected_id, $_POST['closed']);
58 display_notification(_('Selected fiscal year has been updated'));
64 add_fiscalyear($_POST['from_date'], $_POST['to_date'], $_POST['closed']);
65 display_notification(_('New fiscal year has been added'));
70 //---------------------------------------------------------------------------------------------
72 function check_can_delete($selected_id)
74 $myrow = get_fiscalyear($selected_id);
75 // PREVENT DELETES IF DEPENDENT RECORDS IN gl_trans
76 $from = $myrow['begin'];
78 $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE tran_date >= '$from' AND tran_date <= '$to'";
79 $result = db_query($sql, "could not query gl_trans master");
80 $myrow = db_fetch_row($result);
83 display_error(_("Cannot delete this fiscal year because items have been created referring to it."));
90 //---------------------------------------------------------------------------------------------
92 function handle_delete()
94 global $selected_id, $Mode;
96 if (check_can_delete($selected_id)) {
97 //only delete if used in neither customer or supplier, comp prefs, bank trans accounts
98 delete_fiscalyear($selected_id);
99 display_notification(_('Selected fiscal year has been deleted'));
104 //---------------------------------------------------------------------------------------------
106 function display_fiscalyears()
110 $company_year = get_company_pref('f_year');
112 $result = get_all_fiscalyears();
114 start_table($table_style);
116 $th = array(_("Fiscal Year Begin"), _("Fiscal Year End"), _("Closed"), "", "");
120 while ($myrow=db_fetch($result))
122 if ($myrow['id'] == $company_year)
124 start_row("class='stockmankobg'");
127 alt_table_row_color($k);
129 $from = sql2date($myrow["begin"]);
130 $to = sql2date($myrow["end"]);
131 if ($myrow["closed"] == 0)
133 $closed_text = _("No");
137 $closed_text = _("Yes");
141 label_cell($closed_text);
142 edit_button_cell("Edit".$myrow['id'], _("Edit"));
143 if ($myrow["id"] != $company_year)
144 edit_button_cell("Delete".$myrow['id'], _("Delete"));
152 display_note(_("The marked fiscal year is the current fiscal year which cannot be deleted."), 0, 0, "class='currentfg'");
155 //---------------------------------------------------------------------------------------------
157 function display_fiscalyear_edit($selected_id)
159 global $table_style2, $Mode;
162 start_table($table_style2);
164 if ($selected_id != -1)
168 $myrow = get_fiscalyear($selected_id);
170 $_POST['from_date'] = sql2date($myrow["begin"]);
171 $_POST['to_date'] = sql2date($myrow["end"]);
172 $_POST['closed'] = $myrow["closed"];
176 label_row(_("Fiscal Year Begin:"), $_POST['from_date']);
177 label_row(_("Fiscal Year End:"), $_POST['to_date']);
181 date_row(_("Fiscal Year Begin:"), 'from_date', '', null, 0, 0, 1001);
182 date_row(_("Fiscal Year End:"), 'to_date', '', null, 0, 0, 1001);
184 hidden('selected_id', $selected_id);
186 yesno_list_row(_("Is Closed:"), 'closed', null, "", "", false);
190 submit_add_or_update_center($selected_id == -1, '', true);
195 //---------------------------------------------------------------------------------------------
197 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
202 //---------------------------------------------------------------------------------------------
204 if ($Mode == 'Delete')
207 handle_delete($selected_id);
210 if ($Mode == 'RESET')
214 //---------------------------------------------------------------------------------------------
216 display_fiscalyears();
220 display_fiscalyear_edit($selected_id);
222 //---------------------------------------------------------------------------------------------