void Money::checkCurrency(Money& ans, const Money& v1, const Money& v2) { if(v1.currency() == "" && v1.valueInCents() != 0){ throw WException("Payment::Money::checkCurrency " "money with no currency has value."); } if(v2.currency() == "" && v2.valueInCents() != 0){ throw WException("Payment::Money::checkCurrency " "money with no currency has value."); } if(v1.currency() == ""){ ans.setCurrency(v2.currency()); return; } if(v2.currency() == ""){ ans.setCurrency(v1.currency()); return; } if(v1.currency() != v2.currency()){ throw WException("Payment::Money::checkCurrency different currency"); } }
//------------------------------------------------------------------------------ void ConditionValueDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { switch (field(index)) { case AssignmentRule::Date: { QDateEdit* dateEditor = static_cast<QDateEdit*>(editor); QDate date = dateEditor->date(); model->setData(index, date.toString(Qt::ISODate), Qt::EditRole); } break; case AssignmentRule::Amount: { MoneyEdit* moneyEditor = static_cast<MoneyEdit*>(editor); Money amount = moneyEditor->value(); QString newVal = QString("%1,%2") .arg(amount.amount()).arg(amount.currency().code()); model->setData(index, newVal, Qt::EditRole); } break; default: { LineEdit* stringEditor = static_cast<LineEdit*>(editor); model->setData(index, stringEditor->text(), Qt::EditRole); } } }
Decimal operator/(const Money& m1, const Money& m2) { if (m1.currency() == m2.currency()) { return m1.value()/m2.value(); } else if (Money::conversionType == Money::BaseCurrencyConversion) { Money tmp1 = m1; convertToBase(tmp1); Money tmp2 = m2; convertToBase(tmp2); return tmp1/tmp2; } else if (Money::conversionType == Money::AutomatedConversion) { Money tmp = m2; convertTo(tmp, m1.currency()); return m1/tmp; } else { QL_FAIL("currency mismatch and no conversion specified"); } }
bool close_enough(const Money& m1, const Money& m2, Size n) { if (m1.currency() == m2.currency()) { return close_enough(m1.value(),m2.value(),n); } else if (Money::conversionType == Money::BaseCurrencyConversion) { Money tmp1 = m1; convertToBase(tmp1); Money tmp2 = m2; convertToBase(tmp2); return close_enough(tmp1,tmp2,n); } else if (Money::conversionType == Money::AutomatedConversion) { Money tmp = m2; convertTo(tmp, m1.currency()); return close_enough(m1,tmp,n); } else { QL_FAIL("currency mismatch and no conversion specified"); } }
Money ExchangeRate::exchange(const Money& amount) const { switch (type_) { case Direct: if (amount.currency() == source_) return Money(amount.value()*rate_, target_); else if (amount.currency() == target_) return Money(amount.value()/rate_, source_); else QL_FAIL("exchange rate not applicable"); case Derived: if (amount.currency() == rateChain_.first->source() || amount.currency() == rateChain_.first->target()) return rateChain_.second->exchange( rateChain_.first->exchange(amount)); else if (amount.currency() == rateChain_.second->source() || amount.currency() == rateChain_.second->target()) return rateChain_.first->exchange( rateChain_.second->exchange(amount)); else QL_FAIL("exchange rate not applicable"); default: QL_FAIL("unknown exchange-rate type"); } }