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