Skip to content

Commit

Permalink
[IMP] account_global_discount: Remake amount global discount
Browse files Browse the repository at this point in the history
  • Loading branch information
Rferri44-S73 authored and pedrobaeza committed May 17, 2024
1 parent 077ca26 commit 3d1c7ac
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
80 changes: 46 additions & 34 deletions account_global_discount/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,42 +89,48 @@ def _set_global_discounts_by_tax(self):
for inv_line in _self.invoice_line_ids.filtered(
lambda l: l.display_type not in ["line_section", "line_note"]
):
for key in taxes_keys:
if key == tuple(inv_line.tax_ids.ids):
break
else:
taxes_keys[tuple(inv_line.tax_ids.ids)] = True
taxes_keys.setdefault(tuple(inv_line.tax_ids.ids), 0)
taxes_keys[tuple(inv_line.tax_ids.ids)] += inv_line.price_subtotal
# Reset previous global discounts
self.invoice_global_discount_ids -= self.invoice_global_discount_ids
model = "account.invoice.global.discount"
create_method = in_draft_mode and self.env[model].new or self.env[model].create
for tax_line in _self.line_ids.filtered("tax_line_id"):
key = []
to_create = True
discount_line_base = 0
for key in taxes_keys:
if tax_line.tax_line_id.id in key:
to_create = taxes_keys[key]
taxes_keys[key] = False # mark for not duplicating
discount_line_base = taxes_keys[key]
taxes_keys[key] = 0 # mark for not duplicating
break # we leave in key variable the proper taxes value
if not to_create:
if not discount_line_base:
continue
base = tax_line.base_before_global_discounts or tax_line.tax_base_amount
for global_discount in self.global_discount_ids:
vals = self._prepare_global_discount_vals(global_discount, base, key)
vals = self._prepare_global_discount_vals(
global_discount, discount_line_base, key
)
create_method(vals)
base = vals["base_discounted"]
discount_line_base = vals["base_discounted"]
_self._set_global_discounts_by_zero_tax(taxes_keys, create_method)

def _set_global_discounts_by_zero_tax(self, taxes_keys, create_method):
# Check all moves with defined taxes to check if there's any discount not
# created (tax amount is zero and only one tax is applied)
for line in _self.line_ids.filtered("tax_ids"):
base_total = 0
zero_taxes = self.env["account.tax"]
for line in self.line_ids.filtered("tax_ids"):
key = tuple(line.tax_ids.ids)
if taxes_keys.get(key):
base = line.price_subtotal
for global_discount in self.global_discount_ids:
vals = self._prepare_global_discount_vals(
global_discount, base, key
)
create_method(vals)
base = vals["base_discounted"]
base_total += line.price_subtotal
zero_taxes |= line.tax_ids
for global_discount in self.global_discount_ids:
if not base_total:
break
vals = self._prepare_global_discount_vals(
global_discount, base_total, zero_taxes.ids
)
create_method(vals)
base_total = vals["base_discounted"]

def _recompute_global_discount_lines(self):
"""Append global discounts move lines.
Expand Down Expand Up @@ -230,6 +236,23 @@ def _compute_amount(self):
record._compute_amount_one()
return res

def _clean_global_discount_lines(self):
self.ensure_one()
gbl_disc_lines = self.env["account.move.line"].search(
[
("move_id", "=", self.id),
"|",
("global_discount_item", "=", True),
("invoice_global_discount_id", "!=", False),
]
)
if gbl_disc_lines:
move_container = {"records": self}
with self._check_balanced(move_container), self._sync_dynamic_lines(
move_container
):
gbl_disc_lines.unlink()

@api.model_create_multi
def create(self, vals_list):
"""If we create the invoice with the discounts already set like from
Expand All @@ -238,6 +261,8 @@ def create(self, vals_list):
"""
moves = super().create(vals_list)
for move in moves:
if move.move_type in ["out_refund", "in_refund"]:
move._clean_global_discount_lines()
move._set_global_discounts_by_tax()
move._recompute_global_discount_lines()
return moves
Expand All @@ -246,20 +271,7 @@ def write(self, vals):
res = super().write(vals)
if "invoice_line_ids" in vals or "global_discount_ids" in vals:
for move in self:
gbl_disc_lines = self.env["account.move.line"].search(
[
("move_id", "=", move.id),
"|",
("global_discount_item", "=", True),
("invoice_global_discount_id", "!=", False),
]
)
if gbl_disc_lines:
move_container = {"records": move}
with self._check_balanced(move_container), self._sync_dynamic_lines(
move_container
):
gbl_disc_lines.unlink()
move._clean_global_discount_lines()
move._set_global_discounts_by_tax()
move._recompute_global_discount_lines()
move_container = {"records": self}
Expand Down
8 changes: 4 additions & 4 deletions account_global_discount/views/account_invoice_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@
<field
name="amount_untaxed_before_global_discounts"
string="Untaxed Amount Before Disc."
attrs="{'invisible': [('global_discount_ids', '=', [])]}"
attrs="{'invisible': ['|',('global_discount_ids', '=', []),('invoice_global_discount_ids','=',[])]}"
groups="base_global_discount.group_global_discount"
/>
<field
name="amount_untaxed_before_global_discounts"
string="Untaxed Amount Before Disc."
attrs="{'invisible': [('global_discount_ids_readonly', '=', [])]}"
attrs="{'invisible': ['|',('global_discount_ids_readonly', '=', []),('invoice_global_discount_ids','=',[])]}"
groups="!base_global_discount.group_global_discount"
/>
<field
name="amount_global_discount"
string="Global Discounts"
attrs="{'invisible': [('global_discount_ids', '=', [])]}"
attrs="{'invisible': ['|',('global_discount_ids', '=', []),('invoice_global_discount_ids','=',[])]}"
groups="base_global_discount.group_global_discount"
/>
<field
name="amount_global_discount"
string="Global Discounts"
attrs="{'invisible': [('global_discount_ids_readonly', '=', [])]}"
attrs="{'invisible': ['|',('global_discount_ids_readonly', '=', []),('invoice_global_discount_ids','=',[])]}"
groups="!base_global_discount.group_global_discount"
/>
</field>
Expand Down

0 comments on commit 3d1c7ac

Please sign in to comment.