Skip to content

Commit

Permalink
[FIX] account_global_discount: link line taxes to discount move line
Browse files Browse the repository at this point in the history
  • Loading branch information
chienandalu committed Jun 30, 2020
1 parent 30efc4a commit d2f508a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
7 changes: 7 additions & 0 deletions account_global_discount/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ You can assign global discounts to partners as well:
#. In section purchase (if the partner is a supplier), you can set purchase
discounts.

Known issues / Roadmap
======================

* Global Discount move lines are created for a common base amount. If that
wasn't the case, we should split discount move lines between bases and
assign proper taxes accordingly.

Bug Tracker
===========

Expand Down
33 changes: 20 additions & 13 deletions account_global_discount/models/account_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,32 @@ class AccountInvoice(models.Model):
inverse_name='invoice_id',
)

def _set_global_discounts(self):
"""Get global discounts in order and apply them in chain. They will be
fetched in their sequence order """
for inv in self:
if inv.amount_untaxed_before_global_discounts:
base = inv.amount_untaxed_before_global_discounts
else:
base = inv.amount_untaxed
invoice_global_discounts = (
self.env['account.invoice.global.discount'])
for global_discount in inv.global_discount_ids:
def _set_global_discounts_by_tax(self):
"""Create invoice global discount lines by tax and discount"""
self.ensure_one()
invoice_global_discounts = self.env['account.invoice.global.discount']
for tax_line in self.tax_line_ids:
base = tax_line.base
for global_discount in self.global_discount_ids:
discount = global_discount._get_global_discount_vals(base)
invoice_global_discounts += invoice_global_discounts.new({
'name': global_discount.display_name,
'invoice_id': inv.id,
'invoice_id': self.id,
'global_discount_id': global_discount.id,
'discount': global_discount.discount,
'base': base,
'base_discounted': discount['base_discounted'],
'account_id': global_discount.account_id.id,
'tax_id': tax_line.tax_id.id,
})
base = discount['base_discounted']
inv.invoice_global_discount_ids = invoice_global_discounts
self.invoice_global_discount_ids = invoice_global_discounts

def _set_global_discounts(self):
"""Get global discounts in order and apply them in chain. They will be
fetched in their sequence order """
for inv in self:
inv._set_global_discounts_by_tax()
# Recompute line taxes according to global discounts
taxes_grouped = inv.get_taxes_values()
tax_lines = inv.tax_line_ids.filtered('manual')
Expand Down Expand Up @@ -154,6 +157,7 @@ def invoice_line_move_line_get(self):
'price': discount.discount_amount * -1,
'account_id': discount.account_id.id,
'account_analytic_id': discount.account_analytic_id.id,
'tax_ids': discount.tax_id.id,
'invoice_id': self.id,
})
return res
Expand Down Expand Up @@ -208,6 +212,9 @@ class AccountInvoiceGlobalDiscount(models.Model):
currency_field='currency_id',
readonly=True,
)
tax_id = fields.Many2one(
comodel_name='account.tax',
)
account_id = fields.Many2one(
comodel_name='account.account',
required=True,
Expand Down
4 changes: 4 additions & 0 deletions account_global_discount/models/account_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ class AccountMoveLine(models.Model):
string='Global Discount',
ondelete='restrict',
)
invoice_global_discount_id = fields.Many2one(
comodel_name='account.invoice.global.discount',
string='Invoice Global Discount',
)
3 changes: 3 additions & 0 deletions account_global_discount/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* Global Discount move lines are created for a common base amount. If that
wasn't the case, we should split discount move lines between bases and
assign proper taxes accordingly.

0 comments on commit d2f508a

Please sign in to comment.