Skip to content

Commit

Permalink
[MIG] account_invoice_triple_discount to v13
Browse files Browse the repository at this point in the history
  • Loading branch information
benwillig authored and Cédric Pigeon committed Jan 29, 2021
1 parent eb78f9e commit 1adcb23
Show file tree
Hide file tree
Showing 17 changed files with 352 additions and 261 deletions.
72 changes: 0 additions & 72 deletions account_invoice_triple_discount/README.rst

This file was deleted.

1 change: 0 additions & 1 deletion account_invoice_triple_discount/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

from . import models
26 changes: 10 additions & 16 deletions account_invoice_triple_discount/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@
# Copyright 2017 Tecnativa - David Vidal
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
'name': 'Account Invoice Triple Discount',
'version': '12.0.1.0.0',
'category': 'Accounting & Finance',
'author': 'QubiQ,'
'Tecnativa,'
'Odoo Community Association (OCA)',
'website': 'https://github.com/OCA/account-invoicing',
'license': 'AGPL-3',
'summary': 'Manage triple discount on invoice lines',
'depends': [
'account',
],
'data': [
'views/account_invoice_view.xml',
],
'installable': True,
"name": "Account Invoice Triple Discount",
"version": "13.0.1.0.0",
"category": "Accounting & Finance",
"author": "QubiQ, Tecnativa, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/account-invoicing",
"license": "AGPL-3",
"summary": "Manage triple discount on invoice lines",
"depends": ["account"],
"data": ["report/invoice.xml", "views/account_move.xml"],
"installable": True,
}
5 changes: 2 additions & 3 deletions account_invoice_triple_discount/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import account_invoice
from . import account_move
from . import account_move_line
61 changes: 0 additions & 61 deletions account_invoice_triple_discount/models/account_invoice.py

This file was deleted.

41 changes: 41 additions & 0 deletions account_invoice_triple_discount/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2017 Tecnativa - David Vidal
# Copyright 2017 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models


class AccountMove(models.Model):

_inherit = "account.move"

def _recompute_tax_lines(self, **kwargs):
"""
As the taxes are recalculated based on a single discount, we need to
simulate a multiple discount by changing the unit price. Values are
restored after the original process is done
"""
old_values_by_line_id = {}
for line in self.line_ids:
aggregated_discount = line._compute_aggregated_discount(line.discount)
old_values_by_line_id[line.id] = {
"price_unit": line.price_unit,
"discount": line.discount,
}
price_unit = line.price_unit * (1 - aggregated_discount / 100)
line.update({"price_unit": price_unit, "discount": 0})
res = super(AccountMove, self)._recompute_tax_lines(**kwargs)
for line in self.line_ids:
if line.id not in old_values_by_line_id:
continue
line.update(old_values_by_line_id[line.id])
return res

def _has_discount(self):
self.ensure_one()
return any(
[
line._compute_aggregated_discount(line.discount) > 0
for line in self.invoice_line_ids
]
)
96 changes: 96 additions & 0 deletions account_invoice_triple_discount/models/account_move_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Copyright 2020 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import functools

from odoo import api, fields, models
from odoo.tools import float_compare


class AccountMoveLine(models.Model):

_inherit = "account.move.line"

discount2 = fields.Float(string="Discount 2 (%)", digits="Discount",)
discount3 = fields.Float(string="Discount 3 (%)", digits="Discount",)

@api.model_create_multi
def create(self, values_list):
"""
During the create of move lines, if the system detect that there is a
difference between the balance and the price subtotal, it will update
the unit price. When computing those, Odoo base module use a single
discount on creation. So as there is a difference of the price given
by the UI and the price computed during create method, the system will
change the unit price of the invoice line. To avoid that, we update
the discount field to have the aggregated discount, and we change it
back after the creation.
(Similar to _recompute_tax_lines on account.move)
"""
old_values = []
dp_discount = self.env["decimal.precision"].precision_get("Discount")
for values in values_list:
old_discount = values.get("discount", 0.0)
new_discount = self._get_aggregated_discount_from_values(values)
tmp_values = {}
discount_changed = (
float_compare(old_discount, new_discount, precision_digits=dp_discount)
!= 0
)
if discount_changed:
values["discount"] = new_discount
tmp_values["discount"] = old_discount
old_values.append(tmp_values)
records = super(AccountMoveLine, self).create(values_list)
for index, record in enumerate(records):
values = old_values[index]
if values:
record.write(old_values[index])
return records

@api.onchange(
"discount", "price_unit", "tax_ids", "quantity", "discount2", "discount3",
)
def _onchange_price_subtotal(self):
return super(AccountMoveLine, self)._onchange_price_subtotal()

def _get_price_total_and_subtotal(self, **kwargs):
self.ensure_one()
kwargs["discount"] = self._compute_aggregated_discount(
kwargs.get("discount") or self.discount
)
return super(AccountMoveLine, self)._get_price_total_and_subtotal(**kwargs)

def _get_fields_onchange_balance(self, **kwargs):
self.ensure_one()
kwargs["discount"] = self._compute_aggregated_discount(
kwargs.get("discount") or self.discount
)
return super(AccountMoveLine, self)._get_fields_onchange_balance(**kwargs)

def _compute_aggregated_discount(self, base_discount):
self.ensure_one()
discounts = [base_discount]
for discount_fname in self._get_multiple_discount_field_names():
discounts.append(getattr(self, discount_fname, 0.0))
return self._get_aggregated_multiple_discounts(discounts)

def _get_aggregated_discount_from_values(self, values):
discount_fnames = ["discount"]
discount_fnames.extend(self._get_multiple_discount_field_names())
discounts = []
for discount_fname in discount_fnames:
discounts.append(values.get(discount_fname) or 0.0)
return self._get_aggregated_multiple_discounts(discounts)

def _get_aggregated_multiple_discounts(self, discounts):
discount_values = []
for discount in discounts:
discount_values.append(1 - (discount or 0.0) / 100.0)
aggregated_discount = (
1 - functools.reduce((lambda x, y: x * y), discount_values)
) * 100
return aggregated_discount

def _get_multiple_discount_field_names(self):
return ["discount2", "discount3"]
3 changes: 3 additions & 0 deletions account_invoice_triple_discount/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* David Vidal <david.vidal@tecnativa.com>
* Pedro M. Baeza <pedro.baeza@tecnativa.com>
* Nikul Chaudhary <nikulchaudhary2112@gmail.com>
1 change: 1 addition & 0 deletions account_invoice_triple_discount/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module allows to have three successive discounts on each invoice line.
17 changes: 17 additions & 0 deletions account_invoice_triple_discount/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Create a new invoice and add discounts in any of the three discount fields
given. They go in order of precedence so discount 2 will be calculated over
discount 1 and discount 3 over the result of discount 2. For example, let's
divide by two on every discount:

Unit price: 600.00 ->

- Disc. 1 = 50% -> Amount = 300.00
- Disc. 2 = 50% -> Amount = 150.00
- Disc. 3 = 50% -> Amount = 75.00

You can also use negative values to charge instead of discount:

Unit price: 600.00 ->

- Disc. 1 = 50% -> Amount = 300.00
- Disc. 2 = -5% -> Amount = 315.00
38 changes: 38 additions & 0 deletions account_invoice_triple_discount/report/invoice.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<template id="report_invoice_document" inherit_id="account.report_invoice_document">
<xpath expr="//t[@t-set='display_discount']" position="replace">
<t t-set="display_discount" t-value="o._has_discount()" />
<t
t-set="discount_class"
t-value="'text-right %s' % ('d-none d-md-table-cell' if report_type == 'html' else '')"
/>
</xpath>
<xpath expr="//th[@name='th_price_unit']" position="after">
<th
name="th_discount2"
t-if="display_discount"
t-att-class="discount_class"
>
<span>Disc.2 %</span>
<t t-set="colspan" t-value="colspan+1" />
</th>
<th
name="th_discount3"
t-if="display_discount"
t-att-class="discount_class"
>
<span>Disc.3 %</span>
<t t-set="colspan" t-value="colspan+1" />
</th>
</xpath>
<xpath expr="//td[span[@t-field='line.discount']]" position="after">
<td t-if="display_discount" t-att-class="discount_class">
<span class="text-nowrap" t-field="line.discount2" />
</td>
<td t-if="display_discount" t-att-class="discount_class">
<span class="text-nowrap" t-field="line.discount3" />
</td>
</xpath>
</template>
</odoo>
Loading

0 comments on commit 1adcb23

Please sign in to comment.