def __init__(self, request, paymentmodule, *args, **kwargs): super(SimplePayShipForm, self).__init__(*args, **kwargs) self.order = None self.orderpayment = None try: self.tempCart = Cart.objects.from_request(request) if self.tempCart.numItems > 0: products = [item.product for item in self.tempCart.cartitem_set.all()] sale = find_best_auto_discount(products) if sale: self.fields['discount'].initial = sale.code except Cart.DoesNotExist: self.tempCart = None try: self.tempContact = Contact.objects.from_request(request) except Contact.DoesNotExist: self.tempContact = None if kwargs.has_key('default_view_tax'): default_view_tax = kwargs['default_view_tax'] else: default_view_tax = config_value_safe('TAX', 'TAX_SHIPPING', False) shipping_choices, shipping_dict = _get_shipping_choices(request, paymentmodule, self.tempCart, self.tempContact, default_view_tax=default_view_tax) self.fields['shipping'].choices = shipping_choices self.shipping_dict = shipping_dict if not config_value('PAYMENT', 'USE_DISCOUNTS'): self.fields['discount'].widget = forms.HiddenInput() signals.payment_form_init.send(SimplePayShipForm, form=self)
def _get_shipping_choices(request, paymentmodule, cart, contact, default_view_tax=False): """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 = {} if not cart.is_shippable: methods = [shipping_method_by_key('NoShipping'),] else: methods = shipping_methods() for method in methods: method.calculate(cart, contact) if method.valid(): template = lookup_template(paymentmodule, 'shipping/options.html') t = loader.get_template(template) shipcost = method.cost() shipping_tax = None taxed_shipping_price = None if config_value_safe('TAX','TAX_SHIPPING', False): shipping_tax = TaxClass.objects.get(title=config_value('TAX', 'TAX_CLASS')) taxer = _get_taxprocessor(request) total = shipcost + taxer.by_price(shipping_tax, shipcost) taxed_shipping_price = moneyfmt(total) c = RequestContext(request, { 'amount': shipcost, '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}) shipping_options.append((method.id, t.render(c))) shipping_dict[method.id] = shipcost return shipping_options, shipping_dict
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(): 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 __init__(self, request, paymentmodule, *args, **kwargs): super(SimplePayShipForm, self).__init__(*args, **kwargs) try: order = Order.objects.from_request(request) except Order.DoesNotExist: order = None self.order = order self.orderpayment = None self.paymentmodule = paymentmodule try: self.tempCart = Cart.objects.from_request(request) if self.tempCart.numItems > 0: products = [item.product for item in self.tempCart.cartitem_set.all()] except Cart.DoesNotExist: self.tempCart = None try: self.tempContact = Contact.objects.from_request(request) except Contact.DoesNotExist: self.tempContact = None if kwargs.has_key('default_view_tax'): default_view_tax = kwargs['default_view_tax'] else: default_view_tax = config_value_safe('TAX', 'TAX_SHIPPING', False) shipping_choices, shipping_dict = _get_shipping_choices(request, paymentmodule, self.tempCart, self.tempContact, default_view_tax=default_view_tax) cheapshipping = _get_cheapest_shipping(shipping_dict) self.cheapshipping = cheapshipping discount = None if order and order.discount_code: try: discount = Discount.objects.by_code(order.discount_code) if discount and discount.shipping == "FREECHEAP": if cheapshipping: shipping_choices = [opt for opt in shipping_choices if opt[0] == cheapshipping] shipping_dict = {cheapshipping: shipping_dict[cheapshipping]} except Discount.DoesNotExist: pass # possibly hide the shipping based on store config shiphide = config_value('SHIPPING','HIDING') # Handle a partial payment and make sure we don't show a shipping choice after one has # already been chosen if self.order and self.order.is_partially_paid and shipping_dict.get(self.order.shipping_model, False): self.fields['shipping'] = forms.CharField(max_length=30, initial=self.order.shipping_model, widget=forms.HiddenInput(attrs={'value' : shipping_choices[0][0]})) self.shipping_hidden = True # Possibly hide if there is only 1 choise elif shiphide in ('YES', 'DESCRIPTION') and len(shipping_choices) == 1: self.fields['shipping'] = forms.CharField(max_length=30, initial=shipping_choices[0][0], widget=forms.HiddenInput(attrs={'value' : shipping_choices[0][0]})) if shiphide == 'DESCRIPTION': self.shipping_hidden = False self.shipping_description = shipping_choices[0][1] else: self.shipping_hidden = True self.shipping_description = "" elif len(shipping_choices) == 0: self.shipping_hidden = True else: self.fields['shipping'].choices = shipping_choices if config_value('SHIPPING','SELECT_CHEAPEST'): if cheapshipping is not None: self.fields['shipping'].initial = cheapshipping self.shipping_hidden = False self.shipping_dict = shipping_dict form_init.send(SimplePayShipForm, form=self)
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 __init__(self, request, paymentmodule, *args, **kwargs): super(SimplePayShipForm, self).__init__(*args, **kwargs) try: order = Order.objects.from_request(request) except Order.DoesNotExist: order = None self.order = order self.orderpayment = None self.paymentmodule = paymentmodule try: self.tempCart = Cart.objects.from_request(request) if self.tempCart.numItems > 0: products = [ item.product for item in self.tempCart.cartitem_set.all() ] except Cart.DoesNotExist: self.tempCart = None try: self.tempContact = Contact.objects.from_request(request) except Contact.DoesNotExist: self.tempContact = None if kwargs.has_key('default_view_tax'): default_view_tax = kwargs['default_view_tax'] else: default_view_tax = config_value_safe('TAX', 'TAX_SHIPPING', False) shipping_choices, shipping_dict = _get_shipping_choices( request, paymentmodule, self.tempCart, self.tempContact, default_view_tax=default_view_tax) cheapshipping = _get_cheapest_shipping(shipping_dict) self.cheapshipping = cheapshipping discount = None if order and order.discount_code: try: discount = Discount.objects.by_code(order.discount_code) # 'discount' object could be NullDiscount instance if discount and hasattr( discount, 'shipping') and discount.shipping == "FREECHEAP": if cheapshipping: shipping_choices = [ opt for opt in shipping_choices if opt[0] == cheapshipping ] shipping_dict = { cheapshipping: shipping_dict[cheapshipping] } except Discount.DoesNotExist: pass # possibly hide the shipping based on store config shiphide = config_value('SHIPPING', 'HIDING') # Handle a partial payment and make sure we don't show a shipping choice after one has # already been chosen if self.order and self.order.is_partially_paid and shipping_dict.get( self.order.shipping_model, False): self.fields['shipping'] = forms.CharField( max_length=30, initial=self.order.shipping_model, widget=forms.HiddenInput( attrs={'value': shipping_choices[0][0]})) self.shipping_hidden = True # Possibly hide if there is only 1 choise elif shiphide in ('YES', 'DESCRIPTION') and len(shipping_choices) == 1: self.fields['shipping'] = forms.CharField( max_length=30, initial=shipping_choices[0][0], widget=forms.HiddenInput( attrs={'value': shipping_choices[0][0]})) if shiphide == 'DESCRIPTION': self.shipping_hidden = False self.shipping_description = shipping_choices[0][1] else: self.shipping_hidden = True self.shipping_description = "" elif len(shipping_choices) == 0: self.shipping_hidden = True else: self.fields['shipping'].choices = shipping_choices if config_value('SHIPPING', 'SELECT_CHEAPEST'): if cheapshipping is not None: self.fields['shipping'].initial = cheapshipping self.shipping_hidden = False self.shipping_dict = shipping_dict form_init.send(SimplePayShipForm, form=self)