Skip to content

Commit

Permalink
[MIG] account_invoice_fixed_discount: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PieterPaulussen committed Aug 10, 2023
1 parent 9005493 commit 85b6c40
Show file tree
Hide file tree
Showing 10 changed files with 178 additions and 177 deletions.
8 changes: 6 additions & 2 deletions account_invoice_fixed_discount/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
{
"name": "Account Fixed Discount",
"summary": "Allows to apply fixed amount discounts in invoices.",
"version": "15.0.1.0.0",
"version": "16.0.1.0.0",
"category": "Accounting & Finance",
"website": "https://github.com/OCA/account-invoicing",
"author": "ForgeFlow, Odoo Community Association (OCA)",
"license": "AGPL-3",
"application": False,
"installable": True,
"depends": ["account"],
"data": ["views/account_move_view.xml", "reports/report_account_invoice.xml"],
"data": [
"security/res_groups.xml",
"views/account_move_view.xml",
"reports/report_account_invoice.xml",
],
}
2 changes: 1 addition & 1 deletion account_invoice_fixed_discount/models/__init__.py
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
124 changes: 0 additions & 124 deletions account_invoice_fixed_discount/models/account_move.py

This file was deleted.

78 changes: 78 additions & 0 deletions account_invoice_fixed_discount/models/account_move_line.py
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
1 change: 1 addition & 0 deletions account_invoice_fixed_discount/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
* Jordi Ballester <[email protected]>
* Rattapong Chokmasermkul <[email protected]>
* Kitti U. <[email protected]>
* Pieter Paulussen <[email protected]>
2 changes: 0 additions & 2 deletions account_invoice_fixed_discount/readme/ROADMAP.rst

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
t-value="any([l.discount_fixed for l in o.invoice_line_ids])"
/>
</xpath>
<xpath expr="//th[@t-if='display_discount']" position="after">
<xpath expr="//th[@t-if='display_discount']" position="before">
<th
t-if="display_discount_fixed"
t-attf-class="text-right {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}"
t-attf-class="text-end {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}"
>
<span>Disc. Fixed Amount</span>
</th>
</xpath>
<xpath expr="//td[@t-if='display_discount']" position="after">
<xpath expr="//td[@t-if='display_discount']" position="before">
<td
t-if="display_discount_fixed"
t-attf-class="text-right {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}"
t-attf-class="text-end {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}"
>
<span t-field="line.discount_fixed" />
</td>
Expand Down
21 changes: 21 additions & 0 deletions account_invoice_fixed_discount/security/res_groups.xml
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>
Loading

0 comments on commit 85b6c40

Please sign in to comment.