Skip to content
Permalink
Browse files

Split compatibility layer and business layer

  • Loading branch information...
kebeclibre committed Apr 18, 2019
1 parent e4df44c commit 841177d48d61c0ddbc2d3b0c8feb8321195ae550
Showing with 74 additions and 40 deletions.
  1. +74 −40 addons/website_quote/static/src/js/website_quotation.js
@@ -23,7 +23,7 @@ if(!$('.o_website_quote').length) {
var token = href.match(/token=([\w\d-]*)/)[1];

var callParams = {
'line_id': line_id,
'line_id': parseInt(line_id),
'order_id': parseInt(order_id),
'token': token,
'remove': this.$el.is('[href*="remove"]'),
@@ -33,9 +33,12 @@ if(!$('.o_website_quote').length) {
return false;
},
/**
* Only for compatibily reasons
* when server is not restarted
* and the route update_line_dict doesn't exist
* Calls the route to get updated values of the line and order
* when the quantity of a product has changed
*
* @private
* @params {dict} params
* @return {Deferred}
*/
_callUpdateLineRoute: function (params) {
var def = new $.Deferred();
@@ -45,69 +48,100 @@ if(!$('.o_website_quote').length) {
// Compatibility: the server may not have been restarted
// So the real route may not exist
ajax.jsonRpc("/quote/update_line", 'call', params)
.then(def.resolve.bind(def))
.fail(def.reject.bind(def));
.then(function (data) {
// Data is an array, convert it to a dict
var actualData = data;
if (data) {
actualData = {
order_amount_total: data[1],
order_line_product_uom_qty: data[0],
};
}
def.resolve(actualData);
});
});
return def;
},
/**
* Processes data from the server to update the UI
* @private
* @param {dict} data: contains order and line updated values
*/
_updateOrderValues: function (data) {
if(!data){
location.reload();
if (!data) {
window.location.reload();
}

// Compatibility: the other route may have been called
var orderAmountTotal = Array.isArray(data) ? data[1] : data.order_amount_total;
var orderAmountTotal = data.order_amount_total;
var orderAmountUntaxed = data.order_amount_untaxed;
var orderAmountTax = data.order_amount_tax;

// Compatibility: the other route may have been called
var lineProductUomQty = Array.isArray(data) ? data[0] : data.order_line_product_uom_qty;
var lineProductUomQty = data.order_line_product_uom_qty;
var linePriceTotal = data.order_line_price_total;
var linePriceSubTotal = data.order_line_price_subtotal;

this.$el.parents('.input-group:first').find('.js_quantity').val(lineProductUomQty);
var elems = this._getUpdatableElements();

var $parentTr = this.$el.parents('tr:first');
var $elPriceTotal = $parentTr.find('.oe_order_line_price_total .oe_currency_value');
var $elPriceSubTotal = $parentTr.find('.oe_order_line_price_subtotal .oe_currency_value');
elems.$lineQuantity.val(lineProductUomQty)

// Compatibility: the module is not updgraded
// so classes in DOM are not present
if ( !$elPriceTotal.length && !$elPriceSubTotal.length ) {
$elPriceTotal = $elPriceSubTotal = $parentTr.find('.oe_currency_value').last();
if (elems.$linePriceTotal.length && linePriceTotal !== undefined) {
elems.$linePriceTotal.text(linePriceTotal);
}
if (elems.$linePriceSubTotal.length && linePriceSubTotal !== undefined) {
elems.$linePriceSubTotal.text(linePriceSubTotal);
}

if ($elPriceTotal.length && linePriceTotal) {
$elPriceTotal.text(linePriceTotal);
if (orderAmountUntaxed !== undefined) {
elems.$orderAmountUntaxed.text(orderAmountUntaxed);
}
if ($elPriceSubTotal.length && linePriceSubTotal) {
$elPriceSubTotal.text(linePriceSubTotal);

if (orderAmountTax !== undefined) {
// Compatibility: data-id may not be correct
elems.$orderAmountTax.text(orderAmountTax);
}

var $elTotalUntaxed = $('[data-id="total_untaxed"]>span');
var $elAmountTax = $('[data-id="total_taxes"]>span');
var $elAmountTotal = $('[data-id="total_amount"]>span');
if (orderAmountTotal !== undefined) {
elems.$orderAmountTotal.text(orderAmountTotal);
}
},
/**
* Locate in the DOM the elements to update
* Mostly for compatibility, when the module has not been upgraded
* In that case, we need to fall back to some other elements
*
* @private
* @return {dict}: Jquery elements to update
*/
_getUpdatableElements: function () {
var $parentTr = this.$el.parents('tr:first');
var $linePriceTotal = $parentTr.find('.oe_order_line_price_total .oe_currency_value');
var $linePriceSubTotal = $parentTr.find('.oe_order_line_price_subtotal .oe_currency_value');

var $elWrite;
if (orderAmountUntaxed) {
// Compatibility: data-id may not be correct
$elWrite = $elTotalUntaxed.length ? $elTotalUntaxed : $elAmountTotal.eq(1);
$elWrite.text(orderAmountUntaxed);
if (!$elPriceTotal.length && !$elPriceSubTotal.length) {
$linePriceTotal = $linePriceSubTotal = $parentTr.find('.oe_currency_value').last();
}

if (orderAmountTax) {
// Compatibility: data-id may not be correct
$elWrite = $elAmountTax.length ? $elAmountTax : $elAmountTotal.eq(2);
$elWrite.text(orderAmountTax);
var $orderTotalUntaxed = $('[data-id="total_untaxed"]>span');
var $orderAmountTax = $('[data-id="total_taxes"]>span');
var $orderAmountTotal = $('[data-id="total_amount"]>span');

if (!$orderTotalUntaxed.length && !$orderAmountTax.length) {
$orderTotalUntaxed = $orderAmountTotal.eq(1);
$orderAmountTax = $orderAmountTotal.eq(2);
$orderAmountTotal: $orderAmountTotal.eq(0).add($orderAmountTotal.eq(3));
}

if (orderAmountTotal) {
// Compatibility: data-id may not be correct
// In this case, there are two elements that contains total_amount
$elWrite = $elAmountTax.length ? $elAmountTotal : $elAmountTotal.eq(3).add($elAmountTotal.eq(0));
$elWrite.text(orderAmountTotal);
return {
$lineQuantity: this.$el.parents('.input-group:first').find('.js_quantity'),
$linePriceSubTotal: $linePriceSubTotal,
$linePriceTotal: $linePriceTotal,
$orderTotalUntaxed: $orderTotalUntaxed,
$orderAmountTax: $orderAmountTax,
$orderAmountTotal: $orderAmountTotal,
}
}

});

var update_button_list = [];

0 comments on commit 841177d

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