]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Currencies/ListCurrency.php
Release 6.5.0
[Github/sugarcrm.git] / modules / Currencies / ListCurrency.php
1 <?php
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-2012 SugarCRM Inc.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38  
39  class ListCurrency{
40         var $focus = null;
41         var $list = null;
42         var $javascript = '<script>';
43         function lookupCurrencies(){
44                 
45                 
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);
51                 }else{
52                         $this->list = Array($this->focus);      
53                 } 
54                 
55         }
56         function handleAdd(){
57                         global $current_user;
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'])){
60                                 
61                                 $currency = new Currency();
62                                 if(isset($_POST['record']) && !empty($_POST['record'])){
63         
64                                         $currency->retrieve($_POST['record']);
65                                 }
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']);
71                                 $currency->save();
72                                 $this->focus = $currency;
73                         }
74                         }
75                 
76         }
77                 
78         function handleUpdate(){
79                 global $current_user;
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'])){
82                         $ids = $_POST['id'];
83                         $names= $_POST['name'];
84                         $symbols= $_POST['symbol'];
85                         $rates  = $_POST['rate'];
86                         $isos  = $_POST['iso'];
87                         $size = sizeof($ids);
88                         if($size != sizeof($names)|| $size != sizeof($isos) || $size != sizeof($symbols) || $size != sizeof($rates)){
89                                 return; 
90                         }
91                         
92                                 $temp = new Currency();
93                         for($i = 0; $i < $size; $i++){
94                                 $temp->id = $ids[$i];
95                                 $temp->name = $names[$i];
96                                 $temp->symbol = $symbols[$i];
97                                 $temp->iso4217 = $isos[$i];
98                                 $temp->conversion_rate = $rates[$i];
99                                 $temp->save();
100                         }
101         }}
102         }
103         
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];
110                                         }
111                                         function ConvertToDollar(amount, rate){
112                                                 return amount / rate;
113                                         }
114                                         function ConvertFromDollar(amount, rate){
115                                                 return amount * rate;
116                                         }
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]));
120                                                         }
121                                                         lastRate = ConversionRates[id];
122                                                 }
123                                         function ConvertRateSingle(id,field){
124                                                 var temp = field.innerHTML.substring(1, field.innerHTML.length);
125                                                 unformattedNumber = unformatNumber(temp, num_grp_sep, dec_sep);
126                                                 
127                                                 field.innerHTML = CurrencySymbols[id] + formatNumber(toDecimal(ConvertFromDollar(ConvertToDollar(unformattedNumber, lastRate), ConversionRates[id])), num_grp_sep, dec_sep, 2, 2);
128                                                 lastRate = ConversionRates[id];
129                                         }
130                                         function CurrencyConvertAll(form){
131                         try {
132                         var id = form.currency_id.options[form.currency_id.selectedIndex].value;
133                                                 var fields = new Array();
134                                                 
135                                                 for(i in currencyFields){
136                                                         var field = currencyFields[i];
137                                                         if(typeof(form[field]) != 'undefined'){
138                                                                 form[field].value = unformatNumber(form[field].value, num_grp_sep, dec_sep);
139                                                                 fields.push(form[field]);
140                                                         }
141                                                         
142                                                 }
143                                                         
144                                                         ConvertRate(id, fields);
145                                                 for(i in fields){
146                                                         fields[i].value = formatNumber(fields[i].value, num_grp_sep, dec_sep);
147
148                                                 }
149                                                         
150                                                 } catch (err) {
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.
153                         }
154                                                 
155                                         }
156                                 </script>
157 EOQ;
158         }
159         
160         
161         function getSelectOptions($id = ''){
162                 global $current_user;
163                 $this->javascript .="var ConversionRates = new Array(); \n";            
164                 $this->javascript .="var CurrencySymbols = new Array(); \n";
165                 $options = '';
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>';
173                         $setLastRate = true;
174                         $this->javascript .= 'var lastRate = "' . $data->conversion_rate . '";';
175                         
176                         }else{
177                                 $options .= '<option value="'. $data->id . '">' ;
178                         }
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";
182                 }}
183                 if(!$setLastRate){
184                         $this->javascript .= 'var lastRate = "1";';
185                 }
186                 
187         }
188         return $options;
189         }
190         function getTable(){
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');
199                 
200                 $form = $html = "<br><table cellpadding='0' cellspacing='0' border='0'  class='tabForm'><tr><td><tableborder='0' cellspacing='0' cellpadding='0'>";
201                 $form .= <<<EOQ
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>
204
205                                         <tr><td><B>$currency</B></td><td><B>ISO 4217</B>&nbsp;</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']}'>
209 EOQ;
210                 if(isset($this->list ) && !empty($this->list )){
211                 foreach ($this->list as $data){
212                         
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.'&nbsp;</td><td><input type="text" name="rate[]" value="'.$data->conversion_rate.'"><td>&nbsp;<input type="button" name="delete" class="button" value="'.$delete.'" onclick="document.forms[\'DeleteCurrency\'].deleteCur.value=\''.$data->id.'\';document.forms[\'DeleteCurrency\'].submit();"> </td></tr>';
214                 }
215                 }
216                 $form .= <<<EOQ
217                                         <tr><td></td><td></td><td></td><td></td><td></td><td>&nbsp;<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=''>&nbsp;</td><td><input type = 'text' name='addiso' size='3' maxlength='3' value=''>&nbsp;</td><td><input type = 'text' name='addsymbol' value=''></td><td colspan='2'>&nbsp;<input type ='text' name='addrate'></td><td>&nbsp;<input type='submit' name='Add' value='$add' class='button'></td></tr>
223                                         </form></table></td></tr></table>
224 EOQ;
225         return $form;
226                 
227         }
228         
229         function setCurrencyFields($fields){
230                 $json = getJSONobj();
231                 $this->javascript .= 'var currencyFields = ' . $json->encode($fields) . ";\n";
232         }
233                                 
234                 
235 }
236
237 //$lc = new ListCurrency();
238 //$lc->handleDelete();
239 //$lc->handleAdd();
240 //$lc->handleUpdate();
241 //echo '<select>'. $lc->getSelectOptions() . '</select>';
242 //echo $lc->getTable();
243
244 ?>