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