186 public function write_file(
$object, $outputlangs, $srctemplatepath =
'', $hidedetails = 0, $hidedesc = 0, $hideref = 0)
189 global $user, $conf, $langs, $hookmanager;
195 if (!is_object($outputlangs)) {
196 $outputlangs = $langs;
200 $outputlangs->charset_output =
'ISO-8859-1';
204 $outputlangs->loadLangs(array(
"main",
"bills",
"products",
"dict",
"companies",
"propal",
"sendings",
"productbatch",
"stocks",
"stocktransfer@stocktransfer"));
206 global $outputlangsbis;
207 $outputlangsbis =
null;
209 $outputlangsbis =
new Translate(
'', $conf);
211 $outputlangsbis->loadLangs(array(
"main",
"bills",
"orders",
"products",
"dict",
"companies",
"propal",
"sendings",
"productbatch"));
217 $realpatharray = array();
218 $this->atleastonephoto =
false;
220 $objphoto =
new Product($this->db);
222 for ($i = 0; $i < $nblines; $i++) {
223 if (empty(
$object->lines[$i]->fk_product)) {
227 $objphoto =
new Product($this->db);
228 $objphoto->fetch(
$object->lines[$i]->fk_product);
230 $pdir =
get_exdir(
$object->lines[$i]->fk_product, 2, 0, 0, $objphoto,
'product').$object->lines[$i]->fk_product.
"/photos/";
231 $dir = $conf->product->dir_output.
'/'.$pdir;
233 $pdir =
get_exdir(0, 2, 0, 0, $objphoto,
'product').dol_sanitizeFileName($objphoto->ref).
'/';
234 $dir = $conf->product->dir_output.
'/'.$pdir;
239 foreach ($objphoto->liste_photos($dir, 1) as $key => $obj) {
242 if ($obj[
'photo_vignette']) {
243 $filename = $obj[
'photo_vignette'];
245 $filename = $obj[
'photo'];
248 $filename = $obj[
'photo'];
251 $realpath = $dir.$filename;
252 $this->atleastonephoto =
true;
257 $realpatharray[$i] = $realpath;
262 if (count($realpatharray) == 0) {
263 $this->posxpicture = $this->posxweightvol;
268 $this->posxpicture = $this->posxlot;
270 $this->posxpicture -=
getDolGlobalInt(
'MAIN_DOCUMENTS_WITH_PICTURE_WIDTH', 20);
274 if ($conf->stocktransfer->dir_output) {
277 $dir = $conf->stocktransfer->dir_output;
278 $file = $dir.
"/SPECIMEN.pdf";
281 $dir = $conf->stocktransfer->dir_output.
'/'.
$object->element.
"/".$stocktransferref;
282 $file = $dir.
"/".$stocktransferref.
".pdf";
285 if (!file_exists($dir)) {
287 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
292 if (file_exists($dir)) {
294 if (!is_object($hookmanager)) {
295 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
298 $hookmanager->initHooks(array(
'pdfgeneration'));
299 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
301 $reshook = $hookmanager->executeHooks(
'beforePDFCreation', $parameters,
$object, $action);
308 $heightforinfotot = 8;
310 $heightforfooter = $this->marge_basse + 8;
312 $heightforfooter += 6;
314 $pdf->setAutoPageBreak(
true, 0);
316 if (class_exists(
'TCPDF')) {
317 $pdf->setPrintHeader(
false);
318 $pdf->setPrintFooter(
false);
323 $pagecount = $pdf->setSourceFile($conf->mycompany->dir_output.
'/' .
getDolGlobalString(
'MAIN_ADD_PDF_BACKGROUND'));
324 $tplidx = $pdf->importPage(1);
329 $pdf->SetDrawColor(128, 128, 128);
331 if (method_exists($pdf,
'AliasNbPages')) {
332 $pdf->AliasNbPages();
335 $pdf->SetTitle($outputlangs->convToOutputCharset(
$object->ref));
336 $pdf->SetSubject($outputlangs->transnoentities(
"StockTransfer"));
337 $pdf->SetCreator(
"Dolibarr ".DOL_VERSION);
338 $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs)));
339 $pdf->SetKeyWords($outputlangs->convToOutputCharset(
$object->ref).
" ".$outputlangs->transnoentities(
"StockTransfer"));
341 $pdf->SetCompression(
false);
345 $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite);
349 if (!empty($tplidx)) {
350 $pdf->useTemplate($tplidx);
353 $top_shift = $this->_pagehead($pdf,
$object, 1, $outputlangs);
354 $pdf->SetFont(
'',
'', $default_font_size - 1);
355 $pdf->MultiCell(0, 3,
'');
356 $pdf->SetTextColor(0, 0, 0);
359 $tab_top_newpage = (!
getDolGlobalInt(
'MAIN_PDF_DONOTREPEAT_HEAD') ? 42 + $top_shift : 10);
361 $tab_height = $this->page_hauteur - $tab_top - $heightforfooter - $heightforfreetext;
363 $tab_height_newpage = 150;
366 $height_incoterms = 0;
368 $desc_incoterms =
$object->getIncotermsForPDF();
369 if ($desc_incoterms) {
372 $pdf->SetFont(
'',
'', $default_font_size - 1);
373 $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top - 1,
dol_htmlentitiesbr($desc_incoterms), 0, 1);
374 $nexY = $pdf->GetY();
375 $height_incoterms = $nexY - $tab_top;
378 $pdf->SetDrawColor(192, 192, 192);
379 $pdf->RoundedRect($this->marge_gauche, $tab_top - 1, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_incoterms + 3, $this->corner_radius,
'1234',
'D');
381 $tab_top = $nexY + 6;
382 $height_incoterms += 4;
387 if (!empty(
$object->note_public) || !empty(
$object->tracking_number)) {
388 $tab_top_alt = $tab_top;
393 if (!empty(
$object->tracking_number)) {
394 $height_trackingnumber = 4;
396 $pdf->SetFont(
'',
'B', $default_font_size - 2);
397 $pdf->writeHTMLCell(60, $height_trackingnumber, $this->posxdesc - 1, $tab_top - 1, $outputlangs->transnoentities(
"TrackingNumber").
" : ".
$object->tracking_number, 0, 1,
false,
true,
'L');
398 $tab_top_alt = $pdf->GetY();
401 if (!empty(
$object->tracking_url)) {
402 if (
$object->shipping_method_id > 0) {
404 $code = $outputlangs->getLabelFromKey($this->db, (
string)
$object->shipping_method_id,
'c_shipment_mode',
'rowid',
'code');
407 $label .= $outputlangs->trans(
"LinkToTrackYourPackage").
"<br>";
409 $label .= $outputlangs->trans(
"SendingMethod").
": ".$outputlangs->trans(
"SendingMethod".strtoupper($code));
413 $label .=
$object->tracking_url;
416 $height_trackingnumber += 4;
417 $pdf->SetFont(
'',
'B', $default_font_size - 2);
418 $pdf->writeHTMLCell(60, $height_trackingnumber, $this->posxdesc - 1, $tab_top_alt, $label, 0, 1,
false,
true,
'L');
421 $tab_top = $pdf->GetY();
425 if (!empty(
$object->note_public)) {
426 $pdf->SetFont(
'',
'', $default_font_size - 1);
430 $nexY = $pdf->GetY();
431 $height_note = $nexY - $tab_top;
434 $pdf->SetDrawColor(192, 192, 192);
435 $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');
437 $tab_height -= $height_note;
438 $tab_top = $nexY + 6;
447 require_once DOL_DOCUMENT_ROOT.
'/core/modules/barcode/doc/tcpdfbarcode.modules.php';
449 $encoding =
'QRCODE';
453 if ($module->encodingIsSupported($encoding)) {
454 $result = $module->writeBarCode(
$object->ref, $encoding);
458 if (!preg_match(
'/^\w+$/', $newcode) ||
dol_strlen($newcode) > 32) {
459 $newcode =
dol_hash($newcode,
'md5');
461 $barcode_path = $conf->barcode->dir_temp .
'/barcode_' . $newcode .
'_' . $encoding .
'.png';
467 $pdf->Image($barcode_path, $this->marge_gauche, $tab_top, 20, 20);
469 $nexY = $pdf->GetY();
470 $height_barcode = 20;
474 $this->error =
'Failed to generate barcode';
478 $iniY = $tab_top + 7;
479 $curY = $tab_top + 7;
480 $nexY = $tab_top + 7;
482 $TCacheEntrepots = array();
484 for ($i = 0; $i < $nblines; $i++) {
486 $pdf->SetFont(
'',
'', $default_font_size - 1);
487 $pdf->SetTextColor(0, 0, 0);
490 $imglinesize = array();
491 if (!empty($realpatharray[$i])) {
495 $pdf->setTopMargin($tab_top_newpage);
496 $pdf->setPageOrientation(
'',
true, $heightforfooter + $heightforfreetext + $heightforinfotot);
497 $pageposbefore = $pdf->getPage();
499 $showpricebeforepagebreak = 1;
501 $posYAfterDescription = 0;
502 $heightforsignature = 0;
505 if (isset($imglinesize[
'width']) && isset($imglinesize[
'height']) && ($curY + $imglinesize[
'height']) > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
506 $pdf->AddPage(
'',
'',
true);
507 if (!empty($tplidx)) {
508 $pdf->useTemplate($tplidx);
511 $this->_pagehead($pdf,
$object, 0, $outputlangs);
513 $pdf->setPage($pageposbefore + 1);
515 $curY = $tab_top_newpage;
519 $showpricebeforepagebreak = 1;
521 $showpricebeforepagebreak = 0;
525 if (isset($imglinesize[
'width']) && isset($imglinesize[
'height'])) {
526 $curX = $this->posxpicture - 1;
527 $pdf->Image($realpatharray[$i], $curX + (($this->posxqty - $this->posxweightvol - $imglinesize[
'width']
528 + (
getDolGlobalString(
'STOCKTRANSFER_PDF_HIDE_WEIGHT_AND_VOLUME') ?
getDolGlobalInt(
'MAIN_DOCUMENTS_WITH_PICTURE_WIDTH', 20) : 0)) / 2), $curY, $imglinesize[
'width'], $imglinesize[
'height'],
'',
'',
'', 2, 300);
530 $posYAfterImage = $curY + $imglinesize[
'height'];
534 $curX = $this->posxdesc - 1;
536 $pdf->startTransaction();
537 if (method_exists(
$object->lines[$i],
'fetch_product')) {
538 $object->lines[$i]->fetch_product();
540 $object->lines[$i]->description =
$object->lines[$i]->product->description;
542 $object->lines[$i]->weight_units =
$object->lines[$i]->product->weight_units;
544 $object->lines[$i]->length_units =
$object->lines[$i]->product->length_units;
545 $object->lines[$i]->surface =
$object->lines[$i]->product->surface;
546 $object->lines[$i]->surface_units =
$object->lines[$i]->product->surface_units;
548 $object->lines[$i]->volume_units =
$object->lines[$i]->product->volume_units;
549 $object->lines[$i]->fk_unit =
$object->lines[$i]->product->fk_unit;
553 pdf_writelinedesc($pdf,
$object, $i, $outputlangs, $this->posxpicture - $curX, 3, $curX, $curY, $hideref, $hidedesc);
555 $pageposafter = $pdf->getPage();
556 if ($pageposafter > $pageposbefore) {
557 $pdf->rollbackTransaction(
true);
558 $pageposafter = $pageposbefore;
560 $pdf->setPageOrientation(
'',
true, $heightforfooter);
561 pdf_writelinedesc($pdf,
$object, $i, $outputlangs, $this->posxpicture - $curX, 3, $curX, $curY, $hideref, $hidedesc);
563 $pageposafter = $pdf->getPage();
564 $posyafter = $pdf->GetY();
566 if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforinfotot))) {
567 if ($i == ($nblines - 1)) {
568 $pdf->AddPage(
'',
'',
true);
569 if (!empty($tplidx)) {
570 $pdf->useTemplate($tplidx);
573 $this->_pagehead($pdf,
$object, 0, $outputlangs);
575 $pdf->setPage($pageposafter + 1);
582 $showpricebeforepagebreak = 1;
584 $showpricebeforepagebreak = 0;
588 $pdf->commitTransaction();
590 $posYAfterDescription = $pdf->GetY();
592 $nexY = max($pdf->GetY(), $posYAfterImage);
593 $pageposafter = $pdf->getPage();
595 $pdf->setPage($pageposbefore);
596 $pdf->setTopMargin($this->marge_haute);
597 $pdf->setPageOrientation(
'',
true, 0);
600 if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) {
601 $pdf->setPage($pageposafter);
602 $curY = $tab_top_newpage;
606 if ($pageposafter > $pageposbefore) {
607 $pdf->setPage($pageposafter);
608 $curY = $tab_top_newpage;
611 $pdf->SetFont(
'',
'', $default_font_size - 1);
615 $pdf->SetXY($this->posxlot, $curY);
616 $pdf->MultiCell(($this->posxweightvol - $this->posxlot), 3,
$object->lines[$i]->batch,
'',
'C');
621 $pdf->SetXY($this->posxweightvol, $curY);
623 if (empty(
$object->lines[$i]->fk_product_type) &&
$object->lines[$i]->weight) {
627 if (empty(
$object->lines[$i]->fk_product_type) &&
$object->lines[$i]->volume) {
633 $pdf->writeHTMLCell($this->posxqty - $this->posxweightvol + 2, 3, $this->posxweightvol - 1, $curY, $weighttxt.(($weighttxt && $voltxt) ?
'<br>' :
'').$voltxt, 0, 0,
false,
true,
'C');
638 $pdf->SetXY($this->posxqty, $curY);
639 $pdf->writeHTMLCell($this->posxwarehousesource - $this->posxqty + 2, 3, $this->posxqty - 1, $curY, (
string)
$object->lines[$i]->qty, 0, 0,
false,
true,
'C');
643 $wh_source =
new Entrepot($this->db);
644 if (!empty($TCacheEntrepots[
$object->lines[$i]->fk_warehouse_source])) {
645 $wh_source = $TCacheEntrepots[
$object->lines[$i]->fk_warehouse_source];
647 $wh_source->fetch(
$object->lines[$i]->fk_warehouse_source);
648 $TCacheEntrepots[
$object->lines[$i]->fk_warehouse_source] = $wh_source;
650 $pdf->SetXY($this->posxwarehousesource, $curY);
651 $pdf->MultiCell(($this->posxwarehousedestination - $this->posxwarehousesource), 3, $wh_source->ref.(!empty($wh_source->lieu) ?
' - '.$wh_source->lieu :
''),
'',
'C');
654 $wh_destination =
new Entrepot($this->db);
655 if (!empty($TCacheEntrepots[
$object->lines[$i]->fk_warehouse_destination])) {
656 $wh_destination = $TCacheEntrepots[
$object->lines[$i]->fk_warehouse_destination];
658 $wh_destination->fetch(
$object->lines[$i]->fk_warehouse_destination);
659 $TCacheEntrepots[
$object->lines[$i]->fk_warehouse_destination] = $wh_destination;
661 $pdf->SetXY($this->posxwarehousedestination, $curY);
662 $pdf->MultiCell(($this->posxpuht - $this->posxwarehousedestination), 3, $wh_destination->ref.(!empty($wh_destination->lieu) ?
' - '.$wh_destination->lieu :
''),
'',
'C');
665 $pdf->SetXY($this->posxpuht, $curY);
666 $pdf->MultiCell(($this->posxtotalht - $this->posxpuht - 1), 3,
price(
$object->lines[$i]->subprice, 0, $outputlangs),
'',
'R');
668 $pdf->SetXY($this->posxtotalht, $curY);
669 $pdf->MultiCell(($this->page_largeur - $this->marge_droite - $this->posxtotalht), 3,
price(
$object->lines[$i]->total_ht, 0, $outputlangs),
'',
'R');
673 if ($weighttxt && $voltxt) {
679 $pdf->setPage($pageposafter);
680 $pdf->SetLineStyle(array(
'dash' =>
'1,1',
'color' => array(80, 80, 80)));
682 $pdf->line($this->marge_gauche, $nexY - 1, $this->page_largeur - $this->marge_droite, $nexY - 1);
683 $pdf->SetLineStyle(array(
'dash' => 0));
687 while ($pagenb < $pageposafter) {
688 $pdf->setPage($pagenb);
690 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1);
692 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1);
696 $pdf->setPage($pagenb);
697 $pdf->setPageOrientation(
'',
true, 0);
699 $this->_pagehead($pdf,
$object, 0, $outputlangs);
701 if (!empty($tplidx)) {
702 $pdf->useTemplate($tplidx);
705 if (isset(
$object->lines[$i + 1]->pagebreak) &&
$object->lines[$i + 1]->pagebreak) {
707 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1);
709 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1);
714 if (!empty($tplidx)) {
715 $pdf->useTemplate($tplidx);
719 $this->_pagehead($pdf,
$object, 0, $outputlangs);
726 $this->
_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0);
727 $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
729 $this->
_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0);
730 $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1;
738 if (method_exists($pdf,
'AliasNbPages')) {
739 $pdf->AliasNbPages();
744 $pdf->Output($file,
'F');
747 $hookmanager->initHooks(array(
'pdfgeneration'));
748 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs);
750 $reshook = $hookmanager->executeHooks(
'afterPDFCreation', $parameters, $this, $action);
751 $this->warnings = $hookmanager->warnings;
753 $this->error = $hookmanager->error;
754 $this->errors = $hookmanager->errors;
761 $this->result = array(
'fullpath' => $file);
765 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
769 $this->error = $langs->transnoentities(
"ErrorConstantNotDefined",
"STOCKTRANSFER_OUTPUTDIR");
901 protected function _tableau(&$pdf, $tab_top, $tab_height, $nexY, $outputlangs, $hidetop = 0, $hidebottom = 0)
915 $pdf->SetTextColor(0, 0, 0);
916 $pdf->SetFont(
'',
'', $default_font_size - 2);
919 $this->
printRoundedRect($pdf, $this->marge_gauche, $tab_top, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $tab_height, $this->corner_radius, $hidetop, $hidebottom,
'D');
921 $pdf->SetDrawColor(128, 128, 128);
922 $pdf->SetFont(
'',
'', $default_font_size - 1);
925 if (empty($hidetop)) {
926 $pdf->line($this->marge_gauche, $tab_top + 5, $this->page_largeur - $this->marge_droite, $tab_top + 5);
928 $pdf->SetXY($this->posxdesc - 1, $tab_top + 1);
929 $pdf->MultiCell($this->posxlot - $this->posxdesc, 2, $outputlangs->transnoentities(
"Description"),
'',
'L');
933 $pdf->line($this->posxlot - 1, $tab_top, $this->posxlot - 1, $tab_top + $tab_height);
934 if (empty($hidetop)) {
935 $pdf->SetXY($this->posxlot, $tab_top + 1);
936 $pdf->MultiCell(($this->posxweightvol - $this->posxlot), 2, $outputlangs->transnoentities(
"Batch"),
'',
'C');
941 $pdf->line($this->posxweightvol - 1, $tab_top, $this->posxweightvol - 1, $tab_top + $tab_height);
942 if (empty($hidetop)) {
943 $pdf->SetXY($this->posxweightvol - 1, $tab_top + 1);
944 $pdf->MultiCell(($this->posxqty - $this->posxweightvol), 2, $outputlangs->transnoentities(
"WeightVolShort"),
'',
'C');
948 $pdf->line($this->posxqty - 1, $tab_top, $this->posxqty - 1, $tab_top + $tab_height);
949 if (empty($hidetop)) {
950 $pdf->SetXY($this->posxqty - 1, $tab_top + 1);
951 $pdf->MultiCell(($this->posxwarehousesource - $this->posxqty), 2, $outputlangs->transnoentities(
"Qty"),
'',
'C');
954 $pdf->line($this->posxwarehousesource - 1, $tab_top, $this->posxwarehousesource - 1, $tab_top + $tab_height);
955 if (empty($hidetop)) {
956 $pdf->SetXY($this->posxwarehousesource - 1, $tab_top + 1);
957 $pdf->MultiCell(($this->posxwarehousedestination - $this->posxwarehousesource), 2, $outputlangs->transnoentities(
"WarehouseSource"),
'',
'C');
961 $pdf->line($this->posxwarehousedestination - 1, $tab_top, $this->posxwarehousedestination - 1, $tab_top + $tab_height);
962 if (empty($hidetop)) {
963 $pdf->SetXY($this->posxwarehousedestination - 2.5, $tab_top + 1);
964 $pdf->MultiCell(($this->posxpuht - $this->posxwarehousedestination + 4), 2, $outputlangs->transnoentities(
"WarehouseTarget"),
'',
'C');