dolibarr  13.0.2
commondocgenerator.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
5  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
6  * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
7  * Copyright (C) 2016 Charlie Benke <charlie@patas-monkey.com>
8  * Copyright (C) 2018-2020 Frédéric France <frederic.france@netlogic.fr>
9  * Copyright (C) 2020 Josep Lluís Amador <joseplluis@lliuretic.cat>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <https://www.gnu.org/licenses/>.
23  * or see https://www.gnu.org/
24  */
25 
36 abstract class CommonDocGenerator
37 {
41  public $error = '';
42 
46  public $errors = array();
47 
51  protected $db;
52 
56  public $extrafieldsCache;
57 
63  public function __construct($db)
64  {
65  $this->db = $db;
66  }
67 
68 
69  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
77  public function get_substitutionarray_user($user, $outputlangs)
78  {
79  // phpcs:enable
80  global $conf, $extrafields;
81 
82  $logotouse = $conf->user->dir_output.'/'.get_exdir($user->id, 2, 0, 1, $user, 'user').'/'.$user->photo;
83 
84  $array_user = array(
85  'myuser_lastname'=>$user->lastname,
86  'myuser_firstname'=>$user->firstname,
87  'myuser_fullname'=>$user->getFullName($outputlangs, 1),
88  'myuser_login'=>$user->login,
89  'myuser_phone'=>$user->office_phone,
90  'myuser_address'=>$user->address,
91  'myuser_zip'=>$user->zip,
92  'myuser_town'=>$user->town,
93  'myuser_country'=>$user->country,
94  'myuser_country_code'=>$user->country_code,
95  'myuser_state'=>$user->state,
96  'myuser_state_code'=>$user->state_code,
97  'myuser_fax'=>$user->office_fax,
98  'myuser_mobile'=>$user->user_mobile,
99  'myuser_email'=>$user->email,
100  'myuser_logo'=>$logotouse,
101  'myuser_job'=>$user->job,
102  'myuser_web'=>'' // url not exist in $user object
103  );
104  // Retrieve extrafields
105  if (is_array($user->array_options) && count($user->array_options)) {
106  $array_user = $this->fill_substitutionarray_with_extrafields($user, $array_user, $extrafields, 'myuser', $outputlangs);
107  }
108  return $array_user;
109  }
110 
111 
119  public function getSubstitutionarrayMember($member, $outputlangs)
120  {
121  global $conf, $extrafields;
122 
123  if ($member->photo) {
124  $logotouse = $conf->adherent->dir_output.'/'.get_exdir(0, 0, 0, 1, $member, 'user').'/photos/'.$member->photo;
125  } else {
126  $logotouse = DOL_DOCUMENT_ROOT.'/public/theme/common/nophoto.png';
127  }
128 
129  $array_member = array(
130  'mymember_lastname' => $member->lastname,
131  'mymember_firstname' => $member->firstname,
132  'mymember_fullname' => $member->getFullName($outputlangs, 1),
133  'mymember_login' => $member->login,
134  'mymember_address' => $member->address,
135  'mymember_zip' => $member->zip,
136  'mymember_town' => $member->town,
137  'mymember_country_code' => $member->country_code,
138  'mymember_country' => $member->country,
139  'mymember_state_code' => $member->state_code,
140  'mymember_state' => $member->state,
141  'mymember_phone_perso' => $member->phone_perso,
142  'mymember_phone_pro' => $member->phone,
143  'mymember_phone_mobile' => $member->phone_mobile,
144  'mymember_email' => $member->email,
145  'mymember_logo' => $logotouse,
146  'mymember_gender' => $member->gender,
147  'mymember_birth_locale' => dol_print_date($member->birth, 'day', 'tzuser', $outputlangs),
148  'mymember_birth' => dol_print_date($member->birth, 'day', 'tzuser'),
149  );
150  // Retrieve extrafields
151  if (is_array($member->array_options) && count($member->array_options)) {
152  $array_member = $this->fill_substitutionarray_with_extrafields($member, $array_member, $extrafields, 'mymember', $outputlangs);
153  }
154  return $array_member;
155  }
156 
157 
158  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
166  public function get_substitutionarray_mysoc($mysoc, $outputlangs)
167  {
168  // phpcs:enable
169  global $conf;
170 
171  if (empty($mysoc->forme_juridique) && !empty($mysoc->forme_juridique_code))
172  {
173  $mysoc->forme_juridique = getFormeJuridiqueLabel($mysoc->forme_juridique_code);
174  }
175  if (empty($mysoc->country) && !empty($mysoc->country_code))
176  {
177  $mysoc->country = $outputlangs->transnoentitiesnoconv("Country".$mysoc->country_code);
178  }
179  if (empty($mysoc->state) && !empty($mysoc->state_code))
180  {
181  $mysoc->state = getState($mysoc->state_code, 0);
182  }
183 
184  $logotouse = $conf->mycompany->dir_output.'/logos/thumbs/'.$mysoc->logo_small;
185 
186  return array(
187  'mycompany_logo'=>$logotouse,
188  'mycompany_name'=>$mysoc->name,
189  'mycompany_email'=>$mysoc->email,
190  'mycompany_phone'=>$mysoc->phone,
191  'mycompany_fax'=>$mysoc->fax,
192  'mycompany_address'=>$mysoc->address,
193  'mycompany_zip'=>$mysoc->zip,
194  'mycompany_town'=>$mysoc->town,
195  'mycompany_country'=>$mysoc->country,
196  'mycompany_country_code'=>$mysoc->country_code,
197  'mycompany_state'=>$mysoc->state,
198  'mycompany_state_code'=>$mysoc->state_code,
199  'mycompany_web'=>$mysoc->url,
200  'mycompany_juridicalstatus'=>$mysoc->forme_juridique,
201  'mycompany_managers'=>$mysoc->managers,
202  'mycompany_capital'=>$mysoc->capital,
203  'mycompany_barcode'=>$mysoc->barcode,
204  'mycompany_idprof1'=>$mysoc->idprof1,
205  'mycompany_idprof2'=>$mysoc->idprof2,
206  'mycompany_idprof3'=>$mysoc->idprof3,
207  'mycompany_idprof4'=>$mysoc->idprof4,
208  'mycompany_idprof5'=>$mysoc->idprof5,
209  'mycompany_idprof6'=>$mysoc->idprof6,
210  'mycompany_vatnumber'=>$mysoc->tva_intra,
211  'mycompany_object'=>$mysoc->object,
212  'mycompany_note_private'=>$mysoc->note_private,
213  //'mycompany_note_public'=>$mysoc->note_public, // Only private not exists for "mysoc" but both for thirdparties
214  );
215  }
216 
217 
218  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
227  public function get_substitutionarray_thirdparty($object, $outputlangs, $array_key = 'company')
228  {
229  // phpcs:enable
230  global $conf, $extrafields;
231 
232  if (empty($object->country) && !empty($object->country_code))
233  {
234  $object->country = $outputlangs->transnoentitiesnoconv("Country".$object->country_code);
235  }
236  if (empty($object->state) && !empty($object->state_code))
237  {
238  $object->state = getState($object->state_code, 0);
239  }
240 
241  $array_thirdparty = array(
242  'company_name'=>$object->name,
243  'company_name_alias' => $object->name_alias,
244  'company_email'=>$object->email,
245  'company_phone'=>$object->phone,
246  'company_fax'=>$object->fax,
247  'company_address'=>$object->address,
248  'company_zip'=>$object->zip,
249  'company_town'=>$object->town,
250  'company_country'=>$object->country,
251  'company_country_code'=>$object->country_code,
252  'company_state'=>$object->state,
253  'company_state_code'=>$object->state_code,
254  'company_web'=>$object->url,
255  'company_barcode'=>$object->barcode,
256  'company_vatnumber'=>$object->tva_intra,
257  'company_customercode'=>$object->code_client,
258  'company_suppliercode'=>$object->code_fournisseur,
259  'company_customeraccountancycode'=>$object->code_compta,
260  'company_supplieraccountancycode'=>$object->code_compta_fournisseur,
261  'company_juridicalstatus'=>$object->forme_juridique,
262  'company_outstanding_limit'=>$object->outstanding_limit,
263  'company_capital'=>$object->capital,
264  'company_idprof1'=>$object->idprof1,
265  'company_idprof2'=>$object->idprof2,
266  'company_idprof3'=>$object->idprof3,
267  'company_idprof4'=>$object->idprof4,
268  'company_idprof5'=>$object->idprof5,
269  'company_idprof6'=>$object->idprof6,
270  'company_note_public'=>$object->note_public,
271  'company_note_private'=>$object->note_private,
272  'company_default_bank_iban'=>(is_object($object->bank_account) ? $object->bank_account->iban : ''),
273  'company_default_bank_bic'=>(is_object($object->bank_account) ? $object->bank_account->bic : '')
274  );
275 
276  // Retrieve extrafields
277  if (is_array($object->array_options) && count($object->array_options))
278  {
279  $object->fetch_optionals();
280 
281  $array_thirdparty = $this->fill_substitutionarray_with_extrafields($object, $array_thirdparty, $extrafields, $array_key, $outputlangs);
282  }
283  return $array_thirdparty;
284  }
285 
286  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
295  public function get_substitutionarray_contact($object, $outputlangs, $array_key = 'object')
296  {
297  // phpcs:enable
298  global $conf, $extrafields;
299 
300  if (empty($object->country) && !empty($object->country_code))
301  {
302  $object->country = $outputlangs->transnoentitiesnoconv("Country".$object->country_code);
303  }
304  if (empty($object->state) && !empty($object->state_code))
305  {
306  $object->state = getState($object->state_code, 0);
307  }
308 
309  $array_contact = array(
310  $array_key.'_fullname' => $object->getFullName($outputlangs, 1),
311  $array_key.'_lastname' => $object->lastname,
312  $array_key.'_firstname' => $object->firstname,
313  $array_key.'_address' => $object->address,
314  $array_key.'_zip' => $object->zip,
315  $array_key.'_town' => $object->town,
316  $array_key.'_state_id' => $object->state_id,
317  $array_key.'_state_code' => $object->state_code,
318  $array_key.'_state' => $object->state,
319  $array_key.'_country_id' => $object->country_id,
320  $array_key.'_country_code' => $object->country_code,
321  $array_key.'_country' => $object->country,
322  $array_key.'_poste' => $object->poste,
323  $array_key.'_socid' => $object->socid,
324  $array_key.'_statut' => $object->statut,
325  $array_key.'_code' => $object->code,
326  $array_key.'_email' => $object->email,
327  $array_key.'_jabberid' => $object->jabberid, // deprecated
328  $array_key.'_phone_pro' => $object->phone_pro,
329  $array_key.'_phone_perso' => $object->phone_perso,
330  $array_key.'_phone_mobile' => $object->phone_mobile,
331  $array_key.'_fax' => $object->fax,
332  $array_key.'_birthday' => $object->birthday,
333  $array_key.'_default_lang' => $object->default_lang,
334  $array_key.'_note_public' => $object->note_public,
335  $array_key.'_note_private' => $object->note_private,
336  $array_key.'_civility' => $object->civility,
337  );
338 
339  // Retrieve extrafields
340  if (is_array($object->array_options) && count($object->array_options))
341  {
342  $object->fetch_optionals();
343 
344  $array_contact = $this->fill_substitutionarray_with_extrafields($object, $array_contact, $extrafields, $array_key, $outputlangs);
345  }
346  return $array_contact;
347  }
348 
349 
350  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
357  public function get_substitutionarray_other($outputlangs)
358  {
359  // phpcs:enable
360  global $conf;
361 
362  $now = dol_now('gmt'); // gmt
363  $array_other = array(
364  // Date in default language
365  'current_date'=>dol_print_date($now, 'day', 'tzuser'),
366  'current_datehour'=>dol_print_date($now, 'dayhour', 'tzuser'),
367  'current_server_date'=>dol_print_date($now, 'day', 'tzserver'),
368  'current_server_datehour'=>dol_print_date($now, 'dayhour', 'tzserver'),
369  // Date in requested output language
370  'current_date_locale'=>dol_print_date($now, 'day', 'tzuser', $outputlangs),
371  'current_datehour_locale'=>dol_print_date($now, 'dayhour', 'tzuser', $outputlangs),
372  'current_server_date_locale'=>dol_print_date($now, 'day', 'tzserver', $outputlangs),
373  'current_server_datehour_locale'=>dol_print_date($now, 'dayhour', 'tzserver', $outputlangs),
374  );
375 
376 
377  foreach ($conf->global as $key => $val)
378  {
379  if (isASecretKey($key)) $newval = '*****forbidden*****';
380  else $newval = $val;
381  $array_other['__['.$key.']__'] = $newval;
382  }
383 
384  return $array_other;
385  }
386 
387 
388  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
397  public function get_substitutionarray_object($object, $outputlangs, $array_key = 'object')
398  {
399  // phpcs:enable
400  global $conf, $extrafields;
401 
402  $sumpayed = $sumdeposit = $sumcreditnote = '';
403  $already_payed_all = 0;
404  $remain_to_pay = 0;
405  if ($object->element == 'facture')
406  {
407  $invoice_source = new Facture($this->db);
408  if ($object->fk_facture_source > 0)
409  {
410  $invoice_source->fetch($object->fk_facture_source);
411  }
412  $sumpayed = $object->getSommePaiement();
413  $sumdeposit = $object->getSumDepositsUsed();
414  $sumcreditnote = $object->getSumCreditNotesUsed();
415  $already_payed_all = $sumpayed + $sumdeposit + $sumcreditnote;
416  $remain_to_pay = $sumpayed - $sumdeposit - $sumcreditnote;
417 
418  if ($object->fk_account > 0) {
419  require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
420  $bank_account = new Account($this->db);
421  $bank_account->fetch($object->fk_account);
422  }
423  }
424 
425  $date = ($object->element == 'contrat' ? $object->date_contrat : $object->date);
426 
427  $resarray = array(
428  $array_key.'_id'=>$object->id,
429  $array_key.'_ref'=>$object->ref,
430  $array_key.'_ref_ext'=>$object->ref_ext,
431  $array_key.'_ref_customer'=>(!empty($object->ref_client) ? $object->ref_client : (empty($object->ref_customer) ? '' : $object->ref_customer)),
432  $array_key.'_ref_supplier'=>(!empty($object->ref_fournisseur) ? $object->ref_fournisseur : (empty($object->ref_supplier) ? '' : $object->ref_supplier)),
433  $array_key.'_source_invoice_ref'=>$invoice_source->ref,
434  // Dates
435  $array_key.'_hour'=>dol_print_date($date, 'hour'),
436  $array_key.'_date'=>dol_print_date($date, 'day'),
437  $array_key.'_date_rfc'=>dol_print_date($date, 'dayrfc'),
438  $array_key.'_date_limit'=>(!empty($object->date_lim_reglement) ?dol_print_date($object->date_lim_reglement, 'day') : ''),
439  $array_key.'_date_end'=>(!empty($object->fin_validite) ?dol_print_date($object->fin_validite, 'day') : ''),
440  $array_key.'_date_creation'=>dol_print_date($object->date_creation, 'day'),
441  $array_key.'_date_modification'=>(!empty($object->date_modification) ?dol_print_date($object->date_modification, 'day') : ''),
442  $array_key.'_date_validation'=>(!empty($object->date_validation) ?dol_print_date($object->date_validation, 'dayhour') : ''),
443  $array_key.'_date_delivery_planed'=>(!empty($object->date_livraison) ?dol_print_date($object->date_livraison, 'day') : ''),
444  $array_key.'_date_close'=>(!empty($object->date_cloture) ?dol_print_date($object->date_cloture, 'dayhour') : ''),
445 
446  $array_key.'_payment_mode_code'=>$object->mode_reglement_code,
447  $array_key.'_payment_mode'=>($outputlangs->transnoentitiesnoconv('PaymentType'.$object->mode_reglement_code) != 'PaymentType'.$object->mode_reglement_code ? $outputlangs->transnoentitiesnoconv('PaymentType'.$object->mode_reglement_code) : $object->mode_reglement),
448  $array_key.'_payment_term_code'=>$object->cond_reglement_code,
449  $array_key.'_payment_term'=>($outputlangs->transnoentitiesnoconv('PaymentCondition'.$object->cond_reglement_code) != 'PaymentCondition'.$object->cond_reglement_code ? $outputlangs->transnoentitiesnoconv('PaymentCondition'.$object->cond_reglement_code) : ($object->cond_reglement_doc ? $object->cond_reglement_doc : $object->cond_reglement)),
450 
451  $array_key.'_incoterms'=>(method_exists($object, 'display_incoterms') ? $object->display_incoterms() : ''),
452 
453  $array_key.'_bank_iban'=>$bank_account->iban,
454  $array_key.'_bank_bic'=>$bank_account->bic,
455 
456  $array_key.'_total_ht_locale'=>price($object->total_ht, 0, $outputlangs),
457  $array_key.'_total_vat_locale'=>(!empty($object->total_vat) ?price($object->total_vat, 0, $outputlangs) : price($object->total_tva, 0, $outputlangs)),
458  $array_key.'_total_localtax1_locale'=>price($object->total_localtax1, 0, $outputlangs),
459  $array_key.'_total_localtax2_locale'=>price($object->total_localtax2, 0, $outputlangs),
460  $array_key.'_total_ttc_locale'=>price($object->total_ttc, 0, $outputlangs),
461 
462  $array_key.'_total_ht'=>price2num($object->total_ht),
463  $array_key.'_total_vat'=>(!empty($object->total_vat) ?price2num($object->total_vat) : price2num($object->total_tva)),
464  $array_key.'_total_localtax1'=>price2num($object->total_localtax1),
465  $array_key.'_total_localtax2'=>price2num($object->total_localtax2),
466  $array_key.'_total_ttc'=>price2num($object->total_ttc),
467 
468  $array_key.'_multicurrency_code' => price2num($object->multicurrency_code),
469  $array_key.'_multicurrency_tx' => price2num($object->multicurrency_tx),
470  $array_key.'_multicurrency_total_ht' => price2num($object->multicurrency_total_ht),
471  $array_key.'_multicurrency_total_tva' => price2num($object->multicurrency_total_tva),
472  $array_key.'_multicurrency_total_ttc' => price2num($object->multicurrency_total_ttc),
473  $array_key.'_multicurrency_total_ht_locale' => price($object->multicurrency_total_ht, 0, $outputlangs),
474  $array_key.'_multicurrency_total_tva_locale' => price($object->multicurrency_total_tva, 0, $outputlangs),
475  $array_key.'_multicurrency_total_ttc_locale' => price($object->multicurrency_total_ttc, 0, $outputlangs),
476 
477  $array_key.'_note_private'=>$object->note,
478  $array_key.'_note_public'=>$object->note_public,
479  $array_key.'_note'=>$object->note_public, // For backward compatibility
480 
481  // Payments
482  $array_key.'_already_payed_locale'=>price($sumpayed, 0, $outputlangs),
483  $array_key.'_already_payed'=>price2num($sumpayed),
484  $array_key.'_already_deposit_locale'=>price($sumdeposit, 0, $outputlangs),
485  $array_key.'_already_deposit'=>price2num($sumdeposit),
486  $array_key.'_already_creditnote_locale'=>price($sumcreditnote, 0, $outputlangs),
487  $array_key.'_already_creditnote'=>price2num($sumcreditnote),
488 
489  $array_key.'_already_payed_all_locale'=>price(price2num($already_payed_all, 'MT'), 0, $outputlangs),
490  $array_key.'_already_payed_all'=> price2num($already_payed_all, 'MT'),
491 
492  // Remain to pay with all know information (except open direct debit requests)
493  $array_key.'_remain_to_pay_locale'=>price(price2num($object->total_ttc - $remain_to_pay, 'MT'), 0, $outputlangs),
494  $array_key.'_remain_to_pay'=>price2num($object->total_ttc - $remain_to_pay, 'MT')
495  );
496 
497  if (method_exists($object, 'getTotalDiscount')) {
498  $resarray[$array_key.'_total_discount_ht_locale'] = price($object->getTotalDiscount(), 0, $outputlangs);
499  $resarray[$array_key.'_total_discount_ht'] = price2num($object->getTotalDiscount());
500  } else {
501  $resarray[$array_key.'_total_discount_ht_locale'] = '';
502  $resarray[$array_key.'_total_discount_ht'] = '';
503  }
504 
505  // Fetch project information if there is a project assigned to this object
506  if ($object->element != "project" && !empty($object->fk_project) && $object->fk_project > 0)
507  {
508  if (!is_object($object->project))
509  {
510  $object->fetch_projet();
511  }
512 
513  $resarray[$array_key.'_project_ref'] = $object->project->ref;
514  $resarray[$array_key.'_project_title'] = $object->project->title;
515  $resarray[$array_key.'_project_description'] = $object->project->description;
516  $resarray[$array_key.'_project_date_start'] = dol_print_date($object->project->date_start, 'day');
517  $resarray[$array_key.'_project_date_end'] = dol_print_date($object->project->date_end, 'day');
518  }
519 
520  // Add vat by rates
521  if (is_array($object->lines) && count($object->lines) > 0)
522  {
523  $totalUp = 0;
524  foreach ($object->lines as $line)
525  {
526  // $line->tva_tx format depends on database field accuraty, no reliable. This is kept for backward compatibility
527  if (empty($resarray[$array_key.'_total_vat_'.$line->tva_tx])) $resarray[$array_key.'_total_vat_'.$line->tva_tx] = 0;
528  $resarray[$array_key.'_total_vat_'.$line->tva_tx] += $line->total_tva;
529  $resarray[$array_key.'_total_vat_locale_'.$line->tva_tx] = price($resarray[$array_key.'_total_vat_'.$line->tva_tx]);
530  // $vatformated is vat without not expected chars (so 20, or 8.5 or 5.99 for example)
531  $vatformated = vatrate($line->tva_tx);
532  if (empty($resarray[$array_key.'_total_vat_'.$vatformated])) $resarray[$array_key.'_total_vat_'.$vatformated] = 0;
533  $resarray[$array_key.'_total_vat_'.$vatformated] += $line->total_tva;
534  $resarray[$array_key.'_total_vat_locale_'.$vatformated] = price($resarray[$array_key.'_total_vat_'.$vatformated]);
535 
536  $totalUp += $line->subprice * $line->qty;
537  }
538 
539  // @GS: Calculate total up and total discount percentage
540  // Note that this added fields correspond to nothing in Dolibarr (Dolibarr manage discount on lines not globally)
541  $resarray['object_total_up'] = $totalUp;
542  $resarray['object_total_up_locale'] = price($resarray['object_total_up'], 0, $outputlangs);
543  if (method_exists($object, 'getTotalDiscount')) {
544  $totalDiscount = $object->getTotalDiscount();
545  } else {
546  $totalDiscount = 0;
547  }
548  if (!empty($totalUp) && !empty($totalDiscount)) {
549  $resarray['object_total_discount'] = round(100 / $totalUp * $totalDiscount, 2);
550  $resarray['object_total_discount_locale'] = price($resarray['object_total_discount'], 0, $outputlangs);
551  } else {
552  $resarray['object_total_discount'] = '';
553  $resarray['object_total_discount_locale'] = '';
554  }
555  }
556 
557  // Retrieve extrafields
558  if (is_array($object->array_options) && count($object->array_options))
559  {
560  $object->fetch_optionals();
561 
562  $resarray = $this->fill_substitutionarray_with_extrafields($object, $resarray, $extrafields, $array_key, $outputlangs);
563  }
564 
565  return $resarray;
566  }
567 
568  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
577  public function get_substitutionarray_lines($line, $outputlangs, $linenumber = 0)
578  {
579  // phpcs:enable
580  global $conf;
581 
582  $resarray = array(
583  'line_pos' => $linenumber,
584  'line_fulldesc'=>doc_getlinedesc($line, $outputlangs),
585 
586  'line_product_ref'=>(empty($line->product_ref) ? '' : $line->product_ref),
587  'line_product_ref_fourn'=>(empty($line->ref_fourn) ? '' : $line->ref_fourn), // for supplier doc lines
588  'line_product_label'=>(empty($line->product_label) ? '' : $line->product_label),
589  'line_product_type'=>(empty($line->product_type) ? '' : $line->product_type),
590  'line_product_barcode'=>(empty($line->product_barcode) ? '' : $line->product_barcode),
591 
592  'line_desc'=>$line->desc,
593  'line_vatrate'=>vatrate($line->tva_tx, true, $line->info_bits),
594  'line_localtax1_rate'=>vatrate($line->localtax1_tx),
595  'line_localtax2_rate'=>vatrate($line->localtax1_tx),
596  'line_up'=>price2num($line->subprice),
597  'line_up_locale'=>price($line->subprice, 0, $outputlangs),
598  'line_total_up'=>price2num($line->subprice * $line->qty),
599  'line_total_up_locale'=>price($line->subprice * $line->qty, 0, $outputlangs),
600  'line_qty'=>$line->qty,
601  'line_discount_percent'=>($line->remise_percent ? $line->remise_percent.'%' : ''),
602  'line_price_ht'=>price2num($line->total_ht),
603  'line_price_ttc'=>price2num($line->total_ttc),
604  'line_price_vat'=>price2num($line->total_tva),
605  'line_price_ht_locale'=>price($line->total_ht, 0, $outputlangs),
606  'line_price_ttc_locale'=>price($line->total_ttc, 0, $outputlangs),
607  'line_price_vat_locale'=>price($line->total_tva, 0, $outputlangs),
608  // Dates
609  'line_date_start'=>dol_print_date($line->date_start, 'day'),
610  'line_date_start_locale'=>dol_print_date($line->date_start, 'day', 'tzserver', $outputlangs),
611  'line_date_start_rfc'=>dol_print_date($line->date_start, 'dayrfc'),
612  'line_date_end'=>dol_print_date($line->date_end, 'day'),
613  'line_date_end_locale'=>dol_print_date($line->date_end, 'day', 'tzserver', $outputlangs),
614  'line_date_end_rfc'=>dol_print_date($line->date_end, 'dayrfc'),
615 
616  'line_multicurrency_code' => price2num($line->multicurrency_code),
617  'line_multicurrency_subprice' => price2num($line->multicurrency_subprice),
618  'line_multicurrency_total_ht' => price2num($line->multicurrency_total_ht),
619  'line_multicurrency_total_tva' => price2num($line->multicurrency_total_tva),
620  'line_multicurrency_total_ttc' => price2num($line->multicurrency_total_ttc),
621  'line_multicurrency_subprice_locale' => price($line->multicurrency_subprice, 0, $outputlangs),
622  'line_multicurrency_total_ht_locale' => price($line->multicurrency_total_ht, 0, $outputlangs),
623  'line_multicurrency_total_tva_locale' => price($line->multicurrency_total_tva, 0, $outputlangs),
624  'line_multicurrency_total_ttc_locale' => price($line->multicurrency_total_ttc, 0, $outputlangs),
625  );
626 
627  // Units
628  if (!empty($conf->global->PRODUCT_USE_UNITS))
629  {
630  $resarray['line_unit'] = $outputlangs->trans($line->getLabelOfUnit('long'));
631  $resarray['line_unit_short'] = $outputlangs->trans($line->getLabelOfUnit('short'));
632  }
633 
634  // Retrieve extrafields
635  $extrafieldkey = $line->table_element;
636  $array_key = "line";
637  require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
638  $extrafields = new ExtraFields($this->db);
639  $extrafields->fetch_name_optionals_label($extrafieldkey, true);
640  $line->fetch_optionals();
641 
642  $resarray = $this->fill_substitutionarray_with_extrafields($line, $resarray, $extrafields, $array_key, $outputlangs);
643 
644  // Check if the current line belongs to a supplier order
645  if (get_class($line) == 'CommandeFournisseurLigne')
646  {
647  // Add the product supplier extrafields to the substitutions
648  $extrafields->fetch_name_optionals_label("product_fournisseur_price");
649  $extralabels = $extrafields->attributes["product_fournisseur_price"]['label'];
650 
651  if (!empty($extralabels) && is_array($extralabels))
652  {
653  $columns = "";
654 
655  foreach ($extralabels as $key => $label)
656  {
657  $columns .= "$key, ";
658  }
659 
660  if ($columns != "")
661  {
662  $columns = substr($columns, 0, strlen($columns) - 2);
663  $resql = $this->db->query("SELECT ".$columns." FROM ".MAIN_DB_PREFIX."product_fournisseur_price_extrafields AS ex INNER JOIN ".MAIN_DB_PREFIX."product_fournisseur_price AS f ON ex.fk_object = f.rowid WHERE f.ref_fourn = '".$this->db->escape($line->ref_supplier)."'");
664 
665  if ($this->db->num_rows($resql) > 0)
666  {
667  $resql = $this->db->fetch_object($resql);
668 
669  foreach ($extralabels as $key => $label)
670  {
671  $resarray['line_product_supplier_'.$key] = $resql->{$key};
672  }
673  }
674  }
675  }
676  }
677 
678  // Load product data optional fields to the line -> enables to use "line_options_{extrafield}"
679  if (isset($line->fk_product) && $line->fk_product > 0)
680  {
681  $tmpproduct = new Product($this->db);
682  $result = $tmpproduct->fetch($line->fk_product);
683  foreach ($tmpproduct->array_options as $key=>$label)
684  $resarray["line_product_".$key] = $label;
685  }
686 
687  return $resarray;
688  }
689 
690  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
699  public function get_substitutionarray_shipment($object, $outputlangs, $array_key = 'object')
700  {
701  // phpcs:enable
702  global $conf, $extrafields;
703  dol_include_once('/core/lib/product.lib.php');
704  $object->list_delivery_methods($object->shipping_method_id);
705  $calculatedVolume = ($object->trueWidth * $object->trueHeight * $object->trueDepth);
706 
707  $array_shipment = array(
708  $array_key.'_id'=>$object->id,
709  $array_key.'_ref'=>$object->ref,
710  $array_key.'_ref_ext'=>$object->ref_ext,
711  $array_key.'_ref_customer'=>$object->ref_customer,
712  $array_key.'_date_delivery'=>dol_print_date($object->date_delivery, 'day'),
713  $array_key.'_hour_delivery'=>dol_print_date($object->date_delivery, 'hour'),
714  $array_key.'_date_creation'=>dol_print_date($object->date_creation, 'day'),
715  $array_key.'_total_ht'=>price($object->total_ht),
716  $array_key.'_total_vat'=>price($object->total_tva),
717  $array_key.'_total_ttc'=>price($object->total_ttc),
718  $array_key.'_total_discount_ht' => price($object->getTotalDiscount()),
719  $array_key.'_note_private'=>$object->note_private,
720  $array_key.'_note'=>$object->note_public,
721  $array_key.'_tracking_number'=>$object->tracking_number,
722  $array_key.'_tracking_url'=>$object->tracking_url,
723  $array_key.'_shipping_method'=>$object->listmeths[0]['libelle'],
724  $array_key.'_weight'=>$object->trueWeight.' '.measuringUnitString(0, 'weight', $object->weight_units),
725  $array_key.'_width'=>$object->trueWidth.' '.measuringUnitString(0, 'size', $object->width_units),
726  $array_key.'_height'=>$object->trueHeight.' '.measuringUnitString(0, 'size', $object->height_units),
727  $array_key.'_depth'=>$object->trueDepth.' '.measuringUnitString(0, 'size', $object->depth_units),
728  $array_key.'_size'=>$calculatedVolume.' '.measuringUnitString(0, 'volume'),
729  );
730 
731  // Add vat by rates
732  foreach ($object->lines as $line)
733  {
734  if (empty($array_shipment[$array_key.'_total_vat_'.$line->tva_tx])) $array_shipment[$array_key.'_total_vat_'.$line->tva_tx] = 0;
735  $array_shipment[$array_key.'_total_vat_'.$line->tva_tx] += $line->total_tva;
736  }
737 
738  // Retrieve extrafields
739  if (is_array($object->array_options) && count($object->array_options))
740  {
741  $object->fetch_optionals();
742 
743  $array_shipment = $this->fill_substitutionarray_with_extrafields($object, $array_shipment, $extrafields, $array_key, $outputlangs);
744  }
745 
746  // Add infor from $object->xxx where xxx has been loaded by fetch_origin() of shipment
747  if (!empty($object->commande) && is_object($object->commande)) {
748  $array_shipment['order_ref'] = $object->commande->ref;
749  $array_shipment['order_ref_customer'] = $object->commande->ref_customer;
750  }
751 
752  return $array_shipment;
753  }
754 
755 
756  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
764  public function get_substitutionarray_shipment_lines($line, $outputlangs)
765  {
766  // phpcs:enable
767  global $conf;
768  dol_include_once('/core/lib/product.lib.php');
769 
770  $resarray = array(
771  'line_fulldesc'=>doc_getlinedesc($line, $outputlangs),
772  'line_product_ref'=>$line->product_ref,
773  'line_product_label'=>$line->product_label,
774  'line_desc'=>$line->desc,
775  'line_vatrate'=>vatrate($line->tva_tx, true, $line->info_bits),
776  'line_up'=>price($line->subprice),
777  'line_total_up'=>price($line->subprice * $line->qty),
778  'line_qty'=>$line->qty,
779  'line_qty_shipped'=>$line->qty_shipped,
780  'line_qty_asked'=>$line->qty_asked,
781  'line_discount_percent'=>($line->remise_percent ? $line->remise_percent.'%' : ''),
782  'line_price_ht'=>price($line->total_ht),
783  'line_price_ttc'=>price($line->total_ttc),
784  'line_price_vat'=>price($line->total_tva),
785  'line_weight'=>empty($line->weight) ? '' : $line->weight * $line->qty_shipped.' '.measuringUnitString(0, 'weight', $line->weight_units),
786  'line_length'=>empty($line->length) ? '' : $line->length * $line->qty_shipped.' '.measuringUnitString(0, 'size', $line->length_units),
787  'line_surface'=>empty($line->surface) ? '' : $line->surface * $line->qty_shipped.' '.measuringUnitString(0, 'surface', $line->surface_units),
788  'line_volume'=>empty($line->volume) ? '' : $line->volume * $line->qty_shipped.' '.measuringUnitString(0, 'volume', $line->volume_units),
789  );
790 
791  // Retrieve extrafields
792  $extrafieldkey = $line->element;
793  $array_key = "line";
794  require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
795  $extrafields = new ExtraFields($this->db);
796  $extrafields->fetch_name_optionals_label($extrafieldkey, true);
797  $line->fetch_optionals();
798 
799  $resarray = $this->fill_substitutionarray_with_extrafields($line, $resarray, $extrafields, $array_key, $outputlangs);
800 
801  // Load product data optional fields to the line -> enables to use "line_product_options_{extrafield}"
802  if (isset($line->fk_product) && $line->fk_product > 0)
803  {
804  $tmpproduct = new Product($this->db);
805  $result = $tmpproduct->fetch($line->fk_product);
806  foreach ($tmpproduct->array_options as $key=>$label)
807  $resarray["line_product_".$key] = $label;
808  }
809 
810  return $resarray;
811  }
812 
813 
814  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
823  public function get_substitutionarray_each_var_object(&$object, $outputlangs, $recursive = true)
824  {
825  // phpcs:enable
826  $array_other = array();
827  if (!empty($object)) {
828  foreach ($object as $key => $value) {
829  if (!empty($value)) {
830  if (!is_array($value) && !is_object($value)) {
831  $array_other['object_'.$key] = $value;
832  }
833  if (is_array($value) && $recursive) {
834  $array_other['object_'.$key] = $this->get_substitutionarray_each_var_object($value, $outputlangs, false);
835  }
836  }
837  }
838  }
839  return $array_other;
840  }
841 
842 
843  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
854  public function fill_substitutionarray_with_extrafields($object, $array_to_fill, $extrafields, $array_key, $outputlangs)
855  {
856  // phpcs:enable
857  global $conf;
858 
859  if (is_array($extrafields->attributes[$object->table_element]['label'])) {
860  foreach ($extrafields->attributes[$object->table_element]['label'] as $key=>$label)
861  {
862  if ($extrafields->attributes[$object->table_element]['type'][$key] == 'price')
863  {
864  $object->array_options['options_'.$key] = price2num($object->array_options['options_'.$key]);
865  $object->array_options['options_'.$key.'_currency'] = price($object->array_options['options_'.$key], 0, $outputlangs, 0, 0, -1, $conf->currency);
866  //Add value to store price with currency
867  $array_to_fill = array_merge($array_to_fill, array($array_key.'_options_'.$key.'_currency' => $object->array_options['options_'.$key.'_currency']));
868  } elseif ($extrafields->attributes[$object->table_element]['type'][$key] == 'select')
869  {
870  $object->array_options['options_'.$key] = $extrafields->attributes[$object->table_element]['param'][$key]['options'][$object->array_options['options_'.$key]];
871  } elseif ($extrafields->attributes[$object->table_element]['type'][$key] == 'checkbox') {
872  $valArray = explode(',', $object->array_options['options_'.$key]);
873  $output = array();
874  foreach ($extrafields->attributes[$object->table_element]['param'][$key]['options'] as $keyopt=>$valopt) {
875  if (in_array($keyopt, $valArray)) {
876  $output[] = $valopt;
877  }
878  }
879  $object->array_options['options_'.$key] = implode(', ', $output);
880  } elseif ($extrafields->attributes[$object->table_element]['type'][$key] == 'date')
881  {
882  if (strlen($object->array_options['options_'.$key]) > 0)
883  {
884  $date = $object->array_options['options_'.$key];
885  $object->array_options['options_'.$key] = dol_print_date($date, 'day'); // using company output language
886  $object->array_options['options_'.$key.'_locale'] = dol_print_date($date, 'day', 'tzserver', $outputlangs); // using output language format
887  $object->array_options['options_'.$key.'_rfc'] = dol_print_date($date, 'dayrfc'); // international format
888  } else {
889  $object->array_options['options_'.$key] = '';
890  $object->array_options['options_'.$key.'_locale'] = '';
891  $object->array_options['options_'.$key.'_rfc'] = '';
892  }
893  $array_to_fill = array_merge($array_to_fill, array($array_key.'_options_'.$key.'_locale' => $object->array_options['options_'.$key.'_locale']));
894  $array_to_fill = array_merge($array_to_fill, array($array_key.'_options_'.$key.'_rfc' => $object->array_options['options_'.$key.'_rfc']));
895  } elseif ($extrafields->attributes[$object->table_element]['label'][$key] == 'datetime')
896  {
897  $datetime = $object->array_options['options_'.$key];
898  $object->array_options['options_'.$key] = ($datetime != "0000-00-00 00:00:00" ?dol_print_date($object->array_options['options_'.$key], 'dayhour') : ''); // using company output language
899  $object->array_options['options_'.$key.'_locale'] = ($datetime != "0000-00-00 00:00:00" ?dol_print_date($object->array_options['options_'.$key], 'dayhour', 'tzserver', $outputlangs) : ''); // using output language format
900  $object->array_options['options_'.$key.'_rfc'] = ($datetime != "0000-00-00 00:00:00" ?dol_print_date($object->array_options['options_'.$key], 'dayhourrfc') : ''); // international format
901  $array_to_fill = array_merge($array_to_fill, array($array_key.'_options_'.$key.'_locale' => $object->array_options['options_'.$key.'_locale']));
902  $array_to_fill = array_merge($array_to_fill, array($array_key.'_options_'.$key.'_rfc' => $object->array_options['options_'.$key.'_rfc']));
903  } elseif ($extrafields->attributes[$object->table_element]['type'][$key] == 'link')
904  {
905  $id = $object->array_options['options_'.$key];
906  if ($id != "")
907  {
908  $param = $extrafields->attributes[$object->table_element]['param'][$key];
909  $param_list = array_keys($param['options']); // $param_list='ObjectName:classPath'
910  $InfoFieldList = explode(":", $param_list[0]);
911  $classname = $InfoFieldList[0];
912  $classpath = $InfoFieldList[1];
913  if (!empty($classpath))
914  {
915  dol_include_once($InfoFieldList[1]);
916  if ($classname && class_exists($classname))
917  {
918  $tmpobject = new $classname($this->db);
919  $tmpobject->fetch($id);
920  // completely replace the id with the linked object name
921  $object->array_options['options_'.$key] = $tmpobject->name;
922  }
923  }
924  }
925  }
926 
927  $array_to_fill = array_merge($array_to_fill, array($array_key.'_options_'.$key => $object->array_options['options_'.$key]));
928  }
929  }
930 
931  return $array_to_fill;
932  }
933 
934 
947  public function printRect($pdf, $x, $y, $l, $h, $hidetop = 0, $hidebottom = 0)
948  {
949  if (empty($hidetop) || $hidetop == -1) $pdf->line($x, $y, $x + $l, $y);
950  $pdf->line($x + $l, $y, $x + $l, $y + $h);
951  if (empty($hidebottom)) $pdf->line($x + $l, $y + $h, $x, $y + $h);
952  $pdf->line($x, $y + $h, $x, $y);
953  }
954 
955 
963  public function columnSort($a, $b)
964  {
965  if (empty($a['rank'])) { $a['rank'] = 0; }
966  if (empty($b['rank'])) { $b['rank'] = 0; }
967  if ($a['rank'] == $b['rank']) {
968  return 0;
969  }
970  return ($a['rank'] > $b['rank']) ? -1 : 1;
971  }
972 
983  public function prepareArrayColumnField($object, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
984  {
985  global $conf;
986 
987  $this->defineColumnField($object, $outputlangs, $hidedetails, $hidedesc, $hideref);
988 
989 
990  // Sorting
991  uasort($this->cols, array($this, 'columnSort'));
992 
993  // Positionning
994  $curX = $this->page_largeur - $this->marge_droite; // start from right
995 
996  // Array width
997  $arrayWidth = $this->page_largeur - $this->marge_droite - $this->marge_gauche;
998 
999  // Count flexible column
1000  $totalDefinedColWidth = 0;
1001  $countFlexCol = 0;
1002  foreach ($this->cols as $colKey =>& $colDef)
1003  {
1004  if (!$this->getColumnStatus($colKey)) continue; // continue if disabled
1005 
1006  if (!empty($colDef['scale'])) {
1007  // In case of column width is defined by percentage
1008  $colDef['width'] = abs($arrayWidth * $colDef['scale'] / 100);
1009  }
1010 
1011  if (empty($colDef['width'])) {
1012  $countFlexCol++;
1013  } else {
1014  $totalDefinedColWidth += $colDef['width'];
1015  }
1016  }
1017 
1018  foreach ($this->cols as $colKey =>& $colDef)
1019  {
1020  // setting empty conf with default
1021  if (!empty($colDef['title'])) {
1022  $colDef['title'] = array_replace($this->defaultTitlesFieldsStyle, $colDef['title']);
1023  } else {
1024  $colDef['title'] = $this->defaultTitlesFieldsStyle;
1025  }
1026 
1027  // setting empty conf with default
1028  if (!empty($colDef['content'])) {
1029  $colDef['content'] = array_replace($this->defaultContentsFieldsStyle, $colDef['content']);
1030  } else {
1031  $colDef['content'] = $this->defaultContentsFieldsStyle;
1032  }
1033 
1034  if ($this->getColumnStatus($colKey))
1035  {
1036  // In case of flexible column
1037  if (empty($colDef['width'])) {
1038  $colDef['width'] = abs(($arrayWidth - $totalDefinedColWidth)) / $countFlexCol;
1039  }
1040 
1041  // Set positions
1042  $lastX = $curX;
1043  $curX = $lastX - $colDef['width'];
1044  $colDef['xStartPos'] = $curX;
1045  $colDef['xEndPos'] = $lastX;
1046  }
1047  }
1048  }
1049 
1056  public function getColumnContentWidth($colKey)
1057  {
1058  $colDef = $this->cols[$colKey];
1059  return $colDef['width'] - $colDef['content']['padding'][3] - $colDef['content']['padding'][1];
1060  }
1061 
1062 
1069  public function getColumnContentXStart($colKey)
1070  {
1071  $colDef = $this->cols[$colKey];
1072  return $colDef['xStartPos'] + $colDef['content']['padding'][3];
1073  }
1074 
1081  public function getColumnRank($colKey)
1082  {
1083  if (!isset($this->cols[$colKey]['rank'])) return -1;
1084  return $this->cols[$colKey]['rank'];
1085  }
1086 
1096  public function insertNewColumnDef($newColKey, $defArray, $targetCol = false, $insertAfterTarget = false)
1097  {
1098  // prepare wanted rank
1099  $rank = -1;
1100 
1101  // try to get rank from target column
1102  if (!empty($targetCol)) {
1103  $rank = $this->getColumnRank($targetCol);
1104  if ($rank >= 0 && $insertAfterTarget) { $rank++; }
1105  }
1106 
1107  // get rank from new column definition
1108  if ($rank < 0 && !empty($defArray['rank'])) {
1109  $rank = $defArray['rank'];
1110  }
1111 
1112  // error: no rank
1113  if ($rank < 0) { return -1; }
1114 
1115  foreach ($this->cols as $colKey =>& $colDef)
1116  {
1117  if ($rank <= $colDef['rank'])
1118  {
1119  $colDef['rank'] = $colDef['rank'] + 1;
1120  }
1121  }
1122 
1123  $defArray['rank'] = $rank;
1124  $this->cols[$newColKey] = $defArray; // array_replace is used to preserve keys
1125 
1126  return $rank;
1127  }
1128 
1129 
1139  public function printStdColumnContent($pdf, &$curY, $colKey, $columnText = '')
1140  {
1141  global $hookmanager;
1142 
1143  $parameters = array(
1144  'curY' => &$curY,
1145  'columnText' => $columnText,
1146  'colKey' => $colKey,
1147  'pdf' => &$pdf,
1148  );
1149  $reshook = $hookmanager->executeHooks('printStdColumnContent', $parameters, $this); // Note that $action and $object may have been modified by hook
1150  if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
1151  if (!$reshook)
1152  {
1153  if (empty($columnText)) return;
1154  $pdf->SetXY($this->getColumnContentXStart($colKey), $curY); // Set curent position
1155  $colDef = $this->cols[$colKey];
1156  // save curent cell padding
1157  $curentCellPaddinds = $pdf->getCellPaddings();
1158  // set cell padding with column content definition
1159  $pdf->setCellPaddings($colDef['content']['padding'][3], $colDef['content']['padding'][0], $colDef['content']['padding'][1], $colDef['content']['padding'][2]);
1160  $pdf->writeHTMLCell($colDef['width'], 2, $colDef['xStartPos'], $curY, $columnText, 0, 1, 0, true, $colDef['content']['align']);
1161 
1162  // restore cell padding
1163  $pdf->setCellPaddings($curentCellPaddinds['L'], $curentCellPaddinds['T'], $curentCellPaddinds['R'], $curentCellPaddinds['B']);
1164  }
1165  }
1166 
1167 
1182  public function printColDescContent($pdf, &$curY, $colKey, $object, $i, $outputlangs, $hideref = 0, $hidedesc = 0, $issupplierline = 0)
1183  {
1184  // load desc col params
1185  $colDef = $this->cols[$colKey];
1186  // save curent cell padding
1187  $curentCellPaddinds = $pdf->getCellPaddings();
1188  // set cell padding with column content definition
1189  $pdf->setCellPaddings($colDef['content']['padding'][3], $colDef['content']['padding'][0], $colDef['content']['padding'][1], $colDef['content']['padding'][2]);
1190 
1191  // line description
1192  pdf_writelinedesc($pdf, $object, $i, $outputlangs, $colDef['width'], 3, $colDef['xStartPos'], $curY, $hideref, $hidedesc, $issupplierline);
1193  $posYAfterDescription = $pdf->GetY() - $colDef['content']['padding'][0];
1194 
1195  // restore cell padding
1196  $pdf->setCellPaddings($curentCellPaddinds['L'], $curentCellPaddinds['T'], $curentCellPaddinds['R'], $curentCellPaddinds['B']);
1197 
1198  // Display extrafield if needed
1199  $params = array(
1200  'display' => 'list',
1201  'printableEnable' => array(3),
1202  'printableEnableNotEmpty' => array(4)
1203  );
1204  $extrafieldDesc = $this->getExtrafieldsInHtml($object->lines[$i], $outputlangs, $params);
1205  if (!empty($extrafieldDesc)) {
1206  $this->printStdColumnContent($pdf, $posYAfterDescription, $colKey, $extrafieldDesc);
1207  }
1208  }
1209 
1218  public function getExtrafieldContent($object, $extrafieldKey)
1219  {
1220  global $hookmanager;
1221 
1222  if (empty($object->table_element)) { return; }
1223 
1224  $extrafieldsKeyPrefix = "options_";
1225 
1226  // Cleanup extrafield key to remove prefix if present
1227  $pos = strpos($extrafieldKey, $extrafieldsKeyPrefix);
1228  if ($pos === 0) {
1229  $extrafieldKey = substr($extrafieldKey, strlen($extrafieldsKeyPrefix));
1230  }
1231 
1232  $extrafieldOptionsKey = $extrafieldsKeyPrefix.$extrafieldKey;
1233 
1234 
1235  // Load extrafiels if not allready does
1236  if (empty($this->extrafieldsCache)) { $this->extrafieldsCache = new ExtraFields($this->db); }
1237  if (empty($this->extrafieldsCache->attributes[$object->table_element])) { $this->extrafieldsCache->fetch_name_optionals_label($object->table_element); }
1238  $extrafields = $this->extrafieldsCache;
1239 
1240  $extrafieldOutputContent = $extrafields->showOutputField($extrafieldKey, $object->array_options[$extrafieldOptionsKey], '', $object->table_element);
1241 
1242  // TODO : allow showOutputField to be pdf public friendly, ex: in a link to object, clean getNomUrl to remove link and images... like a getName methode ...
1243  if ($extrafields->attributes[$object->table_element]['type'][$extrafieldKey] == 'link') {
1244  // for lack of anything better we cleanup all html tags
1245  $extrafieldOutputContent = dol_string_nohtmltag($extrafieldOutputContent);
1246  }
1247 
1248  $parameters = array(
1249  'object' => $object,
1250  'extrafields' => $extrafields,
1251  'extrafieldKey' => $extrafieldKey,
1252  'extrafieldOutputContent' =>& $extrafieldOutputContent
1253  );
1254  $reshook = $hookmanager->executeHooks('getPDFExtrafieldContent', $parameters, $this); // Note that $action and $object may have been modified by hook
1255  if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
1256  if ($reshook)
1257  {
1258  $extrafieldOutputContent = $hookmanager->resPrint;
1259  }
1260 
1261  return $extrafieldOutputContent;
1262  }
1263 
1264 
1273  public function getExtrafieldsInHtml($object, $outputlangs, $params = array())
1274  {
1275  global $hookmanager;
1276 
1277  if (empty($object->table_element)) {
1278  return;
1279  }
1280 
1281  // Load extrafiels if not allready does
1282  if (empty($this->extrafieldsCache)) { $this->extrafieldsCache = new ExtraFields($this->db); }
1283  if (empty($this->extrafieldsCache->attributes[$object->table_element])) { $this->extrafieldsCache->fetch_name_optionals_label($object->table_element); }
1284  $extrafields = $this->extrafieldsCache;
1285 
1286  $defaultParams = array(
1287  'style' => '',
1288  'display' => 'auto', // auto, table, list
1289  'printableEnable' => array(1),
1290  'printableEnableNotEmpty' => array(2),
1291 
1292  'table' => array(
1293  'maxItemsInRow' => 2,
1294  'cellspacing' => 0,
1295  'cellpadding' => 0,
1296  'border' => 0,
1297  'labelcolwidth' => '25%',
1298  'arrayOfLineBreakType' => array('text', 'html')
1299  ),
1300 
1301  'list' => array(
1302  'separator' => '<br/>'
1303  ),
1304 
1305  'auto' => array(
1306  'list' => 0, // 0 for default
1307  'table' => 4 // if there more than x extrafield to display
1308  ),
1309  );
1310 
1311  $params = $params + $defaultParams;
1312 
1313 
1318  $html = '';
1319  $fields = array();
1320 
1321  if (!empty($extrafields->attributes[$object->table_element]['label']) && is_array($extrafields->attributes[$object->table_element]['label'])) {
1322  foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $label)
1323  {
1324  // Enable extrafield ?
1325  $enabled = 0;
1326  $disableOnEmpty = 0;
1327  if (!empty($extrafields->attributes[$object->table_element]['printable'][$key])) {
1328  $printable = intval($extrafields->attributes[$object->table_element]['printable'][$key]);
1329  if (in_array($printable, $params['printableEnable']) || in_array($printable, $params['printableEnableNotEmpty'])) {
1330  $enabled = 1;
1331  }
1332 
1333  if (in_array($printable, $params['printableEnableNotEmpty'])) {
1334  $disableOnEmpty = 1;
1335  }
1336  }
1337 
1338  if (empty($enabled)) {
1339  continue;
1340  }
1341 
1342  $field = new stdClass();
1343  $field->rank = intval($extrafields->attributes[$object->table_element]['pos'][$key]);
1344  $field->content = $this->getExtrafieldContent($object, $key);
1345  $field->label = $outputlangs->transnoentities($label);
1346  $field->type = $extrafields->attributes[$object->table_element]['type'][$key];
1347 
1348  // dont display if empty
1349  if ($disableOnEmpty && empty($field->content)) {
1350  continue;
1351  }
1352 
1353  $fields[] = $field;
1354  }
1355  }
1356 
1357  if (!empty($fields))
1358  {
1359  // Sort extrafields by rank
1360  uasort($fields, function ($a, $b) {
1361  return ($a->rank > $b->rank) ? 1 : -1;
1362  });
1363 
1364  // define some HTML content with style
1365  $html .= !empty($params['style']) ? '<style>'.$params['style'].'</style>' : '';
1366 
1367  // auto select display format
1368  if ($params['display'] == 'auto') {
1369  $lastNnumbItems = 0;
1370  foreach ($params['auto'] as $display => $numbItems) {
1371  if ($lastNnumbItems <= $numbItems && count($fields) > $numbItems) {
1372  $lastNnumbItems = $numbItems;
1373  $params['display'] = $display;
1374  }
1375  }
1376  }
1377 
1378  if ($params['display'] == 'list') {
1379  // Display in list format
1380  $i = 0;
1381  foreach ($fields as $field) {
1382  $html .= !empty($i) ? $params['list']['separator'] : '';
1383  $html .= '<strong>'.$field->label.' : </strong>';
1384  $html .= $field->content;
1385  $i++;
1386  }
1387  } elseif ($params['display'] == 'table') {
1388  // Display in table format
1389  $html .= '<table class="extrafield-table" cellspacing="'.$params['table']['cellspacing'].'" cellpadding="'.$params['table']['cellpadding'].'" border="'.$params['table']['border'].'">';
1390 
1391  $html .= "<tr>";
1392  $itemsInRow = 0;
1393  $maxItemsInRow = $params['table']['maxItemsInRow'];
1394  foreach ($fields as $field) {
1395  //$html.= !empty($html)?'<br/>':'';
1396  if ($itemsInRow >= $maxItemsInRow) {
1397  // start a new line
1398  $html .= "</tr><tr>";
1399  $itemsInRow = 0;
1400  }
1401 
1402  // for some type we need line break
1403  if (in_array($field->type, $params['table']['arrayOfLineBreakType'])) {
1404  if ($itemsInRow > 0) {
1405  // close table row and empty cols
1406  for ($i = $itemsInRow; $i <= $maxItemsInRow; $i++) {
1407  $html .= "<td></td><td></td>";
1408  }
1409  $html .= "</tr>";
1410 
1411  // start a new line
1412  $html .= "<tr>";
1413  }
1414 
1415  $itemsInRow = $maxItemsInRow;
1416  $html .= '<td colspan="'.($maxItemsInRow * 2 - 1).'">';
1417  $html .= '<strong>'.$field->label.' :</strong> ';
1418  $html .= $field->content;
1419  $html .= "</td>";
1420  } else {
1421  $itemsInRow++;
1422  $html .= '<td width="'.$params['table']['labelcolwidth'].'" class="extrafield-label">';
1423  $html .= '<strong>'.$field->label.' :</strong>';
1424  $html .= "</td>";
1425 
1426 
1427  $html .= '<td class="extrafield-content">';
1428  $html .= $field->content;
1429  $html .= "</td>";
1430  }
1431  }
1432  $html .= "</tr>";
1433 
1434  $html .= '</table>';
1435  }
1436  }
1437 
1438  return $html;
1439  }
1440 
1441 
1448  public function getColumnStatus($colKey)
1449  {
1450  if (!empty($this->cols[$colKey]['status'])) {
1451  return true;
1452  } else return false;
1453  }
1454 
1465  public function pdfTabTitles(&$pdf, $tab_top, $tab_height, $outputlangs, $hidetop = 0)
1466  {
1467  global $hookmanager, $conf;
1468 
1469  foreach ($this->cols as $colKey => $colDef) {
1470  $parameters = array(
1471  'colKey' => $colKey,
1472  'pdf' => $pdf,
1473  'outputlangs' => $outputlangs,
1474  'tab_top' => $tab_top,
1475  'tab_height' => $tab_height,
1476  'hidetop' => $hidetop
1477  );
1478 
1479  $reshook = $hookmanager->executeHooks('pdfTabTitles', $parameters, $this); // Note that $object may have been modified by hook
1480  if ($reshook < 0) {
1481  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
1482  } elseif (empty($reshook)) {
1483  if (!$this->getColumnStatus($colKey)) continue;
1484 
1485  // get title label
1486  $colDef['title']['label'] = !empty($colDef['title']['label']) ? $colDef['title']['label'] : $outputlangs->transnoentities($colDef['title']['textkey']);
1487 
1488  // Add column separator
1489  if (!empty($colDef['border-left'])) {
1490  $pdf->line($colDef['xStartPos'], $tab_top, $colDef['xStartPos'], $tab_top + $tab_height);
1491  }
1492 
1493  if (empty($hidetop)) {
1494  // save curent cell padding
1495  $curentCellPaddinds = $pdf->getCellPaddings();
1496 
1497  // Add space for lines (more if we need to show a second alternative language)
1498  global $outputlangsbis;
1499  if (is_object($outputlangsbis)) {
1500  // set cell padding with column title definition
1501  $pdf->setCellPaddings($colDef['title']['padding'][3], $colDef['title']['padding'][0], $colDef['title']['padding'][1], 0.5);
1502  } else {
1503  // set cell padding with column title definition
1504  $pdf->setCellPaddings($colDef['title']['padding'][3], $colDef['title']['padding'][0], $colDef['title']['padding'][1], $colDef['title']['padding'][2]);
1505  }
1506 
1507  $pdf->SetXY($colDef['xStartPos'], $tab_top);
1508  $textWidth = $colDef['width'];
1509  $pdf->MultiCell($textWidth, 2, $colDef['title']['label'], '', $colDef['title']['align']);
1510 
1511  // Add variant of translation if $outputlangsbis is an object
1512  if (is_object($outputlangsbis) && trim($colDef['title']['label'])) {
1513  $pdf->setCellPaddings($colDef['title']['padding'][3], 0, $colDef['title']['padding'][1], $colDef['title']['padding'][2]);
1514  $pdf->SetXY($colDef['xStartPos'], $pdf->GetY());
1515  $textbis = $outputlangsbis->transnoentities($colDef['title']['textkey']);
1516  $pdf->MultiCell($textWidth, 2, $textbis, '', $colDef['title']['align']);
1517  }
1518 
1519  $this->tabTitleHeight = max($pdf->GetY() - $tab_top, $this->tabTitleHeight);
1520 
1521  // restore cell padding
1522  $pdf->setCellPaddings($curentCellPaddinds['L'], $curentCellPaddinds['T'], $curentCellPaddinds['R'], $curentCellPaddinds['B']);
1523  }
1524  }
1525  }
1526 
1527  return $this->tabTitleHeight;
1528  }
1529 
1530 
1531 
1540  public function defineColumnExtrafield($object, $outputlangs, $hidedetails = 0)
1541  {
1542  global $conf;
1543 
1544  if (!empty($hidedetails)) {
1545  return;
1546  }
1547 
1548  if (empty($object->table_element)) {
1549  return;
1550  }
1551 
1552  // Load extrafiels if not allready does
1553  if (empty($this->extrafieldsCache)) { $this->extrafieldsCache = new ExtraFields($this->db); }
1554  if (empty($this->extrafieldsCache->attributes[$object->table_element])) { $this->extrafieldsCache->fetch_name_optionals_label($object->table_element); }
1555  $extrafields = $this->extrafieldsCache;
1556 
1557 
1558  if (!empty($extrafields->attributes[$object->table_element]) && is_array($extrafields->attributes[$object->table_element]['label'])) {
1559  foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $label)
1560  {
1561  // Dont display separator yet even is set to be displayed (not compatible yet)
1562  if ($extrafields->attributes[$object->table_element]['type'][$key] == 'separate')
1563  {
1564  continue;
1565  }
1566 
1567  // Enable extrafield ?
1568  $enabled = 0;
1569  if (!empty($extrafields->attributes[$object->table_element]['printable'][$key])) {
1570  $printable = intval($extrafields->attributes[$object->table_element]['printable'][$key]);
1571  if ($printable === 1 || $printable === 2) {
1572  $enabled = 1;
1573  }
1574  // Note : if $printable === 3 or 4 so, it's displayed after line description not in cols
1575  }
1576 
1577  if (!$enabled) { continue; } // don't wast resourses if we don't need them...
1578 
1579  // Load language if required
1580  if (!empty($extrafields->attributes[$object->table_element]['langfile'][$key])) $outputlangs->load($extrafields->attributes[$object->table_element]['langfile'][$key]);
1581 
1582  // TODO : add more extrafield customisation capacities for PDF like width, rank...
1583 
1584  // set column definition
1585  $def = array(
1586  'rank' => intval($extrafields->attributes[$object->table_element]['pos'][$key]),
1587  'width' => 25, // in mm
1588  'status' => boolval($enabled),
1589  'title' => array(
1590  'label' => $outputlangs->transnoentities($label)
1591  ),
1592  'content' => array(
1593  'align' => 'C'
1594  ),
1595  'border-left' => true, // add left line separator
1596  );
1597 
1598  $alignTypeRight = array('double', 'int', 'price');
1599  if (in_array($extrafields->attributes[$object->table_element]['type'][$key], $alignTypeRight)) {
1600  $def['content']['align'] = 'R';
1601  }
1602 
1603  $alignTypeLeft = array('text', 'html');
1604  if (in_array($extrafields->attributes[$object->table_element]['type'][$key], $alignTypeLeft)) {
1605  $def['content']['align'] = 'L';
1606  }
1607 
1608 
1609  // for extrafields we use rank of extrafield to place it on PDF
1610  $this->insertNewColumnDef("options_".$key, $def);
1611  }
1612  }
1613  }
1614 }
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname= '')
Make an include_once using default root and alternate root if it fails.
getExtrafieldContent($object, $extrafieldKey)
get extrafield content for pdf writeHtmlCell compatibility usage for PDF line columns and object note...
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto= 'UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
prepareArrayColumnField($object, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Prepare Array Column Field.
insertNewColumnDef($newColKey, $defArray, $targetCol=false, $insertAfterTarget=false)
get column position rank from column key
getColumnRank($colKey)
get column position rank from column key
get_substitutionarray_each_var_object(&$object, $outputlangs, $recursive=true)
Define array with couple substitution key =&gt; substitution value.
Class to manage products or services.
printColDescContent($pdf, &$curY, $colKey, $object, $i, $outputlangs, $hideref=0, $hidedesc=0, $issupplierline=0)
print description column content
dol_now($mode= 'auto')
Return date for now.
__construct($db)
Constructor.
get_substitutionarray_user($user, $outputlangs)
Define array with couple substitution key =&gt; substitution value.
isASecretKey($keyname)
Return if string has a name dedicated to store a secret.
get_substitutionarray_shipment($object, $outputlangs, $array_key= 'object')
Define array with couple substitution key =&gt; substitution value.
defineColumnExtrafield($object, $outputlangs, $hidedetails=0)
Define Array Column Field for extrafields.
printStdColumnContent($pdf, &$curY, $colKey, $columnText= '')
print standard column content
get_substitutionarray_object($object, $outputlangs, $array_key= 'object')
Define array with couple substitution key =&gt; substitution value.
doc_getlinedesc($line, $outputlangs, $hideref=0, $hidedesc=0, $issupplierline=0)
Return line description translated in outputlangs and encoded into UTF8.
Definition: doc.lib.php:41
$conf db
API class for accounts.
Definition: inc.php:54
Class to manage bank accounts.
get_substitutionarray_shipment_lines($line, $outputlangs)
Define array with couple substitution key =&gt; substitution value.
price($amount, $form=0, $outlangs= '', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code= '')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
getColumnStatus($colKey)
get column status from column key
Class to manage standard extra fields.
get_substitutionarray_contact($object, $outputlangs, $array_key= 'object')
Define array with couple substitution key =&gt; substitution value.
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
getFormeJuridiqueLabel($code)
Retourne le nom traduit de la forme juridique.
pdfTabTitles(&$pdf, $tab_top, $tab_height, $outputlangs, $hidetop=0)
Print standard column content.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
get_substitutionarray_thirdparty($object, $outputlangs, $array_key= 'company')
Define array with couple substitution key =&gt; substitution value.
getColumnContentXStart($colKey)
get column content X (abscissa) left position from column key
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart= '')
Return a path to have a the directory according to object where files are stored. ...
get_substitutionarray_mysoc($mysoc, $outputlangs)
Define array with couple substitution key =&gt; substitution value.
getState($id, $withcode= '', $dbtouse=0, $withregion=0, $outputlangs= '', $entconv=1)
Return state translated from an id.
printRect($pdf, $x, $y, $l, $h, $hidetop=0, $hidebottom=0)
Rect pdf.
columnSort($a, $b)
uasort callback function to Sort columns fields
vatrate($rate, $addpercent=false, $info_bits=0, $usestarfornpr=0)
Return a string with VAT rate label formated for view output Used into pdf and HTML pages...
getSubstitutionarrayMember($member, $outputlangs)
Define array with couple substitution key =&gt; substitution value.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
pdf_writelinedesc(&$pdf, $object, $i, $outputlangs, $w, $h, $posx, $posy, $hideref=0, $hidedesc=0, $issupplierline=0)
Output line description into PDF.
Definition: pdf.lib.php:1174
fill_substitutionarray_with_extrafields($object, $array_to_fill, $extrafields, $array_key, $outputlangs)
Fill array with couple extrafield key =&gt; extrafield value.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
Parent class for documents generators.
get_substitutionarray_other($outputlangs)
Define array with couple substitution key =&gt; substitution value.
Class to manage invoices.
get_substitutionarray_lines($line, $outputlangs, $linenumber=0)
Define array with couple substitution key =&gt; substitution value.
getColumnContentWidth($colKey)
get column content width from column key
measuringUnitString($unit, $measuring_style= '', $scale= '', $use_short_label=0, $outputlangs=null)
Return translation label of a unit key.