def test_basic(self): pcalc = PriceAdjustmentCalc(self.price) p = PriceAdjustment('test', amount=Decimal(1)) pcalc += p pcalc += p p = PriceAdjustment('test2', amount=Decimal(10)) pcalc += p self.assertEqual(pcalc.total_adjustment(), Decimal(12))
def tiered_price_listener(signal, adjustment=None, **kwargs): """Listens for satchmo_price_query signals, and returns a tiered price instead of the default price. Requires threaded_multihost.ThreadLocalMiddleware to be installed so that it can determine the current user.""" if kwargs.has_key('discountable'): discountable = kwargs['discountable'] else: discountable = adjustment.product.is_discountable if discountable: product = adjustment.product user = threadlocals.get_current_user() if user and not user.is_anonymous(): try: tiers = PricingTier.objects.by_user(user) log.debug('got tiers: %s', tiers) best = None besttier = None currentprice = adjustment.final_price() qty = adjustment.price.quantity for tier in tiers: candidate = None try: tp = TieredPrice.objects.by_product_qty( tier, product, qty) log.debug("Found a Tiered Price for %s qty %d = %s", product.slug, qty, tp.price) candidate = tp.price except TieredPrice.DoesNotExist: pcnt = tier.discount_percent if pcnt is not None and pcnt != 0: candidate = currentprice * (100 - pcnt) / 100 if best is None or (candidate and candidate < best): best = candidate besttier = tier log.debug('best = %s', best) if best is not None: delta = currentprice - best adjustment += PriceAdjustment( 'tieredpricing', _('Tiered Pricing for %(tier)s' % {'tier': besttier.group.name}), delta) except PricingTier.DoesNotExist: pass
def _get_shipping_choices(request, paymentmodule, cart, contact, default_view_tax=False, order=None): """Iterate through legal shipping modules, building the list for display to the user. Returns the shipping choices list, along with a dictionary of shipping choices, useful for building javascript that operates on shipping choices. """ shipping_options = [] shipping_dict = {} rendered = {} if not order: try: order = Order.objects.from_request(request) except Order.DoesNotExist: pass discount = None if order: try: discount = Discount.objects.by_code(order.discount_code) except Discount.DoesNotExist: pass if not cart.is_shippable: methods = [ shipping_method_by_key('NoShipping'), ] else: methods = shipping_methods() tax_shipping = config_value_safe('TAX', 'TAX_SHIPPING', False) shipping_tax = None if tax_shipping: taxer = _get_taxprocessor(request) shipping_tax = TaxClass.objects.get( title=config_value('TAX', 'TAX_CLASS')) for method in methods: method.calculate(cart, contact) if method.valid(order=order): template = lookup_template(paymentmodule, 'shipping/options.html') t = loader.get_template(template) shipcost = finalcost = method.cost() if discount and order: order.shipping_cost = shipcost discount.calc(order) shipdiscount = discount.item_discounts.get('Shipping', 0) else: shipdiscount = 0 # set up query to determine shipping price to show shipprice = Price() shipprice.price = shipcost shipadjust = PriceAdjustmentCalc(shipprice) if shipdiscount: shipadjust += PriceAdjustment('discount', _('Discount'), shipdiscount) satchmo_shipping_price_query.send(cart, adjustment=shipadjust) shipdiscount = shipadjust.total_adjustment() if shipdiscount: finalcost -= shipdiscount shipping_dict[method.id] = { 'cost': shipcost, 'discount': shipdiscount, 'final': finalcost } taxed_shipping_price = None if tax_shipping: taxcost = taxer.by_price(shipping_tax, finalcost) total = finalcost + taxcost taxed_shipping_price = moneyfmt(total) shipping_dict[method.id]['taxedcost'] = total shipping_dict[method.id]['tax'] = taxcost c = RequestContext( request, { 'amount': finalcost, 'description': method.description(), 'method': method.method(), 'expected_delivery': method.expectedDelivery(), 'default_view_tax': default_view_tax, 'shipping_tax': shipping_tax, 'taxed_shipping_price': taxed_shipping_price }) rendered[method.id] = t.render(c) #now sort by price, low to high sortme = [(value['cost'], key) for key, value in shipping_dict.items()] sortme.sort() shipping_options = [(key, rendered[key]) for cost, key in sortme] shipping_choices_query.send(sender=cart, cart=cart, paymentmodule=paymentmodule, contact=contact, default_view_tax=default_view_tax, order=order, shipping_options=shipping_options, shipping_dict=shipping_dict) return shipping_options, shipping_dict
def five_off(sender, adjustment=None, **kwargs): adjustment += PriceAdjustment('half', 'Half', amount=Decimal(5))