-
-
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 85b6c40
Showing
10 changed files
with
178 additions
and
177 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,3 @@ | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) | ||
|
||
from . import account_move | ||
from . import account_move_line |
This file was deleted.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
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,78 @@ | ||
# 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.exceptions import ValidationError | ||
from odoo.tools.float_utils import float_compare, float_is_zero, float_round | ||
|
||
|
||
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.constrains("discount_fixed", "discount") | ||
def _check_discounts(self): | ||
"""Check that the fixed discount and the discount percentage are consistent.""" | ||
for line in self: | ||
if line.discount_fixed and line.discount: | ||
calculated_fixed_discount = line._get_discount_from_fixed_discount() | ||
|
||
if ( | ||
float_compare( | ||
calculated_fixed_discount, | ||
line.discount, | ||
precision_rounding=line.currency_id.rounding, | ||
) | ||
!= 0 | ||
): | ||
raise ValidationError( | ||
_( | ||
"The fixed discount '%(fixed)s' does not match the calculated " | ||
"discount '%(discount)s %%'. Please correct one of the discounts." | ||
) | ||
% { | ||
"fixed": line.discount_fixed, | ||
"discount": line.discount, | ||
} | ||
) | ||
|
||
def _get_discount_from_fixed_discount(self): | ||
"""Calculate the discount percentage from the fixed discount amount.""" | ||
self.ensure_one() | ||
if not self.discount_fixed: | ||
return 0.0 | ||
currency = self.currency_id or self.company_id.currency_id | ||
return float_round( | ||
(self.discount_fixed / self.price_unit) * 100, | ||
precision_rounding=currency.rounding, | ||
) | ||
|
||
@api.onchange("discount_fixed", "quantity", "price_unit") | ||
def _onchange_discount_fixed(self): | ||
"""When the discount fixed is changed, we set the ``discount`` on the line to the | ||
appropriate value and apply the default downstream implementation. | ||
When the display_type is not ``'product'`` we reset the discount_fixed to 0.0. | ||
""" | ||
if self.display_type != "product" or not self.quantity or not self.price_unit: | ||
self.discount_fixed = 0.0 | ||
return | ||
|
||
self.discount = self._get_discount_from_fixed_discount() | ||
|
||
@api.onchange("discount") | ||
def _onchange_discount_reset_discount_fixed(self): | ||
"""WHen the discount is changed, we reset the discount_fixed to 0.0.""" | ||
currency = self.currency_id or self.company_id.currency_id | ||
if float_is_zero(self.discount, precision_rounding=currency.rounding): | ||
self.discount_fixed = 0.0 |
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.