Skip to content

Commit

Permalink
[MIG] account_invoice_triple_discount: Migration to 16.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiadavid committed Dec 28, 2022
1 parent 008a8a3 commit cc65e60
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 74 deletions.
2 changes: 1 addition & 1 deletion account_invoice_triple_discount/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
"name": "Account Invoice Triple Discount",
"version": "15.0.1.0.0",
"version": "16.0.1.0.0",
"category": "Accounting & Finance",
"author": "QubiQ, Tecnativa, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/account-invoicing",
Expand Down
1 change: 0 additions & 1 deletion account_invoice_triple_discount/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from . import account_move
from . import account_move_line
44 changes: 0 additions & 44 deletions account_invoice_triple_discount/models/account_move.py

This file was deleted.

75 changes: 59 additions & 16 deletions account_invoice_triple_discount/models/account_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,73 @@ def create(self, values_list):
record.write(old_values[index])
return records

@api.onchange(
@api.depends(
"quantity",
"discount",
"price_unit",
"tax_ids",
"quantity",
"currency_id",
"discount2",
"discount3",
)
def _onchange_price_subtotal(self):
return super(AccountMoveLine, self)._onchange_price_subtotal()
def _compute_totals(self):
"""
As the totals are recalculated based on a single discount, we need to
simulate a multiple discount by changing the discount value. Values are
restored after the original process is done
"""
old_values_by_line_id = {}
digits = self._fields["discount"]._digits
self._fields["discount"]._digits = (16, 16)
for line in self:
aggregated_discount = line._compute_aggregated_discount(line.discount)
old_values_by_line_id[line.id] = {"discount": line.discount}
line.update({"discount": aggregated_discount})
res = super(AccountMoveLine, self)._compute_totals()
self._fields["discount"]._digits = digits
for line in self:
if line.id not in old_values_by_line_id:
continue
line.update(old_values_by_line_id[line.id])
return res

def _get_price_total_and_subtotal(self, **kwargs):
self.ensure_one()
kwargs["discount"] = self._compute_aggregated_discount(
kwargs.get("discount") or self.discount
)
return super(AccountMoveLine, self)._get_price_total_and_subtotal(**kwargs)
@api.depends(
"tax_ids",
"currency_id",
"partner_id",
"analytic_distribution",
"balance",
"partner_id",
"move_id.partner_id",
"price_unit",
"discount2",
"discount3",
)
def _compute_all_tax(self):
"""
As the taxes are recalculated based on a single discount, we need to
simulate a multiple discount by changing discount value. Values are
restored after the original process is done
"""
digits = self._fields["discount"]._digits
self._fields["discount"]._digits = (16, 16)
old_values_by_line_id = {}
for line in self:
aggregated_discount = line._compute_aggregated_discount(line.discount)
old_values_by_line_id[line.id] = {"discount": line.discount}
line.update({"discount": aggregated_discount})
res = super(AccountMoveLine, self)._compute_all_tax()
self._fields["discount"]._digits = digits
for line in self:
if line.id not in old_values_by_line_id:
continue
line.update(old_values_by_line_id[line.id])
return res

def _get_fields_onchange_balance(self, **kwargs):
self.ensure_one()
kwargs["discount"] = self._compute_aggregated_discount(
kwargs.get("discount") or self.discount
)
return super(AccountMoveLine, self)._get_fields_onchange_balance(**kwargs)
def _convert_to_tax_base_line_dict(self):
res = super(AccountMoveLine, self)._convert_to_tax_base_line_dict()
res["discount"] = self._compute_aggregated_discount(res["discount"])
return res

def _compute_aggregated_discount(self, base_discount):
self.ensure_one()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class TestInvoiceTripleDiscount(TransactionCase):
@classmethod
def setUpClass(cls):
super(TestInvoiceTripleDiscount, cls).setUpClass()
cls.env.user.groups_id += cls.env.ref("product.group_discount_per_so_line")
cls.Account = cls.env["account.account"]
cls.AccountMove = cls.env["account.move"]
cls.AccountTax = cls.env["account.tax"]
cls.AccountType = cls.env["account.account.type"]
cls.Partner = cls.env["res.partner"]
cls.Journal = cls.env["account.journal"]

Expand All @@ -26,14 +26,11 @@ def setUpClass(cls):
"country_id": cls.env.ref("base.us").id,
}
)
cls.account_type = cls.AccountType.create(
{"name": "Test", "type": "receivable", "internal_group": "income"}
)
cls.account = cls.Account.create(
{
"name": "Test account",
"code": "TEST",
"user_type_id": cls.account_type.id,
"account_type": "asset_receivable",
"reconcile": True,
}
)
Expand Down
7 changes: 0 additions & 7 deletions account_invoice_triple_discount/views/account_move.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@
<field name="discount2" groups="product.group_discount_per_so_line" />
<field name="discount3" groups="product.group_discount_per_so_line" />
</xpath>
<xpath
expr="//field[@name='line_ids']//tree//field[@name='discount']"
position="after"
>
<field name="discount2" invisible="1" />
<field name="discount3" invisible="1" />
</xpath>
</field>
</record>
</odoo>

0 comments on commit cc65e60

Please sign in to comment.