From d4665a6de6e9e4020a72624029a729258c696cee Mon Sep 17 00:00:00 2001 From: David Arranz Date: Tue, 16 Jul 2024 18:05:52 +0200 Subject: [PATCH] . --- .../common/domain/entities/Percentage.ts | 48 +++++++++++++++++++ .../common/domain/entities/Quantity.ts | 45 +---------------- 2 files changed, 49 insertions(+), 44 deletions(-) diff --git a/shared/lib/contexts/common/domain/entities/Percentage.ts b/shared/lib/contexts/common/domain/entities/Percentage.ts index 1969b5a..cc88f50 100644 --- a/shared/lib/contexts/common/domain/entities/Percentage.ts +++ b/shared/lib/contexts/common/domain/entities/Percentage.ts @@ -121,6 +121,54 @@ export class Percentage extends NullableValueObject { return Result.ok(new this(_props, isNull(_amount), options)); } + public static createFromFormattedValue( + value: NullOr, + _options: IPercentageOptions = {} + ) { + if (value === null || value === "") { + return Percentage.create({ + amount: null, + scale: Percentage.DEFAULT_SCALE, + }); + } + + const valueStr = String(value); + const [integerPart, decimalPart] = valueStr.split(","); + + let _amount = integerPart; + let _scale = 2; + + if (decimalPart === undefined) { + // 99 + _scale = 0; + } else { + if (decimalPart === "") { + // 99, + _amount = integerPart + decimalPart.padEnd(1, "0"); + _scale = 1; + } + if (decimalPart.length === 1) { + // 99,1 + _amount = integerPart + decimalPart.padEnd(1, "0"); + _scale = 1; + } else { + if (decimalPart.length === 2) { + // 99,12 + _amount = integerPart + decimalPart.padEnd(2, "0"); + _scale = 2; + } + } + } + + return Percentage.create( + { + amount: _amount, + scale: _scale, + }, + _options + ); + } + private static _sanitize(value: NullOr): NullOr { let _value: NullOr = null; diff --git a/shared/lib/contexts/common/domain/entities/Quantity.ts b/shared/lib/contexts/common/domain/entities/Quantity.ts index cfd126f..1ef3552 100644 --- a/shared/lib/contexts/common/domain/entities/Quantity.ts +++ b/shared/lib/contexts/common/domain/entities/Quantity.ts @@ -116,8 +116,6 @@ export class Quantity extends NullableValueObject { scale, }; - console.log(_props); - return Result.ok(new Quantity(_props, _amount === null, options)); } @@ -135,8 +133,6 @@ export class Quantity extends NullableValueObject { const valueStr = String(value); const [integerPart, decimalPart] = valueStr.split(","); - console.log(integerPart, decimalPart); - let _amount = integerPart; let _scale = 2; @@ -162,11 +158,6 @@ export class Quantity extends NullableValueObject { } } - console.log({ - amount: _amount, - scale: _scale, - }); - return Quantity.create( { amount: _amount, @@ -174,35 +165,6 @@ export class Quantity extends NullableValueObject { }, _options ); - - /*const _amountArray = String(value).split(",") ?? []; - - let _amount = _amountArray.join(""); - let _scale = 0; - - if (_amountArray.length === 2) { - // Hay parte decimal, rellena o no -> 1,34 / 14,3 / 99, - _scale = 2; - - if (_amountArray[1].length === 0) { - // 99, - _amount = `${_amount}00`; - } - - if (_amountArray[1].length === 1) { - // 99, - _amount = `${_amount}0`; - } - } else { - _scale = 0; - } - - const _params = { - amount: _amount, - scale: _scale, - }; - - return Quantity.create(_params, _options);*/ } private static _sanitize(value: NullOr): NullOr { @@ -224,11 +186,7 @@ export class Quantity extends NullableValueObject { const factor = Math.pow(10, scale); const amount = Number(value) / factor; - const result = amount.toFixed(scale); - - console.log({ result }); - - return result; + return amount.toFixed(scale); } constructor(quantity: IQuantity, isNull: boolean, options: IQuantityOptions) { @@ -262,7 +220,6 @@ export class Quantity extends NullableValueObject { }; public toString(): string { - console.log("paso"); return Quantity._toString(this.amount, this.scale); }