<?php

require 'database.inc.php';
//require("barcode/core.php");
/* CHANGE MADE By Genuix */
require("barcode.php");
/* END CHANGE MADE By Genuix */
//session_start();
//session_register("lg_user");
//session_register("lg_password");
//session_register("lg_admin");
//session_register("lg_menu1");
//session_register("lg_menu2");
//session_register("lg_menu3");
//session_register("lg_menu4");
//session_register("lg_menu5");
require('invoice.php');
if (Test_User($lg_user, $lg_password) == TRUE) {
  $NoPrefixe = "";
  if (isset($NoGarage) == False) {
    define('ID_GARAGE_PRINT', ID_GARAGE);
  } else {
    define('ID_GARAGE_PRINT', $NoGarage);
    $NoPrefixe = $NoGarage . "-";
  }
  $nb_lignes_max = 35;
  if (isset($Type) == False) {
    $Type = 0;
  }
  if (isset($Sous_Type) == False) {
    $Sous_Type = 0;
  }
  switch ($Type) {
    case 0:
      break;
    case 1:
      switch ($Sous_Type) {
        case 1:
          if ($ligne = Commande::getCommandeByNoCommande($NoCommande)) {
            $pdf = new INVOICE('P', 'mm', 'A4');
            $pdf->SetTitle("Facture " . $NoCommande);
            $pdf->SetSubject("Facture " . $NoCommande);
            $pdf->SetAuthor("Laurent");
            $pdf->SetCreator("Laurent");
            $pdf->SetDisplayMode("fullpage");
            $pdf->SetAutoPageBreak(false, 0);
            //$pdf->Open();
            $requette_test = "SELECT * FROM commandearticles" . ID_GARAGE_PRINT . " WHERE NoCommande=$NoCommande AND Rabais != 0.00";
            if ($resultat_test = Connection::executeRawQueryWithfetchAll($requette_test)) {
              $nb_result_test = count($resultat_test);
              if ($nb_result_test == 0) {
                $ColRabais = 0;
              } else {
                $ColRabais = 1;
              }
            }
            $requette_test = "SELECT * FROM commandearticles" . ID_GARAGE_PRINT . " WHERE NoCommande=$NoCommande AND PrixBase != 0.00 AND Quantite != 0.00 AND PrixMulti != 0.00 ORDER BY TVA";
            if ($resultat_test = Connection::executeRawQueryWithfetchAll($requette_test)) {
              $nb_result_test = count($resultat_test);
              if ($nb_result_test == 0) {
                $ColTVA = 0;
                $nb_tva = 0;
              } else {
                $ColTVA = 0;
                if ($ligne_test = $resultat_test[0]) {
                  $taux_tva = abs($ligne_test['TVA']);
                  $tot_tva["$taux_tva"] = 0;
                  $nb_tva = 1;
                }
                foreach ($resultat_test as $ligne_test) {
                  if ($taux_tva != (abs($ligne_test['TVA']))) {
                    $ColTVA = 1;
                    $taux_tva = abs($ligne_test['TVA']);
                    $tot_tva["$taux_tva"] = 0;
                    $nb_tva++;
                  }
                }
              }
            }
            if ($ligne['Type'] == "FO")
              $Mode_col = $NoArticle + ($ColRabais * 2);
            else
              $Mode_col = $NoArticle + ($ColRabais * 2) + ($ColTVA * 4);
            if ($resultat2 = CommandeArticle::getCommandeArticleByNoCommande($NoCommande)) {
              $idxResultat2 = 0;
              $nb_result2 = count($resultat2);
              $nb_pages = ((int)(($nb_result2 - 1) / $nb_lignes_max)) + 1;
              $tot_inter = 0;
              $tot_partiel = 0;
              $tot_partiel2 = 0;
              for ($cpt_page = 1; $cpt_page <= $nb_pages; $cpt_page++) {
                $pdf->AddPage();
                $pdf->Logo(Config::getLogo());
                $gdImage = GenBarCode($NoCommande);
                $pdf->GDImage($gdImage, 8, 45, 35, 15);
                $pdf->addSociete3(Config::getAdresseFacture(), Config::getNoTelephone(), Config::getNoFax(), Config::getEmail(), "IBAN : " . Config::getNoIBAN() . "      BIC(SWIFT) : " . Config::getNoBIC(), Config::getUrlWeb());

                switch ($ligne['Genre']) {
                  case "A":
                    $pdf->fact_dev("Achat ", $NoPrefixe . $NoCommande);
                    break;
                  case "B":
                    $pdf->fact_dev("Bulletin livraison ", $NoPrefixe . $NoCommande);
                    break;
                  case "C":
                    if ($ligne['Type'] == "RC") {
                      $pdf->fact_dev("Retour commande ", $NoPrefixe . $NoCommande);
                    } else {
                      $pdf->fact_dev("Commande ", $NoPrefixe . $NoCommande);
                    }
                    break;
                  case "D":
                    $pdf->fact_dev("Devis ", $NoPrefixe . $NoCommande);
                    break;
                  case "F":
                    $pdf->fact_dev("Facture ", $NoPrefixe . $NoCommande);
                    break;
                  case "G":
                    $pdf->fact_dev("Garantie ", $NoPrefixe . $NoCommande);
                    break;
                  case "O":
                    $pdf->fact_dev("Offre ", $NoPrefixe . $NoCommande);
                    break;
                  case "R":
                    $pdf->fact_dev("Reprise ", $NoPrefixe . $NoCommande);
                    break;
                  case "V":
                    $pdf->fact_dev("Achat ", $NoPrefixe . $NoCommande);
                    break;
                  case "N":
                    $pdf->fact_dev("Note de crédit ", $NoPrefixe . $NoCommande);
                    break;
                }
                //                    $pdf->temporaire( "Facture " . $NoCommande );
                $pdf->addDate(Cree_Entree_Type("vide", 2, 0, $ligne['DateCommande'], AFFICHAGE));
                $pdf->addClient($ligne['NoClient']);
                if ($nb_pages > 1)
                  $pdf->addPageNumber($cpt_page);
                if ($NoClient == 1)
                  $resultat3 = Client::getClientByNoClient($ligne['NoClient']);
                else
                  $resultat3 = Client::getClientByNoClient($ligne['NoClient2']);
                if ($resultat3) {
                  if ($ligne3 = $resultat3) {
                    $co = "";
                    if ($ligne3['Co'] != '') {
                      $co = "\nc/o " . $ligne3['Co'];
                    }
                    if ($ligne3['NPA'] != 0) {
                      $pdf->addClientAdresse($ligne3['Allocution'] . "\n" . $ligne3['Nom'] . " " . $ligne3['Prenom'] . $co . "\n" . $ligne3['Adresse'] . "\n" . $ligne3['NPA'] . " " . $ligne3['Localite']);
                    } else {
                      $pdf->addClientAdresse($ligne3['Allocution'] . "\n" . $ligne3['Nom'] . " " . $ligne3['Prenom'] . $co . "\n" . $ligne3['Adresse'] . "\n" . $ligne3['Localite']);
                    }
                  }
                }
                if (!empty($ligne['Vehicule'])) {
                  if ($ligne3 = Vehicule::getVehiculeByNoChassis($ligne['Vehicule'])) {
                    $pdf->addVehicule($ligne3['NoChasis'] . "\n" . $ligne3['MarqueModel'] . " " . $ligne3['Model'] . "\n" . $ligne['KmVhc'] . " km\n" . $ligne3['NoPlaque']);
                  }
                }
                if ($NoClient == 2) {
                  if ($ligne3 = Client::getClientByNoClient($ligne['NoClient'])) {
                    if ($ligne3['NPA'] != 0) {
                      $pdf->addClient2Adresse("Concerne : " . $ligne3['Allocution'] . " " . $ligne3['Nom'] . " " . $ligne3['Prenom'] . ", " . $ligne3['Adresse'] . " " . $ligne3['NPA'] . " " . $ligne3['Localite']);
                    } else {
                      $pdf->addClient2Adresse("Concerne : " . $ligne3['Allocution'] . " " . $ligne3['Nom'] . " " . $ligne3['Prenom'] . ", " . $ligne3['Adresse'] . " " . $ligne3['Localite']);
                    }
                  }
                }
                $cadre = 'N';
                switch ($Mode_col) {
                  case 0:
                    $cols = array(
                      "DESCRIPTION" => 100,
                      "PRIX UNIT"   => 30,
                      "QUANTITE"    => 30,
                      "TOTAL TTC"   => 30
                    );
                    $pdf->addCols($cols, $cadre);
                    $cols = array(
                      "DESCRIPTION" => "L",
                      "PRIX UNIT"   => "R",
                      "QUANTITE"    => "R",
                      "TOTAL TTC"   => "R"
                    );
                    $pdf->addLineFormat($cols);
                    break;
                  case 1:
                    $cols = array(
                      "NO ARTICLE"  => 42,
                      "DESCRIPTION" => 76,
                      "PRIX UNIT"   => 24,
                      "QUANTITE"    => 24,
                      "TOTAL TTC"   => 24
                    );
                    $pdf->addCols($cols, $cadre);
                    $cols = array(
                      "NO ARTICLE"  => "L",
                      "DESCRIPTION" => "L",
                      "PRIX UNIT"   => "R",
                      "QUANTITE"    => "R",
                      "TOTAL TTC"   => "R"
                    );
                    $pdf->addLineFormat($cols);
                    break;
                  case 2:
                    $cols = array(
                      "DESCRIPTION" => 80,
                      "PRIX UNIT"   => 28,
                      "RABAIS"      => 26,
                      "QUANTITE"    => 28,
                      "TOTAL TTC"   => 28
                    );
                    $pdf->addCols($cols, $cadre);
                    $cols = array(
                      "DESCRIPTION" => "L",
                      "PRIX UNIT"   => "R",
                      "RABAIS"      => "R",
                      "QUANTITE"    => "R",
                      "TOTAL TTC"   => "R"
                    );
                    $pdf->addLineFormat($cols);
                    break;
                  case 3:
                    $cols = array(
                      "NO ARTICLE"  => 39,
                      "DESCRIPTION" => 71,
                      "PRIX UNIT"   => 21,
                      "RABAIS"      => 17,
                      "QUANTITE"    => 21,
                      "TOTAL TTC"   => 21
                    );
                    $pdf->addCols($cols, $cadre);
                    $cols = array(
                      "NO ARTICLE"  => "L",
                      "DESCRIPTION" => "L",
                      "PRIX UNIT"   => "R",
                      "RABAIS"      => "R",
                      "QUANTITE"    => "R",
                      "TOTAL TTC"   => "R"
                    );
                    $pdf->addLineFormat($cols);
                    break;
                  case 4:
                    $cols = array(
                      "DESCRIPTION" => 80,
                      "PRIX UNIT"   => 28,
                      "TVA"         => 26,
                      "QUANTITE"    => 28,
                      "TOTAL TTC"   => 28
                    );
                    $pdf->addCols($cols, $cadre);
                    $cols = array(
                      "DESCRIPTION" => "L",
                      "PRIX UNIT"   => "R",
                      "TVA"         => "R",
                      "QUANTITE"    => "R",
                      "TOTAL TTC"   => "R"
                    );
                    $pdf->addLineFormat($cols);
                    break;
                  case 5:
                    $cols = array(
                      "NO ARTICLE"  => 39,
                      "DESCRIPTION" => 71,
                      "PRIX UNIT"   => 21,
                      "TVA"         => 17,
                      "QUANTITE"    => 21,
                      "TOTAL TTC"   => 21
                    );
                    $pdf->addCols($cols, $cadre);
                    $cols = array(
                      "NO ARTICLE"  => "L",
                      "DESCRIPTION" => "L",
                      "PRIX UNIT"   => "R",
                      "TVA"         => "R",
                      "QUANTITE"    => "R",
                      "TOTAL TTC"   => "R"
                    );
                    $pdf->addLineFormat($cols);
                    break;
                  case 6:
                    $cols = array(
                      "DESCRIPTION" => 74,
                      "PRIX UNIT"   => 24,
                      "RABAIS"      => 22,
                      "TVA"         => 22,
                      "QUANTITE"    => 24,
                      "TOTAL TTC"   => 24
                    );
                    $pdf->addCols($cols, $cadre);
                    $cols = array(
                      "DESCRIPTION" => "L",
                      "PRIX UNIT"   => "R",
                      "RABAIS"      => "R",
                      "TVA"         => "R",
                      "QUANTITE"    => "R",
                      "TOTAL TTC"   => "R"
                    );
                    $pdf->addLineFormat($cols);
                    break;
                  case 7:
                    $cols = array(
                      "NO ARTICLE"  => 36,
                      "DESCRIPTION" => 68,
                      "PRIX UNIT"   => 18,
                      "RABAIS"      => 16,
                      "TVA"         => 16,
                      "QUANTITE"    => 18,
                      "TOTAL TTC"   => 18
                    );
                    $pdf->addCols($cols, $cadre);
                    $cols = array(
                      "NO ARTICLE"  => "L",
                      "DESCRIPTION" => "L",
                      "PRIX UNIT"   => "R",
                      "RABAIS"      => "R",
                      "TVA"         => "R",
                      "QUANTITE"    => "R",
                      "TOTAL TTC"   => "R"
                    );
                    $pdf->addLineFormat($cols);
                    break;
                }
                $y = 99;
                $nb_lignes_page = $nb_result2 - (($cpt_page - 1) * $nb_lignes_max);
                if ($nb_lignes_page > $nb_lignes_max) {
                  $nb_lignes_page = $nb_lignes_max;
                }
                $interlignes = floor((150 - ($nb_lignes_page * 3)) / $nb_lignes_page);
                if ($interlignes < 1.35) {
                  $interlignes = 1.35;
                }
                if ($interlignes > 4) {
                  $interlignes = 4;
                }
                for ($cpt_ligne = 0; $cpt_ligne < $nb_lignes_page; $cpt_ligne++)
                //			        while ($ligne2 = mysql_fetch_array( $resultat2 ))
                {
                  $ligne2 = $resultat2[$idxResultat2];
                  $idxResultat2++;
                  if ($ligne2['TVA'] > 0) {
                    $prix_unit = Arrondi_5_ct($ligne2['PrixBase'] * ((100 + $ligne2['TVA']) / 100));
                  } else {
                    $prix_unit = $ligne2['PrixBase'];
                  }
                  if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                  } else {
                    $tmp = abs($ligne2['TVA']);
                    if ($ligne2['TypeData'] == 4) {
                      $tva_new = $tmp;
                      $tot_tva["$tva_norm"] += $ligne2['PrixMulti'];
                    } else {
                      $tva_norm = $tmp;
                      $tot_tva["$tmp"] += $ligne2['PrixMulti'];
                    }
                    if ($ligne2['TypeData'] == 0 || $ligne2['TypeData'] == 4) {
                      $tot_inter += $ligne2['PrixMulti'];
                      $tot_partiel += $ligne2['PrixMulti'];
                      $tot_partiel2 += $ligne2['PrixMulti'];
                    }
                    //					    Arrondi_5_ct($tot_inter)
                  }
                  switch ($Mode_col) {
                    case 0:
                      if ($ligne2['TypeData'] == 0) {
                        if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => ""
                          );
                          $size = $pdf->addLine($y, $line);
                        } else {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => $prix_unit,
                            "QUANTITE"    => $ligne2['Quantite'],
                            "TOTAL TTC"   => $ligne2['PrixMulti']
                          );
                          $size = $pdf->addLine($y, $line);
                        }
                      } else if ($ligne2['TypeData'] == 1) {
                        $size = $pdf->addTexte($y, $ligne2['Description']);
                      } else if ($ligne2['TypeData'] == 2) {
                        $line = array(
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total intermédiaire" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_inter)
                        );
                        $size = $pdf->addLine($y, $line, "B", 12);
                      } else if ($ligne2['TypeData'] == 4) {
                        $line = array(
                          "DESCRIPTION" => $ligne2['Description'],
                          "PRIX UNIT"   => $ligne2['PrixMulti'],
                          "QUANTITE"    => $ligne2['Quantite'],
                          "TOTAL TTC"   => $ligne2['PrixMulti']
                        );
                        $size = $pdf->addLine($y, $line);
                      } else if ($ligne2['TypeData'] == 5) {
                        $line = array(
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_partiel)
                        );
                        $size = $pdf->addLine($y, $line, "B");
                        $tot_partiel = 0;
                      } else if ($ligne2['TypeData'] == 6) {
                        $size = $pdf->addTexte($y, $ligne2['Description'], 'B');
                      } else if ($ligne2['TypeData'] == 7) {
                        $line = array(
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel 2" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_partiel2)
                        );
                        $size = $pdf->addLine($y, $line, "B", 12);
                        $tot_partiel2 = 0;
                      }
                      break;
                    case 1:
                      if ($ligne2['TypeData'] == 0) {
                        if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                          $line = array(
                            "NO ARTICLE"  => $ligne2['NoArticle'],
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => ""
                          );
                          $size = $pdf->addLine($y, $line);
                        } else {
                          $line = array(
                            "NO ARTICLE"  => $ligne2['NoArticle'],
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => $prix_unit,
                            "QUANTITE"    => $ligne2['Quantite'],
                            "TOTAL TTC"   => $ligne2['PrixMulti']
                          );
                          $size = $pdf->addLine($y, $line);
                        }
                      } else if ($ligne2['TypeData'] == 1) {
                        $size = $pdf->addTexte($y, $ligne2['Description']);
                      } else if ($ligne2['TypeData'] == 2) {
                        $line = array(
                          "NO ARTICLE"  => "",
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total intermédiaire" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_inter)
                        );
                        $size = $pdf->addLine($y, $line, '', 12);
                      } else if ($ligne2['TypeData'] == 4) {
                        $line = array(
                          "NO ARTICLE"  => $ligne2['NoArticle'],
                          "DESCRIPTION" => $ligne2['Description'],
                          "PRIX UNIT"   => $ligne2['PrixMulti'],
                          "QUANTITE"    => $ligne2['Quantite'],
                          "TOTAL TTC"   => $ligne2['PrixMulti']
                        );
                        $size = $pdf->addLine($y, $line);
                      } else if ($ligne2['TypeData'] == 5) {
                        $line = array(
                          "NO ARTICLE"  => "",
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_partiel)
                        );
                        $size = $pdf->addLine($y, $line);
                        $tot_partiel = 0;
                      } else if ($ligne2['TypeData'] == 6) {
                        $size = $pdf->addTexte($y, $ligne2['Description'], 'B');
                      } else if ($ligne2['TypeData'] == 7) {
                        $line = array(
                          "NO ARTICLE"  => "",
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel 2" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_partiel2)
                        );
                        $size = $pdf->addLine($y, $line, "B", 12);
                        $tot_partiel2 = 0;
                      }
                      break;
                    case 2:
                      if ($ligne2['TypeData'] == 0) {
                        if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "RABAIS"      => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => ""
                          );
                          $size = $pdf->addLine($y, $line);
                        } else {
                          if ($ligne2['Rabais'] != 0.00)
                            $line = array(
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => $prix_unit,
                              "RABAIS"      => $ligne2['Rabais'],
                              "QUANTITE"    => $ligne2['Quantite'],
                              "TOTAL TTC"   => $ligne2['PrixMulti']
                            );
                          else
                            $line = array(
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => $prix_unit,
                              "RABAIS"      => "",
                              "QUANTITE"    => $ligne2['Quantite'],
                              "TOTAL TTC"   => $ligne2['PrixMulti']
                            );
                          $size = $pdf->addLine($y, $line);
                        }
                      } else if ($ligne2['TypeData'] == 1) {
                        $size = $pdf->addTexte($y, $ligne2['Description']);
                      } else if ($ligne2['TypeData'] == 2) {
                        $line = array(
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total intermédiaire" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "RABAIS"      => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_inter)
                        );
                        $size = $pdf->addLine($y, $line, "B", 12);
                      } else if ($ligne2['TypeData'] == 4) {
                        $line = array(
                          "DESCRIPTION" => $ligne2['Description'],
                          "PRIX UNIT"   => $ligne2['PrixMulti'],
                          "RABAIS"      => "",
                          "QUANTITE"    => $ligne2['Quantite'],
                          "TOTAL TTC"   => $ligne2['PrixMulti']
                        );
                        $size = $pdf->addLine($y, $line);
                      } else if ($ligne2['TypeData'] == 5) {
                        $line = array(
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "RABAIS"      => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_partiel)
                        );
                        $size = $pdf->addLine($y, $line, "B");
                        $tot_partiel = 0;
                      } else if ($ligne2['TypeData'] == 6) {
                        $size = $pdf->addTexte($y, $ligne2['Description'], 'B');
                      } else if ($ligne2['TypeData'] == 7) {
                        $line = array(
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel 2" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "RABAIS"      => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_partiel2)
                        );
                        $size = $pdf->addLine($y, $line, "B", 12);
                        $tot_partiel2 = 0;
                      }
                      break;
                    case 3:
                      if ($ligne2['TypeData'] == 0) {
                        if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                          $line = array(
                            "NO ARTICLE"  => $ligne2['NoArticle'],
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "RABAIS"      => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => ""
                          );
                          $size = $pdf->addLine($y, $line);
                        } else {
                          if ($ligne2['Rabais'] != 0.00)
                            $line = array(
                              "NO ARTICLE"  => $ligne2['NoArticle'],
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => $prix_unit,
                              "RABAIS"      => $ligne2['Rabais'],
                              "QUANTITE"    => $ligne2['Quantite'],
                              "TOTAL TTC"   => $ligne2['PrixMulti']
                            );
                          else
                            $line = array(
                              "NO ARTICLE"  => $ligne2['NoArticle'],
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => $prix_unit,
                              "RABAIS"      => "",
                              "QUANTITE"    => $ligne2['Quantite'],
                              "TOTAL TTC"   => $ligne2['PrixMulti']
                            );
                          $size = $pdf->addLine($y, $line);
                        }
                      } else if ($ligne2['TypeData'] == 1) {
                        $size = $pdf->addTexte($y, $ligne2['Description']);
                      } else if ($ligne2['TypeData'] == 2) {
                        $line = array(
                          "NO ARTICLE"  => "",
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total intermédiaire" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "RABAIS"      => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_inter)
                        );
                        $size = $pdf->addLine($y, $line, "B", 12);
                      } else if ($ligne2['TypeData'] == 4) {
                        $line = array(
                          "NO ARTICLE"  => $ligne2['NoArticle'],
                          "DESCRIPTION" => $ligne2['Description'],
                          "PRIX UNIT"   => $ligne2['PrixMulti'],
                          "RABAIS"      => "",
                          "QUANTITE"    => $ligne2['Quantite'],
                          "TOTAL TTC"   => $ligne2['PrixMulti']
                        );
                        $size = $pdf->addLine($y, $line);
                      } else if ($ligne2['TypeData'] == 5) {
                        $line = array(
                          "NO ARTICLE"  => "",
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "RABAIS"      => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_partiel)
                        );
                        $size = $pdf->addLine($y, $line, "B");
                        $tot_partiel = 0;
                      } else if ($ligne2['TypeData'] == 6) {
                        $size = $pdf->addTexte($y, $ligne2['Description'], 'B');
                      } else if ($ligne2['TypeData'] == 7) {
                        $line = array(
                          "NO ARTICLE"  => "",
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel 2" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "RABAIS"      => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_partiel2)
                        );
                        $size = $pdf->addLine($y, $line, "B", 12);
                        $tot_partiel2 = 0;
                      }
                      break;
                    case 4:
                      if ($ligne2['TypeData'] == 0) {
                        if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "TVA"         => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => ""
                          );
                          $size = $pdf->addLine($y, $line);
                        } else {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => $prix_unit,
                            "TVA"         => abs($ligne2['TVA']),
                            "QUANTITE"    => $ligne2['Quantite'],
                            "TOTAL TTC"   => $ligne2['PrixMulti']
                          );
                          $size = $pdf->addLine($y, $line);
                        }
                      } else if ($ligne2['TypeData'] == 1) {
                        $size = $pdf->addTexte($y, $ligne2['Description']);
                      } else if ($ligne2['TypeData'] == 2) {
                        $line = array(
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total intermédiaire" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "TVA"         => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_inter)
                        );
                        $size = $pdf->addLine($y, $line, "B", 12);
                      } else if ($ligne2['TypeData'] == 4) {
                        $line = array(
                          "DESCRIPTION" => $ligne2['Description'],
                          "PRIX UNIT"   => $ligne2['PrixMulti'],
                          "TVA"         => abs($ligne2['TVA']),
                          "QUANTITE"    => $ligne2['Quantite'],
                          "TOTAL TTC"   => $ligne2['PrixMulti']
                        );
                        $size = $pdf->addLine($y, $line);
                      } else if ($ligne2['TypeData'] == 5) {
                        $line = array(
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "TVA"         => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_partiel)
                        );
                        $size = $pdf->addLine($y, $line, "B");
                        $tot_partiel = 0;
                      } else if ($ligne2['TypeData'] == 6) {
                        $size = $pdf->addTexte($y, $ligne2['Description'], 'B');
                      } else if ($ligne2['TypeData'] == 7) {
                        $line = array(
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel 2" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "TVA"         => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_partiel2)
                        );
                        $size = $pdf->addLine($y, $line, "B", 12);
                        $tot_partiel2 = 0;
                      }
                      break;
                    case 5:
                      if ($ligne2['TypeData'] == 0) {
                        if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                          $line = array(
                            "NO ARTICLE"  => $ligne2['NoArticle'],
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "TVA"         => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => ""
                          );
                          $size = $pdf->addLine($y, $line);
                        } else {
                          $line = array(
                            "NO ARTICLE"  => $ligne2['NoArticle'],
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => $prix_unit,
                            "TVA"         => abs($ligne2['TVA']),
                            "QUANTITE"    => $ligne2['Quantite'],
                            "TOTAL TTC"   => $ligne2['PrixMulti']
                          );
                          $size = $pdf->addLine($y, $line);
                        }
                      } else if ($ligne2['TypeData'] == 1) {
                        $size = $pdf->addTexte($y, $ligne2['Description']);
                      } else if ($ligne2['TypeData'] == 2) {
                        $line = array(
                          "NO ARTICLE"  => "",
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total intermédiaire" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "TVA"         => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_inter)
                        );
                        $size = $pdf->addLine($y, $line, "B", 12);
                      } else if ($ligne2['TypeData'] == 4) {
                        $line = array(
                          "NO ARTICLE"  => $ligne2['NoArticle'],
                          "DESCRIPTION" => $ligne2['Description'],
                          "PRIX UNIT"   => $ligne2['PrixMulti'],
                          "TVA"         => abs($ligne2['TVA']),
                          "QUANTITE"    => $ligne2['Quantite'],
                          "TOTAL TTC"   => $ligne2['PrixMulti']
                        );
                        $size = $pdf->addLine($y, $line);
                      } else if ($ligne2['TypeData'] == 5) {
                        $line = array(
                          "NO ARTICLE"  => "",
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "TVA"         => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_partiel)
                        );
                        $size = $pdf->addLine($y, $line, "B");
                        $tot_partiel = 0;
                      } else if ($ligne2['TypeData'] == 6) {
                        $size = $pdf->addTexte($y, $ligne2['Description'], 'B');
                      } else if ($ligne2['TypeData'] == 7) {
                        $line = array(
                          "NO ARTICLE"  => "",
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel 2" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "TVA"         => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_partiel2)
                        );
                        $size = $pdf->addLine($y, $line, "B", 12);
                        $tot_partiel2 = 0;
                      }
                      break;
                    case 6:
                      if ($ligne2['TypeData'] == 0) {
                        if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "RABAIS"      => "",
                            "TVA"         => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => ""
                          );
                          $size = $pdf->addLine($y, $line);
                        } else {
                          if ($ligne2['Rabais'] != 0.00)
                            $line = array(
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => $prix_unit,
                              "RABAIS"      => $ligne2['Rabais'],
                              "TVA"         => abs($ligne2['TVA']),
                              "QUANTITE"    => $ligne2['Quantite'],
                              "TOTAL TTC"   => $ligne2['PrixMulti']
                            );
                          else
                            $line = array(
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => $prix_unit,
                              "RABAIS"      => "",
                              "TVA"         => abs($ligne2['TVA']),
                              "QUANTITE"    => $ligne2['Quantite'],
                              "TOTAL TTC"   => $ligne2['PrixMulti']
                            );
                          $size = $pdf->addLine($y, $line);
                        }
                      } else if ($ligne2['TypeData'] == 1) {
                        $size = $pdf->addTexte($y, $ligne2['Description']);
                      } else if ($ligne2['TypeData'] == 2) {
                        $line = array(
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total intermédiaire" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "RABAIS"      => "",
                          "TVA"         => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_inter)
                        );
                        $size = $pdf->addLine($y, $line, "B", 12);
                      } else if ($ligne2['TypeData'] == 4) {
                        $line = array(
                          "DESCRIPTION" => $ligne2['Description'],
                          "PRIX UNIT"   => $ligne2['PrixMulti'],
                          "RABAIS"      => "",
                          "TVA"         => abs($ligne2['TVA']),
                          "QUANTITE"    => $ligne2['Quantite'],
                          "TOTAL TTC"   => $ligne2['PrixMulti']
                        );
                        $size = $pdf->addLine($y, $line);
                      } else if ($ligne2['TypeData'] == 5) {
                        $line = array(
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "RABAIS"      => "",
                          "TVA"         => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_partiel)
                        );
                        $size = $pdf->addLine($y, $line, "B");
                        $tot_partiel = 0;
                      } else if ($ligne2['TypeData'] == 6) {
                        $size = $pdf->addTexte($y, $ligne2['Description'], 'B');
                      } else if ($ligne2['TypeData'] == 7) {
                        $line = array(
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel 2" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "RABAIS"      => "",
                          "TVA"         => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_partiel2)
                        );
                        $size = $pdf->addLine($y, $line, "B", 12);
                        $tot_partiel2 = 0;
                      }
                      break;
                    case 7:
                      if ($ligne2['TypeData'] == 0) {
                        if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                          $line = array(
                            "NO ARTICLE"  => $ligne2['NoArticle'],
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "RABAIS"      => "",
                            "TVA"         => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => ""
                          );
                          $size = $pdf->addLine($y, $line);
                        } else {
                          if ($ligne2['Rabais'] != 0.00)
                            $line = array(
                              "NO ARTICLE"  => $ligne2['NoArticle'],
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => $prix_unit,
                              "RABAIS"      => $ligne2['Rabais'],
                              "TVA"         => abs($ligne2['TVA']),
                              "QUANTITE"    => $ligne2['Quantite'],
                              "TOTAL TTC"   => $ligne2['PrixMulti']
                            );
                          else
                            $line = array(
                              "NO ARTICLE"  => $ligne2['NoArticle'],
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => $prix_unit,
                              "RABAIS"      => "",
                              "TVA"         => abs($ligne2['TVA']),
                              "QUANTITE"    => $ligne2['Quantite'],
                              "TOTAL TTC"   => $ligne2['PrixMulti']
                            );
                          $size = $pdf->addLine($y, $line);
                        }
                      } else if ($ligne2['TypeData'] == 1) {
                        $size = $pdf->addTexte($y, $ligne2['Description']);
                      } else if ($ligne2['TypeData'] == 2) {
                        $line = array(
                          "NO ARTICLE"  => "",
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total intermédiaire" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "RABAIS"      => "",
                          "TVA"         => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_inter)
                        );
                        $size = $pdf->addLine($y, $line, "B", 12);
                      } else if ($ligne2['TypeData'] == 4) {
                        $line = array(
                          "NO ARTICLE"  => $ligne2['NoArticle'],
                          "DESCRIPTION" => $ligne2['Description'],
                          "PRIX UNIT"   => $ligne2['PrixMulti'],
                          "RABAIS"      => "",
                          "TVA"         => abs($ligne2['TVA']),
                          "QUANTITE"    => $ligne2['Quantite'],
                          "TOTAL TTC"   => $ligne2['PrixMulti']
                        );
                        $size = $pdf->addLine($y, $line);
                      } else if ($ligne2['TypeData'] == 5) {
                        $line = array(
                          "NO ARTICLE"  => "",
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "RABAIS"      => "",
                          "TVA"         => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_partiel)
                        );
                        $size = $pdf->addLine($y, $line, "B");
                        $tot_partiel = 0;
                      } else if ($ligne2['TypeData'] == 6) {
                        $size = $pdf->addTexte($y, $ligne2['Description'], 'B');
                      } else if ($ligne2['TypeData'] == 7) {
                        $line = array(
                          "NO ARTICLE"  => "",
                          "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel 2" : $ligne2['Description'],
                          "PRIX UNIT"   => "",
                          "RABAIS"      => "",
                          "TVA"         => "",
                          "QUANTITE"    => "",
                          "TOTAL TTC"   => Arrondi_5_ct($tot_partiel2)
                        );
                        $size = $pdf->addLine($y, $line, "B", 12);
                        $tot_partiel2 = 0;
                      }
                      break;
                  }
                  $y   += $size + $interlignes;
                } // end for
              }
            }
            //                $tot_tva["9.3"]=1230;
            if (isset($tot_tva)) // array(1) { ["7.7"]=> float(4740) }
              //          $pdf->addCadreTVAnew($tot_tva, "458 663", $y);
              //          $pdf->addTotal(Arrondi_5_ct($tot_inter), $y);
              if ($ligne['Type'] != "FO" || substr($ligne['DateCommande'], 0, 4) > 2009) {
                if (isset($tva_new)) {
                  $tot_tva["$tva_new"] = $tot_tva["$tva_norm"];
                  unset($tot_tva["$tva_norm"]);
                }
                if ($ligne["FactureSansTexteTVA"] != 1) {
                  $pdf->addCadreTVAnew($tot_tva, Config::getNoTVA());
                }
              } else {
                //            $pdf->addCadreTVAnew($tot_tva, "458 663");
                $pdf->addCadreTVAdesMarges(Config::getNoTVA());
              }
            $pdf->addTotal(Arrondi_5_ct($tot_inter));
            //          $pdf->addCadreTVAs();

            //                $tot_prods = array( array ( "px_unit" => 600, "qte" => 1, "tva" => 1 ),
            //                                    array ( "px_unit" =>  10, "qte" => 1, "tva" => 1 ));
            //                $tab_tva = array( "1"       => 19.6,
            //                                  "2"       => 5.5);
            //                $params  = array( "RemiseGlobale" => 1,
            //                                  "remise_tva"     => 1,       // {la remise s'applique sur ce code TVA}
            //                                  "remise"         => 0,       // {montant de la remise}
            //                                  "remise_percent" => 10,      // {pourcentage de remise sur ce montant de TVA}
            //                                  "FraisPort"     => 1,
            //                                  "portTTC"        => 10,      // montant des frais de ports TTC
            //                                                               // par defaut la TVA = 19.6 %
            //                                  "portHT"         => 0,       // montant des frais de ports HT
            //                                  "portTVA"        => 19.6,    // valeur de la TVA a appliquer sur le montant HT
            //                                  "AccompteExige" => 1,
            //                                  "accompte"         => 0,     // montant de l'acompte (TTC)
            //                                  "accompte_percent" => 15,    // pourcentage d'acompte (TTC)
            //                                  "Remarque" => "Avec un acompte, svp..." );
            //
            //          $pdf->addTVAs( $params, $tab_tva, $tot_prods);
            //                $pdf->addCadreEurosFrancs();
            if ($Direct == 1) {
              $printer = "colors";
              if ($ligne = Poste::getPostesByIP()) {
                $printer = $ligne['Printer'];
              }
              $pdf->Output("../files/factures/$NoCommande.pdf", "F");
              $cmd = "lp -d $printer ../files/factures/$NoCommande.pdf";
              //                echo $cmd;
              exec($cmd);
              unlink("../files/factures/$NoCommande.pdf");
              echo "<script language=JavaScript>\n";
              echo "window.close();\n";
              echo "</script>\n";
            } else {
              $pdf->Output('I', $NoCommande . '.pdf');
            }
          }
      }
      break;
    case 2:
      switch ($Sous_Type) {
        case 1:
          if ($resultat = Commande::getCommandeByNoCommande($NoCommande)) {
            if ($ligne = $resultat) {
              $pdf = new INVOICE('P', 'mm', 'A4');
              $pdf->SetTitle("Contrat de vente " . $NoCommande);
              $pdf->SetSubject("Contrat de vente " . $NoCommande);
              $pdf->SetAuthor("Laurent");
              $pdf->SetCreator("Laurent");
              $pdf->SetDisplayMode("fullpage");
              $pdf->SetAutoPageBreak(false, 0);
              //$pdf->Open();
              $requette_test = "SELECT * FROM commandearticles" . ID_GARAGE_PRINT . " WHERE NoCommande=$NoCommande AND Rabais != 0.00";
              if ($resultat_test = Connection::executeRawQueryWithfetchAll($requette_test)) {
                $nb_result_test = count($resultat_test);
                if ($nb_result_test == 0) {
                  $ColRabais = 0;
                } else {
                  $ColRabais = 1;
                }
              }
              $requette_test = "SELECT * FROM commandearticles" . ID_GARAGE_PRINT . " WHERE NoCommande=$NoCommande AND PrixBase != 0.00 AND Quantite != 0.00 AND PrixMulti != 0.00 ORDER BY TVA";
              if ($resultat_test = Connection::executeRawQueryWithfetchAll($requette_test)) {
                $nb_result_test = count($resultat_test);
                if ($nb_result_test == 0) {
                  $ColTVA = 0;
                  $nb_tva = 0;
                } else {
                  $ColTVA = 0;
                  if ($ligne_test = $resultat_test[0]) {
                    $taux_tva = abs($ligne_test['TVA']);
                    $tot_tva["$taux_tva"] = 0;
                    $nb_tva = 1;
                  }
                  foreach ($resultat_test as $ligne_test) {
                    if ($taux_tva != (abs($ligne_test['TVA']))) {
                      $ColTVA = 1;
                      $taux_tva = abs($ligne_test['TVA']);
                      $tot_tva["$taux_tva"] = 0;
                      $nb_tva++;
                    }
                  }
                }
              }
              if ($ligne['Type'] == "FO")
                $Mode_col = $NoArticle + ($ColRabais * 2);
              else
                $Mode_col = $NoArticle + ($ColRabais * 2) + ($ColTVA * 4);
              if ($resultat2 = CommandeArticle::getCommandeArticleByNoCommande($NoCommande)) {
                $nb_result2 = count($resultat2);
                $nb_lignes_max_page1 = 35;
                $nb_lignes_max_page_autres = 45;
                if ($nb_result2 <= $nb_lignes_max_page1) {
                  $nb_pages = 1;
                } else {
                  $nb_pages = ceil(($nb_result2 - $nb_lignes_max_page1) / $nb_lignes_max_page_autres) + 1;
                }
                //$nb_pages=((int)(($nb_result2-1)/$nb_lignes_max))+1;

                $tot_inter = 0;
                $tot_partiel = 0;
                $tot_partiel2 = 0;
                for ($cpt_page = 1; $cpt_page <= $nb_pages; $cpt_page++) {
                  $pdf->AddPage();
                  if ($cpt_page == 1) {
                    $pdf->addContratDeVente($ligne['NoClient']);
                    $firstPage = true;
                  } else {
                    $firstPage = false;
                  }
                  $pdf->Logo(Config::getLogo());
                  $pdf->addPageNumber($cpt_page);
                  $pdf->addSociete3(Config::getAdresseFacture(), Config::getNoTelephone(), Config::getNoFax(), Config::getEmail(), "IBAN : " . Config::getNoIBAN() . "      BIC(SWIFT) : " . Config::getNoBIC(), Config::getUrlWeb());

                  if ($NoClient == 1)
                    $resultat3 = Client::getClientByNoClient($ligne['NoClient']);
                  else
                    $resultat3 = Client::getClientByNoClient($ligne['NoClient2']);
                  if ($resultat3) {
                    if ($ligne3 = $resultat3) {
                      $co = "";
                      if ($ligne3['Co'] != '') {
                        $co = "\nc/o " . $ligne3['Co'];
                      }
                      if ($ligne3['NPA'] != 0) {
                        //$pdf->addClientAdresse($ligne3['Allocution'] . "\n" . $ligne3['Nom'] . " " . $ligne3['Prenom'] . $co . "\n" . $ligne3['Adresse'] . "\n" . $ligne3['NPA'] . " " . $ligne3['Localite']);
                      } else {
                        //$pdf->addClientAdresse($ligne3['Allocution'] . "\n" . $ligne3['Nom'] . " " . $ligne3['Prenom'] . $co . "\n" . $ligne3['Adresse'] . "\n" . $ligne3['Localite']);
                      }
                    }
                  }
                  if (!empty($ligne['Vehicule'])) {
                    if ($ligne3 = Vehicule::getVehiculeByNoChassis($ligne['Vehicule'])) {
                      $pdf->addVehicule($ligne3['NoChasis'] . "\n" . $ligne3['MarqueModel'] . " " . $ligne3['Model'] . "\n" . $ligne['KmVhc'] . " km\n" . $ligne3['NoPlaque']);
                    }
                  }
                  if ($NoClient == 2) {
                    if ($ligne3 = Client::getClientByNoClient($ligne['NoClient'])) {
                      if ($ligne3['NPA'] != 0) {
                        $pdf->addClient2Adresse("Concerne : " . $ligne3['Allocution'] . " " . $ligne3['Nom'] . " " . $ligne3['Prenom'] . ", " . $ligne3['Adresse'] . " " . $ligne3['NPA'] . " " . $ligne3['Localite']);
                      } else {
                        $pdf->addClient2Adresse("Concerne : " . $ligne3['Allocution'] . " " . $ligne3['Nom'] . " " . $ligne3['Prenom'] . ", " . $ligne3['Adresse'] . " " . $ligne3['Localite']);
                      }
                    }
                  }
                  $cadre = 'N';
                  switch ($Mode_col) {
                    case 0:
                      $cols = array(
                        "DESCRIPTION" => 100,
                        "PRIX UNIT"   => 30,
                        "QUANTITE"    => 30,
                        "TOTAL TTC"   => 30
                      );
                      $pdf->addCols($cols, $cadre, $firstPage);
                      $cols = array(
                        "DESCRIPTION" => "L",
                        "PRIX UNIT"   => "R",
                        "QUANTITE"    => "R",
                        "TOTAL TTC"   => "R"
                      );
                      $pdf->addLineFormat($cols);
                      break;
                    case 1:
                      $cols = array(
                        "NO ARTICLE"  => 42,
                        "DESCRIPTION" => 76,
                        "PRIX UNIT"   => 24,
                        "QUANTITE"    => 24,
                        "TOTAL TTC"   => 24
                      );
                      $pdf->addCols($cols, $cadre, $firstPage);
                      $cols = array(
                        "NO ARTICLE"  => "L",
                        "DESCRIPTION" => "L",
                        "PRIX UNIT"   => "R",
                        "QUANTITE"    => "R",
                        "TOTAL TTC"   => "R"
                      );
                      $pdf->addLineFormat($cols);
                      break;
                    case 2:
                      $cols = array(
                        "DESCRIPTION" => 80,
                        "PRIX UNIT"   => 28,
                        "RABAIS"      => 26,
                        "QUANTITE"    => 28,
                        "TOTAL TTC"   => 28
                      );
                      $pdf->addCols($cols, $cadre, $firstPage);
                      $cols = array(
                        "DESCRIPTION" => "L",
                        "PRIX UNIT"   => "R",
                        "RABAIS"      => "R",
                        "QUANTITE"    => "R",
                        "TOTAL TTC"   => "R"
                      );
                      $pdf->addLineFormat($cols);
                      break;
                    case 3:
                      $cols = array(
                        "NO ARTICLE"  => 39,
                        "DESCRIPTION" => 71,
                        "PRIX UNIT"   => 21,
                        "RABAIS"      => 17,
                        "QUANTITE"    => 21,
                        "TOTAL TTC"   => 21
                      );
                      $pdf->addCols($cols, $cadre, $firstPage);
                      $cols = array(
                        "NO ARTICLE"  => "L",
                        "DESCRIPTION" => "L",
                        "PRIX UNIT"   => "R",
                        "RABAIS"      => "R",
                        "QUANTITE"    => "R",
                        "TOTAL TTC"   => "R"
                      );
                      $pdf->addLineFormat($cols);
                      break;
                    case 4:
                      $cols = array(
                        "DESCRIPTION" => 80,
                        "PRIX UNIT"   => 28,
                        "TVA"         => 26,
                        "QUANTITE"    => 28,
                        "TOTAL TTC"   => 28
                      );
                      $pdf->addCols($cols, $cadre, $firstPage);
                      $cols = array(
                        "DESCRIPTION" => "L",
                        "PRIX UNIT"   => "R",
                        "TVA"         => "R",
                        "QUANTITE"    => "R",
                        "TOTAL TTC"   => "R"
                      );
                      $pdf->addLineFormat($cols);
                      break;
                    case 5:
                      $cols = array(
                        "NO ARTICLE"  => 39,
                        "DESCRIPTION" => 71,
                        "PRIX UNIT"   => 21,
                        "TVA"         => 17,
                        "QUANTITE"    => 21,
                        "TOTAL TTC"   => 21
                      );
                      $pdf->addCols($cols, $cadre, $firstPage);
                      $cols = array(
                        "NO ARTICLE"  => "L",
                        "DESCRIPTION" => "L",
                        "PRIX UNIT"   => "R",
                        "TVA"         => "R",
                        "QUANTITE"    => "R",
                        "TOTAL TTC"   => "R"
                      );
                      $pdf->addLineFormat($cols);
                      break;
                    case 6:
                      $cols = array(
                        "DESCRIPTION" => 74,
                        "PRIX UNIT"   => 24,
                        "RABAIS"      => 22,
                        "TVA"         => 22,
                        "QUANTITE"    => 24,
                        "TOTAL TTC"   => 24
                      );
                      $pdf->addCols($cols, $cadre, $firstPage);
                      $cols = array(
                        "DESCRIPTION" => "L",
                        "PRIX UNIT"   => "R",
                        "RABAIS"      => "R",
                        "TVA"         => "R",
                        "QUANTITE"    => "R",
                        "TOTAL TTC"   => "R"
                      );
                      $pdf->addLineFormat($cols);
                      break;
                    case 7:
                      $cols = array(
                        "NO ARTICLE"  => 36,
                        "DESCRIPTION" => 68,
                        "PRIX UNIT"   => 18,
                        "RABAIS"      => 16,
                        "TVA"         => 16,
                        "QUANTITE"    => 18,
                        "TOTAL TTC"   => 18
                      );
                      $pdf->addCols($cols, $cadre, $firstPage);
                      $cols = array(
                        "NO ARTICLE"  => "L",
                        "DESCRIPTION" => "L",
                        "PRIX UNIT"   => "R",
                        "RABAIS"      => "R",
                        "TVA"         => "R",
                        "QUANTITE"    => "R",
                        "TOTAL TTC"   => "R"
                      );
                      $pdf->addLineFormat($cols);
                      break;
                  }
                  if ($cpt_page == 1) {
                    $y = 99;
                    $nb_lignes_max = $nb_lignes_max_page1;
                    $nb_lignes_page = $nb_result2 - (($cpt_page - 1) * $nb_lignes_max);
                  } else {
                    $y = 40;
                    $nb_lignes_max = $nb_lignes_max_page_autres;
                    $nb_lignes_page = $nb_result2 - $nb_lignes_max_page1 - (($cpt_page - 2) * $nb_lignes_max);
                  }
                  if ($nb_lignes_page > $nb_lignes_max) {
                    $nb_lignes_page = $nb_lignes_max;
                  }
                  $interlignes = floor((150 - ($nb_lignes_page * 3)) / $nb_lignes_page);
                  if ($interlignes < 1.35) {
                    $interlignes = 1.35;
                  }
                  if ($interlignes > 4) {
                    $interlignes = 4;
                  }
                  for ($cpt_ligne = 0; $cpt_ligne < $nb_lignes_page; $cpt_ligne++)
                  //			        while ($ligne2 = mysql_fetch_array( $resultat2 ))
                  {
                    $ligne2 = $resultat2[0];
                    if ($ligne2['TVA'] > 0) {
                      $prix_unit = Arrondi_5_ct($ligne2['PrixBase'] * ((100 + $ligne2['TVA']) / 100));
                    } else {
                      $prix_unit = $ligne2['PrixBase'];
                    }
                    if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                    } else {
                      $tmp = abs($ligne2['TVA']);
                      if ($ligne2['TypeData'] == 4) {
                        $tva_new = $tmp;
                        $tot_tva["$tva_norm"] += $ligne2['PrixMulti'];
                      } else {
                        $tva_norm = $tmp;
                        $tot_tva["$tmp"] += $ligne2['PrixMulti'];
                      }
                      if ($ligne2['TypeData'] == 0 || $ligne2['TypeData'] == 4) {
                        $tot_inter += $ligne2['PrixMulti'];
                        $tot_partiel += $ligne2['PrixMulti'];
                        $tot_partiel2 += $ligne2['PrixMulti'];
                      }
                      //					    Arrondi_5_ct($tot_inter)
                    }
                    switch ($Mode_col) {
                      case 0:
                        if ($ligne2['TypeData'] == 0) {
                          if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                            $line = array(
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => "",
                              "QUANTITE"    => "",
                              "TOTAL TTC"   => ""
                            );
                            $size = $pdf->addLine($y, $line);
                          } else {
                            $line = array(
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => $prix_unit,
                              "QUANTITE"    => $ligne2['Quantite'],
                              "TOTAL TTC"   => $ligne2['PrixMulti']
                            );
                            $size = $pdf->addLine($y, $line);
                          }
                        } else if ($ligne2['TypeData'] == 1) {
                          $size = $pdf->addTexte($y, $ligne2['Description']);
                        } else if ($ligne2['TypeData'] == 2) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total intermédiaire" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_inter)
                          );
                          $size = $pdf->addLine($y, $line, "B", 12);
                        } else if ($ligne2['TypeData'] == 4) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => $ligne2['PrixMulti'],
                            "QUANTITE"    => $ligne2['Quantite'],
                            "TOTAL TTC"   => $ligne2['PrixMulti']
                          );
                          $size = $pdf->addLine($y, $line);
                        } else if ($ligne2['TypeData'] == 5) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_partiel)
                          );
                          $size = $pdf->addLine($y, $line, "B");
                          $tot_partiel = 0;
                        } else if ($ligne2['TypeData'] == 6) {
                          $size = $pdf->addTexte($y, $ligne2['Description'], 'B');
                        } else if ($ligne2['TypeData'] == 7) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel 2" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_partiel2)
                          );
                          $size = $pdf->addLine($y, $line, "B", 12);
                          $tot_partiel2 = 0;
                        }
                        break;
                      case 1:
                        if ($ligne2['TypeData'] == 0) {
                          if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                            $line = array(
                              "NO ARTICLE"  => $ligne2['NoArticle'],
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => "",
                              "QUANTITE"    => "",
                              "TOTAL TTC"   => ""
                            );
                            $size = $pdf->addLine($y, $line);
                          } else {
                            $line = array(
                              "NO ARTICLE"  => $ligne2['NoArticle'],
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => $prix_unit,
                              "QUANTITE"    => $ligne2['Quantite'],
                              "TOTAL TTC"   => $ligne2['PrixMulti']
                            );
                            $size = $pdf->addLine($y, $line);
                          }
                        } else if ($ligne2['TypeData'] == 1) {
                          $size = $pdf->addTexte($y, $ligne2['Description']);
                        } else if ($ligne2['TypeData'] == 2) {
                          $line = array(
                            "NO ARTICLE"  => "",
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total intermédiaire" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_inter)
                          );
                          $size = $pdf->addLine($y, $line, '', 12);
                        } else if ($ligne2['TypeData'] == 4) {
                          $line = array(
                            "NO ARTICLE"  => $ligne2['NoArticle'],
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => $ligne2['PrixMulti'],
                            "QUANTITE"    => $ligne2['Quantite'],
                            "TOTAL TTC"   => $ligne2['PrixMulti']
                          );
                          $size = $pdf->addLine($y, $line);
                        } else if ($ligne2['TypeData'] == 5) {
                          $line = array(
                            "NO ARTICLE"  => "",
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_partiel)
                          );
                          $size = $pdf->addLine($y, $line);
                          $tot_partiel = 0;
                        } else if ($ligne2['TypeData'] == 6) {
                          $size = $pdf->addTexte($y, $ligne2['Description'], 'B');
                        } else if ($ligne2['TypeData'] == 7) {
                          $line = array(
                            "NO ARTICLE"  => "",
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel 2" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_partiel2)
                          );
                          $size = $pdf->addLine($y, $line, "B", 12);
                          $tot_partiel2 = 0;
                        }
                        break;
                      case 2:
                        if ($ligne2['TypeData'] == 0) {
                          if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                            $line = array(
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => "",
                              "RABAIS"      => "",
                              "QUANTITE"    => "",
                              "TOTAL TTC"   => ""
                            );
                            $size = $pdf->addLine($y, $line);
                          } else {
                            if ($ligne2['Rabais'] != 0.00)
                              $line = array(
                                "DESCRIPTION" => $ligne2['Description'],
                                "PRIX UNIT"   => $prix_unit,
                                "RABAIS"      => $ligne2['Rabais'],
                                "QUANTITE"    => $ligne2['Quantite'],
                                "TOTAL TTC"   => $ligne2['PrixMulti']
                              );
                            else
                              $line = array(
                                "DESCRIPTION" => $ligne2['Description'],
                                "PRIX UNIT"   => $prix_unit,
                                "RABAIS"      => "",
                                "QUANTITE"    => $ligne2['Quantite'],
                                "TOTAL TTC"   => $ligne2['PrixMulti']
                              );
                            $size = $pdf->addLine($y, $line);
                          }
                        } else if ($ligne2['TypeData'] == 1) {
                          $size = $pdf->addTexte($y, $ligne2['Description']);
                        } else if ($ligne2['TypeData'] == 2) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total intermédiaire" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "RABAIS"      => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_inter)
                          );
                          $size = $pdf->addLine($y, $line, "B", 12);
                        } else if ($ligne2['TypeData'] == 4) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => $ligne2['PrixMulti'],
                            "RABAIS"      => "",
                            "QUANTITE"    => $ligne2['Quantite'],
                            "TOTAL TTC"   => $ligne2['PrixMulti']
                          );
                          $size = $pdf->addLine($y, $line);
                        } else if ($ligne2['TypeData'] == 5) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "RABAIS"      => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_partiel)
                          );
                          $size = $pdf->addLine($y, $line, "B");
                          $tot_partiel = 0;
                        } else if ($ligne2['TypeData'] == 6) {
                          $size = $pdf->addTexte($y, $ligne2['Description'], 'B');
                        } else if ($ligne2['TypeData'] == 7) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel 2" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "RABAIS"      => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_partiel2)
                          );
                          $size = $pdf->addLine($y, $line, "B", 12);
                          $tot_partiel2 = 0;
                        }
                        break;
                      case 3:
                        if ($ligne2['TypeData'] == 0) {
                          if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                            $line = array(
                              "NO ARTICLE"  => $ligne2['NoArticle'],
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => "",
                              "RABAIS"      => "",
                              "QUANTITE"    => "",
                              "TOTAL TTC"   => ""
                            );
                            $size = $pdf->addLine($y, $line);
                          } else {
                            if ($ligne2['Rabais'] != 0.00)
                              $line = array(
                                "NO ARTICLE"  => $ligne2['NoArticle'],
                                "DESCRIPTION" => $ligne2['Description'],
                                "PRIX UNIT"   => $prix_unit,
                                "RABAIS"      => $ligne2['Rabais'],
                                "QUANTITE"    => $ligne2['Quantite'],
                                "TOTAL TTC"   => $ligne2['PrixMulti']
                              );
                            else
                              $line = array(
                                "NO ARTICLE"  => $ligne2['NoArticle'],
                                "DESCRIPTION" => $ligne2['Description'],
                                "PRIX UNIT"   => $prix_unit,
                                "RABAIS"      => "",
                                "QUANTITE"    => $ligne2['Quantite'],
                                "TOTAL TTC"   => $ligne2['PrixMulti']
                              );
                            $size = $pdf->addLine($y, $line);
                          }
                        } else if ($ligne2['TypeData'] == 1) {
                          $size = $pdf->addTexte($y, $ligne2['Description']);
                        } else if ($ligne2['TypeData'] == 2) {
                          $line = array(
                            "NO ARTICLE"  => "",
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total intermédiaire" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "RABAIS"      => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_inter)
                          );
                          $size = $pdf->addLine($y, $line, "B", 12);
                        } else if ($ligne2['TypeData'] == 4) {
                          $line = array(
                            "NO ARTICLE"  => $ligne2['NoArticle'],
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => $ligne2['PrixMulti'],
                            "RABAIS"      => "",
                            "QUANTITE"    => $ligne2['Quantite'],
                            "TOTAL TTC"   => $ligne2['PrixMulti']
                          );
                          $size = $pdf->addLine($y, $line);
                        } else if ($ligne2['TypeData'] == 5) {
                          $line = array(
                            "NO ARTICLE"  => "",
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "RABAIS"      => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_partiel)
                          );
                          $size = $pdf->addLine($y, $line, "B");
                          $tot_partiel = 0;
                        } else if ($ligne2['TypeData'] == 6) {
                          $size = $pdf->addTexte($y, $ligne2['Description'], 'B');
                        } else if ($ligne2['TypeData'] == 7) {
                          $line = array(
                            "NO ARTICLE"  => "",
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel 2" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "RABAIS"      => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_partiel2)
                          );
                          $size = $pdf->addLine($y, $line, "B", 12);
                          $tot_partiel2 = 0;
                        }
                        break;
                      case 4:
                        if ($ligne2['TypeData'] == 0) {
                          if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                            $line = array(
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => "",
                              "TVA"         => "",
                              "QUANTITE"    => "",
                              "TOTAL TTC"   => ""
                            );
                            $size = $pdf->addLine($y, $line);
                          } else {
                            $line = array(
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => $prix_unit,
                              "TVA"         => abs($ligne2['TVA']),
                              "QUANTITE"    => $ligne2['Quantite'],
                              "TOTAL TTC"   => $ligne2['PrixMulti']
                            );
                            $size = $pdf->addLine($y, $line);
                          }
                        } else if ($ligne2['TypeData'] == 1) {
                          $size = $pdf->addTexte($y, $ligne2['Description']);
                        } else if ($ligne2['TypeData'] == 2) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total intermédiaire" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "TVA"         => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_inter)
                          );
                          $size = $pdf->addLine($y, $line, "B", 12);
                        } else if ($ligne2['TypeData'] == 4) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => $ligne2['PrixMulti'],
                            "TVA"         => abs($ligne2['TVA']),
                            "QUANTITE"    => $ligne2['Quantite'],
                            "TOTAL TTC"   => $ligne2['PrixMulti']
                          );
                          $size = $pdf->addLine($y, $line);
                        } else if ($ligne2['TypeData'] == 5) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "TVA"         => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_partiel)
                          );
                          $size = $pdf->addLine($y, $line, "B");
                          $tot_partiel = 0;
                        } else if ($ligne2['TypeData'] == 6) {
                          $size = $pdf->addTexte($y, $ligne2['Description'], 'B');
                        } else if ($ligne2['TypeData'] == 7) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel 2" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "TVA"         => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_partiel2)
                          );
                          $size = $pdf->addLine($y, $line, "B", 12);
                          $tot_partiel2 = 0;
                        }
                        break;
                      case 5:
                        if ($ligne2['TypeData'] == 0) {
                          if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                            $line = array(
                              "NO ARTICLE"  => $ligne2['NoArticle'],
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => "",
                              "TVA"         => "",
                              "QUANTITE"    => "",
                              "TOTAL TTC"   => ""
                            );
                            $size = $pdf->addLine($y, $line);
                          } else {
                            $line = array(
                              "NO ARTICLE"  => $ligne2['NoArticle'],
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => $prix_unit,
                              "TVA"         => abs($ligne2['TVA']),
                              "QUANTITE"    => $ligne2['Quantite'],
                              "TOTAL TTC"   => $ligne2['PrixMulti']
                            );
                            $size = $pdf->addLine($y, $line);
                          }
                        } else if ($ligne2['TypeData'] == 1) {
                          $size = $pdf->addTexte($y, $ligne2['Description']);
                        } else if ($ligne2['TypeData'] == 2) {
                          $line = array(
                            "NO ARTICLE"  => "",
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total intermédiaire" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "TVA"         => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_inter)
                          );
                          $size = $pdf->addLine($y, $line, "B", 12);
                        } else if ($ligne2['TypeData'] == 4) {
                          $line = array(
                            "NO ARTICLE"  => $ligne2['NoArticle'],
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => $ligne2['PrixMulti'],
                            "TVA"         => abs($ligne2['TVA']),
                            "QUANTITE"    => $ligne2['Quantite'],
                            "TOTAL TTC"   => $ligne2['PrixMulti']
                          );
                          $size = $pdf->addLine($y, $line);
                        } else if ($ligne2['TypeData'] == 5) {
                          $line = array(
                            "NO ARTICLE"  => "",
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "TVA"         => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_partiel)
                          );
                          $size = $pdf->addLine($y, $line, "B");
                          $tot_partiel = 0;
                        } else if ($ligne2['TypeData'] == 6) {
                          $size = $pdf->addTexte($y, $ligne2['Description'], 'B');
                        } else if ($ligne2['TypeData'] == 7) {
                          $line = array(
                            "NO ARTICLE"  => "",
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel 2" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "TVA"         => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_partiel2)
                          );
                          $size = $pdf->addLine($y, $line, "B", 12);
                          $tot_partiel2 = 0;
                        }
                        break;
                      case 6:
                        if ($ligne2['TypeData'] == 0) {
                          if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                            $line = array(
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => "",
                              "RABAIS"      => "",
                              "TVA"         => "",
                              "QUANTITE"    => "",
                              "TOTAL TTC"   => ""
                            );
                            $size = $pdf->addLine($y, $line);
                          } else {
                            if ($ligne2['Rabais'] != 0.00)
                              $line = array(
                                "DESCRIPTION" => $ligne2['Description'],
                                "PRIX UNIT"   => $prix_unit,
                                "RABAIS"      => $ligne2['Rabais'],
                                "TVA"         => abs($ligne2['TVA']),
                                "QUANTITE"    => $ligne2['Quantite'],
                                "TOTAL TTC"   => $ligne2['PrixMulti']
                              );
                            else
                              $line = array(
                                "DESCRIPTION" => $ligne2['Description'],
                                "PRIX UNIT"   => $prix_unit,
                                "RABAIS"      => "",
                                "TVA"         => abs($ligne2['TVA']),
                                "QUANTITE"    => $ligne2['Quantite'],
                                "TOTAL TTC"   => $ligne2['PrixMulti']
                              );
                            $size = $pdf->addLine($y, $line);
                          }
                        } else if ($ligne2['TypeData'] == 1) {
                          $size = $pdf->addTexte($y, $ligne2['Description']);
                        } else if ($ligne2['TypeData'] == 2) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total intermédiaire" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "RABAIS"      => "",
                            "TVA"         => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_inter)
                          );
                          $size = $pdf->addLine($y, $line, "B", 12);
                        } else if ($ligne2['TypeData'] == 4) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => $ligne2['PrixMulti'],
                            "RABAIS"      => "",
                            "TVA"         => abs($ligne2['TVA']),
                            "QUANTITE"    => $ligne2['Quantite'],
                            "TOTAL TTC"   => $ligne2['PrixMulti']
                          );
                          $size = $pdf->addLine($y, $line);
                        } else if ($ligne2['TypeData'] == 5) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "RABAIS"      => "",
                            "TVA"         => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_partiel)
                          );
                          $size = $pdf->addLine($y, $line, "B");
                          $tot_partiel = 0;
                        } else if ($ligne2['TypeData'] == 6) {
                          $size = $pdf->addTexte($y, $ligne2['Description'], 'B');
                        } else if ($ligne2['TypeData'] == 7) {
                          $line = array(
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel 2" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "RABAIS"      => "",
                            "TVA"         => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_partiel2)
                          );
                          $size = $pdf->addLine($y, $line, "B", 12);
                          $tot_partiel2 = 0;
                        }
                        break;
                      case 7:
                        if ($ligne2['TypeData'] == 0) {
                          if (($ligne2['PrixBase'] == 0.00) and ($ligne2['Rabais'] == 0.00) and ($ligne2['TVA'] == 0.00) and ($ligne2['Quantite'] == 0.00) and ($ligne2['PrixMulti'] == 0.00)) {
                            $line = array(
                              "NO ARTICLE"  => $ligne2['NoArticle'],
                              "DESCRIPTION" => $ligne2['Description'],
                              "PRIX UNIT"   => "",
                              "RABAIS"      => "",
                              "TVA"         => "",
                              "QUANTITE"    => "",
                              "TOTAL TTC"   => ""
                            );
                            $size = $pdf->addLine($y, $line);
                          } else {
                            if ($ligne2['Rabais'] != 0.00)
                              $line = array(
                                "NO ARTICLE"  => $ligne2['NoArticle'],
                                "DESCRIPTION" => $ligne2['Description'],
                                "PRIX UNIT"   => $prix_unit,
                                "RABAIS"      => $ligne2['Rabais'],
                                "TVA"         => abs($ligne2['TVA']),
                                "QUANTITE"    => $ligne2['Quantite'],
                                "TOTAL TTC"   => $ligne2['PrixMulti']
                              );
                            else
                              $line = array(
                                "NO ARTICLE"  => $ligne2['NoArticle'],
                                "DESCRIPTION" => $ligne2['Description'],
                                "PRIX UNIT"   => $prix_unit,
                                "RABAIS"      => "",
                                "TVA"         => abs($ligne2['TVA']),
                                "QUANTITE"    => $ligne2['Quantite'],
                                "TOTAL TTC"   => $ligne2['PrixMulti']
                              );
                            $size = $pdf->addLine($y, $line);
                          }
                        } else if ($ligne2['TypeData'] == 1) {
                          $size = $pdf->addTexte($y, $ligne2['Description']);
                        } else if ($ligne2['TypeData'] == 2) {
                          $line = array(
                            "NO ARTICLE"  => "",
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total intermédiaire" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "RABAIS"      => "",
                            "TVA"         => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_inter)
                          );
                          $size = $pdf->addLine($y, $line, "B", 12);
                        } else if ($ligne2['TypeData'] == 4) {
                          $line = array(
                            "NO ARTICLE"  => $ligne2['NoArticle'],
                            "DESCRIPTION" => $ligne2['Description'],
                            "PRIX UNIT"   => $ligne2['PrixMulti'],
                            "RABAIS"      => "",
                            "TVA"         => abs($ligne2['TVA']),
                            "QUANTITE"    => $ligne2['Quantite'],
                            "TOTAL TTC"   => $ligne2['PrixMulti']
                          );
                          $size = $pdf->addLine($y, $line);
                        } else if ($ligne2['TypeData'] == 5) {
                          $line = array(
                            "NO ARTICLE"  => "",
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "RABAIS"      => "",
                            "TVA"         => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_partiel)
                          );
                          $size = $pdf->addLine($y, $line, "B");
                          $tot_partiel = 0;
                        } else if ($ligne2['TypeData'] == 6) {
                          $size = $pdf->addTexte($y, $ligne2['Description'], 'B');
                        } else if ($ligne2['TypeData'] == 7) {
                          $line = array(
                            "NO ARTICLE"  => "",
                            "DESCRIPTION" => $ligne2['Description'] == "" ? "Total partiel 2" : $ligne2['Description'],
                            "PRIX UNIT"   => "",
                            "RABAIS"      => "",
                            "TVA"         => "",
                            "QUANTITE"    => "",
                            "TOTAL TTC"   => Arrondi_5_ct($tot_partiel2)
                          );
                          $size = $pdf->addLine($y, $line, "B", 12);
                          $tot_partiel2 = 0;
                        }
                        break;
                    }
                    $y   += $size + $interlignes;
                  }
                }
              }
              //                $tot_tva["9.3"]=1230;
              if (isset($tot_tva))
                //          $pdf->addCadreTVAnew($tot_tva, "458 663", $y);
                //          $pdf->addTotal(Arrondi_5_ct($tot_inter), $y);
                if ($ligne['Type'] != "FO" || substr($ligne['DateCommande'], 0, 4) > 2009) {
                  if (isset($tva_new)) {
                    $tot_tva["$tva_new"] = $tot_tva["$tva_norm"];
                    unset($tot_tva["$tva_norm"]);
                  }
                  if ($ligne["FactureSansTexteTVA"] != 1) {
                    //$pdf->addCadreTVAnew($tot_tva, Config::getNoTVA());
                  }
                } else {
                  //            $pdf->addCadreTVAnew($tot_tva, "458 663");
                  //$pdf->addCadreTVAdesMarges(Config::getNoTVA());
                }
              $pdf->addTotal(Arrondi_5_ct($tot_inter));

              $pdf->AddPage();
              $pdf->Logo(Config::getLogo());
              $pdf->addPageNumber($cpt_page);
              $pdf->addConditions();
              $pdf->addSociete3(Config::getAdresseFacture(), Config::getNoTelephone(), Config::getNoFax(), Config::getEmail(), "IBAN : " . Config::getNoIBAN() . "      BIC(SWIFT) : " . Config::getNoBIC(), Config::getUrlWeb());
              //          $pdf->addCadreTVAs();

              //                $tot_prods = array( array ( "px_unit" => 600, "qte" => 1, "tva" => 1 ),
              //                                    array ( "px_unit" =>  10, "qte" => 1, "tva" => 1 ));
              //                $tab_tva = array( "1"       => 19.6,
              //                                  "2"       => 5.5);
              //                $params  = array( "RemiseGlobale" => 1,
              //                                  "remise_tva"     => 1,       // {la remise s'applique sur ce code TVA}
              //                                  "remise"         => 0,       // {montant de la remise}
              //                                  "remise_percent" => 10,      // {pourcentage de remise sur ce montant de TVA}
              //                                  "FraisPort"     => 1,
              //                                  "portTTC"        => 10,      // montant des frais de ports TTC
              //                                                               // par defaut la TVA = 19.6 %
              //                                  "portHT"         => 0,       // montant des frais de ports HT
              //                                  "portTVA"        => 19.6,    // valeur de la TVA a appliquer sur le montant HT
              //                                  "AccompteExige" => 1,
              //                                  "accompte"         => 0,     // montant de l'acompte (TTC)
              //                                  "accompte_percent" => 15,    // pourcentage d'acompte (TTC)
              //                                  "Remarque" => "Avec un acompte, svp..." );
              //
              //          $pdf->addTVAs( $params, $tab_tva, $tot_prods);
              //                $pdf->addCadreEurosFrancs();
              if ($Direct == 1) {
                $printer = "colors";
                if ($ligne = Poste::getPostesByIP()) {
                  $printer = $ligne['Printer'];
                }
                $pdf->Output("../files/factures/$NoCommande.pdf", "F");
                $cmd = "lp -d $printer ../files/factures/$NoCommande.pdf";
                //                echo $cmd;
                exec($cmd);
                unlink("../files/factures/$NoCommande.pdf");
                echo "<script language=JavaScript>\n";
                echo "window.close();\n";
                echo "</script>\n";
              } else {
                /*header('Content-type: application/pdf');
	header('Content-Disposition: inline; filename="'.$NoCommande.'.pdf"');*/
                $pdf->Output();
              }
            }
          }
      }
      break;
  }
}
