158 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
161 global $user, $langs,
$conf, $mysoc, $db, $hookmanager, $nblines;
163 dol_syslog(
"write_file outputlangs->defaultlang=".(is_object($outputlangs) ? $outputlangs->defaultlang :
'null'));
165 if (!is_object($outputlangs)) {
166 $outputlangs = $langs;
170 $outputlangs->charset_output =
'ISO-8859-1';
174 $outputlangs->loadLangs(array(
"main",
"bills",
"products",
"dict",
"companies"));
176 global $outputlangsbis;
177 $outputlangsbis =
null;
181 $outputlangsbis->loadLangs(array(
"main",
"bills",
"products",
"dict",
"companies"));
184 $nblines = (is_array(
$object->lines) ? count(
$object->lines) : 0);
191 if (
$conf->asset->dir_output.
'/asset') {
196 $dir =
$conf->asset->dir_output.
'/asset';
197 $file = $dir.
"/SPECIMEN.pdf";
200 $dir =
$conf->asset->dir_output.
'/asset/'.$objectref;
201 $file = $dir.
"/".$objectref.
".pdf";
203 if (!file_exists($dir)) {
205 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
210 if (file_exists($dir)) {
212 if (!is_object($hookmanager)) {
213 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
216 $hookmanager->initHooks(array(
'pdfgeneration'));
217 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
219 $reshook = $hookmanager->executeHooks(
'beforePDFCreation', $parameters,
$object, $action);
222 $nblines = (is_array(
$object->lines) ? count(
$object->lines) : 0);
227 $pdf->SetAutoPageBreak(1, 0);
229 $heightforinfotot = 50;
231 $heightforfooter = $this->marge_basse + (!
getDolGlobalString(
'MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS') ? 12 : 22);
233 if (class_exists(
'TCPDF')) {
234 $pdf->setPrintHeader(
false);
235 $pdf->setPrintFooter(
false);
242 $tplidx = $pdf->importPage(1);
247 $pdf->SetDrawColor(128, 128, 128);
249 $pdf->SetTitle($outputlangs->convToOutputCharset(
$object->ref));
250 $pdf->SetSubject($outputlangs->transnoentities(
"PdfTitle"));
251 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
252 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
253 $pdf->SetKeyWords($outputlangs->convToOutputCharset(
$object->ref).
" ".$outputlangs->transnoentities(
"PdfTitle").
" ".$outputlangs->convToOutputCharset(
$object->thirdparty->name));
255 $pdf->SetCompression(
false);
259 $cert = empty($user->conf->CERTIFICATE_CRT) ?
'' : $user->conf->CERTIFICATE_CRT;
267 'Name' => $this->emetteur->name,
268 'Location' =>
getCountry($this->emetteur->country_code,
''),
270 'ContactInfo' => $this->emetteur->email
272 $pdf->setSignature($cert, $cert, $this->emetteur->name,
'', 2, $info);
276 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
280 if (!empty($tplidx)) {
281 $pdf->useTemplate($tplidx);
285 $top_shift = $this->
_pagehead($pdf,
$object, 1, $outputlangs, $outputlangsbis);
286 $pdf->SetFont(
'',
'', $default_font_size - 1);
287 $pdf->MultiCell(0, 3,
'');
288 $pdf->SetTextColor(0, 0, 0);
290 $tab_top = 90 + $top_shift;
291 $tab_top_newpage = (!
getDolGlobalInt(
'MAIN_PDF_DONOTREPEAT_HEAD') ? 42 + $top_shift : 10);
292 $tab_height = 130 - $top_shift;
293 $tab_height_newpage = 150;
295 $tab_height_newpage -= $top_shift;
298 $nexY = $tab_top - 1;
301 $notetoshow = empty(
$object->note_public) ?
'' :
$object->note_public;
303 $extranote = $this->getExtrafieldsInHtml(
$object, $outputlangs);
304 if (!empty($extranote)) {
308 $pagenb = $pdf->getPage();
312 $tab_width = $this->page_largeur - $this->marge_gauche - $this->marge_droite;
313 $pageposbeforenote = $pagenb;
320 $pdf->startTransaction();
322 $pdf->SetFont(
'',
'', $default_font_size - 1);
323 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top,
dol_htmlentitiesbr($notetoshow), 0, 1);
325 $pageposafternote = $pdf->getPage();
326 $posyafter = $pdf->GetY();
328 if ($pageposafternote > $pageposbeforenote) {
329 $pdf->rollbackTransaction(
true);
332 while ($pagenb < $pageposafternote) {
335 if (!empty($tplidx)) {
336 $pdf->useTemplate($tplidx);
342 $pdf->setTopMargin($tab_top_newpage);
344 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext);
348 $pdf->setPage($pageposbeforenote);
349 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext);
350 $pdf->SetFont(
'',
'', $default_font_size - 1);
351 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top,
dol_htmlentitiesbr($notetoshow), 0, 1);
352 $pageposafternote = $pdf->getPage();
354 $posyafter = $pdf->GetY();
356 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) {
357 $pdf->AddPage(
'',
'',
true);
360 $pdf->setPage($pageposafternote);
361 $pdf->setTopMargin($tab_top_newpage);
363 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext);
369 $i = $pageposbeforenote;
370 while ($i < $pageposafternote) {
374 $pdf->SetDrawColor(128, 128, 128);
376 if ($i > $pageposbeforenote) {
377 $height_note = $this->page_hauteur - ($tab_top_newpage + $heightforfooter);
378 $pdf->RoundedRect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1, $this->corner_radius,
'1234',
'D');
380 $height_note = $this->page_hauteur - ($tab_top + $heightforfooter);
381 $pdf->RoundedRect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1, $this->corner_radius,
'1234',
'D');
385 $pdf->setPageOrientation(
'', 1, 0);
392 $pdf->setPage($pageposafternote);
393 if (!empty($tplidx)) {
394 $pdf->useTemplate($tplidx);
399 $height_note = $posyafter - $tab_top_newpage;
400 $pdf->RoundedRect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1, $this->corner_radius,
'1234',
'D');
402 $pdf->commitTransaction();
403 $posyafter = $pdf->GetY();
404 $height_note = $posyafter - $tab_top;
405 $pdf->RoundedRect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1, $this->corner_radius,
'1234',
'D');
408 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) {
410 $pdf->AddPage(
'',
'',
true);
413 $pdf->setPage($pageposafternote);
414 if (!empty($tplidx)) {
415 $pdf->useTemplate($tplidx);
421 $posyafter = $tab_top_newpage;
425 $tab_height -= $height_note;
426 $tab_top = $posyafter + 6;
435 $pdf->startTransaction();
436 $this->
pdfTabTitles($pdf, $tab_top, $tab_height, $outputlangs, $hidetop);
437 $pdf->rollbackTransaction(
true);
439 $nexY = $tab_top + $this->tabTitleHeight;
442 $pageposbeforeprintlines = $pdf->getPage();
443 $pagenb = $pageposbeforeprintlines;
444 for ($i = 0; $i < $nblines; $i++) {
446 $pdf->SetFont(
'',
'', $default_font_size - 1);
447 $pdf->SetTextColor(0, 0, 0);
449 $pdf->setTopMargin($tab_top_newpage);
450 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext + $heightforinfotot);
451 $pageposbefore = $pdf->getPage();
453 $showpricebeforepagebreak = 1;
458 $pdf->startTransaction();
461 $pageposafter = $pdf->getPage();
463 if ($pageposafter > $pageposbefore) {
464 $pdf->rollbackTransaction(
true);
465 $pdf->setPageOrientation(
'', 1, $heightforfooter);
469 $pageposafter = $pdf->getPage();
470 $posyafter = $pdf->GetY();
472 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
473 if ($i == ($nblines - 1)) {
474 $pdf->AddPage(
'',
'',
true);
475 if (!empty($tplidx)) {
476 $pdf->useTemplate($tplidx);
478 $pdf->setPage($pageposafter + 1);
484 $showpricebeforepagebreak = 1;
486 $showpricebeforepagebreak = 0;
490 $pdf->commitTransaction();
494 $nexY = $pdf->GetY();
495 $pageposafter = $pdf->getPage();
496 $pdf->setPage($pageposbefore);
497 $pdf->setTopMargin($this->marge_haute);
498 $pdf->setPageOrientation(
'', 1, 0);
501 if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
502 $pdf->setPage($pageposafter);
503 $curY = $tab_top_newpage;
506 $pdf->SetFont(
'',
'', $default_font_size - 1);
513 $nexY = max($pdf->GetY(), $nexY);
517 if (!empty(
$object->lines[$i]->array_options)) {
518 foreach (
$object->lines[$i]->array_options as $extrafieldColKey => $extrafieldValue) {
522 $nexY = max($pdf->GetY(), $nexY);
534 'outputlangs' => $outputlangs,
535 'hidedetails' => $hidedetails
537 $reshook = $hookmanager->executeHooks(
'printPDFline', $parameters, $this);
542 $prev_progress =
$object->lines[$i]->get_prev_progress(
$object->id);
543 if ($prev_progress > 0 && !empty(
$object->lines[$i]->situation_percent)) {
544 if (isModEnabled(
"multicurrency") &&
$object->multicurrency_tx != 1) {
545 $tvaligne = $sign *
$object->lines[$i]->multicurrency_total_tva * (
$object->lines[$i]->situation_percent - $prev_progress) /
$object->lines[$i]->situation_percent;
547 $tvaligne = $sign *
$object->lines[$i]->total_tva * (
$object->lines[$i]->situation_percent - $prev_progress) /
$object->lines[$i]->situation_percent;
550 if (isModEnabled(
"multicurrency") &&
$object->multicurrency_tx != 1) {
551 $tvaligne = $sign *
$object->lines[$i]->multicurrency_total_tva;
553 $tvaligne = $sign *
$object->lines[$i]->total_tva;
557 $localtax1ligne =
$object->lines[$i]->total_localtax1;
558 $localtax2ligne =
$object->lines[$i]->total_localtax2;
559 $localtax1_rate =
$object->lines[$i]->localtax1_tx;
560 $localtax2_rate =
$object->lines[$i]->localtax2_tx;
561 $localtax1_type =
$object->lines[$i]->localtax1_type;
562 $localtax2_type =
$object->lines[$i]->localtax2_type;
575 $vatrate = (string)
$object->lines[$i]->tva_tx;
578 if ((!isset($localtax1_type) || $localtax1_type ==
'' || !isset($localtax2_type) || $localtax2_type ==
'')
579 && (!empty($localtax1_rate) || !empty($localtax2_rate))) {
581 $localtax1_type = isset($localtaxtmp_array[0]) ? $localtaxtmp_array[0] :
'';
582 $localtax2_type = isset($localtaxtmp_array[2]) ? $localtaxtmp_array[2] :
'';
586 if ($localtax1_type && $localtax1ligne != 0) {
587 if (empty($this->localtax1[$localtax1_type][$localtax1_rate])) {
588 $this->localtax1[$localtax1_type][$localtax1_rate] = $localtax1ligne;
590 $this->localtax1[$localtax1_type][$localtax1_rate] += $localtax1ligne;
593 if ($localtax2_type && $localtax2ligne != 0) {
594 if (empty($this->localtax2[$localtax2_type][$localtax2_rate])) {
595 $this->localtax2[$localtax2_type][$localtax2_rate] = $localtax2ligne;
597 $this->localtax2[$localtax2_type][$localtax2_rate] += $localtax2ligne;
601 if ((
$object->lines[$i]->info_bits & 0x01) == 0x01) {
604 if (!isset($this->tva[$vatrate])) {
605 $this->tva[$vatrate] = 0;
607 $this->tva[$vatrate] += $tvaligne;
609 $nexY = max($nexY, $posYAfterImage);
613 $pdf->setPage($pageposafter);
614 $pdf->SetLineStyle(array(
'dash' =>
'1,1',
'color' => array(80, 80, 80)));
616 $pdf->line($this->marge_gauche, $nexY, $this->page_largeur - $this->marge_droite, $nexY);
617 $pdf->SetLineStyle(array(
'dash' => 0));
621 while ($pagenb < $pageposafter) {
622 $pdf->setPage($pagenb);
623 if ($pagenb == $pageposbeforeprintlines) {
624 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1,
$object->multicurrency_code, $outputlangsbis);
626 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1,
$object->multicurrency_code, $outputlangsbis);
630 $pdf->setPage($pagenb);
631 $pdf->setPageOrientation(
'', 1, 0);
637 if (isset(
$object->lines[$i + 1]->pagebreak) &&
$object->lines[$i + 1]->pagebreak) {
638 if ($pagenb == $pageposafter) {
639 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1,
$object->multicurrency_code, $outputlangsbis);
641 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1,
$object->multicurrency_code, $outputlangsbis);
646 if (!empty($tplidx)) {
647 $pdf->useTemplate($tplidx);
657 if ($pagenb == $pageposbeforeprintlines) {
658 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, $hidetop, 0,
$object->multicurrency_code, $outputlangsbis);
659 $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
661 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0,
$object->multicurrency_code, $outputlangsbis);
662 $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
681 if (method_exists($pdf,
'AliasNbPages')) {
682 $pdf->AliasNbPages();
687 $pdf->Output($file,
'F');
690 $hookmanager->initHooks(array(
'pdfgeneration'));
691 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
693 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
695 $this->error = $hookmanager->error;
696 $this->errors = $hookmanager->errors;
701 $this->result = array(
'fullpath' => $file);
705 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
709 $this->error = $langs->transnoentities(
"ErrorConstantNotDefined",
"ASSET_OUTPUTDIR");
800 protected function _pagehead(&$pdf,
$object, $showaddress, $outputlangs, $outputlangsbis =
null)
802 global
$conf, $langs;
805 $outputlangs->loadLangs(array(
"main",
"bills",
"propal",
"companies"));
813 pdf_watermark($pdf, $outputlangs, $this->page_hauteur, $this->page_largeur,
'mm',
$conf->global->FACTURE_DRAFT_WATERMARK);
816 $pdf->SetTextColor(0, 0, 60);
817 $pdf->SetFont(
'',
'B', $default_font_size + 3);
821 $posy = $this->marge_haute;
822 $posx = $this->page_largeur - $this->marge_droite - $w;
824 $pdf->SetXY($this->marge_gauche, $posy);
828 if ($this->emetteur->logo) {
829 $logodir =
$conf->mycompany->dir_output;
830 if (!empty(
$conf->mycompany->multidir_output[
$object->entity])) {
831 $logodir =
$conf->mycompany->multidir_output[
$object->entity];
834 $logo = $logodir.
'/logos/thumbs/'.$this->emetteur->logo_small;
836 $logo = $logodir.
'/logos/'.$this->emetteur->logo;
838 if (is_readable($logo)) {
840 $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height);
842 $pdf->SetTextColor(200, 0, 0);
843 $pdf->SetFont(
'',
'B', $default_font_size - 2);
844 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorLogoFileNotFound", $logo), 0,
'L');
845 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorGoToGlobalSetup"), 0,
'L');
848 $text = $this->emetteur->name;
849 $pdf->MultiCell($w, 4, $outputlangs->convToOutputCharset($text), 0,
'L');
853 $pdf->SetFont(
'',
'B', $default_font_size + 3);
854 $pdf->SetXY($posx, $posy);
855 $pdf->SetTextColor(0, 0, 60);
856 $title = $outputlangs->transnoentities(
"PdfTitle");
859 $title .= $outputlangsbis->transnoentities(
"PdfTitle");
861 $pdf->MultiCell($w, 3, $title,
'',
'R');
863 $pdf->SetFont(
'',
'B', $default_font_size);
866 $pdf->SetXY($posx, $posy);
867 $pdf->SetTextColor(0, 0, 60);
868 $textref = $outputlangs->transnoentities(
"Ref").
" : ".$outputlangs->convToOutputCharset(
$object->ref);
869 if (
$object->status == $object::STATUS_DRAFT) {
870 $pdf->SetTextColor(128, 0, 0);
871 $textref .=
' - '.$outputlangs->transnoentities(
"NotValidated");
873 $pdf->MultiCell($w, 4, $textref,
'',
'R');
876 $pdf->SetFont(
'',
'', $default_font_size - 2);
887 if (!empty(
$object->project->ref)) {
889 $pdf->SetXY($posx, $posy);
890 $pdf->SetTextColor(0, 0, 60);
891 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"Project").
" : ".(empty(
$object->project->title) ?
'' :
$object->projet->title),
'',
'R');
897 if (!empty(
$object->project->ref)) {
898 $outputlangs->load(
"projects");
900 $pdf->SetXY($posx, $posy);
901 $pdf->SetTextColor(0, 0, 60);
902 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"RefProject").
" : ".(empty(
$object->project->ref) ?
'' :
$object->project->ref),
'',
'R');
907 $pdf->SetXY($posx, $posy);
908 $pdf->SetTextColor(0, 0, 60);
910 $title = $outputlangs->transnoentities(
"Date");
912 $title .=
' - '.$outputlangsbis->transnoentities(
"Date");
914 $pdf->MultiCell($w, 3, $title.
" : ".
dol_print_date(
$object->date_acquisition,
"day",
false, $outputlangs),
'',
'R');
916 if (
$object->thirdparty->code_client) {
918 $pdf->SetXY($posx, $posy);
919 $pdf->SetTextColor(0, 0, 60);
920 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerCode").
" : ".$outputlangs->transnoentities(
$object->thirdparty->code_client),
'',
'R');
925 $arrayidcontact =
$object->getIdContact(
'internal',
'SALESREPFOLL');
926 if (count($arrayidcontact) > 0) {
927 $usertmp =
new User($this->db);
928 $usertmp->fetch($arrayidcontact[0]);
930 $pdf->SetXY($posx, $posy);
931 $pdf->SetTextColor(0, 0, 60);
932 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"SalesRepresentative").
" : ".$usertmp->getFullName($langs),
'',
'R');
940 $current_y = $pdf->getY();
942 if ($current_y < $pdf->getY()) {
943 $top_shift = $pdf->getY() - $current_y;
953 $posx = $this->marge_gauche;
955 $posx = $this->page_largeur - $this->marge_droite - 80;
963 $pdf->SetTextColor(0, 0, 0);
964 $pdf->SetFont(
'',
'', $default_font_size - 2);
965 $pdf->SetXY($posx, $posy - 5);
966 $pdf->MultiCell(66, 5, $outputlangs->transnoentities(
"BillFrom").
":", 0,
'L');
967 $pdf->SetXY($posx, $posy);
968 $pdf->SetFillColor(230, 230, 230);
969 $pdf->RoundedRect($posx, $posy, $widthrecbox, $hautcadre, $this->corner_radius,
'1234',
'D');
970 $pdf->SetTextColor(0, 0, 60);
973 $pdf->SetXY($posx + 2, $posy + 3);
974 $pdf->SetFont(
'',
'B', $default_font_size);
975 $pdf->MultiCell($widthrecbox - 2, 4, $outputlangs->convToOutputCharset($this->emetteur->name), 0,
'L');
976 $posy = $pdf->getY();
979 $pdf->SetXY($posx + 2, $posy);
980 $pdf->SetFont(
'',
'', $default_font_size - 1);
981 $pdf->MultiCell($widthrecbox - 2, 4, $carac_emetteur, 0,
'L');
985 $arrayidcontact =
$object->getIdContact(
'external',
'BILLING');
986 if (count($arrayidcontact) > 0) {
988 $result =
$object->fetch_contact($arrayidcontact[0]);
993 $thirdparty =
$object->contact;
995 $thirdparty =
$object->thirdparty;
998 if (is_object($thirdparty)) {
1006 if ($this->page_largeur < 210) {
1010 $posy += $top_shift;
1011 $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
1013 $posx = $this->marge_gauche;
1017 $pdf->SetTextColor(0, 0, 0);
1018 $pdf->SetFont(
'',
'', $default_font_size - 2);
1019 $pdf->SetXY($posx + 2, $posy - 5);
1020 $pdf->MultiCell($widthrecbox, 5, $outputlangs->transnoentities(
"BillTo").
":", 0,
'L');
1021 $pdf->RoundedRect($posx, $posy, $widthrecbox, $hautcadre, $this->corner_radius,
'1234',
'D');
1024 $pdf->SetXY($posx + 2, $posy + 3);
1025 $pdf->SetFont(
'',
'B', $default_font_size);
1026 $pdf->MultiCell($widthrecbox, 2, $carac_client_name, 0,
'L');
1028 $posy = $pdf->getY();
1031 $pdf->SetFont(
'',
'', $default_font_size - 1);
1032 $pdf->SetXY($posx + 2, $posy);
1033 $pdf->MultiCell($widthrecbox, 4, $carac_client, 0,
'L');
1036 $pdf->SetTextColor(0, 0, 0);