Moved all SQL statements from PHP files into relevant *_db.inc files.
[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 = 'SA_FISCALYEARS';
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 . "/admin/db/fiscalyears_db.inc");
19 include_once($path_to_root . "/includes/ui.inc");
20 include_once($path_to_root . "/sales/includes/db/cust_trans_db.inc");
21 include_once($path_to_root . "/admin/db/maintenance_db.inc");
22 $js = "";
23 if ($use_date_picker)
24         $js .= get_js_date_picker();
25 page(_($help_context = "Fiscal Years"), false, false, "", $js);
26
27 simple_page_mode(true);
28 //---------------------------------------------------------------------------------------------
29
30 function check_data()
31 {
32         if (!is_date($_POST['from_date']) || is_date_in_fiscalyears($_POST['from_date']) || is_bad_begin_date($_POST['from_date']))
33         {
34                 display_error( _("Invalid BEGIN date in fiscal year."));
35                 set_focus('from_date');
36                 return false;
37         }
38         if (!is_date($_POST['to_date']) || is_date_in_fiscalyears($_POST['to_date']))
39         {
40                 display_error( _("Invalid END date in fiscal year."));
41                 set_focus('to_date');
42                 return false;
43         }
44         if (date1_greater_date2($_POST['from_date'], $_POST['to_date']))
45         {
46                 display_error( _("BEGIN date bigger than END date."));
47                 set_focus('from_date');
48                 return false;
49         }
50         return true;
51 }
52
53 function handle_submit()
54 {
55         global $selected_id, $Mode;
56
57         $ok = true;
58         if ($selected_id != -1)
59         {
60                 if ($_POST['closed'] == 1)
61                 {
62                         if (check_years_before($_POST['from_date'], false))
63                         {
64                                 display_error( _("Cannot CLOSE this year because there are open fiscal years before"));
65                                 set_focus('closed');
66                                 return false;
67                         }       
68                         $ok = close_year($selected_id);
69                 }       
70                 else
71                         open_year($selected_id);
72                 if ($ok)
73                 {
74                         update_fiscalyear($selected_id, $_POST['closed']);
75                         display_notification(_('Selected fiscal year has been updated'));
76                 }       
77         }
78         else
79         {
80                 if (!check_data())
81                         return false;
82                 add_fiscalyear($_POST['from_date'], $_POST['to_date'], $_POST['closed']);
83                 display_notification(_('New fiscal year has been added'));
84         }
85         $Mode = 'RESET';
86 }
87
88 //---------------------------------------------------------------------------------------------
89
90 function check_can_delete($selected_id)
91 {
92         $myrow = get_fiscalyear($selected_id);
93         // PREVENT DELETES IF DEPENDENT RECORDS IN gl_trans
94         if (check_years_before(sql2date($myrow['begin']), true))
95         {
96                 display_error(_("Cannot delete this fiscal year because thera are fiscal years before."));
97                 return false;
98         }
99         if ($myrow['closed'] == 0)
100         {
101                 display_error(_("Cannot delete this fiscal year because the fiscal year is not closed."));
102                 return false;
103         }
104         return true;
105 }
106
107 function handle_delete()
108 {
109         global $selected_id, $Mode;
110
111         if (check_can_delete($selected_id)) {
112         //only delete if used in neither customer or supplier, comp prefs, bank trans accounts
113                 delete_this_fiscalyear($selected_id);
114                 display_notification(_('Selected fiscal year has been deleted'));
115         }
116         $Mode = 'RESET';
117 }
118
119 //---------------------------------------------------------------------------------------------
120
121 function display_fiscalyears()
122 {
123         global $table_style;
124
125         $company_year = get_company_pref('f_year');
126
127         $result = get_all_fiscalyears();
128         start_form();
129         display_note(_("Warning: During fiscal year removal all transactions 
130                 are removed and converted into relevant balances. This process is irreversible!"), 
131                 0, 0, "class='currentfg'");
132         start_table($table_style);
133
134         $th = array(_("Fiscal Year Begin"), _("Fiscal Year End"), _("Closed"), "", "");
135         table_header($th);
136
137         $k=0;
138         while ($myrow=db_fetch($result))
139         {
140         if ($myrow['id'] == $company_year)
141         {
142                 start_row("class='stockmankobg'");
143         }
144         else
145                 alt_table_row_color($k);
146
147                 $from = sql2date($myrow["begin"]);
148                 $to = sql2date($myrow["end"]);
149                 if ($myrow["closed"] == 0)
150                 {
151                         $closed_text = _("No");
152                 }
153                 else
154                 {
155                         $closed_text = _("Yes");
156                 }
157                 label_cell($from);
158                 label_cell($to);
159                 label_cell($closed_text);
160                 edit_button_cell("Edit".$myrow['id'], _("Edit"));
161                 if ($myrow["id"] != $company_year) {
162                         delete_button_cell("Delete".$myrow['id'], _("Delete"));
163                         submit_js_confirm("Delete".$myrow['id'],
164                                 sprintf(_("Are you sure you want to remove fiscal year %s - %s? All transactions are removed and converted into relevant balances. Do you want to continue ?"), $from, $to));
165                 } else
166                         label_cell('');
167                 end_row();
168         }
169
170         end_table();
171         end_form();
172         display_note(_("The marked fiscal year is the current fiscal year which cannot be deleted."), 0, 0, "class='currentfg'");
173 }
174
175 //---------------------------------------------------------------------------------------------
176
177 function display_fiscalyear_edit($selected_id)
178 {
179         global $table_style2, $Mode;
180
181         start_form();
182         start_table($table_style2);
183
184         if ($selected_id != -1)
185         {
186                 if($Mode =='Edit')
187                 {
188                         $myrow = get_fiscalyear($selected_id);
189
190                         $_POST['from_date'] = sql2date($myrow["begin"]);
191                         $_POST['to_date']  = sql2date($myrow["end"]);
192                         $_POST['closed']  = $myrow["closed"];
193                 }
194                 hidden('from_date');
195                 hidden('to_date');
196                 label_row(_("Fiscal Year Begin:"), $_POST['from_date']);
197                 label_row(_("Fiscal Year End:"), $_POST['to_date']);
198         }
199         else
200         {
201                 date_row(_("Fiscal Year Begin:"), 'from_date', '', null, 0, 0, 1001);
202                 date_row(_("Fiscal Year End:"), 'to_date', '', null, 0, 0, 1001);
203         }
204         hidden('selected_id', $selected_id);
205
206         yesno_list_row(_("Is Closed:"), 'closed', null, "", "", false);
207
208         end_table(1);
209
210         submit_add_or_update_center($selected_id == -1, '', 'both');
211
212         end_form();
213 }
214
215 //---------------------------------------------------------------------------------------------
216
217 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
218 {
219         handle_submit();
220 }
221
222 //---------------------------------------------------------------------------------------------
223
224 if ($Mode == 'Delete')
225 {
226         global $selected_id;
227         handle_delete($selected_id);
228 }
229
230 if ($Mode == 'RESET')
231 {
232         $selected_id = -1;
233 }
234 //---------------------------------------------------------------------------------------------
235
236 display_fiscalyears();
237
238 echo '<br>';
239
240 display_fiscalyear_edit($selected_id);
241
242 //---------------------------------------------------------------------------------------------
243
244 end_page();
245
246 ?>