Add Extrafields on PDF Models: Difference between revisions

From Dolibarr ERP CRM Wiki
Jump to navigation Jump to search
No edit summary
Tag: 2017 source edit
No edit summary
Tag: 2017 source edit
Line 107: Line 107:
*Result : [[File:Invoices module template Modification Result 2.png|none|thumb]]
*Result : [[File:Invoices module template Modification Result 2.png|none|thumb]]


= Special Case for Extrafields type Select list =
=Special Case for Extrafields type "Select list"=


==Configuration==
==Configuration==
Line 120: Line 120:
On the php invoice template, if you use the classical php code to print the extrafield, you will print the code (1 or 2 in our exemple) instead of printing the label (Hello Word or Hello Alone in our exemple).
On the php invoice template, if you use the classical php code to print the extrafield, you will print the code (1 or 2 in our exemple) instead of printing the label (Hello Word or Hello Alone in our exemple).


To display the label instead of the code, you shoudl replace the :
To display the label instead of the code, you should replace the :
<source lang="php">$outputlangs->convToOutputCharset($object->array_options['options_my_extra'])</source>
<source lang="php">$outputlangs->convToOutputCharset($object->array_options['options_my_extra'])</source>
by :
by :
<source lang="php">$extrafields->showOutputField('my_extra', $object->array_options['options_my_extra'])</source>
<source lang="php">$extrafields->showOutputField('my_extra', $object->array_options['options_my_extra'])</source>
=Special Case for Extrafields type "Date"=
==Configuration==
You can create a line or a module extrafield with the type '''Date'''
[[File:Date Extrafield.png|none|thumb]]
==Verification on the Invoices Module==
You should have a Module or Line extrafield (on this print screen this a Module)
[[File:Date Extrafield Invoice.png|none|thumb]]
==Invoice Template==
On the php invoice template, if you use the classical php code to print the extrafield, you will print the timestamp (1570450273 for exemple) instead of printing the date in human readable format (10/7/2019 12:11:13).
To display the date in human readable format, you should replace the :
<source lang="php">$outputlangs->convToOutputCharset($object->array_options['options_my_extra'])</source>
by :
<source lang="php">dol_print_date($object->array_options['options_my_extra'],"dayhour",false,$outputlangs,true)</source>

Revision as of 12:24, 7 October 2019

This page give you some advises how to integrate complementary attributes (extrafields) into PDF models of Dolibarr.

This is a exemple and needs to be adapted to your needs. It is based on billing document but it will be the same for the others modules

Prerequisite

You can read the following links before, some paragraph will come from these pages :

Create a Complementary Attributes on Invoices Module

First you need to create your complementary attribute. This complementary attributes can be of two types :

  • Module complementary attribute
  • Line complementary attribute

Access to the configuration

Go on the Dolibarr application > Setup > Modules/Applications

Click on the parameter icon Cog.png of the Invoices module Then you can create Module or Line complementary attribute

Module complementary attribute

Go on the Complementary attributes (invoices) tab

Invoices module setup.png

Put the all the mandatory fields, especially the Attribute code that will be used in the next chapters

You should end up with something like that :

Invoices module setup Step 2.png

Line complementary attribute

Go on the Complementary attributes (lines) tab

Invoices module setup line.png

Put the all the mandatory fields, especially the Attribute code that will be used in the next chapters

You should end up with something like that :

Invoices module setup line Step 2.png

Verification on the Invoices Module

Clic on Billing|Payement and select New invoice

If you have created a Module complementary attribute, you should have that :

Invoices module extrafield.png

If you have created a Line complementary attribute, you should have that :

Invoices module line extrafield.png


Create a new invoice template

All the steps are describes there Create a PDF document template

  • Open the core\modules\facture\doc\ directory
  • Copy the pdf_crabe.modules.php
  • Rename the copy as pdf_my_template.modules.php
  • Edit and make the following changes in the code:
  1. Rename class pdf_crabe to class pdf_my_template
  2. Rename constant $this->name = "crabe"; to $this->name = "my_template";
  3. Update the $this->description with your description
  4. Save file. Now the template will be available in the list of models in Dolibarr
    Invoices module setup template.png
  5. Test this model (by generating a pdf) before going further ...

Modify the new invoice template

  • Open the new template pdf_my_template.modules.php
  • Look for the last require_one lines ate the beginning of the template
  • Add the line
    require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
    
    Add extrafields in pdf 1.png

Module complementary attribute

  • Find the
    function write_file
    
  • Just before the
    // Affiche notes
    
  • Add the code to load extrafield array in object
    $extrafields = new ExtraFields($db);
    $extralabels=$extrafields->fetch_name_optionals_label($object->table_element);
    
  • Add the code to load extrafield into object
    $object->fetch($rowid);
    $object->fetch_optionals($rowid,$extralabels);
    
  • Add the code to print the extrafiled
    $pdf->writeHTMLCell (190,3, $this->posxdesc-1, $tab_top-5, $outputlangs->convToOutputCharset($object->array_options['options_my_extra']),0,1);
    
    Invoices module template Modification.png
  • Result :
    Invoices module template Modification Result.png

Line complementary attribute

  • Find
    $object->fetch_thirdparty();
    
  • Add, just after, the code to load extrafield array in object
    $extrafields = new ExtraFields($db);
    $extralabels=$extrafields->fetch_name_optionals_label($object->table_element);
    
    Add extrafields line in pdf 1.png
  • Find
    // Loop on each lines
    for ($i = 0; $i < $nblignes; $i++)
    {
    
  • Add, just after, the code to load line extrafield into object
    $object->lines[$i]->fetch_optionals($object->lines[$i]->rowid,$extralabelsline);
    
    Add extrafields line in pdf 2.png
  • Find
    $posYAfterDescription=$pdf->GetY();
    
  • Add, just before, the code to display the line extrafiled
    $pdf->MultiCell(0, 3, $outputlangs->convToOutputCharset($object->lines[$i]->array_options['options_my_line_extra']), 0, 'L', 0);
    
    Add extrafields line in pdf 3.png
  • Result :
    Invoices module template Modification Result 2.png

Special Case for Extrafields type "Select list"

Configuration

You can create a line or a module extrafield with the type Select list

List Extrafield.png

Verification on the Invoices Module

You should have a Module or Line extrafield (on this print screen this a Module)

List Extrafield Invoice.png

Invoice Template

On the php invoice template, if you use the classical php code to print the extrafield, you will print the code (1 or 2 in our exemple) instead of printing the label (Hello Word or Hello Alone in our exemple).

To display the label instead of the code, you should replace the :

$outputlangs->convToOutputCharset($object->array_options['options_my_extra'])

by :

$extrafields->showOutputField('my_extra', $object->array_options['options_my_extra'])

Special Case for Extrafields type "Date"

Configuration

You can create a line or a module extrafield with the type Date

Date Extrafield.png

Verification on the Invoices Module

You should have a Module or Line extrafield (on this print screen this a Module)

Date Extrafield Invoice.png

Invoice Template

On the php invoice template, if you use the classical php code to print the extrafield, you will print the timestamp (1570450273 for exemple) instead of printing the date in human readable format (10/7/2019 12:11:13).

To display the date in human readable format, you should replace the :

$outputlangs->convToOutputCharset($object->array_options['options_my_extra'])

by :

dol_print_date($object->array_options['options_my_extra'],"dayhour",false,$outputlangs,true)