182 public function write_file($object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
185 global $user, $langs, $conf, $mysoc, $db, $hookmanager, $nblines;
187 if (!is_object($outputlangs)) {
188 $outputlangs = $langs;
191 if (!empty($conf->global->MAIN_USE_FPDF)) {
192 $outputlangs->charset_output =
'ISO-8859-1';
196 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"contracts"));
199 if ($object->statut == $object::STATUS_DRAFT && (!empty($conf->global->CONTRACT_DRAFT_WATERMARK))) {
200 $this->watermark = $conf->global->CONTRACT_DRAFT_WATERMARK;
203 global $outputlangsbis;
204 $outputlangsbis =
null;
205 if (!empty($conf->global->PDF_USE_ALSO_LANGUAGE_CODE) && $outputlangs->defaultlang != $conf->global->PDF_USE_ALSO_LANGUAGE_CODE) {
206 $outputlangsbis =
new Translate(
'', $conf);
207 $outputlangsbis->setDefaultLang($conf->global->PDF_USE_ALSO_LANGUAGE_CODE);
208 $outputlangsbis->loadLangs(array(
"main",
"dict",
"companies",
"bills",
"products",
"orders",
"deliveries"));
211 $nblines = count($object->lines);
213 if ($conf->contract->multidir_output[$conf->entity]) {
214 $object->fetch_thirdparty();
217 if ($object->specimen) {
218 $dir = $conf->contract->multidir_output[$conf->entity];
219 $file = $dir.
"/SPECIMEN.pdf";
222 $dir = $conf->contrat->multidir_output[$object->entity].
"/".$objectref;
223 $file = $dir.
"/".$objectref.
".pdf";
226 if (!file_exists($dir)) {
228 $this->error = $langs->trans(
"ErrorCanNotCreateDir", $dir);
233 if (file_exists($dir)) {
235 if (!is_object($hookmanager)) {
236 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
239 $hookmanager->initHooks(array(
'pdfgeneration'));
240 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
242 $reshook = $hookmanager->executeHooks(
'beforePDFCreation', $parameters, $object, $action);
245 $nblines = count($object->lines);
250 $pdf->SetAutoPageBreak(1, 0);
252 $heightforinfotot = 50;
253 $heightforfreetext = (isset($conf->global->MAIN_PDF_FREETEXT_HEIGHT) ? $conf->global->MAIN_PDF_FREETEXT_HEIGHT : 5);
254 $heightforfooter = $this->marge_basse + 9;
255 if (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS)) {
256 $heightforfooter += 6;
259 if (class_exists(
'TCPDF')) {
260 $pdf->setPrintHeader(
false);
261 $pdf->setPrintFooter(
false);
265 if (!empty($conf->global->MAIN_ADD_PDF_BACKGROUND)) {
266 $logodir = $conf->mycompany->dir_output;
267 if (!empty($conf->mycompany->multidir_output[$object->entity])) {
268 $logodir = $conf->mycompany->multidir_output[$object->entity];
270 $pagecount = $pdf->setSourceFile($logodir.
'/'.$conf->global->MAIN_ADD_PDF_BACKGROUND);
271 $tplidx = $pdf->importPage(1);
276 $pdf->SetDrawColor(128, 128, 128);
278 $pdf->SetTitle($outputlangs->convToOutputCharset($object->ref));
279 $pdf->SetSubject($outputlangs->transnoentities(
"Contract"));
280 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
281 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
282 $pdf->SetKeyWords($outputlangs->convToOutputCharset($object->ref).
" ".$outputlangs->transnoentities(
"Contract").
" ".$outputlangs->convToOutputCharset($object->thirdparty->name));
284 $pdf->SetCompression(
false);
287 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
291 if (!empty($tplidx)) {
292 $pdf->useTemplate($tplidx);
295 $top_shift = $this->
_pagehead($pdf, $object, 1, $outputlangs, (is_object($outputlangsbis) ? $outputlangsbis :
null));
296 $pdf->SetFont(
'',
'', $default_font_size - 1);
297 $pdf->MultiCell(0, 3,
'');
298 $pdf->SetTextColor(0, 0, 0);
301 $tab_top_newpage = (!
getDolGlobalInt(
'MAIN_PDF_DONOTREPEAT_HEAD') ? 42 : 10);
304 $notetoshow = empty($object->note_public) ?
'' : $object->note_public;
307 $extranote = $this->getExtrafieldsInHtml($object, $outputlangs);
308 if (!empty($extranote)) {
312 if (!empty($notetoshow)) {
320 $pdf->SetFont(
'',
'', $default_font_size - 1);
321 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top - 1,
dol_htmlentitiesbr($notetoshow), 0, 1);
322 $nexY = $pdf->GetY();
323 $height_note = $nexY - $tab_top;
326 $pdf->SetDrawColor(192, 192, 192);
327 $pdf->Rect($this->marge_gauche, $tab_top - 1, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_note + 1);
329 $tab_top = $nexY + 6;
332 $iniY = $tab_top + 7;
333 $curY = $tab_top + 7;
334 $nexY = $tab_top + 2;
336 $pdf->SetXY($this->marge_gauche, $tab_top);
338 $pdf->MultiCell(0, 2,
'');
340 $nblines = count($object->lines);
343 for ($i = 0; $i < $nblines; $i++) {
344 $objectligne = $object->lines[$i];
346 $valide = $objectligne->id ? 1 : 0;
348 if ($valide > 0 || $object->specimen) {
349 $curX = $this->posxdesc - 1;
351 $pdf->SetFont(
'',
'', $default_font_size - 1);
352 $pdf->SetTextColor(0, 0, 0);
354 $pdf->setTopMargin($tab_top_newpage);
355 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext + $heightforinfotot);
356 $pageposbefore = $pdf->getPage();
360 if (!empty($objectligne->date_start)) {
361 $datei =
dol_print_date((
int) $objectligne->date_start,
'day',
false, $outputlangs,
true);
363 $datei = $langs->trans(
"Unknown");
366 if (!empty($objectligne->date_end)) {
367 $durationi =
convertSecondToTime((
int) $objectligne->date_end - (
int) $objectligne->date_start,
'allwithouthour');
368 $datee =
dol_print_date($objectligne->date_end,
'day',
false, $outputlangs,
true);
370 $durationi = $langs->trans(
"Unknown");
371 $datee = $langs->trans(
"Unknown");
374 if (!empty($objectligne->date_start_real)) {
375 $daters =
dol_print_date((
int) $objectligne->date_start_real,
'day',
false, $outputlangs,
true);
377 $daters = $langs->trans(
"Unknown");
380 if (!empty($objectligne->date_end_real)) {
381 $datere =
dol_print_date((
int) $objectligne->date_end_real,
'day',
false, $outputlangs,
true);
383 $datere = $langs->trans(
"Unknown");
386 $txtpredefinedservice = $objectligne->product_ref;
387 if ($objectligne->product_label) {
388 $txtpredefinedservice .=
' - ';
389 $txtpredefinedservice .= $objectligne->product_label;
394 if (empty($conf->global->CONTRACT_HIDE_QTY_ON_PDF)) {
395 $txt .= $outputlangs->transnoentities(
"Quantity") .
' : <strong>' . $objectligne->qty .
'</strong>';
397 if (empty($conf->global->CONTRACT_HIDE_PRICE_ON_PDF)) {
398 $txt .=
' - ' . $outputlangs->transnoentities(
"UnitPrice") .
' : <strong>' .
price($objectligne->subprice) .
'</strong>';
400 if (empty($conf->global->CONTRACT_HIDE_PLANNED_DATE_ON_PDF)) {
402 $txt .= $outputlangs->transnoentities(
"DateStartPlannedShort").
" : <strong>".$datei.
"</strong> - ".$outputlangs->transnoentities(
"DateEndPlanned").
" : <strong>".$datee.
'</strong>';
404 if (empty($conf->global->CONTRACT_HIDE_REAL_DATE_ON_PDF)) {
406 $txt .= $outputlangs->transnoentities(
"DateStartRealShort").
" : <strong>".$daters.
'</strong>';
407 if (!empty($objectligne->date_end_real)) {
408 $txt .=
" - ".$outputlangs->transnoentities(
"DateEndRealShort").
" : '<strong>'".$datere.
'</strong>';
412 $pdf->startTransaction();
414 $pageposafter = $pdf->getPage();
415 if ($pageposafter > $pageposbefore) {
416 $pdf->rollbackTransaction(
true);
417 $pageposafter = $pageposbefore;
419 $pdf->setPageOrientation(
'', 1, $heightforfooter);
421 $pageposafter = $pdf->getPage();
422 $posyafter = $pdf->GetY();
423 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
424 if ($i == ($nblines - 1)) {
425 $pdf->AddPage(
'',
'',
true);
426 if (!empty($tplidx)) {
427 $pdf->useTemplate($tplidx);
430 $this->
_pagehead($pdf, $object, 0, $outputlangs);
432 $pdf->setPage($pageposafter + 1);
438 if (!empty($conf->global->MAIN_PDF_DATA_ON_FIRST_PAGE)) {
439 $showpricebeforepagebreak = 1;
441 $showpricebeforepagebreak = 0;
446 $pdf->commitTransaction();
448 $posYAfterDescription = $pdf->GetY();
450 $nexY = $pdf->GetY() + 2;
451 $pageposafter = $pdf->getPage();
453 $pdf->setPage($pageposbefore);
454 $pdf->setTopMargin($this->marge_haute);
455 $pdf->setPageOrientation(
'', 1, 0);
458 if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
459 $pdf->setPage($pageposafter);
460 $curY = $tab_top_newpage;
463 $pdf->SetFont(
'',
'', $default_font_size - 1);
466 while ($pagenb < $pageposafter) {
467 $pdf->setPage($pagenb);
469 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
471 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
473 $this->
_pagefoot($pdf, $object, $outputlangs, 1);
475 $pdf->setPage($pagenb);
476 $pdf->setPageOrientation(
'', 1, 0);
477 if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) {
478 $this->
_pagehead($pdf, $object, 0, $outputlangs);
480 if (!empty($tplidx)) {
481 $pdf->useTemplate($tplidx);
485 if (isset($object->lines[$i + 1]->pagebreak) && $object->lines[$i + 1]->pagebreak) {
487 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
489 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
491 $this->
_pagefoot($pdf, $object, $outputlangs, 1);
494 if (!empty($tplidx)) {
495 $pdf->useTemplate($tplidx);
505 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
506 $this->
tabSignature($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
507 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
509 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
510 $this->
tabSignature($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
511 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
514 $this->
_pagefoot($pdf, $object, $outputlangs);
515 if (method_exists($pdf,
'AliasNbPages')) {
516 $pdf->AliasNbPages();
521 $pdf->Output($file,
'F');
524 $hookmanager->initHooks(array(
'pdfgeneration'));
525 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
527 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
529 $this->error = $hookmanager->error;
530 $this->errors = $hookmanager->errors;
535 $this->result = array(
'fullpath'=>$file);
539 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
543 $this->error = $langs->transnoentities(
"ErrorConstantNotDefined",
"CONTRACT_OUTPUTDIR");
643 protected function _pagehead(&$pdf, $object, $showaddress, $outputlangs, $outputlangsbis =
null, $titlekey =
"Contract")
646 global $conf, $langs, $hookmanager;
649 if ($outputlangs->trans(
"DIRECTION") ==
'rtl') {
654 $outputlangs->loadLangs(array(
"main",
"dict",
"contract",
"companies"));
660 $pdf->SetTextColor(0, 0, 60);
661 $pdf->SetFont(
'',
'B', $default_font_size + 3);
665 $posy = $this->marge_haute;
666 $posx = $this->page_largeur - $this->marge_droite - $w;
668 $pdf->SetXY($this->marge_gauche, $posy);
671 if (empty($conf->global->PDF_DISABLE_MYCOMPANY_LOGO)) {
672 if ($this->emetteur->logo) {
673 $logodir = $conf->mycompany->dir_output;
674 if (!empty($conf->mycompany->multidir_output[$object->entity])) {
675 $logodir = $conf->mycompany->multidir_output[$object->entity];
677 if (empty($conf->global->MAIN_PDF_USE_LARGE_LOGO)) {
678 $logo = $logodir.
'/logos/thumbs/'.$this->emetteur->logo_small;
680 $logo = $logodir.
'/logos/'.$this->emetteur->logo;
682 if (is_readable($logo)) {
684 $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height);
686 $pdf->SetTextColor(200, 0, 0);
687 $pdf->SetFont(
'',
'B', $default_font_size - 2);
688 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorLogoFileNotFound", $logo), 0,
'L');
689 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorGoToGlobalSetup"), 0,
'L');
692 $text = $this->emetteur->name;
693 $pdf->MultiCell($w, 4, $outputlangs->convToOutputCharset($text), 0, $ltrdirection);
697 $pdf->SetFont(
'',
'B', $default_font_size + 3);
698 $pdf->SetXY($posx, $posy);
699 $pdf->SetTextColor(0, 0, 60);
700 $title = $outputlangs->transnoentities($titlekey);
701 $title .=
' '.$outputlangs->convToOutputCharset($object->ref);
702 if ($object->statut == $object::STATUS_DRAFT) {
703 $pdf->SetTextColor(128, 0, 0);
704 $title .=
' - '.$outputlangs->transnoentities(
"NotValidated");
706 $pdf->MultiCell($w, 3, $title,
'',
'R');
708 $pdf->SetFont(
'',
'B', $default_font_size);
718 $pdf->SetFont(
'',
'', $default_font_size - 1);
721 $pdf->SetXY($posx, $posy);
722 $pdf->SetTextColor(0, 0, 60);
723 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"Date").
" : ".
dol_print_date($object->date_contrat,
"day",
false, $outputlangs,
true),
'',
'R');
725 if (empty($conf->global->MAIN_PDF_HIDE_CUSTOMER_CODE) && $object->thirdparty->code_client) {
727 $pdf->SetXY($posx, $posy);
728 $pdf->SetTextColor(0, 0, 60);
729 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerCode").
" : ".$outputlangs->transnoentities($object->thirdparty->code_client),
'',
'R');
734 $carac_emetteur =
'';
736 $arrayidcontact = $object->getIdContact(
'internal',
'INTERREPFOLL');
737 if (count($arrayidcontact) > 0) {
738 $object->fetch_user($arrayidcontact[0]);
739 $labelbeforecontactname = ($outputlangs->transnoentities(
"FromContactName") !=
'FromContactName' ? $outputlangs->transnoentities(
"FromContactName") : $outputlangs->transnoentities(
"Name"));
740 $carac_emetteur .= ($carac_emetteur ?
"\n" :
'').$labelbeforecontactname.
": ".$outputlangs->convToOutputCharset($object->user->getFullName($outputlangs));
742 $carac_emetteur .= (
getDolGlobalInt(
'PDF_SHOW_PHONE_AFTER_USER_CONTACT') && !empty($object->user->office_phone)) ? $object->user->office_phone :
'';
744 $carac_emetteur .= (
getDolGlobalInt(
'PDF_SHOW_EMAIL_AFTER_USER_CONTACT') && !empty($object->user->email)) ? $object->user->email :
'';
746 $carac_emetteur .=
"\n";
749 $carac_emetteur .=
pdf_build_address($outputlangs, $this->emetteur, $object->thirdparty,
'', 0,
'source', $object);
753 $posx = $this->marge_gauche;
754 if (!empty($conf->global->MAIN_INVERT_SENDER_RECIPIENT)) {
755 $posx = $this->page_largeur - $this->marge_droite - 80;
760 if (empty($conf->global->MAIN_PDF_NO_SENDER_FRAME)) {
761 $pdf->SetTextColor(0, 0, 0);
762 $pdf->SetFont(
'',
'', $default_font_size - 2);
763 $pdf->SetXY($posx, $posy - 5);
764 $pdf->SetXY($posx, $posy);
765 $pdf->SetFillColor(230, 230, 230);
766 $pdf->MultiCell(82, $hautcadre,
"", 0,
'R', 1);
767 $pdf->SetTextColor(0, 0, 60);
771 if (empty($conf->global->MAIN_PDF_HIDE_SENDER_NAME)) {
772 $pdf->SetXY($posx + 2, $posy + 3);
773 $pdf->SetTextColor(0, 0, 60);
774 $pdf->SetFont(
'',
'B', $default_font_size);
775 $pdf->MultiCell(80, 4, $outputlangs->convToOutputCharset($this->emetteur->name), 0,
'L');
776 $posy = $pdf->getY();
780 $pdf->SetXY($posx + 2, $posy);
781 $pdf->SetFont(
'',
'', $default_font_size - 1);
782 $pdf->MultiCell(80, 4, $carac_emetteur, 0,
'L');
787 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
788 if (count($arrayidcontact) > 0) {
790 $result = $object->fetch_contact($arrayidcontact[0]);
793 $this->recipient = $object->thirdparty;
796 if ($usecontact && ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || !empty($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT)))) {
797 $thirdparty = $object->contact;
799 $thirdparty = $object->thirdparty;
805 $carac_client =
pdf_build_address($outputlangs, $this->emetteur, $object->thirdparty, (isset($object->contact) ? $object->contact :
''), $usecontact, $mode, $object);
808 $widthrecbox = !empty($conf->global->MAIN_PDF_USE_ISO_LOCATION) ? 92 : 100;
809 if ($this->page_largeur < 210) {
812 $posy = !empty($conf->global->MAIN_PDF_USE_ISO_LOCATION) ? 40 : 42;
814 $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
815 if (!empty($conf->global->MAIN_INVERT_SENDER_RECIPIENT)) {
816 $posx = $this->marge_gauche;
820 if (empty($conf->global->MAIN_PDF_NO_RECIPENT_FRAME)) {
821 $pdf->SetTextColor(0, 0, 0);
822 $pdf->SetFont(
'',
'', $default_font_size - 2);
823 $pdf->SetXY($posx + 2, $posy - 5);
824 $pdf->Rect($posx, $posy, $widthrecbox, $hautcadre);
825 $pdf->SetTextColor(0, 0, 0);
829 $pdf->SetXY($posx + 2, $posy + 3);
830 $pdf->SetFont(
'',
'B', $default_font_size);
831 $pdf->MultiCell($widthrecbox, 4, $this->recipient->name, 0, $ltrdirection);
833 $posy = $pdf->getY();
836 $pdf->SetFont(
'',
'', $default_font_size - 1);
837 $pdf->SetXY($posx + 2, $posy);
838 $pdf->MultiCell($widthrecbox, 4, $carac_client, 0, $ltrdirection);
841 $pdf->SetTextColor(0, 0, 0);