function generatePDF($id_invoice, $introduction_letter = false, $target = 'file', $docs = false) { if (!is_numeric($id_invoice)) { error_log('$id_invoice not defined'); exit(1); } if (!defined('EURO')) { define('EURO', chr(128)); } $facture = Facture::getInfos($id_invoice); foreach (array(LC_MESSAGES, LC_TIME, LC_MONETARY, LC_CTYPE) as $locale) { setlocale($locale, $facture->language . ".UTF-8") or die("locale {$locale} language failed {$facture->language}"); } bindtextdomain('webfinance', dirname(__FILE__) . '/../../lang') or die("Set gettext bindtextdomain language failed\n"); textdomain('webfinance') or die("Set gettext textdomain language failed\n"); $type_doc = $facture->type_doc; if ($facture->language == 'en_US' and $facture->type_doc == 'facture') { $type_doc = 'invoice'; } if ($facture->language == 'en_US' and $facture->type_doc == 'devis') { $type_doc = 'quote'; } // Generate PDF filename $filename = sys_get_temp_dir() . '/' . ucfirst($type_doc) . ' ' . preg_replace('/[^a-z0-9éàçùî\\._-]/i', ' ', $facture->num_facture . ' ' . $facture->nom_client) . '.pdf'; foreach ($facture as $n => $v) { if (!is_array($v)) { $facture->{$n} = preg_replace("/€/", "EUROSYMBOL", $facture->{$n}); // FPDF ne support pas l'UTF-8 $facture->{$n} = utf8_decode($facture->{$n}); $facture->{$n} = preg_replace("/EUROSYMBOL/", chr(128), $facture->{$n}); $facture->{$n} = preg_replace("/\\\\EUR\\{([0-9.,]+)\\}/", "\\1 " . chr(128), $facture->{$n}); } } $pdf = new FPDI('P', 'mm', 'A4'); // Address $address = "{$facture->nom_client}\n"; for ($i = 0; $i < 3; $i++) { $n = sprintf("addr%d", $i + 1); if ($facture->{$n} != "") { $address .= $facture->{$n} . "\n"; } } $address .= "{$facture->cp} {$facture->ville}\n{$facture->pays}"; //Get docs, if needed if ($docs !== false) { $pagecount = $pdf->SetSourceFile(dirname(__FILE__) . '/../admin/invoice_docs/docs_' . $facture->language . '.pdf'); for ($n = 1; $n <= $pagecount; $n++) { $tplidx = $pdf->ImportPage($n, '/MediaBox'); $pdf->AddPage(); $pdf->UseTemplate($tplidx); } } $pdf->SetMargins(10, 10, 10); $pdf->SetDisplayMode('fullwidth'); $pdf->AddPage(); $prefs = new WebfinancePreferences(); // UTF8 to ISO $prefs->prefs['societe']->invoice_top_line1 = preg_replace("/€/", "EUROSYMBOL", $prefs->prefs['societe']->invoice_top_line1); // FPDF ne support pas l'UTF-8 $prefs->prefs['societe']->invoice_top_line1 = utf8_decode($prefs->prefs['societe']->invoice_top_line1); $prefs->prefs['societe']->invoice_top_line1 = preg_replace("/EUROSYMBOL/", chr(128), $prefs->prefs['societe']->invoice_top_line1); // Save the logo to a temp file since fpdf cannot read from a var $tempfile_logo = tempnam(sys_get_temp_dir(), 'logo') . '.png'; $logo_tmp = fopen($tempfile_logo, "w"); fwrite($logo_tmp, $prefs->prefs['logo']); fclose($logo_tmp); // Logo $pdf->Image($tempfile_logo, 90, 5, 25, 0, 'PNG'); // Display text headers $pdf->SetFont('Arial', '', 5); $logo_size = getimagesize($tempfile_logo); $logo_height = $logo_size[1] * 25 / $logo_size[0]; $pdf->SetXY(10, $logo_height + 5); $pdf->Cell(190, 5, $prefs->prefs['societe']->invoice_top_line1, 0, 0, "C"); $pdf->SetLineWidth(0.3); $pdf->SetXY(10, $logo_height + 8); $pdf->Cell(190, 5, utf8_decode($prefs->prefs['societe']->invoice_top_line2), "B", 0, "C"); // Address $pdf->SetFont('Arial', 'B', 11); $pdf->SetXY(115, 50); $pdf->Cell(80, 5, $facture->nom_client, 0, 0); $pdf->SetFont('Arial', '', 11); $y = 54; for ($i = 0; $i < 3; $i++) { $n = sprintf("addr%d", $i + 1); if ($facture->{$n} != "") { $pdf->SetXY(115, $y); $pdf->Cell(80, 5, $facture->{$n}, 0, 0); $y += 5; } } $pdf->SetXY(115, $y); $pdf->Cell(80, 4, $facture->cp . " " . $facture->ville, 0, 0); $pdf->SetXY(115, $y + 5); $pdf->Cell(80, 4, $facture->pays, 0, 0); // Donnees factures $pdf->SetXY(10, 19 + $logo_height); $pdf->SetFont('Arial', 'B', 14); $pdf->Cell(60, 4, ucfirst($type_doc) . utf8_decode(_(' #')) . $facture->num_facture); $pdf->SetFont('Arial', '', 9); $pdf->SetXY(10, 27 + $logo_height); $pdf->Cell(60, 4, $societe->ville . " " . utf8_decode(_("on")) . " " . strftime("%d/%m/%Y", $facture->timestamp_date_facture)); $pdf->SetXY(10, 32 + $logo_height); if (!empty($societe->tva_intracommunautaire)) { $pdf->Cell(60, 4, utf8_decode(_("VAT code")) . " " . $societe->raison_sociale . " : {$societe->tva_intracommunautaire}\n"); } if (!empty($facture->vat_number)) { $pdf->Write(5, utf8_decode(_("Your VAT code")) . " : {$facture->vat_number}\n"); } $pdf->Write(5, $facture->ref_contrat . "\n"); $pdf->Write(5, wordwrap($facture->extra_top, 70)); // Lignes de facturation $pdf->SetLineWidth(0.1); $pdf->SetXY(10, 80); $pdf->SetFont('Arial', 'B', '10'); $pdf->Cell(110, 6, utf8_decode(_("Designation")), 1); $pdf->Cell(20, 6, utf8_decode(_("Quantity")), 1, 0, "C"); $pdf->Cell(30, 6, utf8_decode(_("VAT excl.")), 1, 0, "C"); $pdf->Cell(30, 6, utf8_decode(_("Total")), 1, 0, "C"); $pdf->Ln(); $total_ht = 0; foreach ($facture->lignes as $ligne) { foreach ($ligne as $n => $v) { $ligne->{$n} = preg_replace("/€/", "EUROSYMBOL", $ligne->{$n}); $ligne->{$n} = utf8_decode($ligne->{$n}); $ligne->{$n} = preg_replace("/EUROSYMBOL/", chr(128), $ligne->{$n}); } setlocale(LC_TIME, "fr_FR.UTF8"); // Replace dates like YYYY-MM-DD with nice expanded date $ligne->description = preg_replace_callback('/\\d{4}-\\d{2}-\\d{2}/', create_function('$matches', 'return utf8_decode(strftime("%e %B %Y", strtotime($matches[0])));'), $ligne->description); $y_start = $pdf->getY(); $pdf->SetFont('Arial', '', '10'); $pdf->MultiCell(110, 6, $ligne->description, "LR", 'L'); $x = $pdf->getX(); $y = $pdf->getY(); $pdf->setXY(120, $y_start); $pdf->Cell(20, 6, $ligne->qtt, "LR", 0, "C"); $pdf->Cell(30, 6, preg_replace("/\\./", ",", sprintf("%.2f" . EURO, $ligne->prix_ht)), "LR", 0, "R"); $pdf->Cell(30, 6, preg_replace("/\\./", ",", sprintf("%.2f" . EURO, $ligne->prix_ht * $ligne->qtt)), "LR", 0, "R"); $pdf->setXY(120, $y_start); $pdf->Cell(20, $y - $y_start, '', "LR", 0, "C"); $pdf->Cell(30, $y - $y_start, '', "LR", 0, "R"); $pdf->Cell(30, $y - $y_start, '', "LR", 0, "R"); $total_ht += $ligne->prix_ht * $ligne->qtt; $pdf->Ln(); $pdf->Cell(110, 2, "", "LR"); $pdf->Cell(20, 2, "", "LR"); $pdf->Cell(30, 2, "", "LR"); $pdf->Cell(30, 2, "", "LR"); $pdf->Ln(); } $y_fin = $pdf->getY(); if ($y < 190) { $pdf->Cell(110, 190 - $y, "", "LRB", 0, "C"); $pdf->Cell(20, 190 - $y, "", "LRB", 0, "C"); $pdf->Cell(30, 190 - $y, "", "LRB", 0, "C"); $pdf->Cell(30, 190 - $y, "", "LRB", 0, "C"); $pdf->Ln(); } // Total HT $txt_type_paiement = $facture->type_paiement; if (preg_match('/eption de cette facture/', $txt_type_paiement) and $facture->language != 'fr_FR') { $txt_type_paiement = _('upon receipt of invoice'); } $pdf->SetFont('Arial', '', '11'); $pdf->Cell(130, 6, utf8_decode(_("Payment")) . " : " . $txt_type_paiement); $pdf->Cell(30, 6, utf8_decode(_("Subtotal")), "", 0, "R"); $pdf->Cell(30, 6, preg_replace("/\\./", ",", sprintf("%.2f" . EURO, $total_ht)), "", 0, "R"); $pdf->Ln(); // TVA $pdf->Cell(130, 6, ""); $pdf->Cell(30, 6, utf8_decode(_("VAT")) . " " . str_replace('.', ',', $facture->taxe) . "%", "", 0, "R"); $pdf->Cell(30, 6, preg_replace("/\\./", ",", sprintf("%.2f" . EURO, $facture->taxe / 100 * $total_ht)), "", 0, "R"); $pdf->Ln(); // Solde à régler $pdf->SetFont('Arial', 'B', '11'); $pdf->Cell(130, 6, ""); $pdf->Cell(30, 6, utf8_decode(_('Total')), "", 0, "R"); $pdf->Cell(30, 6, preg_replace("/\\./", ",", sprintf("%.2f" . EURO, (1 + $facture->taxe / 100) * $total_ht - $facture->accompte)), "", 0, "R"); $pdf->Ln(); $pdf->Ln(); $pdf->SetFont('Arial', '', '8'); $pdf->MultiCell(190, 4, utf8_decode(_('Discount not practiced by the company. In case of default in the invoice due date, the client agrees to pay a penalty pursuant to the provisions of Article 1226 of the Civil Code, an increase of 15% in addition to the main total without any notice of default being required.'))); // Extra data $pdf->SetFont('Arial', '', '10'); if (!empty($facture->extra_bottom)) { $pdf->Ln(10); $pdf->MultiCell(120, 6, $facture->extra_bottom, 0); } // RIB $result = mysql_query('SELECT value ' . 'FROM webfinance_pref ' . 'WHERE id_pref=' . $facture->id_compte) or die(mysql_error()); list($cpt) = mysql_fetch_array($result); mysql_free_result($result); $cpt = unserialize(base64_decode($cpt)); if (!is_object($cpt)) { echo "compte Impossible de generer la facture. <a " . "href='../admin/societe'>Vous devez saisir au moins un compte " . "bancaire dans les options pour emettre des factures</a>"; exit(1); } foreach ($cpt as $n => $v) { $cpt->{$n} = utf8_decode($cpt->{$n}); } $pdf->SetFont('Arial', 'B', '10'); $pdf->Ln(); $pdf->Cell(160, 6, utf8_decode(_("Bank references")) . " ", "LTR", 0, "C"); $pdf->Ln(); $pdf->SetFont('Arial', '', '8'); $pdf->Cell(35, 6, utf8_decode(_("Bank")) . " : ", "L"); $pdf->Cell(125, 6, $cpt->banque, "R"); $pdf->Ln(); $pdf->Cell(35, 6, "IBAN : ", "L"); $pdf->Cell(125, 6, $cpt->iban, "R"); $pdf->Ln(); $pdf->Cell(35, 6, "SWIFT/BIC : ", "LB"); $pdf->Cell(125, 6, $cpt->swift, "BR"); $pdf->Ln(); $pdf->SetAuthor($societe->raison_sociale); $pdf->SetCreator('Webfinance $Id: gen_facture.php 532 2012-11-10 10:32:19Z pierre $ Using FPDF'); $pdf->SetSubject(ucfirst($facture->type_doc) . utf8_decode(_(' #')) . " " . $facture->num_facture . " " . utf8_decode(_("for")) . " " . $facture->nom_client); $pdf->SetTitle(ucfirst($facture->type_doc) . utf8_decode(_(' #')) . " " . $facture->num_facture); if ($target == 'file') { $pdf->Output($filename, 'F'); } else { $pdf->Output(basename($filename), 'I'); } $pdf->Close(); // Delete temporary logo file unlink($tempfile_logo); global $language; foreach (array(LC_MESSAGES, LC_TIME, LC_MONETARY, LC_CTYPE) as $locale) { setlocale($locale, $language . ".UTF-8") or die("locale {$locale} language failed {$language}"); } bindtextdomain('webfinance', dirname(__FILE__) . '/../../lang') or die("Set gettext bindtextdomain language failed\n"); textdomain('webfinance') or die("Set gettext textdomain language failed\n"); return $filename; }