[FIX] website_sale: better fiscal position detection + support update during checkout
authorOlivier Dony <odo@openerp.com>
Fri, 10 Oct 2014 19:02:30 +0000 (21:02 +0200)
committerOlivier Dony <odo@openerp.com>
Fri, 10 Oct 2014 19:02:33 +0000 (21:02 +0200)
The customer can change the country and tax
number in the billing information during
checkout, and the taxes should be properly
updated according to the re-detected fiscal
position.

The fiscal position detection also depends
on the `vat_subjected` flag, which we now assume
to be implicit as soon as the customer filled
in a valid Tax Identification Number.

addons/website_sale/controllers/main.py
addons/website_sale/models/sale_order.py

index 7e0412e..060370b 100644 (file)
@@ -426,7 +426,7 @@ class website_sale(http.Controller):
         return values
 
     mandatory_billing_fields = ["name", "phone", "email", "street2", "city", "country_id", "zip"]
-    optional_billing_fields = ["street", "state_id", "vat"]
+    optional_billing_fields = ["street", "state_id", "vat", "vat_subjected"]
     mandatory_shipping_fields = ["name", "phone", "street", "city", "country_id", "zip"]
     optional_shipping_fields = ["state_id"]
 
@@ -457,6 +457,9 @@ class website_sale(http.Controller):
         if query.get(prefix + 'country_id'):
             query[prefix + 'country_id'] = int(query[prefix + 'country_id'])
 
+        if query.get(prefix + 'vat'):
+            query[prefix + 'vat_subjected'] = True
+
         if not remove_prefix:
             return query
 
@@ -531,7 +534,13 @@ class website_sale(http.Controller):
             'partner_invoice_id': partner_id,
         }
         order_info.update(order_obj.onchange_partner_id(cr, SUPERUSER_ID, [], partner_id, context=context)['value'])
-        order_info.update(order_obj.onchange_delivery_id(cr, SUPERUSER_ID, [], order.company_id.id, partner_id, checkout.get('shipping_id'), None, context=context)['value'])
+        address_change = order_obj.onchange_delivery_id(cr, SUPERUSER_ID, [], order.company_id.id, partner_id,
+                                                        checkout.get('shipping_id'), None, context=context)['value']
+        order_info.update(address_change)
+        if address_change.get('fiscal_position'):
+            fiscal_update = order_obj.onchange_fiscal_position(cr, SUPERUSER_ID, [], address_change['fiscal_position'],
+                                                               [(4, l.id) for l in order.order_line], context=None)['value']
+            order_info.update(fiscal_update)
 
         order_info.pop('user_id')
         order_info.update(partner_shipping_id=checkout.get('shipping_id') or partner_id)
index 1f7e3f1..6242e4c 100644 (file)
@@ -49,6 +49,7 @@ class sale_order(osv.Model):
             pricelist=so.pricelist_id.id,
             product=product_id,
             partner_id=so.partner_id.id,
+            fiscal_position=so.fiscal_position.id,
             qty=qty,
             context=context
         )['value']