152 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
155 global $user, $langs, $conf, $mysoc, $db, $hookmanager, $nblines;
157 dol_syslog(
"write_file outputlangs->defaultlang=".(is_object($outputlangs) ? $outputlangs->defaultlang :
'null'));
159 if (!is_object($outputlangs)) {
160 $outputlangs = $langs;
164 $outputlangs->charset_output =
'ISO-8859-1';
168 $outputlangs->loadLangs(array(
"main",
"bills",
"products",
"dict",
"companies"));
171 global $outputlangsbis;
172 $outputlangsbis =
new Translate(
'', $conf);
174 $outputlangsbis->loadLangs(array(
"main",
"bills",
"products",
"dict",
"companies"));
177 $nblines = (is_array(
$object->lines) ? count(
$object->lines) : 0);
184 if ($conf->asset->dir_output.
'/asset') {
189 $dir = $conf->asset->dir_output.
'/asset';
190 $file = $dir.
"/SPECIMEN.pdf";
193 $dir = $conf->asset->dir_output.
'/asset/'.$objectref;
194 $file = $dir.
"/".$objectref.
".pdf";
196 if (!file_exists($dir)) {
198 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
203 if (file_exists($dir)) {
205 if (!is_object($hookmanager)) {
206 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
209 $hookmanager->initHooks(array(
'pdfgeneration'));
210 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
212 $reshook = $hookmanager->executeHooks(
'beforePDFCreation', $parameters,
$object, $action);
215 $nblines = (is_array(
$object->lines) ? count(
$object->lines) : 0);
220 $pdf->SetAutoPageBreak(1, 0);
222 $heightforinfotot = 50;
224 $heightforfooter = $this->marge_basse + (!
getDolGlobalString(
'MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS') ? 12 : 22);
226 if (class_exists(
'TCPDF')) {
227 $pdf->setPrintHeader(
false);
228 $pdf->setPrintFooter(
false);
234 $pagecount = $pdf->setSourceFile($conf->mycompany->multidir_output[
$object->entity].
'/' .
getDolGlobalString(
'MAIN_ADD_PDF_BACKGROUND'));
235 $tplidx = $pdf->importPage(1);
240 $pdf->SetDrawColor(128, 128, 128);
242 $pdf->SetTitle($outputlangs->convToOutputCharset(
$object->ref));
243 $pdf->SetSubject($outputlangs->transnoentities(
"PdfTitle"));
244 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
245 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
246 $pdf->SetKeyWords($outputlangs->convToOutputCharset(
$object->ref).
" ".$outputlangs->transnoentities(
"PdfTitle").
" ".$outputlangs->convToOutputCharset(
$object->thirdparty->name));
248 $pdf->SetCompression(
false);
252 $cert = empty($user->conf->CERTIFICATE_CRT) ?
'' : $user->conf->CERTIFICATE_CRT;
260 'Name' => $this->emetteur->name,
261 'Location' =>
getCountry($this->emetteur->country_code,
''),
263 'ContactInfo' => $this->emetteur->email
265 $pdf->setSignature($cert, $cert, $this->emetteur->name,
'', 2, $info);
269 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
273 if (!empty($tplidx)) {
274 $pdf->useTemplate($tplidx);
278 $top_shift = $this->
_pagehead($pdf,
$object, 1, $outputlangs, $outputlangsbis);
279 $pdf->SetFont(
'',
'', $default_font_size - 1);
280 $pdf->MultiCell(0, 3,
'');
281 $pdf->SetTextColor(0, 0, 0);
283 $tab_top = 90 + $top_shift;
284 $tab_top_newpage = (!
getDolGlobalInt(
'MAIN_PDF_DONOTREPEAT_HEAD') ? 42 + $top_shift : 10);
285 $tab_height = 130 - $top_shift;
286 $tab_height_newpage = 150;
288 $tab_height_newpage -= $top_shift;
291 $nexY = $tab_top - 1;
294 $notetoshow = empty(
$object->note_public) ?
'' :
$object->note_public;
296 $extranote = $this->getExtrafieldsInHtml(
$object, $outputlangs);
297 if (!empty($extranote)) {
301 $pagenb = $pdf->getPage();
305 $tab_width = $this->page_largeur - $this->marge_gauche - $this->marge_droite;
306 $pageposbeforenote = $pagenb;
313 $pdf->startTransaction();
315 $pdf->SetFont(
'',
'', $default_font_size - 1);
316 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top,
dol_htmlentitiesbr($notetoshow), 0, 1);
318 $pageposafternote = $pdf->getPage();
319 $posyafter = $pdf->GetY();
321 if ($pageposafternote > $pageposbeforenote) {
322 $pdf->rollbackTransaction(
true);
325 while ($pagenb < $pageposafternote) {
328 if (!empty($tplidx)) {
329 $pdf->useTemplate($tplidx);
335 $pdf->setTopMargin($tab_top_newpage);
337 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext);
341 $pdf->setPage($pageposbeforenote);
342 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext);
343 $pdf->SetFont(
'',
'', $default_font_size - 1);
344 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top,
dol_htmlentitiesbr($notetoshow), 0, 1);
345 $pageposafternote = $pdf->getPage();
347 $posyafter = $pdf->GetY();
349 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) {
350 $pdf->AddPage(
'',
'',
true);
353 $pdf->setPage($pageposafternote);
354 $pdf->setTopMargin($tab_top_newpage);
356 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext);
362 $i = $pageposbeforenote;
363 while ($i < $pageposafternote) {
367 $pdf->SetDrawColor(128, 128, 128);
369 if ($i > $pageposbeforenote) {
370 $height_note = $this->page_hauteur - ($tab_top_newpage + $heightforfooter);
371 $pdf->Rect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1);
373 $height_note = $this->page_hauteur - ($tab_top + $heightforfooter);
374 $pdf->Rect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1);
378 $pdf->setPageOrientation(
'', 1, 0);
385 $pdf->setPage($pageposafternote);
386 if (!empty($tplidx)) {
387 $pdf->useTemplate($tplidx);
392 $height_note = $posyafter - $tab_top_newpage;
393 $pdf->Rect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1);
395 $pdf->commitTransaction();
396 $posyafter = $pdf->GetY();
397 $height_note = $posyafter - $tab_top;
398 $pdf->Rect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1);
401 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) {
403 $pdf->AddPage(
'',
'',
true);
406 $pdf->setPage($pageposafternote);
407 if (!empty($tplidx)) {
408 $pdf->useTemplate($tplidx);
414 $posyafter = $tab_top_newpage;
418 $tab_height -= $height_note;
419 $tab_top = $posyafter + 6;
428 $pdf->startTransaction();
429 $this->
pdfTabTitles($pdf, $tab_top, $tab_height, $outputlangs, $hidetop);
430 $pdf->rollbackTransaction(
true);
432 $nexY = $tab_top + $this->tabTitleHeight;
435 $pageposbeforeprintlines = $pdf->getPage();
436 $pagenb = $pageposbeforeprintlines;
437 for ($i = 0; $i < $nblines; $i++) {
439 $pdf->SetFont(
'',
'', $default_font_size - 1);
440 $pdf->SetTextColor(0, 0, 0);
442 $pdf->setTopMargin($tab_top_newpage);
443 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext + $heightforinfotot);
444 $pageposbefore = $pdf->getPage();
446 $showpricebeforepagebreak = 1;
451 $pdf->startTransaction();
454 $pageposafter = $pdf->getPage();
456 if ($pageposafter > $pageposbefore) {
457 $pdf->rollbackTransaction(
true);
458 $pdf->setPageOrientation(
'', 1, $heightforfooter);
462 $pageposafter = $pdf->getPage();
463 $posyafter = $pdf->GetY();
465 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
466 if ($i == ($nblines - 1)) {
467 $pdf->AddPage(
'',
'',
true);
468 if (!empty($tplidx)) {
469 $pdf->useTemplate($tplidx);
471 $pdf->setPage($pageposafter + 1);
477 $showpricebeforepagebreak = 1;
479 $showpricebeforepagebreak = 0;
483 $pdf->commitTransaction();
487 $nexY = $pdf->GetY();
488 $pageposafter = $pdf->getPage();
489 $pdf->setPage($pageposbefore);
490 $pdf->setTopMargin($this->marge_haute);
491 $pdf->setPageOrientation(
'', 1, 0);
494 if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
495 $pdf->setPage($pageposafter);
496 $curY = $tab_top_newpage;
499 $pdf->SetFont(
'',
'', $default_font_size - 1);
506 $nexY = max($pdf->GetY(), $nexY);
510 if (!empty(
$object->lines[$i]->array_options)) {
511 foreach (
$object->lines[$i]->array_options as $extrafieldColKey => $extrafieldValue) {
515 $nexY = max($pdf->GetY(), $nexY);
527 'outputlangs' => $outputlangs,
528 'hidedetails' => $hidedetails
530 $reshook = $hookmanager->executeHooks(
'printPDFline', $parameters, $this);
535 $prev_progress =
$object->lines[$i]->get_prev_progress(
$object->id);
536 if ($prev_progress > 0 && !empty(
$object->lines[$i]->situation_percent)) {
537 if (isModEnabled(
"multicurrency") &&
$object->multicurrency_tx != 1) {
538 $tvaligne = $sign *
$object->lines[$i]->multicurrency_total_tva * (
$object->lines[$i]->situation_percent - $prev_progress) /
$object->lines[$i]->situation_percent;
540 $tvaligne = $sign *
$object->lines[$i]->total_tva * (
$object->lines[$i]->situation_percent - $prev_progress) /
$object->lines[$i]->situation_percent;
543 if (isModEnabled(
"multicurrency") &&
$object->multicurrency_tx != 1) {
544 $tvaligne = $sign *
$object->lines[$i]->multicurrency_total_tva;
546 $tvaligne = $sign *
$object->lines[$i]->total_tva;
550 $localtax1ligne =
$object->lines[$i]->total_localtax1;
551 $localtax2ligne =
$object->lines[$i]->total_localtax2;
552 $localtax1_rate =
$object->lines[$i]->localtax1_tx;
553 $localtax2_rate =
$object->lines[$i]->localtax2_tx;
554 $localtax1_type =
$object->lines[$i]->localtax1_type;
555 $localtax2_type =
$object->lines[$i]->localtax2_type;
568 $vatrate = (string)
$object->lines[$i]->tva_tx;
571 if ((!isset($localtax1_type) || $localtax1_type ==
'' || !isset($localtax2_type) || $localtax2_type ==
'')
572 && (!empty($localtax1_rate) || !empty($localtax2_rate))) {
574 $localtax1_type = isset($localtaxtmp_array[0]) ? $localtaxtmp_array[0] :
'';
575 $localtax2_type = isset($localtaxtmp_array[2]) ? $localtaxtmp_array[2] :
'';
579 if ($localtax1_type && $localtax1ligne != 0) {
580 if (empty($this->localtax1[$localtax1_type][$localtax1_rate])) {
581 $this->localtax1[$localtax1_type][$localtax1_rate] = $localtax1ligne;
583 $this->localtax1[$localtax1_type][$localtax1_rate] += $localtax1ligne;
586 if ($localtax2_type && $localtax2ligne != 0) {
587 if (empty($this->localtax2[$localtax2_type][$localtax2_rate])) {
588 $this->localtax2[$localtax2_type][$localtax2_rate] = $localtax2ligne;
590 $this->localtax2[$localtax2_type][$localtax2_rate] += $localtax2ligne;
594 if ((
$object->lines[$i]->info_bits & 0x01) == 0x01) {
597 if (!isset($this->tva[$vatrate])) {
598 $this->tva[$vatrate] = 0;
600 $this->tva[$vatrate] += $tvaligne;
602 $nexY = max($nexY, $posYAfterImage);
606 $pdf->setPage($pageposafter);
607 $pdf->SetLineStyle(array(
'dash' =>
'1,1',
'color' => array(80, 80, 80)));
609 $pdf->line($this->marge_gauche, $nexY, $this->page_largeur - $this->marge_droite, $nexY);
610 $pdf->SetLineStyle(array(
'dash' => 0));
614 while ($pagenb < $pageposafter) {
615 $pdf->setPage($pagenb);
616 if ($pagenb == $pageposbeforeprintlines) {
617 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1,
$object->multicurrency_code, $outputlangsbis);
619 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1,
$object->multicurrency_code, $outputlangsbis);
623 $pdf->setPage($pagenb);
624 $pdf->setPageOrientation(
'', 1, 0);
630 if (isset(
$object->lines[$i + 1]->pagebreak) &&
$object->lines[$i + 1]->pagebreak) {
631 if ($pagenb == $pageposafter) {
632 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1,
$object->multicurrency_code, $outputlangsbis);
634 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1,
$object->multicurrency_code, $outputlangsbis);
639 if (!empty($tplidx)) {
640 $pdf->useTemplate($tplidx);
650 if ($pagenb == $pageposbeforeprintlines) {
651 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, $hidetop, 0,
$object->multicurrency_code, $outputlangsbis);
652 $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
654 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0,
$object->multicurrency_code, $outputlangsbis);
655 $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
674 if (method_exists($pdf,
'AliasNbPages')) {
675 $pdf->AliasNbPages();
680 $pdf->Output($file,
'F');
683 $hookmanager->initHooks(array(
'pdfgeneration'));
684 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
686 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
688 $this->error = $hookmanager->error;
689 $this->errors = $hookmanager->errors;
694 $this->result = array(
'fullpath' => $file);
698 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
702 $this->error = $langs->transnoentities(
"ErrorConstantNotDefined",
"ASSET_OUTPUTDIR");
793 protected function _pagehead(&$pdf,
$object, $showaddress, $outputlangs, $outputlangsbis =
null)
795 global $conf, $langs;
798 $outputlangs->loadLangs(array(
"main",
"bills",
"propal",
"companies"));
806 pdf_watermark($pdf, $outputlangs, $this->page_hauteur, $this->page_largeur,
'mm', $conf->global->FACTURE_DRAFT_WATERMARK);
809 $pdf->SetTextColor(0, 0, 60);
810 $pdf->SetFont(
'',
'B', $default_font_size + 3);
814 $posy = $this->marge_haute;
815 $posx = $this->page_largeur - $this->marge_droite - $w;
817 $pdf->SetXY($this->marge_gauche, $posy);
821 if ($this->emetteur->logo) {
822 $logodir = $conf->mycompany->dir_output;
823 if (!empty($conf->mycompany->multidir_output[
$object->entity])) {
824 $logodir = $conf->mycompany->multidir_output[
$object->entity];
827 $logo = $logodir.
'/logos/thumbs/'.$this->emetteur->logo_small;
829 $logo = $logodir.
'/logos/'.$this->emetteur->logo;
831 if (is_readable($logo)) {
833 $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height);
835 $pdf->SetTextColor(200, 0, 0);
836 $pdf->SetFont(
'',
'B', $default_font_size - 2);
837 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorLogoFileNotFound", $logo), 0,
'L');
838 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorGoToGlobalSetup"), 0,
'L');
841 $text = $this->emetteur->name;
842 $pdf->MultiCell($w, 4, $outputlangs->convToOutputCharset($text), 0,
'L');
846 $pdf->SetFont(
'',
'B', $default_font_size + 3);
847 $pdf->SetXY($posx, $posy);
848 $pdf->SetTextColor(0, 0, 60);
849 $title = $outputlangs->transnoentities(
"PdfTitle");
852 $title .= $outputlangsbis->transnoentities(
"PdfTitle");
854 $pdf->MultiCell($w, 3, $title,
'',
'R');
856 $pdf->SetFont(
'',
'B', $default_font_size);
859 $pdf->SetXY($posx, $posy);
860 $pdf->SetTextColor(0, 0, 60);
861 $textref = $outputlangs->transnoentities(
"Ref").
" : ".$outputlangs->convToOutputCharset(
$object->ref);
862 if (
$object->status == $object::STATUS_DRAFT) {
863 $pdf->SetTextColor(128, 0, 0);
864 $textref .=
' - '.$outputlangs->transnoentities(
"NotValidated");
866 $pdf->MultiCell($w, 4, $textref,
'',
'R');
869 $pdf->SetFont(
'',
'', $default_font_size - 2);
880 if (!empty(
$object->project->ref)) {
882 $pdf->SetXY($posx, $posy);
883 $pdf->SetTextColor(0, 0, 60);
884 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"Project").
" : ".(empty(
$object->project->title) ?
'' :
$object->projet->title),
'',
'R');
890 if (!empty(
$object->project->ref)) {
891 $outputlangs->load(
"projects");
893 $pdf->SetXY($posx, $posy);
894 $pdf->SetTextColor(0, 0, 60);
895 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"RefProject").
" : ".(empty(
$object->project->ref) ?
'' :
$object->project->ref),
'',
'R');
900 $pdf->SetXY($posx, $posy);
901 $pdf->SetTextColor(0, 0, 60);
903 $title = $outputlangs->transnoentities(
"Date");
905 $title .=
' - '.$outputlangsbis->transnoentities(
"Date");
907 $pdf->MultiCell($w, 3, $title.
" : ".
dol_print_date(
$object->date_acquisition,
"day",
false, $outputlangs),
'',
'R');
909 if (
$object->thirdparty->code_client) {
911 $pdf->SetXY($posx, $posy);
912 $pdf->SetTextColor(0, 0, 60);
913 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerCode").
" : ".$outputlangs->transnoentities(
$object->thirdparty->code_client),
'',
'R');
918 $arrayidcontact =
$object->getIdContact(
'internal',
'SALESREPFOLL');
919 if (count($arrayidcontact) > 0) {
920 $usertmp =
new User($this->db);
921 $usertmp->fetch($arrayidcontact[0]);
923 $pdf->SetXY($posx, $posy);
924 $pdf->SetTextColor(0, 0, 60);
925 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"SalesRepresentative").
" : ".$usertmp->getFullName($langs),
'',
'R');
933 $current_y = $pdf->getY();
935 if ($current_y < $pdf->getY()) {
936 $top_shift = $pdf->getY() - $current_y;
946 $posx = $this->marge_gauche;
948 $posx = $this->page_largeur - $this->marge_droite - 80;
956 $pdf->SetTextColor(0, 0, 0);
957 $pdf->SetFont(
'',
'', $default_font_size - 2);
958 $pdf->SetXY($posx, $posy - 5);
959 $pdf->MultiCell(66, 5, $outputlangs->transnoentities(
"BillFrom").
":", 0,
'L');
960 $pdf->SetXY($posx, $posy);
961 $pdf->SetFillColor(230, 230, 230);
962 $pdf->MultiCell($widthrecbox, $hautcadre,
"", 0,
'R', 1);
963 $pdf->SetTextColor(0, 0, 60);
966 $pdf->SetXY($posx + 2, $posy + 3);
967 $pdf->SetFont(
'',
'B', $default_font_size);
968 $pdf->MultiCell($widthrecbox - 2, 4, $outputlangs->convToOutputCharset($this->emetteur->name), 0,
'L');
969 $posy = $pdf->getY();
972 $pdf->SetXY($posx + 2, $posy);
973 $pdf->SetFont(
'',
'', $default_font_size - 1);
974 $pdf->MultiCell($widthrecbox - 2, 4, $carac_emetteur, 0,
'L');
978 $arrayidcontact =
$object->getIdContact(
'external',
'BILLING');
979 if (count($arrayidcontact) > 0) {
981 $result =
$object->fetch_contact($arrayidcontact[0]);
985 if (
$object->contact->socid !=
$object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
986 $thirdparty =
$object->contact;
988 $thirdparty =
$object->thirdparty;
991 if (is_object($thirdparty)) {
999 if ($this->page_largeur < 210) {
1003 $posy += $top_shift;
1004 $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
1006 $posx = $this->marge_gauche;
1010 $pdf->SetTextColor(0, 0, 0);
1011 $pdf->SetFont(
'',
'', $default_font_size - 2);
1012 $pdf->SetXY($posx + 2, $posy - 5);
1013 $pdf->MultiCell($widthrecbox, 5, $outputlangs->transnoentities(
"BillTo").
":", 0,
'L');
1014 $pdf->Rect($posx, $posy, $widthrecbox, $hautcadre);
1017 $pdf->SetXY($posx + 2, $posy + 3);
1018 $pdf->SetFont(
'',
'B', $default_font_size);
1019 $pdf->MultiCell($widthrecbox, 2, $carac_client_name, 0,
'L');
1021 $posy = $pdf->getY();
1024 $pdf->SetFont(
'',
'', $default_font_size - 1);
1025 $pdf->SetXY($posx + 2, $posy);
1026 $pdf->MultiCell($widthrecbox, 4, $carac_client, 0,
'L');
1029 $pdf->SetTextColor(0, 0, 0);