22import json
33import typing as t # noqa
44
5+ from viur import toolkit
56from viur .core import current , db , utils
67from viur .core .skeleton import SkeletonInstance
7-
8- from viur import toolkit
98from .enums import ApplicationDomain , ConditionOperator , DiscountType
109from ..globals import SHOP_INSTANCE , SHOP_LOGGER
1110from ..types import ConfigurationError
1514
1615logger = SHOP_LOGGER .getChild (__name__ )
1716
18-
1917# TODO: Use decimal package instead of floats?
2018# -> decimal mode in NumericBone?
2119
20+ PRICE_PRECISION : t .Final [int ] = 2
21+ """Precision, how many digits are used to round prices"""
22+
23+
2224class Price :
2325 cart_discounts : list [SkeletonInstance ] = []
2426 article_discount : SkeletonInstance = None
@@ -60,30 +62,30 @@ def retail(self) -> float:
6062
6163 @property
6264 def retail_net (self ) -> float :
63- return toolkit .round_decimal (self .gross_to_net (self .retail , self .vat_rate_percentage ), 2 )
65+ return toolkit .round_decimal (self .gross_to_net (self .retail , self .vat_rate_percentage ), PRICE_PRECISION )
6466
6567 @property
6668 def recommended (self ) -> float :
6769 return self .article_skel ["shop_price_recommended" ]
6870
6971 @property
7072 def recommended_net (self ) -> float :
71- return toolkit .round_decimal (self .gross_to_net (self .recommended , self .vat_rate_percentage ), 2 )
73+ return toolkit .round_decimal (self .gross_to_net (self .recommended , self .vat_rate_percentage ), PRICE_PRECISION )
7274
7375 @property
7476 def saved (self ) -> float :
7577 if self .retail is None or self .current is None :
7678 return 0
77- return toolkit .round_decimal (self .retail - self .current , 2 )
79+ return toolkit .round_decimal (self .retail - self .current , PRICE_PRECISION )
7880
7981 @property
8082 def saved_net (self ) -> float :
81- return toolkit .round_decimal (self .gross_to_net (self .saved , self .vat_rate_percentage ), 2 )
83+ return toolkit .round_decimal (self .gross_to_net (self .saved , self .vat_rate_percentage ), PRICE_PRECISION )
8284
8385 @property
8486 def saved_percentage (self ) -> float :
8587 try :
86- return toolkit .round_decimal (self .saved / self .current , 2 or 4 ) # TODO
88+ return toolkit .round_decimal (self .saved / self .current , PRICE_PRECISION )
8789 except (ZeroDivisionError , TypeError ): # One value is None
8890 return 0.0
8991
@@ -92,21 +94,21 @@ def saved_percentage(self) -> float:
9294 def current (self ) -> float :
9395 if (not self .is_in_cart or not self .cart_discounts ) and self .article_discount :
9496 # only the article_discount is applicable
95- return toolkit .round_decimal (self .apply_discount (self .article_discount , self .retail ), 2 )
97+ return toolkit .round_decimal (self .apply_discount (self .article_discount , self .retail ), PRICE_PRECISION )
9698 if self .is_in_cart and self .cart_discounts :
9799 # TODO: if self.article_discount:
98100 best_price , best_discounts = self .choose_best_discount_set ()
99- return toolkit .round_decimal (best_price , 2 )
101+ return toolkit .round_decimal (best_price , PRICE_PRECISION )
100102 return self .retail
101103
102104 @property
103105 def current_net (self ) -> float :
104- return toolkit .round_decimal (self .gross_to_net (self .current , self .vat_rate_percentage ), 2 )
106+ return toolkit .round_decimal (self .gross_to_net (self .current , self .vat_rate_percentage ), PRICE_PRECISION )
105107
106108 def shop_current_discount (self , article_skel : SkeletonInstance ) -> None | tuple [float , "SkeletonInstance" ]:
107109 """Best permanent discount campaign for article"""
108110 best_discount = None
109- article_price = self .retail or 0.0 # FIXME: how to handle None prices?
111+ article_price = self .retail or 0.0 # FIXME(discuss) : how to handle None prices?
110112 if not article_price :
111113 return None
112114 discount_module : "Discount" = SHOP_INSTANCE .get ().discount
@@ -186,7 +188,7 @@ def vat_rate_percentage(self) -> float:
186188 def vat_included (self ) -> float :
187189 """Calculate the included vat value based on current price and vat rate"""
188190 try :
189- return toolkit .round_decimal (self .gross_to_vat (self .current , self .vat_rate_percentage ), 2 )
191+ return toolkit .round_decimal (self .gross_to_vat (self .current , self .vat_rate_percentage ), PRICE_PRECISION )
190192 except TypeError : # One value is None
191193 return 0.0
192194
0 commit comments