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