140 public function write_file($object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
143 global $user, $langs, $conf, $mysoc, $db, $hookmanager, $nblines;
145 if (!is_object($outputlangs)) {
146 $outputlangs = $langs;
150 $outputlangs->charset_output =
'ISO-8859-1';
154 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"contracts"));
157 if ($object->statut == $object::STATUS_DRAFT && (
getDolGlobalString(
'CONTRACT_DRAFT_WATERMARK'))) {
158 $this->watermark = $conf->global->CONTRACT_DRAFT_WATERMARK;
161 global $outputlangsbis;
162 $outputlangsbis =
null;
163 if (
getDolGlobalString(
'PDF_USE_ALSO_LANGUAGE_CODE') && $outputlangs->defaultlang != $conf->global->PDF_USE_ALSO_LANGUAGE_CODE) {
164 $outputlangsbis =
new Translate(
'', $conf);
165 $outputlangsbis->setDefaultLang($conf->global->PDF_USE_ALSO_LANGUAGE_CODE);
166 $outputlangsbis->loadLangs(array(
"main",
"dict",
"companies",
"bills",
"products",
"orders",
"deliveries"));
169 $nblines = count($object->lines);
171 if ($conf->contract->multidir_output[$conf->entity]) {
172 $object->fetch_thirdparty();
175 if ($object->specimen) {
176 $dir = $conf->contract->multidir_output[$conf->entity];
177 $file = $dir.
"/SPECIMEN.pdf";
180 $dir = $conf->contrat->multidir_output[$object->entity].
"/".$objectref;
181 $file = $dir.
"/".$objectref.
".pdf";
184 if (!file_exists($dir)) {
186 $this->error = $langs->trans(
"ErrorCanNotCreateDir", $dir);
191 if (file_exists($dir)) {
193 if (!is_object($hookmanager)) {
194 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
197 $hookmanager->initHooks(array(
'pdfgeneration'));
198 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
200 $reshook = $hookmanager->executeHooks(
'beforePDFCreation', $parameters, $object, $action);
203 $nblines = count($object->lines);
208 $pdf->SetAutoPageBreak(1, 0);
210 $heightforinfotot = 50;
211 $heightforfreetext = (isset($conf->global->MAIN_PDF_FREETEXT_HEIGHT) ? $conf->global->MAIN_PDF_FREETEXT_HEIGHT : 5);
212 $heightforfooter = $this->marge_basse + 9;
214 $heightforfooter += 6;
217 if (class_exists(
'TCPDF')) {
218 $pdf->setPrintHeader(
false);
219 $pdf->setPrintFooter(
false);
224 $logodir = $conf->mycompany->dir_output;
225 if (!empty($conf->mycompany->multidir_output[$object->entity])) {
226 $logodir = $conf->mycompany->multidir_output[$object->entity];
228 $pagecount = $pdf->setSourceFile($logodir.
'/' .
getDolGlobalString(
'MAIN_ADD_PDF_BACKGROUND'));
229 $tplidx = $pdf->importPage(1);
234 $pdf->SetDrawColor(128, 128, 128);
236 $pdf->SetTitle($outputlangs->convToOutputCharset($object->ref));
237 $pdf->SetSubject($outputlangs->transnoentities(
"Contract"));
238 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
239 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
240 $pdf->SetKeyWords($outputlangs->convToOutputCharset($object->ref).
" ".$outputlangs->transnoentities(
"Contract").
" ".$outputlangs->convToOutputCharset($object->thirdparty->name));
242 $pdf->SetCompression(
false);
245 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
249 if (!empty($tplidx)) {
250 $pdf->useTemplate($tplidx);
253 $top_shift = $this->
_pagehead($pdf, $object, 1, $outputlangs, (is_object($outputlangsbis) ? $outputlangsbis :
null));
254 $pdf->SetFont(
'',
'', $default_font_size - 1);
255 $pdf->MultiCell(0, 3,
'');
256 $pdf->SetTextColor(0, 0, 0);
259 $tab_top_newpage = (!
getDolGlobalInt(
'MAIN_PDF_DONOTREPEAT_HEAD') ? 42 : 10);
262 if (!empty($object->note_public)) {
265 $pdf->SetFont(
'',
'', $default_font_size - 1);
266 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top - 1,
dol_htmlentitiesbr($object->note_public), 0, 1);
267 $nexY = $pdf->GetY();
268 $height_note = $nexY - $tab_top;
271 $pdf->SetDrawColor(192, 192, 192);
272 $pdf->Rect($this->marge_gauche, $tab_top - 1, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_note + 1);
274 $tab_top = $nexY + 6;
277 $iniY = $tab_top + 7;
278 $curY = $tab_top + 7;
279 $nexY = $tab_top + 2;
281 $pdf->SetXY($this->marge_gauche, $tab_top);
283 $pdf->MultiCell(0, 2,
'');
285 $nblines = count($object->lines);
288 for ($i = 0; $i < $nblines; $i++) {
289 $objectligne = $object->lines[$i];
291 $valide = $objectligne->id ? 1 : 0;
293 if ($valide > 0 || $object->specimen) {
294 $curX = $this->posxdesc - 1;
296 $pdf->SetFont(
'',
'', $default_font_size - 1);
297 $pdf->SetTextColor(0, 0, 0);
299 $pdf->setTopMargin($tab_top_newpage);
300 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext + $heightforinfotot);
301 $pageposbefore = $pdf->getPage();
305 if (!empty($objectligne->date_start)) {
306 $datei =
dol_print_date((
int) $objectligne->date_start,
'day',
false, $outputlangs,
true);
308 $datei = $langs->trans(
"Unknown");
311 if (!empty($objectligne->date_end)) {
312 $durationi =
convertSecondToTime((
int) $objectligne->date_end - (
int) $objectligne->date_start,
'allwithouthour');
313 $datee =
dol_print_date($objectligne->date_end,
'day',
false, $outputlangs,
true);
315 $durationi = $langs->trans(
"Unknown");
316 $datee = $langs->trans(
"Unknown");
319 if (!empty($objectligne->date_start_real)) {
320 $daters =
dol_print_date((
int) $objectligne->date_start_real,
'day',
false, $outputlangs,
true);
322 $daters = $langs->trans(
"Unknown");
325 if (!empty($objectligne->date_end_real)) {
326 $datere =
dol_print_date((
int) $objectligne->date_end_real,
'day',
false, $outputlangs,
true);
328 $datere = $langs->trans(
"Unknown");
331 $txtpredefinedservice = $objectligne->product_ref;
332 if ($objectligne->product_label) {
333 $txtpredefinedservice .=
' - ';
334 $txtpredefinedservice .= $objectligne->product_label;
340 $txt .= $outputlangs->transnoentities(
"Quantity") .
' : <strong>' . $objectligne->qty .
'</strong>';
343 $txt .=
' - ' . $outputlangs->transnoentities(
"UnitPrice") .
' : <strong>' .
price($objectligne->subprice) .
'</strong>';
347 $txt .= $outputlangs->transnoentities(
"DateStartPlannedShort").
" : <strong>".$datei.
"</strong> - ".$outputlangs->transnoentities(
"DateEndPlanned").
" : <strong>".$datee.
'</strong>';
351 $txt .= $outputlangs->transnoentities(
"DateStartRealShort").
" : <strong>".$daters.
'</strong>';
352 if (!empty($objectligne->date_end_real)) {
353 $txt .=
" - ".$outputlangs->transnoentities(
"DateEndRealShort").
" : '<strong>'".$datere.
'</strong>';
357 $pdf->startTransaction();
359 $pageposafter = $pdf->getPage();
360 if ($pageposafter > $pageposbefore) {
361 $pdf->rollbackTransaction(
true);
362 $pageposafter = $pageposbefore;
364 $pdf->setPageOrientation(
'', 1, $heightforfooter);
366 $pageposafter = $pdf->getPage();
367 $posyafter = $pdf->GetY();
368 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
369 if ($i == ($nblines - 1)) {
370 $pdf->AddPage(
'',
'',
true);
371 if (!empty($tplidx)) {
372 $pdf->useTemplate($tplidx);
375 $this->
_pagehead($pdf, $object, 0, $outputlangs);
377 $pdf->setPage($pageposafter + 1);
384 $showpricebeforepagebreak = 1;
386 $showpricebeforepagebreak = 0;
390 $pdf->commitTransaction();
392 $posYAfterDescription = $pdf->GetY();
394 $nexY = $pdf->GetY() + 2;
395 $pageposafter = $pdf->getPage();
397 $pdf->setPage($pageposbefore);
398 $pdf->setTopMargin($this->marge_haute);
399 $pdf->setPageOrientation(
'', 1, 0);
402 if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
403 $pdf->setPage($pageposafter);
404 $curY = $tab_top_newpage;
407 $pdf->SetFont(
'',
'', $default_font_size - 1);
410 while ($pagenb < $pageposafter) {
411 $pdf->setPage($pagenb);
413 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
415 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
417 $this->
_pagefoot($pdf, $object, $outputlangs, 1);
419 $pdf->setPage($pagenb);
420 $pdf->setPageOrientation(
'', 1, 0);
422 $this->
_pagehead($pdf, $object, 0, $outputlangs);
424 if (!empty($tplidx)) {
425 $pdf->useTemplate($tplidx);
429 if (isset($object->lines[$i + 1]->pagebreak) && $object->lines[$i + 1]->pagebreak) {
431 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
433 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
435 $this->
_pagefoot($pdf, $object, $outputlangs, 1);
438 if (!empty($tplidx)) {
439 $pdf->useTemplate($tplidx);
449 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
450 $this->
tabSignature($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
451 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
453 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
454 $this->
tabSignature($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
455 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
458 $this->
_pagefoot($pdf, $object, $outputlangs);
459 if (method_exists($pdf,
'AliasNbPages')) {
460 $pdf->AliasNbPages();
465 $pdf->Output($file,
'F');
468 $hookmanager->initHooks(array(
'pdfgeneration'));
469 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs);
471 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
473 $this->error = $hookmanager->error;
474 $this->errors = $hookmanager->errors;
479 $this->result = array(
'fullpath'=>$file);
483 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
487 $this->error = $langs->transnoentities(
"ErrorConstantNotDefined",
"CONTRACT_OUTPUTDIR");
586 protected function _pagehead(&$pdf, $object, $showaddress, $outputlangs, $outputlangsbis =
null, $titlekey =
"Contract")
594 if ($outputlangs->trans(
"DIRECTION") ==
'rtl') {
599 $outputlangs->loadLangs(array(
"main",
"dict",
"contract",
"companies"));
605 $pdf->SetTextColor(0, 0, 60);
606 $pdf->SetFont(
'',
'B', $default_font_size + 3);
610 $posy = $this->marge_haute;
611 $posx = $this->page_largeur - $this->marge_droite - $w;
613 $pdf->SetXY($this->marge_gauche, $posy);
617 if ($this->emetteur->logo) {
618 $logodir = $conf->mycompany->dir_output;
619 if (!empty($conf->mycompany->multidir_output[$object->entity])) {
620 $logodir = $conf->mycompany->multidir_output[$object->entity];
623 $logo = $logodir.
'/logos/thumbs/'.$this->emetteur->logo_small;
625 $logo = $logodir.
'/logos/'.$this->emetteur->logo;
627 if (is_readable($logo)) {
629 $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height);
631 $pdf->SetTextColor(200, 0, 0);
632 $pdf->SetFont(
'',
'B', $default_font_size - 2);
633 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorLogoFileNotFound", $logo), 0,
'L');
634 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorGoToGlobalSetup"), 0,
'L');
637 $text = $this->emetteur->name;
638 $pdf->MultiCell($w, 4, $outputlangs->convToOutputCharset($text), 0, $ltrdirection);
642 $pdf->SetFont(
'',
'B', $default_font_size + 3);
643 $pdf->SetXY($posx, $posy);
644 $pdf->SetTextColor(0, 0, 60);
645 $title = $outputlangs->transnoentities($titlekey);
646 $title .=
' '.$outputlangs->convToOutputCharset($object->ref);
647 if ($object->statut == $object::STATUS_DRAFT) {
648 $pdf->SetTextColor(128, 0, 0);
649 $title .=
' - '.$outputlangs->transnoentities(
"NotValidated");
651 $pdf->MultiCell($w, 3, $title,
'',
'R');
653 $pdf->SetFont(
'',
'B', $default_font_size);
663 $pdf->SetFont(
'',
'', $default_font_size - 1);
666 $pdf->SetXY($posx, $posy);
667 $pdf->SetTextColor(0, 0, 60);
668 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"Date").
" : ".
dol_print_date($object->date_contrat,
"day",
false, $outputlangs,
true),
'',
'R');
670 if (!
getDolGlobalString(
'MAIN_PDF_HIDE_CUSTOMER_CODE') && $object->thirdparty->code_client) {
672 $pdf->SetXY($posx, $posy);
673 $pdf->SetTextColor(0, 0, 60);
674 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerCode").
" : ".$outputlangs->transnoentities($object->thirdparty->code_client),
'',
'R');
679 $carac_emetteur =
'';
681 $arrayidcontact = $object->getIdContact(
'internal',
'INTERREPFOLL');
682 if (count($arrayidcontact) > 0) {
683 $object->fetch_user($arrayidcontact[0]);
684 $labelbeforecontactname = ($outputlangs->transnoentities(
"FromContactName") !=
'FromContactName' ? $outputlangs->transnoentities(
"FromContactName") : $outputlangs->transnoentities(
"Name"));
685 $carac_emetteur .= ($carac_emetteur ?
"\n" :
'').$labelbeforecontactname.
": ".$outputlangs->convToOutputCharset($object->user->getFullName($outputlangs));
687 $carac_emetteur .= (
getDolGlobalInt(
'PDF_SHOW_PHONE_AFTER_USER_CONTACT') && !empty($object->user->office_phone)) ? $object->user->office_phone :
'';
689 $carac_emetteur .= (
getDolGlobalInt(
'PDF_SHOW_EMAIL_AFTER_USER_CONTACT') && !empty($object->user->email)) ? $object->user->email :
'';
691 $carac_emetteur .=
"\n";
694 $carac_emetteur .=
pdf_build_address($outputlangs, $this->emetteur, $object->thirdparty,
'', 0,
'source', $object);
698 $posx = $this->marge_gauche;
700 $posx = $this->page_largeur - $this->marge_droite - 80;
706 $pdf->SetTextColor(0, 0, 0);
707 $pdf->SetFont(
'',
'', $default_font_size - 2);
708 $pdf->SetXY($posx, $posy - 5);
709 $pdf->SetXY($posx, $posy);
710 $pdf->SetFillColor(230, 230, 230);
711 $pdf->MultiCell(82, $hautcadre,
"", 0,
'R', 1);
712 $pdf->SetTextColor(0, 0, 60);
717 $pdf->SetXY($posx + 2, $posy + 3);
718 $pdf->SetTextColor(0, 0, 60);
719 $pdf->SetFont(
'',
'B', $default_font_size);
720 $pdf->MultiCell(80, 4, $outputlangs->convToOutputCharset($this->emetteur->name), 0,
'L');
721 $posy = $pdf->getY();
725 $pdf->SetXY($posx + 2, $posy);
726 $pdf->SetFont(
'',
'', $default_font_size - 1);
727 $pdf->MultiCell(80, 4, $carac_emetteur, 0,
'L');
732 $arrayidcontact = $object->getIdContact(
'external',
'CUSTOMER');
733 if (count($arrayidcontact) > 0) {
735 $result = $object->fetch_contact($arrayidcontact[0]);
738 $this->recipient = $object->thirdparty;
741 if ($usecontact && ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT')))) {
742 $thirdparty = $object->contact;
744 $thirdparty = $object->thirdparty;
750 $carac_client =
pdf_build_address($outputlangs, $this->emetteur, $object->thirdparty, (isset($object->contact) ? $object->contact :
''), $usecontact, $mode, $object);
754 if ($this->page_largeur < 210) {
759 $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
761 $posx = $this->marge_gauche;
766 $pdf->SetTextColor(0, 0, 0);
767 $pdf->SetFont(
'',
'', $default_font_size - 2);
768 $pdf->SetXY($posx + 2, $posy - 5);
769 $pdf->Rect($posx, $posy, $widthrecbox, $hautcadre);
770 $pdf->SetTextColor(0, 0, 0);
774 $pdf->SetXY($posx + 2, $posy + 3);
775 $pdf->SetFont(
'',
'B', $default_font_size);
776 $pdf->MultiCell($widthrecbox, 4, $this->recipient->name, 0, $ltrdirection);
778 $posy = $pdf->getY();
781 $pdf->SetFont(
'',
'', $default_font_size - 1);
782 $pdf->SetXY($posx + 2, $posy);
783 $pdf->MultiCell($widthrecbox, 4, $carac_client, 0, $ltrdirection);
786 $pdf->SetTextColor(0, 0, 0);