]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/generic/SugarWidgets/SugarWidgetFieldcurrency.php
Release 6.5.8
[Github/sugarcrm.git] / include / generic / SugarWidgets / SugarWidgetFieldcurrency.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                                                                                        
40
41
42 global $current_user;
43                                                                                        
44 $global_currency_obj = null;
45                                                                                        
46 function get_currency()
47 {
48         global $current_user,$global_currency_obj;
49         if (empty($global_currency_obj))
50         {
51         $global_currency_obj = new Currency();
52       //  $global_currency_symbol = '$';
53                                                                                        
54         if($current_user->getPreference('currency') )
55         {
56                 $global_currency_obj->retrieve($current_user->getPreference('currency'));
57         }
58         else
59         {
60                 $global_currency_obj->retrieve('-99');
61         }
62         }
63         return $global_currency_obj;
64 }
65
66
67 class SugarWidgetFieldCurrency extends SugarWidgetFieldInt
68 {
69     function SugarWidgetFieldCurrency(&$layout_manager) {
70         parent::__construct($layout_manager);
71         $this->reporter = $this->layout_manager->getAttribute('reporter');  
72     }
73     
74
75     function & displayList($layout_def)
76         {
77             $symbol = '';
78             $currency_id = '-99';
79             // 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
80             if ( empty($layout_def['group_function']) || $this->isSystemCurrency($layout_def) ) {
81                 $c = $this->getCurrency($layout_def);
82                 if(!empty($c['currency_id']) && !empty($c['currency_symbol']))
83                 {
84                     $symbol = $c['currency_symbol'];
85                     $currency_id = $c['currency_id'];
86                 }
87             }
88             $layout_def['currency_symbol'] = $symbol;
89             $layout_def['currency_id'] = $currency_id;
90             $display = $this->displayListPlain($layout_def);
91             
92         if(!empty($layout_def['column_key'])){
93             $field_def = $this->reporter->all_fields[$layout_def['column_key']];    
94         }else if(!empty($layout_def['fields'])){
95             $field_def = $layout_def['fields'];
96         }
97         $record = '';
98         if ($layout_def['table_key'] == 'self' && isset($layout_def['fields']['PRIMARYID']))
99             $record = $layout_def['fields']['PRIMARYID'];
100         else if (isset($layout_def['fields'][strtoupper($layout_def['table_alias']."_id")])){ 
101             $record = $layout_def['fields'][strtoupper($layout_def['table_alias']."_id")];
102         }
103         if (!empty($record)) {
104                 $field_name = $layout_def['name'];
105                 $field_type = $field_def['type'];
106                 $module = $field_def['module'];
107         
108                 $div_id = $module ."&$record&$field_name";
109                 $str = "<div id='$div_id'>".$display;
110             global $sugar_config;
111             if (isset ($sugar_config['enable_inline_reports_edit']) && $sugar_config['enable_inline_reports_edit']) {
112                 $str .= "&nbsp;" .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\");'");
113             }
114                 $str .= "</div>";
115                 return $str;
116         }
117         else
118             return $display;
119     }
120                              
121     function displayListPlain($layout_def) {
122         $cs = $this->getCurrency($layout_def);
123         $value = format_number(parent::displayListPlain($layout_def), null, null, array_merge(array('convert' => false), $cs));
124         return $value;
125     }                                                          
126  function queryFilterEquals(&$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 queryFilterNot_Equals(&$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 queryFilterGreater(&$layout_def)
137  {
138      return $this->_get_column_select($layout_def)." > ".$GLOBALS['db']->quote(unformat_number($layout_def['input_name0']))."\n";
139  }
140                                                                                        
141  function queryFilterLess(&$layout_def)
142  {
143      return $this->_get_column_select($layout_def)." < ".$GLOBALS['db']->quote(unformat_number($layout_def['input_name0']))."\n";
144  }
145
146  function queryFilterBetween(&$layout_def){
147      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";
148  }
149
150  function isSystemCurrency(&$layout_def)
151  {
152      if (strpos($layout_def['name'],'_usdoll') === false) {
153          return false;
154      } else {
155          return true;
156      }
157  }
158
159  function querySelect(&$layout_def)
160  {
161     // add currency column to select
162     $table = $this->getCurrencyIdTable($layout_def);
163     if($table) {
164         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";
165     }
166     return $this->_get_column_select($layout_def)." ".$this->_get_column_alias($layout_def)."\n";
167  }
168
169  function queryGroupBy($layout_def)
170  {
171     // add currency column to group by
172     $table = $this->getCurrencyIdTable($layout_def);
173     if($table) {
174         return $this->_get_column_select($layout_def)." , ".$table.".currency_id \n";
175     }
176     return $this->_get_column_select($layout_def)." \n";
177  }
178
179  function getCurrencyIdTable($layout_def)
180  {
181     // We need to fetch the currency id as well
182     if ( !$this->isSystemCurrency($layout_def) && empty($layout_def['group_function'])) {
183
184         if ( !empty($layout_def['table_alias']) ) {
185             $table = $layout_def['table_alias'];
186         } else {
187             $table = '';
188         }
189
190         $real_table = '';
191         if (!empty($this->reporter->all_fields[$layout_def['column_key']]['real_table']))
192             $real_table = $this->reporter->all_fields[$layout_def['column_key']]['real_table'];
193
194         $add_currency_id = false;
195         if(!empty($table)) {
196             $cols = $GLOBALS['db']->getHelper()->get_columns($real_table);
197             $add_currency_id = isset($cols['currency_id']) ? true : false;
198
199             if(!$add_currency_id && preg_match('/.*?_cstm$/i', $real_table)) {
200                 $table = str_replace('_cstm', '', $table);
201                 $cols = $GLOBALS['db']->getHelper()->get_columns($table);
202                 $add_currency_id = isset($cols['currency_id']) ? true : false;
203             }
204             if($add_currency_id) {
205                 return $table;
206             }
207         }
208     }
209     return false;
210  }
211
212     /**
213      * Return currency for layout_def
214      * @param $layout_def mixed
215      * @return array Array with currency symbol and currency ID
216      */
217     protected function getCurrency($layout_def)
218     {
219         $currency_id = false;
220         $currency_symbol = false;
221         if(isset($layout_def['currency_symbol']) && isset($layout_def['currency_id']))
222         {
223             $currency_symbol = $layout_def['currency_symbol'];
224             $currency_id = $layout_def['currency_id'];
225         }
226         else
227         {
228             $key = strtoupper(isset($layout_def['varname']) ? $layout_def['varname'] : $this->_get_column_alias($layout_def));
229             if ( $this->isSystemCurrency($layout_def) )
230             {
231                 $currency_id = '-99';
232             }
233             elseif (isset($layout_def['fields'][$key.'_CURRENCY']))
234             {
235                 $currency_id = $layout_def['fields'][$key.'_CURRENCY'];
236             }
237             elseif(isset($layout_def['fields'][$this->getTruncatedColumnAlias($this->_get_column_alias($layout_def)."_currency")]))
238             {
239                 $currency_id = $layout_def['fields'][$this->getTruncatedColumnAlias($this->_get_column_alias($layout_def)."_currency")];
240             }
241             if($currency_id)
242             {
243                 $currency = BeanFactory::getBean('Currencies', $currency_id);
244                 if(!empty($currency ->symbol))
245                 {
246                     $currency_symbol = $currency ->symbol;
247                 }
248             }
249         }
250         return array('currency_symbol' => $currency_symbol, 'currency_id' => $currency_id);
251     }
252 }
253 ?>