2

I try to do this: When an invoice is created, if the customer is "x" some journals must be restricted in "Register Payment" form

This is my .py model

from odoo import api, fields, models


class AccountPayment(models.Model):
_inherit = "account.payment"
_description = "Restrict Journals"

partner_id_check = fields.Integer(compute="_compute_value")

restriction_value = fields.Boolean(related="journal_id.restriction")

@api.onchange("partner_id")
def _compute_value(self):
    if self.partner_id.name == "Generic":
        self.partner_id_check = 1
    else:
        self.partner_id_check = 0


class AccountJournal(models.Model):
_inherit = "account.journal"
_description = ""

restriction = fields.Boolean(string="Generic Restriction")

And this is my .xml file

    <record id="account_journal_view_form" model="ir.ui.view">
    <field name="name">account.journal.view.form</field>
    <field name="model">account.journal</field>
    <field name="inherit_id" ref="account.view_account_journal_form"/>
    <field name="arch" type="xml">
        <xpath expr="//form/sheet//field[@name='type']" position="before">
            <field name="restriction"/>
        </xpath>
    </field>
    </record>

    <record id="payment_invoice_form" model="ir.ui.view">
    <field name="name">payment.invoice.form</field>
    <field name="model">account.payment</field>
    <field name="inherit_id" ref="account.view_account_payment_invoice_form"/>
    <field name="priority" eval="16"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='journal_id']" position="attributes">
            <attribute name="domain">
                [('restriction_value','=',True),('partner_id_check','=',1)]
            </attribute>
        </xpath>
    </field>
    </record>

The problem is that, the domain from xpath is not working. I don't get any errors but it doesn't have effect. If turn on developer mode and get the fields, the domain is there, xpath is correct, I don't think that domain syntax is correct. I tried everything but it doesn't seem to work

2 Answers2

1

Thank you for answer but is not working, but I finally solved it with a single onchange, without that 2 fields:

class AccountPayment(models.Model):
_inherit = "account.payment"

@api.onchange('journal_id')
def _onchange_journal(self):
    if self.partner_id.name == "Generic":
        return {'domain': {'journal_id': [('restriction', '=', False), ('type', 'in', ('bank', 'cash'))]}}
    else:
        return {'domain': {'journal_id': [('type', 'in', ('bank', 'cash'))]}}

class AccountJournal(models.Model):
_inherit = "account.journal"
_description = ""

restriction = fields.Boolean(string="Generic Restriction")

And this is xml

    <record id="account_journal_view_form" model="ir.ui.view">
    <field name="name">account.journal.view.form</field>
    <field name="model">account.journal</field>
    <field name="inherit_id" ref="account.view_account_journal_form"/>
    <field name="arch" type="xml">
        <xpath expr="//form/sheet//field[@name='type']" position="before">
            <field name="restriction"/>
        </xpath>
    </field>
    </record>

First time when I run this, it was without "else" at the end of the code and I was getting an error something from base model account. I found out afterwards that it is necessary to return a domain when the condition is not satisfied.

In the end the cod from this answer is working great.

-1
partner_id_check = fields.Integer(compute="_compute_value", store=True)

restriction_value = fields.Boolean(related="journal_id.restriction", store=True)
hardik patel
  • 134
  • 1
  • 9
  • 1
    Is there maybe more then just 2 lines of code as answer? – CZoellner Jul 02 '21 at 06:47
  • Yes if everything related to your xml is correct. To use any field in domain it must be in the database table. And in you .py file there two field one is compute and other is is related and it will not store in database until store=True. – hardik patel Jul 02 '21 at 11:02
  • 2
    My intention was to tell you to update your answer with more information. Don't expect others to read comments or upvote answers without explanations. – CZoellner Jul 02 '21 at 11:49