Skip to content
Permalink
Browse files

m

  • Loading branch information...
rdeodoo committed Mar 15, 2019
1 parent 576f075 commit 0c5127375db27e8a38327c37b7d77aca7bb1165b
@@ -1105,23 +1105,6 @@ def country_infos(self, country, mode, **kw):
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}')

@http.route(['/shop/cart/update_option'], type='http', auth="public", methods=['POST'], website=True, multilang=False)
def cart_options_update_json(self, product_id, add_qty=1, set_qty=0, goto_shop=None, lang=None, **kw):
"""This route is called when submitting the optional product modal."""
@@ -21,6 +21,30 @@ def payment(self, **post):

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)

@http.route(['/shop/carrier_rate_shipment'], type='json', auth='public', methods=['POST'], website=True)
def cart_carrier_rate_shipment(self, carrier_id, **kw):
order = request.website.sale_get_order(force_create=True)
res = {'carrier_id': carrier_id}
carrier = request.env['delivery.carrier'].browse(int(carrier_id))
rate = carrier.rate_shipment(order)
if rate.get('success'):
res['status'] = True
res['new_amount_delivery'] = rate['price']
res['error_message'] = rate['warning_message']
else:
res['status'] = False
res['new_amount_delivery'] = 0.0
res['error_message'] = rate['error_message']
return res

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 +79,6 @@ def _get_shop_payment_values(self, order, **kwargs):
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 +93,9 @@ def _update_website_sale_delivery_return(self, order, **post):
'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}')
@@ -19,64 +19,90 @@ publicWidget.registry.websiteSaleDelivery = publicWidget.Widget.extend({
* @override
*/
start: function () {
var self = this;
var $carriers = $('#delivery_carrier input[name="delivery_type"]');
// Workaround to:
// - update the amount/error on the label at first rendering
// - prevent clicking on 'Pay Now' if the shipper rating fails
if ($carriers.length > 0) {
$carriers.filter(':checked').click();
}

// Asynchronously retrieve every carrier price
_.each($carriers, function (carrierInput, k) {
self._showLoading($(carrierInput));
self._rpc({
route: '/shop/carrier_rate_shipment',
params: {
carrier_id: carrierInput.value,
},
}).then(self._handleCarrierUpdateResultBadge.bind(self));
});

return this._super.apply(this, arguments);
},

//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------

/**
* @private
* @param {jQuery} $carrierInput
*/
_showLoading: function ($carrierInput) {
$carrierInput.siblings('.o_delivery_badge_price').addClass('d-none');
$carrierInput.siblings('.o_delivery_compute').removeClass('d-none').html('<span class="fa fa-spinner fa-spin"/>');
},
/**
* @private
* @param {Object} result
*/
_handleCarrierUpdateResult: function (result) {
this._handleCarrierUpdateResultBadge(result);
var $payButton = $('#o_payment_form_pay');
var $amountDelivery = $('#order_delivery span.oe_currency_value');
var $amountUntaxed = $('#order_total_untaxed span.oe_currency_value');
var $amountTax = $('#order_total_taxes span.oe_currency_value');
var $amountTotal = $('#order_total span.oe_currency_value');
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(.carrier-error)').text(_t('Free'));
}

if (result.status === true) {
$amountDelivery.text(result.new_amount_delivery);
$amountUntaxed.text(result.new_amount_untaxed);
$amountTax.text(result.new_amount_tax);
$amountTotal.text(result.new_amount_total);
$carrierBadge.children('span').text(result.new_amount_delivery);
$carrierBadge.removeClass('d-none');
$computeBadge.addClass('d-none');
$computeBadge.removeClass('carrier-error');
$payButton.data('disabled_reasons').carrier_selection = false;
$payButton.prop('disabled', _.contains($payButton.data('disabled_reasons'), true));
} else {
console.error(result.error_message);
$computeBadge.addClass('carrier-error');
$computeBadge.text(result.error_message);
$amountDelivery.text(result.new_amount_delivery);
$amountUntaxed.text(result.new_amount_untaxed);
$amountTax.text(result.new_amount_tax);
$amountTotal.text(result.new_amount_total);
}
},
/**
* @private
* @param {Object} result
*/
_handleCarrierUpdateResultBadge: function (result) {
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.status === true) {
if (result.new_amount_delivery) {
$carrierBadge.children('span').text(result.new_amount_delivery);
} else {
$carrierBadge.text(_t('Free'));
}
$carrierBadge.removeClass('d-none');
$computeBadge.addClass('d-none');
$computeBadge.removeClass('carrier-error');
} else {
console.error(result.error_message);
$computeBadge.addClass('carrier-error');
$computeBadge.text(result.error_message);
}
},

//--------------------------------------------------------------------------
// Handlers
@@ -88,6 +114,7 @@ publicWidget.registry.websiteSaleDelivery = publicWidget.Widget.extend({
*/
_onCarrierClick: function (ev) {
var $radio = $(ev.currentTarget).find('input[type="radio"]');
this._showLoading($radio);
$radio.prop("checked", true);
var $payButton = $('#o_payment_form_pay');
$payButton.prop('disabled', true);
@@ -31,7 +31,7 @@
<span t-else="" t-att-class="badge_class">Free</span>
</t>
<t t-else="">
<span t-attf-class="#{badge_class} d-none" t-field="delivery.fixed_price" t-options='{"widget": "monetary", "from_currency": delivery.product_id.company_id.currency_id, "display_currency": website_sale_order.currency_id}'/>
<span t-attf-class="#{badge_class} o_delivery_badge_price d-none" t-field="delivery.fixed_price" t-options='{"widget": "monetary", "from_currency": delivery.product_id.company_id.currency_id, "display_currency": website_sale_order.currency_id}'/>
<span t-attf-class="#{badge_class} o_delivery_compute">Select to compute delivery rate</span>
</t>
<t t-if="delivery.website_description">

0 comments on commit 0c51273

Please sign in to comment.
You can’t perform that action at this time.