Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
lib-py-b2b / discount.py
Size: Mime:
from decimal import Decimal
from enum import Enum

from lib_b2b.persistent import Persistable


class DiscountType(Enum):
    FIXED_AMOUNT = "fixed_amount"
    PERCENTAGE = "percentage"
    SHIPPING = "shipping"


class Discount(Persistable):

    def __init__(self, amount: Decimal, code: str, discount_type: DiscountType, value: Decimal):
        self.amount = amount
        self.value = value
        self.code = code
        self.discount_type = discount_type

    @staticmethod
    def from_dict(data: dict):
        return Discount(
            amount=Decimal(str(data['amount'])),
            code=data['code'],
            value=Decimal(str(data['value'])),
            discount_type=DiscountType(data.get('discount_type', data.get('type')))
        )

    def as_dict(self) -> dict:
        return {
            'amount': self.amount,
            'value': self.value,
            'code': self.code,
            'discount_type': self.discount_type.value
        }