[ADD] website_sale_coupon_delivery: add bridge module to avoid hacks

This commit introduce a new module which is a bridge between
`website_sale_coupon` and `website_sale_delivery`.

Before that, commit odoo/enterprise@a0a8118 and odoo/odoo@1ab53c2 introduced coupon
code into delivery module, and coupon+delivery code into `website_sale`
module.

That was the only way in stable but was nasty as it is not only code from one
module in another module but also code from enterprise module in community
module..

This is done to prepare the new feature to load carrier asynchronously on cart,
see #31857

(Note that `sale_coupon_delivery` already exists)

task-1867265

Coming from #31857
This commit is contained in:
Romain Derie
2019-04-02 08:00:34 +00:00
committed by Jeremy Kersten
parent 9a6a00ed13
commit ca7da746d8
10 changed files with 102 additions and 51 deletions
-17
View File
@@ -1104,23 +1104,6 @@ class WebsiteSale(http.Controller):
phone_code=country.phone_code
)
@http.route(['/shop/update_carrier'], type='json', auth='public', methods=['POST'], website=True, csrf=False)
def update_eshop_carrier(self, **post):
results = {}
if hasattr(self, '_update_website_sale_delivery'):
results.update(self._update_website_sale_delivery(**post))
if hasattr(self, '_update_website_sale_coupon'):
results.update(self._update_website_sale_coupon(**post))
return results
def _format_amount(self, amount, currency):
fmt = "%.{0}f".format(currency.decimal_places)
lang = request.env['res.lang']._lang_get(request.env.context.get('lang') or 'en_US')
return lang.format(fmt, currency.round(amount), grouping=True, monetary=True)\
.replace(r' ', u'\N{NO-BREAK SPACE}').replace(r'-', u'-\N{ZERO WIDTH NO-BREAK SPACE}')
# --------------------------------------------------------------------------
# Products Search Bar
# --------------------------------------------------------------------------
@@ -28,23 +28,6 @@ class WebsiteSale(WebsiteSale):
order.recompute_coupon_lines()
return super(WebsiteSale, self).cart(**post)
def _update_website_sale_coupon(self, **post):
order = request.website.sale_get_order()
order.recompute_coupon_lines()
free_shipping_lines = order._get_free_shipping_lines()
currency = order.currency_id
result = {}
if free_shipping_lines:
amount_free_shipping = sum(free_shipping_lines.mapped('price_subtotal'))
result.update({
'new_amount_delivery': self._format_amount(0.0, currency),
'new_amount_untaxed': self._format_amount(order.amount_untaxed, currency),
'new_amount_tax': self._format_amount(order.amount_tax, currency),
'new_amount_total': self._format_amount(order.amount_total, currency),
'new_amount_order_discounted': self._format_amount(order.reward_amount - amount_free_shipping, currency)
})
return result
# Override
# Add in the rendering the free_shipping_line
def _get_shop_payment_values(self, order, **kwargs):
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import controllers
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
{
'name': "Free Delivery with Coupon on eCommerce",
'summary': """Allows to offer free shippings in coupon reward on eCommerce""",
'description': """Allows to offer free shippings in coupon reward on eCommerce""",
'category': 'Website',
'version': '1.0',
'depends': ['website_sale_delivery', 'sale_coupon_delivery'],
'data': [
'views/website_sale_coupon_delivery_templates.xml',
],
'auto_install': True,
}
@@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import main
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
from odoo import http
from odoo.addons.website_sale_delivery.controllers.main import WebsiteSaleDelivery
from odoo.http import request
class WebsiteSaleCouponDelivery(WebsiteSaleDelivery):
@http.route()
def update_eshop_carrier(self, **post):
result = super(WebsiteSaleCouponDelivery, self).update_eshop_carrier(**post)
order = request.website.sale_get_order()
order.recompute_coupon_lines()
free_shipping_lines = order._get_free_shipping_lines()
if free_shipping_lines:
currency = order.currency_id
amount_free_shipping = sum(free_shipping_lines.mapped('price_subtotal'))
result.update({
'new_amount_delivery': self._format_amount(0.0, currency),
'new_amount_untaxed': self._format_amount(order.amount_untaxed, currency),
'new_amount_tax': self._format_amount(order.amount_tax, currency),
'new_amount_total': self._format_amount(order.amount_total, currency),
'new_amount_order_discounted': self._format_amount(order.reward_amount - amount_free_shipping, currency),
})
return result
@@ -0,0 +1,31 @@
odoo.define('website_sale_coupon_delivery.checkout', function (require) {
'use strict';
var core = require('web.core');
var publicWidget = require('web.public.widget');
require('website_sale_delivery.checkout');
var _t = core._t;
publicWidget.registry.websiteSaleDelivery.include({
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* @override
*/
_handleCarrierUpdateResult: function (result) {
this._super.apply(this, arguments);
if (result.new_amount_order_discounted) {
// Update discount of the order
$('#order_discounted').find('.oe_currency_value').text(result.new_amount_order_discounted);
// We are in freeshipping, so every carrier is Free but we don't
// want to replace error message by 'Free'
$('#delivery_carrier .badge:not(.o_wsale_delivery_carrier_error)').text(_t('Free'));
}
},
});
});
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="assets_frontend" inherit_id="website.assets_frontend">
<xpath expr="." position="inside">
<script type="text/javascript" src="/website_sale_coupon_delivery/static/src/js/website_sale_coupon_delivery.js"></script>
</xpath>
</template>
</odoo>
@@ -21,6 +21,14 @@ class WebsiteSaleDelivery(WebsiteSale):
return super(WebsiteSaleDelivery, self).payment(**post)
@http.route(['/shop/update_carrier'], type='json', auth='public', methods=['POST'], website=True, csrf=False)
def update_eshop_carrier(self, **post):
order = request.website.sale_get_order()
carrier_id = int(post['carrier_id'])
if order:
order._check_carrier_quotation(force_carrier_id=carrier_id)
return self._update_website_sale_delivery_return(order, **post)
def order_lines_2_google_api(self, order_lines):
""" Transforms a list of order lines into a dict for google analytics """
order_lines_not_delivery = order_lines.filtered(lambda line: not line.is_delivery)
@@ -55,13 +63,6 @@ class WebsiteSaleDelivery(WebsiteSale):
values['delivery_action_id'] = request.env.ref('delivery.action_delivery_carrier_form').id
return values
def _update_website_sale_delivery(self, **post):
order = request.website.sale_get_order()
carrier_id = int(post['carrier_id'])
if order:
order._check_carrier_quotation(force_carrier_id=carrier_id)
return self._update_website_sale_delivery_return(order, **post)
def _update_website_sale_delivery_return(self, order, **post):
carrier_id = int(post['carrier_id'])
currency = order.currency_id
@@ -76,3 +77,9 @@ class WebsiteSaleDelivery(WebsiteSale):
'new_amount_total': self._format_amount(order.amount_total, currency),
}
return {}
def _format_amount(self, amount, currency):
fmt = "%.{0}f".format(currency.decimal_places)
lang = request.env['res.lang']._lang_get(request.env.context.get('lang') or 'en_US')
return lang.format(fmt, currency.round(amount), grouping=True, monetary=True)\
.replace(r' ', u'\N{NO-BREAK SPACE}').replace(r'-', u'-\N{ZERO WIDTH NO-BREAK SPACE}')
@@ -46,16 +46,6 @@ publicWidget.registry.websiteSaleDelivery = publicWidget.Widget.extend({
var $carrierBadge = $('#delivery_carrier input[name="delivery_type"][value=' + result.carrier_id + '] ~ .badge.d-none');
var $computeBadge = $('#delivery_carrier input[name="delivery_type"][value=' + result.carrier_id + '] ~ .o_delivery_compute');
if (result.new_amount_order_discounted) {
// Cross module without bridge
// Update discount of the order
$('#order_discounted').find('.oe_currency_value').text(result.new_amount_order_discounted);
// We are in freeshipping, so every carrier is Free but we don't
// want to replace error message by 'Free'
$('#delivery_carrier .badge:not(.o_wsale_delivery_carrier_error)').text(_t('Free'));
}
if (result.status === true) {
// if free delivery (`free_over` field), show 'Free', not '$0'
if (parseInt(result.new_amount_delivery)) {