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

188 lines
5.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;
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;
scale: number;
currency_code: string;
}
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,
precision: scale,
currency: currency_code as Currency,
})
); // 🔒 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-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-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);
}
}