/** * There is a special way you can import rateToBase and currencyCode for an amount attribute. * if the column data is formatted like: $54.67__1.2__USD then it will split the column and properly * handle rate and currency code. Eventually this will be exposed in the user interface * * @param mixed $value * @param string $columnName * @param array $columnMappingData * @param ImportSanitizeResultsUtil $importSanitizeResultsUtil * @return array */ public function resolveValueForImport($value, $columnName, $columnMappingData, ImportSanitizeResultsUtil $importSanitizeResultsUtil) { assert('is_string($columnName)'); $attributeNames = $this->getRealModelAttributeNames(); $modelClassName = $this->getModelClassName(); $parts = explode(FormModelUtil::DELIMITER, $value); if (count($parts) == 3) { $value = $parts[0]; $rateToBase = $parts[1]; try { $currency = Currency::getByCode($parts[2]); } catch (NotFoundException $e) { $currency = null; $importSanitizeResultsUtil->addMessage('Currency Code: ' . $parts[2] . ' is invalid.'); $importSanitizeResultsUtil->setModelShouldNotBeSaved(); } } else { $rateToBase = $columnMappingData['mappingRulesData']['CurrencyRateToBaseModelAttributeMappingRuleForm']['rateToBase']; $currency = Currency::getById((int) $columnMappingData['mappingRulesData']['CurrencyIdModelAttributeMappingRuleForm']['id']); } $sanitizedValue = ImportSanitizerUtil::sanitizeValueBySanitizerTypes(static::getSanitizerUtilTypesInProcessingOrder(), $modelClassName, $this->getModelAttributeName(), $value, $columnName, $columnMappingData, $importSanitizeResultsUtil); if ($sanitizedValue == null) { $sanitizedValue = 0; } $currencyValue = new CurrencyValue(); $currencyValue->setScenario('importModel'); $currencyValue->value = $sanitizedValue; $currencyValue->rateToBase = $rateToBase; $currencyValue->currency = $currency; return array($this->getModelAttributeName() => $currencyValue); }
/** * @param $login * @param $sum * @return bool */ public function addMoney($login, $sum) { $result = $this->proxy->addMoneyWithCurrency($login, CurrencyValue::valueOf(ItemMallCurrency::MAIN(), $sum), 3, time()); if ($result->status['name'] === 'Ok') { return true; } else { throw new \BadFunctionCallException("I don't know, what the f**k!"); } }
/** * Given an id of a currency model, determine if any currency values are using this currency. * @return true if at least one currency value model is using this currency. * @param integer $currencyId */ public static function isCurrencyInUseById($currencyId) { assert('is_int($currencyId)'); $columnName = RedBeanModel::getForeignKeyName('CurrencyValue', 'currency'); $quote = DatabaseCompatibilityUtil::getQuote(); $where = "{$quote}{$columnName}{$quote} = '{$currencyId}'"; $count = CurrencyValue::getCount(null, $where); if ($count > 0) { return true; } return false; }
/** * Render a form layout. * @param $form If the layout is editable, then pass a $form otherwise it can * be null. * @return A string containing the element's content. */ protected function renderFormLayout(ZurmoActiveForm $form) { $content = '<table>'; $content .= '<colgroup>'; $content .= '<col style="width:15%" /><col style="width:15%" /><col style="width:50%" /><col style="width:20%" />'; $content .= '</colgroup>'; $content .= '<tbody>'; $content .= '<tr><th>' . $this->renderActiveHeaderContent() . '</th>'; $content .= '<th>' . Zurmo::t('ZurmoModule', 'Code') . '</th>'; $content .= '<th>' . Zurmo::t('ZurmoModule', 'Rate to') . ' ' . Yii::app()->currencyHelper->getBaseCode() . ' ' . $this->renderLastUpdatedHeaderContent() . '</th>'; $content .= '<th>' . Zurmo::t('Core', 'Remove') . '</th>'; $content .= '</tr>'; foreach ($this->currencies as $currency) { $route = $this->moduleId . '/' . $this->controllerId . '/delete/'; $content .= '<tr>'; $content .= '<td class="checkbox-column">' . self::renderActiveCheckBoxContent($form, $currency) . '</td>'; $content .= '<td>' . $currency->code . '</td>'; $content .= '<td>' . $currency->rateToBase . '</td>'; $content .= '<td>'; if (count($this->currencies) == 1 || CurrencyValue::isCurrencyInUseById($currency->id)) { $content .= Zurmo::t('ZurmoModule', 'Currency in use.'); } else { $content .= ZurmoHtml::link(Zurmo::t('Core', 'Remove'), Yii::app()->createUrl($route, array('id' => $currency->id)), array('class' => 'z-link')); } $content .= '</td>'; $content .= '</tr>'; } $content .= '</tbody>'; $content .= '</table>'; return $content; }
/** * @param string $tableAliasName * @return string */ protected function getAttributeClauseQueryStringExtraPart($tableAliasName) { assert('is_string($tableAliasName)'); if ($this->componentForm->isATypeOfCurrencyValue() && ($this->currencyConversionType == Report::CURRENCY_CONVERSION_TYPE_BASE || $this->currencyConversionType == Report::CURRENCY_CONVERSION_TYPE_SPOT)) { $quote = DatabaseCompatibilityUtil::getQuote(); $currencyValue = new CurrencyValue(); return " * {$quote}{$tableAliasName}{$quote}." . "{$quote}" . $currencyValue->getColumnNameByAttribute('rateToBase') . "{$quote}"; } }
/** * @depends testCreateStageValues */ public function testVariousCurrencyValues() { $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; $currencies = Currency::getAll(); $currencyValue = new CurrencyValue(); $currencyValue->value = 100; $currencyValue->currency = $currencies[0]; $this->assertEquals('USD', $currencyValue->currency->code); $opportunity = new Opportunity(); $opportunity->owner = $super; $opportunity->name = 'test'; $opportunity->amount = $currencyValue; $opportunity->closeDate = '2011-01-01'; $opportunity->stage->value = 'Verbal'; $this->assertEquals(0, $opportunity->probability); $saved = $opportunity->save(); $this->assertTrue($saved); $this->assertEquals(75, $opportunity->probability); $opportunity1Id = $opportunity->id; $opportunity->forget(); $currencyValue = new CurrencyValue(); $currencyValue->value = 800; $currencyValue->currency = $currencies[0]; $this->assertEquals('USD', $currencyValue->currency->code); $opportunity = new Opportunity(); $opportunity->owner = $super; $opportunity->name = 'test'; $opportunity->amount = $currencyValue; $opportunity->closeDate = '2011-01-01'; $opportunity->stage->value = 'Verbal'; $saved = $opportunity->save(); $this->assertTrue($saved); $opportunity2Id = $opportunity->id; $opportunity->forget(); $currencyValue->forget(); //need to forget this to pull the accurate value from the database $opportunity1 = Opportunity::getById($opportunity1Id); $this->assertEquals(100, $opportunity1->amount->value); $opportunity2 = Opportunity::getById($opportunity2Id); $this->assertEquals(800, $opportunity2->amount->value); $opportunity1->delete(); $opportunity2->delete(); }
/** * Delete a currency as long as it is not in use. */ public function actionDelete($id) { if (!CurrencyValue::isCurrencyInUseById(intval($id))) { $currency = Currency::GetById(intval($id)); $currency->delete(); } else { Yii::app()->user->setFlash('notification', Zurmo::t('ZurmoModule', 'The currency was not removed because it is in use.')); } $this->redirect(array($this->getId() . '/configurationList')); }
/** * @depends testConstructDerivedWithUserDefaultCurrency */ public function testIsCurrencyInUseByIdAndChangRateIfValueChangesOrCurrencyChanges() { $currencyHelper = Yii::app()->currencyHelper; $euro = Currency::getByCode('EUR'); $this->assertFalse(CurrencyValue::isCurrencyInUseById($euro->id)); $opportunity = new Opportunity(); $opportunity->name = 'Tyfe'; $opportunity->stage->value = 'Starting Up'; $opportunity->closeDate = '2008-10-05'; $opportunity->amount->value = 456.78; $opportunity->amount->currency = $euro; $this->assertTrue($opportunity->save()); $this->assertEquals(1.5, $opportunity->amount->rateToBase); $this->assertTrue(CurrencyValue::isCurrencyInUseById($euro->id)); //change the currency rate for the euro. $euro->rateToBase = 3; $this->assertTrue($euro->save()); //Now test saving the opportunity again but not changing the amount value. The rate should stay the same. $id = $opportunity->id; unset($opportunity); $opportunity = Opportunity::getById($id); $this->assertEquals(456.78, $opportunity->amount->value); $opportunity->amount->value = 456.78; $this->assertTrue($opportunity->save()); $this->assertEquals(456.78, $opportunity->amount->value); $this->assertEquals(1.5, $opportunity->amount->rateToBase); //Now change amount. the exchange rate should change. $opportunity->amount->value = 566.0; $this->assertTrue($opportunity->save()); $this->assertEquals(3, $opportunity->amount->rateToBase); //Now change the currency only. should change rate. $id = $opportunity->id; unset($opportunity); $opportunity = Opportunity::getById($id); $opportunity->amount->currency = Currency::getByCode('USD'); $this->assertTrue($opportunity->save()); $this->assertEquals(1, $opportunity->amount->rateToBase); }