class CartItemTaxes: """Adapter which provides ITaxes for cart item content objects. """ implements(ITaxes) adapts(ICartItem) def __init__(self, context): """ """ self.context = context # adapt Product to Taxes self.taxes = ITaxes(self.context.getProduct()) def getTax(self): """Returns absolute tax. """ price = IPrices(self.context).getPriceGross(with_discount=True) tax_rate = self.taxes.getTaxRate() tax = (tax_rate / (tax_rate + 100)) * price return tax def getTaxForCustomer(self): """Returns absolute tax for customer. """ price = IPrices(self.context).getPriceGross(with_discount=True) tax_rate = self.taxes.getTaxRateForCustomer() tax = (tax_rate / (tax_rate + 100)) * price return tax def getTaxRate(self): """Returns tax rate """ tax_rate = self.taxes.getTaxRate() return tax_rate def getTaxRateForCustomer(self): """Returns tax rate for a customer. """ tax = self.taxes.getTaxRateForCustomer() * self.context.getAmount() return tax
class CartItemTaxes: """Adapter which provides ITaxes for cart item content objects. """ implements(ITaxes) adapts(ICartItem) def __init__(self, context): """ """ self.context = context # adapt Product to Taxes self.taxes = ITaxes(self.context.getProduct()) def getTax(self): """Returns absolute tax. """ price = IPrices(self.context).getPriceGross(with_discount=True) tax_rate = self.taxes.getTaxRate() tax = (tax_rate/(tax_rate+100)) * price return tax def getTaxForCustomer(self): """Returns absolute tax for customer. """ price = IPrices(self.context).getPriceGross(with_discount=True) tax_rate = self.taxes.getTaxRateForCustomer() tax = (tax_rate/(tax_rate+100)) * price return tax def getTaxRate(self): """Returns tax rate """ tax_rate = self.taxes.getTaxRate() return tax_rate def getTaxRateForCustomer(self): """Returns tax rate for a customer. """ tax = self.taxes.getTaxRateForCustomer() * self.context.getAmount() return tax
def _addItemFromCartItem(self, id, cart_item): """Sets the item by given cart item. """ self.context.manage_addProduct["easyshop.core"].addOrderItem(id=str(id)) new_item = getattr(self.context, str(id)) # set product quantity new_item.setProductQuantity(cart_item.getAmount()) # Set product prices & taxes product_taxes = ITaxes(cart_item.getProduct()) product_prices = IPrices(cart_item.getProduct()) item_prices = IPrices(cart_item) item_taxes = ITaxes(cart_item) new_item.setTaxRate(product_taxes.getTaxRateForCustomer()) new_item.setProductTax(product_taxes.getTaxForCustomer()) new_item.setProductPriceGross(product_prices.getPriceForCustomer()) new_item.setProductPriceNet(product_prices.getPriceNet()) # Set item prices & taxes new_item.setTax(item_taxes.getTaxForCustomer()) new_item.setPriceGross(item_prices.getPriceForCustomer()) new_item.setPriceNet(item_prices.getPriceNet()) # Discount discount = IDiscountsCalculation(cart_item).getDiscount() if discount is not None: new_item.setDiscountDescription(discount.Title()) dp = getMultiAdapter((discount, cart_item)) new_item.setDiscountGross(dp.getPriceForCustomer()) new_item.setDiscountNet(dp.getPriceNet()) # Set product product = cart_item.getProduct() new_item.setProduct(product) # Set product name and id data = IData(product).asDict() new_item.setProductTitle(data["title"]) new_item.setArticleId(data["article_id"]) # Set properties properties = [] pm = IPropertyManagement(product) for selected_property in cart_item.getProperties(): # Get the price property_price = pm.getPriceForCustomer( selected_property["id"], selected_property["selected_option"]) # By default we save the titles of the properties and selected # options In this way they are kept if the title of a property or # option will be changed after the product has been bought. titles = getTitlesByIds( product, selected_property["id"], selected_property["selected_option"]) # If we don't find the property or option we ignore the property. # This can only happen if the property has been deleted after a # product has been added to the cart. In this case we don't want the # property at all (I think). if titles is None: continue properties.append({ "title" : titles["property"], "selected_option" : titles["option"], "price" : str(property_price), }) new_item.setProperties(properties)
def testGetTaxRateForCustomer(self): """ """ t = ITaxes(self.shop.products.product_1) self.assertEqual(t.getTaxRateForCustomer(), 10.0)
def _addItemFromCartItem(self, id, cart_item): """Sets the item by given cart item. """ self.context.manage_addProduct["easyshop.core"].addOrderItem( id=str(id)) new_item = getattr(self.context, str(id)) # set product quantity new_item.setProductQuantity(cart_item.getAmount()) # Set product prices & taxes product_taxes = ITaxes(cart_item.getProduct()) product_prices = IPrices(cart_item.getProduct()) item_prices = IPrices(cart_item) item_taxes = ITaxes(cart_item) new_item.setTaxRate(product_taxes.getTaxRateForCustomer()) new_item.setProductTax(product_taxes.getTaxForCustomer()) new_item.setProductPriceGross(product_prices.getPriceForCustomer()) new_item.setProductPriceNet(product_prices.getPriceNet()) # Set item prices & taxes new_item.setTax(item_taxes.getTaxForCustomer()) new_item.setPriceGross(item_prices.getPriceForCustomer()) new_item.setPriceNet(item_prices.getPriceNet()) # Discount discount = IDiscountsCalculation(cart_item).getDiscount() if discount is not None: new_item.setDiscountDescription(discount.Title()) dp = getMultiAdapter((discount, cart_item)) new_item.setDiscountGross(dp.getPriceForCustomer()) new_item.setDiscountNet(dp.getPriceNet()) # Set product product = cart_item.getProduct() new_item.setProduct(product) # Set product name and id data = IData(product).asDict() new_item.setProductTitle(data["title"]) new_item.setArticleId(data["article_id"]) # Set properties properties = [] pm = IPropertyManagement(product) for selected_property in cart_item.getProperties(): # Get the price property_price = pm.getPriceForCustomer( selected_property["id"], selected_property["selected_option"]) # By default we save the titles of the properties and selected # options In this way they are kept if the title of a property or # option will be changed after the product has been bought. titles = getTitlesByIds(product, selected_property["id"], selected_property["selected_option"]) # If we don't find the property or option we ignore the property. # This can only happen if the property has been deleted after a # product has been added to the cart. In this case we don't want the # property at all (I think). if titles is None: continue properties.append({ "title": titles["property"], "selected_option": titles["option"], "price": str(property_price), }) new_item.setProperties(properties)
class DiscountPrices: """Multia adapter which provides IPrices for discount content objects and product. """ implements(IPrices) adapts(IDiscount, ICartItem) # NOTE: We need the product additionally as we need to calculate taxes, as # the discount has the same tax rate as the related product. def __init__(self, discount, cart_item): """ """ self.discount = discount self.cart_item = cart_item self.product = cart_item.getProduct() self.taxes = ITaxes(self.product) self.shop = IShopManagement(self.product).getShop() def getPriceForCustomer(self): """ """ # If the discount is by percentage, we don't have to calc the tax for # the discount, because the discount is a part of the already calculated # price, hence we can do it here. if self.discount.getType() == "percentage": price = IPrices(self.cart_item).getPriceForCustomer() return price * (self.discount.getValue() / 100) else: tax_rate_for_customer = self.taxes.getTaxRateForCustomer() price_net = self.getPriceNet() # We take the net price and add the customer specific taxes. return price_net * ((tax_rate_for_customer + 100) / 100) def getPriceGross(self): """ """ if self.discount.getType() == "percentage": price = IPrices(self.cart_item).getPriceGross() return price * (self.discount.getValue() / 100) else: tax_rate = self.taxes.getTaxRate() price = self._calcTotalPrice() # The price entered is considered as gross price, so we simply # return it. if self.shop.getGrossPrices() == True: return price # The price entered is considered as net price. So we have to # calculate the gross price first. else: return price * ((tax_rate + 100) / 100) def getPriceNet(self): """ """ if self.discount.getType() == "percentage": price = IPrices(self.cart_item).getPriceNet() return price * (self.discount.getValue() / 100) else: tax_rate = self.taxes.getTaxRate() price = self._calcTotalPrice() # The price entered is considered as gross price. So we have to # calculate the net price first. if self.shop.getGrossPrices() == True: return price * (100 / (tax_rate + 100)) # The price entered is considered as net price, so we simply return # it. else: return price def _calcTotalPrice(self): """ """ if self.discount.getBase() == "cart_item": return self.discount.getValue() else: return self.discount.getValue() * self.cart_item.getAmount()
class DiscountPrices: """Multia adapter which provides IPrices for discount content objects and product. """ implements(IPrices) adapts(IDiscount, ICartItem) # NOTE: We need the product additionally as we need to calculate taxes, as # the discount has the same tax rate as the related product. def __init__(self, discount, cart_item): """ """ self.discount = discount self.cart_item = cart_item self.product = cart_item.getProduct() self.taxes = ITaxes(self.product) self.shop = IShopManagement(self.product).getShop() def getPriceForCustomer(self): """ """ # If the discount is by percentage, we don't have to calc the tax for # the discount, because the discount is a part of the already calculated # price, hence we can do it here. if self.discount.getType() == "percentage": price = IPrices(self.cart_item).getPriceForCustomer() return price * (self.discount.getValue() / 100) else: tax_rate_for_customer = self.taxes.getTaxRateForCustomer() price_net = self.getPriceNet() # We take the net price and add the customer specific taxes. return price_net * ((tax_rate_for_customer+100)/100) def getPriceGross(self): """ """ if self.discount.getType() == "percentage": price = IPrices(self.cart_item).getPriceGross() return price * (self.discount.getValue() / 100) else: tax_rate = self.taxes.getTaxRate() price = self._calcTotalPrice() # The price entered is considered as gross price, so we simply # return it. if self.shop.getGrossPrices() == True: return price # The price entered is considered as net price. So we have to # calculate the gross price first. else: return price * ((tax_rate+100)/100) def getPriceNet(self): """ """ if self.discount.getType() == "percentage": price = IPrices(self.cart_item).getPriceNet() return price * (self.discount.getValue() / 100) else: tax_rate = self.taxes.getTaxRate() price = self._calcTotalPrice() # The price entered is considered as gross price. So we have to # calculate the net price first. if self.shop.getGrossPrices() == True: return price * (100/(tax_rate+100)) # The price entered is considered as net price, so we simply return # it. else: return price def _calcTotalPrice(self): """ """ if self.discount.getBase() == "cart_item": return self.discount.getValue() else: return self.discount.getValue() * self.cart_item.getAmount()