# Product Overrides

Product overrides happen when one of the elements (Calculation, RiskField, RiskItem, RateTable or RateTableSource) is updated/edited in the child product.

For example, if we had a RiskField that we were inheriting from the parent product, which was a string and allowed a maximum of 30 characters, and we update that field on the child product to allow a maximum of 50 characters instead. The field will remain the same on the parent product, and it will be "overridden" on the child product. Internally the system will duplicate the RiskFieldVersion and the RiskField instances, and then update this new duplicated instances with the new values. The updated field will be shown on the list of fields with a label saying "overridden".

# Implementation

In order to that, there is a helper class called ProductOverrider which is defined in britelines/apps/lines/helpers.py. This class defines a method for each version model (override_risk_item_version, override_calculation_version, override_risk_field_version, etc.) and also a method duplicate that takes care of the actual duplication of the version and instance models

In order to determine if a VersionedLineEntity should be duplicated, the ProductOverrider class calls the inheritance_status method that is defined in the abstract model VersionedLineEntity. If the result of inheritance_status is inherited, then we proceed with the duplication of the Version and instance model (and any related field that might need duplication).

The VersionedModelSerializerBase then defines a method called check_override which instantiates the ProductOverrider and calls the proper overrider method for the current version model. The check_override methods is always called when updating a versioned model.

# Cascade Duplication

Duplication needs to happen in cascade, this means for example, if we are duplicating a RiskItemVersion that has calculations defined, we also need to duplicate those calculations. If we are duplicating a CalculationVersion we also need to duplicate the SharedCalculationOrder or ItemCalculationOrder, depending on which type of calculation it is.

It is important to make sure we don't duplicate unnecessary models. We should only duplicate those instances that are being updated.