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