439
edits
(→Linking Dolibarr objects from two different modules: fix mistake with php object creation) |
(→Change printed total price per product without tampering the subprice in the database: Adding subsection about adding a coefficient column) |
||
| Line 522: | Line 522: | ||
</source> | </source> | ||
=== Implementing the change of price in the Dolibarr interface === | === Implementing the change of total 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: | ||
| Line 558: | Line 558: | ||
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. | ||
=== Implementing the coefficient column in the Dolibarr interface === | |||
It's also possible to show the coefficient in a new column on the Dolibarr interface. | |||
To do that, you need to do two things: | |||
1- Add the column title "Coeff." | |||
2- Add the generator that will input the coefficient value for each line. | |||
==== Add the column title "Coeff." ==== | |||
Open in your favourite editor the file /htdocs/core/class/commonobject.class.php and inside the function printObjectLines (note the plural s), find the following lines of code: | |||
<source lang="php"> | |||
// Total HT | |||
print '<td align="right" width="50">'.$langs->trans('TotalHTShort').'</td>'; | |||
</source> | |||
This is what prints the Total HT price column. We will place the Coeff column just before. | |||
So, just above this code, add the following: | |||
<source lang="php"> | |||
// Coefficient (CustomFields) | |||
dol_include_once('/customfields/class/customfields.class.php'); | |||
$customfields = new CustomFields($this->db, $object->table_element_line); | |||
$coeff_field = $customfields->fetchFieldStruct('coefficient'); | |||
if (!empty($coeff_field)) { | |||
print '<td align="right" width="50">'.$langs->trans('Coeff.').'</td>'; | |||
} | |||
</source> | |||
This will take care of showing the Coeff. column, but only for modules where you created such a custom field (because the file we modified is a common object for all modules in Dolibarr, not just invoices, thus this modification will gracefully skip this custom field if it does not exists for other modules). | |||
==== Add the generator ==== | |||
Open the file /htdocs/core/tpl/objectline_view.tpl.php and try to find the following block of code: | |||
<source lang="php"> | |||
<?php if ($line->special_code == 3) { ?> | |||
<td align="right" class="nowrap"><?php $coldisplay++; ?><?php echo $langs->trans('Option'); ?></td> | |||
<?php } else { ?> | |||
<td align="right" class="nowrap"><?php $coldisplay++; ?><?php echo price($line->total_ht); ?></td> | |||
<?php } ?> | |||
</source> | |||
This is what prints the total HT price value for each line. | |||
Just above of this block of code, add the following: | |||
<source lang="php"> | |||
<?php | |||
// Coefficient (CustomFields) | |||
// Include the facade API of CustomFields | |||
dol_include_once('/customfields/lib/customfields_aux.lib.php'); | |||
// Filling the $object with customfields (you can then access customfields by doing $object->customfields->cf_yourfield) | |||
$linecf = new stdClass(); | |||
$linecf->id = (!empty($line->id))?$line->id:$line->rowid; | |||
$linecf->table_element = $object->table_element_line; // you can use $this->table_element_line here instead of 'facturedet' if you want this feature to work for every module supported by CustomFields | |||
$customfields = customfields_fill_object($linecf); | |||
if (isset($linecf->customfields->cf_coefficient)) { | |||
?> | |||
<td align="right" class="nowrap"><?php $coldisplay++; ?><?php echo $linecf->customfields->cf_coefficient; ?></td> | |||
<?php } ?> | |||
</source> | |||
Just like for the column title, this code will take care of generating the coefficient value only if the custom field is defined, because this file is also a generic template for all modules of Dolibarr. | |||
At this point, everything is done, you should see the following: | |||
= Custom tax per product = | = Custom tax per product = | ||
edits