e5e9c2f7f209be6fad1c8790a9c7abbc8e333be6
[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 function close_year($year)
60 {
61         $myrow = get_fiscalyear($year);
62         $to = $myrow['end'];
63         // retrieve total balances from balance sheet accounts
64     $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans INNER JOIN ".TB_PREF."chart_master ON account=account_code
65         INNER JOIN ".TB_PREF."chart_types ON account_type=id INNER JOIN ".TB_PREF."chart_class ON class_id=cid 
66                 WHERE balance_sheet=1 AND tran_date <= '$to'";
67         $result = db_query($sql, "The total balance could not be calculated");
68
69         $row = db_fetch_row($result);
70         $balance = round2($row[0], user_price_dec());
71         if ($balance != 0.0)
72         {
73                 $co = get_company_prefs();
74                 $to = sql2date($to);
75
76                 begin_transaction();
77
78                 $trans_type = systypes::journal_entry();
79                 $trans_id = get_next_trans_no($trans_type);
80
81                 add_gl_trans($trans_type, $trans_id, $to, $co['retained_earnings_act'],
82                         0, 0, _("Closing Year"), -$balance);
83                 add_gl_trans($trans_type, $trans_id, $to, $co['profit_loss_year_act'],
84                         0, 0, _("Closing Year"), $balance);
85
86                 commit_transaction();
87         }       
88 }
89         
90 function handle_submit()
91 {
92         global $selected_id, $Mode;
93
94         if ($selected_id != -1)
95         {
96                 if ($_POST['closed'] == 1)
97                         close_year($selected_id);       
98                 update_fiscalyear($selected_id, $_POST['closed']);
99                 display_notification(_('Selected fiscal year has been updated'));
100         }
101         else
102         {
103                 if (!check_data())
104                         return false;
105                 add_fiscalyear($_POST['from_date'], $_POST['to_date'], $_POST['closed']);
106                 display_notification(_('New fiscal year has been added'));
107         }
108         $Mode = 'RESET';
109 }
110
111 //---------------------------------------------------------------------------------------------
112
113 function check_can_delete($selected_id)
114 {
115         $myrow = get_fiscalyear($selected_id);
116         // PREVENT DELETES IF DEPENDENT RECORDS IN gl_trans
117         $from = $myrow['begin'];
118         $to = $myrow['end'];
119         $sql= "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE tran_date >= '$from' AND tran_date <= '$to'";
120         $result = db_query($sql, "could not query gl_trans master");
121         $myrow = db_fetch_row($result);
122         if ($myrow[0] > 0)
123         {
124                 display_error(_("Cannot delete this fiscal year because items have been created referring to it."));
125                 return false;
126         }
127
128         return true;
129 }
130
131 //---------------------------------------------------------------------------------------------
132
133 function handle_delete()
134 {
135         global $selected_id, $Mode;
136
137         if (check_can_delete($selected_id)) {
138         //only delete if used in neither customer or supplier, comp prefs, bank trans accounts
139                 delete_fiscalyear($selected_id);
140                 display_notification(_('Selected fiscal year has been deleted'));
141         }
142         $Mode = 'RESET';
143 }
144
145 //---------------------------------------------------------------------------------------------
146
147 function display_fiscalyears()
148 {
149         global $table_style;
150
151         $company_year = get_company_pref('f_year');
152
153         $result = get_all_fiscalyears();
154         start_form();
155         start_table($table_style);
156
157         $th = array(_("Fiscal Year Begin"), _("Fiscal Year End"), _("Closed"), "", "");
158         table_header($th);
159
160         $k=0;
161         while ($myrow=db_fetch($result))
162         {
163         if ($myrow['id'] == $company_year)
164         {
165                 start_row("class='stockmankobg'");
166         }
167         else
168                 alt_table_row_color($k);
169
170                 $from = sql2date($myrow["begin"]);
171                 $to = sql2date($myrow["end"]);
172                 if ($myrow["closed"] == 0)
173                 {
174                         $closed_text = _("No");
175                 }
176                 else
177                 {
178                         $closed_text = _("Yes");
179                 }
180                 label_cell($from);
181                 label_cell($to);
182                 label_cell($closed_text);
183                 edit_button_cell("Edit".$myrow['id'], _("Edit"));
184                 if ($myrow["id"] != $company_year)
185                         delete_button_cell("Delete".$myrow['id'], _("Delete"));
186                 else
187                         label_cell('');
188                 end_row();
189         }
190
191         end_table();
192         end_form();
193         display_note(_("The marked fiscal year is the current fiscal year which cannot be deleted."), 0, 0, "class='currentfg'");
194 }
195
196 //---------------------------------------------------------------------------------------------
197
198 function display_fiscalyear_edit($selected_id)
199 {
200         global $table_style2, $Mode;
201
202         start_form();
203         start_table($table_style2);
204
205         if ($selected_id != -1)
206         {
207                 if($Mode =='Edit')
208                 {
209                         $myrow = get_fiscalyear($selected_id);
210
211                         $_POST['from_date'] = sql2date($myrow["begin"]);
212                         $_POST['to_date']  = sql2date($myrow["end"]);
213                         $_POST['closed']  = $myrow["closed"];
214                 }
215                 hidden('from_date');
216                 hidden('to_date');
217                 label_row(_("Fiscal Year Begin:"), $_POST['from_date']);
218                 label_row(_("Fiscal Year End:"), $_POST['to_date']);
219         }
220         else
221         {
222                 date_row(_("Fiscal Year Begin:"), 'from_date', '', null, 0, 0, 1001);
223                 date_row(_("Fiscal Year End:"), 'to_date', '', null, 0, 0, 1001);
224         }
225         hidden('selected_id', $selected_id);
226
227         yesno_list_row(_("Is Closed:"), 'closed', null, "", "", false);
228
229         end_table(1);
230
231         submit_add_or_update_center($selected_id == -1, '', 'both');
232
233         end_form();
234 }
235
236 //---------------------------------------------------------------------------------------------
237
238 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
239 {
240         handle_submit();
241 }
242
243 //---------------------------------------------------------------------------------------------
244
245 if ($Mode == 'Delete')
246 {
247         global $selected_id;
248         handle_delete($selected_id);
249 }
250
251 if ($Mode == 'RESET')
252 {
253         $selected_id = -1;
254 }
255 //---------------------------------------------------------------------------------------------
256
257 display_fiscalyears();
258
259 echo '<br>';
260
261 display_fiscalyear_edit($selected_id);
262
263 //---------------------------------------------------------------------------------------------
264
265 end_page();
266
267 ?>