/** * Override to support getting the rate of the currency to the base currency by a web-service. */ protected function attemptToSaveModelFromPost($model, $redirectUrlParams = null, $redirect = true) { assert('$redirectUrlParams == null || is_array($redirectUrlParams)'); $postVariableName = get_class($model); if (isset($_POST[$postVariableName])) { $model->setAttributes($_POST[$postVariableName]); if ($model->rateToBase == null && $model->code != null) { $currencyHelper = Yii::app()->currencyHelper; if (!ZurmoCurrencyCodes::isValidCode($model->code)) { $model->addError('code', Zurmo::t('ZurmoModule', 'Invalid currency code')); $currencyHelper->resetErrors(); return $model; } $rate = (double) $currencyHelper->getConversionRateToBase($model->code); if ($currencyHelper->getWebServiceErrorCode() == $currencyHelper::ERROR_INVALID_CODE) { Yii::app()->user->setFlash('notification', Zurmo::t('ZurmoModule', 'The currency rate web service says this currency code is invalid even though zurmo says it is valid. The rate could not be automatically updated.')); $currencyHelper->resetErrors(); } elseif ($currencyHelper->getWebServiceErrorCode() == $currencyHelper::ERROR_WEB_SERVICE) { Yii::app()->user->setFlash('notification', Zurmo::t('ZurmoModule', 'The currency rate web service was unavailable. The rate could not be automatically updated.')); $currencyHelper->resetErrors(); } $model->rateToBase = $rate; } if ($model->save()) { $this->redirectAfterSaveModel($model->id, $redirectUrlParams); } } return $model; }
/** * @return array - Jui AutoComplete ready array * containing value and label elements. */ public static function getByPartialCodeOrName($partialCodeOrName) { $autoCompleteResults = array(); $codesAndNames = ZurmoCurrencyCodes::getByPartialCodeOrName($partialCodeOrName); foreach ($codesAndNames as $code => $name) { $autoCompleteResults[] = array('value' => $code, 'label' => $code . ' ' . $name); } return $autoCompleteResults; }
/** * @return array - Jui AutoComplete ready array * containing value and label elements. */ public static function getByPartialCodeOrName($partialCodeOrName, $autoCompleteOptions = null) { // autoCompleteOptions is not used but here for future uses. $autoCompleteResults = array(); $codesAndNames = ZurmoCurrencyCodes::getByPartialCodeOrName($partialCodeOrName); foreach ($codesAndNames as $code => $name) { $autoCompleteResults[] = array('value' => $code, 'label' => $code . ' ' . $name); } return $autoCompleteResults; }
public function testIsValidCode() { $this->assertTrue(ZurmoCurrencyCodes::isValidCode('USD')); $this->assertTrue(ZurmoCurrencyCodes::isValidCode('EUR')); $this->assertFalse(ZurmoCurrencyCodes::isValidCode('INVALID')); }