Recurrent Invoices: fixed buggy call to non existing function and payment terms type...
[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 (user_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']))
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 (!check_begin_end_date($_POST['from_date'], $_POST['to_date']))
45         {
46                 display_error( _("Invalid BEGIN or END date in fiscal year."));
47                 set_focus('from_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 function handle_submit()
60 {
61         global $selected_id, $Mode;
62
63         $ok = true;
64         if ($selected_id != -1)
65         {
66                 if ($_POST['closed'] == 1)
67                 {
68                         if (check_years_before($_POST['from_date'], false))
69                         {
70                                 display_error( _("Cannot CLOSE this year because there are open fiscal years before"));
71                                 set_focus('closed');
72                                 return false;
73                         }       
74                         $co = get_company_prefs();
75                         if (get_gl_account($co['retained_earnings_act']) == false || get_gl_account($co['profit_loss_year_act']) == false)
76                         {
77                                 display_error(_("The Retained Earnings Account or the Profit and Loss Year Account has not been set in System and General GL Setup"));
78                                 return false;
79                         }
80                         if (!is_account_balancesheet($co['retained_earnings_act']) || is_account_balancesheet($co['profit_loss_year_act']))
81                         {
82                                 display_error(_("The Retained Earnings Account should be a Balance Account or the Profit and Loss Year Account should be an Expense Account (preferred the last one in the Expense Class)"));
83                                 return false;
84                         }
85
86                         $ok = close_year($selected_id);
87                 }       
88                 else
89                         open_year($selected_id);
90                 if ($ok)
91                 {
92                         update_fiscalyear($selected_id, $_POST['closed']);
93                         display_notification(_('Selected fiscal year has been updated'));
94                 }       
95         }
96         else
97         {
98                 if (!check_data())
99                         return false;
100                 add_fiscalyear($_POST['from_date'], $_POST['to_date'], $_POST['closed']);
101                 display_notification(_('New fiscal year has been added'));
102         }
103         $Mode = 'RESET';
104 }
105
106 //---------------------------------------------------------------------------------------------
107
108 function check_can_delete($selected_id)
109 {
110         $myrow = get_fiscalyear($selected_id);
111         // PREVENT DELETES IF DEPENDENT RECORDS IN gl_trans
112         if (check_years_before(sql2date($myrow['begin']), true))
113         {
114                 display_error(_("Cannot delete this fiscal year because there are fiscal years before."));
115                 return false;
116         }
117         if ($myrow['closed'] == 0)
118         {
119                 display_error(_("Cannot delete this fiscal year because the fiscal year is not closed."));
120                 return false;
121         }
122         return true;
123 }
124
125 function handle_delete()
126 {
127         global $selected_id, $Mode;
128
129         if (check_can_delete($selected_id)) {
130         //only delete if used in neither customer or supplier, comp prefs, bank trans accounts
131                 delete_this_fiscalyear($selected_id);
132                 display_notification(_('Selected fiscal year has been deleted'));
133         }
134         $Mode = 'RESET';
135 }
136
137 //---------------------------------------------------------------------------------------------
138
139 function display_fiscalyears()
140 {
141         $company_year = get_company_pref('f_year');
142
143         $result = get_all_fiscalyears();
144         start_form();
145         display_note(_("Warning: Deleting a fiscal year all transactions 
146                 are removed and converted into relevant balances. This process is irreversible!"), 
147                 0, 1, "class='currentfg'");
148         start_table(TABLESTYLE);
149
150         $th = array(_("Fiscal Year Begin"), _("Fiscal Year End"), _("Closed"), "", "");
151         table_header($th);
152
153         $k=0;
154         while ($myrow=db_fetch($result))
155         {
156         if ($myrow['id'] == $company_year)
157         {
158                 start_row("class='stockmankobg'");
159         }
160         else
161                 alt_table_row_color($k);
162
163                 $from = sql2date($myrow["begin"]);
164                 $to = sql2date($myrow["end"]);
165                 if ($myrow["closed"] == 0)
166                 {
167                         $closed_text = _("No");
168                 }
169                 else
170                 {
171                         $closed_text = _("Yes");
172                 }
173                 label_cell($from);
174                 label_cell($to);
175                 label_cell($closed_text);
176                 edit_button_cell("Edit".$myrow['id'], _("Edit"));
177                 if ($myrow["id"] != $company_year) {
178                         delete_button_cell("Delete".$myrow['id'], _("Delete"));
179                         submit_js_confirm("Delete".$myrow['id'],
180                                 sprintf(_("Are you sure you want to delete fiscal year %s - %s? All transactions are deleted and converted into relevant balances. Do you want to continue ?"), $from, $to));
181                 } else
182                         label_cell('');
183                 end_row();
184         }
185
186         end_table();
187         end_form();
188         display_note(_("The marked fiscal year is the current fiscal year which cannot be deleted."), 0, 0, "class='currentfg'");
189 }
190
191 //---------------------------------------------------------------------------------------------
192
193 function display_fiscalyear_edit($selected_id)
194 {
195         global $Mode;
196
197         start_form();
198         start_table(TABLESTYLE2);
199
200         if ($selected_id != -1)
201         {
202                 if($Mode =='Edit')
203                 {
204                         $myrow = get_fiscalyear($selected_id);
205
206                         $_POST['from_date'] = sql2date($myrow["begin"]);
207                         $_POST['to_date']  = sql2date($myrow["end"]);
208                         $_POST['closed']  = $myrow["closed"];
209                 }
210                 hidden('from_date');
211                 hidden('to_date');
212                 label_row(_("Fiscal Year Begin:"), $_POST['from_date']);
213                 label_row(_("Fiscal Year End:"), $_POST['to_date']);
214         }
215         else
216         {
217                 $begin = next_begin_date();
218                 if ($begin && $Mode != 'ADD_ITEM')
219                 {
220                         $_POST['from_date'] = $begin;
221                         $_POST['to_date'] = end_month(add_months($begin, 11));
222                 }
223                 date_row(_("Fiscal Year Begin:"), 'from_date', '', null, 0, 0, 1001);
224                 date_row(_("Fiscal Year End:"), 'to_date', '', null, 0, 0, 1001);
225         }
226         hidden('selected_id', $selected_id);
227
228         yesno_list_row(_("Is Closed:"), 'closed', null, "", "", false);
229
230         end_table(1);
231
232         submit_add_or_update_center($selected_id == -1, '', 'both');
233
234         end_form();
235 }
236
237 //---------------------------------------------------------------------------------------------
238
239 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
240 {
241         handle_submit();
242 }
243
244 //---------------------------------------------------------------------------------------------
245
246 if ($Mode == 'Delete')
247 {
248         global $selected_id;
249         handle_delete($selected_id);
250 }
251
252 if ($Mode == 'RESET')
253 {
254         $selected_id = -1;
255 }
256 //---------------------------------------------------------------------------------------------
257
258 display_fiscalyears();
259
260 echo '<br>';
261
262 display_fiscalyear_edit($selected_id);
263
264 //---------------------------------------------------------------------------------------------
265
266 end_page();
267