Skip to Content
Menu
This question has been flagged
13 Replies
2096 Views

Hi 

I just created a custom module to add Invoices on customer Payments form, I am using onchange_partner_id event in my custom module so that when I change customer It has to fetch related Invoices for that particular customer.....

Able to display the Invoice field as a dropdown with all the Invoices on payment from, but the Onchange_partner_id event is not functioning it is not filtering Invoice custom field


Here is my code

def onchange_partner_id(self, cr, uid, ids, partner_id, journal_id, amount, currency_id, ttype, date, context=None):

        res = super(account_voucher, self).onchange_partner_id(cr, uid, ids, partner_id, journal_id, amount, currency_id, ttype, date, context=None)

        res['value'].update({'invoice_id': False})

        commercial = False

        if partner_id:

            partner = self.pool.get('account.invoice').search(cr, uid, [('partner_id','=',partner_id)])

            res.update({'value': {'invoice_id' : partner}})

            return res

can anyone help me regarding the above criteria

Thanks in Advance

Avatar
Discard
Author

Hi actually I dont have enough karma to comment on your answer but Iam getting a server error as I can't start the odoo server itself it says return^ res Invalid syntax whn I am using below code @IT Libertas

I updated my answer to the full code function. Try it

Author

Hey thanks a lot its working but in the dropdown it is showing all invoices default,,as of now its filtering only one latest invoice...

can we display as the drop down shows invoices only related to him like if he has two open invoices then.... by default it select the latest one and in dropdown list it should display another one only not all

how can it be

Didn't really understand what you meant. But I guess, you should add 'domain' to the field 'invoice_id'. Read the section 'Domains' here: https://www.odoo.com/documentation/8.0/reference/orm.html

Best Answer

Please try:

def onchange_partner_id(self, cr, uid, ids, partner_id, journal_id, amount, currency_id, ttype, date, context=None):

    res = super(account_voucher, self).onchange_partner_id(cr, uid, ids, partner_id=partner_id, journal_id=journal_id, amount=amount, currency_id=currency_id, ttype=ttype, date=date, context=context)

    res['value'].update({'invoice_id': False})

    if partner_id:

        invoice_ids = self.pool.get('account.invoice').search(cr, uid, [('partner_id','=',partner_id)], limit=1)

        if len(invoice_ids) > 0:

            res['value'].update({'invoice_id': invoice_ids[0]})

    return res

Avatar
Discard
Author

Thanks, Its working now the Invoice field is filtering with latest invoice of related customer...

but in the dropdown it is displaying all the remaining invoices from invoice table

can we make it as it should dispaly only related invoices for a particular customer which we are selecting

Author

I need the Invoices related to a particular customer, when I am using above code it returns all the Invoices from the account.invoice table I need to have the get the invoices of only selected customer in the dropdown as well...like if customer1 has only two invoices then in the dropdown it should display only two invoices not all the invoices available in invoice table am i clear

Okay. But how it relates to a function onchange? In this function ypu find an invoice of the related partner (one of those basically) but onchange doesn't restrict choice of invoices. If you need it, you should add a domain fo invoice_id. Sth like domain="[('partner_id','=',partner_id)]". Please Read the section 'Domains' here: https://www.odoo.com/documentation/8.0/reference/orm.html

Author

ok thanks..when I am using domain="[('partner_id','=',partner_id)]"...without quotes to partner_id it return as error while loading the server...when i use quotes for both 'partner_id' s it returns nothing

Make sure partner_id (right leaf) is placed placed on the related form (xml!).

It works basically in the same way as in search of onchange: [('partner_id','=',partner_id)]

Author

yeah...Tried in the right leaf itself still its giving server error when i use partner_id without quotes...when i use single quotes for partner_id it returns no data

Author

I was able to do it by using the domain in view i.e., in xml

as

<field name="invoice_id" domain="[('partner_id','=', partner_id)]">

It returns only related invoice_ids in dropdown of invoice field when a customer is selected

Thanks for the help @ IT Libertas

Author Best Answer

I was able to do it finally by using the domain for invoice_id in xml of my custom module as

<field name="invoice_id" domain="[('partner_id','=', partner_id)]">

which returns only related invoice_ids in dropdown of invoice field when a customer is selected


Thanks for the help @ IT Libertas

Avatar
Discard