How to correct an error in product duplication


A user duplicated a product to create a new one but edited the original one instead of the duplicated. We realized this as for the original product, no stock was visible (0 for all locations, even CLIENT) and no move showed up that product. I suppose it was because products are internally linked everywhere by their id and not by their title or code.

I corrected this by swapping code, infos and title between the two products. Now the history of all docs and the stocks are right for the original product.

But… since a week all stock moves and invoices are made with the wrong product (facing as the new one, code and title and price…). Now that I swapped the infos, stock is all wrong concerning the new product and the invoices don’t appear in Tryton the same as they are on generated reports.

How is it possible to repair this (swap a product by another in all docs after a given date)?
Or should we cancel all moves, sales, purchases and regenerate them with the (real) new product?

For me, there are only two options and choosing depend on the expertise:

  • Update in SQL the product field of the sale line, stock move and invoice line to use the right one. But this requires high level of expertise to not make mistakes.

  • Cancel the orders and re-encode them with the proper product (could use the copy feature).

PS: I’m wondering if the copy feature should be limited to power users only?

IMHO I don’t think so. If the user has permission to create some model is reasonable for me that can copy it too because copy is more or less the equivalence to a semi-automatic create action.

Moreover it used to be one of the most valuable features to the user.

My two cents.

in order to avoid such problem, it should be possible to append some static string to the name ? once duplicated, the new product would be name “my old name (duplicate)”.

it could help the end-user to see the product to edit (even if once duplicated the current product is the new one)

Not sure it will help because there is not always a “name”. Or there may be other more important fields than name. In the case of product for some people the code is much more important.
Also the ModelStorage.copy is used internal for specific behavior like splitting stock moves. In this case we expect to have the same values.
The only case were such change is needed is when a unique constraint exists. But usually in this case we reset the field to a new default value.

Maybe the real solution is to fill the product code automatically like for party.

I agree that will be a good adition to allow to define a sequence for product code like we do for party code.

Indeed we agreed that code should be optionally filled by a sequence

But I think there should be no default sequence created by default (unlike for party).

What about an optional default string prepended after duplication? For products, it could be ‘DUPLICATA’ in name.
Maybe the problem is also in the fields displayed in list mode: the id of the records are not visible for products, showing only the name.
Or maybe allow to duplicate only in detailed view and not in list view? this way the duplicated item is the one being presented.

This would require a second save which may be annoying and/or skipped by the user.
Also it will not work on list because record that is not the currently selected must always be saved.

This will reduce a lot the power of the feature. Not sure it is going in the right direction.

Can you confirm that the concerned tables are only stock_move, sale_line, account_invoice_line and purchase_line?

You can look at the “referenced by” from table description of product_product and product_template.
Such as:

# \d product_product
# \d product_template

I’ve created Issue 8545: Fill product code automatically - Tryton issue tracker which implements it. Any feedback will be much appreciated.