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;
569 if ((!isset($localtax1_type) || $localtax1_type ==
'' || !isset($localtax2_type) || $localtax2_type ==
'')
570 && (!empty($localtax1_rate) || !empty($localtax2_rate))) {
572 $localtax1_type = isset($localtaxtmp_array[0]) ? $localtaxtmp_array[0] :
'';
573 $localtax2_type = isset($localtaxtmp_array[2]) ? $localtaxtmp_array[2] :
'';
577 if ($localtax1_type && $localtax1ligne != 0) {
578 if (empty($this->localtax1[$localtax1_type][$localtax1_rate])) {
579 $this->localtax1[$localtax1_type][$localtax1_rate] = $localtax1ligne;
581 $this->localtax1[$localtax1_type][$localtax1_rate] += $localtax1ligne;
584 if ($localtax2_type && $localtax2ligne != 0) {
585 if (empty($this->localtax2[$localtax2_type][$localtax2_rate])) {
586 $this->localtax2[$localtax2_type][$localtax2_rate] = $localtax2ligne;
588 $this->localtax2[$localtax2_type][$localtax2_rate] += $localtax2ligne;
592 if (((
int)
$object->lines[$i]->info_bits & 0x01) == 0x01) {
595 if (!isset($this->tva[$vatrate])) {
596 $this->tva[$vatrate] = 0;
598 $this->tva[$vatrate] += $tvaligne;
600 $nexY = max($nexY, $posYAfterImage);
604 $pdf->setPage($pageposafter);
605 $pdf->SetLineStyle(array(
'dash' =>
'1,1',
'color' => array(80, 80, 80)));
607 $pdf->line($this->marge_gauche, $nexY, $this->page_largeur - $this->marge_droite, $nexY);
608 $pdf->SetLineStyle(array(
'dash' => 0));
612 while ($pagenb < $pageposafter) {
613 $pdf->setPage($pagenb);
614 if ($pagenb == $pageposbeforeprintlines) {
615 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1,
$object->multicurrency_code, $outputlangsbis);
617 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1,
$object->multicurrency_code, $outputlangsbis);
621 $pdf->setPage($pagenb);
622 $pdf->setPageOrientation(
'',
true, 0);
628 if (isset(
$object->lines[$i + 1]->pagebreak) &&
$object->lines[$i + 1]->pagebreak) {
629 if ($pagenb == $pageposafter) {
630 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1,
$object->multicurrency_code, $outputlangsbis);
632 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1,
$object->multicurrency_code, $outputlangsbis);
637 if (!empty($tplidx)) {
638 $pdf->useTemplate($tplidx);
648 if ($pagenb == $pageposbeforeprintlines) {
649 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, $hidetop, 0,
$object->multicurrency_code, $outputlangsbis);
650 $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
652 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0,
$object->multicurrency_code, $outputlangsbis);
653 $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
672 if (method_exists($pdf,
'AliasNbPages')) {
673 $pdf->AliasNbPages();
678 $pdf->Output($file,
'F');
681 $hookmanager->initHooks(array(
'pdfgeneration'));
682 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
684 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
685 $this->warnings = $hookmanager->warnings;
687 $this->error = $hookmanager->error;
688 $this->errors = $hookmanager->errors;
695 $this->result = array(
'fullpath' => $file);
699 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
703 $this->error = $langs->transnoentities(
"ErrorConstantNotDefined",
"ASSET_OUTPUTDIR");
794 protected function _pagehead(&$pdf,
$object, $showaddress, $outputlangs, $outputlangsbis =
null)
796 global
$conf, $langs;
799 $outputlangs->loadLangs(array(
"main",
"bills",
"propal",
"companies"));
807 pdf_watermark($pdf, $outputlangs, $this->page_hauteur, $this->page_largeur,
'mm',
$conf->global->FACTURE_DRAFT_WATERMARK);
810 $pdf->SetTextColor(0, 0, 60);
811 $pdf->SetFont(
'',
'B', $default_font_size + 3);
815 $posy = $this->marge_haute;
816 $posx = $this->page_largeur - $this->marge_droite - $w;
818 $pdf->SetXY($this->marge_gauche, $posy);
821 $logodir =
$conf->mycompany->dir_output;
822 if (!empty(
$conf->mycompany->multidir_output[
$object->entity ??
$conf->entity])) {
825 pdf_writeLogoOrCompanyName($pdf, $outputlangs, $this->emetteur, $logodir, $this->marge_gauche, $posy, $w, $default_font_size,
'L');
827 $pdf->SetFont(
'',
'B', $default_font_size + 3);
828 $pdf->SetXY($posx, $posy);
829 $pdf->SetTextColor(0, 0, 60);
830 $title = $outputlangs->transnoentities(
"PdfTitle");
833 $title .= $outputlangsbis->transnoentities(
"PdfTitle");
835 $pdf->MultiCell($w, 3, $title,
'',
'R');
837 $pdf->SetFont(
'',
'B', $default_font_size);
840 $pdf->SetXY($posx, $posy);
841 $pdf->SetTextColor(0, 0, 60);
842 $textref = $outputlangs->transnoentities(
"Ref").
" : ".$outputlangs->convToOutputCharset(
$object->ref);
843 if (
$object->status == $object::STATUS_DRAFT) {
844 $pdf->SetTextColor(128, 0, 0);
845 $textref .=
' - '.$outputlangs->transnoentities(
"NotValidated");
847 $pdf->MultiCell($w, 4, $textref,
'',
'R');
850 $pdf->SetFont(
'',
'', $default_font_size - 2);
861 if (!empty(
$object->project->ref)) {
863 $pdf->SetXY($posx, $posy);
864 $pdf->SetTextColor(0, 0, 60);
865 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"Project").
" : ".(empty(
$object->project->title) ?
'' :
$object->project->title),
'',
'R');
871 if (!empty(
$object->project->ref)) {
872 $outputlangs->load(
"projects");
874 $pdf->SetXY($posx, $posy);
875 $pdf->SetTextColor(0, 0, 60);
876 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"RefProject").
" : ".(empty(
$object->project->ref) ?
'' :
$object->project->ref),
'',
'R');
881 $pdf->SetXY($posx, $posy);
882 $pdf->SetTextColor(0, 0, 60);
884 $title = $outputlangs->transnoentities(
"Date");
886 $title .=
' - '.$outputlangsbis->transnoentities(
"Date");
888 $pdf->MultiCell($w, 3, $title.
" : ".
dol_print_date(
$object->date_acquisition,
"day",
false, $outputlangs),
'',
'R');
890 if (
$object->thirdparty->code_client) {
892 $pdf->SetXY($posx, $posy);
893 $pdf->SetTextColor(0, 0, 60);
894 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerCode").
" : ".$outputlangs->transnoentities((
string)
$object->thirdparty->code_client),
'',
'R');
899 $arrayidcontact =
$object->getIdContact(
'internal',
'SALESREPFOLL');
900 if (count($arrayidcontact) > 0) {
901 $usertmp =
new User($this->db);
902 $usertmp->fetch($arrayidcontact[0]);
904 $pdf->SetXY($posx, $posy);
905 $pdf->SetTextColor(0, 0, 60);
906 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"SalesRepresentative").
" : ".$usertmp->getFullName($langs),
'',
'R');
914 $current_y = $pdf->getY();
916 if ($current_y < $pdf->getY()) {
917 $top_shift = $pdf->getY() - $current_y;
927 $posx = $this->marge_gauche;
929 $posx = $this->page_largeur - $this->marge_droite - 80;
937 $pdf->SetTextColor(0, 0, 0);
938 $pdf->SetFont(
'',
'', $default_font_size - 2);
939 $pdf->SetXY($posx, $posy - 5);
940 $pdf->MultiCell(66, 5, $outputlangs->transnoentities(
"BillFrom").
":", 0,
'L');
941 $pdf->SetXY($posx, $posy);
942 $pdf->SetFillColor(230, 230, 230);
943 $pdf->RoundedRect($posx, $posy, $widthrecbox, $hautcadre, $this->corner_radius,
'1234',
'D');
944 $pdf->SetTextColor(0, 0, 60);
947 $pdf->SetXY($posx + 2, $posy + 3);
948 $pdf->SetFont(
'',
'B', $default_font_size);
949 $pdf->MultiCell($widthrecbox - 2, 4, $outputlangs->convToOutputCharset((
string) $this->emetteur->name), 0,
'L');
950 $posy = $pdf->getY();
953 $pdf->SetXY($posx + 2, $posy);
954 $pdf->SetFont(
'',
'', $default_font_size - 1);
955 $pdf->MultiCell($widthrecbox - 2, 4, $carac_emetteur, 0,
'L');
959 $arrayidcontact =
$object->getIdContact(
'external',
'BILLING');
960 if (count($arrayidcontact) > 0) {
962 $result =
$object->fetch_contact($arrayidcontact[0]);
967 $thirdparty =
$object->contact;
969 $thirdparty =
$object->thirdparty;
972 if (is_object($thirdparty)) {
975 $carac_client_name =
'';
982 if ($this->page_largeur < 210) {
987 $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
989 $posx = $this->marge_gauche;
993 $pdf->SetTextColor(0, 0, 0);
994 $pdf->SetFont(
'',
'', $default_font_size - 2);
995 $pdf->SetXY($posx + 2, $posy - 5);
996 $pdf->MultiCell($widthrecbox, 5, $outputlangs->transnoentities(
"BillTo").
":", 0,
'L');
997 $pdf->RoundedRect($posx, $posy, $widthrecbox, $hautcadre, $this->corner_radius,
'1234',
'D');
1000 $pdf->SetXY($posx + 2, $posy + 3);
1001 $pdf->SetFont(
'',
'B', $default_font_size);
1002 $pdf->MultiCell($widthrecbox, 2, $carac_client_name, 0,
'L');
1004 $posy = $pdf->getY();
1007 $pdf->SetFont(
'',
'', $default_font_size - 1);
1008 $pdf->SetXY($posx + 2, $posy);
1009 $pdf->MultiCell($widthrecbox, 4, $carac_client, 0,
'L');
1012 $pdf->SetTextColor(0, 0, 0);