Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

2

How to raise exception to prevent invoicing to customers with overdue invoices?

By
Santiago EV
on 1/26/16, 12:09 PM 432 views

I have this module that restricts invoicing to customers with overdue invoices.  The module works, but it takes about 10 minutes to confirm a sale.  In the meantime it hangs everything.  This is not practical so we are wondering what could be making it take so long.  Here is the code:


@api.model

def create(self, vals):

today = datetime.now().date()

inv_ids = self.search([('partner_id', '=', vals['partner_id']), ('state', '=', 'open'), ('type', '=', 'out_invoice'), ('date_due', '<', today)],)

if inv_ids:

raise Warning("You can not create invoice for this Customer. This customer already has one or more overdue invoices.")

return super(account_invoice, self).create(vals)

Offtopic: You should prevent delivering to customers with overdue invoices, not invoicing. Then it is too late :-)

Ermin Trevisan
on 1/27/16, 2:47 AM
0

Temur

--Temur--
2896
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 1/27/16, 5:51 AM

I guess all requirements in a search domain should be meet (?), so the multiple conditions you use should be connected them with "AND" operator, but they are with "OR" instead (operator is omitted, so default "OR" is used), and so it should always find all "out_invoice"-s for example and even more, united with ones that satisfy any other condition in your domain... it means if customer has already one invoice (or almost any invoice in system may have same effect), then he/she will be unable to make another, he/she will always get a warning "You can not create..." . You'll need to replace OR with AND.

Here is version with  AND  (it should be a bit faster because of nature of "and" operator in Short-Circuit Evaluation, if not considerable fast then at least it's a correct version):


    @api.model
def create(self, vals):
today = datetime.now().date()
inv_ids = self.search(['&', '&', '&', ('partner_id', '=', vals['partner_id']), ('state', '=', 'open'), ('type', '=', 'out_invoice'), ('date_due', '<', today)])
if inv_ids:
raise Warning("You can not create invoice for this Customer. This customer already has one or more overdue invoices.")
return super(account_invoice, self).create(vals)


Please try it. it's interesting if it'll considerable fast then the OR version...

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 1/26/16, 12:09 PM
Seen: 432 times
Last updated: 1/27/16, 5:51 AM