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