2021-02-08 11:24:14 +07:00
|
|
|
package com.cubetiqs.money
|
2020-08-27 10:48:31 +07:00
|
|
|
|
2021-02-08 16:08:00 +07:00
|
|
|
infix fun StdMoney.exchangeTo(currency: StdMoney.Currency): StdMoney {
|
2020-08-27 21:42:05 +07:00
|
|
|
return MoneyExchangeUtils.exchange(this, currency)
|
|
|
|
}
|
|
|
|
|
2021-02-08 16:08:00 +07:00
|
|
|
infix fun StdMoney.exchangeTo(currency: String): StdMoney = this exchangeTo object : StdMoney.Currency {
|
|
|
|
override fun getCurrency(): String {
|
|
|
|
return currency.toUpperCase().trim()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
infix fun StdMoney.plusWith(other: StdMoney): StdMoney = this + other
|
|
|
|
infix fun StdMoney.minusWith(other: StdMoney): StdMoney = this - other
|
|
|
|
infix fun StdMoney.divideWith(other: StdMoney): StdMoney = this / other
|
|
|
|
infix fun StdMoney.multiplyWith(other: StdMoney): StdMoney = this * other
|
|
|
|
|
|
|
|
infix fun StdMoney.plusOf(value: Number): StdMoney = object : StdMoney {
|
2021-02-08 21:34:17 +07:00
|
|
|
override fun getCurrency(): StdMoney.Currency {
|
|
|
|
return this@plusOf.getCurrency()
|
2021-02-08 16:08:00 +07:00
|
|
|
}
|
|
|
|
|
2021-02-08 21:34:17 +07:00
|
|
|
override fun getValue(): Double {
|
|
|
|
return this@plusOf.getValue() + value.toDouble()
|
2021-02-08 16:08:00 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
infix fun StdMoney.minusOf(value: Number): StdMoney = object : StdMoney {
|
2021-02-08 21:34:17 +07:00
|
|
|
override fun getCurrency(): StdMoney.Currency {
|
|
|
|
return this@minusOf.getCurrency()
|
2021-02-08 16:08:00 +07:00
|
|
|
}
|
|
|
|
|
2021-02-08 21:34:17 +07:00
|
|
|
override fun getValue(): Double {
|
|
|
|
return this@minusOf.getValue() - value.toDouble()
|
2021-02-08 16:08:00 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
infix fun StdMoney.divideOf(value: Number): StdMoney = object : StdMoney {
|
2021-02-08 21:34:17 +07:00
|
|
|
override fun getCurrency(): StdMoney.Currency {
|
|
|
|
return this@divideOf.getCurrency()
|
2021-02-08 16:08:00 +07:00
|
|
|
}
|
|
|
|
|
2021-02-08 21:34:17 +07:00
|
|
|
override fun getValue(): Double {
|
|
|
|
return this@divideOf.getValue() / value.toDouble()
|
2021-02-08 16:08:00 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
infix fun StdMoney.multiplyOf(value: Number): StdMoney = object : StdMoney {
|
2021-02-08 21:34:17 +07:00
|
|
|
override fun getCurrency(): StdMoney.Currency {
|
|
|
|
return this@multiplyOf.getCurrency()
|
2021-02-08 16:08:00 +07:00
|
|
|
}
|
|
|
|
|
2021-02-08 21:34:17 +07:00
|
|
|
override fun getValue(): Double {
|
|
|
|
return this@multiplyOf.getValue() * value.toDouble()
|
2021-02-08 16:08:00 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
infix fun Number.withCurrency(currency: StdMoney.Currency): StdMoney = object : StdMoney {
|
2021-02-08 21:34:17 +07:00
|
|
|
override fun getCurrency(): StdMoney.Currency {
|
2021-02-08 16:08:00 +07:00
|
|
|
return currency
|
|
|
|
}
|
|
|
|
|
2021-02-08 21:34:17 +07:00
|
|
|
override fun getValue(): Double {
|
2021-02-08 16:08:00 +07:00
|
|
|
return this@withCurrency.toDouble()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
infix fun Number.withCurrency(currency: String): StdMoney = this withCurrency object : StdMoney.Currency {
|
|
|
|
override fun getCurrency(): String {
|
|
|
|
return currency.toUpperCase().trim()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// toString function for StdMoney interface
|
2021-02-08 21:34:17 +07:00
|
|
|
fun StdMoney.asString(): String = "StdMoney(value=${getValue()}, currency=${getCurrency().getCurrency()})"
|
|
|
|
fun StdMoney.asMoneyString(): String = "${getValue()}:${getCurrency().getCurrency()}"
|
2021-02-08 16:08:00 +07:00
|
|
|
fun String?.fromStringToMoney(): StdMoney {
|
|
|
|
val values = this?.split(":")
|
|
|
|
if (values.isNullOrEmpty()) {
|
|
|
|
return StdMoney.ZERO
|
|
|
|
}
|
|
|
|
|
|
|
|
val currency = StdMoney.initCurrency(values.firstOrNull())
|
|
|
|
val value = values.lastOrNull()?.toDouble() ?: 0.0
|
|
|
|
|
|
|
|
return object : StdMoney {
|
2021-02-08 21:34:17 +07:00
|
|
|
override fun getCurrency(): StdMoney.Currency {
|
2021-02-08 16:08:00 +07:00
|
|
|
return currency
|
|
|
|
}
|
|
|
|
|
2021-02-08 21:34:17 +07:00
|
|
|
override fun getValue(): Double {
|
2021-02-08 16:08:00 +07:00
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 13:09:54 +07:00
|
|
|
fun StdMoney.isMatchedCurrency(currency: StdMoney.Currency) =
|
2021-02-08 21:34:17 +07:00
|
|
|
this.getCurrency().getCurrency().equals(currency.getCurrency(), ignoreCase = true)
|
2021-02-08 13:09:54 +07:00
|
|
|
|
|
|
|
inline fun buildMoneyConfigProperties(
|
|
|
|
builderAction: MoneyConfig.MoneyConfigProperties.MoneyConfigPropertiesBuilder.() -> Unit
|
|
|
|
): MoneyConfig.MoneyConfigProperties {
|
|
|
|
return MoneyConfig
|
|
|
|
.builder().apply(builderAction)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
inline fun applyMoneyConfig(
|
|
|
|
builderAction: MoneyConfig.() -> Unit,
|
|
|
|
) {
|
|
|
|
MoneyConfig.apply(builderAction)
|
|
|
|
}
|