Fixed record selection on pages using non-numeric selectors with simple_page_mode...
[fa-stable.git] / admin / fiscalyears.php
1 <?php
2
3 $page_security = 9;
4 $path_to_root="..";
5 include_once($path_to_root . "/includes/session.inc");
6
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");
10 $js = "";
11 if ($use_date_picker)
12         $js .= get_js_date_picker();
13 page(_("Fiscal Years"), false, false, "", $js);
14
15 simple_page_mode(true);
16 //---------------------------------------------------------------------------------------------
17
18 function is_date_in_fiscalyears($date)
19 {
20         $date = date2sql($date);
21         $sql = "SELECT * FROM ".TB_PREF."fiscal_year WHERE '$date' >= begin AND '$date' <= end";
22
23         $result = db_query($sql, "could not get all fiscal years");
24         return db_fetch($result) !== false;
25 }
26
27 function check_data()
28 {
29         if (!is_date($_POST['from_date']) || is_date_in_fiscalyears($_POST['from_date']))
30         {
31                 display_error( _("Invalid BEGIN date in fiscal year."));
32                 set_focus('from_date');
33                 return false;
34         }
35         if (!is_date($_POST['to_date']) || is_date_in_fiscalyears($_POST['to_date']))
36         {
37                 display_error( _("Invalid END date in fiscal year."));
38                 set_focus('to_date');
39                 return false;
40         }
41         if (date1_greater_date2($_POST['from_date'], $_POST['to_date']))
42         {
43                 display_error( _("BEGIN date bigger than END date."));
44                 set_focus('from_date');
45                 return false;
46         }
47         return true;
48 }
49 //---------------------------------------------------------------------------------------------
50
51 function handle_submit()
52 {
53         global $selected_id, $Mode;
54
55         if (!check_data())
56                 return false;
57
58         if ($selected_id != -1)
59         {
60                 update_fiscalyear($_POST['from_date'], $_POST['closed']);
61                 display_notification(_('Selected fiscal year has been updated'));
62         }
63         else
64         {
65                 add_fiscalyear($_POST['from_date'], $_POST['to_date'], $_POST['closed']);
66                 display_notification(_('New fiscal year has been added'));
67         }
68         $Mode = 'RESET';
69 }
70
71 //---------------------------------------------------------------------------------------------
72
73 function check_can_delete($selected_id)
74 {
75         $myrow = get_fiscalyear($selected_id);
76         // PREVENT DELETES IF DEPENDENT RECORDS IN gl_trans
77         $from = $myrow['begin'];
78         $to = $myrow['end'];
79         $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE tran_date >= '$from' AND tran_date <= '$to'";
80         $result = db_query($sql, "could not query gl_trans master");
81         $myrow = db_fetch_row($result);
82         if ($myrow[0] > 0)
83         {
84                 display_error(_("Cannot delete this fiscal year because items have been created referring to it."));
85                 return false;
86         }
87
88         return true;
89 }
90
91 //---------------------------------------------------------------------------------------------
92
93 function handle_delete()
94 {
95         global $selected_id, $Mode;
96
97         if (!check_can_delete($selected_id))
98                 return;
99         //only delete if used in neither customer or supplier, comp prefs, bank trans accounts
100
101         delete_fiscalyear($selected_id);
102         display_notification(_('Selected fiscal year has been deleted'));
103         $Mode = 'RESET';
104 }
105
106 //---------------------------------------------------------------------------------------------
107
108 function display_fiscalyears()
109 {
110         global $table_style;
111
112         $company_year = get_company_pref('f_year');
113
114         $result = get_all_fiscalyears();
115         start_form();
116         start_table($table_style);
117
118         $th = array(_("Fiscal Year Begin"), _("Fiscal Year End"), _("Closed"), "", "");
119         table_header($th);
120
121         $k=0;
122         while ($myrow=db_fetch($result))
123         {
124         if ($myrow['id'] == $company_year)
125         {
126                 start_row("class='stockmankobg'");
127         }
128         else
129                 alt_table_row_color($k);
130
131                 $from = sql2date($myrow["begin"]);
132                 $to = sql2date($myrow["end"]);
133                 if ($myrow["closed"] == 0)
134                 {
135                         $closed_text = _("No");
136                 }
137                 else
138                 {
139                         $closed_text = _("Yes");
140                 }
141                 label_cell($from);
142                 label_cell($to);
143                 label_cell($closed_text);
144                 edit_button_cell("Edit".$myrow['id'], _("Edit"));
145                 if ($myrow["id"] != $company_year)
146                         edit_button_cell("Delete".$myrow['id'], _("Delete"));
147                 else
148                         label_cell('');
149                 end_row();
150         }
151
152         end_table();
153         end_form();
154         display_note(_("The marked fiscal year is the current fiscal year which cannot be deleted."), 0, 0, "class='currentfg'");
155 }
156
157 //---------------------------------------------------------------------------------------------
158
159 function display_fiscalyear_edit($selected_id)
160 {
161         global $table_style2, $Mode;
162
163         start_form();
164         start_table($table_style2);
165
166         if ($selected_id != -1)
167         {
168                 if($Mode =='Edit')
169                 {
170                         $myrow = get_fiscalyear($selected_id);
171
172                         $_POST['from_date'] = sql2date($myrow["begin"]);
173                         $_POST['to_date']  = sql2date($myrow["end"]);
174                         $_POST['closed']  = $myrow["closed"];
175                 }
176                 hidden('from_date');
177                 hidden('to_date');
178                 label_row(_("Fiscal Year Begin:"), $_POST['from_date']);
179                 label_row(_("Fiscal Year End:"), $_POST['to_date']);
180         }
181         else
182         {
183                 date_row(_("Fiscal Year Begin:"), 'from_date', '', null, 0, 0, 1001);
184                 date_row(_("Fiscal Year End:"), 'to_date', '', null, 0, 0, 1001);
185         }
186         hidden('selected_id', $selected_id);
187
188         yesno_list_row(_("Is Closed:"), 'closed', null, "", "", false);
189
190         end_table(1);
191
192         submit_add_or_update_center($selected_id == -1, '', true);
193
194         end_form();
195 }
196
197 //---------------------------------------------------------------------------------------------
198
199 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
200 {
201         handle_submit();
202 }
203
204 //---------------------------------------------------------------------------------------------
205
206 if ($Mode == 'Delete')
207 {
208         global $selected_id;
209         handle_delete($selected_id);
210 }
211
212 if ($Mode == 'RESET')
213 {
214         $selected_id = -1;
215 }
216 //---------------------------------------------------------------------------------------------
217
218 display_fiscalyears();
219
220 echo '<br>';
221
222 display_fiscalyear_edit($selected_id);
223
224 //---------------------------------------------------------------------------------------------
225
226 end_page();
227
228 ?>