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 Aug 14, 2024
1 parent e724509 commit 20b1a28
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 27 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
11 changes: 6 additions & 5 deletions account_global_discount/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ 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)]},
)
amount_global_discount = fields.Monetary(
string="Total Global Discounts",
Expand Down Expand Up @@ -87,7 +85,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 +159,11 @@ 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": (
f"{discount.name} - {
', '.join(discount.tax_ids.mapped('name'))
}"
),
"debit": disc_amount_company_currency > 0.0
and disc_amount_company_currency
or 0.0,
Expand Down
16 changes: 8 additions & 8 deletions account_global_discount/tests/test_global_discount.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def test_02_global_invoice_discounts_from_partner(self):
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 Down Expand Up @@ -230,7 +230,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 Down Expand Up @@ -261,7 +261,7 @@ 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 +320,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 @@ -370,7 +370,7 @@ def test_10_customer_invoice_currency(self):
self.assertAlmostEqual(invoice.amount_untaxed, 200.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.assertAlmostEqual(
Expand All @@ -380,7 +380,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)
tax_line_balance_before_discount = tax_line.balance
Expand Down Expand Up @@ -413,7 +413,7 @@ def test_10_customer_invoice_currency(self):
self.assertAlmostEqual(invoice.amount_untaxed, 160.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 +426,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
13 changes: 7 additions & 6 deletions account_global_discount/views/account_invoice_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
name="global_discount_ids"
widget="many2many_tags"
placeholder="Discounts..."
readonly="state != 'draft'"
groups="base_global_discount.group_global_discount"
/>
<field
Expand All @@ -30,25 +31,25 @@
<field
name="amount_untaxed_before_global_discounts"
string="Untaxed Amount Before Disc."
attrs="{'invisible': ['|',('global_discount_ids', '=', []),('invoice_global_discount_ids','=',[])]}"
invisible="not global_discount_ids or not 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', '=', []),('invoice_global_discount_ids','=',[])]}"
invisible="not global_discount_ids_readonly or not invoice_global_discount_ids"
groups="!base_global_discount.group_global_discount"
/>
<field
name="amount_global_discount"
string="Global Discounts"
attrs="{'invisible': ['|',('global_discount_ids', '=', []),('invoice_global_discount_ids','=',[])]}"
invisible="not global_discount_ids or not 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', '=', []),('invoice_global_discount_ids','=',[])]}"
invisible="not global_discount_ids_readonly or not invoice_global_discount_ids"
groups="!base_global_discount.group_global_discount"
/>
</field>
Expand All @@ -65,7 +66,7 @@
name="invoice_global_discount_ids"
nolabel="1"
readonly="1"
attrs="{'invisible': [('global_discount_ids', '=', [])]}"
invisible="global_discount_ids"
force_save="1"
groups="base_global_discount.group_global_discount"
>
Expand Down Expand Up @@ -100,7 +101,7 @@
name="invoice_global_discount_ids"
nolabel="1"
readonly="1"
attrs="{'invisible': [('global_discount_ids_readonly', '=', [])]}"
invisible="global_discount_ids_readonly"
force_save="1"
groups="!base_global_discount.group_global_discount"
>
Expand Down

0 comments on commit 20b1a28

Please sign in to comment.