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"));
177 global $outputlangsbis;
180 $outputlangsbis->loadLangs(array(
"main",
"bills",
"products",
"dict",
"companies"));
183 $nblines = (is_array(
$object->lines) ? count(
$object->lines) : 0);
190 if (
$conf->asset->dir_output.
'/asset') {
195 $dir =
$conf->asset->dir_output.
'/asset';
196 $file = $dir.
"/SPECIMEN.pdf";
199 $dir =
$conf->asset->dir_output.
'/asset/'.$objectref;
200 $file = $dir.
"/".$objectref.
".pdf";
202 if (!file_exists($dir)) {
204 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
209 if (file_exists($dir)) {
211 if (!is_object($hookmanager)) {
212 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
215 $hookmanager->initHooks(array(
'pdfgeneration'));
216 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
218 $reshook = $hookmanager->executeHooks(
'beforePDFCreation', $parameters,
$object, $action);
221 $nblines = (is_array(
$object->lines) ? count(
$object->lines) : 0);
226 $pdf->SetAutoPageBreak(1, 0);
228 $heightforinfotot = 50;
230 $heightforfooter = $this->marge_basse + (!
getDolGlobalString(
'MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS') ? 12 : 22);
232 if (class_exists(
'TCPDF')) {
233 $pdf->setPrintHeader(
false);
234 $pdf->setPrintFooter(
false);
241 $tplidx = $pdf->importPage(1);
246 $pdf->SetDrawColor(128, 128, 128);
248 $pdf->SetTitle($outputlangs->convToOutputCharset(
$object->ref));
249 $pdf->SetSubject($outputlangs->transnoentities(
"PdfTitle"));
250 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
251 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
252 $pdf->SetKeyWords($outputlangs->convToOutputCharset(
$object->ref).
" ".$outputlangs->transnoentities(
"PdfTitle").
" ".$outputlangs->convToOutputCharset(
$object->thirdparty->name));
254 $pdf->SetCompression(
false);
258 $cert = empty($user->conf->CERTIFICATE_CRT) ?
'' : $user->conf->CERTIFICATE_CRT;
266 'Name' => $this->emetteur->name,
267 'Location' =>
getCountry($this->emetteur->country_code,
''),
269 'ContactInfo' => $this->emetteur->email
271 $pdf->setSignature($cert, $cert, $this->emetteur->name,
'', 2, $info);
275 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
279 if (!empty($tplidx)) {
280 $pdf->useTemplate($tplidx);
284 $top_shift = $this->
_pagehead($pdf,
$object, 1, $outputlangs, $outputlangsbis);
285 $pdf->SetFont(
'',
'', $default_font_size - 1);
286 $pdf->MultiCell(0, 3,
'');
287 $pdf->SetTextColor(0, 0, 0);
289 $tab_top = 90 + $top_shift;
290 $tab_top_newpage = (!
getDolGlobalInt(
'MAIN_PDF_DONOTREPEAT_HEAD') ? 42 + $top_shift : 10);
291 $tab_height = 130 - $top_shift;
292 $tab_height_newpage = 150;
294 $tab_height_newpage -= $top_shift;
297 $nexY = $tab_top - 1;
300 $notetoshow = empty(
$object->note_public) ?
'' :
$object->note_public;
302 $extranote = $this->getExtrafieldsInHtml(
$object, $outputlangs);
303 if (!empty($extranote)) {
307 $pagenb = $pdf->getPage();
311 $tab_width = $this->page_largeur - $this->marge_gauche - $this->marge_droite;
312 $pageposbeforenote = $pagenb;
319 $pdf->startTransaction();
321 $pdf->SetFont(
'',
'', $default_font_size - 1);
322 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top,
dol_htmlentitiesbr($notetoshow), 0, 1);
324 $pageposafternote = $pdf->getPage();
325 $posyafter = $pdf->GetY();
327 if ($pageposafternote > $pageposbeforenote) {
328 $pdf->rollbackTransaction(
true);
331 while ($pagenb < $pageposafternote) {
334 if (!empty($tplidx)) {
335 $pdf->useTemplate($tplidx);
341 $pdf->setTopMargin($tab_top_newpage);
343 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext);
347 $pdf->setPage($pageposbeforenote);
348 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext);
349 $pdf->SetFont(
'',
'', $default_font_size - 1);
350 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top,
dol_htmlentitiesbr($notetoshow), 0, 1);
351 $pageposafternote = $pdf->getPage();
353 $posyafter = $pdf->GetY();
355 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) {
356 $pdf->AddPage(
'',
'',
true);
359 $pdf->setPage($pageposafternote);
360 $pdf->setTopMargin($tab_top_newpage);
362 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext);
368 $i = $pageposbeforenote;
369 while ($i < $pageposafternote) {
373 $pdf->SetDrawColor(128, 128, 128);
375 if ($i > $pageposbeforenote) {
376 $height_note = $this->page_hauteur - ($tab_top_newpage + $heightforfooter);
377 $pdf->RoundedRect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1, $this->corner_radius,
'1234',
'D');
379 $height_note = $this->page_hauteur - ($tab_top + $heightforfooter);
380 $pdf->RoundedRect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1, $this->corner_radius,
'1234',
'D');
384 $pdf->setPageOrientation(
'', 1, 0);
391 $pdf->setPage($pageposafternote);
392 if (!empty($tplidx)) {
393 $pdf->useTemplate($tplidx);
398 $height_note = $posyafter - $tab_top_newpage;
399 $pdf->RoundedRect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1, $this->corner_radius,
'1234',
'D');
401 $pdf->commitTransaction();
402 $posyafter = $pdf->GetY();
403 $height_note = $posyafter - $tab_top;
404 $pdf->RoundedRect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1, $this->corner_radius,
'1234',
'D');
407 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) {
409 $pdf->AddPage(
'',
'',
true);
412 $pdf->setPage($pageposafternote);
413 if (!empty($tplidx)) {
414 $pdf->useTemplate($tplidx);
420 $posyafter = $tab_top_newpage;
424 $tab_height -= $height_note;
425 $tab_top = $posyafter + 6;
434 $pdf->startTransaction();
435 $this->
pdfTabTitles($pdf, $tab_top, $tab_height, $outputlangs, $hidetop);
436 $pdf->rollbackTransaction(
true);
438 $nexY = $tab_top + $this->tabTitleHeight;
441 $pageposbeforeprintlines = $pdf->getPage();
442 $pagenb = $pageposbeforeprintlines;
443 for ($i = 0; $i < $nblines; $i++) {
445 $pdf->SetFont(
'',
'', $default_font_size - 1);
446 $pdf->SetTextColor(0, 0, 0);
448 $pdf->setTopMargin($tab_top_newpage);
449 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext + $heightforinfotot);
450 $pageposbefore = $pdf->getPage();
452 $showpricebeforepagebreak = 1;
457 $pdf->startTransaction();
460 $pageposafter = $pdf->getPage();
462 if ($pageposafter > $pageposbefore) {
463 $pdf->rollbackTransaction(
true);
464 $pdf->setPageOrientation(
'', 1, $heightforfooter);
468 $pageposafter = $pdf->getPage();
469 $posyafter = $pdf->GetY();
471 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
472 if ($i == ($nblines - 1)) {
473 $pdf->AddPage(
'',
'',
true);
474 if (!empty($tplidx)) {
475 $pdf->useTemplate($tplidx);
477 $pdf->setPage($pageposafter + 1);
483 $showpricebeforepagebreak = 1;
485 $showpricebeforepagebreak = 0;
489 $pdf->commitTransaction();
493 $nexY = $pdf->GetY();
494 $pageposafter = $pdf->getPage();
495 $pdf->setPage($pageposbefore);
496 $pdf->setTopMargin($this->marge_haute);
497 $pdf->setPageOrientation(
'', 1, 0);
500 if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
501 $pdf->setPage($pageposafter);
502 $curY = $tab_top_newpage;
505 $pdf->SetFont(
'',
'', $default_font_size - 1);
512 $nexY = max($pdf->GetY(), $nexY);
516 if (!empty(
$object->lines[$i]->array_options)) {
517 foreach (
$object->lines[$i]->array_options as $extrafieldColKey => $extrafieldValue) {
521 $nexY = max($pdf->GetY(), $nexY);
533 'outputlangs' => $outputlangs,
534 'hidedetails' => $hidedetails
536 $reshook = $hookmanager->executeHooks(
'printPDFline', $parameters, $this);
541 $prev_progress =
$object->lines[$i]->get_prev_progress(
$object->id);
542 if ($prev_progress > 0 && !empty(
$object->lines[$i]->situation_percent)) {
543 if (isModEnabled(
"multicurrency") &&
$object->multicurrency_tx != 1) {
544 $tvaligne = $sign *
$object->lines[$i]->multicurrency_total_tva * (
$object->lines[$i]->situation_percent - $prev_progress) /
$object->lines[$i]->situation_percent;
546 $tvaligne = $sign *
$object->lines[$i]->total_tva * (
$object->lines[$i]->situation_percent - $prev_progress) /
$object->lines[$i]->situation_percent;
549 if (isModEnabled(
"multicurrency") &&
$object->multicurrency_tx != 1) {
550 $tvaligne = $sign *
$object->lines[$i]->multicurrency_total_tva;
552 $tvaligne = $sign *
$object->lines[$i]->total_tva;
556 $localtax1ligne =
$object->lines[$i]->total_localtax1;
557 $localtax2ligne =
$object->lines[$i]->total_localtax2;
558 $localtax1_rate =
$object->lines[$i]->localtax1_tx;
559 $localtax2_rate =
$object->lines[$i]->localtax2_tx;
560 $localtax1_type =
$object->lines[$i]->localtax1_type;
561 $localtax2_type =
$object->lines[$i]->localtax2_type;
574 $vatrate = (string)
$object->lines[$i]->tva_tx;
577 if ((!isset($localtax1_type) || $localtax1_type ==
'' || !isset($localtax2_type) || $localtax2_type ==
'')
578 && (!empty($localtax1_rate) || !empty($localtax2_rate))) {
580 $localtax1_type = isset($localtaxtmp_array[0]) ? $localtaxtmp_array[0] :
'';
581 $localtax2_type = isset($localtaxtmp_array[2]) ? $localtaxtmp_array[2] :
'';
585 if ($localtax1_type && $localtax1ligne != 0) {
586 if (empty($this->localtax1[$localtax1_type][$localtax1_rate])) {
587 $this->localtax1[$localtax1_type][$localtax1_rate] = $localtax1ligne;
589 $this->localtax1[$localtax1_type][$localtax1_rate] += $localtax1ligne;
592 if ($localtax2_type && $localtax2ligne != 0) {
593 if (empty($this->localtax2[$localtax2_type][$localtax2_rate])) {
594 $this->localtax2[$localtax2_type][$localtax2_rate] = $localtax2ligne;
596 $this->localtax2[$localtax2_type][$localtax2_rate] += $localtax2ligne;
600 if ((
$object->lines[$i]->info_bits & 0x01) == 0x01) {
603 if (!isset($this->tva[$vatrate])) {
604 $this->tva[$vatrate] = 0;
606 $this->tva[$vatrate] += $tvaligne;
608 $nexY = max($nexY, $posYAfterImage);
612 $pdf->setPage($pageposafter);
613 $pdf->SetLineStyle(array(
'dash' =>
'1,1',
'color' => array(80, 80, 80)));
615 $pdf->line($this->marge_gauche, $nexY, $this->page_largeur - $this->marge_droite, $nexY);
616 $pdf->SetLineStyle(array(
'dash' => 0));
620 while ($pagenb < $pageposafter) {
621 $pdf->setPage($pagenb);
622 if ($pagenb == $pageposbeforeprintlines) {
623 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1,
$object->multicurrency_code, $outputlangsbis);
625 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1,
$object->multicurrency_code, $outputlangsbis);
629 $pdf->setPage($pagenb);
630 $pdf->setPageOrientation(
'', 1, 0);
636 if (isset(
$object->lines[$i + 1]->pagebreak) &&
$object->lines[$i + 1]->pagebreak) {
637 if ($pagenb == $pageposafter) {
638 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1,
$object->multicurrency_code, $outputlangsbis);
640 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1,
$object->multicurrency_code, $outputlangsbis);
645 if (!empty($tplidx)) {
646 $pdf->useTemplate($tplidx);
656 if ($pagenb == $pageposbeforeprintlines) {
657 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, $hidetop, 0,
$object->multicurrency_code, $outputlangsbis);
658 $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
660 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0,
$object->multicurrency_code, $outputlangsbis);
661 $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
680 if (method_exists($pdf,
'AliasNbPages')) {
681 $pdf->AliasNbPages();
686 $pdf->Output($file,
'F');
689 $hookmanager->initHooks(array(
'pdfgeneration'));
690 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
692 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
694 $this->error = $hookmanager->error;
695 $this->errors = $hookmanager->errors;
700 $this->result = array(
'fullpath' => $file);
704 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
708 $this->error = $langs->transnoentities(
"ErrorConstantNotDefined",
"ASSET_OUTPUTDIR");
799 protected function _pagehead(&$pdf,
$object, $showaddress, $outputlangs, $outputlangsbis =
null)
801 global
$conf, $langs;
804 $outputlangs->loadLangs(array(
"main",
"bills",
"propal",
"companies"));
812 pdf_watermark($pdf, $outputlangs, $this->page_hauteur, $this->page_largeur,
'mm',
$conf->global->FACTURE_DRAFT_WATERMARK);
815 $pdf->SetTextColor(0, 0, 60);
816 $pdf->SetFont(
'',
'B', $default_font_size + 3);
820 $posy = $this->marge_haute;
821 $posx = $this->page_largeur - $this->marge_droite - $w;
823 $pdf->SetXY($this->marge_gauche, $posy);
827 if ($this->emetteur->logo) {
828 $logodir =
$conf->mycompany->dir_output;
829 if (!empty(
$conf->mycompany->multidir_output[
$object->entity])) {
830 $logodir =
$conf->mycompany->multidir_output[
$object->entity];
833 $logo = $logodir.
'/logos/thumbs/'.$this->emetteur->logo_small;
835 $logo = $logodir.
'/logos/'.$this->emetteur->logo;
837 if (is_readable($logo)) {
839 $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height);
841 $pdf->SetTextColor(200, 0, 0);
842 $pdf->SetFont(
'',
'B', $default_font_size - 2);
843 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorLogoFileNotFound", $logo), 0,
'L');
844 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorGoToGlobalSetup"), 0,
'L');
847 $text = $this->emetteur->name;
848 $pdf->MultiCell($w, 4, $outputlangs->convToOutputCharset($text), 0,
'L');
852 $pdf->SetFont(
'',
'B', $default_font_size + 3);
853 $pdf->SetXY($posx, $posy);
854 $pdf->SetTextColor(0, 0, 60);
855 $title = $outputlangs->transnoentities(
"PdfTitle");
858 $title .= $outputlangsbis->transnoentities(
"PdfTitle");
860 $pdf->MultiCell($w, 3, $title,
'',
'R');
862 $pdf->SetFont(
'',
'B', $default_font_size);
865 $pdf->SetXY($posx, $posy);
866 $pdf->SetTextColor(0, 0, 60);
867 $textref = $outputlangs->transnoentities(
"Ref").
" : ".$outputlangs->convToOutputCharset(
$object->ref);
868 if (
$object->status == $object::STATUS_DRAFT) {
869 $pdf->SetTextColor(128, 0, 0);
870 $textref .=
' - '.$outputlangs->transnoentities(
"NotValidated");
872 $pdf->MultiCell($w, 4, $textref,
'',
'R');
875 $pdf->SetFont(
'',
'', $default_font_size - 2);
886 if (!empty(
$object->project->ref)) {
888 $pdf->SetXY($posx, $posy);
889 $pdf->SetTextColor(0, 0, 60);
890 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"Project").
" : ".(empty(
$object->project->title) ?
'' :
$object->projet->title),
'',
'R');
896 if (!empty(
$object->project->ref)) {
897 $outputlangs->load(
"projects");
899 $pdf->SetXY($posx, $posy);
900 $pdf->SetTextColor(0, 0, 60);
901 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"RefProject").
" : ".(empty(
$object->project->ref) ?
'' :
$object->project->ref),
'',
'R');
906 $pdf->SetXY($posx, $posy);
907 $pdf->SetTextColor(0, 0, 60);
909 $title = $outputlangs->transnoentities(
"Date");
911 $title .=
' - '.$outputlangsbis->transnoentities(
"Date");
913 $pdf->MultiCell($w, 3, $title.
" : ".
dol_print_date(
$object->date_acquisition,
"day",
false, $outputlangs),
'',
'R');
915 if (
$object->thirdparty->code_client) {
917 $pdf->SetXY($posx, $posy);
918 $pdf->SetTextColor(0, 0, 60);
919 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerCode").
" : ".$outputlangs->transnoentities(
$object->thirdparty->code_client),
'',
'R');
924 $arrayidcontact =
$object->getIdContact(
'internal',
'SALESREPFOLL');
925 if (count($arrayidcontact) > 0) {
926 $usertmp =
new User($this->db);
927 $usertmp->fetch($arrayidcontact[0]);
929 $pdf->SetXY($posx, $posy);
930 $pdf->SetTextColor(0, 0, 60);
931 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"SalesRepresentative").
" : ".$usertmp->getFullName($langs),
'',
'R');
939 $current_y = $pdf->getY();
941 if ($current_y < $pdf->getY()) {
942 $top_shift = $pdf->getY() - $current_y;
952 $posx = $this->marge_gauche;
954 $posx = $this->page_largeur - $this->marge_droite - 80;
962 $pdf->SetTextColor(0, 0, 0);
963 $pdf->SetFont(
'',
'', $default_font_size - 2);
964 $pdf->SetXY($posx, $posy - 5);
965 $pdf->MultiCell(66, 5, $outputlangs->transnoentities(
"BillFrom").
":", 0,
'L');
966 $pdf->SetXY($posx, $posy);
967 $pdf->SetFillColor(230, 230, 230);
968 $pdf->RoundedRect($posx, $posy, $widthrecbox, $hautcadre, $this->corner_radius,
'1234',
'D');
969 $pdf->SetTextColor(0, 0, 60);
972 $pdf->SetXY($posx + 2, $posy + 3);
973 $pdf->SetFont(
'',
'B', $default_font_size);
974 $pdf->MultiCell($widthrecbox - 2, 4, $outputlangs->convToOutputCharset($this->emetteur->name), 0,
'L');
975 $posy = $pdf->getY();
978 $pdf->SetXY($posx + 2, $posy);
979 $pdf->SetFont(
'',
'', $default_font_size - 1);
980 $pdf->MultiCell($widthrecbox - 2, 4, $carac_emetteur, 0,
'L');
984 $arrayidcontact =
$object->getIdContact(
'external',
'BILLING');
985 if (count($arrayidcontact) > 0) {
987 $result =
$object->fetch_contact($arrayidcontact[0]);
992 $thirdparty =
$object->contact;
994 $thirdparty =
$object->thirdparty;
997 if (is_object($thirdparty)) {
1005 if ($this->page_largeur < 210) {
1009 $posy += $top_shift;
1010 $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
1012 $posx = $this->marge_gauche;
1016 $pdf->SetTextColor(0, 0, 0);
1017 $pdf->SetFont(
'',
'', $default_font_size - 2);
1018 $pdf->SetXY($posx + 2, $posy - 5);
1019 $pdf->MultiCell($widthrecbox, 5, $outputlangs->transnoentities(
"BillTo").
":", 0,
'L');
1020 $pdf->RoundedRect($posx, $posy, $widthrecbox, $hautcadre, $this->corner_radius,
'1234',
'D');
1023 $pdf->SetXY($posx + 2, $posy + 3);
1024 $pdf->SetFont(
'',
'B', $default_font_size);
1025 $pdf->MultiCell($widthrecbox, 2, $carac_client_name, 0,
'L');
1027 $posy = $pdf->getY();
1030 $pdf->SetFont(
'',
'', $default_font_size - 1);
1031 $pdf->SetXY($posx + 2, $posy);
1032 $pdf->MultiCell($widthrecbox, 4, $carac_client, 0,
'L');
1035 $pdf->SetTextColor(0, 0, 0);