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