Line 343:
Line 343:
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 =
+
= Modify the price of every product/service line with a coefficient (without tampering the discount) =
−
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.
+
Here we will go through the creation and management of a custom field called '''coefficient''' that will change the subprice of each product by modifying it by the coefficient.
−
The preliminary and necessary step is to first create a custom field that will be used to modify the total price. Then, the following sub-chapters are all independent, thus you can only follow the ones that you require, eg: if you only need to change the price in your PDF documents, just create a custom field then jump to the sub-chapter to Implement the change of price in PDF.
+
The preliminary and necessary step is to first create a custom field that will be used to modify the subprice. We will create one for Client Invoice Lines.
−
−
== 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'''.
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'''.
Line 359:
Line 355:
You can see that our coefficient field was successfully created.
You can see that our coefficient field was successfully created.
−
== Implementing the change of price in ODT templates ==
+
Then here are two alternative ways of implementing the change of the subprice relativery to this coefficient.
+
+
== Change subprice in database ==
+
+
Here we will use a "save" overloading function to change the subprice directly in the database, thus we won't have to modify anything else (eg: in PDT or ODT) since the subprice will be already updated in the database.
+
+
Warning: this method will work only with CustomFields => 3.2.21 since the "save" overloading function did not work for products lines before.
+
+
Just add the following overloading function in your customfields_fields_extend.lib.php file:
+
+
<source lang="php">
+
function customfields_field_save_facturedet_vl (&$currentmodule, &$object, &$action, &$user, &$customfields, &$field, &$name) {
+
global $db; // allow to access database functions
+
+
// Fetch the new posted values
+
// All $_POST data are generically appended into the $object properties
+
$coefficient = $object->{$customfields->varprefix.'coefficient'};
+
+
// Clean parameters
+
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
+
$coefficient = price2num($coefficient);
+
+
// Get the original subprices (it's not automatic when we edit remise_percent and update, we have to recompute manually the prices before calling $object->update())
+
global $mysoc;
+
$localtaxes_type=getLocalTaxesFromRate($object->tva_tx,0,$mysoc);
+
$tabprice=calcul_price_total($object->qty, $object->price, $object->remise_percent, $object->tva_tx, $object->localtax1_tx, $object->localtax2_tx, 0, 'HT', $object->info_bits, $object->type, '', $localtaxes_type);
+
$object->total_ht = $tabprice[0];
+
$object->total_tva = $tabprice[1];
+
$object->total_ttc = $tabprice[2];
+
$object->total_localtax1 = $tabprice[9];
+
$object->total_localtax2 = $tabprice[10];
+
+
// Update the subprices with the coefficient
+
$object->total_ht = $object->total_ht * $coefficient; // compute no tax price
+
$object->total_tva = $object->total_tva * $coefficient; // compute tax amount
+
$object->total_ttc = $object->total_ttc * $coefficient; // compute tax included price
+
+
// Update into database
+
$object->update(true); // true to disable the trigger, else it will recall customfields, which will call this overloading function, etc. in an infinite loop.
+
$object->update_total();
+
+
// Finally, cleanup POST data to avoid conflicts next time, reinserting the same discount. Else the fields will remember these values and it may mistakenly reuse the same values.
+
unset($_POST[$customfields->varprefix."coefficient"]);
+
}
+
</source>
+
+
This should show the correct subprice on the Dolibarr's interface. You have nothing to edit in your PDF or ODT template, since the new subprice with applied coefficient will be automatically printed instead of the old one without coefficient.
+
+
== Change printed subprice without tampering the subprice in the database ==
+
+
Here is a way to change the printed subprice (the one shown on screen and in PDF/ODT) but without changing the subprice in the database.
+
+
The following sub-chapters are all independent, thus you can only follow the ones that you require, eg: if you only need to change the price in your PDF documents, just create a custom field then jump to the sub-chapter to Implement the change of price in PDF.
+
+
=== Implementing the change of price in ODT templates ===
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]].
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]].
Line 384:
Line 434:
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.
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 templates ==
+
=== Implementing the change of price in PDF templates ===
First, we will create our own PDF template by copying an existing template, and then we will edit it to change the price of items.
First, we will create our own PDF template by copying an existing template, and then we will edit it to change the price of items.
−
=== Creating your own PDF template by copying an existing template ===
+
==== '''Creating your own PDF template by copying an existing template''' ====
Line 413:
Line 463:
Now, let's proceed onto modifying the prices.
Now, let's proceed onto modifying the prices.
−
=== Changing the prices automatically in your PDF ===
+
==== '''Changing the prices automatically in your PDF''' ====
You can now do pretty much anything you want in your PDF template, but I will give you a mean to automatically change the prices of every products with our coefficient, as well as the total price, without editing too much the template.
You can now do pretty much anything you want in your PDF template, but I will give you a mean to automatically change the prices of every products with our coefficient, as well as the total price, without editing too much the template.
Line 464:
Line 514:
</source>
</source>
−
== Implementing the change of price in the Dolibarr interface ==
+
=== 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 '''template file''' and manually put inside your code to load your custom field and print it the way you want using HTML formatting, for our example:
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 '''template file''' and manually put inside your code to load your custom field and print it the way you want using HTML formatting, for our example: