/**
  * @see SugarFieldBase::importSanitize()
  */
 public function importSanitize($value, $vardef, $focus, ImportFieldSanitize $settings)
 {
     require_once 'include/SugarCurrency/SugarCurrency.php';
     /** @var Currency $base_currency */
     $base_currency = SugarCurrency::getBaseCurrency();
     $currency_id = $settings->currency_id;
     // Remove the grouping separator
     $value = str_replace($settings->num_grp_sep, '', $value);
     // change the decimal separator to a . if it's not already one
     if ($settings->dec_sep != '.') {
         $value = str_replace($settings->dec_sep, '.', $value);
     }
     if (isset($vardef['convertToBase']) && $vardef['convertToBase']) {
         // convert amount from base
         $value = str_replace($base_currency->symbol, '', $value);
         try {
             $value = SugarCurrency::convertAmountFromBase($value, $settings->currency_id);
         } catch (SugarMath_Exception $sme) {
             $GLOBALS['log']->error('Currency Field Import Error: ' . $sme->getMessage());
             return false;
         }
     } elseif (isset($vardef['is_base_currency']) && $vardef['is_base_currency']) {
         $value = str_replace($base_currency->symbol, '', $value);
         $currency_id = $base_currency->id;
     } else {
         $value = str_replace($settings->currency_symbol, '', $value);
     }
     // last check, if for some reason we get here and the value is not numeric, we should just fail out.
     if (!is_numeric($value)) {
         return false;
     }
     return SugarCurrency::formatAmount($value, $currency_id, 6, '.', '', false);
 }
Example #2
0
 public function __construct()
 {
     parent::__construct();
     // initialize currency
     $currency = SugarCurrency::getBaseCurrency();
     $this->currency_id = $currency->id;
     $this->base_rate = $currency->conversion_rate;
 }
 function &displayList($layout_def)
 {
     global $current_user;
     if (empty($layout_def['group_function'])) {
         $currency = $this->getCurrency($layout_def);
         $symbol = $currency['currency_symbol'];
         $currency_id = $currency['currency_id'];
     } else {
         $currency = $current_user->getPreference('currency_show_preferred') ? SugarCurrency::getUserLocaleCurrency() : SugarCurrency::getBaseCurrency();
         $currency_id = $currency->id;
         $symbol = $currency->symbol;
     }
     $layout_def['currency_symbol'] = $symbol;
     $layout_def['currency_id'] = $currency_id;
     $display = $this->displayListPlain($layout_def);
     if (!empty($layout_def['column_key'])) {
         $field_def = $this->reporter->all_fields[$layout_def['column_key']];
     } else {
         if (!empty($layout_def['fields'])) {
             $field_def = $layout_def['fields'];
         }
     }
     $record = '';
     if ($layout_def['table_key'] == 'self' && isset($layout_def['fields']['PRIMARYID'])) {
         $record = $layout_def['fields']['PRIMARYID'];
     } else {
         if (isset($layout_def['fields'][strtoupper($layout_def['table_alias'] . "_id")])) {
             $record = $layout_def['fields'][strtoupper($layout_def['table_alias'] . "_id")];
         }
     }
     if (!empty($record)) {
         $field_name = $layout_def['name'];
         $field_type = $field_def['type'];
         $module = $field_def['module'];
         $div_id = $module . "&{$record}&{$field_name}";
         $str = "<div id='{$div_id}'>" . $display;
         global $sugar_config;
         if (isset($sugar_config['enable_inline_reports_edit']) && $sugar_config['enable_inline_reports_edit']) {
             $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}\");'");
         }
         $str .= "</div>";
         return $str;
     } else {
         return $display;
     }
 }
 /**
  * @param ServiceBase $api
  * @param array $args
  * @return array
  * @throws SugarApiExceptionInvalidParameter
  * @throws SugarApiExceptionNotAuthorized
  * @throws SugarApiExceptionNotFound
  */
 public function pipeline(ServiceBase $api, $args)
 {
     // if not in the allowed module list, then we throw a 404 not found
     if (!in_array($args['module'], $this->allowedModules)) {
         throw new SugarApiExceptionNotFound();
     }
     // make sure we can view the module first
     // since we don't have a proper record just make an empty one
     $args['record'] = '';
     /* @var $seed Opportunity|Product|RevenueLineItem */
     $seed = $this->loadBean($api, $args, 'view');
     if (!$seed->ACLAccess('view')) {
         throw new SugarApiExceptionNotAuthorized();
     }
     $tp = $this->getTimeperiod($args['timeperiod_id']);
     // check the type param
     if (!isset($args['type']) || $args['type'] != 'user' && $args['type'] != 'group') {
         $args['type'] = 'user';
     }
     $settings = $this->getForecastSettings();
     // get sales_stages to ignore
     $ignore_stages = array_merge($settings['sales_stage_won'], $settings['sales_stage_lost']);
     // get the amount field here
     $amount_field = $this->moduleAmountField[$seed->module_name];
     $sq = $this->buildQuery($api, $seed, $tp, $amount_field, $args['type']);
     // run the query
     $rows = $sq->execute();
     // data storage
     $data = array();
     // keep track of the total for later user
     $total = SugarMath::init('0', 0);
     foreach ($rows as $row) {
         // if the sales stage is one we need to ignore, the just continue to the next record
         if (in_array($row['sales_stage'], $ignore_stages)) {
             continue;
         }
         // if we have not seen this sales stage before, set the value to zero (0)
         if (!isset($data[$row['sales_stage']])) {
             $data[$row['sales_stage']] = array('count' => 0, 'total' => '0');
         }
         // if customers have made amount not required, it saves to the DB as NULL
         // make sure we set it to 0 for the math ahead
         if (empty($row['amount'])) {
             $row['amount'] = 0;
         }
         // convert to the base currency
         $base_amount = SugarCurrency::convertWithRate($row[$amount_field], $row['base_rate']);
         // add the new value into what was already there
         $data[$row['sales_stage']]['total'] = SugarMath::init($data[$row['sales_stage']]['total'], 0)->add($base_amount)->result();
         $data[$row['sales_stage']]['count']++;
         // add to the total
         $total->add($base_amount);
     }
     // get the default currency
     /* @var $currency Currency */
     $currency = SugarCurrency::getBaseCurrency();
     // setup for return format
     $return_data = array();
     $series = 0;
     $previous_value = SugarMath::init('0', 0);
     foreach ($data as $key => $item) {
         $value = $item['total'];
         // set up each return key
         $return_data[] = array('key' => $key, 'count' => $item['count'], 'values' => array(array('series' => $series++, 'label' => SugarCurrency::formatAmount($value, $currency->id, 0), 'value' => intval($value), 'x' => 0, 'y' => intval($value), 'y0' => intval($previous_value->result()))));
         // save the previous value for use in the next item in the series
         $previous_value->add($value);
     }
     // actually return the formatted data
     $mod_strings = return_module_language($GLOBALS['current_language'], $seed->module_name);
     //return the total from the SugarMath Object.
     $total = $total->result();
     return array('properties' => array('title' => $mod_strings['LBL_PIPELINE_TOTAL_IS'] . SugarCurrency::formatAmount($total, $currency->id), 'total' => $total, 'scale' => 1000, 'units' => $currency->symbol), 'data' => $return_data);
 }
 /**
  * Override save so we always set the currency_id and base_rate to the default system currency
  *
  * @override
  * @param bool $check_notify
  * @return String
  */
 public function save($check_notify = false)
 {
     // make sure the currency and base_rate are always set to the base currency for the manager worksheets
     $currency = SugarCurrency::getBaseCurrency();
     $this->currency_id = $currency->id;
     $this->base_rate = $currency->conversion_rate;
     return parent::save($check_notify);
 }
Example #6
0
 public function save($check_notify = false)
 {
     // set the currency for the forecast to always be the base currency
     // since the committed end point only sends the data as the base currency format
     if (empty($this->currency_id)) {
         // use user preferences for currency
         $currency = SugarCurrency::getBaseCurrency();
         $this->currency_id = $currency->id;
     } else {
         $currency = SugarCurrency::getCurrencyByID($this->currency_id);
     }
     $this->base_rate = $currency->conversion_rate;
     parent::save($check_notify);
 }
Example #7
0
 function get_next_row($result_field_name = 'result', $column_field_name = 'display_columns', $skip_non_summary_columns = false, $exporting = false)
 {
     global $current_user;
     $chart_cells = array();
     if ($this->do_export) {
         $db_row = $this->db->fetchByAssoc($this->{$result_field_name}, false);
     } else {
         $db_row = $this->db->fetchByAssoc($this->{$result_field_name});
     }
     if ($db_row == 0 || sizeof($db_row) == 0) {
         return 0;
     }
     // Call custom hooks
     if (isset($this->full_bean_list) && is_array($this->full_bean_list) && array_key_exists('self', $this->full_bean_list) && is_object($this->full_bean_list['self']) && method_exists($this->full_bean_list['self'], 'call_custom_logic')) {
         $this->full_bean_list['self']->call_custom_logic('process_report_row', array('row' => &$db_row, 'reporter' => $this));
     }
     if ($result_field_name == 'summary_result') {
         if (!empty($this->child_filter) && !empty($db_row[$this->child_filter_name])) {
             $this->child_filter_by = $db_row[$this->child_filter_name];
         } else {
             $this->child_filter = '';
             $this->child_filter_by = '';
             $this->child_filter_name = '';
         }
     }
     $row = array();
     $cells = array();
     $fields = array();
     foreach ($db_row as $key => $value) {
         //if value is null or not set, then change to empty string.  This prevents array index errors while processing
         $fields[strtoupper($key)] = is_null($value) ? '' : $value;
     }
     // here we want to make copies, so use foreach
     foreach ($this->report_def[$column_field_name] as $display_column) {
         $display_column['table_alias'] = $this->getTableFromField($display_column);
         $this->register_field_for_query($display_column);
         if ($skip_non_summary_columns && empty($display_column['group_function'])) {
             if ($exporting || $this->plain_text_output) {
                 array_push($cells, ' ');
             } else {
                 array_push($cells, '&nbsp;');
             }
             continue;
         }
         $display_column['fields'] = $fields;
         if ($this->plain_text_output == true) {
             /*nsingh: bug 13554- date and time fields must be displayed using user's locale settings.
              * Since to_pdf uses plain_text_output=true, we handle the date and time case here by using the 'List' context of the layout_manager
              */
             if ($display_column['type'] == 'date' || $display_column['type'] == 'time' || $display_column['type'] == 'datetimecombo') {
                 $this->layout_manager->setAttribute('context', 'List');
             } else {
                 $this->layout_manager->setAttribute('context', 'ListPlain');
             }
         } else {
             $this->layout_manager->setAttribute('context', 'List');
         }
         // Make sure 'AVG' aggregate is shown as float, regardless of the original field type
         if (!empty($display_column['group_function']) && strtolower($display_column['group_function']) === 'avg' && $display_column['type'] != 'currency') {
             $display_column['type'] = 'float';
         }
         if ($display_column['type'] != 'currency' || substr_count($display_column['name'], '_usdoll') == 0 && (isset($display_column['group_function']) ? $display_column['group_function'] != 'weighted_amount' && $display_column['group_function'] != 'weighted_sum' : true)) {
             $pos = $display_column['table_key'];
             $module_name = '';
             if ($pos) {
                 $module_name = substr($pos, strrpos($pos, ':') + 1);
             }
             $field_name = $this->getColumnFieldName($display_column);
             if ($module_name == 'currencies' && empty($display_column['fields'][$field_name])) {
                 switch ($display_column['name']) {
                     case 'iso4217':
                         $display = $this->currency_obj->getDefaultISO4217();
                         break;
                     case 'symbol':
                         $display = $this->currency_obj->getDefaultCurrencySymbol();
                         break;
                     case 'name':
                         $display = $this->currency_obj->getDefaultCurrencyName();
                         break;
                     default:
                         $display = $this->layout_manager->widgetDisplay($display_column);
                 }
                 $display_column['fields'][$field_name] = $display;
             } else {
                 if (!empty($field_name) && isset($display_column['fields'][$field_name])) {
                     $display_column['fields'][$field_name] = $this->db->fromConvert($display_column['fields'][$field_name], $display_column['type']);
                 }
                 $display = $this->layout_manager->widgetDisplay($display_column);
             }
         } else {
             if (isset($display_column['group_function'])) {
                 $field_name = $this->getTruncatedColumnAlias(strtoupper($display_column['table_alias']) . "_" . strtoupper($display_column['group_function']) . "_" . strtoupper($display_column['name']));
             } else {
                 unset($field_name);
             }
             if (!isset($field_name) || !isset($display_column['fields'][$field_name])) {
                 $field_name = $this->getTruncatedColumnAlias(strtoupper($display_column['table_alias']) . "_" . strtoupper($display_column['name']));
             }
             if (isset($display_column['fields'][$field_name])) {
                 $display = $display_column['fields'][$field_name];
             }
         }
         if ($display_column['type'] == 'currency' && (strpos($display_column['name'], '_usdoll') !== false || !empty($display_column['group_function']))) {
             // convert base to user preferred if set in user prefs
             if ($current_user->getPreference('currency_show_preferred')) {
                 $userCurrency = SugarCurrency::getUserLocaleCurrency();
                 $raw_display = SugarCurrency::convertWithRate($display_column['fields'][$field_name], 1.0, $userCurrency->conversion_rate);
                 $display = SugarCurrency::formatAmountUserLocale($raw_display, $userCurrency->id);
             } else {
                 $raw_display = $display_column['fields'][$field_name];
                 $display = SugarCurrency::formatAmountUserLocale($raw_display, SugarCurrency::getBaseCurrency()->id);
             }
         } else {
             $raw_display = $display;
         }
         if (isset($display_column['type']) && $display_column['type'] == 'float') {
             $display = $this->layout_manager->widgetDisplay($display_column);
         }
         if (isset($display_column['type'])) {
             $alias = $this->alias_lookup[$display_column['table_key']];
             $array_key = strtoupper($alias . '__count');
             if (array_key_exists($array_key, $display_column['fields'])) {
                 $displayData = $display_column['fields'][$array_key];
                 if (empty($displayData) && $display_column['type'] != 'bool' && ($display_column['type'] != 'enum' || $display_column['type'] == 'enum' && $displayData != '0')) {
                     $display = "";
                 }
             }
             // if
             $module_bean = BeanFactory::getBean($this->module);
             if (is_array($module_bean->field_defs)) {
                 if (isset($module_bean->field_defs[$display_column['type']])) {
                     if (isset($module_bean->field_defs[$display_column['type']]['options'])) {
                         $trans_options = translate($module_bean->field_defs[$display_column['type']]['options']);
                         if (isset($trans_options[$display_column['fields'][$field_name]])) {
                             $display = $trans_options[$display_column['fields'][$field_name]];
                         }
                     }
                 }
             }
         }
         // if
         //  for charts
         if ($column_field_name == 'summary_columns' && $this->do_chart) {
             //_pp($display);
             $raw_value = "";
             $keys = array_keys($fields);
             foreach ($this->report_def['summary_columns'] as $index => $column) {
                 if ($column['name'] == $display_column['name'] && isset($keys[$index]) && isset($fields[$keys[$index]])) {
                     $raw_value = $fields[$keys[$index]];
                     break;
                 }
             }
             $cell_arr = array('val' => $raw_display, 'key' => $display_column['column_key'], 'raw_value' => $raw_value);
             //_pp($cell_arr);
             array_push($chart_cells, $cell_arr);
         }
         if ($exporting) {
             global $app_list_strings;
             // parse out checkboxes
             // TODO: wp this should be done in the widget
             if (preg_match('/type.*=.*checkbox/Uis', $display)) {
                 if (preg_match('/checked/i', $display)) {
                     $display = $app_list_strings['dom_switch_bool']['on'];
                 } else {
                     $display = $app_list_strings['dom_switch_bool']['off'];
                 }
             }
         }
         array_push($cells, $display);
     }
     // END foreach
     $row['cells'] = $cells;
     // calculate summary rows count as the product of all count fields in summary
     $count = 1;
     $count_exists = false;
     foreach ($db_row as $count_column => $count_value) {
         if (substr($count_column, -10) == "__allcount" || $count_column == 'count') {
             $count *= max($count_value, 1);
             $count_exists = true;
         }
     }
     if ($count_exists) {
         $row['count'] = $count;
     }
     // for charts
     if ($column_field_name == 'summary_columns' && $this->do_chart) {
         $chart_row = 0;
         if (!empty($db_row['count'])) {
             $chart_row = array('cells' => $chart_cells, 'count' => $db_row['count']);
         } else {
             $chart_row = array('cells' => $chart_cells);
         }
         array_push($this->chart_rows, $chart_row);
     }
     if ($column_field_name == 'summary_columns' && isset($this->chart_group_position) && isset($this->group_header)) {
         $row['group_pos'] = $this->chart_group_position;
         $row['group_header'] = $this->group_header;
         $row['group_column_is_invisible'] = $this->group_column_is_invisible;
     }
     return $row;
 }