439
edits
(→Dynamic discount computation per products lines on propales: Fix, must use subprice and not price) |
(Added Custom tax per product) |
||
| Line 346: | Line 346: | ||
Here we will go through the creation and management of a custom field called '''coefficient''' that will change the total price of each product 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 total price of each product by modifying it by the coefficient. | ||
NOTE: this will not create a coefficient per product but rather per product's line. Thus if you want to create a coefficient that never change between invoices but only depending on the product and stays static, please refer to the next chapter below about creating a custom tax per product. | |||
The preliminary and necessary step is to first create a custom field that will be used to modify the total price. We will create one for Client Invoice Lines. | The preliminary and necessary step is to first create a custom field that will be used to modify the total price. We will create one for Client Invoice Lines. | ||
| Line 554: | Line 556: | ||
Technical note: here we are using customfields_fill_object() instead of customfields_fill_object_lines() (the latter being specifically adapted to lines and thus should theoretically be used here) for performance reasons because the template file we are editing is called in a loop, and customfields_fill_object_lines() should be called outside a loop (because it already does the job that is done by the loop), so here we 'tricks' the customfields_fill_object() function to avoid this loop by submitting 'facturedet' as a module by itself (when in fact it's a product lines table, but it works the same way nevertheless). This is a good demonstration of the flexibility of the CustomFields functions and methods. | Technical note: here we are using customfields_fill_object() instead of customfields_fill_object_lines() (the latter being specifically adapted to lines and thus should theoretically be used here) for performance reasons because the template file we are editing is called in a loop, and customfields_fill_object_lines() should be called outside a loop (because it already does the job that is done by the loop), so here we 'tricks' the customfields_fill_object() function to avoid this loop by submitting 'facturedet' as a module by itself (when in fact it's a product lines table, but it works the same way nevertheless). This is a good demonstration of the flexibility of the CustomFields functions and methods. | ||
= Custom tax per product = | |||
This part will show you how to create a static custom tax per product. Static means that the tax will be stored per product, and not per product's line, thus you just have to setup the tax once per product and it will always be applied in every invoice you add this product. | |||
To do that, you will need the CustomFields Pro module + ProductsEasyAccess. | |||
1- Create a product's custom fields called "custom_tax". Now, using ProductsEasyAccess, you can access this field wherever you wants, either to show it on the Dolibarr's interface (see previous chapter), or in a PDF template, which we will describe below. | |||
2- Modify your invoice's PDF template. You first need to load all the products' fields ''before'' the loop over the products' lines. To do this, search for a line like this: | |||
<source lang="php"> | |||
// This should be placed above the "loop lines" below, add this: | |||
dol_include_once('/productseasyaccess/lib/productseasyaccess.lib.php'); | |||
fill_object_with_products_fields($object); | |||
// Loop on each lines, search for this command | |||
for ($i = 0 ; $i < $nblignes ; $i++) | |||
// Add this just below the for loop, this will ease your php manipulation after. | |||
// This will allow you to more easily access the products' fields (this will store the id of each product inside $lineproductid). | |||
$line_product_id = $object->lines[$i]->fk_product; | |||
</source> | |||
4- You can now use your products' fields in your PDF. To access the custom_tax field, just use: | |||
<source lang="php"> | |||
$object->productslines->$line_product_id->cf_custom_tax | |||
</source> | |||
And to print it in your PDF: | |||
<source lang="php"> | |||
$pdf->MultiCell(0,3, $object->productslines->$line_product_id->cf_custom_tax, 0, 'L'); | |||
</source> | |||
5- Then, if you want to count the custom tax total and add it to your vat total, you can create a counter inside the for loop we cited in the first step: | |||
<source lang="php"> | |||
$object->total_custom_tax = 0; // Initialize the custom tax total counter | |||
// Loop on each lines | |||
for ($i = 0 ; $i < $nblignes ; $i++) | |||
$object->total_custom_tax = $total_custom_tax + $object->productslines->$line_product_id->cf_custom_tax; | |||
</source> | |||
Then find inside the function **_tableau_tot**, find the following line: | |||
<source lang="php"> | |||
$pdf->MultiCell($largcol2, $tab2_hl, price($sign * $object->total_ttc, 0, $outputlangs), $useborder, 'R', 1); | |||
</source> | |||
And prepend the total modification like this: | |||
<source lang="php"> | |||
$object->total_ttc = $object->total_ttc + $object->total_custom_tax; | |||
$pdf->MultiCell($largcol2, $tab2_hl, price($sign * $object->total_ttc, 0, $outputlangs), $useborder, 'R', 1); | |||
</source> | |||
Done. This should show your custom tax per product and your total including all custom taxes. | |||
edits