| Conditions | 45 |
| Paths | > 20000 |
| Total Lines | 328 |
| Code Lines | 189 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 135 | function write_file($object, $outputlangs='', $srctemplatepath='', $hidedetails=0, $hidedesc=0, $hideref=0) |
||
| 136 | { |
||
| 137 | global $user,$langs,$conf,$mysoc,$hookmanager; |
||
| 138 | |||
| 139 | // Get source company |
||
| 140 | if (! is_object($object->thirdparty)) $object->fetch_thirdparty(); |
||
| 141 | if (! is_object($object->thirdparty)) $object->thirdparty=$mysoc; // If fetch_thirdparty fails, object has no socid (specimen) |
||
| 142 | $this->emetteur=$object->thirdparty; |
||
| 143 | if (! $this->emetteur->country_code) $this->emetteur->country_code=substr($langs->defaultlang,-2); // By default, if was not defined |
||
| 144 | |||
| 145 | if (! is_object($outputlangs)) $outputlangs=$langs; |
||
| 146 | // For backward compatibility with FPDF, force output charset to ISO, because FPDF expect text to be encoded in ISO |
||
| 147 | if (! empty($conf->global->MAIN_USE_FPDF)) $outputlangs->charset_output='ISO-8859-1'; |
||
| 148 | |||
| 149 | $outputlangs->load("main"); |
||
| 150 | $outputlangs->load("dict"); |
||
| 151 | $outputlangs->load("companies"); |
||
| 152 | $outputlangs->load("bills"); |
||
| 153 | $outputlangs->load("products"); |
||
| 154 | $outputlangs->load("suppliers"); |
||
| 155 | $object->factures = array(); |
||
| 156 | |||
| 157 | if ($conf->fournisseur->payment->dir_output) |
||
| 158 | { |
||
| 159 | $object->fetch_thirdparty(); |
||
| 160 | /** |
||
| 161 | * Supplier invoice list |
||
| 162 | */ |
||
| 163 | $sql = 'SELECT f.rowid, f.ref, f.datef, f.ref_supplier, f.total_ht, f.total_tva, f.total_ttc, pf.amount, f.rowid as facid, f.paye'; |
||
| 164 | $sql .= ', f.fk_statut, s.nom as name, s.rowid as socid'; |
||
| 165 | $sql .= ' FROM '.MAIN_DB_PREFIX.'paiementfourn_facturefourn as pf,'.MAIN_DB_PREFIX.'facture_fourn as f,'.MAIN_DB_PREFIX.'societe as s'; |
||
| 166 | $sql .= ' WHERE pf.fk_facturefourn = f.rowid AND f.fk_soc = s.rowid'; |
||
| 167 | $sql .= ' AND pf.fk_paiementfourn = '.$object->id; |
||
| 168 | $resql=$this->db->query($sql); |
||
| 169 | if ($resql) |
||
| 170 | { |
||
| 171 | if ($this->db->num_rows($resql) > 0) |
||
| 172 | { |
||
| 173 | while($objp = $this->db->fetch_object($resql)) { |
||
| 174 | $objp->type = $outputlangs->trans('SupplierInvoice'); |
||
| 175 | $object->lines[] = $objp; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | $total = $object->montant; |
||
| 181 | |||
| 182 | // Definition of $dir and $file |
||
| 183 | if ($object->specimen) |
||
| 184 | { |
||
| 185 | $dir = $conf->fournisseur->payment->dir_output; |
||
| 186 | $file = $dir . "/SPECIMEN.pdf"; |
||
| 187 | } |
||
| 188 | else |
||
| 189 | { |
||
| 190 | $objectref = dol_sanitizeFileName($object->ref); |
||
| 191 | $objectrefsupplier = dol_sanitizeFileName($object->ref_supplier); |
||
| 192 | $dir = $conf->fournisseur->payment->dir_output.'/'.$objectref; |
||
| 193 | $file = $dir . "/" . $objectref . ".pdf"; |
||
| 194 | if (! empty($conf->global->SUPPLIER_REF_IN_NAME)) $file = $dir . "/" . $objectref . ($objectrefsupplier?"_".$objectrefsupplier:"").".pdf"; |
||
| 195 | } |
||
| 196 | |||
| 197 | if (! file_exists($dir)) |
||
| 198 | { |
||
| 199 | if (dol_mkdir($dir) < 0) |
||
| 200 | { |
||
| 201 | $this->error=$langs->transnoentities("ErrorCanNotCreateDir",$dir); |
||
| 202 | return 0; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | if (file_exists($dir)) |
||
| 207 | { |
||
| 208 | // Add pdfgeneration hook |
||
| 209 | if (! is_object($hookmanager)) |
||
| 210 | { |
||
| 211 | include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; |
||
| 212 | $hookmanager=new HookManager($this->db); |
||
| 213 | } |
||
| 214 | $hookmanager->initHooks(array('pdfgeneration')); |
||
| 215 | $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs); |
||
| 216 | global $action; |
||
| 217 | $reshook=$hookmanager->executeHooks('beforePDFCreation',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks |
||
| 218 | |||
| 219 | $nblignes = count($object->lines); |
||
| 220 | |||
| 221 | $pdf=pdf_getInstance($this->format); |
||
| 222 | $default_font_size = pdf_getPDFFontSize($outputlangs); // Must be after pdf_getInstance |
||
| 223 | $heightforinfotot = 50; // Height reserved to output the info and total part |
||
| 224 | $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 |
||
| 225 | $heightforfooter = $this->marge_basse + 8; // Height reserved to output the footer (value include bottom margin) |
||
| 226 | $pdf->SetAutoPageBreak(1,0); |
||
| 227 | |||
| 228 | if (class_exists('TCPDF')) |
||
| 229 | { |
||
| 230 | $pdf->setPrintHeader(false); |
||
| 231 | $pdf->setPrintFooter(false); |
||
| 232 | } |
||
| 233 | $pdf->SetFont(pdf_getPDFFont($outputlangs)); |
||
| 234 | // Set path to the background PDF File |
||
| 235 | if (empty($conf->global->MAIN_DISABLE_FPDI) && ! empty($conf->global->MAIN_ADD_PDF_BACKGROUND)) |
||
| 236 | { |
||
| 237 | $pagecount = $pdf->setSourceFile($conf->mycompany->dir_output.'/'.$conf->global->MAIN_ADD_PDF_BACKGROUND); |
||
| 238 | $tplidx = $pdf->importPage(1); |
||
| 239 | } |
||
| 240 | |||
| 241 | $pdf->Open(); |
||
| 242 | $pagenb=0; |
||
| 243 | $pdf->SetDrawColor(128,128,128); |
||
| 244 | |||
| 245 | $pdf->SetTitle($outputlangs->convToOutputCharset($object->ref)); |
||
| 246 | $pdf->SetSubject($outputlangs->transnoentities("Invoice")); |
||
| 247 | $pdf->SetCreator("Dolibarr ".DOL_VERSION); |
||
| 248 | $pdf->SetAuthor($outputlangs->convToOutputCharset($user->getFullName($outputlangs))); |
||
| 249 | $pdf->SetKeyWords($outputlangs->convToOutputCharset($object->ref)." ".$outputlangs->transnoentities("Order")." ".$outputlangs->convToOutputCharset($object->thirdparty->name)); |
||
| 250 | if (! empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) $pdf->SetCompression(false); |
||
| 251 | |||
| 252 | $pdf->SetMargins($this->marge_gauche, $this->marge_haute, $this->marge_droite); // Left, Top, Right |
||
| 253 | |||
| 254 | |||
| 255 | // New page |
||
| 256 | $pdf->AddPage(); |
||
| 257 | if (! empty($tplidx)) $pdf->useTemplate($tplidx); |
||
| 258 | $pagenb++; |
||
| 259 | $this->_pagehead($pdf, $object, 1, $outputlangs); |
||
| 260 | $pdf->SetFont('','', $default_font_size - 1); |
||
| 261 | $pdf->MultiCell(0, 3, ''); // Set interline to 3 |
||
| 262 | $pdf->SetTextColor(0,0,0); |
||
| 263 | |||
| 264 | $tab_top = 90; |
||
| 265 | $tab_top_newpage = (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)?42:10); |
||
| 266 | $tab_height = 130; |
||
| 267 | $tab_height_newpage = 150; |
||
| 268 | |||
| 269 | // Incoterm |
||
| 270 | $height_incoterms = 0; |
||
| 271 | |||
| 272 | $height_note=0; |
||
| 273 | |||
| 274 | $iniY = $tab_top + 7; |
||
| 275 | $curY = $tab_top + 7; |
||
| 276 | $nexY = $tab_top + 7; |
||
| 277 | |||
| 278 | // Loop on each lines |
||
| 279 | for ($i = 0 ; $i < $nblignes ; $i++) |
||
| 280 | { |
||
| 281 | $curY = $nexY; |
||
| 282 | $pdf->SetFont('','', $default_font_size - 1); // Into loop to work with multipage |
||
| 283 | $pdf->SetTextColor(0,0,0); |
||
| 284 | |||
| 285 | $pdf->setTopMargin($tab_top_newpage); |
||
| 286 | $pdf->setPageOrientation('', 1, $heightforfooter+$heightforfreetext+$heightforinfotot); // The only function to edit the bottom margin of current page to set it. |
||
| 287 | $pageposbefore=$pdf->getPage(); |
||
| 288 | |||
| 289 | // Description of product line |
||
| 290 | $curX = $this->posxdate-1; |
||
| 291 | $showpricebeforepagebreak=1; |
||
| 292 | |||
| 293 | $pdf->startTransaction(); |
||
| 294 | //pdf_writelinedesc($pdf,$object,$i,$outputlangs,$this->posxtva-$curX,3,$curX,$curY,$hideref,$hidedesc,1); |
||
| 295 | $pdf->writeHTMLCell($this->posxtva-$curX, 4, $curX, $curY, $object->lines[$i]->datef, 0, 1, false, true, 'J',true); |
||
| 296 | $pageposafter=$pdf->getPage(); |
||
| 297 | if ($pageposafter > $pageposbefore) // There is a pagebreak |
||
| 298 | { |
||
| 299 | $pdf->rollbackTransaction(true); |
||
| 300 | $pageposafter=$pageposbefore; |
||
| 301 | //print $pageposafter.'-'.$pageposbefore;exit; |
||
| 302 | $pdf->setPageOrientation('', 1, $heightforfooter); // The only function to edit the bottom margin of current page to set it. |
||
| 303 | //pdf_writelinedesc($pdf,$object,$i,$outputlangs,$this->posxtva-$curX,4,$curX,$curY,$hideref,$hidedesc,1); |
||
| 304 | $pdf->writeHTMLCell($this->posxtva-$curX, 4, $curX, $curY, $object->lines[$i]->datef, 0, 1, false, true, 'J',true); |
||
| 305 | $posyafter=$pdf->GetY(); |
||
| 306 | if ($posyafter > ($this->page_hauteur - ($heightforfooter+$heightforfreetext+$heightforinfotot))) // There is no space left for total+free text |
||
| 307 | { |
||
| 308 | if ($i == ($nblignes-1)) // No more lines, and no space left to show total, so we create a new page |
||
| 309 | { |
||
| 310 | $pdf->AddPage('','',true); |
||
| 311 | if (! empty($tplidx)) $pdf->useTemplate($tplidx); |
||
| 312 | if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $this->_pagehead($pdf, $object, 0, $outputlangs); |
||
| 313 | $pdf->setPage($pageposafter+1); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | else |
||
| 317 | { |
||
| 318 | // We found a page break |
||
| 319 | $showpricebeforepagebreak=0; |
||
| 320 | } |
||
| 321 | } |
||
| 322 | else // No pagebreak |
||
| 323 | { |
||
| 324 | $pdf->commitTransaction(); |
||
| 325 | } |
||
| 326 | |||
| 327 | $nexY = $pdf->GetY(); |
||
| 328 | $pageposafter=$pdf->getPage(); |
||
| 329 | $pdf->setPage($pageposbefore); |
||
| 330 | $pdf->setTopMargin($this->marge_haute); |
||
| 331 | $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it. |
||
| 332 | |||
| 333 | // We suppose that a too long description is moved completely on next page |
||
| 334 | if ($pageposafter > $pageposbefore && empty($showpricebeforepagebreak)) { |
||
| 335 | $pdf->setPage($pageposafter); $curY = $tab_top_newpage; |
||
| 336 | } |
||
| 337 | |||
| 338 | $pdf->SetFont('','', $default_font_size - 1); // On repositionne la police par defaut |
||
| 339 | |||
| 340 | // ref fourn |
||
| 341 | $pdf->SetXY($this->posxreffacturefourn, $curY); |
||
| 342 | $pdf->MultiCell($this->posxreffacturefourn-$this->posxup-0.8, 3, $object->lines[$i]->ref_supplier, 0, 'L', 0); |
||
| 343 | |||
| 344 | // ref facture fourn |
||
| 345 | $pdf->SetXY($this->posxreffacture, $curY); |
||
| 346 | $pdf->MultiCell($this->posxreffacture-$this->posxup-0.8, 3, $object->lines[$i]->ref, 0, 'L', 0); |
||
| 347 | |||
| 348 | // type |
||
| 349 | $pdf->SetXY($this->posxtype, $curY); |
||
| 350 | $pdf->MultiCell($this->posxtype-$this->posxup-0.8, 3, $object->lines[$i]->type, 0, 'L', 0); |
||
| 351 | |||
| 352 | // Total ht |
||
| 353 | $pdf->SetXY($this->posxtotalht, $curY); |
||
| 354 | $pdf->MultiCell($this->posxtotalht-$this->posxup-0.8, 3, price($object->lines[$i]->total_ht), 0, 'R', 0); |
||
| 355 | |||
| 356 | // Total tva |
||
| 357 | $pdf->SetXY($this->posxtva, $curY); |
||
| 358 | $pdf->MultiCell($this->posxtva-$this->posxup-0.8, 3, price($object->lines[$i]->total_tva), 0, 'R', 0); |
||
| 359 | |||
| 360 | // Total TTC line |
||
| 361 | $pdf->SetXY($this->posxtotalttc, $curY); |
||
| 362 | $pdf->MultiCell($this->page_largeur-$this->marge_droite-$this->posxtotalttc, 3, price($object->lines[$i]->total_ttc), 0, 'R', 0); |
||
| 363 | |||
| 364 | |||
| 365 | // Add line |
||
| 366 | if (! empty($conf->global->MAIN_PDF_DASH_BETWEEN_LINES) && $i < ($nblignes - 1)) |
||
| 367 | { |
||
| 368 | $pdf->setPage($pageposafter); |
||
| 369 | $pdf->SetLineStyle(array('dash'=>'1,1','color'=>array(80,80,80))); |
||
| 370 | //$pdf->SetDrawColor(190,190,200); |
||
| 371 | $pdf->line($this->marge_gauche, $nexY+1, $this->page_largeur - $this->marge_droite, $nexY+1); |
||
| 372 | $pdf->SetLineStyle(array('dash'=>0)); |
||
| 373 | } |
||
| 374 | |||
| 375 | $nexY+=2; // Passe espace entre les lignes |
||
| 376 | |||
| 377 | // Detect if some page were added automatically and output _tableau for past pages |
||
| 378 | while ($pagenb < $pageposafter) |
||
| 379 | { |
||
| 380 | $pdf->setPage($pagenb); |
||
| 381 | if ($pagenb == 1) |
||
| 382 | { |
||
| 383 | $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1); |
||
| 384 | } |
||
| 385 | else |
||
| 386 | { |
||
| 387 | $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1); |
||
| 388 | } |
||
| 389 | $this->_pagefoot($pdf,$object,$outputlangs,1); |
||
| 390 | $pagenb++; |
||
| 391 | $pdf->setPage($pagenb); |
||
| 392 | $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it. |
||
| 393 | if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $this->_pagehead($pdf, $object, 0, $outputlangs); |
||
| 394 | } |
||
| 395 | if (isset($object->lines[$i+1]->pagebreak) && $object->lines[$i+1]->pagebreak) |
||
| 396 | { |
||
| 397 | if ($pagenb == 1) |
||
| 398 | { |
||
| 399 | $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter, 0, $outputlangs, 0, 1); |
||
| 400 | } |
||
| 401 | else |
||
| 402 | { |
||
| 403 | $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1); |
||
| 404 | } |
||
| 405 | $this->_pagefoot($pdf,$object,$outputlangs,1); |
||
| 406 | // New page |
||
| 407 | $pdf->AddPage(); |
||
| 408 | if (! empty($tplidx)) $pdf->useTemplate($tplidx); |
||
| 409 | $pagenb++; |
||
| 410 | if (empty($conf->global->MAIN_PDF_DONOTREPEAT_HEAD)) $this->_pagehead($pdf, $object, 0, $outputlangs); |
||
| 411 | } |
||
| 412 | } |
||
| 413 | |||
| 414 | // Show square |
||
| 415 | if ($pagenb == 1) |
||
| 416 | { |
||
| 417 | $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 0, 0); |
||
| 418 | $bottomlasttab=$this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1; |
||
| 419 | } |
||
| 420 | else |
||
| 421 | { |
||
| 422 | $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0); |
||
| 423 | $bottomlasttab=$this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1; |
||
| 424 | } |
||
| 425 | |||
| 426 | // Affiche zone cheèque |
||
| 427 | $posy=$this->_tableau_cheque($pdf, $object, $bottomlasttab, $outputlangs); |
||
| 428 | |||
| 429 | // Affiche zone totaux |
||
| 430 | //$posy=$this->_tableau_tot($pdf, $object, $deja_regle, $bottomlasttab, $outputlangs); |
||
| 431 | |||
| 432 | // Pied de page |
||
| 433 | $this->_pagefoot($pdf, $object, $outputlangs); |
||
| 434 | if (method_exists($pdf,'AliasNbPages')) $pdf->AliasNbPages(); |
||
| 435 | |||
| 436 | $pdf->Close(); |
||
| 437 | |||
| 438 | $pdf->Output($file,'F'); |
||
| 439 | |||
| 440 | // Add pdfgeneration hook |
||
| 441 | $hookmanager->initHooks(array('pdfgeneration')); |
||
| 442 | $parameters=array('file'=>$file,'object'=>$object,'outputlangs'=>$outputlangs); |
||
| 443 | global $action; |
||
| 444 | $reshook=$hookmanager->executeHooks('afterPDFCreation',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks |
||
| 445 | |||
| 446 | if (! empty($conf->global->MAIN_UMASK)) |
||
| 447 | @chmod($file, octdec($conf->global->MAIN_UMASK)); |
||
| 448 | |||
| 449 | return 1; // Pas d'erreur |
||
| 450 | } |
||
| 451 | else |
||
| 452 | { |
||
| 453 | $this->error=$langs->trans("ErrorCanNotCreateDir",$dir); |
||
| 454 | return 0; |
||
| 455 | } |
||
| 456 | } |
||
| 457 | else |
||
| 458 | { |
||
| 459 | $this->error=$langs->trans("ErrorConstantNotDefined","SUPPLIER_OUTPUTDIR"); |
||
| 460 | return 0; |
||
| 461 | } |
||
| 462 | } |
||
| 463 | |||
| 781 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: