Merged bugfixes upto [0000072] (version 2.0.3).
[fa-stable.git] / gl / manage / exchange_rates.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 . "/includes/ui.inc");
9 include_once($path_to_root . "/includes/banking.inc");
10
11 $js = "";
12 if ($use_date_picker)
13         $js .= get_js_date_picker();
14 page(_("Exchange Rates"), false, false, "", $js);
15
16 simple_page_mode(false);
17
18 //---------------------------------------------------------------------------------------------
19 function check_data()
20 {
21         if (!is_date($_POST['date_']))
22         {
23                 display_error( _("The entered date is invalid."));
24                 set_focus('date_');
25                 return false;
26         }
27         if (!check_num('BuyRate', 0))
28         {
29                 display_error( _("The exchange rate must be numeric and greater than zero."));
30                 set_focus('BuyRate');
31                 return false;
32         }
33         if ($_POST['BuyRate'] <= 0)
34         {
35                 display_error( _("The exchange rate cannot be zero or a negative number."));
36                 set_focus('BuyRate');
37                 return false;
38         }
39
40         return true;
41 }
42
43 //---------------------------------------------------------------------------------------------
44
45 function handle_submit()
46 {
47         global $selected_id;
48
49         if (!check_data())
50                 return false;
51
52         if ($selected_id != "")
53         {
54
55                 update_exchange_rate($_POST['curr_abrev'], $_POST['date_'],
56                 input_num('BuyRate'), input_num('BuyRate'));
57         }
58         else
59         {
60
61                 add_exchange_rate($_POST['curr_abrev'], $_POST['date_'],
62                     input_num('BuyRate'), input_num('BuyRate'));
63         }
64
65         $selected_id = '';
66 }
67
68 //---------------------------------------------------------------------------------------------
69
70 function handle_delete()
71 {
72         global $selected_id;
73
74         if ($selected_id == "")
75                 return;
76         delete_exchange_rate($selected_id);
77         $selected_id = '';
78 }
79
80 //---------------------------------------------------------------------------------------------
81
82 function display_rates($curr_code)
83 {
84         global $table_style;
85
86         $result = get_exchange_rates($curr_code);
87
88         br(2);
89         start_table($table_style);
90         $th = array(_("Date to Use From"), _("Exchange Rate"), "", "");
91         table_header($th);
92
93     $k = 0; //row colour counter
94
95     while ($myrow = db_fetch($result))
96     {
97
98                 alt_table_row_color($k);
99
100         label_cell(sql2date($myrow["date_"]));
101                 label_cell(number_format2($myrow["rate_buy"], user_exrate_dec()), "nowrap align=right");
102                 edit_button_cell("Edit".$myrow["id"], _("Edit"));
103                 edit_button_cell("Delete".$myrow["id"], _("Delete"));
104
105                 end_row();
106
107     }
108
109     end_table();
110 }
111
112 //---------------------------------------------------------------------------------------------
113
114 function display_rate_edit()
115 {
116         global $selected_id, $table_style2, $Ajax;
117
118         start_table($table_style2);
119
120         if ($selected_id != "")
121         {
122                 //editing an existing exchange rate
123
124                 $myrow = get_exchange_rate($selected_id);
125
126                 $_POST['date_'] = sql2date($myrow["date_"]);
127                 $_POST['BuyRate'] = exrate_format($myrow["rate_buy"]);
128
129                 hidden('selected_id', $selected_id);
130                 hidden('date_', $_POST['date_']);
131
132                 label_row(_("Date to Use From:"), $_POST['date_']);
133         }
134         else
135         {
136                 $_POST['date_'] = Today();
137                 $_POST['BuyRate'] = '';
138                 date_row(_("Date to Use From:"), 'date_');
139         }
140         if (isset($_POST['get_rate']))
141         {
142                 $_POST['BuyRate'] = exrate_format(get_ecb_rate($_POST['curr_abrev']));
143                 if ($_POST['BuyRate'] == 0) {
144                         display_error(_('This currency is not listed by ECB. Enter exchange rate manually.'));
145                 }
146                 $Ajax->activate('BuyRate');
147         }
148         small_amount_row(_("Exchange Rate:"), 'BuyRate', null, '',
149                 submit('get_rate',_("Get"), false, _('Get current ECB rate') , true),
150                 user_exrate_dec());
151
152         end_table(1);
153
154         submit_add_or_update_center($selected_id == '', '', true);
155
156         display_note(_("Exchange rates are entered against the company currency."), 1);
157 }
158
159 //---------------------------------------------------------------------------------------------
160
161 function clear_data()
162 {
163         unset($_POST['selected_id']);
164         unset($_POST['date_']);
165         unset($_POST['BuyRate']);
166 }
167
168 //---------------------------------------------------------------------------------------------
169
170 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
171         handle_submit();
172
173 //---------------------------------------------------------------------------------------------
174
175 if ($Mode == 'Delete')
176         handle_delete();
177
178
179 //---------------------------------------------------------------------------------------------
180
181 start_form(false, true);
182
183 if (!isset($_POST['curr_abrev']))
184         $_POST['curr_abrev'] = get_global_curr_code();
185
186 echo "<center>";
187 echo _("Select a currency :") . "  ";
188 currencies_list('curr_abrev', null, true);
189 echo "</center>";
190
191 // if currency sel has changed, clear the form
192 if ($_POST['curr_abrev'] != get_global_curr_code())
193 {
194         clear_data();
195         $selected_id = "";
196 }
197
198 set_global_curr_code($_POST['curr_abrev']);
199
200 if (is_company_currency($_POST['curr_abrev']))
201 {
202
203         display_note(_("The selected currency is the company currency."), 2);
204         display_note(_("The company currency is the base currency so exchange rates cannot be set for it."), 1);
205 }
206 else
207 {
208
209     display_rates($_POST['curr_abrev']);
210         br(1);
211     display_rate_edit();
212 }
213
214 end_form();
215
216 end_page();
217
218 ?>