Skip to Content
Menu
This question has been flagged
2 Replies
28350 Views

I have 3 models:

seniat_form_86:

'line_ids':fields.one2many('seniat.form.86.lines','line_id','Tax lines',readonly=True, states={'draft':[('readonly',False)]}),
'invoice_ids':fields.one2many('account.invoice','num_import_form_id','Related invoices',readonly=True),

seniat_form_86_lines:

'line_id':fields.many2one('seniat.form.86', 'Line', required=True, ondelete='cascade'),
'line_vat_ids':fields.one2many('seniat.form_86.lines.vat','line_vat_id','Vat lines',attrs="{'readonly':[('vat_detail','=',True)],'required':[('vat_detail','=',True)]}"),

seniat_form_86_lines_vat:

'line_vat_id':fields.many2one('seniat.form.86.lines', 'Vat line', required=True, ondelete='cascade'),
'invoice_id': fields.many2one('account.invoice', 'Invoice Reference', ondelete='restrict', select=True, required=True),

...

as you can see the 3 models are "master-detail" interrelated.

I need to set a domain for seniat_form_86_lines_vat.invoice_id like this:

domain="[('id','in',line_vat_id.line_id.invoice_ids)]"

Error: NameError: name 'line_vat_id' is not defined if widget="selection" in view

I want to filter seniat_form_86_lines_vat.invoice_id with only invoices listed in seniat_form_86.invoice_ids.

Note: Server 6.0

Note 2: I also try with:

domain="[('id','in',parent.parent.invoice_ids)]"
domain="[('id','in',parent.line_id.invoice_ids)]"
domain="[('id','in',parent.invoice_ids)]"

Thanks.

Avatar
Discard
Author Best Answer

Hi, Mohammad thanks for your fast answer but when I try to implement it I note this:

The on_change method is never called

view:

<field name="line_vat_id" invisible="1" on_change="on_change_line_vat_id(line_vat_id)"/>

Full view

   <record model="ir.ui.view" id="view_seniat_form_86_form">
        <field name="name">seniat.form.86.form</field>
        <field name="model">seniat.form.86</field>
        <field name="type">form</field>
        <field name="arch" type="xml">
            <form>
                <group col="8" colspan="8">
                    <field name="name"/>
                    <field name="ref"/>
                    <field name="broker_id"/>
                    <field name="date"/>
                </group>
                <notebook colspan="4">
                    <page string="General info">
                        <group col="8" colspan="8">
                            <field name="ref_reg"/>
                            <field name="date_reg"/>
                            <field name="ref_liq"/>
                            <field name="date_liq"/>
                        </group>
                        <field name="custom_id" colspan="4"/>
                        <field name="line_ids" colspan="4" nolabel="1">
                            <tree string="Tax lines">
                                <field name="tax_code"/>
                                <field name="amount" sum="Amount total"/>
                            </tree>
                            <form string="Tax lines">
                                <group col="8" colspan="8">
                                    <field name="tax_code" colspan="6"/>
                                    <field name="amount"/>
                                </group>
                                <field name="vat_detail" invisible="1"/>
                                <field name="line_vat_ids" colspan="4" nolabel="1" attrs="{'invisible':[('vat_detail','!=',True)]}">
                                    <tree string="Vat tax detail" editable="bottom">
                                        <field name="line_vat_id" invisible="1" on_change="on_change_line_vat_id(line_vat_id)"/>
                                        <field name="invoice_id" on_change="on_change_invoice_id(invoice_id)"/>
                                        <field name="partner_id"/>
                                        <field name="reference"/>
                                        <field name="acc_tax_id" widget="selection"/>
                                        <field name="base_amount" on_change="on_change_amount(acc_tax_id, base_amount, tax_amount)"/>
                                        <field name="tax_amount" on_change="on_change_amount(acc_tax_id, base_amount, tax_amount)" sum="Tax amount total"/>
                                    </tree>
                                    <form>
                                        <field name="line_vat_id" invisible="1" on_change="on_change_line_vat_id(line_vat_id)"/>
                                        <field name="invoice_id" on_change="on_change_invoice_id(invoice_id)"/>
                                        <field name="partner_id"/>
                                        <field name="reference"/>
                                        <field name="acc_tax_id" widget="selection"/>
                                        <field name="base_amount" on_change="on_change_amount(acc_tax_id, base_amount, tax_amount)"/>
                                        <field name="tax_amount" on_change="on_change_amount(acc_tax_id, base_amount, tax_amount)"/>
                                    </form>
                                </field>    
                            </form>
                        </field>    
                        <field name="amount_total"/>
                    </page>    
                    <page string="Other">
                        <label string="Notes" colspan="4"/>
                        <field name="narration" colspan="4" nolabel="1"/>  
                        <field name="invoice_ids" colspan="4" nolabel="1"/>  
                        <field name="move_id"/>
                        <field name="company_id"/>
                    </page>    
                </notebook>    
                <group col="8" colspan="8">
                    <field name="state"/>
                    <button name="button_open" icon="gtk-go-forward" string="Open" states="draft,done" colspan="1"/>
                    <button name="button_done" icon="gtk-ok" string="Done" states="open" colspan="1"/>
                    <button name="button_cancel" icon="gtk-cancel" string="Cancel" states="draft,open" colspan="1"/>
                    <button name="button_draft" icon="terp-stock_effects-object-colorize" string="Reset to draft" states="cancel" colspan="1"/>
                </group>
            </form>
        </field>
    </record>

model:

def on_change_line_vat_id(self, cr, uid, ids, line_vat_id):
    '''
    Create a domain to filter invoice_id for invoices listed in seniat_form_86.invoice_ids only
    '''
    res = {}
    if line_vat_id:
        line_obj = self.pool.get('seniat.form.86.lines')
        invoices = [i.id for i in line_obj.browse(cr, uid, line_vat_id).line_id.invoice_ids]
        res = {'domain': {'invoice_id': [('id','in',invoices)]}}
    return res

The method on_change_line_vat_id only is called when I make the field visible and change it manually. How I can set this domain in new or edited records?

Thanks.

Avatar
Discard
Best Answer

In seniat_form_86_lines_vat create an on_change method for the field line_vat_id (make it invisible). The on_change method should return a domain for invoice_id using a list.

it should be something like this:

def def onchange_line_id(self, cr, uid, ids, line_vat_id)
    line_obj = self.pool['seniat.form.86.lines']
    invoices = [i.id for i in 
                line_obj.browse(cr, uid, id[0]).line_id.invoice_ids]
    return {'domain':
               {'invoice_id': [('id','in',invoices)]}
           }
Avatar
Discard
Related Posts Replies Views Activity
1
Dec 19
2591
1
Dec 17
5402
0
Mar 15
4014
0
Feb 19
3398
2
Mar 24
574