콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
2 답글
5360 화면

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 :-)

베스트 답변

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

아바타
취소
관련 게시물 답글 화면 활동
3
5월 19
12173
1
7월 25
752
2
7월 25
854
1
7월 25
1928
3
4월 25
1943