Пример #1
0
 /**
  * @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);
 }
Пример #2
0
 /**
  * @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);
 }