[FIX] website_sale_delivery: Adapt website_sale_delivery module to the new checkout

This commit is contained in:
Jeremy Kersten
2016-07-04 15:48:15 +02:00
parent e4a317a176
commit 68b95fb264
7 changed files with 62 additions and 64 deletions
+15 -5
View File
@@ -518,7 +518,8 @@ class WebsiteSale(http.Controller):
def address(self, **kw):
Partner = request.env['res.partner'].with_context(show_address=1).sudo()
order = request.website.sale_get_order(force_create=1)
mode, def_country_id = False, False
mode = (False, False)
def_country_id = order.partner_id.country_id
values, errors = {}, {}
partner_id = int(kw.get('partner_id', -1))
@@ -537,7 +538,6 @@ class WebsiteSale(http.Controller):
def_country_id = request.website.user_id.sudo().country_id
# IF ORDER LINKED TO A PARTNER
else:
def_country_id = order.partner_id.country_id
if partner_id > 0:
if partner_id == order.partner_id.id:
mode = ('edit', 'billing')
@@ -551,7 +551,7 @@ class WebsiteSale(http.Controller):
values = Partner.browse(partner_id)
elif partner_id == -1:
mode = ('new', 'shipping')
else: # no mode - refresh without post?
else: # no mode - refresh without post?
return request.redirect('/shop/checkout')
# IF POSTED
@@ -580,12 +580,14 @@ class WebsiteSale(http.Controller):
return request.redirect(kw.get('callback') or '/shop/checkout')
country = 'country_id' in values and values['country_id'] != '' and request.env['res.country'].browse(int(values['country_id']))
country = country and country.exists() or def_country_id
render_values = {
'partner_id': partner_id,
'mode': mode,
'checkout': values,
'country': country and country.exists() or def_country_id,
'country': country,
'countries': country.get_website_sale_countries(mode=mode[1]),
"states": country.get_website_sale_states(mode=mode[1]),
'error': errors,
'callback': kw.get('callback'),
}
@@ -962,3 +964,11 @@ class WebsiteSale(http.Controller):
},
'lines': self.order_lines_2_google_api(order.order_line)
}
@http.route(['/shop/country_infos/<model("res.country"):country>'], type='json', auth="public", methods=['POST'], website=True)
def country_infos(self, country, mode, **kw):
return dict(
fields=country.get_address_fields(),
states=[(st.id, st.name, st.code) for st in country.get_website_sale_states(mode=mode)],
phone_code=country.phone_code
)
+10
View File
@@ -426,6 +426,16 @@ class WebsitePricelist(models.Model):
return res
class ResCountry(models.Model):
_inherit = 'res.country'
def get_website_sale_countries(self, mode='billing'):
return self.sudo().search([])
def get_website_sale_states(self, mode='billing'):
return self.sudo().state_ids
class ResCountryGroup(models.Model):
_inherit = 'res.country.group'
@@ -340,7 +340,7 @@ odoo.define('website_sale.website_sale', function (require) {
$(oe_website_sale).on('change', "select[name='country_id']", function () {
clickwatch(function() {
if ($("#country_id").val()) {
ajax.jsonRpc("/website/country_infos/" + $("#country_id").val(), 'call', {}).then(
ajax.jsonRpc("/shop/country_infos/" + $("#country_id").val(), 'call', {mode: 'shipping'}).then(
function(data) {
// placeholder phone_code
//$("input[name='phone']").attr('placeholder', data.phone_code !== 0 ? '+'+ data.phone_code : '');
+1 -1
View File
@@ -1116,7 +1116,7 @@
<label class="control-label" for="country_id">Country</label>
<select id="country_id" name="country_id" class="form-control">
<option value="">Country...</option>
<t t-foreach="request.env['res.country'].sudo().search([])" t-as="c">
<t t-foreach="countries" t-as="c">
<option t-att-value="c.id" t-att-selected="c.id == (country and country.id or -1)">
<t t-esc="c.name" />
</option>
@@ -25,10 +25,6 @@ class WebsiteSaleDelivery(WebsiteSale):
order_lines_not_delivery = order_lines.filtered(lambda line: not line.is_delivery)
return super(WebsiteSaleDelivery, self).order_lines_2_google_api(order_lines_not_delivery)
def checkout_values(self, data=None):
values = super(WebsiteSaleDelivery, self).checkout_values(data)
return request.env['sale.order']._get_shipping_country(values)
def order_2_return_dict(self, order):
""" Returns the tracking_cart dict of the order for Google analytics """
ret = super(WebsiteSaleDelivery, self).order_2_return_dict(order)
@@ -6,6 +6,7 @@ from odoo.exceptions import ValidationError
_logger = logging.getLogger(__name__)
class DeliveryCarrier(models.Model):
_name = 'delivery.carrier'
_inherit = ['delivery.carrier', 'website.published.mixin']
@@ -13,6 +14,7 @@ class DeliveryCarrier(models.Model):
website_description = fields.Text(related='product_id.description_sale', string='Description for Online Quotations')
website_published = fields.Boolean(default=False)
class SaleOrder(models.Model):
_inherit = 'sale.order'
@@ -123,7 +125,7 @@ class SaleOrder(models.Model):
""" Override to update carrier quotation if quantity changed """
self._delivery_unset()
# When you update a cart, it is not enouf to remove the "delivery cost" line
# The carrier might also be invalid, eg: if you bought things that are too heavy
# -> this may cause a bug if you go to the checkout screen, choose a carrier,
@@ -138,23 +140,37 @@ class SaleOrder(models.Model):
return values
def _get_shipping_country(self, values):
countries = self.env['res.country']
class ResCountry(models.Model):
_inherit = 'res.country'
def get_website_sale_countries(self, mode='billing'):
res = super(ResCountry, self).get_website_sale_countries(mode=mode)
if mode == 'shipping':
countries = self.env['res.country']
delivery_carriers = self.env['delivery.carrier'].sudo().search([('website_published', '=', True)])
for carrier in delivery_carriers:
if not carrier.country_ids and not carrier.state_ids:
countries = res
break
countries |= carrier.country_ids
res = res & countries
return res
def get_website_sale_states(self, mode='billing'):
res = super(ResCountry, self).get_website_sale_states(mode=mode)
states = self.env['res.country.state']
values['shipping_countries'] = values['countries']
values['shipping_states'] = values['states']
if mode == 'shipping':
dom = ['|', ('country_ids', 'in', self.id), ('country_ids', '=', False), ('website_published', '=', True)]
delivery_carriers = self.env['delivery.carrier'].sudo().search(dom)
delivery_carriers = self.env['delivery.carrier'].sudo().search([('website_published', '=', True)])
for carrier in delivery_carriers:
if not carrier.country_ids and not carrier.state_ids:
return values
# Authorized shipping countries
countries |= carrier.country_ids
# Authorized shipping countries without any state restriction
state_countries = carrier.country_ids - carrier.state_ids.mapped('country_id')
# Authorized shipping states + all states from shipping countries without any state restriction
states |= carrier.state_ids | values['states'].filtered(lambda state: state.country_id in state_countries)
values['shipping_countries'] = values['countries'] & countries
values['shipping_states'] = values['states'] & states
return values
for carrier in delivery_carriers:
if not carrier.country_ids and not carrier.state_ids:
states = res
break
states |= carrier.state_ids
res = res & states
return res
@@ -51,40 +51,6 @@
</xpath>
</template>
<template id="country_state_shipping" name="Shipping Country and states" inherit_id="website_sale.checkout">
<!-- By default, with 'ship to the same address', we can only ship to restricted countries & states -->
<xpath expr="//select[@name='country_id']" position="attributes">
<attribute name="t-att-disabled">not shipping_id and 'disabled' or None</attribute>
<attribute name="t-att-style">not shipping_id and 'display: none' or None</attribute>
</xpath>
<xpath expr="//select[@name='country_id']" position="after">
<select name="country_id" class="form-control o_provider_restricted" t-att-style="shipping_id and 'display:none' or None" t-att-disabled="shipping_id and 'disabled' or None">
<option value="">Country...</option>
<t t-foreach="shipping_countries or []" t-as="country">
<option t-att-value="country.id" t-att-selected="country.id == checkout.get('country_id')"><t t-esc="country.name"/></option>
</t>
</select>
</xpath>
<xpath expr="//select[@name='state_id']" position="attributes">
<attribute name="t-att-disabled">not shipping_id and 'disabled' or None</attribute>
<attribute name="t-att-style">not shipping_id and 'display: none' or None</attribute>
</xpath>
<xpath expr="//select[@name='state_id']" position="after">
<select name="state_id" class="form-control o_provider_restricted" t-att-style="shipping_id and 'display:none' or None" t-att-disabled="shipping_id and 'disabled' or None">
<option value="">Select...</option>
<t t-foreach="shipping_states or []" t-as="state">
<option t-att-value="state.id" style="display:none;" t-att-data-country_id="state.country_id.id" t-att-selected="state.id == checkout.get('state_id')"><t t-esc="state.name"/></option>
</t>
</select>
</xpath>
<xpath expr="//select[@name='shipping_country_id']/t[@t-foreach]" position="attributes">
<attribute name="t-foreach">shipping_countries</attribute>
</xpath>
<xpath expr="//select[@name='shipping_state_id']/t[@t-foreach]" position="attributes">
<attribute name="t-foreach">shipping_states</attribute>
</xpath>
</template>
<template id="shipping_tracking" name="Shipping tracking on orders followup" inherit_id="website_sale_stock.orders_followup_shipping">
<xpath expr="//div[@id='picking_info']" position="after">
<div t-if="i.carrier_tracking_ref">