141 public function write_file($object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
144 global $user, $langs, $conf, $mysoc, $db, $hookmanager, $nblines;
146 if (!is_object($outputlangs)) {
147 $outputlangs = $langs;
151 $outputlangs->charset_output =
'ISO-8859-1';
155 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"contracts"));
158 if ($object->statut == $object::STATUS_DRAFT && (
getDolGlobalString(
'CONTRACT_DRAFT_WATERMARK'))) {
159 $this->watermark = $conf->global->CONTRACT_DRAFT_WATERMARK;
162 global $outputlangsbis;
163 $outputlangsbis =
null;
164 if (
getDolGlobalString(
'PDF_USE_ALSO_LANGUAGE_CODE') && $outputlangs->defaultlang != $conf->global->PDF_USE_ALSO_LANGUAGE_CODE) {
165 $outputlangsbis =
new Translate(
'', $conf);
166 $outputlangsbis->setDefaultLang($conf->global->PDF_USE_ALSO_LANGUAGE_CODE);
167 $outputlangsbis->loadLangs(array(
"main",
"dict",
"companies",
"bills",
"products",
"orders",
"deliveries"));
170 $nblines = count($object->lines);
172 if ($conf->contract->multidir_output[$conf->entity]) {
173 $object->fetch_thirdparty();
176 if ($object->specimen) {
177 $dir = $conf->contract->multidir_output[$conf->entity];
178 $file = $dir.
"/SPECIMEN.pdf";
181 $dir = $conf->contrat->multidir_output[$object->entity].
"/".$objectref;
182 $file = $dir.
"/".$objectref.
".pdf";
185 if (!file_exists($dir)) {
187 $this->error = $langs->trans(
"ErrorCanNotCreateDir", $dir);
192 if (file_exists($dir)) {
194 if (!is_object($hookmanager)) {
195 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
198 $hookmanager->initHooks(array(
'pdfgeneration'));
199 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
201 $reshook = $hookmanager->executeHooks(
'beforePDFCreation', $parameters, $object, $action);
204 $nblines = count($object->lines);
209 $pdf->SetAutoPageBreak(1, 0);
211 $heightforinfotot = 50;
212 $heightforfreetext = (isset($conf->global->MAIN_PDF_FREETEXT_HEIGHT) ? $conf->global->MAIN_PDF_FREETEXT_HEIGHT : 5);
213 $heightforfooter = $this->marge_basse + 9;
215 $heightforfooter += 6;
218 if (class_exists(
'TCPDF')) {
219 $pdf->setPrintHeader(
false);
220 $pdf->setPrintFooter(
false);
225 $logodir = $conf->mycompany->dir_output;
226 if (!empty($conf->mycompany->multidir_output[$object->entity])) {
227 $logodir = $conf->mycompany->multidir_output[$object->entity];
229 $pagecount = $pdf->setSourceFile($logodir.
'/' .
getDolGlobalString(
'MAIN_ADD_PDF_BACKGROUND'));
230 $tplidx = $pdf->importPage(1);
235 $pdf->SetDrawColor(128, 128, 128);
237 $pdf->SetTitle($outputlangs->convToOutputCharset($object->ref));
238 $pdf->SetSubject($outputlangs->transnoentities(
"Contract"));
239 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
240 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
241 $pdf->SetKeyWords($outputlangs->convToOutputCharset($object->ref).
" ".$outputlangs->transnoentities(
"Contract").
" ".$outputlangs->convToOutputCharset($object->thirdparty->name));
243 $pdf->SetCompression(
false);
246 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
250 if (!empty($tplidx)) {
251 $pdf->useTemplate($tplidx);
254 $top_shift = $this->
_pagehead($pdf, $object, 1, $outputlangs, (is_object($outputlangsbis) ? $outputlangsbis :
null));
255 $pdf->SetFont(
'',
'', $default_font_size - 1);
256 $pdf->MultiCell(0, 3,
'');
257 $pdf->SetTextColor(0, 0, 0);
260 $tab_top_newpage = (!
getDolGlobalInt(
'MAIN_PDF_DONOTREPEAT_HEAD') ? 42 : 10);
263 $notetoshow = empty($object->note_public) ?
'' : $object->note_public;
266 $extranote = $this->getExtrafieldsInHtml($object, $outputlangs);
267 if (!empty($extranote)) {
271 if (!empty($notetoshow)) {
279 $pdf->SetFont(
'',
'', $default_font_size - 1);
280 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top - 1,
dol_htmlentitiesbr($notetoshow), 0, 1);
281 $nexY = $pdf->GetY();
282 $height_note = $nexY - $tab_top;
285 $pdf->SetDrawColor(192, 192, 192);
286 $pdf->Rect($this->marge_gauche, $tab_top - 1, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_note + 1);
288 $tab_top = $nexY + 6;
291 $iniY = $tab_top + 7;
292 $curY = $tab_top + 7;
293 $nexY = $tab_top + 2;
295 $pdf->SetXY($this->marge_gauche, $tab_top);
297 $pdf->MultiCell(0, 2,
'');
299 $nblines = count($object->lines);
302 for ($i = 0; $i < $nblines; $i++) {
303 $objectligne = $object->lines[$i];
305 $valide = $objectligne->id ? 1 : 0;
307 if ($valide > 0 || $object->specimen) {
308 $curX = $this->posxdesc - 1;
310 $pdf->SetFont(
'',
'', $default_font_size - 1);
311 $pdf->SetTextColor(0, 0, 0);
313 $pdf->setTopMargin($tab_top_newpage);
314 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext + $heightforinfotot);
315 $pageposbefore = $pdf->getPage();
319 if (!empty($objectligne->date_start)) {
320 $datei =
dol_print_date((
int) $objectligne->date_start,
'day',
false, $outputlangs,
true);
322 $datei = $langs->trans(
"Unknown");
325 if (!empty($objectligne->date_end)) {
326 $durationi =
convertSecondToTime((
int) $objectligne->date_end - (
int) $objectligne->date_start,
'allwithouthour');
327 $datee =
dol_print_date($objectligne->date_end,
'day',
false, $outputlangs,
true);
329 $durationi = $langs->trans(
"Unknown");
330 $datee = $langs->trans(
"Unknown");
333 if (!empty($objectligne->date_start_real)) {
334 $daters =
dol_print_date((
int) $objectligne->date_start_real,
'day',
false, $outputlangs,
true);
336 $daters = $langs->trans(
"Unknown");
339 if (!empty($objectligne->date_end_real)) {
340 $datere =
dol_print_date((
int) $objectligne->date_end_real,
'day',
false, $outputlangs,
true);
342 $datere = $langs->trans(
"Unknown");
345 $txtpredefinedservice = $objectligne->product_ref;
346 if ($objectligne->product_label) {
347 $txtpredefinedservice .=
' - ';
348 $txtpredefinedservice .= $objectligne->product_label;
354 $txt .= $outputlangs->transnoentities(
"Quantity") .
' : <strong>' . $objectligne->qty .
'</strong>';
357 $txt .=
' - ' . $outputlangs->transnoentities(
"UnitPrice") .
' : <strong>' .
price($objectligne->subprice) .
'</strong>';
361 $txt .= $outputlangs->transnoentities(
"DateStartPlannedShort").
" : <strong>".$datei.
"</strong> - ".$outputlangs->transnoentities(
"DateEndPlanned").
" : <strong>".$datee.
'</strong>';
365 $txt .= $outputlangs->transnoentities(
"DateStartRealShort").
" : <strong>".$daters.
'</strong>';
366 if (!empty($objectligne->date_end_real)) {
367 $txt .=
" - ".$outputlangs->transnoentities(
"DateEndRealShort").
" : '<strong>'".$datere.
'</strong>';
371 $pdf->startTransaction();
373 $pageposafter = $pdf->getPage();
374 if ($pageposafter > $pageposbefore) {
375 $pdf->rollbackTransaction(
true);
376 $pageposafter = $pageposbefore;
378 $pdf->setPageOrientation(
'', 1, $heightforfooter);
380 $pageposafter = $pdf->getPage();
381 $posyafter = $pdf->GetY();
382 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
383 if ($i == ($nblines - 1)) {
384 $pdf->AddPage(
'',
'',
true);
385 if (!empty($tplidx)) {
386 $pdf->useTemplate($tplidx);
389 $this->
_pagehead($pdf, $object, 0, $outputlangs);
391 $pdf->setPage($pageposafter + 1);
398 $showpricebeforepagebreak = 1;
400 $showpricebeforepagebreak = 0;
404 $pdf->commitTransaction();
406 $posYAfterDescription = $pdf->GetY();
408 $nexY = $pdf->GetY() + 2;
409 $pageposafter = $pdf->getPage();
411 $pdf->setPage($pageposbefore);
412 $pdf->setTopMargin($this->marge_haute);
413 $pdf->setPageOrientation(
'', 1, 0);
416 if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
417 $pdf->setPage($pageposafter);
418 $curY = $tab_top_newpage;
421 $pdf->SetFont(
'',
'', $default_font_size - 1);
424 while ($pagenb < $pageposafter) {
425 $pdf->setPage($pagenb);
427 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
429 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
431 $this->
_pagefoot($pdf, $object, $outputlangs, 1);
433 $pdf->setPage($pagenb);
434 $pdf->setPageOrientation(
'', 1, 0);
436 $this->
_pagehead($pdf, $object, 0, $outputlangs);
438 if (!empty($tplidx)) {
439 $pdf->useTemplate($tplidx);
443 if (isset($object->lines[$i + 1]->pagebreak) && $object->lines[$i + 1]->pagebreak) {
445 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
447 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
449 $this->
_pagefoot($pdf, $object, $outputlangs, 1);
452 if (!empty($tplidx)) {
453 $pdf->useTemplate($tplidx);
463 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
464 $this->
tabSignature($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
465 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
467 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
468 $this->
tabSignature($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
469 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
472 $this->
_pagefoot($pdf, $object, $outputlangs);
473 if (method_exists($pdf,
'AliasNbPages')) {
474 $pdf->AliasNbPages();
479 $pdf->Output($file,
'F');
482 $hookmanager->initHooks(array(
'pdfgeneration'));
483 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
485 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
487 $this->error = $hookmanager->error;
488 $this->errors = $hookmanager->errors;
493 $this->result = array(
'fullpath'=>$file);
497 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
501 $this->error = $langs->transnoentities(
"ErrorConstantNotDefined",
"CONTRACT_OUTPUTDIR");
600 protected function _pagehead(&$pdf, $object, $showaddress, $outputlangs, $outputlangsbis =
null, $titlekey =
"Contract")
608 if ($outputlangs->trans(
"DIRECTION") ==
'rtl') {
613 $outputlangs->loadLangs(array(
"main",
"dict",
"contract",
"companies"));
619 $pdf->SetTextColor(0, 0, 60);
620 $pdf->SetFont(
'',
'B', $default_font_size + 3);
624 $posy = $this->marge_haute;
625 $posx = $this->page_largeur - $this->marge_droite - $w;
627 $pdf->SetXY($this->marge_gauche, $posy);
631 if ($this->emetteur->logo) {
632 $logodir = $conf->mycompany->dir_output;
633 if (!empty($conf->mycompany->multidir_output[$object->entity])) {
634 $logodir = $conf->mycompany->multidir_output[$object->entity];
637 $logo = $logodir.
'/logos/thumbs/'.$this->emetteur->logo_small;
639 $logo = $logodir.
'/logos/'.$this->emetteur->logo;
641 if (is_readable($logo)) {
643 $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height);
645 $pdf->SetTextColor(200, 0, 0);
646 $pdf->SetFont(
'',
'B', $default_font_size - 2);
647 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorLogoFileNotFound", $logo), 0,
'L');
648 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorGoToGlobalSetup"), 0,
'L');
651 $text = $this->emetteur->name;
652 $pdf->MultiCell($w, 4, $outputlangs->convToOutputCharset($text), 0, $ltrdirection);
656 $pdf->SetFont(
'',
'B', $default_font_size + 3);
657 $pdf->SetXY($posx, $posy);
658 $pdf->SetTextColor(0, 0, 60);
659 $title = $outputlangs->transnoentities($titlekey);
660 $title .=
' '.$outputlangs->convToOutputCharset($object->ref);
661 if ($object->statut == $object::STATUS_DRAFT) {
662 $pdf->SetTextColor(128, 0, 0);
663 $title .=
' - '.$outputlangs->transnoentities(
"NotValidated");
665 $pdf->MultiCell($w, 3, $title,
'',
'R');
667 $pdf->SetFont(
'',
'B', $default_font_size);
677 $pdf->SetFont(
'',
'', $default_font_size - 1);
680 $pdf->SetXY($posx, $posy);
681 $pdf->SetTextColor(0, 0, 60);
682 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"Date").
" : ".
dol_print_date($object->date_contrat,
"day",
false, $outputlangs,
true),
'',
'R');
684 if (!
getDolGlobalString(
'MAIN_PDF_HIDE_CUSTOMER_CODE') && $object->thirdparty->code_client) {
686 $pdf->SetXY($posx, $posy);
687 $pdf->SetTextColor(0, 0, 60);
688 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerCode").
" : ".$outputlangs->transnoentities($object->thirdparty->code_client),
'',
'R');
693 $carac_emetteur =
'';
695 $arrayidcontact = $object->getIdContact(
'internal',
'INTERREPFOLL');
696 if (count($arrayidcontact) > 0) {
697 $object->fetch_user($arrayidcontact[0]);
698 $labelbeforecontactname = ($outputlangs->transnoentities(
"FromContactName") !=
'FromContactName' ? $outputlangs->transnoentities(
"FromContactName") : $outputlangs->transnoentities(
"Name"));
699 $carac_emetteur .= ($carac_emetteur ?
"\n" :
'').$labelbeforecontactname.
": ".$outputlangs->convToOutputCharset($object->user->getFullName($outputlangs));
701 $carac_emetteur .= (
getDolGlobalInt(
'PDF_SHOW_PHONE_AFTER_USER_CONTACT') && !empty($object->user->office_phone)) ? $object->user->office_phone :
'';
703 $carac_emetteur .= (
getDolGlobalInt(
'PDF_SHOW_EMAIL_AFTER_USER_CONTACT') && !empty($object->user->email)) ? $object->user->email :
'';
705 $carac_emetteur .=
"\n";
708 $carac_emetteur .=
pdf_build_address($outputlangs, $this->emetteur, $object->thirdparty,
'', 0,
'source', $object);
712 $posx = $this->marge_gauche;
714 $posx = $this->page_largeur - $this->marge_droite - 80;
720 $pdf->SetTextColor(0, 0, 0);
721 $pdf->SetFont(
'',
'', $default_font_size - 2);
722 $pdf->SetXY($posx, $posy - 5);
723 $pdf->SetXY($posx, $posy);
724 $pdf->SetFillColor(230, 230, 230);
725 $pdf->MultiCell(82, $hautcadre,
"", 0,
'R', 1);
726 $pdf->SetTextColor(0, 0, 60);
731 $pdf->SetXY($posx + 2, $posy + 3);
732 $pdf->SetTextColor(0, 0, 60);
733 $pdf->SetFont(
'',
'B', $default_font_size);
734 $pdf->MultiCell(80, 4, $outputlangs->convToOutputCharset($this->emetteur->name), 0,
'L');
735 $posy = $pdf->getY();
739 $pdf->SetXY($posx + 2, $posy);
740 $pdf->SetFont(
'',
'', $default_font_size - 1);
741 $pdf->MultiCell(80, 4, $carac_emetteur, 0,
'L');
746 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
747 if (count($arrayidcontact) > 0) {
749 $result = $object->fetch_contact($arrayidcontact[0]);
752 $this->recipient = $object->thirdparty;
755 if ($usecontact && ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT')))) {
756 $thirdparty = $object->contact;
758 $thirdparty = $object->thirdparty;
764 $carac_client =
pdf_build_address($outputlangs, $this->emetteur, $object->thirdparty, (isset($object->contact) ? $object->contact :
''), $usecontact, $mode, $object);
768 if ($this->page_largeur < 210) {
773 $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
775 $posx = $this->marge_gauche;
780 $pdf->SetTextColor(0, 0, 0);
781 $pdf->SetFont(
'',
'', $default_font_size - 2);
782 $pdf->SetXY($posx + 2, $posy - 5);
783 $pdf->Rect($posx, $posy, $widthrecbox, $hautcadre);
784 $pdf->SetTextColor(0, 0, 0);
788 $pdf->SetXY($posx + 2, $posy + 3);
789 $pdf->SetFont(
'',
'B', $default_font_size);
790 $pdf->MultiCell($widthrecbox, 4, $this->recipient->name, 0, $ltrdirection);
792 $posy = $pdf->getY();
795 $pdf->SetFont(
'',
'', $default_font_size - 1);
796 $pdf->SetXY($posx + 2, $posy);
797 $pdf->MultiCell($widthrecbox, 4, $carac_client, 0, $ltrdirection);
800 $pdf->SetTextColor(0, 0, 0);