Calculated custom field for provider prices tab in product module

In products module configuration I have created the following custom fields in the provider prices tab:

Margen comercial (margen) (for the commercial margin)
PVP recomendado (pvprecom) (for the recommended RRP)

Mi goal is to make commercial margin to calculate price / (1 - (margen / 100))
Example:

Cost price: 100€
Margin: 25%
RRP = 100 / (1 - (25 / 100)) = 100 / (1 - 0,25) = 100 / 0,75 = 133,33€

In a calculated field I set:

$object->price / (1 - ($object->array_options[‘margen’] / 100))

And it did not work :frowning: . The problem is that $object in this context refers to llx_product table, but it should refer to llx_product_fourniseur_price, since these are custom fields for provider prices tab.

I don’t know if this has to be considered as a bug or there is a way of retrieve the llx_product_fourniseur_price table fields in this context (since this extrafields are for this table, not for llx_product).

I expect to have been clear enough.

Thanks for your help.

Muchas gracias

You can use the following formula in your PVP recomendado as calculated field:

(($reloadedobj = new ProductFournisseurPrice($db)) && ($reloadedobj->fetchNoCompute($objectoffield->id) > 0)) ? round($reloadedobj->price / (1-($reloadedobj->array_options['options_margen']/100)),2) : $reloadedobj->price

But be aware that you have to do save twice if you change the price or margen.
The extrafield function can save only one value at time.

Dolibarr-Version: 17.0.3
regards

It works like a charm and it is simply great!!!

Thanks a lot for your straight, concise and helpful answer.

Is there kind of a reference of documentation of variables and functions you used?

Best.

:smiley:
I did something similar for our company and so it’s not that difficult to develop.
But sometimes it’s trial and error and also searching the source code was helpful for me. Otherwise search here in the forum or in the wiki.
And also inserting “var_dump(…);exit;” gives a clue as to which variables are requested or returned.
The debugger is also helpful when it comes to SQL queries.

regards