def get_payment_costs(request, payment_method): """ Returns the payment price and tax for the given request. """ if payment_method is None: return { "price": 0.0, "tax": 0.0 } try: tax_rate = payment_method.tax.rate except AttributeError: tax_rate = 0.0 price = criteria_utils.get_first_valid(request, payment_method.prices.all()) # TODO: this assumes that payment price is given as gross price, we have to add payment processor here if price is None: price = payment_method.price tax = (tax_rate / (tax_rate + 100)) * price return { "price": price, "tax": tax } else: tax = (tax_rate / (tax_rate + 100)) * price.price return { "price": price.price, "tax": tax }
def get_shipping_costs(request, shipping_method): """Returns a dictionary with the shipping price and tax for the passed request and shipping method. The format of the dictionary is: {"price" : 0.0, "tax" : 0.0} """ if shipping_method is None: return { "price": 0.0, "tax": 0.0 } try: tax_rate = shipping_method.tax.rate except AttributeError: tax_rate = 0.0 price = criteria_utils.get_first_valid(request, shipping_method.prices.all()) if price is None: price = shipping_method.price tax = (tax_rate / (tax_rate + 100)) * price return { "price": price, "tax": tax } else: tax = (tax_rate / (tax_rate + 100)) * price.price return { "price": price.price, "tax": tax }
def get_payment_costs(request, payment_method): """ Returns the payment price and tax for the given request. """ if payment_method is None: return { "price": 0.0, "tax": 0.0 } try: tax_rate = payment_method.tax.rate except AttributeError: tax_rate = 0.0 price = criteria_utils.get_first_valid(request, payment_method.prices.all()) if price is None: price = payment_method.price tax = (tax_rate / (tax_rate + 100)) * price return { "price": price, "tax": tax } else: tax = (tax_rate / (tax_rate + 100)) * price.price return { "price": price.price, "tax": tax }
def get_default_shipping_method(request): """Returns the default shipping method for the passed request. At the moment is this the first valid shipping method, but this could be made more explicit in future. """ active_shipping_methods = ShippingMethod.objects.filter(active=True) return criteria_utils.get_first_valid(request, active_shipping_methods)
def get_customer_tax_rate(request, product): """Returns the specfic customer tax for the current customer and product. """ customer_tax = get_first_valid(request, CustomerTax.objects.all(), product) if customer_tax: return customer_tax.rate else: return _calc_product_tax_rate(request, product)
def get_price(self): """ Returns the stored price without any calculations. """ from lfs.criteria import utils as criteria_utils price = criteria_utils.get_first_valid( self.request, self.shipping_method.prices.all()) if price: return price.price return self.shipping_method.price
def get_tax_rate(self): from lfs.criteria.utils import get_first_valid from lfs.customer_tax.models import CustomerTax customer_tax = get_first_valid(self.request, CustomerTax.objects.all(), self.shipping_method) if customer_tax: return customer_tax.rate if self.shipping_method.tax is None: return 0 return self.shipping_method.tax.rate
def get_price(self): """ Returns the stored price without any calculations. """ from lfs.criteria import utils as criteria_utils price = criteria_utils.get_first_valid(self.request, self.shipping_method.prices.all()) if price: return price.price return self.shipping_method.price
def get_customer_tax_rate(request, product): """Returns the specfic customer tax for the current customer and product. """ if request and hasattr(request, 'cached_customer_tax_rate'): return getattr(request, 'cached_customer_tax_rate') customer_tax = get_first_valid(request, CustomerTax.objects.all(), product) if customer_tax: taxrate = customer_tax.rate else: taxrate = _calc_product_tax_rate(request, product) if request: setattr(request, 'cached_customer_tax_rate', taxrate) return taxrate
def get_customer_tax_rate(request, product): """Returns the specfic customer tax for the current customer and product. """ cache_key = 'cached_customer_tax_rate_%s' % product.pk if request and hasattr(request, cache_key): return getattr(request, cache_key) customer_tax = get_first_valid(request, CustomerTax.objects.all(), product) if customer_tax: taxrate = customer_tax.rate else: taxrate = _calc_product_tax_rate(request, product) if request: setattr(request, cache_key, taxrate) return taxrate
def get_tax_rate(self): from lfs.criteria.utils import get_first_valid from lfs.customer_tax.models import CustomerTax from django.core.cache import cache customer_tax = get_first_valid(self.request, CustomerTax.objects.all(), self.shipping_method) if customer_tax: return customer_tax.rate cache_key = 'shipping_method_tax_{}'.format(self.shipping_method.pk) tax_rate = cache.get(cache_key) if tax_rate is None: if self.shipping_method.tax_id is None: tax_rate = 0 else: tax_rate = self.shipping_method.tax.rate cache.set(cache_key, tax_rate, 60) return tax_rate
def get_customer_tax_rate(request, product): """Returns the specfic customer tax for the current customer and product. """ from lfs.customer_tax.models import CustomerTax cache_key = 'cached_customer_tax_rate_%s' % product.pk if request and hasattr(request, cache_key): return getattr(request, cache_key) all_cache_key = u'all_customer_taxes' customer_taxes = cache.get(all_cache_key) if customer_taxes is None: customer_taxes = CustomerTax.objects.all() cache.set(all_cache_key, customer_taxes) customer_tax = get_first_valid(request, customer_taxes, product) if customer_tax: taxrate = customer_tax.rate else: taxrate = _calc_product_tax_rate(request, product) if request: setattr(request, cache_key, taxrate) return taxrate
def get_first_valid_shipping_method(request, product=None): """Returns the valid shipping method with the highest priority. """ active_shipping_methods = ShippingMethod.objects.filter(active=True) return criteria_utils.get_first_valid(request, active_shipping_methods, product)
def get_default_payment_method(request): """ Returns the default payment method for given request. """ active_payment_methods = PaymentMethod.objects.filter(active=True) return criteria_utils.get_first_valid(request, active_payment_methods)