[REF] Invoice Action

Action server has no property 'groups_ids', therefore refactored into an action window
This commit is contained in:
Quentin Groulard
2020-04-23 17:41:47 +02:00
parent eedb5d2da7
commit 524db91cff
4 changed files with 34 additions and 42 deletions

View File

@@ -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

View File

@@ -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:

View File

@@ -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

View File

@@ -32,7 +32,7 @@
</record>
<!--Add to account.move.line action-->
<act_window id="act_clearance_plan_wizard"
<act_window id="act__move_line_clearance_plan_wizard"
name="Clearance Plan"
res_model="account.clearance.plan"
src_model="account.move.line"
@@ -42,15 +42,14 @@
target="new" />
<!--Add to account.invoice action-->
<record model="ir.actions.server" id="action_clearance_plan_wizard">
<field name="name">Clearance Plan</field>
<field name="model_id" ref="model_account_invoice"/>
<field name="binding_model_id" ref="account.model_account_invoice"/>
<field name="binding_type">action</field>
<field name="state">code</field>
<field name="code">action = records.get_clearance_plan_wizard()</field>
<field name="groups_id" eval="[(6, 0, [ref('account.group_account_manager')])]"/>
</record>
<act_window id="act_invoice_clearance_plan_wizard"
name="Clearance Plan"
res_model="account.clearance.plan"
src_model="account.invoice"
view_mode="form"
groups="account.group_account_manager"
key2="client_action_multi"
target="new" />
</odoo>