Line 279:
Line 279:
Thank's to netassopro for the tip.
Thank's to netassopro for the tip.
+
+
= Modify the price of every product/service line with a coefficient =
+
+
Here we will go through the creation and management of a custom field called '''coefficient''' that will change the total price by modifying it by the coefficient.
+
+
== Creation of the custom field called coefficient ==
+
+
First you have to create a custom field. We will create one for Client Invoice Lines.
+
+
Go to the CustomFields administration panel, then into the Client Invoice Lines tab, then click on the button New Field, name the field '''coefficient''', and set the type to '''Double'''.
+
+
When you go to client invoices in Dolibarr, you should then see something similar to this:
+
[[File:Cfe1.jpg]]
+
You can see that our coefficient field was successfully created.
+
+
== Implementing the change of price in ODT ==
+
+
There's no eval implementation currently inside ODT templates, but there is a kind of hook implementation, called [[Create_an_ODT_document_template#Other_personalized_tags|substitution functions]].
+
+
You can either create your own module, then create your own substitution function, but for the sake of simplicity in this tutorial, we will directly use the CustomFields substitution function. However, please remember that you will have to redo your changes everytime you will update the CustomFields module, so if you don't want to do that it would be better if you create your own module and substitution function.
+
+
Open the file '''htdocs/customfields/core/substitutions/functions_customfields.lib.php''' and go to the function '''customfields_completesubstitutionarray_lines'''.
+
+
At the very end of this function, but inside the customfields check:
+
+
<source lang="php">
+
if ($conf->global->MAIN_MODULE_CUSTOMFIELDS) {
+
</source>
+
+
But just before the ending bracket, you should add the following code:
+
<source lang="php">
+
if ($object->table_element_line === 'facturedet') { // First check that we are in the right module, we don't want to use coefficient for other modules if we didn't define the custom field there
+
$substitutionarray['line_price_full'] = $substitutionarray['line_price_ttc'] * $substitutionarray['cf_coefficient']; // multiply the total price with our coefficient
+
}
+
</source>
+
+
This code will add a new tag '''{line_price_full}''' that you can then use inside your ODT templates.
+
+
Just place this tag in a table in your ODT document, between the table tags ( [!-- BEGIN row.lines --] ... [!-- END row.lines --] ), and this will print your full price.
+
+
== Implementing the change of price in PDF ==
+
+
== Implementing the change of price in the Dolibarr interface ==
+
+
Unluckily there is not (yet) any hooking context to modify how lines of products/services are printed, thus the only thing you can do is directly edit the '''htdocs/core/tpl/objectline_view.tpl.php''' template file, then load your customfield manually and print it the way you want using HTML formatting, for example:
+
+
<source lang="php">
+
+
</source>