148 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
151 global $user, $langs, $conf, $mysoc, $db, $hookmanager, $nblines;
153 if (!is_object($outputlangs)) {
154 $outputlangs = $langs;
158 $outputlangs->charset_output =
'ISO-8859-1';
162 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"contracts",
"compta"));
169 global $outputlangsbis;
170 $outputlangsbis =
null;
172 $outputlangsbis =
new Translate(
'', $conf);
174 $outputlangsbis->loadLangs(array(
"main",
"dict",
"companies",
"contracts",
"compta"));
177 $nblines = count(
$object->lines);
179 if ($conf->contract->multidir_output[$conf->entity]) {
185 $file = $dir.
"/SPECIMEN.pdf";
189 $file = $dir.
"/".$objectref.
".pdf";
192 if (!file_exists($dir)) {
194 $this->error = $langs->transnoentitiesnoconv(
"ErrorCanNotCreateDir", $dir);
199 if (file_exists($dir)) {
201 if (!is_object($hookmanager)) {
202 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
205 $hookmanager->initHooks(array(
'pdfgeneration'));
206 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
208 $reshook = $hookmanager->executeHooks(
'beforePDFCreation', $parameters,
$object, $action);
211 $nblines = (is_array(
$object->lines) ? count(
$object->lines) : 0);
216 $pdf->SetAutoPageBreak(1, 0);
218 $heightforinfotot = 50;
220 $heightforfooter = $this->marge_basse + 9;
222 $heightforfooter += 6;
225 if (class_exists(
'TCPDF')) {
226 $pdf->setPrintHeader(
false);
227 $pdf->setPrintFooter(
false);
232 $logodir = $conf->mycompany->dir_output;
233 if (!empty($conf->mycompany->multidir_output[
$object->entity])) {
234 $logodir = $conf->mycompany->multidir_output[
$object->entity];
236 $pagecount = $pdf->setSourceFile($logodir.
'/' .
getDolGlobalString(
'MAIN_ADD_PDF_BACKGROUND'));
237 $tplidx = $pdf->importPage(1);
242 $pdf->SetDrawColor(128, 128, 128);
244 $pdf->SetTitle($outputlangs->convToOutputCharset(
$object->ref));
245 $pdf->SetSubject($outputlangs->transnoentities(
"Contract"));
246 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
247 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
248 $pdf->SetKeyWords($outputlangs->convToOutputCharset(
$object->ref).
" ".$outputlangs->transnoentities(
"Contract").
" ".$outputlangs->convToOutputCharset(
$object->thirdparty->name));
250 $pdf->SetCompression(
false);
254 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
258 if (!empty($tplidx)) {
259 $pdf->useTemplate($tplidx);
262 $top_shift = $this->
_pagehead($pdf,
$object, 1, $outputlangs, (is_object($outputlangsbis) ? $outputlangsbis :
null));
263 $pdf->SetFont(
'',
'', $default_font_size - 1);
264 $pdf->MultiCell(0, 3,
'');
265 $pdf->SetTextColor(0, 0, 0);
268 $tab_top_newpage = (!
getDolGlobalInt(
'MAIN_PDF_DONOTREPEAT_HEAD') ? 42 : 10);
271 if (!empty(
$object->note_public)) {
274 $pdf->SetFont(
'',
'', $default_font_size - 1);
276 $nexY = $pdf->GetY();
277 $height_note = $nexY - $tab_top;
280 $pdf->SetDrawColor(192, 192, 192);
281 $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');
283 $tab_top = $nexY + 6;
286 $iniY = $tab_top + 7;
287 $curY = $tab_top + 7;
288 $nexY = $tab_top + 2;
290 $pdf->SetXY($this->marge_gauche, $tab_top);
292 $pdf->MultiCell(0, 2,
'');
294 $nblines = count(
$object->lines);
297 for ($i = 0; $i < $nblines; $i++) {
298 $objectligne =
$object->lines[$i];
300 $valide = $objectligne->id ? 1 : 0;
302 if ($valide > 0 ||
$object->specimen) {
303 $curX = $this->posxdesc - 1;
305 $pdf->SetFont(
'',
'', $default_font_size - 1);
306 $pdf->SetTextColor(0, 0, 0);
308 $pdf->setTopMargin($tab_top_newpage);
309 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext + $heightforinfotot);
310 $pageposbefore = $pdf->getPage();
314 if (!empty($objectligne->date_start)) {
315 $datei =
dol_print_date((
int) $objectligne->date_start,
'day',
false, $outputlangs,
true);
317 $datei = $langs->trans(
"Unknown");
320 if (!empty($objectligne->date_end)) {
321 $durationi =
convertSecondToTime((
int) $objectligne->date_end - (
int) $objectligne->date_start,
'allwithouthour');
322 $datee =
dol_print_date($objectligne->date_end,
'day',
false, $outputlangs,
true);
324 $durationi = $langs->trans(
"Unknown");
325 $datee = $langs->trans(
"Unknown");
328 if (!empty($objectligne->date_start_real)) {
329 $daters =
dol_print_date((
int) $objectligne->date_start_real,
'day',
false, $outputlangs,
true);
331 $daters = $langs->trans(
"Unknown");
334 if (!empty($objectligne->date_end_real)) {
335 $datere =
dol_print_date((
int) $objectligne->date_end_real,
'day',
false, $outputlangs,
true);
337 $datere = $langs->trans(
"Unknown");
340 $txtpredefinedservice = $objectligne->product_ref;
341 if ($objectligne->product_label) {
342 $txtpredefinedservice .=
' - ';
343 $txtpredefinedservice .= $objectligne->product_label;
349 $txt .= $outputlangs->transnoentities(
"Quantity") .
' : <strong>' . $objectligne->qty .
'</strong>';
352 $txt .=
' - ' . $outputlangs->transnoentities(
"UnitPrice") .
' : <strong>' .
price($objectligne->subprice) .
'</strong>';
356 $txt .= $outputlangs->transnoentities(
"DateStartPlannedShort").
" : <strong>".$datei.
"</strong> - ".$outputlangs->transnoentities(
"DateEndPlanned").
" : <strong>".$datee.
'</strong>';
360 $txt .= $outputlangs->transnoentities(
"DateStartRealShort").
" : <strong>".$daters.
'</strong>';
361 if (!empty($objectligne->date_end_real)) {
362 $txt .=
" - ".$outputlangs->transnoentities(
"DateEndRealShort").
" : <strong>".$datere.
'</strong>';
366 $pdf->startTransaction();
368 $pageposafter = $pdf->getPage();
369 if ($pageposafter > $pageposbefore) {
370 $pdf->rollbackTransaction(
true);
371 $pageposafter = $pageposbefore;
373 $pdf->setPageOrientation(
'', 1, $heightforfooter);
375 $pageposafter = $pdf->getPage();
376 $posyafter = $pdf->GetY();
377 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
378 if ($i == ($nblines - 1)) {
379 $pdf->AddPage(
'',
'',
true);
380 if (!empty($tplidx)) {
381 $pdf->useTemplate($tplidx);
386 $pdf->setPage($pageposafter + 1);
393 $showpricebeforepagebreak = 1;
395 $showpricebeforepagebreak = 0;
399 $pdf->commitTransaction();
401 $posYAfterDescription = $pdf->GetY();
403 $nexY = $pdf->GetY() + 2;
404 $pageposafter = $pdf->getPage();
406 $pdf->setPage($pageposbefore);
407 $pdf->setTopMargin($this->marge_haute);
408 $pdf->setPageOrientation(
'', 1, 0);
411 if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
412 $pdf->setPage($pageposafter);
413 $curY = $tab_top_newpage;
416 $pdf->SetFont(
'',
'', $default_font_size - 1);
419 while ($pagenb < $pageposafter) {
420 $pdf->setPage($pagenb);
422 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
424 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
428 $pdf->setPage($pagenb);
429 $pdf->setPageOrientation(
'', 1, 0);
433 if (!empty($tplidx)) {
434 $pdf->useTemplate($tplidx);
438 if (isset(
$object->lines[$i + 1]->pagebreak) &&
$object->lines[$i + 1]->pagebreak) {
440 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
442 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
447 if (!empty($tplidx)) {
448 $pdf->useTemplate($tplidx);
458 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
459 $this->
tabSignature($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
460 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
462 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
463 $this->
tabSignature($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
464 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
468 if (method_exists($pdf,
'AliasNbPages')) {
469 $pdf->AliasNbPages();
474 $pdf->Output($file,
'F');
477 $hookmanager->initHooks(array(
'pdfgeneration'));
478 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
480 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
482 $this->error = $hookmanager->error;
483 $this->errors = $hookmanager->errors;
488 $this->result = array(
'fullpath' => $file);
492 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
496 $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;
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');
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 $pdf->SetXY($posx, $posy);
694 $pdf->SetTextColor(0, 0, 60);
695 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerAccountancyCode").
" : ".$outputlangs->transnoentities(
$object->thirdparty->code_compta_client),
'',
'R');
700 $carac_emetteur =
'';
702 $arrayidcontact =
$object->getIdContact(
'internal',
'INTERREPFOLL');
703 if (count($arrayidcontact) > 0) {
704 $object->fetch_user($arrayidcontact[0]);
705 $labelbeforecontactname = ($outputlangs->transnoentities(
"FromContactName") !=
'FromContactName' ? $outputlangs->transnoentities(
"FromContactName") : $outputlangs->transnoentities(
"Name"));
706 $carac_emetteur .= ($carac_emetteur ?
"\n" :
'').$labelbeforecontactname.
": ".$outputlangs->convToOutputCharset(
$object->user->getFullName($outputlangs));
708 $carac_emetteur .= (
getDolGlobalInt(
'PDF_SHOW_PHONE_AFTER_USER_CONTACT') && !empty(
$object->user->office_phone)) ?
$object->user->office_phone :
'';
712 $carac_emetteur .=
"\n";
719 $posx = $this->marge_gauche;
721 $posx = $this->page_largeur - $this->marge_droite - 80;
727 $pdf->SetTextColor(0, 0, 0);
728 $pdf->SetFont(
'',
'', $default_font_size - 2);
729 $pdf->SetXY($posx, $posy - 5);
730 $pdf->SetXY($posx, $posy);
731 $pdf->SetFillColor(230, 230, 230);
732 $pdf->RoundedRect($posx, $posy, 82, $hautcadre, $this->corner_radius,
'1234',
'F');
733 $pdf->SetTextColor(0, 0, 60);
738 $pdf->SetXY($posx + 2, $posy + 3);
739 $pdf->SetTextColor(0, 0, 60);
740 $pdf->SetFont(
'',
'B', $default_font_size);
741 $pdf->MultiCell(80, 4, $outputlangs->convToOutputCharset($this->emetteur->name), 0,
'L');
742 $posy = $pdf->getY();
746 $pdf->SetXY($posx + 2, $posy);
747 $pdf->SetFont(
'',
'', $default_font_size - 1);
748 $pdf->MultiCell(80, 4, $carac_emetteur, 0,
'L');
753 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
754 if (count($arrayidcontact) > 0) {
756 $result =
$object->fetch_contact($arrayidcontact[0]);
759 $this->recipient =
$object->thirdparty;
762 if ($usecontact && (
$object->contact->socid !=
$object->thirdparty->id) && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
763 $thirdparty =
$object->contact;
765 $thirdparty =
$object->thirdparty;
775 if ($this->page_largeur < 210) {
780 $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
782 $posx = $this->marge_gauche;
787 $pdf->SetTextColor(0, 0, 0);
788 $pdf->SetFont(
'',
'', $default_font_size - 2);
789 $pdf->SetXY($posx + 2, $posy - 5);
790 $pdf->RoundedRect($posx, $posy, $widthrecbox, $hautcadre, $this->corner_radius,
'1234',
'D');
791 $pdf->SetTextColor(0, 0, 0);
795 $pdf->SetXY($posx + 2, $posy + 3);
796 $pdf->SetFont(
'',
'B', $default_font_size);
797 $pdf->MultiCell($widthrecbox, 4, $this->recipient->name, 0, $ltrdirection);
799 $posy = $pdf->getY();
802 $pdf->SetFont(
'',
'', $default_font_size - 1);
803 $pdf->SetXY($posx + 2, $posy);
805 $pdf->MultiCell($widthrecbox, 4, $carac_client, 0, $ltrdirection);
808 $pdf->SetTextColor(0, 0, 0);