from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from oscar.apps.checkout import session, exceptions
from oscar_vat_moss import vat


class SeparateVATCheckoutSessionMixin(session.CheckoutSessionMixin):

    def get_context_data(self, **kwargs):
        ctx = super(SeparateVATCheckoutSessionMixin,
                    self).get_context_data(**kwargs)

        # Oscar's checkout templates look for this variable which specifies to
        # break out the tax totals into a separate subtotal.
        ctx['show_tax_separately'] = True

        return ctx


class FixedRateVATCheckoutSessionMixin(SeparateVATCheckoutSessionMixin):
    pass


class DeferredVATCheckoutSessionMixin(SeparateVATCheckoutSessionMixin):

    def build_submission(self, **kwargs):
        submission = super(DeferredVATCheckoutSessionMixin,
                           self).build_submission(**kwargs)

        assess_tax = (submission['shipping_method'] and
                      submission['shipping_address'] and
                      submission['shipping_address'].phone_number)
        if assess_tax:
            try:
                vat.apply_to(submission)
                # Recalculate order total to ensure we have a
                # tax-inclusive total
                submission['order_total'] = self.get_order_totals(
                    submission['basket'], submission['shipping_charge'])
            except vat.VATAssessmentException:
                pass
        return submission

    def check_a_valid_shipping_address_is_captured(self):
        super(DeferredVATCheckoutSessionMixin, self)
        shipping_address = self.get_shipping_address(
            basket=self.request.basket)
        try:
            vat.lookup_vat_for_shipping_address(shipping_address)
        except vat.VATAssessmentException as e:
            message = _("%s. Please try again." % str(e))
            raise exceptions.FailedPreCondition(
                url=reverse('checkout:shipping-address'),
                message=message
            )

@@ -185,24 +185,16 @@
 %0A%0Aclass 
-Separate
 VATCheck
@@ -303,24 +303,16 @@
 = super(
-Separate
 VATCheck
@@ -589,53 +589,1088 @@
 tx%0A%0A
-%0Aclass FixedRateVATCheckoutSessionMixin(Separ
+    def check_a_valid_shipping_address_is_captured(self):%0A        %22%22%22Check that the just-captured shipping address is valid.%0A%0A        There is one thing that we can't do through a normal field%0A        validator, and that is to check multiple form fields against%0A        one another. We have to that for VAT: check whether the phone%0A        country code matches the country, whether the phone area code%0A        matches the postcode (in case of a user living in a VAT%0A        exception territory), and whether or not any VATIN that the%0A        user entered matches the registered company name. So, we do%0A        that here.%0A%0A        %22%22%22%0A        shipping_address = self.get_shipping_address(%0A            basket=self.request.basket)%0A        try:%0A            vat.lookup_vat_for_shipping_address(shipping_address)%0A        except vat.VATAssessmentException as e:%0A            message = _(%22%25s. Please try again.%22 %25 str(e))%0A            raise exceptions.FailedPreCondition(%0A                url=reverse('checkout:shipping-address'),%0A                message=message%0A            )%0A%0A%0Aclass FixedR
 ateV
@@ -1695,68 +1695,1191 @@
 ixin
+(VATCheckoutSessionMixin
 ):%0A
+%0A
     
-pass%0A%0A%0Aclass DeferredVATCheckoutSessionMixin(Separate
+def check_a_valid_shipping_address_is_captured(self):%0A        %22%22%22Check that the just-captured shipping address is valid.%0A%0A        Normally, this method is only used to perform a general%0A        shipping address check. With the FixedRateVATStrategy however,%0A        we rely on the default shipping address to figure out a user's%0A        applicable VAT rate. Therefore, when we are satisfied that the%0A        shipping address is valid, we also set it as the default for%0A        shipping. This will cause a minor nuisance for any user who%0A        doesn't expect that the default shipping address is simply the%0A        one used most recently, but in this case, the benefits%0A        outweigh the disadvantages.%0A%0A        %22%22%22%0A        # The superclass does the VAT validation check. Once that has%0A        # completed, we can set the shipping address to be the new%0A        # default.%0A        super(FixedRateVATCheckoutSessionMixin, self)%0A        shipping_address = self.get_shipping_address(%0A            basket=self.request.basket)%0A        shipping_address.is_default_for_shipping = True%0A        shipping_address.save()%0A%0A%0Aclass DeferredVATCheckoutSessionMixin(
 VATC
@@ -3663,548 +3663,4 @@
 ion%0A
-%0A    def check_a_valid_shipping_address_is_captured(self):%0A        super(DeferredVATCheckoutSessionMixin, self)%0A        shipping_address = self.get_shipping_address(%0A            basket=self.request.basket)%0A        try:%0A            vat.lookup_vat_for_shipping_address(shipping_address)%0A        except vat.VATAssessmentException as e:%0A            message = _(%22%25s. Please try again.%22 %25 str(e))%0A            raise exceptions.FailedPreCondition(%0A                url=reverse('checkout:shipping-address'),%0A                message=message%0A            )%0A

Improve shipping address check