Thayif kabir
Thayif kabir

Reputation: 726

Remove create and edit depending up on value of another field in odoo11

Need to remove create and edit in partner_id field in sales order depending on value of another field. I found one similar answer but it not working.

<field name="partner_id" position="replace">
   <field name="partner_id" string="partner" domain=[('customer','=',True),('sale_invoice_type','=',sale_invoice_type)]" context="{'search_default_customer':1, 'show_address': 1,'default_sale_invoice_type':sale_invoice_type}" attrs="  {'invisible': [('sale_invoice_type', '=', 'cash')]}" options='{"always_reload": True, "no_create_edit": True}'/>         

   <field name="partner_id" domain="[('customer','=',True),('sale_invoice_type','=',sale_invoice_type)]" context="{'search_default_customer':1, 'show_address': 1,'default_sale_invoice_type':sale_invoice_type}" attrs="{'invisible': [('sale_invoice_type', '=', 'credit')]}" options='{"always_reload": True}'/>

</field>

When the sale_invoice_type is cash edit and create should be removed

Upvotes: 4

Views: 1299

Answers (1)

Charif DZ
Charif DZ

Reputation: 14721

First the domain of both field should look like this

    <field ....  attrs="  {'invisible': [('sale_invoice_type', '!=', 'cash')]}"/>

    <field ....  attrs="  {'invisible': [('sale_invoice_type', '=', 'cash')]}"/>

IF The solution didn't work because you should not have the same field two time in your view Odoo will be confused witch one to pass.

But you can workaround it by creating a new field.

In your model define another partner field just a dummy one to use it instead of the real partner_id but make sure that both field at the end of write and create will always be equal.

    partner_no_create = fields.Man.......

In your code make sure that this two field always equal:

    # I think this onchage handle the cases in Odoo views
    @api.onchange('partner_id')
    def set_partner_no_create(self):
        if self.sale_invoice_type != 'cash':
            self.partner_no_create = self.partner_id


    @api.onchange('partner_no_create')
    def set_partner_no_create(self):
        if self.sale_invoice_type == 'cash':
            self.partner_id = self.partner_no_create

In your XML

       <field name="partner_no_create" string="partner" domain="[('customer','=',True),('sale_invoice_type','=',sale_invoice_type)]" 
           context="{'search_default_customer':1, 'show_address': 1,'default_sale_invoice_type':sale_invoice_type}" 
           attrs="  {'invisible': [('sale_invoice_type', '!=', 'cash')]}" options='{"always_reload": True, "no_create_edit": True}'/>         

       <field name="partner_id" domain="[('customer','=',True),('sale_invoice_type','=',sale_invoice_type)]" 
           context="{'search_default_customer':1, 'show_address': 1,'default_sale_invoice_type':sale_invoice_type}" 
           attrs="{'invisible': [('sale_invoice_type', '=', 'cash')]}" options='{"always_reload": True}'/>

But still have to handle more cases specially when the record is updated or create from RPC call, you need to override create and write method to handle all cases.

Upvotes: 3

Related Questions