From 524db91cff699b72a4f43bcd66a6892de73b471b Mon Sep 17 00:00:00 2001 From: Quentin Groulard Date: Thu, 23 Apr 2020 17:41:47 +0200 Subject: [PATCH] [REF] Invoice Action Action server has no property 'groups_ids', therefore refactored into an action window --- .../models/account_invoice.py | 28 ++++--------------- .../tests/test_account_clearance_plan.py | 3 +- .../wizard/account_clearance_plan.py | 26 +++++++++++------ .../wizard/account_clearance_plan_wizard.xml | 19 ++++++------- 4 files changed, 34 insertions(+), 42 deletions(-) diff --git a/account_clearance_plan/models/account_invoice.py b/account_clearance_plan/models/account_invoice.py index d8f2b06a7..cee91f4ee 100644 --- a/account_clearance_plan/models/account_invoice.py +++ b/account_clearance_plan/models/account_invoice.py @@ -1,31 +1,15 @@ # Copyright 2020 ACSONE SA/NV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from odoo import models, _ -from odoo.exceptions import UserError +from odoo import models class AccountInvoice(models.Model): _inherit = "account.invoice" - def get_clearance_plan_wizard(self): - account = self.mapped("account_id") - if len(account.ids) != 1: - raise UserError(_("Please select invoices from exactly one account.")) - move_lines = self.mapped("move_id.line_ids").filtered( - lambda l: l.account_id == account - ) - context = self.env.context.copy() - context.update( - {"active_ids": move_lines.ids, "active_model": "account.move.line"} - ) - - return { - "type": "ir.actions.act_window", - "res_model": "account.clearance.plan", - "name": "Clearance Plan", - "target": "new", - "view_mode": "form", - "context": context, - } + def _get_open_move_lines_ids(self): + self.ensure_one() + return self.mapped("move_id.line_ids").filtered( + lambda l: l.account_id == self.account_id and not l.reconciled + ).ids diff --git a/account_clearance_plan/tests/test_account_clearance_plan.py b/account_clearance_plan/tests/test_account_clearance_plan.py index da362dc41..8cb676835 100644 --- a/account_clearance_plan/tests/test_account_clearance_plan.py +++ b/account_clearance_plan/tests/test_account_clearance_plan.py @@ -84,9 +84,8 @@ class TestAccountClearancePlan(TransactionCase): self.register_payments.create_payments() def create_and_fill_wizard(self): - res = self.invoice.with_context(self.invoice_ctx).get_clearance_plan_wizard() clearance_plan_wizard = Form( - self.env[res["res_model"]].with_context(res["context"]) + self.env["account.clearance.plan"].with_context(self.invoice_ctx) ) i = 1 while i <= 4: diff --git a/account_clearance_plan/wizard/account_clearance_plan.py b/account_clearance_plan/wizard/account_clearance_plan.py index 1d4865e8e..a2c698164 100644 --- a/account_clearance_plan/wizard/account_clearance_plan.py +++ b/account_clearance_plan/wizard/account_clearance_plan.py @@ -54,21 +54,31 @@ class AccountClearancePlan(models.TransientModel): for rec in self: rec.amount_allocated = sum(rec.clearance_plan_line_ids.mapped("amount")) - @api.model - def default_get(self, fields): - rec = super().default_get(fields) - - if not self._context.get("active_model") == "account.move.line": + def _get_move_lines_from_context(self): + active_model = self._context.get("active_model") + if active_model == "account.invoice": + move_line_ids = [] + for invoice in self.env["account.invoice"].browse( + self._context.get("active_ids") + ): + move_line_ids += invoice._get_open_move_lines_ids() + elif not self._context.get("active_model") == "account.move.line": raise UserError( _( "Programming error: wizard action executed with 'active_model' " "different from 'account.move.line' in context." ) ) + else: + move_line_ids = self._context.get("active_ids") - move_lines = self.env["account.move.line"].browse( - self._context.get("active_ids") - ) + return self.env["account.move.line"].browse(move_line_ids) + + @api.model + def default_get(self, fields): + rec = super().default_get(fields) + + move_lines = self._get_move_lines_from_context() account_id = move_lines.mapped("account_id") # Check all move lines are from same partner diff --git a/account_clearance_plan/wizard/account_clearance_plan_wizard.xml b/account_clearance_plan/wizard/account_clearance_plan_wizard.xml index 412b5c4e4..6927b6dcd 100644 --- a/account_clearance_plan/wizard/account_clearance_plan_wizard.xml +++ b/account_clearance_plan/wizard/account_clearance_plan_wizard.xml @@ -32,7 +32,7 @@ - - - Clearance Plan - - - action - code - action = records.get_clearance_plan_wizard() - - +