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 11, 2023
1 parent 9005493 commit f5a9fd6
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 190 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",
],
}
3 changes: 2 additions & 1 deletion account_invoice_fixed_discount/models/__init__.py
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
124 changes: 0 additions & 124 deletions account_invoice_fixed_discount/models/account_move.py

This file was deleted.

85 changes: 85 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,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
49 changes: 49 additions & 0 deletions account_invoice_fixed_discount/models/account_tax.py
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
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.

33 changes: 18 additions & 15 deletions account_invoice_fixed_discount/reports/report_account_invoice.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@
t-value="any([l.discount_fixed for l in o.invoice_line_ids])"
/>
</xpath>
<xpath expr="//th[@t-if='display_discount']" position="after">
<th
t-if="display_discount_fixed"
t-attf-class="text-right {{ '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">
<td
t-if="display_discount_fixed"
t-attf-class="text-right {{ 'd-none d-md-table-cell' if report_type == 'html' else '' }}"
>
<span t-field="line.discount_fixed" />
</td>
<xpath expr="//th[@t-if='display_discount']/span" position="replace">
<t t-if="display_discount_fixed">
<span>Discount Amount (%)</span>
</t>
<t t-else="">
<span>Disc. %</span>
</t>
</xpath>
<span t-field="line.discount" position="replace">
<t t-if="display_discount_fixed">
<span class="text-nowrap" t-field="line.discount_fixed" /> (or <span
class="text-nowrap"
t-field="line.discount"
/> %)
</t>
<t t-else="">
<span class="text-nowrap" t-field="line.discount" />
</t>
</span>
</template>
</odoo>
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 f5a9fd6

Please sign in to comment.