Changes

Line 322: Line 322:     
== 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.
 +
 +
=== Creating your own PDF template by copying an existing template ===
 +
 +
 +
Go into the following folder: '''htdocs/core/modules/facture/doc/''', then copy the file '''pdf_crabe.modules.php''' and name the new one '''pdf_crabecoeff.modules.php'''.
 +
 +
Then open it in your favourite editor, and change the following lines:
 +
<source lang="php">
 +
class pdf_crabe extends ModelePDFFactures // change pdf_crabe into pdf_crabecoeff
 +
{
 +
...
 +
    function __construct($db)
 +
    {
 +
        ...
 +
$this->name = "crabe"; // change "crabe" into "crabecoeff"
 +
        ...
 +
    }
 +
}
 +
</source>
 +
 +
That's it, now you have your own PDF template!
 +
 +
Now you should '''enable your PDF template''' before doing anything else, by going into the Modules Administration panel, and into the Invoice admin page, then click on the ON button at the right of the '''crabecoeff''' entry to enable the template.
 +
 +
Now, let's proceed onto modifying the prices.
 +
 +
=== 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.
 +
 +
First, try to find the following code of line (should be inside the write_file() method):
 +
<source lang="php">
 +
$pdf=pdf_getInstance($this->format);
 +
</source>
 +
 +
Now, just copy/paste the following code '''below''' pdf_getInstance():
 +
<source lang="php">
 +
// Init and main vars for 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)
 +
$this->customfields = customfields_fill_object($object, null, $outputlangs, null, true); // beautified values
 +
$this->customfields_raw = customfields_fill_object($object, null, $outputlangs, 'raw', null); // raw values
 +
$this->customfields_lines = customfields_fill_object_lines($object, null, $outputlangs, null, true); // product lines' values
 +
 +
// Resetting every $object price because we will compute them anew
 +
$object->total_ht = 0;
 +
$object->total_tva = 0;
 +
$object->total_ttc = 0;
 +
// For each line of product/service, we will recompute all the prices
 +
foreach($object->lines as $line) {
 +
    // Get the coefficient custom field's value for the current line
 +
    $coeff = $object->customfields->lines->{$line->rowid}->cf_coefficient;
 +
    // Recompute the price of this product/service line
 +
    $line->total_ht = $line->total_ht*$coeff; // compute no tax price
 +
    $line->total_tva = $line->total_tva*$coeff; // compute tax amount
 +
    $line->total_ttc = $line->total_ttc*$coeff; // compute tax included price
 +
    // Do the sum of all new products/services prices and tva (will be incremented with every product/service line)
 +
    $object->total_ht += $line->total_ht; // global no tax price
 +
    $object->total_tva += $line->total_tva; // global vat/tax amount
 +
    $object->total_ttc += $line->total_ttc; // global  tax included price
 +
}
 +
</source>
 +
 +
That's it, now you should try to generate your PDF document, and you should get something like this:
 +
[[File:Cfe3.jpg]]
 +
 +
Note: printing the coefficient as a column in your PDF document is also possible but will not be covered here because PDF templates will vary for every persons depending on your needs.
 +
For guidelines about how to do this, you can read the documentation on [[Create_a_PDF_document_template|how to create a PDF template]] and [[Module_CustomFields#Implementing_in_PDF_templates|how to use custom fields in PDF templates]].
    
== Implementing the change of price in the Dolibarr interface ==
 
== Implementing the change of price in the Dolibarr interface ==
439

edits