From ba62e339e62bc71eb589b75196d8c1285981d30c Mon Sep 17 00:00:00 2001 From: Thomas Fillon Date: Wed, 30 Sep 2015 00:30:34 +0200 Subject: [PATCH] Manage Shipping rate rules for France/Euro Zone/World --- app/diggersdigest/settings.py | 7 +- app/records/models.py | 4 +- app/records/utils.py | 153 ++++++++++++++++++++++++++++++++++ requirements-dev.txt | 1 + 4 files changed, 161 insertions(+), 4 deletions(-) diff --git a/app/diggersdigest/settings.py b/app/diggersdigest/settings.py index 2223274..19dee6a 100644 --- a/app/diggersdigest/settings.py +++ b/app/diggersdigest/settings.py @@ -134,8 +134,8 @@ SHOP_CURRENCY_LOCALE = '' # is called on submit of the billing/shipping checkout step. This # is where shipping calculation can be performed and set using the # function ``cartridge.shop.utils.set_shipping``. -# SHOP_HANDLER_BILLING_SHIPPING = \ -# "cartridge.shop.checkout.default_billship_handler" +SHOP_HANDLER_BILLING_SHIPPING = \ + "records.utils.custom_billship_handler" # Dotted package path and name of the function that # is called once an order is successful and all of the order @@ -180,7 +180,8 @@ MIGRATION_MODULES = { # PAYPAL # USE or EXTEND the custom callback-uuid form -SHOP_CHECKOUT_FORM_CLASS = 'payments.multipayments.forms.base.CallbackUUIDOrderForm' +#SHOP_CHECKOUT_FORM_CLASS = 'payments.multipayments.forms.base.CallbackUUIDOrderForm' +SHOP_CHECKOUT_FORM_CLASS = 'records.utils.CustomOrderForm' PRIMARY_PAYMENT_PROCESSOR_IN_USE = False diff --git a/app/records/models.py b/app/records/models.py index bcdaf11..fec84e9 100644 --- a/app/records/models.py +++ b/app/records/models.py @@ -17,7 +17,9 @@ from mezzanine.core.models import CONTENT_STATUS_DRAFT, CONTENT_STATUS_PUBLISHED from mezzanine.blog.models import BlogPost from mezzanine.utils.models import upload_to -from utils import * +from cartridge.shop.models import Product + +#from utils import * # Auto-generated Django models with manage.py inspectdb on the old database # You'll have to do the following manually to clean this up: diff --git a/app/records/utils.py b/app/records/utils.py index 8442b5f..758b504 100644 --- a/app/records/utils.py +++ b/app/records/utils.py @@ -5,6 +5,7 @@ from __future__ import unicode_literals from django.db import models from django.utils.translation import ugettext_lazy as _ +from django import forms import datetime import os @@ -12,10 +13,23 @@ import fnmatch from importlib import import_module from mezzanine.conf import settings +from cartridge.shop import checkout from cartridge.shop.models import Product, Category, Cart, Order, ProductVariation, DiscountCode +from cartridge.shop.utils import set_shipping + +from django_countries.data import COUNTRIES +COUNTRY_CHOICES = tuple((k,v) for k,v in COUNTRIES.items()) + +from operator import itemgetter + +COUNTRY_CHOICES = tuple(sorted(COUNTRY_CHOICES, key=itemgetter(1))) + +from payments.multipayments.forms.base import CallbackUUIDOrderForm + from paypal.standard.ipn.signals import valid_ipn_received + def payment_complete(sender, **kwargs): """Performs the same logic as the code in cartridge.shop.models.Order.complete(), but fetches the session, @@ -72,3 +86,142 @@ def payment_complete(sender, **kwargs): pass valid_ipn_received.connect(payment_complete) + +# See https://github.com/SmileyChris/django-countries/blob/master/django_countries/data.py#L46 +EURO_ZONE = [ + ## Zone 1 + 'DE', ## Allemagne, + 'BE', ## Belgique, + 'LU', ## Luxembourg, + 'NL', ## Pays-Bas. + ## Zone 2 : + 'AT', ## Autriche, + 'DK', ## Danemark, + 'ES', ## Espagne, + 'FI', ## Finlande, + 'UK', ## Grande-Bretagne, + 'GR', ## Grèce, + 'IE', ## Irlande, + 'IT', ## Italie, + 'PT', ## Portugal, + 'SE', ## Suède. + ## Zone 3 : + 'BG', ## Bulgarie, + 'CY', ## Chypre, + 'HR', ## Croatie, + 'EE', ## Estonie, + 'HU', ## Hongrie, + 'LV', ## Lettonie, + 'LT', ## Lituanie, + 'MT', ## Malte, + 'PL', ## Pologne, + 'CZ', ## République Tchèque, + 'RO', ## Roumanie, + 'SK', ## Slovaquie, + 'SI', ## Slovénie. + + ## Hors Zone + 'NO', ## Norvege + 'CH', ## Suisse + ] + + +def custom_billship_handler(request, order_form): + """ + Default billing/shipping handler - called when the first step in + the checkout process with billing/shipping address fields is + submitted. Implement your own and specify the path to import it + from via the setting ``SHOP_HANDLER_BILLING_SHIPPING``. + This function will typically contain any shipping calculation + where the shipping amount can then be set using the function + ``cartridge.shop.utils.set_shipping``. The Cart object is also + accessible via ``request.cart`` + """ + + shipping_type = "shipping rate" + shipping_total = 15 + + if order_form is not None: + shipping_detail_country = order_form.cleaned_data['shipping_detail_country'] + if shipping_detail_country == 'FR': + if (request.cart.total_price() <= 100) & (request.cart.total_quantity()< 3): + shipping_type = _("Colissimo shipping") + shipping_total = 7 + else: + shipping_type = _("Colissimo recommande shipping") + shipping_total = 10 + + elif shipping_detail_country in EURO_ZONE: + if (request.cart.total_price() <= 50) & (request.cart.total_quantity()< 6): + shipping_type = _("SIMPLE REGISTERED PRIORITY PARCEL shipping") + shipping_total = 12 + elif (request.cart.total_price() <= 150) & (request.cart.total_quantity()< 6): + shipping_type = _("SIMPLE REGISTERED PRIORITY PARCEL shipping") + shipping_total = 14.50 + else: + shipping_type = _("INTERNATIONAL COLISSIMO (INSURED) shipping") + shipping_total = 18 + + else: + if (request.cart.total_price() <= 50) & (request.cart.total_quantity()< 6): + shipping_type = _("SIMPLE REGISTERED PRIORITY PARCEL shipping") + shipping_total = 13 + elif (request.cart.total_price() <= 150) & (request.cart.total_quantity()< 6): + shipping_type = _("SIMPLE REGISTERED PRIORITY PARCEL shipping") + shipping_total = 16 + else: + shipping_type = _("INTERNATIONAL COLISSIMO (INSURED) shipping") + shipping_total = 25 + + + + if not request.session.get("free_shipping"): + settings.use_editable() + print shipping_type, shipping_total + set_shipping(request, shipping_type, shipping_total) + + +SHIPPING_CHOICES = ( (0,"SIMPLE REGISTERED PRIORITY PARCEL"), + (1,"INTERNATIONAL COLISSIMO (INSURED)"), + (2,"COLISSIMO (France only)"), + (3,"COLISSIMO RECOMMANDE (France only)"), + ) + + + +class CustomOrderForm(CallbackUUIDOrderForm): + ## shipping_method = forms.ChoiceField(choices=SHIPPING_CHOICES, + ## label=_("Shipping method")) + + def __init__(self, request, step, data=None, initial=None, errors=None, + **kwargs): + """ + Setup for each order form step which does a few things: + """ + super(CustomOrderForm, self).__init__(request, step, data=data, initial=initial, **kwargs) + billing_country = forms.Select(choices=COUNTRY_CHOICES) + shipping_country = forms.Select(choices=COUNTRY_CHOICES) + self.fields['billing_detail_country'].widget = billing_country + self.fields['shipping_detail_country'].widget = shipping_country + + + + ## if step == checkout.CHECKOUT_STEP_PAYMENT: + ## print "Payment step" + ## if self.is_valid(): + ## data = self.cleaned_data + ## if data["shipping_detail_country"] == 'France': + ## print ' ----- FRANCE ----' + ## choices = SHIPPING_CHOICES[2:3] + ## self.fields["shipping_method"].choices = choices + ## else: + ## print ' ----- PAS FRANCE ---------' + ## print self.fields["shipping_detail_country"] + ## else: + ## print "Pas VALIDE" + + + + ## class Meta(CallbackUUIDOrderForm.Meta): + ## + ## fields = CallbackUUIDOrderForm.Meta.fields + ["shipping_method"] + ## print fields diff --git a/requirements-dev.txt b/requirements-dev.txt index e10f4b5..1787476 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,3 +1,4 @@ -e git+https://github.com/Parisson/cartridge-payments.git#egg=cartridge-payments -e git+https://github.com/Parisson/django-paypal.git#egg=django-paypal-0.2.5 +django-countries -- 2.39.5