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(
true, 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);
241 $logodir =
$conf->mycompany->dir_output;
242 if (!empty(
$conf->mycompany->multidir_output[
$object->entity ??
$conf->entity])) {
245 $pagecount = $pdf->setSourceFile($logodir .
'/' .
getDolGlobalString(
'MAIN_ADD_PDF_BACKGROUND'));
246 $tplidx = $pdf->importPage(1);
251 $pdf->SetDrawColor(128, 128, 128);
253 $pdf->SetTitle($outputlangs->convToOutputCharset(
$object->ref));
254 $pdf->SetSubject($outputlangs->transnoentities(
"PdfTitle"));
255 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
256 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getAnonymisableFullName($outputlangs)));
257 $pdf->SetKeyWords($outputlangs->convToOutputCharset(
$object->ref).
" ".$outputlangs->transnoentities(
"PdfTitle").
" ".$outputlangs->convToOutputCharset((
string)
$object->thirdparty->name));
259 $pdf->SetCompression(
false);
269 'Name' => $this->emetteur->name,
270 'Location' =>
getCountry($this->emetteur->country_code,
''),
272 'ContactInfo' => $this->emetteur->email
274 $pdf->setSignature($cert, $certprivate, $this->emetteur->name,
'', 2, $info);
278 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
282 if (!empty($tplidx)) {
283 $pdf->useTemplate($tplidx);
287 $top_shift = $this->
_pagehead($pdf,
$object, 1, $outputlangs, $outputlangsbis);
288 $pdf->SetFont(
'',
'', $default_font_size - 1);
289 $pdf->MultiCell(0, 3,
'');
290 $pdf->SetTextColor(0, 0, 0);
292 $tab_top = 90 + $top_shift;
293 $tab_top_newpage = (!
getDolGlobalInt(
'MAIN_PDF_DONOTREPEAT_HEAD') ? 42 + $top_shift : 10);
294 $tab_height = 130 - $top_shift;
295 $tab_height_newpage = 150;
297 $tab_height_newpage -= $top_shift;
300 $nexY = $tab_top - 1;
303 $notetoshow = empty(
$object->note_public) ?
'' :
$object->note_public;
305 $extranote = $this->getExtrafieldsInHtml(
$object, $outputlangs);
306 if (!empty($extranote)) {
310 $pagenb = $pdf->getPage();
314 $tab_width = $this->page_largeur - $this->marge_gauche - $this->marge_droite;
315 $pageposbeforenote = $pagenb;
322 $pdf->startTransaction();
324 $pdf->SetFont(
'',
'', $default_font_size - 1);
325 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top,
dol_htmlentitiesbr($notetoshow), 0, 1);
327 $pageposafternote = $pdf->getPage();
328 $posyafter = $pdf->GetY();
330 if ($pageposafternote > $pageposbeforenote) {
331 $pdf->rollbackTransaction(
true);
334 while ($pagenb < $pageposafternote) {
337 if (!empty($tplidx)) {
338 $pdf->useTemplate($tplidx);
344 $pdf->setTopMargin($tab_top_newpage);
346 $pdf->setPageOrientation(
'',
true, $heightforfooter + $heightforfreetext);
350 $pdf->setPage($pageposbeforenote);
351 $pdf->setPageOrientation(
'',
true, $heightforfooter + $heightforfreetext);
352 $pdf->SetFont(
'',
'', $default_font_size - 1);
353 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top,
dol_htmlentitiesbr($notetoshow), 0, 1);
354 $pageposafternote = $pdf->getPage();
356 $posyafter = $pdf->GetY();
358 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) {
359 $pdf->AddPage(
'',
'',
true);
362 $pdf->setPage($pageposafternote);
363 $pdf->setTopMargin($tab_top_newpage);
365 $pdf->setPageOrientation(
'',
true, $heightforfooter + $heightforfreetext);
371 $i = $pageposbeforenote;
372 while ($i < $pageposafternote) {
376 $pdf->SetDrawColor(128, 128, 128);
378 if ($i > $pageposbeforenote) {
379 $height_note = $this->page_hauteur - ($tab_top_newpage + $heightforfooter);
380 $pdf->RoundedRect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1, $this->corner_radius,
'1234',
'D');
382 $height_note = $this->page_hauteur - ($tab_top + $heightforfooter);
383 $pdf->RoundedRect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1, $this->corner_radius,
'1234',
'D');
387 $pdf->setPageOrientation(
'',
true, 0);
394 $pdf->setPage($pageposafternote);
395 if (!empty($tplidx)) {
396 $pdf->useTemplate($tplidx);
401 $height_note = $posyafter - $tab_top_newpage;
402 $pdf->RoundedRect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1, $this->corner_radius,
'1234',
'D');
404 $pdf->commitTransaction();
405 $posyafter = $pdf->GetY();
406 $height_note = $posyafter - $tab_top;
407 $pdf->RoundedRect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1, $this->corner_radius,
'1234',
'D');
410 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) {
412 $pdf->AddPage(
'',
'',
true);
415 $pdf->setPage($pageposafternote);
416 if (!empty($tplidx)) {
417 $pdf->useTemplate($tplidx);
423 $posyafter = $tab_top_newpage;
427 $tab_height -= $height_note;
428 $tab_top = $posyafter + 6;
437 $pdf->startTransaction();
438 $this->
pdfTabTitles($pdf, $tab_top, $tab_height, $outputlangs, $hidetop);
439 $pdf->rollbackTransaction(
true);
441 $nexY = $tab_top + $this->tabTitleHeight;
444 $pageposbeforeprintlines = $pdf->getPage();
445 $pagenb = $pageposbeforeprintlines;
446 for ($i = 0; $i < $nblines; $i++) {
448 $pdf->SetFont(
'',
'', $default_font_size - 1);
449 $pdf->SetTextColor(0, 0, 0);
451 $pdf->setTopMargin($tab_top_newpage);
452 $pdf->setPageOrientation(
'',
true, $heightforfooter + $heightforfreetext + $heightforinfotot);
453 $pageposbefore = $pdf->getPage();
455 $showpricebeforepagebreak = 1;
460 $pdf->startTransaction();
463 $pageposafter = $pdf->getPage();
465 if ($pageposafter > $pageposbefore) {
466 $pdf->rollbackTransaction(
true);
467 $pdf->setPageOrientation(
'',
true, $heightforfooter);
471 $pageposafter = $pdf->getPage();
472 $posyafter = $pdf->GetY();
474 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
475 if ($i == ($nblines - 1)) {
476 $pdf->AddPage(
'',
'',
true);
477 if (!empty($tplidx)) {
478 $pdf->useTemplate($tplidx);
480 $pdf->setPage($pageposafter + 1);
486 $showpricebeforepagebreak = 1;
488 $showpricebeforepagebreak = 0;
492 $pdf->commitTransaction();
496 $nexY = $pdf->GetY();
497 $pageposafter = $pdf->getPage();
498 $pdf->setPage($pageposbefore);
499 $pdf->setTopMargin($this->marge_haute);
500 $pdf->setPageOrientation(
'',
true, 0);
503 if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
504 $pdf->setPage($pageposafter);
505 $curY = $tab_top_newpage;
508 $pdf->SetFont(
'',
'', $default_font_size - 1);
515 $nexY = max($pdf->GetY(), $nexY);
519 if (!empty(
$object->lines[$i]->array_options)) {
520 foreach (
$object->lines[$i]->array_options as $extrafieldColKey => $extrafieldValue) {
524 $nexY = max($pdf->GetY(), $nexY);
536 'outputlangs' => $outputlangs,
537 'hidedetails' => $hidedetails
539 $reshook = $hookmanager->executeHooks(
'printPDFline', $parameters, $this);
544 $prev_progress =
$object->lines[$i]->get_prev_progress(
$object->id);
545 if ($prev_progress > 0 && !empty(
$object->lines[$i]->situation_percent)) {
547 $tvaligne = $sign *
$object->lines[$i]->multicurrency_total_tva * (
$object->lines[$i]->situation_percent - $prev_progress) /
$object->lines[$i]->situation_percent;
549 $tvaligne = $sign *
$object->lines[$i]->total_tva * (
$object->lines[$i]->situation_percent - $prev_progress) /
$object->lines[$i]->situation_percent;
553 $tvaligne = $sign *
$object->lines[$i]->multicurrency_total_tva;
555 $tvaligne = $sign *
$object->lines[$i]->total_tva;
559 $localtax1ligne =
$object->lines[$i]->total_localtax1;
560 $localtax2ligne =
$object->lines[$i]->total_localtax2;
561 $localtax1_rate =
$object->lines[$i]->localtax1_tx;
562 $localtax2_rate =
$object->lines[$i]->localtax2_tx;
563 $localtax1_type =
$object->lines[$i]->localtax1_type;
564 $localtax2_type =
$object->lines[$i]->localtax2_type;
580 if ((!isset($localtax1_type) || $localtax1_type ==
'' || !isset($localtax2_type) || $localtax2_type ==
'')
581 && (!empty($localtax1_rate) || !empty($localtax2_rate))) {
583 $localtax1_type = isset($localtaxtmp_array[0]) ? $localtaxtmp_array[0] :
'';
584 $localtax2_type = isset($localtaxtmp_array[2]) ? $localtaxtmp_array[2] :
'';
588 if ($localtax1_type && $localtax1ligne != 0) {
589 if (empty($this->localtax1[$localtax1_type][$localtax1_rate])) {
590 $this->localtax1[$localtax1_type][$localtax1_rate] = $localtax1ligne;
592 $this->localtax1[$localtax1_type][$localtax1_rate] += $localtax1ligne;
595 if ($localtax2_type && $localtax2ligne != 0) {
596 if (empty($this->localtax2[$localtax2_type][$localtax2_rate])) {
597 $this->localtax2[$localtax2_type][$localtax2_rate] = $localtax2ligne;
599 $this->localtax2[$localtax2_type][$localtax2_rate] += $localtax2ligne;
603 if (((
int)
$object->lines[$i]->info_bits & 0x01) == 0x01) {
606 if (!isset($this->tva[$vatrate])) {
607 $this->tva[$vatrate] = 0;
609 $this->tva[$vatrate] += $tvaligne;
611 $nexY = max($nexY, $posYAfterImage);
615 $pdf->setPage($pageposafter);
616 $pdf->SetLineStyle(array(
'dash' =>
'1,1',
'color' => array(80, 80, 80)));
618 $pdf->line($this->marge_gauche, $nexY, $this->page_largeur - $this->marge_droite, $nexY);
619 $pdf->SetLineStyle(array(
'dash' => 0));
623 while ($pagenb < $pageposafter) {
624 $pdf->setPage($pagenb);
625 if ($pagenb == $pageposbeforeprintlines) {
626 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1,
$object->multicurrency_code, $outputlangsbis);
628 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1,
$object->multicurrency_code, $outputlangsbis);
632 $pdf->setPage($pagenb);
633 $pdf->setPageOrientation(
'',
true, 0);
639 if (isset(
$object->lines[$i + 1]->pagebreak) &&
$object->lines[$i + 1]->pagebreak) {
640 if ($pagenb == $pageposafter) {
641 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1,
$object->multicurrency_code, $outputlangsbis);
643 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1,
$object->multicurrency_code, $outputlangsbis);
648 if (!empty($tplidx)) {
649 $pdf->useTemplate($tplidx);
659 if ($pagenb == $pageposbeforeprintlines) {
660 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, $hidetop, 0,
$object->multicurrency_code, $outputlangsbis);
661 $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
663 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0,
$object->multicurrency_code, $outputlangsbis);
664 $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
683 if (method_exists($pdf,
'AliasNbPages')) {
684 $pdf->AliasNbPages();
689 $pdf->Output($file,
'F');
692 $hookmanager->initHooks(array(
'pdfgeneration'));
693 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
695 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
696 $this->warnings = $hookmanager->warnings;
698 $this->error = $hookmanager->error;
699 $this->errors = $hookmanager->errors;
706 $this->result = array(
'fullpath' => $file);
710 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
714 $this->error = $langs->transnoentities(
"ErrorConstantNotDefined",
"ASSET_OUTPUTDIR");
805 protected function _pagehead(&$pdf,
$object, $showaddress, $outputlangs, $outputlangsbis =
null)
807 global
$conf, $langs;
810 $outputlangs->loadLangs(array(
"main",
"bills",
"propal",
"companies"));
818 pdf_watermark($pdf, $outputlangs, $this->page_hauteur, $this->page_largeur,
'mm',
$conf->global->FACTURE_DRAFT_WATERMARK);
821 $pdf->SetTextColor(0, 0, 60);
822 $pdf->SetFont(
'',
'B', $default_font_size + 3);
826 $posy = $this->marge_haute;
827 $posx = $this->page_largeur - $this->marge_droite - $w;
829 $pdf->SetXY($this->marge_gauche, $posy);
833 if ($this->emetteur->logo) {
834 $logodir =
$conf->mycompany->dir_output;
835 if (!empty(
$conf->mycompany->multidir_output[
$object->entity ??
$conf->entity])) {
839 $logo = $logodir.
'/logos/thumbs/'.$this->emetteur->logo_small;
841 $logo = $logodir.
'/logos/'.$this->emetteur->logo;
843 if (is_readable($logo)) {
845 $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height);
847 $pdf->SetTextColor(200, 0, 0);
848 $pdf->SetFont(
'',
'B', $default_font_size - 2);
849 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorLogoFileNotFound", $logo), 0,
'L');
850 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorGoToGlobalSetup"), 0,
'L');
853 $text = (
string) $this->emetteur->name;
854 $pdf->MultiCell($w, 4, $outputlangs->convToOutputCharset($text), 0,
'L');
858 $pdf->SetFont(
'',
'B', $default_font_size + 3);
859 $pdf->SetXY($posx, $posy);
860 $pdf->SetTextColor(0, 0, 60);
861 $title = $outputlangs->transnoentities(
"PdfTitle");
864 $title .= $outputlangsbis->transnoentities(
"PdfTitle");
866 $pdf->MultiCell($w, 3, $title,
'',
'R');
868 $pdf->SetFont(
'',
'B', $default_font_size);
871 $pdf->SetXY($posx, $posy);
872 $pdf->SetTextColor(0, 0, 60);
873 $textref = $outputlangs->transnoentities(
"Ref").
" : ".$outputlangs->convToOutputCharset(
$object->ref);
874 if (
$object->status == $object::STATUS_DRAFT) {
875 $pdf->SetTextColor(128, 0, 0);
876 $textref .=
' - '.$outputlangs->transnoentities(
"NotValidated");
878 $pdf->MultiCell($w, 4, $textref,
'',
'R');
881 $pdf->SetFont(
'',
'', $default_font_size - 2);
892 if (!empty(
$object->project->ref)) {
894 $pdf->SetXY($posx, $posy);
895 $pdf->SetTextColor(0, 0, 60);
896 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"Project").
" : ".(empty(
$object->project->title) ?
'' :
$object->project->title),
'',
'R');
902 if (!empty(
$object->project->ref)) {
903 $outputlangs->load(
"projects");
905 $pdf->SetXY($posx, $posy);
906 $pdf->SetTextColor(0, 0, 60);
907 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"RefProject").
" : ".(empty(
$object->project->ref) ?
'' :
$object->project->ref),
'',
'R');
912 $pdf->SetXY($posx, $posy);
913 $pdf->SetTextColor(0, 0, 60);
915 $title = $outputlangs->transnoentities(
"Date");
917 $title .=
' - '.$outputlangsbis->transnoentities(
"Date");
919 $pdf->MultiCell($w, 3, $title.
" : ".
dol_print_date(
$object->date_acquisition,
"day",
false, $outputlangs),
'',
'R');
921 if (
$object->thirdparty->code_client) {
923 $pdf->SetXY($posx, $posy);
924 $pdf->SetTextColor(0, 0, 60);
925 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerCode").
" : ".$outputlangs->transnoentities((
string)
$object->thirdparty->code_client),
'',
'R');
930 $arrayidcontact =
$object->getIdContact(
'internal',
'SALESREPFOLL');
931 if (count($arrayidcontact) > 0) {
932 $usertmp =
new User($this->db);
933 $usertmp->fetch($arrayidcontact[0]);
935 $pdf->SetXY($posx, $posy);
936 $pdf->SetTextColor(0, 0, 60);
937 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"SalesRepresentative").
" : ".$usertmp->getFullName($langs),
'',
'R');
945 $current_y = $pdf->getY();
947 if ($current_y < $pdf->getY()) {
948 $top_shift = $pdf->getY() - $current_y;
958 $posx = $this->marge_gauche;
960 $posx = $this->page_largeur - $this->marge_droite - 80;
968 $pdf->SetTextColor(0, 0, 0);
969 $pdf->SetFont(
'',
'', $default_font_size - 2);
970 $pdf->SetXY($posx, $posy - 5);
971 $pdf->MultiCell(66, 5, $outputlangs->transnoentities(
"BillFrom").
":", 0,
'L');
972 $pdf->SetXY($posx, $posy);
973 $pdf->SetFillColor(230, 230, 230);
974 $pdf->RoundedRect($posx, $posy, $widthrecbox, $hautcadre, $this->corner_radius,
'1234',
'D');
975 $pdf->SetTextColor(0, 0, 60);
978 $pdf->SetXY($posx + 2, $posy + 3);
979 $pdf->SetFont(
'',
'B', $default_font_size);
980 $pdf->MultiCell($widthrecbox - 2, 4, $outputlangs->convToOutputCharset((
string) $this->emetteur->name), 0,
'L');
981 $posy = $pdf->getY();
984 $pdf->SetXY($posx + 2, $posy);
985 $pdf->SetFont(
'',
'', $default_font_size - 1);
986 $pdf->MultiCell($widthrecbox - 2, 4, $carac_emetteur, 0,
'L');
990 $arrayidcontact =
$object->getIdContact(
'external',
'BILLING');
991 if (count($arrayidcontact) > 0) {
993 $result =
$object->fetch_contact($arrayidcontact[0]);
998 $thirdparty =
$object->contact;
1000 $thirdparty =
$object->thirdparty;
1003 if (is_object($thirdparty)) {
1006 $carac_client_name =
'';
1013 if ($this->page_largeur < 210) {
1017 $posy += $top_shift;
1018 $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
1020 $posx = $this->marge_gauche;
1024 $pdf->SetTextColor(0, 0, 0);
1025 $pdf->SetFont(
'',
'', $default_font_size - 2);
1026 $pdf->SetXY($posx + 2, $posy - 5);
1027 $pdf->MultiCell($widthrecbox, 5, $outputlangs->transnoentities(
"BillTo").
":", 0,
'L');
1028 $pdf->RoundedRect($posx, $posy, $widthrecbox, $hautcadre, $this->corner_radius,
'1234',
'D');
1031 $pdf->SetXY($posx + 2, $posy + 3);
1032 $pdf->SetFont(
'',
'B', $default_font_size);
1033 $pdf->MultiCell($widthrecbox, 2, $carac_client_name, 0,
'L');
1035 $posy = $pdf->getY();
1038 $pdf->SetFont(
'',
'', $default_font_size - 1);
1039 $pdf->SetXY($posx + 2, $posy);
1040 $pdf->MultiCell($widthrecbox, 4, $carac_client, 0,
'L');
1043 $pdf->SetTextColor(0, 0, 0);