Skip to content

Commit

Permalink
[IMP] account_global_discount: add fixed discount support
Browse files Browse the repository at this point in the history
  • Loading branch information
primes2h committed Feb 5, 2024
1 parent 837d6d6 commit 5079ded
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 10 deletions.
54 changes: 45 additions & 9 deletions account_global_discount/models/account_invoice.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Copyright 2019 Tecnativa - David Vidal
# Copyright 2020 Tecnativa - Pedro M. Baeza
# Copyright 2022 Simone Rubino - TAKOBI
# Copyright 2024 Sergio Zanchetta - PNLUG APS
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import _, api, exceptions, fields, models
from odoo.addons import decimal_precision as dp
Expand Down Expand Up @@ -29,7 +30,15 @@ class AccountInvoice(models.Model):
('total', 'Total'),
],
string='Discount Base',
compute='_compute_global_discount_base',
compute='_compute_global_discount_selection',
)
global_discount_type = fields.Selection(
selection=[
('percentage', 'Percentage'),
('fixed', 'Fixed'),
],
string='Discount Type',
compute='_compute_global_discount_selection',
)
# HACK: Looks like UI doesn't behave well with Many2many fields and
# negative groups when the same field is shown. In this case, we want to
Expand Down Expand Up @@ -66,12 +75,13 @@ class AccountInvoice(models.Model):

@api.multi
@api.depends('global_discount_ids')
def _compute_global_discount_base(self):
def _compute_global_discount_selection(self):
for invoice in self:
# Only check first because sanity checks
# assure all global discounts in same invoice have same base
first_global_discount = first(invoice.global_discount_ids)
invoice.global_discount_base = first_global_discount.discount_base
invoice.global_discount_type = first_global_discount.discount_type

def _set_global_discounts_by_tax(self):
"""Create invoice global discount lines by taxes combinations and
Expand Down Expand Up @@ -128,7 +138,9 @@ def _set_global_discounts_by_tax(self):
'name': global_discount.display_name,
'invoice_id': self.id,
'global_discount_id': global_discount.id,
'discount_type': global_discount.discount_type,
'discount': global_discount.discount,
'discount_fixed': global_discount.discount_fixed,
'base': base,
'base_discounted': discount['base_discounted'],
'account_id': global_discount.account_id.id,
Expand All @@ -143,7 +155,9 @@ def _set_global_discounts_by_tax(self):
'name': global_discount.display_name,
'invoice_id': self.id,
'global_discount_id': global_discount.id,
'discount_type': global_discount.discount_type,
'discount': global_discount.discount,
'discount_fixed': global_discount.discount_fixed,
'base': base,
'base_discounted': discount['base_discounted'],
'account_id': global_discount.account_id.id,
Expand Down Expand Up @@ -238,18 +252,21 @@ def get_taxes_values(self):
for discount in self.global_discount_ids:
base = discount._get_global_discount_vals(
base)['base_discounted']
amount = discount._get_global_discount_vals(
amount)['base_discounted']
if discount.discount_type == 'percentage':
amount = discount._get_global_discount_vals(
amount)['base_discounted']
tax_grouped[key]['base'] = round_curr(base)
tax_grouped[key]['amount'] = round_curr(amount)

return tax_grouped

@api.model
def invoice_line_move_line_get(self):
"""Append global discounts move lines"""
res = super().invoice_line_move_line_get()

for discount in self.invoice_global_discount_ids:
if not discount.discount:
if not (discount.discount or discount.discount_fixed):
continue
# Traverse upstream result for taking existing dictionary vals
inv_lines = self.invoice_line_ids.filtered(
Expand Down Expand Up @@ -303,10 +320,23 @@ class AccountInvoiceGlobalDiscount(models.Model):
string='Global Discount',
readonly=True,
)
discount_type = fields.Selection(
selection=[
('percentage', 'Percentage'),
('fixed', 'Fixed'),
],
string='Discount Type',
readonly=True,
)
discount = fields.Float(
string='Discount (number)',
readonly=True,
)
discount_fixed = fields.Float(
string='Discount (number)',
currency_field='currency_id',
readonly=True,
)
discount_display = fields.Char(
compute='_compute_discount_display',
readonly=True,
Expand All @@ -327,7 +357,7 @@ class AccountInvoiceGlobalDiscount(models.Model):
readonly=True,
)
discount_amount = fields.Monetary(
string='Discounted Amount',
string='Discount Amount',
compute='_compute_discount_amount',
currency_field='currency_id',
readonly=True,
Expand All @@ -352,10 +382,16 @@ class AccountInvoiceGlobalDiscount(models.Model):

def _compute_discount_display(self):
"""Given a discount type, we need to render a different symbol"""

for one in self:
precision = self.env['decimal.precision'].precision_get('Discount')
one.discount_display = '{0:.{1}f}%'.format(
one.discount * -1, precision)
if one.discount_type == 'percentage':
precision = self.env['decimal.precision'].precision_get('Discount')
one.discount_display = '{0:.{1}f}%'.format(
one.discount * -1, precision)
elif one.discount_type == 'fixed':
precision = self.env['decimal.precision'].precision_get('Product Price')
one.discount_display = '{0:.{1}f}'.format(

Check warning on line 393 in account_global_discount/models/account_invoice.py

View check run for this annotation

Codecov / codecov/patch

account_global_discount/models/account_invoice.py#L392-L393

Added lines #L392 - L393 were not covered by tests
one.discount_fixed * -1, precision)

@api.depends('base', 'base_discounted')
def _compute_discount_amount(self):
Expand Down
3 changes: 3 additions & 0 deletions account_global_discount/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
* `TAKOBI <https://takobi.online>`_:

* Simone Rubino <[email protected]>
* `PNLUG APS <https://www.pnlug.it>`_:

* Sergio Zanchetta
3 changes: 3 additions & 0 deletions account_global_discount/views/account_invoice_views.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2019 Tecnativa - David Vidal
Copyright 2022 Simone Rubino - TAKOBI
Copyright 2024 Sergio Zanchetta - PNLUG APS
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>

Expand Down Expand Up @@ -28,7 +29,9 @@
<field name="name"/>
<field name="currency_id" invisible="1"/>
<field name="global_discount_id" invisible="1" force_save="1"/>
<field name="discount_type" invisible="1" force_save="1"/>
<field name="discount" invisible="1" force_save="1"/>
<field name="discount_fixed" invisible="1" force_save="1"/>
<field name="base" widget="monetary" options="{'currency_field': 'currency_id'}" force_save="1"/>
<field name="discount_display"/>
<field name="discount_amount"/>
Expand Down
8 changes: 7 additions & 1 deletion account_global_discount/views/report_account_invoice.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2022 Simone Rubino - TAKOBI
Copyright 2024 Sergio Zanchetta - PNLUG APS
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>

Expand Down Expand Up @@ -34,7 +35,12 @@
</tr>
<tr>
<td><strong>Global Discounts</strong><br />
<t t-esc="'→'.join(['{:.2f}%'.format(x.discount) for x in o.global_discount_ids])"/></td>
<t t-foreach="o.global_discount_ids" t-as="global_discount_id">
<t t-if="global_discount_id.discount_type == 'percentage'">
<t t-esc="'→'.join(['{:.2f}%'.format(global_discount_id.discount)])"/>
</t>
</t>
</td>
<td class="text-right">
<span t-field="o.amount_global_discount"
t-options='{"widget": "monetary", "display_currency": o.currency_id}'/>
Expand Down
2 changes: 2 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# https://github.com/OCA/server-backend/pull/264
git+https://github.com/OCA/server-backend@refs/pull/264/head#subdirectory=setup/base_global_discount

0 comments on commit 5079ded

Please sign in to comment.