How to avoid Many2many duplication in Odoo 13?

59 Views Asked by At

In Odoo 13, when I duplicate a sale order which has invoices linked to it, the new sale order has no invoices linked to it, which is OK.

The problem is that in my case, there are a lot of third party apps installed in my database, and here, when I duplicate a sale order, the new one has the same invoices linked to it than the source one, which obviously is a wrong behavior.

I am trying to fix it. So first I checked even through the interface that the following fields have copy=False:

  • The Many2many field invoice_lines from the model sale.order.line.
  • The Many2many field sale_line_ids from the model account.move.line.

Both seem to be OK, with copy=False. Why is this happening then? I've also modified the code to break the program if they are modified in create or write ORM methods of both models, but they aren't.

Any ideas of how can I locate the guilty code?

3

There are 3 best solutions below

0
forvas On BEST ANSWER

Finally I've found the problem: one of the multiple third party apps installed on the database adds a new Many2many field on sale.order pointing to account.move. This field has not copy=False. It shouldn't affect to sale order invoices link, but it does because the app also overwrites de method _get_invoiced() from sale.order, to add the value of that M2M field to the original invoice_ids. The combination of both actions is the problem.

I was able to fix it just adding a copy=False to that third party app new field. The problem was also fixed if I commented the _get_invoiced() overwrite, but I've chose the first option to preserve what that app does.

These are the reasons why checking sale.order copy() method or checking copy attribute on original fields gave no clues.

1
holydragon On

You might have a chance to find an inherited copy method in one of your third party custom modules. It should be on sale.order model because that's where the duplication happened. Try investigating from there.

1
Ahrimann On

sale.order relates to account.move using its field invoice_ids.

Maybe the original field invoice_ids (official addons) has been overridden in one of your third party module with copy=true ?

The original is located in addons/sale/models/sale_order.py:

class SaleOrder(models.Model):
    _name = 'sale.order'
    
    #...
    invoice_ids = fields.Many2many(
        comodel_name='account.move',
        string="Invoices",
        compute='_get_invoiced',
        search='_search_invoice_ids',
        copy=False)

To investigate the inherited models, you could inherit sale.order in one of your custom module and override the def copy. And Then use the "Step into" command of the (Pycharm-)Debugger to track the other possible overridden def copy in the other custom modules :

class SaleOrder(models.Model):
    _inherit = 'sale.order'
    
    def copy(self, default=None):
        # put a DEBUG break point before the following line:
        res_super = super(SaleOrder,self).copy(default=default)
        return res_super