-
-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MIG] account_invoice_fixed_discount: Migration to 16.0
- Loading branch information
1 parent
9005493
commit f5a9fd6
Showing
11 changed files
with
269 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) | ||
|
||
from . import account_move | ||
from . import account_move_line | ||
from . import account_tax |
This file was deleted.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
account_invoice_fixed_discount/models/account_move_line.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Copyright 2017 ForgeFlow S.L. | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) | ||
|
||
from odoo import api, fields, models | ||
from odoo.tools.float_utils import float_is_zero | ||
|
||
|
||
class AccountMoveLine(models.Model): | ||
_inherit = "account.move.line" | ||
|
||
discount_fixed = fields.Monetary( | ||
string="Discount (Fixed)", | ||
default=0.0, | ||
currency_field="currency_id", | ||
help=( | ||
"Apply a fixed amount discount to this line. The amount is multiplied by " | ||
"the quantity of the product." | ||
), | ||
) | ||
|
||
@api.depends("quantity", "discount", "price_unit", "tax_ids", "currency_id") | ||
def _compute_totals(self): | ||
"""Adjust the computation of the price_subtotal and price_total fields to | ||
account for the fixed discount amount. | ||
By using the unrounded calculated discount value, we avoid rounding errors | ||
in the resulting calculated totals. | ||
We only need to do this for lines with a fixed discount. | ||
""" | ||
done_lines = self.env["account.move.line"] | ||
for line in self: | ||
if float_is_zero( | ||
line.discount_fixed, precision_rounding=line.currency_id.rounding | ||
): | ||
continue | ||
# Pass the actual float value of the discount to the tax computation method. | ||
discount = line._get_discount_from_fixed_discount() | ||
line_discount_price_unit = line.price_unit * (1 - (discount / 100.0)) | ||
if line.tax_ids: | ||
taxes_res = line.tax_ids.compute_all( | ||
line_discount_price_unit, | ||
quantity=line.quantity, | ||
currency=line.currency_id, | ||
product=line.product_id, | ||
partner=line.partner_id, | ||
is_refund=line.is_refund, | ||
) | ||
line.price_subtotal = taxes_res["total_excluded"] | ||
line.price_total = taxes_res["total_included"] | ||
else: | ||
# No taxes applied on the line. | ||
subtotal = line.quantity * line_discount_price_unit | ||
line.price_total = line.price_subtotal = subtotal | ||
|
||
done_lines |= line | ||
|
||
# Compute the regular totals for regular lines. | ||
return super(AccountMoveLine, self - done_lines)._compute_totals() | ||
|
||
@api.onchange("discount_fixed") | ||
def _onchange_discount_fixed(self): | ||
"""Compute the fixed discount based on the discount percentage.""" | ||
if self.env.context.get("ignore_discount_onchange"): | ||
return | ||
self.env.context = self.with_context(ignore_discount_onchange=True).env.context | ||
self.discount = self._get_discount_from_fixed_discount() | ||
|
||
@api.onchange("discount") | ||
def _onchange_discount(self): | ||
"""Compute the discount percentage based on the fixed discount. | ||
Ignore the onchange if the fixed discount is already set. | ||
""" | ||
if self.env.context.get("ignore_discount_onchange"): | ||
return | ||
self.env.context = self.with_context(ignore_discount_onchange=True).env.context | ||
self.discount_fixed = 0.0 | ||
|
||
def _get_discount_from_fixed_discount(self): | ||
"""Calculate the discount percentage from the fixed discount amount.""" | ||
self.ensure_one() | ||
currency = self.currency_id or self.company_id.currency_id | ||
if float_is_zero(self.discount_fixed, precision_rounding=currency.rounding): | ||
return 0.0 | ||
return (self.discount_fixed / self.price_unit) * 100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright 2017 ForgeFlow S.L. | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) | ||
|
||
from odoo import api, models | ||
|
||
|
||
class AccountTax(models.Model): | ||
_inherit = "account.tax" | ||
|
||
@api.model | ||
def _convert_to_tax_base_line_dict( | ||
self, | ||
base_line, | ||
partner=None, | ||
currency=None, | ||
product=None, | ||
taxes=None, | ||
price_unit=None, | ||
quantity=None, | ||
discount=None, | ||
account=None, | ||
analytic_distribution=None, | ||
price_subtotal=None, | ||
is_refund=False, | ||
rate=None, | ||
handle_price_include=True, | ||
extra_context=None, | ||
): | ||
"""Insert the un-rounded discount value in the resulting tax computation values.""" | ||
res = super()._convert_to_tax_base_line_dict( | ||
base_line, | ||
partner=partner, | ||
currency=currency, | ||
product=product, | ||
taxes=taxes, | ||
price_unit=price_unit, | ||
quantity=quantity, | ||
discount=discount, | ||
account=account, | ||
analytic_distribution=analytic_distribution, | ||
price_subtotal=price_subtotal, | ||
is_refund=is_refund, | ||
rate=rate, | ||
handle_price_include=handle_price_include, | ||
extra_context=extra_context, | ||
) | ||
if base_line.discount_fixed: | ||
res["discount"] = base_line._get_discount_from_fixed_discount() | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
* Jordi Ballester <[email protected]> | ||
* Rattapong Chokmasermkul <[email protected]> | ||
* Kitti U. <[email protected]> | ||
* Pieter Paulussen <[email protected]> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<odoo> | ||
<data> | ||
|
||
<record id="group_fixed_discount" model="res.groups"> | ||
<field name="name">Fixed Discount</field> | ||
</record> | ||
|
||
</data> | ||
|
||
<data noupdate="1"> | ||
<record id="account.group_account_invoice" model="res.groups"> | ||
<field | ||
name="implied_ids" | ||
eval="[(4, ref('account_invoice_fixed_discount.group_fixed_discount'))]" | ||
/> | ||
</record> | ||
</data> | ||
|
||
|
||
</odoo> |
Oops, something went wrong.