142 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
145 global $user, $langs, $conf, $mysoc, $db, $hookmanager, $nblines;
147 if (!is_object($outputlangs)) {
148 $outputlangs = $langs;
152 $outputlangs->charset_output =
'ISO-8859-1';
156 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"contracts"));
163 global $outputlangsbis;
164 $outputlangsbis =
null;
166 $outputlangsbis =
new Translate(
'', $conf);
168 $outputlangsbis->loadLangs(array(
"main",
"dict",
"companies",
"bills",
"products",
"orders",
"deliveries"));
171 $nblines = count(
$object->lines);
173 if ($conf->contract->multidir_output[$conf->entity]) {
179 $file = $dir.
"/SPECIMEN.pdf";
183 $file = $dir.
"/".$objectref.
".pdf";
186 if (!file_exists($dir)) {
188 $this->error = $langs->transnoentitiesnoconv(
"ErrorCanNotCreateDir", $dir);
193 if (file_exists($dir)) {
195 if (!is_object($hookmanager)) {
196 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
199 $hookmanager->initHooks(array(
'pdfgeneration'));
200 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
202 $reshook = $hookmanager->executeHooks(
'beforePDFCreation', $parameters,
$object, $action);
205 $nblines = (is_array(
$object->lines) ? count(
$object->lines) : 0);
210 $pdf->SetAutoPageBreak(1, 0);
212 $heightforinfotot = 50;
214 $heightforfooter = $this->marge_basse + 9;
216 $heightforfooter += 6;
219 if (class_exists(
'TCPDF')) {
220 $pdf->setPrintHeader(
false);
221 $pdf->setPrintFooter(
false);
226 $logodir = $conf->mycompany->dir_output;
227 if (!empty($conf->mycompany->multidir_output[
$object->entity])) {
228 $logodir = $conf->mycompany->multidir_output[
$object->entity];
230 $pagecount = $pdf->setSourceFile($logodir.
'/' .
getDolGlobalString(
'MAIN_ADD_PDF_BACKGROUND'));
231 $tplidx = $pdf->importPage(1);
236 $pdf->SetDrawColor(128, 128, 128);
238 $pdf->SetTitle($outputlangs->convToOutputCharset(
$object->ref));
239 $pdf->SetSubject($outputlangs->transnoentities(
"Contract"));
240 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
241 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
242 $pdf->SetKeyWords($outputlangs->convToOutputCharset(
$object->ref).
" ".$outputlangs->transnoentities(
"Contract").
" ".$outputlangs->convToOutputCharset(
$object->thirdparty->name));
244 $pdf->SetCompression(
false);
248 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
252 if (!empty($tplidx)) {
253 $pdf->useTemplate($tplidx);
256 $top_shift = $this->
_pagehead($pdf,
$object, 1, $outputlangs, (is_object($outputlangsbis) ? $outputlangsbis :
null));
257 $pdf->SetFont(
'',
'', $default_font_size - 1);
258 $pdf->MultiCell(0, 3,
'');
259 $pdf->SetTextColor(0, 0, 0);
262 $tab_top_newpage = (!
getDolGlobalInt(
'MAIN_PDF_DONOTREPEAT_HEAD') ? 42 : 10);
265 $notetoshow = empty(
$object->note_public) ?
'' :
$object->note_public;
268 $extranote = $this->getExtrafieldsInHtml(
$object, $outputlangs);
269 if (!empty($extranote)) {
273 if (!empty($notetoshow)) {
281 $pdf->SetFont(
'',
'', $default_font_size - 1);
282 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top - 1,
dol_htmlentitiesbr($notetoshow), 0, 1);
283 $nexY = $pdf->GetY();
284 $height_note = $nexY - $tab_top;
287 $pdf->SetDrawColor(192, 192, 192);
288 $pdf->Rect($this->marge_gauche, $tab_top - 1, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_note + 1);
290 $tab_top = $nexY + 6;
293 $iniY = $tab_top + 7;
294 $curY = $tab_top + 7;
295 $nexY = $tab_top + 2;
297 $pdf->SetXY($this->marge_gauche, $tab_top);
299 $pdf->MultiCell(0, 2,
'');
301 $nblines = count(
$object->lines);
304 for ($i = 0; $i < $nblines; $i++) {
305 $objectligne =
$object->lines[$i];
307 $valide = $objectligne->id ? 1 : 0;
309 if ($valide > 0 ||
$object->specimen) {
310 $curX = $this->posxdesc - 1;
312 $pdf->SetFont(
'',
'', $default_font_size - 1);
313 $pdf->SetTextColor(0, 0, 0);
315 $pdf->setTopMargin($tab_top_newpage);
316 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext + $heightforinfotot);
317 $pageposbefore = $pdf->getPage();
321 if (!empty($objectligne->date_start)) {
322 $datei =
dol_print_date((
int) $objectligne->date_start,
'day',
false, $outputlangs,
true);
324 $datei = $langs->trans(
"Unknown");
327 if (!empty($objectligne->date_end)) {
328 $durationi =
convertSecondToTime((
int) $objectligne->date_end - (
int) $objectligne->date_start,
'allwithouthour');
329 $datee =
dol_print_date($objectligne->date_end,
'day',
false, $outputlangs,
true);
331 $durationi = $langs->trans(
"Unknown");
332 $datee = $langs->trans(
"Unknown");
335 if (!empty($objectligne->date_start_real)) {
336 $daters =
dol_print_date((
int) $objectligne->date_start_real,
'day',
false, $outputlangs,
true);
338 $daters = $langs->trans(
"Unknown");
341 if (!empty($objectligne->date_end_real)) {
342 $datere =
dol_print_date((
int) $objectligne->date_end_real,
'day',
false, $outputlangs,
true);
344 $datere = $langs->trans(
"Unknown");
347 $txtpredefinedservice = $objectligne->product_ref;
348 if ($objectligne->product_label) {
349 $txtpredefinedservice .=
' - ';
350 $txtpredefinedservice .= $objectligne->product_label;
356 $txt .= $outputlangs->transnoentities(
"Quantity") .
' : <strong>' . $objectligne->qty .
'</strong>';
359 $txt .=
' - ' . $outputlangs->transnoentities(
"UnitPrice") .
' : <strong>' .
price($objectligne->subprice) .
'</strong>';
363 $txt .= $outputlangs->transnoentities(
"DateStartPlannedShort").
" : <strong>".$datei.
"</strong> - ".$outputlangs->transnoentities(
"DateEndPlanned").
" : <strong>".$datee.
'</strong>';
367 $txt .= $outputlangs->transnoentities(
"DateStartRealShort").
" : <strong>".$daters.
'</strong>';
368 if (!empty($objectligne->date_end_real)) {
369 $txt .=
" - ".$outputlangs->transnoentities(
"DateEndRealShort").
" : <strong>".$datere.
'</strong>';
373 $pdf->startTransaction();
375 $pageposafter = $pdf->getPage();
376 if ($pageposafter > $pageposbefore) {
377 $pdf->rollbackTransaction(
true);
378 $pageposafter = $pageposbefore;
380 $pdf->setPageOrientation(
'', 1, $heightforfooter);
382 $pageposafter = $pdf->getPage();
383 $posyafter = $pdf->GetY();
384 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
385 if ($i == ($nblines - 1)) {
386 $pdf->AddPage(
'',
'',
true);
387 if (!empty($tplidx)) {
388 $pdf->useTemplate($tplidx);
393 $pdf->setPage($pageposafter + 1);
400 $showpricebeforepagebreak = 1;
402 $showpricebeforepagebreak = 0;
406 $pdf->commitTransaction();
408 $posYAfterDescription = $pdf->GetY();
410 $nexY = $pdf->GetY() + 2;
411 $pageposafter = $pdf->getPage();
413 $pdf->setPage($pageposbefore);
414 $pdf->setTopMargin($this->marge_haute);
415 $pdf->setPageOrientation(
'', 1, 0);
418 if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
419 $pdf->setPage($pageposafter);
420 $curY = $tab_top_newpage;
423 $pdf->SetFont(
'',
'', $default_font_size - 1);
426 while ($pagenb < $pageposafter) {
427 $pdf->setPage($pagenb);
429 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
431 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
435 $pdf->setPage($pagenb);
436 $pdf->setPageOrientation(
'', 1, 0);
440 if (!empty($tplidx)) {
441 $pdf->useTemplate($tplidx);
445 if (isset(
$object->lines[$i + 1]->pagebreak) &&
$object->lines[$i + 1]->pagebreak) {
447 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
449 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
454 if (!empty($tplidx)) {
455 $pdf->useTemplate($tplidx);
465 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
466 $this->
tabSignature($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
467 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
469 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
470 $this->
tabSignature($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
471 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
475 if (method_exists($pdf,
'AliasNbPages')) {
476 $pdf->AliasNbPages();
481 $pdf->Output($file,
'F');
484 $hookmanager->initHooks(array(
'pdfgeneration'));
485 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
487 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
489 $this->error = $hookmanager->error;
490 $this->errors = $hookmanager->errors;
495 $this->result = array(
'fullpath' => $file);
499 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
503 $this->error = $langs->transnoentities(
"ErrorConstantNotDefined",
"CONTRACT_OUTPUTDIR");
602 protected function _pagehead(&$pdf,
$object, $showaddress, $outputlangs, $outputlangsbis =
null, $titlekey =
"Contract")
610 if ($outputlangs->trans(
"DIRECTION") ==
'rtl') {
615 $outputlangs->loadLangs(array(
"main",
"dict",
"contract",
"companies"));
621 $pdf->SetTextColor(0, 0, 60);
622 $pdf->SetFont(
'',
'B', $default_font_size + 3);
626 $posy = $this->marge_haute;
627 $posx = $this->page_largeur - $this->marge_droite - $w;
629 $pdf->SetXY($this->marge_gauche, $posy);
633 if ($this->emetteur->logo) {
634 $logodir = $conf->mycompany->dir_output;
639 $logo = $logodir.
'/logos/thumbs/'.$this->emetteur->logo_small;
641 $logo = $logodir.
'/logos/'.$this->emetteur->logo;
643 if (is_readable($logo)) {
645 $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height);
647 $pdf->SetTextColor(200, 0, 0);
648 $pdf->SetFont(
'',
'B', $default_font_size - 2);
649 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorLogoFileNotFound", $logo), 0,
'L');
650 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorGoToGlobalSetup"), 0,
'L');
653 $text = $this->emetteur->name;
654 $pdf->MultiCell($w, 4, $outputlangs->convToOutputCharset($text), 0, $ltrdirection);
658 $pdf->SetFont(
'',
'B', $default_font_size + 3);
659 $pdf->SetXY($posx, $posy);
660 $pdf->SetTextColor(0, 0, 60);
661 $title = $outputlangs->transnoentities($titlekey);
662 $title .=
' '.$outputlangs->convToOutputCharset(
$object->ref);
663 if (
$object->statut == $object::STATUS_DRAFT) {
664 $pdf->SetTextColor(128, 0, 0);
665 $title .=
' - '.$outputlangs->transnoentities(
"NotValidated");
667 $pdf->MultiCell($w, 3, $title,
'',
'R');
669 $pdf->SetFont(
'',
'B', $default_font_size);
679 $pdf->SetFont(
'',
'', $default_font_size - 1);
682 $pdf->SetXY($posx, $posy);
683 $pdf->SetTextColor(0, 0, 60);
684 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"Date").
" : ".
dol_print_date(
$object->date_contrat,
"day",
false, $outputlangs,
true),
'',
'R');
688 $pdf->SetXY($posx, $posy);
689 $pdf->SetTextColor(0, 0, 60);
690 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerCode").
" : ".$outputlangs->transnoentities(
$object->thirdparty->code_client),
'',
'R');
695 $carac_emetteur =
'';
697 $arrayidcontact =
$object->getIdContact(
'internal',
'INTERREPFOLL');
698 if (count($arrayidcontact) > 0) {
699 $object->fetch_user($arrayidcontact[0]);
700 $labelbeforecontactname = ($outputlangs->transnoentities(
"FromContactName") !=
'FromContactName' ? $outputlangs->transnoentities(
"FromContactName") : $outputlangs->transnoentities(
"Name"));
701 $carac_emetteur .= ($carac_emetteur ?
"\n" :
'').$labelbeforecontactname.
": ".$outputlangs->convToOutputCharset(
$object->user->getFullName($outputlangs));
703 $carac_emetteur .= (
getDolGlobalInt(
'PDF_SHOW_PHONE_AFTER_USER_CONTACT') && !empty(
$object->user->office_phone)) ?
$object->user->office_phone :
'';
707 $carac_emetteur .=
"\n";
714 $posx = $this->marge_gauche;
716 $posx = $this->page_largeur - $this->marge_droite - 80;
722 $pdf->SetTextColor(0, 0, 0);
723 $pdf->SetFont(
'',
'', $default_font_size - 2);
724 $pdf->SetXY($posx, $posy - 5);
725 $pdf->SetXY($posx, $posy);
726 $pdf->SetFillColor(230, 230, 230);
727 $pdf->MultiCell(82, $hautcadre,
"", 0,
'R', 1);
728 $pdf->SetTextColor(0, 0, 60);
733 $pdf->SetXY($posx + 2, $posy + 3);
734 $pdf->SetTextColor(0, 0, 60);
735 $pdf->SetFont(
'',
'B', $default_font_size);
736 $pdf->MultiCell(80, 4, $outputlangs->convToOutputCharset($this->emetteur->name), 0,
'L');
737 $posy = $pdf->getY();
741 $pdf->SetXY($posx + 2, $posy);
742 $pdf->SetFont(
'',
'', $default_font_size - 1);
743 $pdf->MultiCell(80, 4, $carac_emetteur, 0,
'L');
748 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
749 if (count($arrayidcontact) > 0) {
751 $result =
$object->fetch_contact($arrayidcontact[0]);
754 $this->recipient =
$object->thirdparty;
757 if ($usecontact && (
$object->contact->socid !=
$object->thirdparty->id) && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
758 $thirdparty =
$object->contact;
760 $thirdparty =
$object->thirdparty;
770 if ($this->page_largeur < 210) {
775 $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
777 $posx = $this->marge_gauche;
782 $pdf->SetTextColor(0, 0, 0);
783 $pdf->SetFont(
'',
'', $default_font_size - 2);
784 $pdf->SetXY($posx + 2, $posy - 5);
785 $pdf->Rect($posx, $posy, $widthrecbox, $hautcadre);
786 $pdf->SetTextColor(0, 0, 0);
790 $pdf->SetXY($posx + 2, $posy + 3);
791 $pdf->SetFont(
'',
'B', $default_font_size);
792 $pdf->MultiCell($widthrecbox, 4, $this->recipient->name, 0, $ltrdirection);
794 $posy = $pdf->getY();
797 $pdf->SetFont(
'',
'', $default_font_size - 1);
798 $pdf->SetXY($posx + 2, $posy);
800 $pdf->MultiCell($widthrecbox, 4, $carac_client, 0, $ltrdirection);
803 $pdf->SetTextColor(0, 0, 0);