Skip to content

Commit

Permalink
[MIG] account_global_discount: Migration to 17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
miguel-S73 committed Nov 6, 2024
1 parent d8be2f4 commit 90fbdce
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 72 deletions.
2 changes: 1 addition & 1 deletion account_global_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.html).
{
"name": "Account Global Discount",
"version": "16.0.1.0.1",
"version": "17.0.1.0.0",
"category": "Accounting",
"author": "Tecnativa, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/account-invoicing",
Expand Down
16 changes: 9 additions & 7 deletions account_global_discount/hooks.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
from odoo.tools.sql import column_exists


def _pre_init_global_discount_fields(cr):
if not column_exists(cr, "account_move", "amount_global_discount"):
cr.execute(
def _pre_init_global_discount_fields(env):
if not column_exists(env.cr, "account_move", "amount_global_discount"):
env.cr.execute(
"""
ALTER TABLE "account_move"
ADD COLUMN "amount_global_discount" double precision DEFAULT 0
"""
)
cr.execute(
env.cr.execute(
"""
ALTER TABLE "account_move" ALTER COLUMN "amount_global_discount" DROP DEFAULT
"""
)
if not column_exists(cr, "account_move", "amount_untaxed_before_global_discounts"):
cr.execute(
if not column_exists(
env.cr, "account_move", "amount_untaxed_before_global_discounts"
):
env.cr.execute(
"""
ALTER TABLE "account_move"
ADD COLUMN "amount_untaxed_before_global_discounts" double precision
"""
)
cr.execute(
env.cr.execute(
"""
update account_move set amount_untaxed_before_global_discounts = amount_untaxed
"""
Expand Down
60 changes: 31 additions & 29 deletions account_global_discount/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class AccountMove(models.Model):
" 'in_invoice': ['purchase']"
"}.get(move_type, [])), ('account_id', '!=', False), '|', "
"('company_id', '=', company_id), ('company_id', '=', False)]",
readonly=True,
states={"draft": [("readonly", False)]},
compute="_compute_global_discount_ids",
store=True,
readonly=False,
)
amount_global_discount = fields.Monetary(
string="Total Global Discounts",
Expand Down Expand Up @@ -87,7 +88,7 @@ def _set_global_discounts_by_tax(self):
# incorrect data in discounts
_self = self.filtered("global_discount_ids")
for inv_line in _self.invoice_line_ids.filtered(
lambda l: l.display_type not in ["line_section", "line_note"]
lambda line: line.display_type not in ["line_section", "line_note"]
):
if inv_line.product_id.bypass_global_discount:
continue
Expand Down Expand Up @@ -161,8 +162,9 @@ def _recompute_global_discount_lines(self):
{
"invoice_global_discount_id": discount.id,
"move_id": self.id,
"name": "%s - %s"
% (discount.name, ", ".join(discount.tax_ids.mapped("name"))),
"name": "{} - {}".format(
discount.name, ", ".join(discount.tax_ids.mapped("name"))
),
"debit": disc_amount_company_currency > 0.0
and disc_amount_company_currency
or 0.0,
Expand All @@ -179,27 +181,25 @@ def _recompute_global_discount_lines(self):
}
)

@api.onchange("partner_id", "company_id")
def _onchange_partner_id(self):
res = super()._onchange_partner_id()
discounts = False
if (
self.move_type in ["out_invoice", "out_refund"]
and self.partner_id.customer_global_discount_ids
):
discounts = self.partner_id.customer_global_discount_ids.filtered(
lambda d: d.company_id == self.company_id
)
elif (
self.move_type in ["in_refund", "in_invoice"]
and self.partner_id.supplier_global_discount_ids
):
discounts = self.partner_id.supplier_global_discount_ids.filtered(
lambda d: d.company_id == self.company_id
)
if discounts:
self.global_discount_ids = discounts
return res
@api.depends("partner_id", "company_id", "move_type")
def _compute_global_discount_ids(self):
for move in self:
discounts = self.env["global.discount"]
move_discounts = self.env["global.discount"]
if (
move.move_type in ["out_invoice", "out_refund"]
and move.partner_id.customer_global_discount_ids
):
move_discounts = move.partner_id.customer_global_discount_ids
elif (
move.move_type in ["in_refund", "in_invoice"]
and move.partner_id.supplier_global_discount_ids
):
move_discounts = move.partner_id.supplier_global_discount_ids
for discount in move_discounts:
if discount.company_id == move.company_id:
discounts |= discount
move.global_discount_ids = discounts

def _compute_amount_one(self):
"""Perform totals computation of a move with global discounts."""
Expand Down Expand Up @@ -320,8 +320,8 @@ class AccountMoveLine(models.Model):
string="Amount Untaxed Before Discounts",
readonly=True,
)
# TODO: To be removed on future versions if invoice_global_discount_id is properly filled
# Provided for compatibility in stable branch
# TODO: To be removed on future versions if invoice_global_discount_id
# is properly filled Provided for compatibility in stable branch
# UPD: can be removed past version 16.0
global_discount_item = fields.Boolean()

Expand Down Expand Up @@ -368,7 +368,9 @@ class AccountInvoiceGlobalDiscount(models.Model):
comodel_name="account.account",
required=True,
string="Account",
domain="[('account_type', 'not in', ['asset_receivable', 'liability_payable'])]",
domain=(
"[('account_type', 'not in', ['asset_receivable', 'liability_payable'])]"
),
)
account_analytic_id = fields.Many2one(
comodel_name="account.analytic.account",
Expand Down
4 changes: 3 additions & 1 deletion account_global_discount/models/global_discount.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class GlobalDiscount(models.Model):
account_id = fields.Many2one(
comodel_name="account.account",
string="Account",
domain="[('account_type', 'not in', ['asset_receivable', 'liability_payable'])]",
domain=(
"[('account_type', 'not in', ['asset_receivable', 'liability_payable'])]"
),
check_company=True,
)
account_analytic_id = fields.Many2one(
Expand Down
66 changes: 38 additions & 28 deletions account_global_discount/tests/test_global_discount.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright 2021 Tecnativa - Víctor Martínez
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import exceptions
from odoo import Command, exceptions
from odoo.tests import Form, tagged

from odoo.addons.account.tests.common import AccountTestInvoicingCommon
Expand Down Expand Up @@ -101,6 +101,9 @@ def setUpClass(cls, chart_template_ref=None):
cls.journal = cls.env["account.journal"].create(
{"name": "Test purchase journal", "code": "TPUR", "type": "purchase"}
)
cls.product_3 = cls.env["product.product"].create(
{"name": "Test Product 3", "type": "service", "bypass_global_discount": True}
)
cls.invoice_line = cls.env["account.move.line"]
invoice_form = Form(
cls.env["account.move"].with_context(
Expand All @@ -116,12 +119,19 @@ def setUpClass(cls, chart_template_ref=None):
line_form.quantity = 1
line_form.tax_ids.clear()
line_form.tax_ids.add(cls.tax)
with invoice_form.invoice_line_ids.new() as line_form:
line_form.name = "Line 1"
line_form.product_id = cls.product_3
line_form.price_unit = 200.0
line_form.quantity = 1
line_form.tax_ids.clear()
line_form.tax_ids.add(cls.tax_0)
cls.invoice = invoice_form.save()

def test_01_global_invoice_succesive_discounts(self):
"""Add global discounts to the invoice"""
invoice_tax_line = self.invoice.line_ids.filtered("tax_line_id")
self.assertAlmostEqual(self.invoice.amount_total, 230)
self.assertAlmostEqual(self.invoice.amount_total, 430)
self.assertAlmostEqual(invoice_tax_line.tax_base_amount, 200.0)
self.assertAlmostEqual(invoice_tax_line.balance, 30.0)
# Global discounts are applied to the base and taxes are recomputed:
Expand All @@ -138,8 +148,8 @@ def test_01_global_invoice_succesive_discounts(self):
invoice_tax_line = self.invoice.line_ids.filtered("tax_line_id")
self.assertAlmostEqual(invoice_tax_line.tax_base_amount, 100.0)
self.assertAlmostEqual(invoice_tax_line.balance, 15.0)
self.assertAlmostEqual(self.invoice.amount_untaxed, 100.0)
self.assertAlmostEqual(self.invoice.amount_total, 115.0)
self.assertAlmostEqual(self.invoice.amount_untaxed, 300.0)
self.assertAlmostEqual(self.invoice.amount_total, 315.0)
self.assertAlmostEqual(self.invoice.amount_global_discount, -100.0)
# Global discounts are computed succecively:
# 200 - 50% (global disc. 1) = 100
Expand All @@ -151,8 +161,8 @@ def test_01_global_invoice_succesive_discounts(self):
invoice_tax_line = self.invoice.line_ids.filtered("tax_line_id")
self.assertAlmostEqual(invoice_tax_line.tax_base_amount, 70.0)
self.assertAlmostEqual(invoice_tax_line.balance, 10.5)
self.assertAlmostEqual(self.invoice.amount_untaxed, 70.0)
self.assertAlmostEqual(self.invoice.amount_total, 80.5)
self.assertAlmostEqual(self.invoice.amount_untaxed, 270.0)
self.assertAlmostEqual(self.invoice.amount_total, 280.5)
self.assertAlmostEqual(self.invoice.amount_global_discount, -130.0)
# Line discounts apply before global ones so:
# 200 - 20% (line discount) = 160
Expand All @@ -166,14 +176,14 @@ def test_01_global_invoice_succesive_discounts(self):
invoice_tax_line = self.invoice.line_ids.filtered("tax_line_id")
self.assertAlmostEqual(invoice_tax_line.tax_base_amount, 56.0)
self.assertAlmostEqual(invoice_tax_line.balance, 8.4)
self.assertAlmostEqual(self.invoice.amount_untaxed, 56.0)
self.assertAlmostEqual(self.invoice.amount_total, 64.4)
self.assertAlmostEqual(self.invoice.amount_untaxed, 256.0)
self.assertAlmostEqual(self.invoice.amount_total, 264.4)
self.assertAlmostEqual(self.invoice.amount_global_discount, -104.0)

def test_02_global_invoice_discounts_from_partner(self):
"""Change the partner and his global discounts go to the invoice"""
invoice_tax_line = self.invoice.line_ids.filtered("tax_line_id")
self.assertAlmostEqual(self.invoice.amount_total, 230)
self.assertAlmostEqual(self.invoice.amount_total, 430)
self.assertAlmostEqual(invoice_tax_line.tax_base_amount, 200.0)
self.assertAlmostEqual(invoice_tax_line.balance, 30.0)
# When we change the parter, his global discounts are fetched depending
Expand All @@ -183,12 +193,12 @@ def test_02_global_invoice_discounts_from_partner(self):
invoice_form.partner_id = self.partner_2
self.assertAlmostEqual(invoice_tax_line.tax_base_amount, 140.0)
self.assertAlmostEqual(invoice_tax_line.balance, 21.0)
self.assertAlmostEqual(self.invoice.amount_untaxed, 140.0)
self.assertAlmostEqual(self.invoice.amount_total, 161.0)
self.assertAlmostEqual(self.invoice.amount_untaxed, 340.0)
self.assertAlmostEqual(self.invoice.amount_total, 361.0)
self.assertAlmostEqual(self.invoice.amount_global_discount, -60.0)

def test_03_multiple_taxes_multi_line(self):
tax2 = self.tax.copy(default={"amount": 20.0})
tax2 = self.tax.copy(default={"amount": 20.0, "name": "Tax 2"})
with Form(self.invoice) as invoice_form:
invoice_form.global_discount_ids.add(self.global_discount_1)
with invoice_form.invoice_line_ids.new() as line_form:
Expand All @@ -214,8 +224,8 @@ def test_03_multiple_taxes_multi_line(self):
self.assertAlmostEqual(tax_line_15.balance, 24)
self.assertAlmostEqual(tax_line_20.tax_base_amount, 80.0)
self.assertAlmostEqual(tax_line_20.balance, 16)
self.assertAlmostEqual(self.invoice.amount_untaxed, 240.0)
self.assertAlmostEqual(self.invoice.amount_total, 280)
self.assertAlmostEqual(self.invoice.amount_untaxed, 440.0)
self.assertAlmostEqual(self.invoice.amount_total, 480)
self.assertAlmostEqual(self.invoice.amount_global_discount, -60.0)
# Check journal items validity
lines = self.invoice.line_ids
Expand All @@ -230,7 +240,7 @@ def test_03_multiple_taxes_multi_line(self):

def test_04_multiple_taxes_same_line(self):
tax2 = self.tax.copy(
default={"amount": -20.0}
default={"amount": -20.0, "name": "Tax 2"}
) # negative for testing more use cases
with Form(self.invoice.with_context(check_move_validity=False)) as invoice_form:
invoice_form.global_discount_ids.add(self.global_discount_1)
Expand All @@ -253,15 +263,15 @@ def test_04_multiple_taxes_same_line(self):
self.assertAlmostEqual(tax_line_15.balance, 24)
self.assertAlmostEqual(tax_line_20.tax_base_amount, 160.0)
self.assertAlmostEqual(tax_line_20.balance, -32)
self.assertAlmostEqual(self.invoice.amount_untaxed, 160.0)
self.assertAlmostEqual(self.invoice.amount_total, 152)
self.assertAlmostEqual(self.invoice.amount_untaxed, 360.0)
self.assertAlmostEqual(self.invoice.amount_total, 352)
self.assertAlmostEqual(self.invoice.amount_global_discount, -40.0)

def test_05_incompatible_taxes(self):
# Line 1 with tax and tax2
# Line 2 with only tax2
tax2 = self.tax.copy(
default={"amount": -20.0}
default={"amount": -20.0, "name": "Tax 2"}
) # negative for testing more use cases
with self.assertRaises(exceptions.UserError):
with Form(self.invoice) as invoice_form:
Expand Down Expand Up @@ -320,7 +330,7 @@ def test_09_customer_invoice(self):
"sequence": 1,
}
)
tax = self.tax_sale_a.copy(default={"amount": 15.0})
tax = self.tax_sale_a.copy(default={"amount": 15.0, "name": "Tax 2"})
invoice = (
self.env["account.move"]
.with_context(test_account_global_discount=True)
Expand Down Expand Up @@ -366,21 +376,21 @@ def test_10_customer_invoice_currency(self):
with Form(self.invoice) as invoice_form:
invoice_form.currency_id = eur
invoice = invoice_form.save()
self.assertAlmostEqual(invoice.amount_total, 230.0)
self.assertAlmostEqual(invoice.amount_untaxed, 200.0)
self.assertAlmostEqual(invoice.amount_total, 430.0)
self.assertAlmostEqual(invoice.amount_untaxed, 400.0)
self.assertAlmostEqual(invoice.amount_global_discount, 0)
base_line = invoice.line_ids.filtered(
lambda l: l.tax_ids and not l.invoice_global_discount_id
lambda line: line.tax_ids and not line.invoice_global_discount_id
)
self.assertEqual(len(base_line), 1)
self.assertEqual(len(base_line), 2)
self.assertAlmostEqual(
base_line.balance,
eur._convert(
invoice.amount_untaxed, usd, self.env.user.company_id, invoice.date
),
)
tax_line = invoice.line_ids.filtered(
lambda l: l.tax_line_id and not l.invoice_global_discount_id
lambda line: line.tax_line_id and not line.invoice_global_discount_id
)
self.assertEqual(len(tax_line), 1)
tax_line_balance_before_discount = tax_line.balance
Expand Down Expand Up @@ -409,11 +419,11 @@ def test_10_customer_invoice_currency(self):
invoice = invoice_form.save()
# Check that when we add a global discount it will be based on the
# correct currency
self.assertAlmostEqual(invoice.amount_total, 184)
self.assertAlmostEqual(invoice.amount_untaxed, 160.0)
self.assertAlmostEqual(invoice.amount_total, 384)
self.assertAlmostEqual(invoice.amount_untaxed, 360.0)
self.assertAlmostEqual(invoice.amount_global_discount, -40.0)
base_line = invoice.line_ids.filtered(
lambda l: l.tax_ids and not l.invoice_global_discount_id
lambda line: line.tax_ids and not line.invoice_global_discount_id
)
self.assertEqual(len(base_line), 1)
self.assertAlmostEqual(
Expand All @@ -426,7 +436,7 @@ def test_10_customer_invoice_currency(self):
),
)
tax_line = invoice.line_ids.filtered(
lambda l: l.tax_line_id and not l.invoice_global_discount_id
lambda line: line.tax_line_id and not line.invoice_global_discount_id
)
self.assertEqual(len(tax_line), 1)
self.assertAlmostEqual(
Expand Down
Loading

0 comments on commit 90fbdce

Please sign in to comment.