| Conditions | 143 |
| Total Lines | 768 |
| Code Lines | 434 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 198 | public function write_file($object, $outputlangs, $srctemplatepath = '', $hidedetails = 0, $hidedesc = 0, $hideref = 0) |
||
| 199 | { |
||
| 200 | // phpcs:enable |
||
| 201 | global $user, $langs, $conf, $mysoc, $db, $hookmanager, $nblines; |
||
| 202 | |||
| 203 | dol_syslog("write_file outputlangs->defaultlang=".(is_object($outputlangs) ? $outputlangs->defaultlang : 'null')); |
||
| 204 | |||
| 205 | if (!is_object($outputlangs)) $outputlangs = $langs; |
||
| 206 | // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO |
||
| 207 | if (!empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output = 'ISO-8859-1'; |
||
| 208 | |||
| 209 | // Load translation files required by page |
||
| 210 | $outputlangs->loadLangs(array("main", "dict", "companies", "bills", "products", "propal")); |
||
| 211 | |||
| 212 | if (!empty($conf->global->PDF_USE_ALSO_LANGUAGE_CODE) && $outputlangs->defaultlang != $conf->global->PDF_USE_ALSO_LANGUAGE_CODE) { |
||
| 213 | global $outputlangsbis; |
||
| 214 | $outputlangsbis = new Translate('', $conf); |
||
| 215 | $outputlangsbis->setDefaultLang($conf->global->PDF_USE_ALSO_LANGUAGE_CODE); |
||
| 216 | $outputlangsbis->loadLangs(array("main", "dict", "companies", "bills", "products", "propal")); |
||
| 217 | } |
||
| 218 | |||
| 219 | $nblines = count($object->lines); |
||
| 220 | |||
| 221 | $hidetop = 0; |
||
| 222 | if (!empty($conf->global->MAIN_PDF_DISABLE_COL_HEAD_TITLE)) { |
||
| 223 | $hidetop = $conf->global->MAIN_PDF_DISABLE_COL_HEAD_TITLE; |
||
| 224 | } |
||
| 225 | |||
| 226 | // Loop on each lines to detect if there is at least one image to show |
||
| 227 | $realpatharray = array(); |
||
| 228 | $this->atleastonephoto = false; |
||
| 229 | if (!empty($conf->global->MAIN_GENERATE_PROPOSALS_WITH_PICTURE)) |
||
| 230 | { |
||
| 231 | $objphoto = new Product($this->db); |
||
| 232 | |||
| 233 | for ($i = 0; $i < $nblines; $i++) |
||
| 234 | { |
||
| 235 | if (empty($object->lines[$i]->fk_product)) continue; |
||
| 236 | |||
| 237 | $objphoto->fetch($object->lines[$i]->fk_product); |
||
| 238 | //var_dump($objphoto->ref);exit; |
||
| 239 | if (!empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) |
||
| 240 | { |
||
| 241 | $pdir[0] = get_exdir($objphoto->id, 2, 0, 0, $objphoto, 'product').$objphoto->id."/photos/"; |
||
| 242 | $pdir[1] = get_exdir(0, 0, 0, 0, $objphoto, 'product').dol_sanitizeFileName($objphoto->ref).'/'; |
||
| 243 | } else { |
||
| 244 | $pdir[0] = get_exdir(0, 0, 0, 0, $objphoto, 'product').dol_sanitizeFileName($objphoto->ref).'/'; // default |
||
| 245 | $pdir[1] = get_exdir($objphoto->id, 2, 0, 0, $objphoto, 'product').$objphoto->id."/photos/"; // alternative |
||
| 246 | } |
||
| 247 | |||
| 248 | $arephoto = false; |
||
| 249 | foreach ($pdir as $midir) |
||
| 250 | { |
||
| 251 | if (!$arephoto) |
||
| 252 | { |
||
| 253 | if ($conf->product->entity != $objphoto->entity) { |
||
| 254 | $dir = $conf->product->multidir_output[$objphoto->entity].'/'.$midir; //Check repertories of current entities |
||
| 255 | } else { |
||
| 256 | $dir = $conf->product->dir_output.'/'.$midir; //Check repertory of the current product |
||
| 257 | } |
||
| 258 | |||
| 259 | foreach ($objphoto->liste_photos($dir, 1) as $key => $obj) |
||
| 260 | { |
||
| 261 | if (empty($conf->global->CAT_HIGH_QUALITY_IMAGES)) // If CAT_HIGH_QUALITY_IMAGES not defined, we use thumb if defined and then original photo |
||
| 262 | { |
||
| 263 | if ($obj['photo_vignette']) |
||
| 264 | { |
||
| 265 | $filename = $obj['photo_vignette']; |
||
| 266 | } else { |
||
| 267 | $filename = $obj['photo']; |
||
| 268 | } |
||
| 269 | } else { |
||
| 270 | $filename = $obj['photo']; |
||
| 271 | } |
||
| 272 | |||
| 273 | $realpath = $dir.$filename; |
||
| 274 | $arephoto = true; |
||
| 275 | $this->atleastonephoto = true; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | if ($realpath && $arephoto) $realpatharray[$i] = $realpath; |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | if (count($realpatharray) == 0) $this->posxpicture = $this->posxtva; |
||
| 285 | |||
| 286 | if ($conf->propal->multidir_output[$conf->entity]) |
||
| 287 | { |
||
| 288 | $object->fetch_thirdparty(); |
||
| 289 | |||
| 290 | $deja_regle = 0; |
||
| 291 | |||
| 292 | // Definition of $dir and $file |
||
| 293 | if ($object->specimen) |
||
| 294 | { |
||
| 295 | $dir = $conf->propal->multidir_output[$conf->entity]; |
||
| 296 | $file = $dir."/SPECIMEN.pdf"; |
||
| 297 | } else { |
||
| 298 | $objectref = dol_sanitizeFileName($object->ref); |
||
| 299 | $dir = $conf->propal->multidir_output[$object->entity]."/".$objectref; |
||
| 300 | $file = $dir."/".$objectref.".pdf"; |
||
| 301 | } |
||
| 302 | |||
| 303 | if (!file_exists($dir)) |
||
| 304 | { |
||
| 305 | if (dol_mkdir($dir) < 0) |
||
| 306 | { |
||
| 307 | $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
| 308 | return 0; |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | if (file_exists($dir)) |
||
| 313 | { |
||
| 314 | // Add pdfgeneration hook |
||
| 315 | if (!is_object($hookmanager)) |
||
| 316 | { |
||
| 317 | include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; |
||
| 318 | $hookmanager = new HookManager($this->db); |
||
| 319 | } |
||
| 320 | $hookmanager->initHooks(array('pdfgeneration')); |
||
| 321 | $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs); |
||
| 322 | global $action; |
||
| 323 | $reshook = $hookmanager->executeHooks('beforePDFCreation', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks |
||
| 324 | |||
| 325 | // Set nblines with the new facture lines content after hook |
||
| 326 | $nblines = count($object->lines); |
||
| 327 | //$nbpayments = count($object->getListOfPayments()); |
||
| 328 | |||
| 329 | // Create pdf instance |
||
| 330 | $pdf = pdf_getInstance($this->format); |
||
| 331 | $default_font_size = pdf_getPDFFontSize($outputlangs); // Must be after pdf_getInstance |
||
| 332 | $pdf->SetAutoPageBreak(1, 0); |
||
| 333 | |||
| 334 | if (class_exists('TCPDF')) |
||
| 335 | { |
||
| 336 | $pdf->setPrintHeader(false); |
||
| 337 | $pdf->setPrintFooter(false); |
||
| 338 | } |
||
| 339 | $pdf->SetFont(pdf_getPDFFont($outputlangs)); |
||
| 340 | // Set path to the background PDF File |
||
| 341 | if (!empty($conf->global->MAIN_ADD_PDF_BACKGROUND)) |
||
| 342 | { |
||
| 343 | $pagecount = $pdf->setSourceFile($conf->mycompany->multidir_output[$object->entity].'/'.$conf->global->MAIN_ADD_PDF_BACKGROUND); |
||
| 344 | $tplidx = $pdf->importPage(1); |
||
| 345 | } |
||
| 346 | |||
| 347 | $pdf->Open(); |
||
| 348 | $pagenb = 0; |
||
| 349 | $pdf->SetDrawColor(128, 128, 128); |
||
| 350 | |||
| 351 | $pdf->SetTitle($outputlangs->convToOutputCharset($object->ref)); |
||
| 352 | $pdf->SetSubject($outputlangs->transnoentities("PdfCommercialProposalTitle")); |
||
| 353 | $pdf->SetCreator("Dolibarr ".DOL_VERSION); |
||
| 354 | $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs))); |
||
| 355 | $pdf->SetKeyWords($outputlangs->convToOutputCharset($object->ref)." ".$outputlangs->transnoentities("PdfCommercialProposalTitle")." ".$outputlangs->convToOutputCharset($object->thirdparty->name)); |
||
| 356 | if (!empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false); |
||
| 357 | |||
| 358 | $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right |
||
| 359 | |||
| 360 | // Set $this->atleastonediscount if you have at least one discount |
||
| 361 | for ($i = 0; $i < $nblines; $i++) |
||
| 362 | { |
||
| 363 | if ($object->lines[$i]->remise_percent) |
||
| 364 | { |
||
| 365 | $this->atleastonediscount++; |
||
| 366 | } |
||
| 367 | } |
||
| 368 | |||
| 369 | |||
| 370 | // New page |
||
| 371 | $pdf->AddPage(); |
||
| 372 | if (!empty($tplidx)) $pdf->useTemplate($tplidx); |
||
| 373 | $pagenb++; |
||
| 374 | |||
| 375 | $heightforinfotot = 40; // Height reserved to output the info and total part |
||
| 376 | $heightforsignature = empty($conf->global->PROPAL_DISABLE_SIGNATURE) ? (pdfGetHeightForHtmlContent($pdf, $outputlangs->transnoentities("ProposalCustomerSignature")) + 10) : 0; |
||
| 377 | $heightforfreetext = (isset($conf->global->MAIN_PDF_FREETEXT_HEIGHT) ? $conf->global->MAIN_PDF_FREETEXT_HEIGHT : 5); // Height reserved to output the free text on last page |
||
| 378 | $heightforfooter = $this->marge_basse + (empty($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS) ? 12 : 22); // Height reserved to output the footer (value include bottom margin) |
||
| 379 | //print $heightforinfotot + $heightforsignature + $heightforfreetext + $heightforfooter;exit; |
||
| 380 | |||
| 381 | $top_shift = $this->_pagehead($pdf, $object, 1, $outputlangs, $outputlangsbis); |
||
| 382 | $pdf->SetFont('', '', $default_font_size - 1); |
||
| 383 | $pdf->MultiCell(0, 3, ''); // Set interline to 3 |
||
| 384 | $pdf->SetTextColor(0, 0, 0); |
||
| 385 | |||
| 386 | |||
| 387 | $tab_top = 90 + $top_shift; |
||
| 388 | $tab_top_newpage = (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD) ? 42 + $top_shift : 10); |
||
| 389 | |||
| 390 | |||
| 391 | // Incoterm |
||
| 392 | $height_incoterms = 0; |
||
| 393 | if (!empty($conf->incoterm->enabled)) |
||
| 394 | { |
||
| 395 | $desc_incoterms = $object->getIncotermsForPDF(); |
||
| 396 | if ($desc_incoterms) |
||
| 397 | { |
||
| 398 | $tab_top -= 2; |
||
| 399 | |||
| 400 | $pdf->SetFont('', '', $default_font_size - 1); |
||
| 401 | $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top - 1, dol_htmlentitiesbr($desc_incoterms), 0, 1); |
||
| 402 | $nexY = max($pdf->GetY(), $nexY); |
||
| 403 | $height_incoterms = $nexY - $tab_top; |
||
| 404 | |||
| 405 | // Rect takes a length in 3rd parameter |
||
| 406 | $pdf->SetDrawColor(192, 192, 192); |
||
| 407 | $pdf->Rect($this->marge_gauche, $tab_top - 1, $this->page_largeur - $this->marge_gauche - $this->marge_droite, $height_incoterms + 1); |
||
| 408 | |||
| 409 | $tab_top = $nexY + 6; |
||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | // Displays notes |
||
| 414 | $notetoshow = empty($object->note_public) ? '' : $object->note_public; |
||
| 415 | if (!empty($conf->global->MAIN_ADD_SALE_REP_SIGNATURE_IN_NOTE)) |
||
| 416 | { |
||
| 417 | // Get first sale rep |
||
| 418 | if (is_object($object->thirdparty)) |
||
| 419 | { |
||
| 420 | $salereparray = $object->thirdparty->getSalesRepresentatives($user); |
||
| 421 | $salerepobj = new User($this->db); |
||
| 422 | $salerepobj->fetch($salereparray[0]['id']); |
||
| 423 | if (!empty($salerepobj->signature)) $notetoshow = dol_concatdesc($notetoshow, $salerepobj->signature); |
||
| 424 | } |
||
| 425 | } |
||
| 426 | |||
| 427 | // Extrafields in note |
||
| 428 | $extranote = $this->getExtrafieldsInHtml($object, $outputlangs); |
||
| 429 | if (!empty($extranote)) { |
||
| 430 | $notetoshow = dol_concatdesc($notetoshow, $extranote); |
||
| 431 | } |
||
| 432 | |||
| 433 | if (!empty($conf->global->MAIN_ADD_CREATOR_IN_NOTE) && $object->user_author_id > 0) |
||
| 434 | { |
||
| 435 | $tmpuser = new User($this->db); |
||
| 436 | $tmpuser->fetch($object->user_author_id); |
||
| 437 | $notetoshow .= $langs->trans("CaseFollowedBy").' '.$tmpuser->getFullName($langs); |
||
| 438 | if ($tmpuser->email) $notetoshow .= ', Mail: '.$tmpuser->email; |
||
| 439 | if ($tmpuser->office_phone) $notetoshow .= ', Tel: '.$tmpuser->office_phone; |
||
| 440 | } |
||
| 441 | |||
| 442 | $tab_height = $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforsignature - $heightforfooter; |
||
| 443 | |||
| 444 | $pagenb = $pdf->getPage(); |
||
| 445 | if ($notetoshow) |
||
| 446 | { |
||
| 447 | $tab_top -= 2; |
||
| 448 | |||
| 449 | $tab_width = $this->page_largeur - $this->marge_gauche - $this->marge_droite; |
||
| 450 | $pageposbeforenote = $pagenb; |
||
| 451 | |||
| 452 | $substitutionarray = pdf_getSubstitutionArray($outputlangs, null, $object); |
||
| 453 | complete_substitutions_array($substitutionarray, $outputlangs, $object); |
||
| 454 | $notetoshow = make_substitutions($notetoshow, $substitutionarray, $outputlangs); |
||
| 455 | $notetoshow = convertBackOfficeMediasLinksToPublicLinks($notetoshow); |
||
| 456 | |||
| 457 | $pdf->startTransaction(); |
||
| 458 | |||
| 459 | $pdf->SetFont('', '', $default_font_size - 1); |
||
| 460 | $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top, dol_htmlentitiesbr($notetoshow), 0, 1); |
||
| 461 | // Description |
||
| 462 | $pageposafternote = $pdf->getPage(); |
||
| 463 | $posyafter = $pdf->GetY(); |
||
| 464 | |||
| 465 | if ($pageposafternote > $pageposbeforenote) |
||
| 466 | { |
||
| 467 | $pdf->rollbackTransaction(true); |
||
| 468 | |||
| 469 | // prepare pages to receive notes |
||
| 470 | while ($pagenb < $pageposafternote) { |
||
| 471 | $pdf->AddPage(); |
||
| 472 | $pagenb++; |
||
| 473 | if (!empty($tplidx)) $pdf->useTemplate($tplidx); |
||
| 474 | if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $this->_pagehead($pdf, $object, 0, $outputlangs); |
||
| 475 | // $this->_pagefoot($pdf,$object,$outputlangs,1); |
||
| 476 | $pdf->setTopMargin($tab_top_newpage); |
||
| 477 | // The only function to edit the bottom margin of current page to set it. |
||
| 478 | $pdf->setPageOrientation('', 1, $heightforfooter + $heightforfreetext); |
||
| 479 | } |
||
| 480 | |||
| 481 | // back to start |
||
| 482 | $pdf->setPage($pageposbeforenote); |
||
| 483 | $pdf->setPageOrientation('', 1, $heightforfooter + $heightforfreetext); |
||
| 484 | $pdf->SetFont('', '', $default_font_size - 1); |
||
| 485 | $pdf->writeHTMLCell(190, 3, $this->posxdesc - 1, $tab_top, dol_htmlentitiesbr($notetoshow), 0, 1); |
||
| 486 | $pageposafternote = $pdf->getPage(); |
||
| 487 | |||
| 488 | $posyafter = $pdf->GetY(); |
||
| 489 | |||
| 490 | if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) // There is no space left for total+free text |
||
| 491 | { |
||
| 492 | $pdf->AddPage('', '', true); |
||
| 493 | $pagenb++; |
||
| 494 | $pageposafternote++; |
||
| 495 | $pdf->setPage($pageposafternote); |
||
| 496 | $pdf->setTopMargin($tab_top_newpage); |
||
| 497 | // The only function to edit the bottom margin of current page to set it. |
||
| 498 | $pdf->setPageOrientation('', 1, $heightforfooter + $heightforfreetext); |
||
| 499 | //$posyafter = $tab_top_newpage; |
||
| 500 | } |
||
| 501 | |||
| 502 | |||
| 503 | // apply note frame to previous pages |
||
| 504 | $i = $pageposbeforenote; |
||
| 505 | while ($i < $pageposafternote) { |
||
| 506 | $pdf->setPage($i); |
||
| 507 | |||
| 508 | |||
| 509 | $pdf->SetDrawColor(128, 128, 128); |
||
| 510 | // Draw note frame |
||
| 511 | if ($i > $pageposbeforenote) { |
||
| 512 | $height_note = $this->page_hauteur - ($tab_top_newpage + $heightforfooter); |
||
| 513 | $pdf->Rect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1); |
||
| 514 | } else { |
||
| 515 | $height_note = $this->page_hauteur - ($tab_top + $heightforfooter); |
||
| 516 | $pdf->Rect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1); |
||
| 517 | } |
||
| 518 | |||
| 519 | // Add footer |
||
| 520 | $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it. |
||
| 521 | $this->_pagefoot($pdf, $object, $outputlangs, 1); |
||
| 522 | |||
| 523 | $i++; |
||
| 524 | } |
||
| 525 | |||
| 526 | // apply note frame to last page |
||
| 527 | $pdf->setPage($pageposafternote); |
||
| 528 | if (!empty($tplidx)) $pdf->useTemplate($tplidx); |
||
| 529 | if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $this->_pagehead($pdf, $object, 0, $outputlangs); |
||
| 530 | $height_note = $posyafter - $tab_top_newpage; |
||
| 531 | $pdf->Rect($this->marge_gauche, $tab_top_newpage - 1, $tab_width, $height_note + 1); |
||
| 532 | } else // No pagebreak |
||
| 533 | { |
||
| 534 | $pdf->commitTransaction(); |
||
| 535 | $posyafter = $pdf->GetY(); |
||
| 536 | $height_note = $posyafter - $tab_top; |
||
| 537 | $pdf->Rect($this->marge_gauche, $tab_top - 1, $tab_width, $height_note + 1); |
||
| 538 | |||
| 539 | |||
| 540 | if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + 20))) |
||
| 541 | { |
||
| 542 | // not enough space, need to add page |
||
| 543 | $pdf->AddPage('', '', true); |
||
| 544 | $pagenb++; |
||
| 545 | $pageposafternote++; |
||
| 546 | $pdf->setPage($pageposafternote); |
||
| 547 | if (!empty($tplidx)) $pdf->useTemplate($tplidx); |
||
| 548 | if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $this->_pagehead($pdf, $object, 0, $outputlangs); |
||
| 549 | |||
| 550 | $posyafter = $tab_top_newpage; |
||
| 551 | } |
||
| 552 | } |
||
| 553 | $tab_height = $tab_height - $height_note; |
||
| 554 | $tab_top = $posyafter + 6; |
||
| 555 | } else { |
||
| 556 | $height_note = 0; |
||
| 557 | } |
||
| 558 | |||
| 559 | // Use new auto column system |
||
| 560 | $this->prepareArrayColumnField($object, $outputlangs, $hidedetails, $hidedesc, $hideref); |
||
| 561 | |||
| 562 | // Table simulation to know the height of the title line |
||
| 563 | $pdf->startTransaction(); |
||
| 564 | $this->pdfTabTitles($pdf, $tab_top, $tab_height, $outputlangs, $hidetop); |
||
| 565 | $pdf->rollbackTransaction(true); |
||
| 566 | |||
| 567 | $nexY = $tab_top + $this->tabTitleHeight; |
||
| 568 | |||
| 569 | // Loop on each lines |
||
| 570 | $pageposbeforeprintlines = $pdf->getPage(); |
||
| 571 | $pagenb = $pageposbeforeprintlines; |
||
| 572 | for ($i = 0; $i < $nblines; $i++) |
||
| 573 | { |
||
| 574 | $curY = $nexY; |
||
| 575 | $pdf->SetFont('', '', $default_font_size - 1); // Into loop to work with multipage |
||
| 576 | $pdf->SetTextColor(0, 0, 0); |
||
| 577 | |||
| 578 | // Define size of image if we need it |
||
| 579 | $imglinesize = array(); |
||
| 580 | if (!empty($realpatharray[$i])) $imglinesize = pdf_getSizeForImage($realpatharray[$i]); |
||
| 581 | |||
| 582 | $pdf->setTopMargin($tab_top_newpage); |
||
| 583 | $pdf->setPageOrientation('', 1, $heightforfooter + $heightforfreetext + $heightforsignature + $heightforinfotot); // The only function to edit the bottom margin of current page to set it. |
||
| 584 | $pageposbefore = $pdf->getPage(); |
||
| 585 | |||
| 586 | $showpricebeforepagebreak = 1; |
||
| 587 | $posYAfterImage = 0; |
||
| 588 | $posYAfterDescription = 0; |
||
| 589 | |||
| 590 | if ($this->getColumnStatus('photo')) |
||
| 591 | { |
||
| 592 | // We start with Photo of product line |
||
| 593 | if (isset($imglinesize['width']) && isset($imglinesize['height']) && ($curY + $imglinesize['height']) > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforsignature + $heightforinfotot))) // If photo too high, we moved completely on new page |
||
| 594 | { |
||
| 595 | $pdf->AddPage('', '', true); |
||
| 596 | if (!empty($tplidx)) $pdf->useTemplate($tplidx); |
||
| 597 | $pdf->setPage($pageposbefore + 1); |
||
| 598 | |||
| 599 | $curY = $tab_top_newpage; |
||
| 600 | |||
| 601 | // Allows data in the first page if description is long enough to break in multiples pages |
||
| 602 | if (!empty($conf->global->MAIN_PDF_DATA_ON_FIRST_PAGE)) |
||
| 603 | $showpricebeforepagebreak = 1; |
||
| 604 | else $showpricebeforepagebreak = 0; |
||
| 605 | } |
||
| 606 | |||
| 607 | |||
| 608 | if (!empty($this->cols['photo']) && isset($imglinesize['width']) && isset($imglinesize['height'])) |
||
| 609 | { |
||
| 610 | $pdf->Image($realpatharray[$i], $this->getColumnContentXStart('photo'), $curY, $imglinesize['width'], $imglinesize['height'], '', '', '', 2, 300); // Use 300 dpi |
||
| 611 | // $pdf->Image does not increase value return by getY, so we save it manually |
||
| 612 | $posYAfterImage = $curY + $imglinesize['height']; |
||
| 613 | } |
||
| 614 | } |
||
| 615 | |||
| 616 | // Description of product line |
||
| 617 | if ($this->getColumnStatus('desc')) |
||
| 618 | { |
||
| 619 | $pdf->startTransaction(); |
||
| 620 | |||
| 621 | $this->printColDescContent($pdf, $curY, 'desc', $object, $i, $outputlangs, $hideref, $hidedesc); |
||
| 622 | $pageposafter = $pdf->getPage(); |
||
| 623 | |||
| 624 | if ($pageposafter > $pageposbefore) // There is a pagebreak |
||
| 625 | { |
||
| 626 | $pdf->rollbackTransaction(true); |
||
| 627 | |||
| 628 | $pdf->setPageOrientation('', 1, $heightforfooter); // The only function to edit the bottom margin of current page to set it. |
||
| 629 | |||
| 630 | $this->printColDescContent($pdf, $curY, 'desc', $object, $i, $outputlangs, $hideref, $hidedesc); |
||
| 631 | |||
| 632 | $pageposafter = $pdf->getPage(); |
||
| 633 | $posyafter = $pdf->GetY(); |
||
| 634 | //var_dump($posyafter); var_dump(($this->page_hauteur - ($heightforfooter+$heightforfreetext+$heightforinfotot))); exit; |
||
| 635 | if ($posyafter > ($this->page_hauteur - ($heightforfooter + $heightforfreetext + $heightforsignature + $heightforinfotot))) // There is no space left for total+free text |
||
| 636 | { |
||
| 637 | if ($i == ($nblines - 1)) // No more lines, and no space left to show total, so we create a new page |
||
| 638 | { |
||
| 639 | $pdf->AddPage('', '', true); |
||
| 640 | if (!empty($tplidx)) $pdf->useTemplate($tplidx); |
||
| 641 | $pdf->setPage($pageposafter + 1); |
||
| 642 | } |
||
| 643 | } else { |
||
| 644 | // We found a page break |
||
| 645 | // Allows data in the first page if description is long enough to break in multiples pages |
||
| 646 | if (!empty($conf->global->MAIN_PDF_DATA_ON_FIRST_PAGE)) |
||
| 647 | $showpricebeforepagebreak = 1; |
||
| 648 | else $showpricebeforepagebreak = 0; |
||
| 649 | } |
||
| 650 | } else // No pagebreak |
||
| 651 | { |
||
| 652 | $pdf->commitTransaction(); |
||
| 653 | } |
||
| 654 | $posYAfterDescription = $pdf->GetY(); |
||
| 655 | } |
||
| 656 | |||
| 657 | $nexY = $pdf->GetY(); |
||
| 658 | $pageposafter = $pdf->getPage(); |
||
| 659 | |||
| 660 | $pdf->setPage($pageposbefore); |
||
| 661 | $pdf->setTopMargin($this->marge_haute); |
||
| 662 | $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it. |
||
| 663 | |||
| 664 | // We suppose that a too long description or photo were moved completely on next page |
||
| 665 | if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) { |
||
| 666 | $pdf->setPage($pageposafter); $curY = $tab_top_newpage; |
||
| 667 | } |
||
| 668 | |||
| 669 | $pdf->SetFont('', '', $default_font_size - 1); // We reposition the default font |
||
| 670 | |||
| 671 | // VAT Rate |
||
| 672 | if ($this->getColumnStatus('vat')) |
||
| 673 | { |
||
| 674 | $vat_rate = pdf_getlinevatrate($object, $i, $outputlangs, $hidedetails); |
||
| 675 | $this->printStdColumnContent($pdf, $curY, 'vat', $vat_rate); |
||
| 676 | $nexY = max($pdf->GetY(), $nexY); |
||
| 677 | } |
||
| 678 | |||
| 679 | // Unit price before discount |
||
| 680 | if ($this->getColumnStatus('subprice')) |
||
| 681 | { |
||
| 682 | $up_excl_tax = pdf_getlineupexcltax($object, $i, $outputlangs, $hidedetails); |
||
| 683 | $this->printStdColumnContent($pdf, $curY, 'subprice', $up_excl_tax); |
||
| 684 | $nexY = max($pdf->GetY(), $nexY); |
||
| 685 | } |
||
| 686 | |||
| 687 | // Quantity |
||
| 688 | // Enough for 6 chars |
||
| 689 | if ($this->getColumnStatus('qty')) |
||
| 690 | { |
||
| 691 | $qty = pdf_getlineqty($object, $i, $outputlangs, $hidedetails); |
||
| 692 | $this->printStdColumnContent($pdf, $curY, 'qty', $qty); |
||
| 693 | $nexY = max($pdf->GetY(), $nexY); |
||
| 694 | } |
||
| 695 | |||
| 696 | |||
| 697 | // Unit |
||
| 698 | if ($this->getColumnStatus('unit')) |
||
| 699 | { |
||
| 700 | $unit = pdf_getlineunit($object, $i, $outputlangs, $hidedetails, $hookmanager); |
||
| 701 | $this->printStdColumnContent($pdf, $curY, 'unit', $unit); |
||
| 702 | $nexY = max($pdf->GetY(), $nexY); |
||
| 703 | } |
||
| 704 | |||
| 705 | // Discount on line |
||
| 706 | if ($this->getColumnStatus('discount') && $object->lines[$i]->remise_percent) |
||
| 707 | { |
||
| 708 | $remise_percent = pdf_getlineremisepercent($object, $i, $outputlangs, $hidedetails); |
||
| 709 | $this->printStdColumnContent($pdf, $curY, 'discount', $remise_percent); |
||
| 710 | $nexY = max($pdf->GetY(), $nexY); |
||
| 711 | } |
||
| 712 | |||
| 713 | // Total excl tax line (HT) |
||
| 714 | if ($this->getColumnStatus('totalexcltax')) |
||
| 715 | { |
||
| 716 | $total_excl_tax = pdf_getlinetotalexcltax($object, $i, $outputlangs, $hidedetails); |
||
| 717 | $this->printStdColumnContent($pdf, $curY, 'totalexcltax', $total_excl_tax); |
||
| 718 | $nexY = max($pdf->GetY(), $nexY); |
||
| 719 | } |
||
| 720 | |||
| 721 | // Total with tax line (TTC) |
||
| 722 | if ($this->getColumnStatus('totalincltax')) |
||
| 723 | { |
||
| 724 | $total_incl_tax = pdf_getlinetotalwithtax($object, $i, $outputlangs, $hidedetails); |
||
| 725 | $this->printStdColumnContent($pdf, $curY, 'totalincltax', $total_incl_tax); |
||
| 726 | $nexY = max($pdf->GetY(), $nexY); |
||
| 727 | } |
||
| 728 | |||
| 729 | // Extrafields |
||
| 730 | if (!empty($object->lines[$i]->array_options)) { |
||
| 731 | foreach ($object->lines[$i]->array_options as $extrafieldColKey => $extrafieldValue) { |
||
| 732 | if ($this->getColumnStatus($extrafieldColKey)) |
||
| 733 | { |
||
| 734 | $extrafieldValue = $this->getExtrafieldContent($object->lines[$i], $extrafieldColKey); |
||
| 735 | $this->printStdColumnContent($pdf, $curY, $extrafieldColKey, $extrafieldValue); |
||
| 736 | $nexY = max($pdf->GetY(), $nexY); |
||
| 737 | } |
||
| 738 | } |
||
| 739 | } |
||
| 740 | |||
| 741 | $parameters = array( |
||
| 742 | 'object' => $object, |
||
| 743 | 'i' => $i, |
||
| 744 | 'pdf' =>& $pdf, |
||
| 745 | 'curY' =>& $curY, |
||
| 746 | 'nexY' =>& $nexY, |
||
| 747 | 'outputlangs' => $outputlangs, |
||
| 748 | 'hidedetails' => $hidedetails |
||
| 749 | ); |
||
| 750 | $reshook = $hookmanager->executeHooks('printPDFline', $parameters, $this); // Note that $object may have been modified by hook |
||
| 751 | |||
| 752 | |||
| 753 | // Collection of totals by value of vat in $this->tva["rate"] = total_tva |
||
| 754 | if (!empty($conf->multicurrency->enabled) && $object->multicurrency_tx != 1) $tvaligne = $object->lines[$i]->multicurrency_total_tva; |
||
| 755 | else $tvaligne = $object->lines[$i]->total_tva; |
||
| 756 | |||
| 757 | $localtax1ligne = $object->lines[$i]->total_localtax1; |
||
| 758 | $localtax2ligne = $object->lines[$i]->total_localtax2; |
||
| 759 | $localtax1_rate = $object->lines[$i]->localtax1_tx; |
||
| 760 | $localtax2_rate = $object->lines[$i]->localtax2_tx; |
||
| 761 | $localtax1_type = $object->lines[$i]->localtax1_type; |
||
| 762 | $localtax2_type = $object->lines[$i]->localtax2_type; |
||
| 763 | |||
| 764 | if ($object->remise_percent) $tvaligne -= ($tvaligne * $object->remise_percent) / 100; |
||
| 765 | if ($object->remise_percent) $localtax1ligne -= ($localtax1ligne * $object->remise_percent) / 100; |
||
| 766 | if ($object->remise_percent) $localtax2ligne -= ($localtax2ligne * $object->remise_percent) / 100; |
||
| 767 | |||
| 768 | $vatrate = (string) $object->lines[$i]->tva_tx; |
||
| 769 | |||
| 770 | // Retrieve type from database for backward compatibility with old records |
||
| 771 | if ((!isset($localtax1_type) || $localtax1_type == '' || !isset($localtax2_type) || $localtax2_type == '') // if tax type not defined |
||
| 772 | && (!empty($localtax1_rate) || !empty($localtax2_rate))) // and there is local tax |
||
| 773 | { |
||
| 774 | $localtaxtmp_array = getLocalTaxesFromRate($vatrate, 0, $object->thirdparty, $mysoc); |
||
| 775 | $localtax1_type = $localtaxtmp_array[0]; |
||
| 776 | $localtax2_type = $localtaxtmp_array[2]; |
||
| 777 | } |
||
| 778 | |||
| 779 | // retrieve global local tax |
||
| 780 | if ($localtax1_type && $localtax1ligne != 0) { |
||
| 781 | $this->localtax1[$localtax1_type][$localtax1_rate] += $localtax1ligne; |
||
| 782 | } |
||
| 783 | if ($localtax2_type && $localtax2ligne != 0) { |
||
| 784 | $this->localtax2[$localtax2_type][$localtax2_rate] += $localtax2ligne; |
||
| 785 | } |
||
| 786 | |||
| 787 | if (($object->lines[$i]->info_bits & 0x01) == 0x01) $vatrate .= '*'; |
||
| 788 | if (!isset($this->tva[$vatrate])) $this->tva[$vatrate] = 0; |
||
| 789 | $this->tva[$vatrate] += $tvaligne; |
||
| 790 | |||
| 791 | if ($posYAfterImage > $posYAfterDescription) $nexY = max($nexY, $posYAfterImage); |
||
| 792 | |||
| 793 | // Add line |
||
| 794 | if (!empty($conf->global->MAIN_PDF_DASH_BETWEEN_LINES) && $i < ($nblines - 1)) { |
||
| 795 | $pdf->setPage($pageposafter); |
||
| 796 | $pdf->SetLineStyle(array('dash'=>'1,1', 'color'=>array(80, 80, 80))); |
||
| 797 | //$pdf->SetDrawColor(190,190,200); |
||
| 798 | $pdf->line($this->marge_gauche, $nexY, $this->page_largeur - $this->marge_droite, $nexY); |
||
| 799 | $pdf->SetLineStyle(array('dash'=>0)); |
||
| 800 | } |
||
| 801 | |||
| 802 | |||
| 803 | |||
| 804 | // Detect if some page were added automatically and output _tableau for past pages |
||
| 805 | while ($pagenb < $pageposafter) { |
||
| 806 | $pdf->setPage($pagenb); |
||
| 807 | if ($pagenb == $pageposbeforeprintlines) { |
||
| 808 | $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1, $object->multicurrency_code, $outputlangsbis); |
||
| 809 | } else { |
||
| 810 | $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1, $object->multicurrency_code, $outputlangsbis); |
||
| 811 | } |
||
| 812 | $this->_pagefoot($pdf, $object, $outputlangs, 1); |
||
| 813 | $pagenb++; |
||
| 814 | $pdf->setPage($pagenb); |
||
| 815 | $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it. |
||
| 816 | if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $this->_pagehead($pdf, $object, 0, $outputlangs); |
||
| 817 | } |
||
| 818 | |||
| 819 | if (isset($object->lines[$i + 1]->pagebreak) && $object->lines[$i + 1]->pagebreak) { |
||
| 820 | if ($pagenb == $pageposafter) { |
||
| 821 | $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, $hidetop, 1, $object->multicurrency_code, $outputlangsbis); |
||
| 822 | } else { |
||
| 823 | $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1, $object->multicurrency_code, $outputlangsbis); |
||
| 824 | } |
||
| 825 | $this->_pagefoot($pdf, $object, $outputlangs, 1); |
||
| 826 | // New page |
||
| 827 | $pdf->AddPage(); |
||
| 828 | if (!empty($tplidx)) $pdf->useTemplate($tplidx); |
||
| 829 | $pagenb++; |
||
| 830 | if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $this->_pagehead($pdf, $object, 0, $outputlangs); |
||
| 831 | } |
||
| 832 | } |
||
| 833 | |||
| 834 | // Show square |
||
| 835 | if ($pagenb == $pageposbeforeprintlines) |
||
| 836 | { |
||
| 837 | $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforsignature - $heightforfooter, 0, $outputlangs, $hidetop, 0, $object->multicurrency_code, $outputlangsbis); |
||
| 838 | $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforsignature - $heightforfooter + 1; |
||
| 839 | } else { |
||
| 840 | $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforsignature - $heightforfooter, 0, $outputlangs, 1, 0, $object->multicurrency_code, $outputlangsbis); |
||
| 841 | $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforsignature - $heightforfooter + 1; |
||
| 842 | } |
||
| 843 | |||
| 844 | // Display infos area |
||
| 845 | $posy = $this->drawInfoTable($pdf, $object, $bottomlasttab, $outputlangs); |
||
| 846 | |||
| 847 | // Display total zone |
||
| 848 | $posy = $this->drawTotalTable($pdf, $object, 0, $bottomlasttab, $outputlangs); |
||
| 849 | |||
| 850 | // Display payment area |
||
| 851 | /* |
||
| 852 | if ($deja_regle || $amount_credit_notes_included || $amount_deposits_included) |
||
| 853 | { |
||
| 854 | $posy=$this->drawPaymentsTable($pdf, $object, $posy, $outputlangs); |
||
| 855 | } |
||
| 856 | */ |
||
| 857 | |||
| 858 | // Customer signature area |
||
| 859 | if (empty($conf->global->PROPAL_DISABLE_SIGNATURE)) |
||
| 860 | { |
||
| 861 | $posy = $this->drawSignatureArea($pdf, $object, $posy, $outputlangs); |
||
| 862 | } |
||
| 863 | |||
| 864 | // Pagefoot |
||
| 865 | $this->_pagefoot($pdf, $object, $outputlangs); |
||
| 866 | if (method_exists($pdf, 'AliasNbPages')) $pdf->AliasNbPages(); |
||
| 867 | |||
| 868 | //If propal merge product PDF is active |
||
| 869 | if (!empty($conf->global->PRODUIT_PDF_MERGE_PROPAL)) |
||
| 870 | { |
||
| 871 | require_once DOL_DOCUMENT_ROOT.'/product/class/propalmergepdfproduct.class.php'; |
||
| 872 | |||
| 873 | $already_merged = array(); |
||
| 874 | foreach ($object->lines as $line) { |
||
| 875 | if (!empty($line->fk_product) && !(in_array($line->fk_product, $already_merged))) { |
||
| 876 | // Find the desire PDF |
||
| 877 | $filetomerge = new Propalmergepdfproduct($this->db); |
||
| 878 | |||
| 879 | if ($conf->global->MAIN_MULTILANGS) { |
||
| 880 | $filetomerge->fetch_by_product($line->fk_product, $outputlangs->defaultlang); |
||
| 881 | } else { |
||
| 882 | $filetomerge->fetch_by_product($line->fk_product); |
||
| 883 | } |
||
| 884 | |||
| 885 | $already_merged[] = $line->fk_product; |
||
| 886 | |||
| 887 | $product = new Product($this->db); |
||
| 888 | $product->fetch($line->fk_product); |
||
| 889 | |||
| 890 | if ($product->entity != $conf->entity) { |
||
| 891 | $entity_product_file = $product->entity; |
||
| 892 | } else { |
||
| 893 | $entity_product_file = $conf->entity; |
||
| 894 | } |
||
| 895 | |||
| 896 | // If PDF is selected and file is not empty |
||
| 897 | if (count($filetomerge->lines) > 0) { |
||
| 898 | foreach ($filetomerge->lines as $linefile) { |
||
| 899 | if (!empty($linefile->id) && !empty($linefile->file_name)) { |
||
| 900 | if (!empty($conf->global->PRODUCT_USE_OLD_PATH_FOR_PHOTO)) |
||
| 901 | { |
||
| 902 | if (!empty($conf->product->enabled)) { |
||
| 903 | $filetomerge_dir = $conf->product->multidir_output[$entity_product_file].'/'.get_exdir($product->id, 2, 0, 0, $product, 'product').$product->id."/photos"; |
||
| 904 | } elseif (!empty($conf->service->enabled)) { |
||
| 905 | $filetomerge_dir = $conf->service->multidir_output[$entity_product_file].'/'.get_exdir($product->id, 2, 0, 0, $product, 'product').$product->id."/photos"; |
||
| 906 | } |
||
| 907 | } else { |
||
| 908 | if (!empty($conf->product->enabled)) { |
||
| 909 | $filetomerge_dir = $conf->product->multidir_output[$entity_product_file].'/'.get_exdir(0, 0, 0, 0, $product, 'product').dol_sanitizeFileName($product->ref); |
||
| 910 | } elseif (!empty($conf->service->enabled)) { |
||
| 911 | $filetomerge_dir = $conf->service->multidir_output[$entity_product_file].'/'.get_exdir(0, 0, 0, 0, $product, 'product').dol_sanitizeFileName($product->ref); |
||
| 912 | } |
||
| 913 | } |
||
| 914 | |||
| 915 | dol_syslog(get_class($this).':: upload_dir='.$filetomerge_dir, LOG_DEBUG); |
||
| 916 | |||
| 917 | $infile = $filetomerge_dir.'/'.$linefile->file_name; |
||
| 918 | if (file_exists($infile) && is_readable($infile)) { |
||
| 919 | $pagecount = $pdf->setSourceFile($infile); |
||
| 920 | for ($i = 1; $i <= $pagecount; $i++) { |
||
| 921 | $tplIdx = $pdf->importPage($i); |
||
| 922 | if ($tplIdx !== false) { |
||
| 923 | $s = $pdf->getTemplatesize($tplIdx); |
||
| 924 | $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L'); |
||
| 925 | $pdf->useTemplate($tplIdx); |
||
| 926 | } else { |
||
| 927 | setEventMessages(null, array($infile.' cannot be added, probably protected PDF'), 'warnings'); |
||
| 928 | } |
||
| 929 | } |
||
| 930 | } |
||
| 931 | } |
||
| 932 | } |
||
| 933 | } |
||
| 934 | } |
||
| 935 | } |
||
| 936 | } |
||
| 937 | |||
| 938 | $pdf->Close(); |
||
| 939 | |||
| 940 | $pdf->Output($file, 'F'); |
||
| 941 | |||
| 942 | //Add pdfgeneration hook |
||
| 943 | $hookmanager->initHooks(array('pdfgeneration')); |
||
| 944 | $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs); |
||
| 945 | global $action; |
||
| 946 | $reshook = $hookmanager->executeHooks('afterPDFCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 947 | if ($reshook < 0) |
||
| 948 | { |
||
| 949 | $this->error = $hookmanager->error; |
||
| 950 | $this->errors = $hookmanager->errors; |
||
| 951 | } |
||
| 952 | |||
| 953 | if (!empty($conf->global->MAIN_UMASK)) |
||
| 954 | @chmod($file, octdec($conf->global->MAIN_UMASK)); |
||
| 955 | |||
| 956 | $this->result = array('fullpath'=>$file); |
||
| 957 | |||
| 958 | return 1; // No error |
||
| 959 | } else { |
||
| 960 | $this->error = $langs->trans("ErrorCanNotCreateDir", $dir); |
||
| 961 | return 0; |
||
| 962 | } |
||
| 963 | } else { |
||
| 964 | $this->error = $langs->trans("ErrorConstantNotDefined", "PROP_OUTPUTDIR"); |
||
| 965 | return 0; |
||
| 966 | } |
||
| 2037 |