2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
42 var $javascript = '<script>';
43 function lookupCurrencies(){
46 $this->focus = new Currency();
47 $this->list = $this->focus->get_full_list('name');
48 $this->focus->retrieve('-99');
49 if(is_array($this->list)){
50 $this->list = array_merge(Array($this->focus), $this->list);
52 $this->list = Array($this->focus);
58 if($current_user->is_admin){
59 if(isset($_POST['edit']) && $_POST['edit'] == 'true' && isset($_POST['name']) && !empty($_POST['name']) && isset($_POST['conversion_rate']) && !empty($_POST['conversion_rate']) && isset($_POST['symbol']) && !empty($_POST['symbol'])){
61 $currency = new Currency();
62 if(isset($_POST['record']) && !empty($_POST['record'])){
64 $currency->retrieve($_POST['record']);
66 $currency->name = $_POST['name'];
67 $currency->status = $_POST['status'];
68 $currency->symbol = $_POST['symbol'];
69 $currency->iso4217 = $_POST['iso4217'];
70 $currency->conversion_rate = unformat_number($_POST['conversion_rate']);
72 $this->focus = $currency;
78 function handleUpdate(){
80 if($current_user->is_admin){
81 if(isset($_POST['id']) && !empty($_POST['id'])&&isset($_POST['name']) && !empty($_POST['name']) && isset($_POST['rate']) && !empty($_POST['rate']) && isset($_POST['symbol']) && !empty($_POST['symbol'])){
83 $names= $_POST['name'];
84 $symbols= $_POST['symbol'];
85 $rates = $_POST['rate'];
86 $isos = $_POST['iso'];
88 if($size != sizeof($names)|| $size != sizeof($isos) || $size != sizeof($symbols) || $size != sizeof($rates)){
92 $temp = new Currency();
93 for($i = 0; $i < $size; $i++){
95 $temp->name = $names[$i];
96 $temp->symbol = $symbols[$i];
97 $temp->iso4217 = $isos[$i];
98 $temp->conversion_rate = $rates[$i];
104 function getJavascript(){
105 // wp: DO NOT add formatting and unformatting numbers in here, add them prior to calling these to avoid double calling
106 // of unformat number
107 return $this->javascript . <<<EOQ
108 function get_rate(id){
109 return ConversionRates[id];
111 function ConvertToDollar(amount, rate){
112 return amount / rate;
114 function ConvertFromDollar(amount, rate){
115 return amount * rate;
117 function ConvertRate(id,fields){
118 for(var i = 0; i < fields.length; i++){
119 fields[i].value = toDecimal(ConvertFromDollar(toDecimal(ConvertToDollar(toDecimal(fields[i].value), lastRate)), ConversionRates[id]));
121 lastRate = ConversionRates[id];
123 function ConvertRateSingle(id,field){
124 var temp = field.innerHTML.substring(1, field.innerHTML.length);
125 unformattedNumber = unformatNumber(temp, num_grp_sep, dec_sep);
127 field.innerHTML = CurrencySymbols[id] + formatNumber(toDecimal(ConvertFromDollar(ConvertToDollar(unformattedNumber, lastRate), ConversionRates[id])), num_grp_sep, dec_sep, 2, 2);
128 lastRate = ConversionRates[id];
130 function CurrencyConvertAll(){
132 var id = document.EditView.currency_id.options[document.EditView.currency_id.selectedIndex].value;
133 var fields = new Array();
135 for(i in currencyFields){
136 var field = currencyFields[i];
137 if(typeof(document.EditView[field]) != 'undefined'){
138 document.EditView[field].value = unformatNumber(document.EditView[field].value, num_grp_sep, dec_sep);
139 fields.push(document.EditView[field]);
144 ConvertRate(id, fields);
146 fields[i].value = formatNumber(fields[i].value, num_grp_sep, dec_sep);
151 // Do nothing, if we can't find the currency_id field we will just not attempt to convert currencies
152 // This typically only happens in lead conversion and quick creates, where the currency_id field may be named somethnig else or hidden deep inside a sub-form.
161 function getSelectOptions($id = ''){
162 global $current_user;
163 $this->javascript .="var ConversionRates = new Array(); \n";
164 $this->javascript .="var CurrencySymbols = new Array(); \n";
166 $this->lookupCurrencies();
167 $setLastRate = false;
168 if(isset($this->list ) && !empty($this->list )){
169 foreach ($this->list as $data){
170 if($data->status == 'Active'){
171 if($id == $data->id){
172 $options .= '<option value="'. $data->id . '" selected>';
174 $this->javascript .= 'var lastRate = "' . $data->conversion_rate . '";';
177 $options .= '<option value="'. $data->id . '">' ;
179 $options .= $data->name . ' : ' . $data->symbol;
180 $this->javascript .=" ConversionRates['".$data->id."'] = '".$data->conversion_rate."';\n";
181 $this->javascript .=" CurrencySymbols['".$data->id."'] = '".$data->symbol."';\n";
184 $this->javascript .= 'var lastRate = "1";';
191 $this->lookupCurrencies();
192 $usdollar = translate('LBL_US_DOLLAR');
193 $currency = translate('LBL_CURRENCY');
194 $currency_sym = $sugar_config['default_currency_symbol'];
195 $conv_rate = translate('LBL_CONVERSION_RATE');
196 $add = translate('LBL_ADD');
197 $delete = translate('LBL_DELETE');
198 $update = translate('LBL_UPDATE');
200 $form = $html = "<br><table cellpadding='0' cellspacing='0' border='0' class='tabForm'><tr><td><tableborder='0' cellspacing='0' cellpadding='0'>";
202 <form name='DeleteCurrency' action='index.php' method='post'><input type='hidden' name='action' value='{$_REQUEST['action']}'>
203 <input type='hidden' name='module' value='{$_REQUEST['module']}'><input type='hidden' name='deleteCur' value=''></form>
205 <tr><td><B>$currency</B></td><td><B>ISO 4217</B> </td><td><B>$currency_sym</B></td><td colspan='2'><B>$conv_rate</B></td></tr>
206 <tr><td>$usdollar</td><td>USD</td><td>$</td><td colspan='2'>1</td></tr>
207 <form name="UpdateCurrency" action="index.php" method="post"><input type='hidden' name='action' value='{$_REQUEST['action']}'>
208 <input type='hidden' name='module' value='{$_REQUEST['module']}'>
210 if(isset($this->list ) && !empty($this->list )){
211 foreach ($this->list as $data){
213 $form .= '<tr><td>'.$data->iso4217. '<input type="hidden" name="iso[]" value="'.$data->iso4217.'"></td><td><input type="hidden" name="id[]" value="'.$data->id.'">'.$data->name. '<input type="hidden" name="name[]" value="'.$data->name.'"></td><td>'.$data->symbol. '<input type="hidden" name="symbol[]" value="'.$data->symbol.'"></td><td>'.$data->conversion_rate.' </td><td><input type="text" name="rate[]" value="'.$data->conversion_rate.'"><td> <input type="button" name="delete" class="button" value="'.$delete.'" onclick="document.forms[\'DeleteCurrency\'].deleteCur.value=\''.$data->id.'\';document.forms[\'DeleteCurrency\'].submit();"> </td></tr>';
217 <tr><td></td><td></td><td></td><td></td><td></td><td> <input type='submit' name='Update' value='$update' class='button'></TD></form> </td></tr>
218 <tr><td colspan='3'><br></td></tr>
219 <form name="AddCurrency" action="index.php" method="post">
220 <input type='hidden' name='action' value='{$_REQUEST['action']}'>
221 <input type='hidden' name='module' value='{$_REQUEST['module']}'>
222 <tr><td><input type = 'text' name='addname' value=''> </td><td><input type = 'text' name='addiso' size='3' maxlength='3' value=''> </td><td><input type = 'text' name='addsymbol' value=''></td><td colspan='2'> <input type ='text' name='addrate'></td><td> <input type='submit' name='Add' value='$add' class='button'></td></tr>
223 </form></table></td></tr></table>
229 function setCurrencyFields($fields){
230 $json = getJSONobj();
231 $this->javascript .= 'var currencyFields = ' . $json->encode($fields) . ";\n";
237 //$lc = new ListCurrency();
238 //$lc->handleDelete();
240 //$lc->handleUpdate();
241 //echo '<select>'. $lc->getSelectOptions() . '</select>';
242 //echo $lc->getTable();