Help

4

Best way to warn user a supplier invoice is already entered (match on supplier invoice number)

Avatar
Ray Carnes

What would be the best way to implement a "duplicate check" when users are recording a supplier invoice?

The aim would be to avoid accountants entering the same invoice twice, based on the supplier invoice number.

It shouldn't prevent them from entering the invoice - just warn them that an existing invoice is already in the system with the same number.

constraints prevent duplicate entry, the objective here is just to detect a potential duplicate and warn the user - letting them enter it again if they need to.

Avatar
Discard
1 Answer
1
Avatar
Ray Carnes
Best Answer

I ended up writing a function:

    def onchange_supplier_invoice(self, cr, uid, ids, reference, context=None):

        msg = reference + 'has already been used!' 
        res = {'reference': reference, }

        if not reference:
            return res

        # Get all Supplier Invoices
        invoice_ids = self.search(cr, uid, [('type','=','in_invoice')], context)

        # Get all the references for all Supplier Invoices
        invoices = self.read(cr, uid, invoice_ids, fields=['reference'],context=context)

        # Check for duplicates
        for inv in invoices:
            if inv['reference'] == reference:
                raise osv.except_osv('Possible duplicate Invoice!',msg)

        return res

And triggering the function via on_change in the relevant field (in this case reference).

<field name="reference" string="Invoice Number" 
       attrs="{'required':[('state','!=','draft')]}"  
       on_change="onchange_supplier_invoice(reference)"/>
1 Comment
Avatar
Discard
Avatar
Ray Carnes
-

Depending on the use case, searching for Supplier Invoices matching the current supplier (if populated) may be better.