public function getCurrentAvgEurRate($targetcurrency, Gpf_DateTime_Range $range) { $sql = new Gpf_SqlBuilder_SelectBuilder(); $sql->select->add('avg(' . Gpf_Db_Table_CurrencyRates::RATE . ')', Gpf_Db_Table_CurrencyRates::RATE); $sql->from->add(Gpf_Db_Table_CurrencyRates::getName()); $sql->where->add(Gpf_Db_Table_CurrencyRates::SOURCE_CURRENCY, '=', 'EUR'); $sql->where->add(Gpf_Db_Table_CurrencyRates::TARGET_CURRENCY, '=', $targetcurrency); $sql->where->add(Gpf_Db_Table_CurrencyRates::TYPE, '=', Gpf_Db_CurrencyRate::TYPE_DAILY); $dateCondition = new Gpf_SqlBuilder_CompoundWhereCondition(); $coumpoundCondWithin = new Gpf_SqlBuilder_CompoundWhereCondition(); $coumpoundCondWithin->add(Gpf_Db_Table_CurrencyRates::VALID_FROM, '>=', $range->getFrom()->toDateTime()); $coumpoundCondWithin->add(Gpf_Db_Table_CurrencyRates::VALID_TO, '<=', $range->getTo()->toDateTime()); $dateCondition->addCondition($coumpoundCondWithin, 'OR'); $coumpoundCondBefore = new Gpf_SqlBuilder_CompoundWhereCondition(); $coumpoundCondBefore->add(Gpf_Db_Table_CurrencyRates::VALID_FROM, '<=', $range->getFrom()->toDateTime()); $coumpoundCondBefore->add(Gpf_Db_Table_CurrencyRates::VALID_TO, '>=', $range->getFrom()->toDateTime()); $dateCondition->addCondition($coumpoundCondBefore, 'OR'); $coumpoundCondAfter = new Gpf_SqlBuilder_CompoundWhereCondition(); $coumpoundCondAfter->add(Gpf_Db_Table_CurrencyRates::VALID_FROM, '<=', $range->getTo()->toDateTime()); $coumpoundCondAfter->add(Gpf_Db_Table_CurrencyRates::VALID_TO, '>=', $range->getTo()->toDateTime()); $dateCondition->addCondition($coumpoundCondAfter, 'OR'); $sql->where->addCondition($dateCondition); Gpf_Log::debug($sql->toString()); Gpf_Log::debug('Avg rate: ' . $sql->getOneRow()->get(Gpf_Db_Table_CurrencyRates::RATE)); return $sql->getOneRow()->get(Gpf_Db_Table_CurrencyRates::RATE); }
/** * * @param $from timestamp or datetime string * @param $to timestamp or datetime string */ public function setRange(Gpf_DateTime_Range $range) { $this->setDateFrom($range->getFrom()); $this->setDateTo($range->getTo()); }