156 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
159 global $user, $langs,
$conf,
$mysoc, $db, $hookmanager, $nblines;
161 if (!is_object($outputlangs)) {
162 $outputlangs = $langs;
166 $outputlangs->charset_output =
'ISO-8859-1';
170 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"contracts",
"compta"));
177 global $outputlangsbis;
178 $outputlangsbis =
null;
182 $outputlangsbis->loadLangs(array(
"main",
"dict",
"companies",
"contracts",
"compta"));
185 $nblines = count(
$object->lines);
187 if (
$conf->contract->multidir_output[
$conf->entity]) {
193 $file = $dir.
"/SPECIMEN.pdf";
197 $file = $dir.
"/".$objectref.
".pdf";
202 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir",
"Null dir");
206 if (!file_exists($dir)) {
208 $this->error = $langs->transnoentitiesnoconv(
"ErrorCanNotCreateDir", $dir);
213 if (file_exists($dir)) {
215 if (!is_object($hookmanager)) {
216 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
219 $hookmanager->initHooks(array(
'pdfgeneration'));
220 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
222 $reshook = $hookmanager->executeHooks(
'beforePDFCreation', $parameters,
$object, $action);
225 $nblines = (is_array(
$object->lines) ? count(
$object->lines) : 0);
230 $pdf->setAutoPageBreak(
true, 0);
232 $heightforinfotot = 50;
234 $heightforfooter = $this->marge_basse + 9;
236 $heightforfooter += 6;
239 if (class_exists(
'TCPDF')) {
240 $pdf->setPrintHeader(
false);
241 $pdf->setPrintFooter(
false);
246 $logodir =
$conf->mycompany->dir_output;
247 if (!empty(
$conf->mycompany->multidir_output[
$object->entity ??
$conf->entity])) {
250 $pagecount = $pdf->setSourceFile($logodir.
'/' .
getDolGlobalString(
'MAIN_ADD_PDF_BACKGROUND'));
251 $tplidx = $pdf->importPage(1);
256 $pdf->SetDrawColor(128, 128, 128);
258 $pdf->SetTitle($outputlangs->convToOutputCharset(
$object->ref));
259 $pdf->SetSubject($outputlangs->transnoentities(
"Contract"));
260 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
261 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getAnonymisableFullName($outputlangs)));
262 $pdf->SetKeyWords($outputlangs->convToOutputCharset(
$object->ref).
" ".$outputlangs->transnoentities(
"Contract").
" ".$outputlangs->convToOutputCharset(
$object->thirdparty->name));
264 $pdf->SetCompression(
false);
268 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
272 if (!empty($tplidx)) {
273 $pdf->useTemplate($tplidx);
276 $top_shift = $this->
_pagehead($pdf,
$object, 1, $outputlangs, (is_object($outputlangsbis) ? $outputlangsbis :
null));
277 $pdf->SetFont(
'',
'', $default_font_size - 1);
278 $pdf->MultiCell(0, 3,
'');
279 $pdf->SetTextColor(0, 0, 0);
281 $tab_top = 80 + $this->marge_haute;
282 $tab_top_newpage = (!
getDolGlobalInt(
'MAIN_PDF_DONOTREPEAT_HEAD') ? 32 + $this->marge_haute : $this->marge_haute);
285 $notetoshow = empty(
$object->note_public) ?
'' :
$object->note_public;
288 $extranote = $this->getExtrafieldsInHtml(
$object, $outputlangs);
289 if (!empty($extranote)) {
293 if (!empty($notetoshow)) {
301 $pdf->SetFont(
'',
'', $default_font_size - 1);
302 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top - 1,
dol_htmlentitiesbr($notetoshow), 0, 1);
303 $nexY = $pdf->GetY();
304 $height_note = $nexY - $tab_top;
307 $pdf->SetDrawColor(192, 192, 192);
308 $pdf->RoundedRect($this->marge_gauche, $tab_top - 1, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_note + 2, $this->corner_radius,
'1234',
'D');
310 $tab_top = $nexY + 6;
313 $iniY = $tab_top + 7;
314 $curY = $tab_top + 7;
315 $nexY = $tab_top + 2;
317 $pdf->SetXY($this->marge_gauche, $tab_top);
319 $pdf->MultiCell(0, 2,
'');
321 $nblines = count(
$object->lines);
324 for ($i = 0; $i < $nblines; $i++) {
325 $objectligne =
$object->lines[$i];
327 $valide = $objectligne->id ? 1 : 0;
329 if ($valide > 0 ||
$object->specimen) {
330 $curX = $this->posxdesc - 1;
332 $pdf->SetFont(
'',
'', $default_font_size - 1);
333 $pdf->SetTextColor(0, 0, 0);
335 $pdf->setTopMargin($tab_top_newpage);
336 $pdf->setPageOrientation(
'',
true, $heightforfooter + $heightforfreetext + $heightforinfotot);
337 $pageposbefore = $pdf->getPage();
341 if (!empty($objectligne->date_start)) {
342 $datei =
dol_print_date((
int) $objectligne->date_start,
'day',
false, $outputlangs,
true);
344 $datei = $langs->trans(
"Unknown");
347 if (!empty($objectligne->date_end)) {
348 $durationi =
convertSecondToTime((
int) $objectligne->date_end - (
int) $objectligne->date_start,
'allwithouthour');
349 $datee =
dol_print_date($objectligne->date_end,
'day',
false, $outputlangs,
true);
351 $durationi = $langs->trans(
"Unknown");
352 $datee = $langs->trans(
"Unknown");
355 if (!empty($objectligne->date_start_real)) {
356 $daters =
dol_print_date((
int) $objectligne->date_start_real,
'day',
false, $outputlangs,
true);
358 $daters = $langs->trans(
"Unknown");
361 if (!empty($objectligne->date_end_real)) {
362 $datere =
dol_print_date((
int) $objectligne->date_end_real,
'day',
false, $outputlangs,
true);
364 $datere = $langs->trans(
"Unknown");
367 $txtpredefinedservice = $objectligne->product_ref;
368 if ($objectligne->product_label) {
369 $txtpredefinedservice .=
' - ';
370 $txtpredefinedservice .= $objectligne->product_label;
376 $txt .= $outputlangs->transnoentities(
"Quantity") .
' : <strong>' . $objectligne->qty .
'</strong>';
379 $txt .=
' - ' . $outputlangs->transnoentities(
"UnitPrice") .
' : <strong>' .
price($objectligne->subprice) .
'</strong>';
383 $txt .= $outputlangs->transnoentities(
"DateStartPlannedShort").
" : <strong>".$datei.
"</strong> - ".$outputlangs->transnoentities(
"DateEndPlanned").
" : <strong>".$datee.
'</strong>';
387 $txt .= $outputlangs->transnoentities(
"DateStartRealShort").
" : <strong>".$daters.
'</strong>';
388 if (!empty($objectligne->date_end_real)) {
389 $txt .=
" - ".$outputlangs->transnoentities(
"DateEndRealShort").
" : <strong>".$datere.
'</strong>';
393 $pdf->startTransaction();
395 $pageposafter = $pdf->getPage();
396 if ($pageposafter > $pageposbefore) {
397 $pdf->rollbackTransaction(
true);
398 $pageposafter = $pageposbefore;
400 $pdf->setPageOrientation(
'',
true, $heightforfooter);
402 $pageposafter = $pdf->getPage();
403 $posyafter = $pdf->GetY();
404 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
405 if ($i == ($nblines - 1)) {
406 $pdf->AddPage(
'',
'',
true);
407 if (!empty($tplidx)) {
408 $pdf->useTemplate($tplidx);
413 $pdf->setPage($pageposafter + 1);
420 $showpricebeforepagebreak = 1;
422 $showpricebeforepagebreak = 0;
426 $pdf->commitTransaction();
428 $posYAfterDescription = $pdf->GetY();
430 $nexY = $pdf->GetY() + 2;
431 $pageposafter = $pdf->getPage();
433 $pdf->setPage($pageposbefore);
434 $pdf->setTopMargin($this->marge_haute);
435 $pdf->setPageOrientation(
'',
true, 0);
438 if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
439 $pdf->setPage($pageposafter);
440 $curY = $tab_top_newpage;
443 $pdf->SetFont(
'',
'', $default_font_size - 1);
446 while ($pagenb < $pageposafter) {
447 $pdf->setPage($pagenb);
449 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
451 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
455 $pdf->setPage($pagenb);
456 $pdf->setPageOrientation(
'',
true, 0);
460 if (!empty($tplidx)) {
461 $pdf->useTemplate($tplidx);
465 if (isset(
$object->lines[$i + 1]->pagebreak) &&
$object->lines[$i + 1]->pagebreak) {
467 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
469 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
474 if (!empty($tplidx)) {
475 $pdf->useTemplate($tplidx);
485 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
486 $this->
tabSignature($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
487 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
489 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
490 $this->
tabSignature($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
491 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
495 if (method_exists($pdf,
'AliasNbPages')) {
496 $pdf->AliasNbPages();
502 $termsofsale =
$conf->contract->dir_output.
'/'.$termsofsalefilename;
503 if (!empty(
$conf->contract->multidir_output[
$object->entity ??
$conf->entity])) {
504 $termsofsale =
$conf->contract->multidir_output[
$object->entity ??
$conf->entity].
'/'.$termsofsalefilename;
506 if (file_exists($termsofsale) && is_readable($termsofsale)) {
507 $pagecount = $pdf->setSourceFile($termsofsale);
508 for ($i = 1; $i <= $pagecount; $i++) {
509 $tplIdx = $pdf->importPage($i);
510 if ($tplIdx !==
false) {
511 $s = $pdf->getTemplatesize($tplIdx);
512 $pdf->AddPage($s[
'h'] > $s[
'w'] ?
'P' :
'L');
513 $pdf->useTemplate($tplIdx);
515 setEventMessages(
null, array($termsofsale.
' cannot be added, probably protected PDF'),
'warnings');
523 $pdf->Output($file,
'F');
526 $hookmanager->initHooks(array(
'pdfgeneration'));
527 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
529 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
530 $this->warnings = $hookmanager->warnings;
532 $this->error = $hookmanager->error;
533 $this->errors = $hookmanager->errors;
540 $this->result = array(
'fullpath' => $file);
544 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
548 $this->error = $langs->transnoentities(
"ErrorConstantNotDefined",
"CONTRACT_OUTPUTDIR");
653 protected function _pagehead(&$pdf,
$object, $showaddress, $outputlangs, $outputlangsbis =
null, $titlekey =
"Contract")
661 if ($outputlangs->trans(
"DIRECTION") ==
'rtl') {
666 $outputlangs->loadLangs(array(
"main",
"dict",
"contract",
"companies"));
672 $pdf->SetTextColor(0, 0, 60);
673 $pdf->SetFont(
'',
'B', $default_font_size + 3);
677 $posy = $this->marge_haute;
678 $posx = $this->page_largeur - $this->marge_droite - $w;
680 $pdf->SetXY($this->marge_gauche, $posy);
683 $logodir =
$conf->mycompany->dir_output;
685 if (!empty($multidiroutput)) {
686 $logodir = $multidiroutput;
688 pdf_writeLogoOrCompanyName($pdf, $outputlangs, $this->emetteur, $logodir, $this->marge_gauche, $posy, $w, $default_font_size, $ltrdirection);
690 $pdf->SetFont(
'',
'B', $default_font_size + 3);
691 $pdf->SetXY($posx, $posy);
692 $pdf->SetTextColor(0, 0, 60);
693 $title = $outputlangs->transnoentities($titlekey);
694 $title .=
' '.$outputlangs->convToOutputCharset(
$object->ref);
695 if (
$object->status == $object::STATUS_DRAFT) {
696 $pdf->SetTextColor(128, 0, 0);
697 $title .=
' - '.$outputlangs->transnoentities(
"NotValidated");
699 $pdf->MultiCell($w, 3, $title,
'',
'R');
701 $pdf->SetFont(
'',
'B', $default_font_size);
711 $pdf->SetFont(
'',
'', $default_font_size - 1);
714 $pdf->SetXY($posx, $posy);
715 $pdf->SetTextColor(0, 0, 60);
716 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"Date").
" : ".
dol_print_date(
$object->date_contrat,
"day",
false, $outputlangs,
true),
'',
'R');
720 $pdf->SetXY($posx, $posy);
721 $pdf->SetTextColor(0, 0, 60);
722 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerCode").
" : ".$outputlangs->transnoentities((
string)
$object->thirdparty->code_client),
'',
'R');
727 $pdf->SetXY($posx, $posy);
728 $pdf->SetTextColor(0, 0, 60);
729 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerAccountancyCode").
" : ".$outputlangs->transnoentities((
string)
$object->thirdparty->code_compta_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 :
'';
746 $carac_emetteur .=
"\n";
752 $posy = 32 + $this->marge_haute;
753 $posx = $this->marge_gauche;
755 $posx = $this->page_largeur - $this->marge_droite - 80;
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->RoundedRect($posx, $posy, 82, $hautcadre, $this->corner_radius,
'1234',
'F');
767 $pdf->SetTextColor(0, 0, 60);
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]);
794 if ($usecontact && (
$object->contact->socid !=
$object->thirdparty->id) && (!isset(
$conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
795 $this->recipient =
$object->contact;
797 $this->recipient =
$object->thirdparty;
801 if ($this->recipient instanceof
Contact || $this->recipient instanceof
Societe) {
810 if ($this->page_largeur < 210) {
814 $posy += $top_shift + $this->marge_haute;
815 $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
817 $posx = $this->marge_gauche;
822 $pdf->SetTextColor(0, 0, 0);
823 $pdf->SetFont(
'',
'', $default_font_size - 2);
824 $pdf->SetXY($posx + 2, $posy - 5);
825 $pdf->RoundedRect($posx, $posy, $widthrecbox, $hautcadre, $this->corner_radius,
'1234',
'D');
826 $pdf->SetTextColor(0, 0, 0);
830 $pdf->SetXY($posx + 2, $posy + 3);
831 $pdf->SetFont(
'',
'B', $default_font_size);
832 $pdf->MultiCell($widthrecbox, 4, $recipientname, 0, $ltrdirection);
834 $posy = $pdf->getY();
837 $pdf->SetFont(
'',
'', $default_font_size - 1);
838 $pdf->SetXY($posx + 2, $posy);
840 $pdf->MultiCell($widthrecbox, 4, $carac_client, 0, $ltrdirection);
843 $pdf->SetTextColor(0, 0, 0);