From e79df91639ffab7195a167d4c033129eed2e02af Mon Sep 17 00:00:00 2001 From: Giovanni Date: Mon, 10 Jan 2022 10:29:37 +0100 Subject: [PATCH] [IMP] l10n_it_ricevute_bancarie: manage multiple bank in partner, migration script --- .../migrations/14.0.1.0.1/post-migration.py | 13 +++++++ l10n_it_ricevute_bancarie/models/account.py | 37 ++++++++++++++++++- .../tests/riba_common.py | 1 + l10n_it_ricevute_bancarie/tests/test_riba.py | 5 +++ .../views/account_view.xml | 6 +++ .../wizard/wizard_riba_issue.py | 8 ++-- 6 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 l10n_it_ricevute_bancarie/migrations/14.0.1.0.1/post-migration.py diff --git a/l10n_it_ricevute_bancarie/migrations/14.0.1.0.1/post-migration.py b/l10n_it_ricevute_bancarie/migrations/14.0.1.0.1/post-migration.py new file mode 100644 index 000000000000..48fb96f1845a --- /dev/null +++ b/l10n_it_ricevute_bancarie/migrations/14.0.1.0.1/post-migration.py @@ -0,0 +1,13 @@ +from openupgradelib import openupgrade + + +@openupgrade.migrate() +def migrate(env, version): + invoices = env["account.move"].search( + [ + ("invoice_payment_term_id.riba", "=", True), + ("riba_partner_bank_id", "=", False), + ] + ) + for invoice in invoices: + invoice._onchange_riba_partner_bank_id() diff --git a/l10n_it_ricevute_bancarie/models/account.py b/l10n_it_ricevute_bancarie/models/account.py index eb4b88cfa253..7adaabd8ad61 100644 --- a/l10n_it_ricevute_bancarie/models/account.py +++ b/l10n_it_ricevute_bancarie/models/account.py @@ -71,9 +71,44 @@ def _compute_is_unsolved(self): "line_id", "Past Due Journal Items", ) + is_unsolved = fields.Boolean( "Is a past due invoice", compute="_compute_is_unsolved", store=True ) + is_riba_payment = fields.Boolean( + "Is C/O Payment", related="invoice_payment_term_id.riba", default=False + ) + + riba_partner_bank_id = fields.Many2one( + "res.partner.bank", + string="C/O Bank Account", + help="Bank Account Number to which the C/O will be debited. " + "If not set, first bank in partner will be used.", + readonly=True, + states={"draft": [("readonly", False)]}, + ) + + @api.model + def create(self, vals): + invoice = super().create(vals) + if not invoice.riba_partner_bank_id: + invoice._onchange_riba_partner_bank_id() + return invoice + + @api.onchange("partner_id", "invoice_payment_term_id", "move_type") + def _onchange_riba_partner_bank_id(self): + if ( + not self.riba_partner_bank_id + or self.riba_partner_bank_id not in self.partner_id.bank_ids + ): + bank_ids = self.env["res.partner.bank"] + if ( + self.partner_id + and self.invoice_payment_term_id.riba + and self.move_type in ["out_invoice", "out_refund"] + ): + bank_ids = self.partner_id.bank_ids + self.riba_partner_bank_id = bank_ids[0] if bank_ids else None def month_check(self, invoice_date_due, all_date_due): """ @@ -226,7 +261,7 @@ class AccountMoveLine(models.Model): "Past Due Invoices", ) iban = fields.Char( - related="partner_id.bank_ids.acc_number", string="IBAN", store=False + related="move_id.riba_partner_bank_id.acc_number", string="IBAN", store=False ) due_cost_line = fields.Boolean("C/O Collection Fees Line") diff --git a/l10n_it_ricevute_bancarie/tests/riba_common.py b/l10n_it_ricevute_bancarie/tests/riba_common.py index 5f52e42643d8..74fd03b60fd5 100644 --- a/l10n_it_ricevute_bancarie/tests/riba_common.py +++ b/l10n_it_ricevute_bancarie/tests/riba_common.py @@ -128,6 +128,7 @@ def _create_invoice(self): "journal_id": self.sale_journal.id, "partner_id": self.partner.id, "invoice_payment_term_id": self.payment_term1.id, + "riba_partner_bank_id": self.partner.bank_ids[0].id, "invoice_line_ids": [ ( 0, diff --git a/l10n_it_ricevute_bancarie/tests/test_riba.py b/l10n_it_ricevute_bancarie/tests/test_riba.py index 3e6d569e59e2..e056e647ea6e 100644 --- a/l10n_it_ricevute_bancarie/tests/test_riba.py +++ b/l10n_it_ricevute_bancarie/tests/test_riba.py @@ -96,6 +96,7 @@ def test_riba_flow(self): ], } ) + invoice._onchange_riba_partner_bank_id() invoice.action_post() riba_move_line_id = False for move_line in invoice.line_ids: @@ -269,6 +270,7 @@ def test_unsolved_riba(self): ], } ) + invoice._onchange_riba_partner_bank_id() invoice.action_post() for move_line in invoice.line_ids: if move_line.account_id.id == self.account_rec1_id.id: @@ -399,6 +401,7 @@ def test_riba_fatturapa(self): ], } ) + invoice._onchange_riba_partner_bank_id() invoice.action_post() # issue wizard riba_move_line_id = invoice.line_ids.filtered( @@ -462,6 +465,7 @@ def test_riba_fatturapa_group(self): ], } ) + invoice._onchange_riba_partner_bank_id() invoice.action_post() invoice1 = self.env["account.move"].create( { @@ -497,6 +501,7 @@ def test_riba_fatturapa_group(self): ], } ) + invoice1._onchange_riba_partner_bank_id() invoice1.action_post() # issue wizard riba_move_line_id = invoice.line_ids.filtered( diff --git a/l10n_it_ricevute_bancarie/views/account_view.xml b/l10n_it_ricevute_bancarie/views/account_view.xml index b40f178e76df..1d4fc581d3a8 100644 --- a/l10n_it_ricevute_bancarie/views/account_view.xml +++ b/l10n_it_ricevute_bancarie/views/account_view.xml @@ -153,6 +153,12 @@ + +