Uecko_ERP/apps/server/src/common/domain/value-objects/money-value.ts

217 lines
6.1 KiB
TypeScript
Raw Normal View History

2025-02-24 19:00:28 +00:00
import { Result } from "@common/helpers";
import DineroFactory, { Currency, Dinero } from "dinero.js";
2025-02-25 15:25:30 +00:00
import { Percentage } from "./percentage";
import { Quantity } from "./quantity";
2025-02-24 19:00:28 +00:00
import { ValueObject } from "./value-object";
const DEFAULT_SCALE = 2;
2025-04-01 14:26:15 +00:00
const DEFAULT_CURRENCY_CODE = "EUR";
2025-02-24 19:00:28 +00:00
type CurrencyData = Currency;
2025-02-25 10:16:34 +00:00
export type RoundingMode =
| "HALF_ODD"
| "HALF_EVEN"
| "HALF_UP"
| "HALF_DOWN"
| "HALF_TOWARDS_ZERO"
| "HALF_AWAY_FROM_ZERO"
| "DOWN";
2025-02-24 19:00:28 +00:00
interface IMoneyValueProps {
amount: number;
2025-04-01 14:26:15 +00:00
scale?: number;
currency_code?: string;
2025-02-24 19:00:28 +00:00
}
interface IMoneyValue {
amount: number;
scale: number;
currency: Dinero.Currency;
getValue(): IMoneyValueProps;
convertScale(newScale: number): MoneyValue;
add(addend: MoneyValue): MoneyValue;
subtract(subtrahend: MoneyValue): MoneyValue;
2025-02-25 15:25:30 +00:00
multiply(multiplier: number | Quantity, roundingMode?: RoundingMode): MoneyValue;
divide(divisor: number, roundingMode?: RoundingMode): MoneyValue;
percentage(percentage: number, roundingMode?: RoundingMode): MoneyValue;
2025-02-24 19:00:28 +00:00
equalsTo(comparator: MoneyValue): boolean;
greaterThan(comparator: MoneyValue): boolean;
lessThan(comparator: MoneyValue): boolean;
isZero(): boolean;
isPositive(): boolean;
isNegative(): boolean;
2025-02-25 15:25:30 +00:00
hasSameCurrency(comparator: MoneyValue): boolean;
hasSameAmount(comparator: MoneyValue): boolean;
2025-02-24 19:00:28 +00:00
format(locale: string): string;
}
export class MoneyValue extends ValueObject<IMoneyValueProps> implements IMoneyValue {
2025-02-25 10:16:34 +00:00
private readonly dinero: Dinero;
2025-02-24 19:00:28 +00:00
static create(props: IMoneyValueProps) {
return Result.ok(new MoneyValue(props));
}
constructor(props: IMoneyValueProps) {
super(props);
const { amount, scale, currency_code } = props;
2025-02-25 10:16:34 +00:00
this.dinero = Object.freeze(
2025-02-24 19:00:28 +00:00
DineroFactory({
amount,
2025-04-01 14:26:15 +00:00
precision: scale || DEFAULT_SCALE,
currency: (currency_code as Currency) || DEFAULT_CURRENCY_CODE,
2025-02-24 19:00:28 +00:00
})
); // 🔒 Garantiza inmutabilidad
}
get amount(): number {
2025-02-25 10:16:34 +00:00
return this.dinero.getAmount() / Math.pow(10, this.dinero.getPrecision());
2025-02-24 19:00:28 +00:00
}
get currency(): CurrencyData {
2025-02-25 10:16:34 +00:00
return this.dinero.getCurrency();
2025-02-24 19:00:28 +00:00
}
get scale(): number {
2025-02-25 10:16:34 +00:00
return this.dinero.getPrecision();
2025-02-24 19:00:28 +00:00
}
getValue(): IMoneyValueProps {
return this.props;
}
2025-04-01 14:26:15 +00:00
/** Serializa el VO a una cadena del tipo "EUR:123400:2" */
toPersistence(): string {
return `${this.currency()}:${this.value.getAmount()}:${this.getScale()}`;
}
/** Reconstruye el VO desde la cadena persistida */
static fromPersistence(value: string): MoneyValue {
const [currencyCode, amountStr, scaleStr] = value.split(":");
const amount = parseInt(amountStr, 10);
const scale = parseInt(scaleStr, 10);
const currency = getCurrencyByCode(currencyCode);
return new MoneyValue(amount, scale, currency);
}
toPrimitive() {
return {
amount: this.amount,
scale: this.scale,
currency_code: this.currency,
};
}
2025-02-25 10:16:34 +00:00
convertScale(newScale: number, roundingMode: RoundingMode = "HALF_UP"): MoneyValue {
const _newDinero = this.dinero.convertPrecision(newScale, roundingMode);
2025-02-24 19:00:28 +00:00
return new MoneyValue({
2025-02-25 10:16:34 +00:00
amount: _newDinero.getAmount(),
scale: _newDinero.getPrecision(),
currency_code: _newDinero.getCurrency(),
2025-02-24 19:00:28 +00:00
});
}
add(addend: MoneyValue): MoneyValue {
return new MoneyValue({
2025-02-25 10:16:34 +00:00
amount: this.dinero.add(addend.dinero).getAmount(),
2025-02-24 19:00:28 +00:00
scale: this.scale,
currency_code: this.currency,
});
}
subtract(subtrahend: MoneyValue): MoneyValue {
return new MoneyValue({
2025-02-25 10:16:34 +00:00
amount: this.dinero.subtract(subtrahend.dinero).getAmount(),
2025-02-24 19:00:28 +00:00
scale: this.scale,
currency_code: this.currency,
});
}
2025-02-25 15:25:30 +00:00
multiply(multiplier: number | Quantity, roundingMode?: RoundingMode): MoneyValue {
const _multiplier = typeof multiplier === "number" ? multiplier : multiplier.toNumber();
const _newDinero = this.dinero.multiply(_multiplier, roundingMode);
2025-02-24 19:00:28 +00:00
return new MoneyValue({
2025-02-25 15:25:30 +00:00
amount: _newDinero.getAmount(),
scale: _newDinero.getPrecision(),
currency_code: _newDinero.getCurrency(),
2025-02-24 19:00:28 +00:00
});
}
2025-02-25 15:25:30 +00:00
divide(divisor: number | Quantity, roundingMode?: RoundingMode): MoneyValue {
const _divisor = typeof divisor === "number" ? divisor : divisor.toNumber();
const _newDinero = this.dinero.divide(_divisor, roundingMode);
2025-02-24 19:00:28 +00:00
return new MoneyValue({
2025-02-25 15:25:30 +00:00
amount: _newDinero.getAmount(),
scale: _newDinero.getPrecision(),
currency_code: _newDinero.getCurrency(),
});
}
percentage(percentage: number | Percentage, roundingMode?: RoundingMode): MoneyValue {
const _percentage = typeof percentage === "number" ? percentage : percentage.toNumber();
const _newDinero = this.dinero.percentage(_percentage, roundingMode);
return new MoneyValue({
amount: _newDinero.getAmount(),
scale: _newDinero.getPrecision(),
currency_code: _newDinero.getCurrency(),
2025-02-24 19:00:28 +00:00
});
}
equalsTo(comparator: MoneyValue): boolean {
2025-02-25 10:16:34 +00:00
return this.dinero.equalsTo(comparator.dinero);
2025-02-24 19:00:28 +00:00
}
greaterThan(comparator: MoneyValue): boolean {
2025-02-25 10:16:34 +00:00
return this.dinero.greaterThan(comparator.dinero);
2025-02-24 19:00:28 +00:00
}
lessThan(comparator: MoneyValue): boolean {
2025-02-25 10:16:34 +00:00
return this.dinero.lessThan(comparator.dinero);
2025-02-24 19:00:28 +00:00
}
isZero(): boolean {
2025-02-25 10:16:34 +00:00
return this.dinero.isZero();
2025-02-24 19:00:28 +00:00
}
isPositive(): boolean {
return this.amount > 0;
}
isNegative(): boolean {
return this.amount < 0;
}
2025-02-25 15:25:30 +00:00
hasSameCurrency(comparator: MoneyValue): boolean {
return this.dinero.hasSameCurrency(comparator.dinero);
}
hasSameAmount(comparator: MoneyValue): boolean {
return this.dinero.hasSameAmount(comparator.dinero);
}
2025-04-01 14:26:15 +00:00
/**
* Devuelve una cadena con el importe formateado.
* Ejemplo: 123456 -> 1,234.56
* @param locale Código de idioma y país (ej. "es-ES")
* @returns Importe formateado
*/
2025-02-24 19:00:28 +00:00
format(locale: string): string {
const amount = this.amount;
const currency = this.currency;
const scale = this.scale;
return new Intl.NumberFormat(locale, {
style: "currency",
currency: currency,
minimumFractionDigits: scale,
maximumFractionDigits: scale,
useGrouping: true,
}).format(amount);
}
}