]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/generic/SugarWidgets/SugarWidgetFieldcurrency.php
Release 6.1.4
[Github/sugarcrm.git] / include / generic / SugarWidgets / SugarWidgetFieldcurrency.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 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                                                                                        
40 require_once('include/generic/SugarWidgets/SugarWidget.php');
41                                                                                        
42
43 global $current_user;
44                                                                                        
45 $global_currency_obj = null;
46                                                                                        
47 function get_currency()
48 {
49         global $current_user,$global_currency_obj;
50         if (empty($global_currency_obj))
51         {
52         $global_currency_obj = new Currency();
53       //  $global_currency_symbol = '$';
54                                                                                        
55         if($current_user->getPreference('currency') )
56         {
57                 $global_currency_obj->retrieve($current_user->getPreference('currency'));
58         }
59         else
60         {
61                 $global_currency_obj->retrieve('-99');
62         }
63         }
64         return $global_currency_obj;
65 }
66
67
68 class SugarWidgetFieldCurrency extends SugarWidgetFieldInt
69 {
70         function & displayList($layout_def)
71         {
72             static $currencies;
73
74             // If it's not grouped, or if it's grouped around a system currency column, look up the currency symbol so we can display it next to the amount
75             if ( empty($layout_def['group_function']) || $this->isSystemCurrency($layout_def) ) {
76                 if ( isset($layout_def['varname']) ) {
77                     $key = $layout_def['varname'];
78                 } else {
79                     $key = $this->_get_column_alias($layout_def);
80                 }
81                 $key = strtoupper($key);
82                 
83                 if ( $this->isSystemCurrency($layout_def) ) {
84                     $currency_id = '-99';
85                 } else {
86                     $currency_id = $layout_def['fields'][$key.'_CURRENCY'];
87                 }
88                 if ( empty($currency_id) ) {
89                     $currency_id = '-99';
90                 }
91                 
92                 if ( !isset($currencies[$currency_id]) ) {
93                     $currencies[$currency_id] = new Currency();
94                     $currencies[$currency_id]->retrieve($currency_id);
95                 }
96                 if(!empty($currencies[$currency_id]->symbol)){
97                     $symbol = $currencies[$currency_id]->symbol.' ';
98                 } else {
99                     $symbol = '';
100                 }
101             } else {
102                 $symbol = '';
103             }
104 //                $global_currency_obj = get_currency();
105 //                  $display = format_number($this->displayListPlain($layout_def), 2, 2, array('convert' => true, 'currency_symbol' => true));
106 //                $display =  $global_currency_obj->symbol. round($global_currency_obj->convertFromDollar($this->displayListPlain($layout_def)),2);
107             $display = $symbol.$this->displayListPlain($layout_def);
108             return $display;
109         }
110                              
111     function displayListPlain($layout_def) {
112 //        $value = $this->_get_list_value($layout_def);
113         $value = format_number(parent::displayListPlain($layout_def), 2, 2, array('convert' => false, 'currency_symbol' => false));
114         return $value;
115     }                                                          
116  function queryFilterEquals(&$layout_def)
117  {
118      return $this->_get_column_select($layout_def)."=".$GLOBALS['db']->quote(unformat_number($layout_def['input_name0']))."\n";
119  }
120                                                                                        
121  function queryFilterNot_Equals(&$layout_def)
122  {
123      return $this->_get_column_select($layout_def)."!=".$GLOBALS['db']->quote(unformat_number($layout_def['input_name0']))."\n";
124  }
125                                                                                        
126  function queryFilterGreater(&$layout_def)
127  {
128      return $this->_get_column_select($layout_def)." > ".$GLOBALS['db']->quote(unformat_number($layout_def['input_name0']))."\n";
129  }
130                                                                                        
131  function queryFilterLess(&$layout_def)
132  {
133      return $this->_get_column_select($layout_def)." < ".$GLOBALS['db']->quote(unformat_number($layout_def['input_name0']))."\n";
134  }
135
136  function queryFilterBetween(&$layout_def){
137      return $this->_get_column_select($layout_def)." > ".$GLOBALS['db']->quote(unformat_number($layout_def['input_name0'])). " AND ". $this->_get_column_select($layout_def)." < ".$GLOBALS['db']->quote(unformat_number($layout_def['input_name1']))."\n";
138  }
139
140  function isSystemCurrency(&$layout_def)
141  {
142      if (strpos($layout_def['name'],'_usdoll') === false) {
143          return false;
144      } else {
145          return true;
146      }
147  }
148
149  function querySelect(&$layout_def)
150  {
151      // We need to fetch the currency id as well
152      if ( !$this->isSystemCurrency($layout_def) && empty($layout_def['group_function'])) {
153          if ( !empty($layout_def['table_alias']) ) {
154              $table = $layout_def['table_alias'];
155          } else {
156              $table = '';
157          }
158          
159          $add_currency_id = false;
160          if(!empty($table)) {
161                 $cols = $GLOBALS['db']->getHelper()->get_columns($table);
162             $add_currency_id = isset($cols['currency_id']) ? true : false;
163             
164             if(!$add_currency_id && preg_match('/.*?_cstm$/i', $table)) {
165                $table = str_replace('_cstm', '', $table);
166                $cols = $GLOBALS['db']->getHelper()->get_columns($table);
167                $add_currency_id = isset($cols['currency_id']) ? true : false;
168             }
169          }
170          
171          if($add_currency_id) {                 
172                 return $this->_get_column_select($layout_def)." ".$this->_get_column_alias($layout_def)." , ".$table.".currency_id ". $this->getTruncatedColumnAlias($this->_get_column_alias($layout_def)."_currency") . "\n";
173          } else {
174                 return $this->_get_column_select($layout_def)." ".$this->_get_column_alias($layout_def)."\n";
175          }      
176      } else {
177          return $this->_get_column_select($layout_def)." ".$this->_get_column_alias($layout_def)."\n";
178      }
179  }
180
181
182 }
183
184 ?>