Focus set to invalid field after submit check fail
[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 //---------------------------------------------------------------------------------------------
16
17 if (isset($_GET['selected_id']))
18 {
19         $selected_id = $_GET['selected_id'];
20
21 elseif (isset($_POST['selected_id']))
22 {
23         $selected_id = $_POST['selected_id'];
24 }
25 else
26         $selected_id = null;
27
28 //---------------------------------------------------------------------------------------------
29
30 function check_data()
31 {
32         if (!isset($selected_id))
33                 $from = $_POST['from_date'];
34         else
35                 $from = $selected_id;
36         if (!is_date($from)) 
37         {
38                 display_error( _("Invalid BEGIN date in fiscal year."));
39                 set_focus('from_date');
40                 return false;
41         }
42         if (!is_date($_POST['to_date'])) 
43         {
44                 display_error( _("Invalid END date in fiscal year."));
45                 set_focus('to_date');
46                 return false;
47         }
48         if (date1_greater_date2($from, $_POST['to_date'])) 
49         {
50                 display_error( _("BEGIN date bigger than END date."));
51                 set_focus('from_date');
52                 return false;
53         }
54         return true;
55 }
56
57 //---------------------------------------------------------------------------------------------
58
59 function handle_submit()
60 {
61         global $selected_id;
62
63         if (!check_data())
64                 return false;
65
66         if (isset($selected_id)) 
67         {
68                 update_fiscalyear($_POST['from_date'], $_POST['closed']);
69         } 
70         else 
71         {
72                 add_fiscalyear($_POST['from_date'], $_POST['to_date'], $_POST['closed']);
73         }
74
75         return true;
76 }
77
78 //---------------------------------------------------------------------------------------------
79
80 function check_can_delete($todate)
81 {
82         global $selected_id;
83
84         // PREVENT DELETES IF DEPENDENT RECORDS IN gl_trans
85         $from = date2sql($selected_id);
86         $to = date2sql($todate);
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);
90         if ($myrow[0] > 0) 
91         {
92                 display_error(_("Cannot delete this fiscal year because items have been created referring to it."));
93                 return false;
94         }
95
96         return true;
97 }
98
99 //---------------------------------------------------------------------------------------------
100
101 function handle_delete($todate)
102 {
103         global $selected_id;
104
105         if (!check_can_delete($todate))
106                 return;
107         //only delete if used in neither customer or supplier, comp prefs, bank trans accounts
108
109         delete_fiscalyear($selected_id);
110
111         meta_forward($_SERVER['PHP_SELF']);
112 }
113
114 //---------------------------------------------------------------------------------------------
115
116 function display_fiscalyears()
117 {
118         global $table_style;
119
120         $company_year = get_company_pref('f_year');
121
122         $result = get_all_fiscalyears();
123
124         start_table($table_style);
125
126         $th = array(_("Fiscal Year Begin"), _("Fiscal Year End"), _("Closed"), "", "");
127         table_header($th);
128
129         $k=0;
130         while ($myrow=db_fetch($result)) 
131         {
132         if ($myrow['id'] == $company_year) 
133         {
134                 start_row("class='stockmankobg'");
135         } 
136         else
137                 alt_table_row_color($k);
138
139                 $from = sql2date($myrow["begin"]);
140                 $to = sql2date($myrow["end"]);
141                 if ($myrow["closed"] == 0) 
142                 {
143                         $closed_text = _("No");
144                 } 
145                 else 
146                 {
147                         $closed_text = _("Yes");
148                 }
149                 label_cell($from);
150                 label_cell($to);
151                 label_cell($closed_text);
152                 edit_link_cell("selected_id=" . urlencode($from));
153                 if ($myrow["id"] != $company_year)
154                         delete_link_cell("selected_id=" . urlencode($from) . "&to_date=" . urlencode($to) . "&delete=1");
155                 end_row();
156         }
157
158         end_table();;
159         display_note(_("The marked fiscal year is the current fiscal year which cannot be deleted."), 0, 0, "class='currentfg'");
160 }
161
162 //---------------------------------------------------------------------------------------------
163
164 function display_fiscalyear_edit($selected_id)
165 {
166         global $table_style2;
167         
168         start_form();
169         start_table($table_style2);
170
171         if ($selected_id) 
172         {
173                 $myrow = get_fiscalyear($selected_id);
174
175                 $_POST['from_date'] = sql2date($myrow["begin"]);
176                 $_POST['to_date']  = sql2date($myrow["end"]);
177                 $_POST['closed']  = $myrow["closed"];
178                 hidden('selected_id', $selected_id);
179                 hidden('from_date', $_POST['from_date']);
180                 hidden('to_date', $_POST['to_date']);
181                 label_row(_("Fiscal Year Begin:"), $_POST['from_date']);
182                 label_row(_("Fiscal Year End:"), $_POST['to_date']);
183         } 
184         else 
185         {
186                 date_row(_("Fiscal Year Begin:"), 'from_date', null, 0, 0, 1001);
187                 date_row(_("Fiscal Year End:"), 'to_date', null, 0, 0, 1001);
188         }
189
190         yesno_list_row(_("Is Closed:"), 'closed', null, "", "", false);
191
192         end_table(1);
193
194         submit_add_or_update_center(!isset($selected_id));
195
196         end_form();
197 }
198
199 //---------------------------------------------------------------------------------------------
200
201 if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM'])) 
202 {
203         if (handle_submit()) 
204         {
205                 meta_forward($_SERVER['PHP_SELF']);
206         }
207 }
208
209 //---------------------------------------------------------------------------------------------
210
211 if (isset($_GET['delete'])) 
212 {
213         handle_delete($_GET['to_date']);
214 }
215
216 //---------------------------------------------------------------------------------------------
217
218 display_fiscalyears();
219
220 hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter a New Fiscal Year"));
221
222 display_fiscalyear_edit($selected_id);
223
224 //---------------------------------------------------------------------------------------------
225
226 end_page();
227
228 ?>