diff --git a/correios/models/posting.py b/correios/models/posting.py index 4084e92..d5fb939 100644 --- a/correios/models/posting.py +++ b/correios/models/posting.py @@ -44,7 +44,6 @@ MAX_CYLINDER_SIZE = 28 INSURANCE_VALUE_THRESHOLD = Decimal("50.00") # R$ INSURANCE_PERCENTUAL_COST = Decimal("0.007") # 0.7% -MONEY_QUANTIZATION = Decimal("0.00") class EventStatus: @@ -361,7 +360,7 @@ def calculate_insurance(cls, per_unit_value = Decimal(per_unit_value) if Service.get(service) == Service.get(SERVICE_PAC) and per_unit_value > INSURANCE_VALUE_THRESHOLD: value = (per_unit_value - INSURANCE_VALUE_THRESHOLD) * INSURANCE_PERCENTUAL_COST - return Decimal(value * quantity).quantize(MONEY_QUANTIZATION) + return to_decimal(value * quantity) @classmethod def validate(cls, @@ -695,10 +694,6 @@ def __init__(self, service: Union[Service, int], error_code: Union[str, int], error_message: str) -> None: - super().__init__( - service=service, - value=Decimal("0.00"), - delivery_time=timedelta(days=0), - ) + super().__init__(service, 0, Decimal("0.00")) self.error_code = int(error_code) self.error_message = error_message diff --git a/correios/utils.py b/correios/utils.py index 10b7657..9fbcedf 100644 --- a/correios/utils.py +++ b/correios/utils.py @@ -69,13 +69,15 @@ def to_datetime(date: Union[datetime, str], fmt="%Y-%m-%d %H:%M:%S%z") -> dateti return date -def to_decimal(value: Union[str, float], precision=2): - value = rreplace(str(value), ",", ".", 1) - if "." in value: - real, imag = value.rsplit(".", 1) - else: - real, imag = value, "0" - real = re.sub("[,._]", "", real) +def to_decimal(value: Union[Decimal, str, float], precision=2): + if not isinstance(value, Decimal): + value = rreplace(str(value), ",", ".", 1) + if "." in value: + real, imag = value.rsplit(".", 1) + else: + real, imag = value, "0" + real = re.sub("[,._]", "", real) + value = Decimal("{}.{}".format(real, imag)) quantize = Decimal("0." + "0" * precision) - return Decimal("{}.{}".format(real, imag)).quantize(quantize) + return value.quantize(quantize)