150 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
153 global $user, $langs,
$conf,
$mysoc, $db, $hookmanager, $nblines;
155 if (!is_object($outputlangs)) {
156 $outputlangs = $langs;
160 $outputlangs->charset_output =
'ISO-8859-1';
164 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"contracts",
"compta"));
171 global $outputlangsbis;
172 $outputlangsbis =
null;
176 $outputlangsbis->loadLangs(array(
"main",
"dict",
"companies",
"contracts",
"compta"));
179 $nblines = count(
$object->lines);
181 if (
$conf->contract->multidir_output[
$conf->entity]) {
187 $file = $dir.
"/SPECIMEN.pdf";
191 $file = $dir.
"/".$objectref.
".pdf";
196 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir",
"Null dir");
200 if (!file_exists($dir)) {
202 $this->error = $langs->transnoentitiesnoconv(
"ErrorCanNotCreateDir", $dir);
207 if (file_exists($dir)) {
209 if (!is_object($hookmanager)) {
210 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
213 $hookmanager->initHooks(array(
'pdfgeneration'));
214 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
216 $reshook = $hookmanager->executeHooks(
'beforePDFCreation', $parameters,
$object, $action);
219 $nblines = (is_array(
$object->lines) ? count(
$object->lines) : 0);
224 $pdf->setAutoPageBreak(
true, 0);
226 $heightforinfotot = 50;
228 $heightforfooter = $this->marge_basse + 9;
230 $heightforfooter += 6;
233 if (class_exists(
'TCPDF')) {
234 $pdf->setPrintHeader(
false);
235 $pdf->setPrintFooter(
false);
240 $logodir =
$conf->mycompany->dir_output;
241 if (!empty(
$conf->mycompany->multidir_output[
$object->entity ??
$conf->entity])) {
244 $pagecount = $pdf->setSourceFile($logodir.
'/' .
getDolGlobalString(
'MAIN_ADD_PDF_BACKGROUND'));
245 $tplidx = $pdf->importPage(1);
250 $pdf->SetDrawColor(128, 128, 128);
252 $pdf->SetTitle($outputlangs->convToOutputCharset(
$object->ref));
253 $pdf->SetSubject($outputlangs->transnoentities(
"Contract"));
254 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
255 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getAnonymisableFullName($outputlangs)));
256 $pdf->SetKeyWords($outputlangs->convToOutputCharset(
$object->ref).
" ".$outputlangs->transnoentities(
"Contract").
" ".$outputlangs->convToOutputCharset(
$object->thirdparty->name));
258 $pdf->SetCompression(
false);
262 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
266 if (!empty($tplidx)) {
267 $pdf->useTemplate($tplidx);
270 $top_shift = $this->
_pagehead($pdf,
$object, 1, $outputlangs, (is_object($outputlangsbis) ? $outputlangsbis :
null));
271 $pdf->SetFont(
'',
'', $default_font_size - 1);
272 $pdf->MultiCell(0, 3,
'');
273 $pdf->SetTextColor(0, 0, 0);
276 $tab_top_newpage = (!
getDolGlobalInt(
'MAIN_PDF_DONOTREPEAT_HEAD') ? 42 : 10);
279 $notetoshow = empty(
$object->note_public) ?
'' :
$object->note_public;
282 $extranote = $this->getExtrafieldsInHtml(
$object, $outputlangs);
283 if (!empty($extranote)) {
287 if (!empty($notetoshow)) {
295 $pdf->SetFont(
'',
'', $default_font_size - 1);
296 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top - 1,
dol_htmlentitiesbr($notetoshow), 0, 1);
297 $nexY = $pdf->GetY();
298 $height_note = $nexY - $tab_top;
301 $pdf->SetDrawColor(192, 192, 192);
302 $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');
304 $tab_top = $nexY + 6;
307 $iniY = $tab_top + 7;
308 $curY = $tab_top + 7;
309 $nexY = $tab_top + 2;
311 $pdf->SetXY($this->marge_gauche, $tab_top);
313 $pdf->MultiCell(0, 2,
'');
315 $nblines = count(
$object->lines);
318 for ($i = 0; $i < $nblines; $i++) {
319 $objectligne =
$object->lines[$i];
321 $valide = $objectligne->id ? 1 : 0;
323 if ($valide > 0 ||
$object->specimen) {
324 $curX = $this->posxdesc - 1;
326 $pdf->SetFont(
'',
'', $default_font_size - 1);
327 $pdf->SetTextColor(0, 0, 0);
329 $pdf->setTopMargin($tab_top_newpage);
330 $pdf->setPageOrientation(
'',
true, $heightforfooter + $heightforfreetext + $heightforinfotot);
331 $pageposbefore = $pdf->getPage();
335 if (!empty($objectligne->date_start)) {
336 $datei =
dol_print_date((
int) $objectligne->date_start,
'day',
false, $outputlangs,
true);
338 $datei = $langs->trans(
"Unknown");
341 if (!empty($objectligne->date_end)) {
342 $durationi =
convertSecondToTime((
int) $objectligne->date_end - (
int) $objectligne->date_start,
'allwithouthour');
343 $datee =
dol_print_date($objectligne->date_end,
'day',
false, $outputlangs,
true);
345 $durationi = $langs->trans(
"Unknown");
346 $datee = $langs->trans(
"Unknown");
349 if (!empty($objectligne->date_start_real)) {
350 $daters =
dol_print_date((
int) $objectligne->date_start_real,
'day',
false, $outputlangs,
true);
352 $daters = $langs->trans(
"Unknown");
355 if (!empty($objectligne->date_end_real)) {
356 $datere =
dol_print_date((
int) $objectligne->date_end_real,
'day',
false, $outputlangs,
true);
358 $datere = $langs->trans(
"Unknown");
361 $txtpredefinedservice = $objectligne->product_ref;
362 if ($objectligne->product_label) {
363 $txtpredefinedservice .=
' - ';
364 $txtpredefinedservice .= $objectligne->product_label;
370 $txt .= $outputlangs->transnoentities(
"Quantity") .
' : <strong>' . $objectligne->qty .
'</strong>';
373 $txt .=
' - ' . $outputlangs->transnoentities(
"UnitPrice") .
' : <strong>' .
price($objectligne->subprice) .
'</strong>';
377 $txt .= $outputlangs->transnoentities(
"DateStartPlannedShort").
" : <strong>".$datei.
"</strong> - ".$outputlangs->transnoentities(
"DateEndPlanned").
" : <strong>".$datee.
'</strong>';
381 $txt .= $outputlangs->transnoentities(
"DateStartRealShort").
" : <strong>".$daters.
'</strong>';
382 if (!empty($objectligne->date_end_real)) {
383 $txt .=
" - ".$outputlangs->transnoentities(
"DateEndRealShort").
" : <strong>".$datere.
'</strong>';
387 $pdf->startTransaction();
389 $pageposafter = $pdf->getPage();
390 if ($pageposafter > $pageposbefore) {
391 $pdf->rollbackTransaction(
true);
392 $pageposafter = $pageposbefore;
394 $pdf->setPageOrientation(
'',
true, $heightforfooter);
396 $pageposafter = $pdf->getPage();
397 $posyafter = $pdf->GetY();
398 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
399 if ($i == ($nblines - 1)) {
400 $pdf->AddPage(
'',
'',
true);
401 if (!empty($tplidx)) {
402 $pdf->useTemplate($tplidx);
407 $pdf->setPage($pageposafter + 1);
414 $showpricebeforepagebreak = 1;
416 $showpricebeforepagebreak = 0;
420 $pdf->commitTransaction();
422 $posYAfterDescription = $pdf->GetY();
424 $nexY = $pdf->GetY() + 2;
425 $pageposafter = $pdf->getPage();
427 $pdf->setPage($pageposbefore);
428 $pdf->setTopMargin($this->marge_haute);
429 $pdf->setPageOrientation(
'',
true, 0);
432 if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
433 $pdf->setPage($pageposafter);
434 $curY = $tab_top_newpage;
437 $pdf->SetFont(
'',
'', $default_font_size - 1);
440 while ($pagenb < $pageposafter) {
441 $pdf->setPage($pagenb);
443 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
445 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
449 $pdf->setPage($pagenb);
450 $pdf->setPageOrientation(
'',
true, 0);
454 if (!empty($tplidx)) {
455 $pdf->useTemplate($tplidx);
459 if (isset(
$object->lines[$i + 1]->pagebreak) &&
$object->lines[$i + 1]->pagebreak) {
461 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
463 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
468 if (!empty($tplidx)) {
469 $pdf->useTemplate($tplidx);
479 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
480 $this->
tabSignature($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
481 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
483 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
484 $this->
tabSignature($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
485 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
489 if (method_exists($pdf,
'AliasNbPages')) {
490 $pdf->AliasNbPages();
496 $termsofsale =
$conf->contract->dir_output.
'/'.$termsofsalefilename;
497 if (!empty(
$conf->contract->multidir_output[
$object->entity ??
$conf->entity])) {
498 $termsofsale =
$conf->contract->multidir_output[
$object->entity ??
$conf->entity].
'/'.$termsofsalefilename;
500 if (file_exists($termsofsale) && is_readable($termsofsale)) {
501 $pagecount = $pdf->setSourceFile($termsofsale);
502 for ($i = 1; $i <= $pagecount; $i++) {
503 $tplIdx = $pdf->importPage($i);
504 if ($tplIdx !==
false) {
505 $s = $pdf->getTemplatesize($tplIdx);
506 $pdf->AddPage($s[
'h'] > $s[
'w'] ?
'P' :
'L');
507 $pdf->useTemplate($tplIdx);
509 setEventMessages(
null, array($termsofsale.
' cannot be added, probably protected PDF'),
'warnings');
517 $pdf->Output($file,
'F');
520 $hookmanager->initHooks(array(
'pdfgeneration'));
521 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
523 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
524 $this->warnings = $hookmanager->warnings;
526 $this->error = $hookmanager->error;
527 $this->errors = $hookmanager->errors;
534 $this->result = array(
'fullpath' => $file);
538 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
542 $this->error = $langs->transnoentities(
"ErrorConstantNotDefined",
"CONTRACT_OUTPUTDIR");
647 protected function _pagehead(&$pdf,
$object, $showaddress, $outputlangs, $outputlangsbis =
null, $titlekey =
"Contract")
655 if ($outputlangs->trans(
"DIRECTION") ==
'rtl') {
660 $outputlangs->loadLangs(array(
"main",
"dict",
"contract",
"companies"));
666 $pdf->SetTextColor(0, 0, 60);
667 $pdf->SetFont(
'',
'B', $default_font_size + 3);
671 $posy = $this->marge_haute;
672 $posx = $this->page_largeur - $this->marge_droite - $w;
674 $pdf->SetXY($this->marge_gauche, $posy);
678 if ($this->emetteur->logo) {
679 $logodir =
$conf->mycompany->dir_output;
684 $logo = $logodir.
'/logos/thumbs/'.$this->emetteur->logo_small;
686 $logo = $logodir.
'/logos/'.$this->emetteur->logo;
688 if (is_readable($logo)) {
690 $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height);
692 $pdf->SetTextColor(200, 0, 0);
693 $pdf->SetFont(
'',
'B', $default_font_size - 2);
694 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorLogoFileNotFound", $logo), 0,
'L');
695 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorGoToGlobalSetup"), 0,
'L');
698 $text = $this->emetteur->name;
699 $pdf->MultiCell($w, 4, $outputlangs->convToOutputCharset($text), 0, $ltrdirection);
703 $pdf->SetFont(
'',
'B', $default_font_size + 3);
704 $pdf->SetXY($posx, $posy);
705 $pdf->SetTextColor(0, 0, 60);
706 $title = $outputlangs->transnoentities($titlekey);
707 $title .=
' '.$outputlangs->convToOutputCharset(
$object->ref);
708 if (
$object->status == $object::STATUS_DRAFT) {
709 $pdf->SetTextColor(128, 0, 0);
710 $title .=
' - '.$outputlangs->transnoentities(
"NotValidated");
712 $pdf->MultiCell($w, 3, $title,
'',
'R');
714 $pdf->SetFont(
'',
'B', $default_font_size);
724 $pdf->SetFont(
'',
'', $default_font_size - 1);
727 $pdf->SetXY($posx, $posy);
728 $pdf->SetTextColor(0, 0, 60);
729 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"Date").
" : ".
dol_print_date(
$object->date_contrat,
"day",
false, $outputlangs,
true),
'',
'R');
733 $pdf->SetXY($posx, $posy);
734 $pdf->SetTextColor(0, 0, 60);
735 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerCode").
" : ".$outputlangs->transnoentities((
string)
$object->thirdparty->code_client),
'',
'R');
740 $pdf->SetXY($posx, $posy);
741 $pdf->SetTextColor(0, 0, 60);
742 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerAccountancyCode").
" : ".$outputlangs->transnoentities((
string)
$object->thirdparty->code_compta_client),
'',
'R');
747 $carac_emetteur =
'';
749 $arrayidcontact =
$object->getIdContact(
'internal',
'INTERREPFOLL');
750 if (count($arrayidcontact) > 0) {
751 $object->fetch_user($arrayidcontact[0]);
752 $labelbeforecontactname = ($outputlangs->transnoentities(
"FromContactName") !=
'FromContactName' ? $outputlangs->transnoentities(
"FromContactName") : $outputlangs->transnoentities(
"Name"));
753 $carac_emetteur .= ($carac_emetteur ?
"\n" :
'').$labelbeforecontactname.
": ".$outputlangs->convToOutputCharset(
$object->user->getFullName($outputlangs));
755 $carac_emetteur .= (
getDolGlobalInt(
'PDF_SHOW_PHONE_AFTER_USER_CONTACT') && !empty(
$object->user->office_phone)) ?
$object->user->office_phone :
'';
759 $carac_emetteur .=
"\n";
766 $posx = $this->marge_gauche;
768 $posx = $this->page_largeur - $this->marge_droite - 80;
774 $pdf->SetTextColor(0, 0, 0);
775 $pdf->SetFont(
'',
'', $default_font_size - 2);
776 $pdf->SetXY($posx, $posy - 5);
777 $pdf->SetXY($posx, $posy);
778 $pdf->SetFillColor(230, 230, 230);
779 $pdf->RoundedRect($posx, $posy, 82, $hautcadre, $this->corner_radius,
'1234',
'F');
780 $pdf->SetTextColor(0, 0, 60);
785 $pdf->SetXY($posx + 2, $posy + 3);
786 $pdf->SetTextColor(0, 0, 60);
787 $pdf->SetFont(
'',
'B', $default_font_size);
788 $pdf->MultiCell(80, 4, $outputlangs->convToOutputCharset($this->emetteur->name), 0,
'L');
789 $posy = $pdf->getY();
793 $pdf->SetXY($posx + 2, $posy);
794 $pdf->SetFont(
'',
'', $default_font_size - 1);
795 $pdf->MultiCell(80, 4, $carac_emetteur, 0,
'L');
800 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
801 if (count($arrayidcontact) > 0) {
803 $result =
$object->fetch_contact($arrayidcontact[0]);
807 if ($usecontact && (
$object->contact->socid !=
$object->thirdparty->id) && (!isset(
$conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
808 $this->recipient =
$object->contact;
810 $this->recipient =
$object->thirdparty;
814 if ($this->recipient instanceof
Contact || $this->recipient instanceof
Societe) {
823 if ($this->page_largeur < 210) {
828 $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
830 $posx = $this->marge_gauche;
835 $pdf->SetTextColor(0, 0, 0);
836 $pdf->SetFont(
'',
'', $default_font_size - 2);
837 $pdf->SetXY($posx + 2, $posy - 5);
838 $pdf->RoundedRect($posx, $posy, $widthrecbox, $hautcadre, $this->corner_radius,
'1234',
'D');
839 $pdf->SetTextColor(0, 0, 0);
843 $pdf->SetXY($posx + 2, $posy + 3);
844 $pdf->SetFont(
'',
'B', $default_font_size);
845 $pdf->MultiCell($widthrecbox, 4, $recipientname, 0, $ltrdirection);
847 $posy = $pdf->getY();
850 $pdf->SetFont(
'',
'', $default_font_size - 1);
851 $pdf->SetXY($posx + 2, $posy);
853 $pdf->MultiCell($widthrecbox, 4, $carac_client, 0, $ltrdirection);
856 $pdf->SetTextColor(0, 0, 0);