Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
2 Risposte
5341 Visualizzazioni

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)

Avatar
Abbandona

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

Risposta migliore

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
Abbandona
Post correlati Risposte Visualizzazioni Attività
3
mag 19
12168
1
lug 25
718
2
lug 25
814
1
lug 25
1895
3
apr 25
1909