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-2013 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 ********************************************************************************/
44 $global_currency_obj = null;
46 function get_currency()
48 global $current_user,$global_currency_obj;
49 if (empty($global_currency_obj))
51 $global_currency_obj = new Currency();
52 // $global_currency_symbol = '$';
54 if($current_user->getPreference('currency') )
56 $global_currency_obj->retrieve($current_user->getPreference('currency'));
60 $global_currency_obj->retrieve('-99');
63 return $global_currency_obj;
67 class SugarWidgetFieldCurrency extends SugarWidgetFieldInt
69 function SugarWidgetFieldCurrency(&$layout_manager) {
70 parent::__construct($layout_manager);
71 $this->reporter = $this->layout_manager->getAttribute('reporter');
75 function & displayList($layout_def)
78 $symbol = $locale->getPrecedentPreference('default_currency_symbol');
79 $currency_id = $locale->getPrecedentPreference('currency');
81 // 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
82 if ( empty($layout_def['group_function']) || $this->isSystemCurrency($layout_def) ) {
83 $c = $this->getCurrency($layout_def);
84 if(!empty($c['currency_id']) && !empty($c['currency_symbol']))
86 $symbol = $c['currency_symbol'];
87 $currency_id = $c['currency_id'];
90 $layout_def['currency_symbol'] = $symbol;
91 $layout_def['currency_id'] = $currency_id;
92 $display = $this->displayListPlain($layout_def);
94 if(!empty($layout_def['column_key'])){
95 $field_def = $this->reporter->all_fields[$layout_def['column_key']];
96 }else if(!empty($layout_def['fields'])){
97 $field_def = $layout_def['fields'];
100 if ($layout_def['table_key'] == 'self' && isset($layout_def['fields']['PRIMARYID']))
101 $record = $layout_def['fields']['PRIMARYID'];
102 else if (isset($layout_def['fields'][strtoupper($layout_def['table_alias']."_id")])){
103 $record = $layout_def['fields'][strtoupper($layout_def['table_alias']."_id")];
105 if (!empty($record)) {
106 $field_name = $layout_def['name'];
107 $field_type = $field_def['type'];
108 $module = $field_def['module'];
110 $div_id = $module ."&$record&$field_name";
111 $str = "<div id='$div_id'>".$display;
112 global $sugar_config;
113 if (isset ($sugar_config['enable_inline_reports_edit']) && $sugar_config['enable_inline_reports_edit']) {
114 $str .= " " .SugarThemeRegistry::current()->getImage("edit_inline","border='0' alt='Edit Layout' align='bottom' onClick='SUGAR.reportsInlineEdit.inlineEdit(\"$div_id\",\"$value\",\"$module\",\"$record\",\"$field_name\",\"$field_type\",\"$currency_id\",\"$symbol\");'");
123 function displayListPlain($layout_def) {
124 $value = currency_format_number(
125 parent::displayListPlain($layout_def),
130 $this->getCurrency($layout_def)
135 function queryFilterEquals(&$layout_def)
137 return $this->_get_column_select($layout_def)."=".$GLOBALS['db']->quote(unformat_number($layout_def['input_name0']))."\n";
140 function queryFilterNot_Equals(&$layout_def)
142 return $this->_get_column_select($layout_def)."!=".$GLOBALS['db']->quote(unformat_number($layout_def['input_name0']))."\n";
145 function queryFilterGreater(&$layout_def)
147 return $this->_get_column_select($layout_def)." > ".$GLOBALS['db']->quote(unformat_number($layout_def['input_name0']))."\n";
150 function queryFilterLess(&$layout_def)
152 return $this->_get_column_select($layout_def)." < ".$GLOBALS['db']->quote(unformat_number($layout_def['input_name0']))."\n";
155 function queryFilterBetween(&$layout_def){
156 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";
159 function isSystemCurrency(&$layout_def)
161 if (strpos($layout_def['name'],'_usdoll') === false) {
168 function querySelect(&$layout_def)
170 // add currency column to select
171 $table = $this->getCurrencyIdTable($layout_def);
173 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";
175 return $this->_get_column_select($layout_def)." ".$this->_get_column_alias($layout_def)."\n";
178 function queryGroupBy($layout_def)
180 // add currency column to group by
181 $table = $this->getCurrencyIdTable($layout_def);
183 return $this->_get_column_select($layout_def)." , ".$table.".currency_id \n";
185 return $this->_get_column_select($layout_def)." \n";
188 function getCurrencyIdTable($layout_def)
190 // We need to fetch the currency id as well
191 if ( !$this->isSystemCurrency($layout_def) && empty($layout_def['group_function'])) {
193 if ( !empty($layout_def['table_alias']) ) {
194 $table = $layout_def['table_alias'];
200 if (!empty($this->reporter->all_fields[$layout_def['column_key']]['real_table']))
201 $real_table = $this->reporter->all_fields[$layout_def['column_key']]['real_table'];
203 $add_currency_id = false;
205 $cols = $GLOBALS['db']->getHelper()->get_columns($real_table);
206 $add_currency_id = isset($cols['currency_id']) ? true : false;
208 if(!$add_currency_id && preg_match('/.*?_cstm$/i', $real_table)) {
209 $table = str_replace('_cstm', '', $table);
210 $cols = $GLOBALS['db']->getHelper()->get_columns($table);
211 $add_currency_id = isset($cols['currency_id']) ? true : false;
213 if($add_currency_id) {
222 * Return currency for layout_def
223 * @param $layout_def mixed
224 * @return array Array with currency symbol and currency ID
226 protected function getCurrency($layout_def)
228 $currency_id = false;
229 $currency_symbol = false;
230 if(isset($layout_def['currency_symbol']) && isset($layout_def['currency_id']))
232 $currency_symbol = $layout_def['currency_symbol'];
233 $currency_id = $layout_def['currency_id'];
237 $key = strtoupper(isset($layout_def['varname']) ? $layout_def['varname'] : $this->_get_column_alias($layout_def));
238 if ( $this->isSystemCurrency($layout_def) )
240 $currency_id = '-99';
242 elseif (isset($layout_def['fields'][$key.'_CURRENCY']))
244 $currency_id = $layout_def['fields'][$key.'_CURRENCY'];
246 elseif(isset($layout_def['fields'][$this->getTruncatedColumnAlias($this->_get_column_alias($layout_def)."_currency")]))
248 $currency_id = $layout_def['fields'][$this->getTruncatedColumnAlias($this->_get_column_alias($layout_def)."_currency")];
252 $currency = BeanFactory::getBean('Currencies', $currency_id);
253 if(!empty($currency ->symbol))
255 $currency_symbol = $currency ->symbol;
259 return array('currency_symbol' => $currency_symbol, 'currency_id' => $currency_id);