Skip to content

Commit

Permalink
[ADD] add new module to display discount amount on sale orders
Browse files Browse the repository at this point in the history
  • Loading branch information
Cédric Pigeon authored and chafique-delli committed Jan 25, 2021
1 parent f559f99 commit d069891
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 0 deletions.
1 change: 1 addition & 0 deletions sale_discount_display_amount/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file is going to be generated by oca-gen-addon-readme.
3 changes: 3 additions & 0 deletions sale_discount_display_amount/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import models
from .hooks import pre_init_hook
from .hooks import post_init_hook
22 changes: 22 additions & 0 deletions sale_discount_display_amount/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Copyright 2018 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
'name': 'Sale Discount Display Amount',
'summary': """
This addon intends to display the amount of the discount computed on
sale_order_line and sale_order level""",
'version': '10.0.1.0.0',
'license': 'AGPL-3',
'author': 'ACSONE SA/NV,Odoo Community Association (OCA)',
'website': 'http://www.acsone.eu',
'depends': [
'sale'
],
'data': [
'views/sale_view.xml'
],
'pre_init_hook': 'pre_init_hook',
'post_init_hook': 'post_init_hook',
}
53 changes: 53 additions & 0 deletions sale_discount_display_amount/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
# Copyright 2018 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
from odoo.api import Environment
from odoo import SUPERUSER_ID

_logger = logging.getLogger(__name__)


def pre_init_hook(cr):
_logger.info("Create discount columns in database")
cr.execute("""
ALTER TABLE sale_order ADD COLUMN price_total_no_discount numeric;
""")
cr.execute("""
ALTER TABLE sale_order ADD COLUMN discount_total numeric;
""")
cr.execute("""
ALTER TABLE sale_order_line ADD COLUMN price_total_no_discount
numeric;
""")
cr.execute("""
ALTER TABLE sale_order_line ADD COLUMN discount_total numeric;
""")


def post_init_hook(cr, registry):
_logger.info("Compute discount columns")
env = Environment(cr, SUPERUSER_ID, {})

query = """
update sale_order_line
set price_total_no_discount = price_total
where discount = 0.0
"""
cr.execute(query)

query = """
update sale_order
set price_total_no_discount = amount_total
"""
cr.execute(query)

query = """
select distinct order_id from sale_order_line where discount > 0.0;
"""

cr.execute(query)
order_ids = cr.fetchall()

orders = env['sale.order'].search([('id', 'in', order_ids)])
orders.mapped('order_line')._compute_discount()
2 changes: 2 additions & 0 deletions sale_discount_display_amount/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import sale_order
from . import sale_order_line
32 changes: 32 additions & 0 deletions sale_discount_display_amount/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
# Copyright 2018 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models


class SaleOrder(models.Model):

_inherit = 'sale.order'

discount_total = fields.Monetary(
compute='_compute_discount',
string='Discount Subtotal',
readonly=True,
store=True)
price_total_no_discount = fields.Monetary(
compute='_compute_discount',
string='Subtotal Without Discount',
readonly=True,
store=True)

@api.depends('order_line.discount_total', 'order_line.discount_total')
def _compute_discount(self):
for order in self:
discount_total = sum(order.order_line.mapped('discount_total'))
price_total_no_discount = sum(
order.order_line.mapped('price_total_no_discount'))
order.update({
'discount_total': discount_total,
'price_total_no_discount': price_total_no_discount
})
48 changes: 48 additions & 0 deletions sale_discount_display_amount/models/sale_order_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
# Copyright 2018 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models


class SaleOrderLine(models.Model):

_inherit = 'sale.order.line'

discount_total = fields.Monetary(
compute='_compute_amount',
string='Discount Subtotal',
readonly=True,
store=True)
price_total_no_discount = fields.Monetary(
compute='_compute_amount',
string='Subtotal Without Discount',
readonly=True,
store=True)

def _compute_discount(self):
for line in self:
if not line.discount:
line.price_total_no_discount = line.price_total
continue
price = line.price_unit
taxes = line.tax_id.compute_all(
price,
line.order_id.currency_id,
line.product_uom_qty,
product=line.product_id,
partner=line.order_id.partner_shipping_id)

price_total_no_discount = taxes['total_included']
discount_total = price_total_no_discount - line.price_total

line.update({
'discount_total': discount_total,
'price_total_no_discount': price_total_no_discount
})

@api.depends('product_uom_qty', 'discount', 'price_unit', 'tax_id')
def _compute_amount(self):
res = super(SaleOrderLine, self)._compute_amount()
self._compute_discount()
return res
3 changes: 3 additions & 0 deletions sale_discount_display_amount/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
To configure this module, you need to:

#. Go to Sales/Settings and check "Allow discounts on sales order lines"
1 change: 1 addition & 0 deletions sale_discount_display_amount/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Cédric Pigeon <[email protected]>
4 changes: 4 additions & 0 deletions sale_discount_display_amount/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
In standard Odoo only display the rate of the discount applied, never the
amount. It could be great to be able to tell the customer how much he spares.
This is the goal of this addons, it will show on a sale
order the total without the discount and the value of the discount.
6 changes: 6 additions & 0 deletions sale_discount_display_amount/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

To use this module, you need to:

#. Go on a sale order
#. Set a discount on a line
#. The value of the discount is dislayed in the total section as well as the total without it.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions sale_discount_display_amount/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_discount_display_amount
31 changes: 31 additions & 0 deletions sale_discount_display_amount/tests/test_discount_display_amount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# Copyright 2018 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo.tests.common import TransactionCase


class TestDiscountDisplay(TransactionCase):

def test_sale_discount_value(self):
product1 = self.env['product.product'].create(
{'name': 'Product TEST',
'type': 'consu'})
customer = self.env['res.partner'].create(
{"name": "Customer TEST",
"is_company": False,
"email": "[email protected]"})
so = self.env["sale.order"].create(
{"partner_id": customer.id})
self.env['sale.order.line'].create(
{'order_id': so.id,
'product_id': product1.id,
'price_unit': 30.75})

first_line = so.order_line[0]
first_line.discount = 10

self.assertAlmostEqual(first_line.price_total_no_discount, 35.36)
self.assertAlmostEqual(first_line.discount_total, 3.53)
self.assertAlmostEqual(so.discount_total, 3.53)
self.assertAlmostEqual(so.price_total_no_discount, 35.36)
17 changes: 17 additions & 0 deletions sale_discount_display_amount/views/sale_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<odoo>
<record id="sale_order_view_form_inherit_abi_product" model="ir.ui.view">
<field name="name">sale.order.form (abi_sale)</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="priority">99</field>
<field name="arch" type="xml">
<data>
<xpath expr="//field[@name='amount_tax']" position="after">
<field name="price_total_no_discount" widget='monetary' options="{'currency_field': 'currency_id'}"/>
<field name="discount_total" widget='monetary' options="{'currency_field': 'currency_id'}"/>
</xpath>
</data>
</field>
</record>
</odoo>

0 comments on commit d069891

Please sign in to comment.