/**
  * Création du second type d'en-tête possible d'un justificatif, celui-ci étant plus léger 
  * 
  * @param int $nb le numéro de la page
  * 
  * @return void
  */
 function ajoutEntete2($nb)
 {
     $this->pdf->setFont($this->fontb, '', 12);
     $this->pdf->WriteHTML("<h4>Justificatif de remboursement</h4>");
     $this->pdf->setFont($this->font, '', 8);
     $this->pdf->SetFillColor(255, 255, 255);
     $this->pdf->SetDrawColor(0);
     $this->pdf->Rect(10, 18, 180, 20);
     $auteur = substr($this->auteur["adresse1"], 0, 29) . " " . $this->auteur["cp"] . " " . $this->auteur["ville"];
     $presta = $this->fourn_presta;
     $presta_adresse = substr($presta["adresse1"], 0, 29) . " " . $presta["0"]->cp . " " . $presta["0"]->ville;
     $lignes = array(array("Document", "Identification", $this->facture->_id . " " . CMbDT::format(null, "%d.%m.%Y %H:%M:%S"), "", "Page {$nb}"), array("Auteur", "N° EAN(B)", $this->auteur["EAN"], $this->auteur["nom"], " Tél: " . $this->auteur["tel"]), array("Facture", "N° RCC(B)", $this->auteur["RCC"], $auteur, "Fax: " . $this->auteur["fax"]), array("Four.de", "N° EAN(P)", $presta["EAN"], $presta["nom_dr"], " Tél: " . $presta["0"]->tel), array("prestations", "N° RCC(B)", $presta["RCC"], $presta_adresse, "Fax: " . $presta["0"]->fax));
     $this->pdf->setXY(10, $this->pdf->getY() - 4);
     foreach ($lignes as $ligne) {
         $this->pdf->setXY(10, $this->pdf->getY() + 4);
         foreach ($ligne as $key => $value) {
             $this->pdf->Cell($this->colonnes[$key], "", $value);
         }
     }
 }
 * @author     SARL OpenXtrem <*****@*****.**>
 * @license    GNU General Public License, see http://www.gnu.org/licenses/gpl.html
 * @version    $Revision: 26699 $
 */
CCanDo::checkRead();
$reception_id = CValue::get('reception_id');
$lot_id = CValue::get('lot_id');
$force_print = CValue::get('force_print');
$reception = new CProductReception();
if ($reception_id) {
    $reception->load($reception_id);
    $reception->loadRefsFwd();
    $reception->loadRefsBack();
}
$pdf = new CMbPdf();
$pdf->setFont("vera", '', "10");
// Définition des marges de la pages
//$pdf->SetMargins(15, 15);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetAutoPageBreak(0);
// Creation d'une nouvelle page
$pdf->AddPage();
if ($reception->_id) {
    $lots = $reception->_ref_reception_items;
} else {
    $lot = new CProductOrderItemReception();
    $lot->load($lot_id);
    $lots = array($lot);
}
$data = array();
 /**
  * Edition du journal des rappels
  *
  * @return void
  */
 function editRappel()
 {
     $colonnes = array("Concerne" => 25, "Destinataire" => 25, "N° fact." => 15, "Débit" => 15, "Crédit" => 15, "Solde" => 15, "Echéance" => 10, "Pas de rappel jusqu'au" => 15);
     $this->editTableau($colonnes, 5, 25);
     $pos_colonne = array(5, 55, 135, 165, 195);
     $rappel_nom = " ";
     $totaux_rappel = array();
     $debut_lignes = 30;
     $ligne = 0;
     foreach ($this->relances as $relance) {
         // Une page par type de rappel
         if (!strstr($rappel_nom, CAppUI::tr("CRelance.statut." . $relance->statut))) {
             if ($ligne != 0) {
                 $ligne++;
                 $pos_ligne = $debut_lignes + $ligne * 4;
                 // Total à chaque fin de type de rappel
                 $this->editCell($pos_colonne[0], $pos_ligne, 45, "Total {$rappel_nom}", "L");
                 $this->editCell($pos_colonne[1], $pos_ligne, 15, "(" . $totaux_rappel[$rappel_nom]["Nombre"] . " rappels)", "L");
                 $this->editCell($pos_colonne[2], $pos_ligne, 15, sprintf("%.2f", $totaux_rappel[$rappel_nom]["Debit"]), "R");
                 $this->editCell($pos_colonne[3], $pos_ligne, 15, sprintf("%.2f", $totaux_rappel[$rappel_nom]["Credit"]), "R");
                 $this->editCell($pos_colonne[4], $pos_ligne, 15, sprintf("%.2f", $totaux_rappel[$rappel_nom]["Solde"]), "R");
                 $ligne = $this->ajoutPage($colonnes);
             }
             $rappel_nom = CAppUI::tr("CRelance.statut." . $relance->statut);
             if ($relance->poursuite) {
                 $rappel_nom .= " - " . $relance->poursuite;
             }
             $totaux_rappel[$rappel_nom] = array("Nombre" => 0.0, "Debit" => 0.0, "Credit" => 0.0, "Solde" => 0.0);
             $this->pdf->setFont($this->font, '', 10);
             $this->editCell(80, 15, 160, $rappel_nom, "C");
             $this->pdf->setFont($this->font, '', 8);
         }
         $this->pdf->setX(5);
         $ligne++;
         $totaux_rappel[$rappel_nom]["Nombre"]++;
         $totaux_rappel[$rappel_nom]["Debit"] += $relance->_ref_object->_montant_avec_remise;
         $totaux_rappel[$rappel_nom]["Credit"] += $relance->_ref_object->_reglements_total_patient;
         $totaux_rappel[$rappel_nom]["Solde"] += $relance->_ref_object->_du_restant_patient;
         $valeurs = array("Concerne" => $relance->_ref_object->_ref_patient->nom . " " . $relance->_ref_object->_ref_patient->prenom, "Destinataire" => $this->loadGarant($relance->_ref_object), "N° fact." => $relance->_ref_object->_view, "Débit" => sprintf("%.2f", $relance->_ref_object->_montant_avec_remise), "Crédit" => sprintf("%.2f", $relance->_ref_object->_reglements_total_patient), "Solde" => sprintf("%.2f", $relance->_ref_object->_du_restant_patient), "Echéance" => CMbDT::transform("", $relance->_ref_object->_echeance, '%d/%m/%Y'), "Pas de rappel jusqu'au" => CMbDT::transform("", CMbDT::date("+1 DAY", $relance->date), '%d/%m/%Y'));
         $x = 0;
         foreach ($colonnes as $key => $value) {
             $cote = $key == "Débit" || $key == "Crédit" || $key == "Solde" ? "R" : "L";
             $this->editCell($this->pdf->getX() + $x, $debut_lignes + $ligne * 4, $value, $valeurs[$key], $cote);
             $x = $value;
         }
         if ($debut_lignes + $ligne * 4 >= 200) {
             $ligne = $this->ajoutPage($colonnes);
         }
     }
     $ligne++;
     $pos_ligne = $debut_lignes + $ligne * 4;
     // Total à chaque fin de type de rappel
     if ($rappel_nom != " ") {
         $this->editCell($pos_colonne[0], $pos_ligne, 45, "Total {$rappel_nom}", "L");
         $this->editCell($pos_colonne[1], $pos_ligne, 15, "(" . $totaux_rappel[$rappel_nom]["Nombre"] . " rappels)", "L");
         $this->editCell($pos_colonne[2], $pos_ligne, 15, sprintf("%.2f", $totaux_rappel[$rappel_nom]["Debit"]), "R");
         $this->editCell($pos_colonne[3], $pos_ligne, 15, sprintf("%.2f", $totaux_rappel[$rappel_nom]["Credit"]), "R");
         $this->editCell($pos_colonne[4], $pos_ligne, 15, sprintf("%.2f", $totaux_rappel[$rappel_nom]["Solde"]), "R");
     }
     // Un récapitulatif par type
     $colonnes = array("Code rappel" => 50, "Nombre" => 25, "Debit" => 25, "Credit" => 25, "Solde" => 25);
     $colonnes_debut = array("Code rappel" => 5, "Nombre" => 105, "Debit" => 155, "Credit" => 205, "Solde" => 255);
     $this->ajoutPage($colonnes);
     $this->pdf->setFont($this->font, '', 10);
     $this->editCell(80, 15, 160, "Récapitulatif", "C");
     $this->pdf->setFont($this->font, '', 9);
     $ligne = 0;
     $total_final = array("Nombre" => 0.0, "Debit" => 0.0, "Credit" => 0.0, "Solde" => 0.0);
     foreach ($totaux_rappel as $compte => $valeurs) {
         $ligne++;
         $pos_ligne = $debut_lignes + $ligne * 4;
         $this->editCell(5, $pos_ligne, 80, $compte, "L");
         foreach ($valeurs as $key => $value) {
             $cote = $key == "Code rappel" ? "L" : "R";
             $valeur = $key == "Code rappel" || $key == "Nombre" ? $value : sprintf("%.2f", $value);
             $x = $colonnes_debut[$key];
             $this->editCell($x, $pos_ligne, $colonnes[$key], $valeur, $cote);
             if ($key != "Code rappel") {
                 $total_final[$key] += $valeur;
             }
         }
     }
     $pos_colonne = array(5, 105, 155, 205, 255);
     $ligne += 2;
     $pos_ligne = $debut_lignes + $ligne * 4;
     $this->editCell($pos_colonne[0], $pos_ligne, 80, "Total général", "L");
     $this->editCell($pos_colonne[1], $pos_ligne, 25, $total_final["Nombre"], "R");
     $this->editCell($pos_colonne[2], $pos_ligne, 25, sprintf("%.2f", $total_final["Debit"]), "R");
     $this->editCell($pos_colonne[3], $pos_ligne, 25, sprintf("%.2f", $total_final["Credit"]), "R");
     $this->editCell($pos_colonne[4], $pos_ligne, 25, sprintf("%.2f", $total_final["Solde"]), "R");
 }
 function printEtiquettes($printer_id = null, $stream = 1)
 {
     // Affectation de la police par défault si aucune n'est choisie
     if ($this->font == "") {
         $this->font = "dejavusansmono";
     }
     // Calcul des dimensions de l'étiquette
     $largeur_etiq = ($this->largeur_page - 2 * $this->marge_horiz) / $this->nb_colonnes;
     $hauteur_etiq = ($this->hauteur_page - 2 * $this->marge_vert) / $this->nb_lignes;
     // Création du PDF
     $pdf = new CMbPdf('P', 'cm', array($this->largeur_page, $this->hauteur_page));
     $pdf->setFont($this->font, '', $this->hauteur_ligne);
     $pdf->setPrintHeader(false);
     $pdf->setPrintFooter(false);
     $pdf->SetMargins($this->marge_horiz, $this->marge_vert, $this->marge_horiz);
     $pdf->SetAutoPageBreak(0, $this->marge_vert);
     $pdf->AddPage();
     $distinct_texts = 1;
     $textes = array();
     $pays = CAppUI::conf("ref_pays");
     // La fonction nl2br ne fait qu'ajouter la balise <br />, elle ne supprime pas le \n.
     // Il faut donc le faire manuellement.
     $textes[1] = preg_replace("/[\t\r\n\f]/", '', utf8_encode(nl2br($this->texte)));
     if ($this->texte_2) {
         $distinct_texts++;
         $textes[] = preg_replace("/[\t\r\n\f]/", '', utf8_encode(nl2br($this->texte_2)));
     }
     if ($this->texte_3) {
         $distinct_texts++;
         $textes[] = preg_replace("/[\t\r\n\f]/", '', utf8_encode(nl2br($this->texte_3)));
     }
     if ($this->texte_4) {
         $distinct_texts++;
         $textes[] = preg_replace("/[\t\r\n\f]/", '', utf8_encode(nl2br($this->texte_4)));
     }
     $nb_etiqs = $this->nb_lignes * $this->nb_colonnes;
     $increment = floor($nb_etiqs / $distinct_texts);
     $current_text = 1;
     // Création de la grille d'étiquettes et écriture du contenu.
     for ($i = 0; $i < $nb_etiqs; $i++) {
         if ($i != 0 && $i % $increment == 0 && isset($textes[$current_text + 1])) {
             $current_text++;
         }
         if (round($pdf->GetX()) >= round($this->largeur_page - 2 * $this->marge_horiz)) {
             $pdf->SetX(0);
             $pdf->SetLeftMargin($this->marge_horiz);
             $pdf->SetY($pdf->GetY() + $hauteur_etiq);
         }
         if ($this->show_border) {
             $pdf->Rect($pdf->GetX(), $pdf->GetY(), $largeur_etiq, $hauteur_etiq, 'D');
         }
         $x = $pdf->GetX();
         $y = $pdf->GetY();
         $pdf->SetLeftMargin($x);
         // On affecte la marge droite de manière à ce que la méthode Write fasse un retour chariot
         // lorsque le contenu écrit va dépasser la largeur de l'étiquette
         $pdf->SetRightMargin($this->largeur_page - $x - $largeur_etiq);
         $fragments = explode("@", $textes[$current_text]);
         $was_barcode = 0;
         CMbArray::removeValue("", $fragments);
         // Evaluation de la hauteur du contenu de la cellule
         // si un alignement spécifique est demandé.
         if ($this->text_align != "top") {
             $pdf_ex = new CMbPdf('p', 'cm', array($largeur_etiq, $hauteur_etiq));
             $pdf_ex->setFont($this->font, '', $this->hauteur_ligne);
             $pdf_ex->SetMargins(0, 0, 0);
             $pdf_ex->setPrintHeader(false);
             $pdf_ex->setPrintFooter(false);
             $pdf_ex->SetAutoPageBreak(false);
             $pdf_ex->AddPage();
             foreach ($fragments as $fragment) {
                 if (preg_match("/BARCODE_(.*)/", $fragment, $matches) == 1) {
                     switch ($pays) {
                         case "2":
                             $save_x = $pdf_ex->getX();
                             $pdf_ex->setY($pdf_ex->getY() + 0.4);
                             $pdf_ex->setX($save_x);
                             $barcode = $matches[1];
                             $pdf_ex->setFont("C39HrP24DhTt", '', 30);
                             $pdf_ex->WriteHTML($barcode, false);
                             $pdf_ex->setFont($this->font, '', $this->hauteur_ligne);
                             break;
                         default:
                             $barcode_x = $pdf_ex->getX() + 0.15;
                             $barcode_y = $pdf_ex->getY();
                             $barcode = $matches[1];
                             $barcode_width = strlen($barcode) * 0.4 + 0.4;
                             $pdf_ex->writeBarcode($barcode_x, $barcode_y, $barcode_width, 0.8, "C128B", 1, null, null, $barcode, 25);
                             $pdf_ex->setX($barcode_x + $barcode_width);
                     }
                     $was_barcode = 1;
                 } else {
                     if ($was_barcode) {
                         $sub_fragments = explode("<br />", $fragment, 2);
                         $pdf_ex->WriteHTML($sub_fragments[0], false);
                         if (isset($sub_fragments[1])) {
                             $actual_y = $pdf_ex->getY();
                             $pdf_ex->setY($actual_y + 0.8);
                             $pdf_ex->WriteHTML($sub_fragments[1], false);
                         }
                     } else {
                         $pdf_ex->WriteHTML($fragment, false);
                     }
                     $was_barcode = 0;
                 }
             }
             $pdf_y = $pdf->getY();
             $pdf_ex_y = $pdf_ex->getY();
             switch ($this->text_align) {
                 case "middle":
                     $pdf->setY($pdf_y - 0.2 + ($hauteur_etiq - $pdf_ex_y) / 2);
                     break;
                 case "bottom":
                     $pdf->setY($pdf_y - 0.4 + $hauteur_etiq - $pdf_ex_y);
             }
         }
         foreach ($fragments as $fragment) {
             if (preg_match("/BARCODE_(.*)/", $fragment, $matches) == 1) {
                 switch ($pays) {
                     case "2":
                         // La position x est à remettre car perdue lors de la méthode setY
                         $save_x = $pdf->getX();
                         $pdf->setY($pdf->getY() + 0.4);
                         $pdf->setX($save_x);
                         $barcode = $matches[1];
                         $pdf->setFont("C39HrP24DhTt", '', 30);
                         $pdf->WriteHTML("*{$barcode}*", false);
                         $pdf->setFont($this->font, '', $this->hauteur_ligne);
                         break;
                     default:
                         $barcode_x = $pdf->getX() + 0.15;
                         $barcode_y = $pdf->getY();
                         $barcode = $matches[1];
                         $barcode_width = strlen($barcode) * 0.4 + 0.4;
                         $pdf->writeBarcode($barcode_x, $barcode_y, $barcode_width, 0.8, "C128B", 1, null, null, $barcode);
                         $pdf->setX($barcode_x + $barcode_width);
                 }
                 $was_barcode = 1;
             } else {
                 if ($was_barcode) {
                     $sub_fragments = explode("<br />", $fragment, 2);
                     $pdf->WriteHTML($sub_fragments[0], false);
                     if (isset($sub_fragments[1])) {
                         $actual_y = $pdf->getY();
                         $pdf->setY($actual_y + 0.8);
                         $pdf->WriteHTML($sub_fragments[1], false);
                     }
                 } else {
                     $pdf->WriteHTML($fragment, false);
                 }
                 $was_barcode = 0;
             }
         }
         $x = $x + $largeur_etiq;
         $pdf->SetY($y);
         $pdf->SetX($x);
     }
     if ($printer_id) {
         $file = new CFile();
         $file->_file_path = tempnam("/tmp", "etiq");
         file_put_contents($file->_file_path, $pdf->Output($this->nom . '.pdf', "S"));
         $printer = new CPrinter();
         $printer->load($printer_id);
         $printer->loadRefSource()->sendDocument($file);
         unlink($file->_file_path);
     } else {
         if ($stream) {
             $pdf->Output($this->nom . '.pdf', "I");
         } else {
             return $pdf->OutPut($this->nom . '.pdf', "S");
         }
     }
 }