protected static function makeChartSqlQuery()
 {
     $quote = DatabaseCompatibilityUtil::getQuote();
     $where = null;
     $selectDistinct = false;
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('Opportunity');
     Opportunity::resolveReadPermissionsOptimizationToSqlQuery(Yii::app()->user->userModel, $joinTablesAdapter, $where, $selectDistinct);
     $selectQueryAdapter = new RedBeanModelSelectQueryAdapter($selectDistinct);
     $sumPart = "{$quote}currencyvalue{$quote}.{$quote}value{$quote} ";
     $sumPart .= "* {$quote}currencyvalue{$quote}.{$quote}ratetobase{$quote}";
     $selectQueryAdapter->addClause('customfield', 'value', 'stage');
     $selectQueryAdapter->addClauseByQueryString("sum({$sumPart})", 'amount');
     $joinTablesAdapter->addFromTableAndGetAliasName('customfield', 'stage_customfield_id', 'opportunity');
     $joinTablesAdapter->addFromTableAndGetAliasName('currencyvalue', 'amount_currencyvalue_id', 'opportunity');
     $groupBy = "{$quote}customfield{$quote}.{$quote}value{$quote}";
     $sql = SQLQueryUtil::makeQuery('opportunity', $selectQueryAdapter, $joinTablesAdapter, null, null, $where, null, $groupBy);
     return $sql;
 }