Help

2

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

Avatar
Marcio Valenzuela

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)

1 Comment
Avatar
Discard
Avatar
Ermin Trevisan
-

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

1 Answer
0
Avatar
Temur
Best Answer

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...

Avatar
Discard