141 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
144 global $user, $langs, $conf, $mysoc, $db, $hookmanager, $nblines;
146 if (!is_object($outputlangs)) {
147 $outputlangs = $langs;
151 $outputlangs->charset_output =
'ISO-8859-1';
155 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"contracts"));
162 global $outputlangsbis;
163 $outputlangsbis =
null;
165 $outputlangsbis =
new Translate(
'', $conf);
167 $outputlangsbis->loadLangs(array(
"main",
"dict",
"companies",
"bills",
"products",
"orders",
"deliveries"));
170 $nblines = count(
$object->lines);
172 if ($conf->contract->multidir_output[$conf->entity]) {
178 $file = $dir.
"/SPECIMEN.pdf";
182 $file = $dir.
"/".$objectref.
".pdf";
185 if (!file_exists($dir)) {
187 $this->error = $langs->transnoentitiesnoconv(
"ErrorCanNotCreateDir", $dir);
192 if (file_exists($dir)) {
194 if (!is_object($hookmanager)) {
195 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
198 $hookmanager->initHooks(array(
'pdfgeneration'));
199 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
201 $reshook = $hookmanager->executeHooks(
'beforePDFCreation', $parameters,
$object, $action);
204 $nblines = (is_array(
$object->lines) ? count(
$object->lines) : 0);
209 $pdf->SetAutoPageBreak(1, 0);
211 $heightforinfotot = 50;
213 $heightforfooter = $this->marge_basse + 9;
215 $heightforfooter += 6;
218 if (class_exists(
'TCPDF')) {
219 $pdf->setPrintHeader(
false);
220 $pdf->setPrintFooter(
false);
225 $logodir = $conf->mycompany->dir_output;
226 if (!empty($conf->mycompany->multidir_output[
$object->entity])) {
227 $logodir = $conf->mycompany->multidir_output[
$object->entity];
229 $pagecount = $pdf->setSourceFile($logodir.
'/' .
getDolGlobalString(
'MAIN_ADD_PDF_BACKGROUND'));
230 $tplidx = $pdf->importPage(1);
235 $pdf->SetDrawColor(128, 128, 128);
237 $pdf->SetTitle($outputlangs->convToOutputCharset(
$object->ref));
238 $pdf->SetSubject($outputlangs->transnoentities(
"Contract"));
239 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
240 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
241 $pdf->SetKeyWords($outputlangs->convToOutputCharset(
$object->ref).
" ".$outputlangs->transnoentities(
"Contract").
" ".$outputlangs->convToOutputCharset(
$object->thirdparty->name));
243 $pdf->SetCompression(
false);
247 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
251 if (!empty($tplidx)) {
252 $pdf->useTemplate($tplidx);
255 $top_shift = $this->
_pagehead($pdf,
$object, 1, $outputlangs, (is_object($outputlangsbis) ? $outputlangsbis :
null));
256 $pdf->SetFont(
'',
'', $default_font_size - 1);
257 $pdf->MultiCell(0, 3,
'');
258 $pdf->SetTextColor(0, 0, 0);
261 $tab_top_newpage = (!
getDolGlobalInt(
'MAIN_PDF_DONOTREPEAT_HEAD') ? 42 : 10);
264 if (!empty(
$object->note_public)) {
267 $pdf->SetFont(
'',
'', $default_font_size - 1);
269 $nexY = $pdf->GetY();
270 $height_note = $nexY - $tab_top;
273 $pdf->SetDrawColor(192, 192, 192);
274 $pdf->Rect($this->marge_gauche, $tab_top - 1, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_note + 1);
276 $tab_top = $nexY + 6;
279 $iniY = $tab_top + 7;
280 $curY = $tab_top + 7;
281 $nexY = $tab_top + 2;
283 $pdf->SetXY($this->marge_gauche, $tab_top);
285 $pdf->MultiCell(0, 2,
'');
287 $nblines = count(
$object->lines);
290 for ($i = 0; $i < $nblines; $i++) {
291 $objectligne =
$object->lines[$i];
293 $valide = $objectligne->id ? 1 : 0;
295 if ($valide > 0 ||
$object->specimen) {
296 $curX = $this->posxdesc - 1;
298 $pdf->SetFont(
'',
'', $default_font_size - 1);
299 $pdf->SetTextColor(0, 0, 0);
301 $pdf->setTopMargin($tab_top_newpage);
302 $pdf->setPageOrientation(
'', 1, $heightforfooter + $heightforfreetext + $heightforinfotot);
303 $pageposbefore = $pdf->getPage();
307 if (!empty($objectligne->date_start)) {
308 $datei =
dol_print_date((
int) $objectligne->date_start,
'day',
false, $outputlangs,
true);
310 $datei = $langs->trans(
"Unknown");
313 if (!empty($objectligne->date_end)) {
314 $durationi =
convertSecondToTime((
int) $objectligne->date_end - (
int) $objectligne->date_start,
'allwithouthour');
315 $datee =
dol_print_date($objectligne->date_end,
'day',
false, $outputlangs,
true);
317 $durationi = $langs->trans(
"Unknown");
318 $datee = $langs->trans(
"Unknown");
321 if (!empty($objectligne->date_start_real)) {
322 $daters =
dol_print_date((
int) $objectligne->date_start_real,
'day',
false, $outputlangs,
true);
324 $daters = $langs->trans(
"Unknown");
327 if (!empty($objectligne->date_end_real)) {
328 $datere =
dol_print_date((
int) $objectligne->date_end_real,
'day',
false, $outputlangs,
true);
330 $datere = $langs->trans(
"Unknown");
333 $txtpredefinedservice = $objectligne->product_ref;
334 if ($objectligne->product_label) {
335 $txtpredefinedservice .=
' - ';
336 $txtpredefinedservice .= $objectligne->product_label;
342 $txt .= $outputlangs->transnoentities(
"Quantity") .
' : <strong>' . $objectligne->qty .
'</strong>';
345 $txt .=
' - ' . $outputlangs->transnoentities(
"UnitPrice") .
' : <strong>' .
price($objectligne->subprice) .
'</strong>';
349 $txt .= $outputlangs->transnoentities(
"DateStartPlannedShort").
" : <strong>".$datei.
"</strong> - ".$outputlangs->transnoentities(
"DateEndPlanned").
" : <strong>".$datee.
'</strong>';
353 $txt .= $outputlangs->transnoentities(
"DateStartRealShort").
" : <strong>".$daters.
'</strong>';
354 if (!empty($objectligne->date_end_real)) {
355 $txt .=
" - ".$outputlangs->transnoentities(
"DateEndRealShort").
" : <strong>".$datere.
'</strong>';
359 $pdf->startTransaction();
361 $pageposafter = $pdf->getPage();
362 if ($pageposafter > $pageposbefore) {
363 $pdf->rollbackTransaction(
true);
364 $pageposafter = $pageposbefore;
366 $pdf->setPageOrientation(
'', 1, $heightforfooter);
368 $pageposafter = $pdf->getPage();
369 $posyafter = $pdf->GetY();
370 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
371 if ($i == ($nblines - 1)) {
372 $pdf->AddPage(
'',
'',
true);
373 if (!empty($tplidx)) {
374 $pdf->useTemplate($tplidx);
379 $pdf->setPage($pageposafter + 1);
386 $showpricebeforepagebreak = 1;
388 $showpricebeforepagebreak = 0;
392 $pdf->commitTransaction();
394 $posYAfterDescription = $pdf->GetY();
396 $nexY = $pdf->GetY() + 2;
397 $pageposafter = $pdf->getPage();
399 $pdf->setPage($pageposbefore);
400 $pdf->setTopMargin($this->marge_haute);
401 $pdf->setPageOrientation(
'', 1, 0);
404 if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
405 $pdf->setPage($pageposafter);
406 $curY = $tab_top_newpage;
409 $pdf->SetFont(
'',
'', $default_font_size - 1);
412 while ($pagenb < $pageposafter) {
413 $pdf->setPage($pagenb);
415 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
417 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
421 $pdf->setPage($pagenb);
422 $pdf->setPageOrientation(
'', 1, 0);
426 if (!empty($tplidx)) {
427 $pdf->useTemplate($tplidx);
431 if (isset(
$object->lines[$i + 1]->pagebreak) &&
$object->lines[$i + 1]->pagebreak) {
433 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext, 0, $outputlangs, 0, 1);
435 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter - $heightforfreetext, 0, $outputlangs, 1, 1);
440 if (!empty($tplidx)) {
441 $pdf->useTemplate($tplidx);
451 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
452 $this->
tabSignature($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
453 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
455 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
456 $this->
tabSignature($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, $outputlangs);
457 $bottomlasttab = $this->page_hauteur - $heightforfooter - $heightforfooter + 1;
461 if (method_exists($pdf,
'AliasNbPages')) {
462 $pdf->AliasNbPages();
467 $pdf->Output($file,
'F');
470 $hookmanager->initHooks(array(
'pdfgeneration'));
471 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
473 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
475 $this->error = $hookmanager->error;
476 $this->errors = $hookmanager->errors;
481 $this->result = array(
'fullpath' => $file);
485 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
489 $this->error = $langs->transnoentities(
"ErrorConstantNotDefined",
"CONTRACT_OUTPUTDIR");
588 protected function _pagehead(&$pdf,
$object, $showaddress, $outputlangs, $outputlangsbis =
null, $titlekey =
"Contract")
596 if ($outputlangs->trans(
"DIRECTION") ==
'rtl') {
601 $outputlangs->loadLangs(array(
"main",
"dict",
"contract",
"companies"));
607 $pdf->SetTextColor(0, 0, 60);
608 $pdf->SetFont(
'',
'B', $default_font_size + 3);
612 $posy = $this->marge_haute;
613 $posx = $this->page_largeur - $this->marge_droite - $w;
615 $pdf->SetXY($this->marge_gauche, $posy);
619 if ($this->emetteur->logo) {
620 $logodir = $conf->mycompany->dir_output;
625 $logo = $logodir.
'/logos/thumbs/'.$this->emetteur->logo_small;
627 $logo = $logodir.
'/logos/'.$this->emetteur->logo;
629 if (is_readable($logo)) {
631 $pdf->Image($logo, $this->marge_gauche, $posy, 0, $height);
633 $pdf->SetTextColor(200, 0, 0);
634 $pdf->SetFont(
'',
'B', $default_font_size - 2);
635 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorLogoFileNotFound", $logo), 0,
'L');
636 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"ErrorGoToGlobalSetup"), 0,
'L');
639 $text = $this->emetteur->name;
640 $pdf->MultiCell($w, 4, $outputlangs->convToOutputCharset($text), 0, $ltrdirection);
644 $pdf->SetFont(
'',
'B', $default_font_size + 3);
645 $pdf->SetXY($posx, $posy);
646 $pdf->SetTextColor(0, 0, 60);
647 $title = $outputlangs->transnoentities($titlekey);
648 $title .=
' '.$outputlangs->convToOutputCharset(
$object->ref);
649 if (
$object->statut == $object::STATUS_DRAFT) {
650 $pdf->SetTextColor(128, 0, 0);
651 $title .=
' - '.$outputlangs->transnoentities(
"NotValidated");
653 $pdf->MultiCell($w, 3, $title,
'',
'R');
655 $pdf->SetFont(
'',
'B', $default_font_size);
665 $pdf->SetFont(
'',
'', $default_font_size - 1);
668 $pdf->SetXY($posx, $posy);
669 $pdf->SetTextColor(0, 0, 60);
670 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"Date").
" : ".
dol_print_date(
$object->date_contrat,
"day",
false, $outputlangs,
true),
'',
'R');
674 $pdf->SetXY($posx, $posy);
675 $pdf->SetTextColor(0, 0, 60);
676 $pdf->MultiCell($w, 3, $outputlangs->transnoentities(
"CustomerCode").
" : ".$outputlangs->transnoentities(
$object->thirdparty->code_client),
'',
'R');
681 $carac_emetteur =
'';
683 $arrayidcontact =
$object->getIdContact(
'internal',
'INTERREPFOLL');
684 if (count($arrayidcontact) > 0) {
685 $object->fetch_user($arrayidcontact[0]);
686 $labelbeforecontactname = ($outputlangs->transnoentities(
"FromContactName") !=
'FromContactName' ? $outputlangs->transnoentities(
"FromContactName") : $outputlangs->transnoentities(
"Name"));
687 $carac_emetteur .= ($carac_emetteur ?
"\n" :
'').$labelbeforecontactname.
": ".$outputlangs->convToOutputCharset(
$object->user->getFullName($outputlangs));
689 $carac_emetteur .= (
getDolGlobalInt(
'PDF_SHOW_PHONE_AFTER_USER_CONTACT') && !empty(
$object->user->office_phone)) ?
$object->user->office_phone :
'';
693 $carac_emetteur .=
"\n";
700 $posx = $this->marge_gauche;
702 $posx = $this->page_largeur - $this->marge_droite - 80;
708 $pdf->SetTextColor(0, 0, 0);
709 $pdf->SetFont(
'',
'', $default_font_size - 2);
710 $pdf->SetXY($posx, $posy - 5);
711 $pdf->SetXY($posx, $posy);
712 $pdf->SetFillColor(230, 230, 230);
713 $pdf->MultiCell(82, $hautcadre,
"", 0,
'R', 1);
714 $pdf->SetTextColor(0, 0, 60);
719 $pdf->SetXY($posx + 2, $posy + 3);
720 $pdf->SetTextColor(0, 0, 60);
721 $pdf->SetFont(
'',
'B', $default_font_size);
722 $pdf->MultiCell(80, 4, $outputlangs->convToOutputCharset($this->emetteur->name), 0,
'L');
723 $posy = $pdf->getY();
727 $pdf->SetXY($posx + 2, $posy);
728 $pdf->SetFont(
'',
'', $default_font_size - 1);
729 $pdf->MultiCell(80, 4, $carac_emetteur, 0,
'L');
734 $arrayidcontact =
$object->getIdContact(
'external',
'CUSTOMER');
735 if (count($arrayidcontact) > 0) {
737 $result =
$object->fetch_contact($arrayidcontact[0]);
740 $this->recipient =
$object->thirdparty;
743 if ($usecontact && (
$object->contact->socid !=
$object->thirdparty->id) && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) ||
getDolGlobalString(
'MAIN_USE_COMPANY_NAME_OF_CONTACT'))) {
744 $thirdparty =
$object->contact;
746 $thirdparty =
$object->thirdparty;
756 if ($this->page_largeur < 210) {
761 $posx = $this->page_largeur - $this->marge_droite - $widthrecbox;
763 $posx = $this->marge_gauche;
768 $pdf->SetTextColor(0, 0, 0);
769 $pdf->SetFont(
'',
'', $default_font_size - 2);
770 $pdf->SetXY($posx + 2, $posy - 5);
771 $pdf->Rect($posx, $posy, $widthrecbox, $hautcadre);
772 $pdf->SetTextColor(0, 0, 0);
776 $pdf->SetXY($posx + 2, $posy + 3);
777 $pdf->SetFont(
'',
'B', $default_font_size);
778 $pdf->MultiCell($widthrecbox, 4, $this->recipient->name, 0, $ltrdirection);
780 $posy = $pdf->getY();
783 $pdf->SetFont(
'',
'', $default_font_size - 1);
784 $pdf->SetXY($posx + 2, $posy);
786 $pdf->MultiCell($widthrecbox, 4, $carac_client, 0, $ltrdirection);
789 $pdf->SetTextColor(0, 0, 0);