181 public function write_file($object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
184 global $user, $langs, $conf, $mysoc, $db, $hookmanager, $nblines;
186 if (!is_object($outputlangs)) {
187 $outputlangs = $langs;
190 if (!empty($conf->global->MAIN_USE_FPDF)) {
191 $outputlangs->charset_output =
'ISO-8859-1';
195 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"contracts"));
198 if ($object->statut == $object::STATUS_DRAFT && (!empty($conf->global->CONTRACT_DRAFT_WATERMARK))) {
199 $this->watermark = $conf->global->CONTRACT_DRAFT_WATERMARK;
202 global $outputlangsbis;
203 $outputlangsbis =
null;
204 if (!empty($conf->global->PDF_USE_ALSO_LANGUAGE_CODE) && $outputlangs->defaultlang != $conf->global->PDF_USE_ALSO_LANGUAGE_CODE) {
205 $outputlangsbis =
new Translate(
'', $conf);
206 $outputlangsbis->setDefaultLang($conf->global->PDF_USE_ALSO_LANGUAGE_CODE);
207 $outputlangsbis->loadLangs(array(
"main",
"dict",
"companies",
"bills",
"products",
"orders",
"deliveries"));
210 $nblines = count($object->lines);
212 if ($conf->contract->multidir_output[$conf->entity]) {
213 $object->fetch_thirdparty();
216 if ($object->specimen) {
217 $dir = $conf->contract->multidir_output[$conf->entity];
218 $file = $dir.
"/SPECIMEN.pdf";
221 $dir = $conf->contrat->multidir_output[$object->entity].
"/".$objectref;
222 $file = $dir.
"/".$objectref.
".pdf";
225 if (!file_exists($dir)) {
227 $this->error = $langs->trans(
"ErrorCanNotCreateDir", $dir);
232 if (file_exists($dir)) {
234 if (!is_object($hookmanager)) {
235 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
238 $hookmanager->initHooks(array(
'pdfgeneration'));
239 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
241 $reshook = $hookmanager->executeHooks(
'beforePDFCreation', $parameters, $object, $action);
244 $nblines = count($object->lines);
249 $pdf->SetAutoPageBreak(1, 0);
251 $heightforinfotot = 50;
252 $heightforfreetext = (isset($conf->global->MAIN_PDF_FREETEXT_HEIGHT) ? $conf->global->MAIN_PDF_FREETEXT_HEIGHT : 5);
253 $heightforfooter = $this->marge_basse + 9;
254 if (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS)) {
255 $heightforfooter += 6;
258 if (class_exists(
'TCPDF')) {
259 $pdf->setPrintHeader(
false);
260 $pdf->setPrintFooter(
false);
264 if (!empty($conf->global->MAIN_ADD_PDF_BACKGROUND)) {
265 $logodir = $conf->mycompany->dir_output;
266 if (!empty($conf->mycompany->multidir_output[$object->entity])) {
267 $logodir = $conf->mycompany->multidir_output[$object->entity];
269 $pagecount = $pdf->setSourceFile($logodir.
'/'.$conf->global->MAIN_ADD_PDF_BACKGROUND);
270 $tplidx = $pdf->importPage(1);
275 $pdf->SetDrawColor(128, 128, 128);
277 $pdf->SetTitle($outputlangs->convToOutputCharset($object->ref));
278 $pdf->SetSubject($outputlangs->transnoentities(
"Contract"));
279 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
280 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
281 $pdf->SetKeyWords($outputlangs->convToOutputCharset($object->ref).
" ".$outputlangs->transnoentities(
"Contract").
" ".$outputlangs->convToOutputCharset($object->thirdparty->name));
283 $pdf->SetCompression(
false);
286 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
290 if (!empty($tplidx)) {
291 $pdf->useTemplate($tplidx);
294 $top_shift = $this->
_pagehead($pdf, $object, 1, $outputlangs, (is_object($outputlangsbis) ? $outputlangsbis :
null));
295 $pdf->SetFont(
'',
'', $default_font_size - 1);
296 $pdf->MultiCell(0, 3,
'');
297 $pdf->SetTextColor(0, 0, 0);
300 $tab_top_newpage = (!
getDolGlobalInt(
'MAIN_PDF_DONOTREPEAT_HEAD') ? 42 : 10);
303 if (!empty($object->note_public)) {
306 $pdf->SetFont(
'',
'', $default_font_size - 1);
307 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top - 1,
dol_htmlentitiesbr($object->note_public), 0, 1);
308 $nexY = $pdf->GetY();
309 $height_note = $nexY - $tab_top;
312 $pdf->SetDrawColor(192, 192, 192);
313 $pdf->Rect($this->marge_gauche, $tab_top - 1, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_note + 1);
315 $tab_top = $nexY + 6;
318 $iniY = $tab_top + 7;
319 $curY = $tab_top + 7;
320 $nexY = $tab_top + 2;
322 $pdf->SetXY($this->marge_gauche, $tab_top);
324 $pdf->MultiCell(0, 2,
'');
326 $nblines = count($object->lines);
329 for ($i = 0; $i < $nblines; $i++) {
330 $objectligne = $object->lines[$i];
332 $valide = $objectligne->id ? 1 : 0;
334 if ($valide > 0 || $object->specimen) {
335 $curX = $this->posxdesc - 1;
337 $pdf->SetFont(
'',
'', $default_font_size - 1);
338 $pdf->SetTextColor(0, 0, 0);
340 $pdf->setTopMargin($tab_top_newpage);
341 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext + $heightforinfotot);
342 $pageposbefore = $pdf->getPage();
346 if (!empty($objectligne->date_start)) {
347 $datei =
dol_print_date((
int) $objectligne->date_start,
'day',
false, $outputlangs,
true);
349 $datei = $langs->trans(
"Unknown");
352 if (!empty($objectligne->date_end)) {
353 $durationi =
convertSecondToTime((
int) $objectligne->date_end - (
int) $objectligne->date_start,
'allwithouthour');
354 $datee =
dol_print_date($objectligne->date_end,
'day',
false, $outputlangs,
true);
356 $durationi = $langs->trans(
"Unknown");
357 $datee = $langs->trans(
"Unknown");
360 if (!empty($objectligne->date_start_real)) {
361 $daters =
dol_print_date((
int) $objectligne->date_start_real,
'day',
false, $outputlangs,
true);
363 $daters = $langs->trans(
"Unknown");
366 if (!empty($objectligne->date_end_real)) {
367 $datere =
dol_print_date((
int) $objectligne->date_end_real,
'day',
false, $outputlangs,
true);
369 $datere = $langs->trans(
"Unknown");
372 $txtpredefinedservice = $objectligne->product_ref;
373 if ($objectligne->product_label) {
374 $txtpredefinedservice .=
' - ';
375 $txtpredefinedservice .= $objectligne->product_label;
380 if (empty($conf->global->CONTRACT_HIDE_QTY_ON_PDF)) {
381 $txt .= $outputlangs->transnoentities(
"Quantity") .
' : <strong>' . $objectligne->qty .
'</strong>';
383 if (empty($conf->global->CONTRACT_HIDE_PRICE_ON_PDF)) {
384 $txt .=
' - ' . $outputlangs->transnoentities(
"UnitPrice") .
' : <strong>' .
price($objectligne->subprice) .
'</strong>';
386 if (empty($conf->global->CONTRACT_HIDE_PLANNED_DATE_ON_PDF)) {
388 $txt .= $outputlangs->transnoentities(
"DateStartPlannedShort").
" : <strong>".$datei.
"</strong> - ".$outputlangs->transnoentities(
"DateEndPlanned").
" : <strong>".$datee.
'</strong>';
390 if (empty($conf->global->CONTRACT_HIDE_REAL_DATE_ON_PDF)) {
392 $txt .= $outputlangs->transnoentities(
"DateStartRealShort").
" : <strong>".$daters.
'</strong>';
393 if (!empty($objectligne->date_end_real)) {
394 $txt .=
" - ".$outputlangs->transnoentities(
"DateEndRealShort").
" : '<strong>'".$datere.
'</strong>';
398 $pdf->startTransaction();
400 $pageposafter = $pdf->getPage();
401 if ($pageposafter > $pageposbefore) {
402 $pdf->rollbackTransaction(
true);
403 $pageposafter = $pageposbefore;
405 $pdf->setPageOrientation(
'', 1, $heightforfooter);
407 $pageposafter = $pdf->getPage();
408 $posyafter = $pdf->GetY();
409 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
410 if ($i == ($nblines - 1)) {
411 $pdf->AddPage(
'',
'',
true);
412 if (!empty($tplidx)) {
413 $pdf->useTemplate($tplidx);
416 $this->
_pagehead($pdf, $object, 0, $outputlangs);
418 $pdf->setPage($pageposafter + 1);
424 if (!empty($conf->global->MAIN_PDF_DATA_ON_FIRST_PAGE)) {
425 $showpricebeforepagebreak = 1;
427 $showpricebeforepagebreak = 0;
432 $pdf->commitTransaction();
434 $posYAfterDescription = $pdf->GetY();
436 $nexY = $pdf->GetY() + 2;
437 $pageposafter = $pdf->getPage();
439 $pdf->setPage($pageposbefore);
440 $pdf->setTopMargin($this->marge_haute);
441 $pdf->setPageOrientation(
'', 1, 0);
444 if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
445 $pdf->setPage($pageposafter);
446 $curY = $tab_top_newpage;
449 $pdf->SetFont(
'',
'', $default_font_size - 1);
452 while ($pagenb < $pageposafter) {
453 $pdf->setPage($pagenb);
455 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
457 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
459 $this->
_pagefoot($pdf, $object, $outputlangs, 1);
461 $pdf->setPage($pagenb);
462 $pdf->setPageOrientation(
'', 1, 0);
463 if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) {
464 $this->
_pagehead($pdf, $object, 0, $outputlangs);
466 if (!empty($tplidx)) {
467 $pdf->useTemplate($tplidx);
471 if (isset($object->lines[$i + 1]->pagebreak) && $object->lines[$i + 1]->pagebreak) {
473 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
475 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
477 $this->
_pagefoot($pdf, $object, $outputlangs, 1);
480 if (!empty($tplidx)) {
481 $pdf->useTemplate($tplidx);
491 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
492 $this->
tabSignature($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
493 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
495 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
496 $this->
tabSignature($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
497 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
500 $this->
_pagefoot($pdf, $object, $outputlangs);
501 if (method_exists($pdf,
'AliasNbPages')) {
502 $pdf->AliasNbPages();
507 $pdf->Output($file,
'F');
510 $hookmanager->initHooks(array(
'pdfgeneration'));
511 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
513 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
515 $this->error = $hookmanager->error;
516 $this->errors = $hookmanager->errors;
521 $this->result = array(
'fullpath'=>$file);
525 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
529 $this->error = $langs->transnoentities(
"ErrorConstantNotDefined",
"CONTRACT_OUTPUTDIR");
629 protected function _pagehead(&$pdf, $object, $showaddress, $outputlangs, $outputlangsbis =
null, $titlekey =
"Contract")
632 global $conf, $langs, $hookmanager;
635 if ($outputlangs->trans(
"DIRECTION") ==
'rtl') {
640 $outputlangs->loadLangs(array(
"main",
"dict",
"contract",
"companies"));
646 $pdf->SetTextColor(0, 0, 60);
647 $pdf->SetFont(
'',
'B', $default_font_size + 3);
651 $posy = $this->marge_haute;
652 $posx = $this->page_largeur - $this->marge_droite - $w;
654 $pdf->SetXY($this->marge_gauche, $posy);
657 if (empty($conf->global->PDF_DISABLE_MYCOMPANY_LOGO)) {
658 if ($this->emetteur->logo) {
659 $logodir = $conf->mycompany->dir_output;
660 if (!empty($conf->mycompany->multidir_output[$object->entity])) {
661 $logodir = $conf->mycompany->multidir_output[$object->entity];
663 if (empty($conf->global->MAIN_PDF_USE_LARGE_LOGO)) {
664 $logo = $logodir.
'/logos/thumbs/'.$this->emetteur->logo_small;
666 $logo = $logodir.
'/logos/'.$this->emetteur->logo;
668 if (is_readable($logo)) {
670 $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height);
672 $pdf->SetTextColor(200, 0, 0);
673 $pdf->SetFont(
'',
'B', $default_font_size - 2);
674 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorLogoFileNotFound", $logo), 0,
'L');
675 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorGoToGlobalSetup"), 0,
'L');
678 $text = $this->emetteur->name;
679 $pdf->MultiCell($w, 4, $outputlangs->convToOutputCharset($text), 0, $ltrdirection);
683 $pdf->SetFont(
'',
'B', $default_font_size + 3);
684 $pdf->SetXY($posx, $posy);
685 $pdf->SetTextColor(0, 0, 60);
686 $title = $outputlangs->transnoentities($titlekey);
687 $title .=
' '.$outputlangs->convToOutputCharset($object->ref);
688 if ($object->statut == $object::STATUS_DRAFT) {
689 $pdf->SetTextColor(128, 0, 0);
690 $title .=
' - '.$outputlangs->transnoentities(
"NotValidated");
692 $pdf->MultiCell($w, 3, $title,
'',
'R');
694 $pdf->SetFont(
'',
'B', $default_font_size);
704 $pdf->SetFont(
'',
'', $default_font_size - 1);
707 $pdf->SetXY($posx, $posy);
708 $pdf->SetTextColor(0, 0, 60);
709 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"Date").
" : ".
dol_print_date($object->date_contrat,
"day",
false, $outputlangs,
true),
'',
'R');
711 if (empty($conf->global->MAIN_PDF_HIDE_CUSTOMER_CODE) && $object->thirdparty->code_client) {
713 $pdf->SetXY($posx, $posy);
714 $pdf->SetTextColor(0, 0, 60);
715 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerCode").
" : ".$outputlangs->transnoentities($object->thirdparty->code_client),
'',
'R');
720 $carac_emetteur =
'';
722 $arrayidcontact = $object->getIdContact(
'internal',
'INTERREPFOLL');
723 if (count($arrayidcontact) > 0) {
724 $object->fetch_user($arrayidcontact[0]);
725 $labelbeforecontactname = ($outputlangs->transnoentities(
"FromContactName") !=
'FromContactName' ? $outputlangs->transnoentities(
"FromContactName") : $outputlangs->transnoentities(
"Name"));
726 $carac_emetteur .= ($carac_emetteur ?
"\n" :
'').$labelbeforecontactname.
": ".$outputlangs->convToOutputCharset($object->user->getFullName($outputlangs));
728 $carac_emetteur .= (
getDolGlobalInt(
'PDF_SHOW_PHONE_AFTER_USER_CONTACT') && !empty($object->user->office_phone)) ? $object->user->office_phone :
'';
730 $carac_emetteur .= (
getDolGlobalInt(
'PDF_SHOW_EMAIL_AFTER_USER_CONTACT') && !empty($object->user->email)) ? $object->user->email :
'';
732 $carac_emetteur .=
"\n";
735 $carac_emetteur .=
pdf_build_address($outputlangs, $this->emetteur, $object->thirdparty,
'', 0,
'source', $object);
739 $posx = $this->marge_gauche;
740 if (!empty($conf->global->MAIN_INVERT_SENDER_RECIPIENT)) {
741 $posx = $this->page_largeur - $this->marge_droite - 80;
746 if (empty($conf->global->MAIN_PDF_NO_SENDER_FRAME)) {
747 $pdf->SetTextColor(0, 0, 0);
748 $pdf->SetFont(
'',
'', $default_font_size - 2);
749 $pdf->SetXY($posx, $posy - 5);
750 $pdf->SetXY($posx, $posy);
751 $pdf->SetFillColor(230, 230, 230);
752 $pdf->MultiCell(82, $hautcadre,
"", 0,
'R', 1);
753 $pdf->SetTextColor(0, 0, 60);
757 if (empty($conf->global->MAIN_PDF_HIDE_SENDER_NAME)) {
758 $pdf->SetXY($posx + 2, $posy + 3);
759 $pdf->SetTextColor(0, 0, 60);
760 $pdf->SetFont(
'',
'B', $default_font_size);
761 $pdf->MultiCell(80, 4, $outputlangs->convToOutputCharset($this->emetteur->name), 0,
'L');
762 $posy = $pdf->getY();
766 $pdf->SetXY($posx + 2, $posy);
767 $pdf->SetFont(
'',
'', $default_font_size - 1);
768 $pdf->MultiCell(80, 4, $carac_emetteur, 0,
'L');
773 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
774 if (count($arrayidcontact) > 0) {
776 $result = $object->fetch_contact($arrayidcontact[0]);
779 $this->recipient = $object->thirdparty;
782 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)))) {
783 $thirdparty = $object->contact;
785 $thirdparty = $object->thirdparty;
791 $carac_client =
pdf_build_address($outputlangs, $this->emetteur, $object->thirdparty, (isset($object->contact) ? $object->contact :
''), $usecontact, $mode, $object);
794 $widthrecbox = !empty($conf->global->MAIN_PDF_USE_ISO_LOCATION) ? 92 : 100;
795 if ($this->page_largeur < 210) {
798 $posy = !empty($conf->global->MAIN_PDF_USE_ISO_LOCATION) ? 40 : 42;
800 $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
801 if (!empty($conf->global->MAIN_INVERT_SENDER_RECIPIENT)) {
802 $posx = $this->marge_gauche;
806 if (empty($conf->global->MAIN_PDF_NO_RECIPENT_FRAME)) {
807 $pdf->SetTextColor(0, 0, 0);
808 $pdf->SetFont(
'',
'', $default_font_size - 2);
809 $pdf->SetXY($posx + 2, $posy - 5);
810 $pdf->Rect($posx, $posy, $widthrecbox, $hautcadre);
811 $pdf->SetTextColor(0, 0, 0);
815 $pdf->SetXY($posx + 2, $posy + 3);
816 $pdf->SetFont(
'',
'B', $default_font_size);
817 $pdf->MultiCell($widthrecbox, 4, $this->recipient->name, 0, $ltrdirection);
819 $posy = $pdf->getY();
822 $pdf->SetFont(
'',
'', $default_font_size - 1);
823 $pdf->SetXY($posx + 2, $posy);
824 $pdf->MultiCell($widthrecbox, 4, $carac_client, 0, $ltrdirection);
827 $pdf->SetTextColor(0, 0, 0);