Multi-company voucher - invoice mismatch?

OpenERP Configuration -

  1. Company Structure - Parent Holding Company, First Child Company, Second Child Company
  2. Voucher Number Pattern is same for all companies: SAJ/Year/Sequence Numbers
  3. Generate Quotation for First Child Company, confirm sale to generate Sales Order and Invoice and then make payment to generate payment voucher.
  4. Generate Quotation for Second Child Company, confirm sale to generate Sales Order and Invoice and then make payment to generate payment voucher.
  5. Confirm that the invoice numbers are same for both the generated invoices.

Problem Statement -

  1. Open the above generated Payment for the First Child Company and Print receipt. As the invoice numbers are same for both the generated invoices, this prints receipt with the correct header information but line items from the invoice of the Second Child Company.

Debugging - Looking at receipt printing related code revealed that this line is picking up invoice based on the invoice number whereas it could have been done based on the invoice id which is stored in the account_voucher. When tried that it still did not work.

Further debugging revealed that the invoice_id gets modified the moment the older payment among the two related payments mentioned above is opened.

which explains why change only in printing did not work.

This bug (if it is!) is most likely due to this line as it fetches the first invoice based on only the invoice number (it may give wrong result if the invoice numbers are same in multi-company scenario) and it updates the voucher’s invoice reference here

Suggested solution -

  1. No Code change - Avoid duplication of invoice numbers (Safe!) by having unique invoice number generation pattern for every company e.g. prefix it with short company name.

  2. Code Change - Qualify this search with the company id so that it fetches the correct invoice.

    inv_ids = self.pool.get("account.invoice").search(cr, uid,[('number','=',inv_no),('company_id', '=',voucher.company_id.id)])
    

    Similarly for printing, qualify this search too with the company id

    inv_ids = self.pool.get("account.invoice").search(cr, uid,[('number','=',inv_no),('company_id', '=',voucher.company_id.id)])