[IMP] sale: Apply discount on all SO lines onchage of first SO line

Purpose fo the task is to Allow changing a discount for the whole SO.

So in this commit, On a quotation, with "discount per line" option
enabled, if the user changes the discount on the first SO line, display
a pop-up requesting if they want to apply the change to all the lines
of the SO.
There is condition to open the confirmation dialog to change on all SO lines:
  1. There should be 3 or more lines in SO
  2. Happens only when changing the discount of the first SO line
  3. The initial discount is the same on all lines
For Ex: Example : a SO with a discount of 10% on all 6 SO lines.
The user clicks the discount field on the first so line, changes to 20%.
A pop-up "apply this discount to all quotation lines"
-> if yes, apply 20% to all SO lines.

closes odoo/odoo#54262

Taskid: 2276759
Closes: #54262
Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
This commit is contained in:
Prakash Prajapati
2020-07-16 10:22:57 +00:00
committed by Barad Mahendra
parent 52b1b9c255
commit 696ea173d3
4 changed files with 97 additions and 2 deletions
@@ -0,0 +1,37 @@
odoo.define('sale.product_discount', function (require) {
"use strict";
const BasicFields = require('web.basic_fields');
const FieldsRegistry = require('web.field_registry');
/**
* The sale.product_discount widget is a simple widget extending FieldFloat
*
*
* !!! WARNING !!!
*
* This widget is only designed for sale_order_line creation/updates.
* !!! It should only be used on a discount field !!!
*/
const ProductDiscountWidget = BasicFields.FieldFloat.extend({
/**
* Override changes at a discount.
*
* @override
* @param {OdooEvent} ev
*
*/
async reset(record, ev) {
if (ev && ev.data.changes && ev.data.changes.discount >= 0) {
this.trigger_up('open_discount_wizard');
}
this._super(...arguments);
},
});
FieldsRegistry.add('product_discount', ProductDiscountWidget);
return ProductDiscountWidget;
});
@@ -0,0 +1,56 @@
odoo.define('sale.SaleOrderView', function (require) {
"use strict";
const FormController = require('web.FormController');
const FormView = require('web.FormView');
const viewRegistry = require('web.view_registry');
const Dialog = require('web.Dialog');
const core = require('web.core');
const _t = core._t;
const SaleOrderFormController = FormController.extend({
custom_events: _.extend({}, FormController.prototype.custom_events, {
open_discount_wizard: '_onOpenDiscountWizard',
}),
// -------------------------------------------------------------------------
// Handlers
// -------------------------------------------------------------------------
/**
* Handler called if user changes the discount field in the sale order line.
* The wizard will open only if
* (1) Sale order line is 3 or more
* (2) First sale order line is changed to discount
* (3) Discount is the same in all sale order line
*/
_onOpenDiscountWizard(ev) {
const orderLines = this.renderer.state.data.order_line.data;
const recordData = ev.target.recordData;
const isEqualDiscount = orderLines.slice(1).every(line => line.data.discount === recordData.discount);
if (orderLines.length >= 3 && recordData.sequence === orderLines[0].data.sequence && isEqualDiscount) {
Dialog.confirm(this, _t("Do you want to apply this discount to all order lines?"), {
confirm_callback: () => {
orderLines.slice(1).forEach((line) => {
this.trigger_up('field_changed', {
dataPointID: this.renderer.state.id,
changes: {order_line: {operation: "UPDATE", id: line.id, data: {discount: orderLines[0].data.discount}}},
});
});
},
});
}
},
});
const SaleOrderView = FormView.extend({
config: _.extend({}, FormView.prototype.config, {
Controller: SaleOrderFormController,
}),
});
viewRegistry.add('sale_discount_form', SaleOrderView);
return SaleOrderView;
});
+2
View File
@@ -9,6 +9,8 @@
<script type="text/javascript" src="/sale/static/src/js/sale.js"></script>
<script type="text/javascript" src="/sale/static/src/js/tours/sale.js"></script>
<script type="text/javascript" src="/sale/static/src/js/product_configurator_widget.js"/>
<script type="text/javascript" src="/sale/static/src/js/sale_order_view.js"/>
<script type="text/javascript" src="/sale/static/src/js/product_discount_widget.js"/>
</xpath>
</template>
<template id="report_assets_common_inherit_sale" inherit_id="web.report_assets_common" name="Sale report common assets">
+2 -2
View File
@@ -259,7 +259,7 @@
<field name="name">sale.order.form</field>
<field name="model">sale.order</field>
<field name="arch" type="xml">
<form string="Sales Order" class="o_sale_order">
<form string="Sales Order" class="o_sale_order" js_class="sale_discount_form">
<header>
<field name="authorized_transaction_ids" invisible="1"/>
<button name="payment_action_capture" type="object"
@@ -567,7 +567,7 @@
attrs="{'readonly': [('qty_invoiced', '&gt;', 0)]}"
optional="show"
/>
<field name="discount" string="Disc.%" groups="product.group_discount_per_so_line" optional="show"/>
<field name="discount" string="Disc.%" groups="product.group_discount_per_so_line" optional="show" widget="product_discount"/>
<field name="price_subtotal" widget="monetary" groups="account.group_show_line_subtotals_tax_excluded"/>
<field name="price_total" widget="monetary" groups="account.group_show_line_subtotals_tax_included"/>
<field name="state" invisible="1"/>