package controller.facture;

import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.sig.fw.db.bddc.dao.Construction_Job_DBAO;
import org.sig.fw.db.corrida.dao.Facture_DBAO;
import org.sig.fw.db.corrida.dao.Rfp_DBAO;
import org.sig.fw.db.corrida.model.Facture;
import org.sig.fw.db.corrida.model.Facture_Content;
import org.sig.fw.db.corrida.model.Rfp;
import org.sig.fw.log.common.LogTools;
import org.sig.fw.spdf.common.PDF;
import org.sig.fw.utility.date.SDate;
import org.sig.fw.utility.number.NumberTools;
import view.facture.Facture_Pdf_View;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class Facture_Pdf_Controller extends PDF
{
	private Facture_Pdf_View vw_facture_pdf;
	private Facture facture;
	private List<Facture_Content> contents;
	private List<Rfp> rfps = new ArrayList<Rfp>();
	private HashMap<Double, Double> tvas = new HashMap<Double, Double>();
	
	private float page_width = 0;
	private float page_height = 0;
	
	private int y;
	
	public Facture_Pdf_Controller(Facture facture, List<Facture_Content> contents)
	{
		super();
		this.facture = facture;
		this.contents = contents;
		this.vw_facture_pdf = new Facture_Pdf_View(this);
		this.vw_facture_pdf.setVisible(true);
	}

	/**
	 * Méthode pour remplacer les occurrences de \n par une chaîne vide.
	 *
	 * @param input La chaîne d'entrée.
	 * @return La chaîne modifiée sans \n.
	 */
	private static String removeNewLines(String input) {
		if (input == null) {
			return null;
		}
		return input.replace("\n", "");
	}


	@Override
	public void createDocument() throws IOException {
		// TODO Auto-generated method stub
		
	}
	
	public void drawPdf()
	{
		new Thread(new Runnable() 
		{
			@Override
			public void run() 
			{
				try
				{
					vw_facture_pdf.setValue("Récupération des données", 0);
					rfps = new Rfp_DBAO("rfp").getWithFacture(facture);
					vw_facture_pdf.setValue("Création du document", 10);
					createPage();
					vw_facture_pdf.setValue("Ajout de l'entête", 20);
					createHead();
					y = 555;
					vw_facture_pdf.setValue("Ajout du contenu", 40);
					createTable();
					vw_facture_pdf.setValue("Ajout des données de paiement", 60);
					createFoot();
					vw_facture_pdf.setValue("Finalisation du document", 100);
					
					closeStream();
					save(facture.getCommande().getDossier().getDirectory()+"\\09-Factures-Caution", facture.getCategory().getName());
					closeDocument();
				}
				catch (IOException e) 
				{
					LogTools.addErrorLog("PDF02", e);
				}
				vw_facture_pdf.dispose();
			}
		}).start();
	}
	
	private void createPage() throws IOException
	{
		this.page = new PDPage(PDRectangle.A4);
		this.document.addPage(page);
		
        this.page_width = this.page.getMediaBox().getWidth();
        this.page_height = this.page.getMediaBox().getHeight();
        this.contentStream = new PDPageContentStream(document, page, AppendMode.OVERWRITE, false);
        
		this.y = (int) (this.page_height - 10);
	}
	
	private void createHead() throws IOException
	{
		/* ENTETE GAUCHE : Logo + Adresse Entreprise */
		BufferedImage image = null;
		if(facture.getCommande().getSociety().getDbId() == 1)
		{
			image = ImageIO.read(Facture_Pdf_Controller.class.getResource("/logo/sondefor_logo.png"));
			this.contentStream.drawImage(LosslessFactory.createFromImage(this.document, image), 10, this.page_height-10-35, 197, 35);
		}
		else if(facture.getCommande().getSociety().getDbId() == 2)
		{
			image = ImageIO.read(Facture_Pdf_Controller.class.getResource("/logo/sbf_logo.png"));
			this.contentStream.drawImage(LosslessFactory.createFromImage(this.document, image), 10, this.page_height-10-35, 197, 35);
		}

		int fontSize = 0;
		float textWidth = 0;
		float textHeight = 0;
		String text = "";
		int x = (int) (this.page_width - 20);
		
		y -= 40;
		
		this.contentStream.beginText();
		text = facture.getCommande().getSociety().getCapital();
		fontSize = 6;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(20, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		
		y -= textHeight;
		y-= 5;
		
		if(!this.facture.getCommande().getSociety().getAddress().getStreet1().isEmpty())
		{
			text = this.facture.getCommande().getSociety().getAddress().getStreet1();
			fontSize = 10;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			this.contentStream.beginText();
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(20, y-textHeight);
			contentStream.showText(text);
			contentStream.endText();
			y -= textHeight;
		}
		
		if(!this.facture.getCommande().getSociety().getAddress().getStreet2().isEmpty())
		{
			text = this.facture.getCommande().getSociety().getAddress().getStreet2();
			fontSize = 10;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			this.contentStream.beginText();
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(20, y-textHeight);
			contentStream.showText(text);
			contentStream.endText();
			y -= textHeight;
		}
		
		if(!this.facture.getCommande().getSociety().getAddress().getStreet3().isEmpty())
		{
			text = this.facture.getCommande().getSociety().getAddress().getStreet3();
			fontSize = 10;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			this.contentStream.beginText();
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(20, y-textHeight);
			contentStream.showText(text);
			contentStream.endText();
			y -= textHeight;
		}

		text = this.facture.getCommande().getSociety().getAddress().getZipcode() + " " + this.facture.getCommande().getSociety().getAddress().getCity();
		fontSize = 10;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		this.contentStream.beginText();
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(20, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		
		/* ENTETE DROITE : Contact + Code SIRET / RCS / ... */
		
		y = (int) (this.page_height-10);
		
		/*if(facture.getCommande().getSociety().getDbId() == 2)
		{*/
			x -= 60;
			image = ImageIO.read(Facture_Pdf_Controller.class.getResource("/images/qualibat.gif"));
			this.contentStream.drawImage(LosslessFactory.createFromImage(this.document, image), page.getMediaBox().getWidth()-60-10, this.page_height-60-10, 60, 60);			
		/*}*/
		
		if(!this.facture.getCommande().getSociety().getPhoneNumber().isEmpty())
		{
			text = "Téléphone : " + this.facture.getCommande().getSociety().getPhoneNumber();
			fontSize = 10;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			this.contentStream.beginText();
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(x-textWidth, y-textHeight);
			contentStream.showText(text);
			contentStream.endText();
			y -= textHeight;
		}
		
		text = "E-mail : " + this.facture.getCommande().getSociety().getEmail();
		fontSize = 10;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		this.contentStream.beginText();
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x-textWidth, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		y -= textHeight;
		
		this.contentStream.beginText();
		text = "Siret : " + facture.getCommande().getSociety().getSiret();
		fontSize = 10;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x-textWidth, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		y -= textHeight;
		
		this.contentStream.beginText();
		text = "RCS : " + facture.getCommande().getSociety().getRcs();
		fontSize = 10;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x-textWidth, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		y -= textHeight;
		
		this.contentStream.beginText();
		text = "N° Intracom : " + facture.getCommande().getSociety().getTvaIntracom();
		fontSize = 10;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x-textWidth, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		
		/* ENTETE Client */
		
		y = 760;
		
		this.contentStream.beginText();
		fontSize = 10;
		textWidth = normal_font.getStringWidth(facture.getCommande().getSociety().getAddress().getCity() + " le,") / 1000 * fontSize;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(this.page_width*3/5 - textWidth, y-textHeight);
		contentStream.showText(facture.getCommande().getSociety().getAddress().getCity() + " le,");
		contentStream.endText();
		
		this.contentStream.beginText();
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(this.page_width*3/5 + 10, y-textHeight);
		contentStream.showText(facture.getFacturedDate().getFormattedDate(SDate.FORMAT_FR));
		contentStream.endText();
		
		y -= textHeight;
		y -= textHeight;
		
		this.contentStream.beginText();
		textWidth = normal_font.getStringWidth("Doit") / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(this.page_width*3/5 - textWidth, y-textHeight);
		if(facture.getAmountHt() >= 0.0)
			contentStream.showText("Doit");
		contentStream.endText();
		
		
		this.contentStream.beginText();
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(this.page_width*3/5 + 10, y-textHeight);
		contentStream.showText(this.rfps.get(0).getPayer().getCorridaClient().getCompany());
		contentStream.endText();
		
		y -= textHeight;
		
		if(!this.rfps.get(0).getPayer().getCorridaClient().getAddress().getStreet1().isEmpty())
		{
			this.contentStream.beginText();
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(this.page_width*3/5 + 10, y-textHeight);
			contentStream.showText(this.rfps.get(0).getPayer().getCorridaClient().getAddress().getStreet1());
			contentStream.endText();
			y -= textHeight;
		}
		
		if(!this.rfps.get(0).getPayer().getCorridaClient().getAddress().getStreet2().isEmpty())
		{
			this.contentStream.beginText();
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(this.page_width*3/5 + 10, y-textHeight);
			contentStream.showText(this.rfps.get(0).getPayer().getCorridaClient().getAddress().getStreet2());
			contentStream.endText();
			y -= textHeight;
		}
		
		if(!this.rfps.get(0).getPayer().getCorridaClient().getAddress().getStreet3().isEmpty())
		{
			this.contentStream.beginText();
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(this.page_width*3/5+ 10, y-textHeight);
			contentStream.showText(this.rfps.get(0).getPayer().getCorridaClient().getAddress().getStreet3());
			contentStream.endText();
			y -= textHeight;
		}
		
		this.contentStream.beginText();
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(this.page_width*3/5 + 10, y-textHeight);
		contentStream.showText(this.rfps.get(0).getPayer().getCorridaClient().getAddress().getZipcode() + " " + this.rfps.get(0).getPayer().getCorridaClient().getAddress().getCity());
		contentStream.endText();
		y -= textHeight;
		
		this.contentStream.beginText();
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(this.page_width*3/5 + 10, y-textHeight);
		contentStream.showText("SIREN : " + this.rfps.get(0).getPayer().getCorridaClient().getSirenNumber());
		contentStream.endText();
		y -= textHeight;

		y -= textHeight;

		this.contentStream.beginText();
		fontSize = 13;
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth("Facture N°") / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(this.page_width*3/5 - textWidth, y-textHeight);
		if(facture.getAmountHt() >= 0.0)
			contentStream.showText("Facture N°");
		else
			contentStream.showText("Avoir N°");
		contentStream.endText();
		
		this.contentStream.beginText();
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(this.page_width*3/5 + 10, y-textHeight);
		contentStream.showText(this.facture.getFactureNumber());
		contentStream.endText();
		
		/* TABLEAU GAUCHE : Ref Client et Ref Commande */

		y = 720;
		fontSize = 10;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		this.contentStream.addRect(10, y-3*textHeight, this.page_width/2 - 60, 3*textHeight);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		text = "N° Affaire : ";
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(12, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		
		this.contentStream.beginText();
		text = this.facture.getCommande().getDossier().getReferenceNumber()+"  /  "+new Construction_Job_DBAO("cj").getTechnicTags(facture.getCommande().getConstructionSite())+" - "+this.facture.getCommande().getConstructionSite().getResponsibleParaphe();
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(12 + textWidth, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		
		y -= textHeight;
		y -= 3;
		this.contentStream.addRect(10, y, this.page_width/2 - 60, 0);
		this.contentStream.stroke();
		y -= 2;
		
		this.contentStream.beginText();
		text = this.facture.getCategory().getName();
		fontSize = 10;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(12, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		
		y -= textHeight;
		y -= textHeight;
		
		this.contentStream.addRect(10, y-5*textHeight, this.page_width/2 - 60, 5*textHeight);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		text = "Référence";
		fontSize = 10;
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset((this.page_width/2 - 60)/2 - textWidth/2 + 10, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		
		y -= textHeight;
		y -= 3;
		this.contentStream.addRect(10, y, this.page_width/2 - 60, 0);
		this.contentStream.stroke();
		y -= 2;

		this.facture.getCommande().computeReference();
		text = this.facture.getCommande().getCommandeReference().getShowText();
		int total_width = (int) (this.page_width/2 - 60-2);
		
	    List<String> lines = new ArrayList<String>();
	    int lastSpace = -1;
	    while (text.length() > 0)
	    {
	        int spaceIndex = text.indexOf(' ', lastSpace + 1);
	        if (spaceIndex < 0)
	            spaceIndex = text.length();
	        String subString = text.substring(0, spaceIndex);
	        float size = 10 * normal_font.getStringWidth(subString) / 1000;
	        if (size > total_width)
	        {
	            if (lastSpace < 0)
	                lastSpace = spaceIndex;
	            subString = text.substring(0, lastSpace);
	            lines.add(subString);
	            text = text.substring(lastSpace).trim();
	            lastSpace = -1;
	        }
	        else if (spaceIndex == text.length())
	        {
	            lines.add(text);
	            text = "";
	        }
	        else
	        {
	            lastSpace = spaceIndex;
	        }
	    }

	    textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
	    contentStream.beginText();
	    contentStream.setFont(normal_font, 10);
	    contentStream.newLineAtOffset(11, y-textHeight);
	    for(int i = 0; i < lines.size(); i++)
	    {
	    	if(i < 2)
	    	{
		        contentStream.showText(lines.get(i));
		        contentStream.newLineAtOffset(0, -textHeight);
		        y -= textHeight;
	    	}
	    }
	    contentStream.endText();
	    
	    y -= textHeight;
		y -= 20;
	    
	    this.contentStream.addRect(10, y-3*textHeight, this.page_width - 20, 3*textHeight);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		text = "Affaire";
		fontSize = 10;
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset((this.page_width - 20)/2 - textWidth/2 + 10, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
		
		y -= textHeight;
		y -= 2;
		this.contentStream.addRect(10, y, this.page_width - 20, 0);
		this.contentStream.stroke();
		y -= 2;
		
		this.contentStream.beginText();
		text = this.facture.getCommande().getDossier().getLocalisation() + " | " + this.facture.getCommande().getDossier().getName();
		fontSize = 10;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset((this.page_width - 20)/2 - textWidth/2 + 10, y-textHeight);
		contentStream.showText(text);
		contentStream.endText();
	}
	
	private void createTable() throws IOException
	{
		//y max (haut de page) = 680
		//y min (bas de page) = 80
		
		//largeur = 595... >< -> 550 <-> table
		
		int designation_width = 200;
		int montant_marche = 55;
		int montant_cumule = 55;
		int montant_precedent = 55;
		int avancement_cumule = 50;
		int avancement_precedent = 50;
		int avancement_present = 50;
		int montant_ht = 60;

		int x = 10;
		
		int height = 15;
		int width = 575;
		
		y -= height;
		
		this.contentStream.addRect(x, y, width, height*2);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		int fontSize = 10;
		float textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		float textWidth = bold_font.getStringWidth("Désignation") / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x + (designation_width/2) - (textWidth/2), y + (textHeight/2) - 2 + height/2);
		contentStream.showText("Désignation");
		contentStream.endText();
		
		x+= designation_width;
		y += height;
		
		this.contentStream.addRect(x, y, montant_marche+montant_cumule+montant_precedent, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth("Montants HT") / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x + ((montant_marche+montant_cumule+montant_precedent)/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText("Montants HT");
		contentStream.endText();
		
		y -= height;
		
		this.contentStream.addRect(x, y, montant_marche, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		fontSize = 8;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth("Mt Marché") / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x + (montant_marche/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText("Mt Marché");
		contentStream.endText();
		
		x+= montant_marche;
		
		this.contentStream.addRect(x, y, montant_cumule, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth("Mt Cumulé") / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x + (montant_cumule/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText("Mt Cumulé");
		contentStream.endText();
		
		x+= montant_cumule;
		
		this.contentStream.addRect(x, y, montant_precedent, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth("Mt Précédent") / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x + (montant_precedent/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText("Mt Précédent");
		contentStream.endText();
		
		x+= montant_cumule;
		
		y += height;
		
		this.contentStream.addRect(x, y, avancement_cumule+avancement_precedent+avancement_present, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		fontSize = 10;
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth("Avancement") / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x + ((avancement_cumule+avancement_precedent+avancement_present)/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText("Avancement");
		contentStream.endText();
		
		y -= height;
		
		this.contentStream.addRect(x, y, avancement_cumule, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		fontSize = 8;
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth("Cumulé") / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x + (avancement_cumule/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText("Cumulé");
		contentStream.endText();
		
		x+= avancement_cumule;
		
		this.contentStream.addRect(x, y, avancement_precedent, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth("Précédent") / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x + (avancement_precedent/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText("Précédent");
		contentStream.endText();
		
		x+= avancement_precedent;
		
		this.contentStream.addRect(x, y, avancement_present, height);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth("Présent") / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x + (avancement_present/2) - (textWidth/2), y + (textHeight/2) - 2);
		contentStream.showText("Présent");
		contentStream.endText();
		
		x+= avancement_present;
		
		this.contentStream.addRect(x, y, montant_ht, height*2);
		this.contentStream.stroke();
		
		this.contentStream.beginText();
		fontSize = 10;
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth("Montant HT") / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x + (montant_ht/2) - (textWidth/2), y + (textHeight/2) - 2 + height/2);
		contentStream.showText("Montant HT");
		contentStream.endText();
		
		//LIBELLE
		x = 10;
		height = 13;
		y -= height;
		
		String text = "";
		double total_ht = 0;
		double total_net = 0;
		fontSize = 8;
		
		boolean onGroup = false;
		double total_group = 0;
		double total_cumule = 0;
		double total_precedent = 0;
		
		PDFont font = null;
		for(Facture_Content fc : contents)
		{
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			if(fc.getCommandeContent().getReference().getTypeId() != 0) {
				font = bold_font;
				this.contentStream.setNonStrokingColor(new Color(192,192,192));
				this.contentStream.addRect(x+1, y, width-1, textHeight);
				this.contentStream.fill();
				this.contentStream.setNonStrokingColor(Color.BLACK);
			} else {
				font = normal_font;
			}
			
			if(fc.getCommandeContent().getReference().getTypeId() == 1)
			{
				onGroup = true;
			}
			else if(onGroup && fc.getCommandeContent().getReference().getTypeId() != 2)
			{
				total_group += fc.getAmountHt();
				total_cumule += fc.getAmountCumule();
				total_precedent += fc.getAmountPrecedent();
			}
			
			this.contentStream.beginText();
			text = fc.getCommandeContent().getReference().getLongText();
			textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			this.contentStream.setFont(font, fontSize);
			contentStream.newLineAtOffset(x+2, y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
			x += designation_width;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			if(fc.getCommandeContent().getAmountHt() != 0)
			{
				this.contentStream.beginText();
				text = NumberTools.getNumberFormat(2).format(fc.getCommandeContent().getAmountHt());
				textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(font, fontSize);
				contentStream.newLineAtOffset(x - textWidth - 2 + montant_marche, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
			}
			x += montant_marche;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();

			if(fc.getCommandeContent().getAmountHt() != 0 && fc.getAmountCumule() != 0)
			{
				this.contentStream.beginText();
				text = NumberTools.getNumberFormat(2).format(fc.getAmountCumule());
				textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(font, fontSize);
				contentStream.newLineAtOffset(x - textWidth - 2 + montant_cumule, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
			}
			else if(fc.getCommandeContent().getReference().getTypeId() == 2 && total_cumule != 0)
			{
				this.contentStream.beginText();
				text = NumberTools.getNumberFormat(2).format(total_cumule);
				textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(font, fontSize);
				contentStream.newLineAtOffset(x - textWidth - 2 + montant_cumule, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
			}
			
			x += montant_cumule;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			if(fc.getCommandeContent().getAmountHt() != 0 && fc.getAmountPrecedent() != 0)
			{
				this.contentStream.beginText();
				text = NumberTools.getNumberFormat(2).format(fc.getAmountPrecedent());
				textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(font, fontSize);
				contentStream.newLineAtOffset(x - textWidth - 2 + montant_precedent, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
			}
			else if(fc.getCommandeContent().getReference().getTypeId() == 2 && total_precedent != 0)
			{
				this.contentStream.beginText();
				text = NumberTools.getNumberFormat(2).format(total_precedent);
				textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(font, fontSize);
				contentStream.newLineAtOffset(x - textWidth - 2 + montant_precedent, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
			}
			
			x+= montant_precedent;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			String unite_text = "";
			if(fc.getPresentPercent() != 0.0) {
				unite_text = "%";
			} else if(fc.getPresentUnit() != 0.0) {
				if(!fc.getCommandeContent().getReference().getUnite().isEmpty()) {
					unite_text = fc.getCommandeContent().getReference().getUnite();
				} else {
					unite_text = "U";
				}
			}
			
			if(fc.getCommandeContent().getAmountHt() != 0 && fc.getAvCumule() != 0)
			{
				this.contentStream.beginText();
				text = NumberTools.getNumberFormat(2).format(fc.getAvCumule()) + unite_text;
				textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(font, fontSize);
				contentStream.newLineAtOffset(x - textWidth - 2 + avancement_cumule, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
			}
			
			x+= avancement_cumule;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			if(fc.getCommandeContent().getAmountHt() != 0 && fc.getAvPrecedent() != 0)
			{
				this.contentStream.beginText();
				text = NumberTools.getNumberFormat(2).format(fc.getAvPrecedent()) + unite_text;
				textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(font, fontSize);
				contentStream.newLineAtOffset(x - textWidth - 2 + avancement_precedent, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
			}
			
			x+= avancement_precedent;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			if(fc.getCommandeContent().getAmountHt() != 0 && (fc.getPresentUnit() + fc.getPresentPercent()) != 0)
			{
				this.contentStream.beginText();
				double value = fc.getPresentPercent() == 0 ? fc.getPresentUnit() : fc.getPresentPercent();
				text = NumberTools.getNumberFormat(2).format(value) + unite_text;
				textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(font, fontSize);
				contentStream.newLineAtOffset(x - textWidth - 2 + avancement_present, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
			}
			
			x+= avancement_present;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			if(fc.getCommandeContent().getAmountHt() != 0 && fc.getAmountHt() != 0)
			{
				this.contentStream.beginText();
				text = NumberTools.getNumberFormat(2).format(fc.getAmountHt());
				textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(font, fontSize);
				contentStream.newLineAtOffset(x - textWidth - 2 + montant_ht, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
				total_ht += fc.getAmountHt();
				
				if(!facture.getCommande().getAutoLiquidation())
				{
					if(tvas.containsKey(fc.getCommandeContent().getTva()))
					{
						double tva = tvas.get(fc.getCommandeContent().getTva());
						tvas.put(fc.getCommandeContent().getTva(), tva + fc.getAmountTva());
					}
					else
					{
						tvas.put(fc.getCommandeContent().getTva(), fc.getAmountTva());
					}
				}
			}
			else if(fc.getCommandeContent().getReference().getTypeId() == 2 && total_group != 0)
			{
				this.contentStream.beginText();
				text = NumberTools.getNumberFormat(2).format(total_group);
				textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(font, fontSize);
				contentStream.newLineAtOffset(x - textWidth - 2 + montant_ht, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
			}
			
			x+= montant_ht;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x = 10;
			y -= height;
			
			if(fc.getCommandeContent().getReference().getTypeId() == 2)
			{
				onGroup = false;
				total_group = 0;
				total_cumule = 0;
				total_precedent = 0;
			}
		}

		if(rfps.size() > 1)
		{
			if(rfps.get(1).getDirectPayment())
			{
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += designation_width;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += montant_marche;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += montant_cumule;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += montant_precedent;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += avancement_cumule;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += avancement_precedent;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += avancement_present;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += montant_ht;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
		
				y -= height;
				x = 10;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				this.contentStream.beginText();
				text = "Paiement direct par " + rfps.get(1).getPayer().getCorridaClient().getCompany();
				textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(bold_font, fontSize);
				contentStream.newLineAtOffset(x+2, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
				
				y -= 1;
				this.contentStream.addRect(x+2, y, textWidth, 0);
				this.contentStream.stroke();
				y += 1;
				x += designation_width;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += montant_marche;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += montant_cumule;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += montant_precedent;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += avancement_cumule;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += avancement_precedent;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += avancement_present;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += montant_ht;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
		
				y -= height;
				x = 10;
			}
		}

		if(facture.getCautionJoin())
		{
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += designation_width;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_marche;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_cumule;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_precedent;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += avancement_cumule;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += avancement_precedent;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += avancement_present;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_ht;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
	
			y -= height;
			x = 10;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			this.contentStream.beginText();
			text = "Caution Bancaire Jointe";
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(x+2, y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
			x += designation_width;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_marche;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_cumule;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_precedent;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += avancement_cumule;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += avancement_precedent;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += avancement_present;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_ht;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
	
			y -= height;
			x = 10;
		}

		if(!facture.getObservation().isEmpty())
		{
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += designation_width;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_marche;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_cumule;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_precedent;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += avancement_cumule;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += avancement_precedent;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += avancement_present;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_ht;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
	
			y -= height;
			x = 10;
				
			text = facture.getObservation();
			text = removeNewLines(text);
			int total_width = designation_width-2;
			
		    List<String> lines = new ArrayList<String>();
		    int lastSpace = -1;
		    while (text.length() > 0)
		    {
		        int spaceIndex = text.indexOf(' ', lastSpace + 1);
		        if (spaceIndex < 0)
		            spaceIndex = text.length();
		        String subString = text.substring(0, spaceIndex);
		        float size = fontSize * normal_font.getStringWidth(subString) / 1000;
		        if (size > total_width)
		        {
		            if (lastSpace < 0)
		                lastSpace = spaceIndex;
		            subString = text.substring(0, lastSpace);
		            lines.add(subString);
		            text = text.substring(lastSpace).trim();
		            lastSpace = -1;
		        }
		        else if (spaceIndex == text.length())
		        {
		            lines.add(text);
		            text = "";
		        }
		        else
		        {
		            lastSpace = spaceIndex;
		        }
		    }

		    textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		    for(int i = 0; i < lines.size(); i++)
		    {
	    		this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				this.contentStream.beginText();
				text = lines.get(i);
				textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				this.contentStream.setFont(normal_font, fontSize);
				contentStream.newLineAtOffset(x+2, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
				x += designation_width;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += montant_marche;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += montant_cumule;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += montant_precedent;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += avancement_cumule;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += avancement_precedent;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += avancement_present;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += montant_ht;
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				y -= height;
				x = 10;
		    }
		}

		while(y > 130)
		{
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += designation_width;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_marche;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_cumule;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_precedent;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += avancement_cumule;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += avancement_precedent;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += avancement_present;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_ht;
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
	
			y -= height;
			x = 10;
		}
		y += height;

		this.contentStream.addRect(x, y, width, 0);
		this.contentStream.stroke();
		
		y -= height;
		int y_payment_mode = y;
		x = designation_width + 10 + montant_marche + montant_cumule;
		
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		/* TOTAL SITU CUMUL */
		double total_situation_precedente = 0.0;
		if(facture.getAmountHt() >= 0.0)
		{
			this.contentStream.beginText();
			text = "Total situation(s) cumulée(s)";
			textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
			this.contentStream.setFont(bold_font, fontSize);
			contentStream.newLineAtOffset(x + 10, y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
		}
		
		x += montant_ht + avancement_cumule + avancement_precedent + avancement_present + montant_precedent;
		
		if(facture.getAmountHt() >= 0.0) {
			total_situation_precedente = new Facture_DBAO("fact").getTotalSituation(facture);
			this.contentStream.beginText();
			text = NumberTools.getNumberFormat(2).format(total_situation_precedente + total_ht) + " €";
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(x - textWidth - 10, y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
		}
		
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		x = designation_width + 10 + montant_marche + montant_cumule;
		y -= height;
		
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		/* - SITU PREC */
		if(facture.getAmountHt() >= 0.0) 
		{
			this.contentStream.beginText();
			text = "- Situation(s) précédente(s)";
			textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
			this.contentStream.setFont(bold_font, fontSize);
			contentStream.newLineAtOffset(x + 10, y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
		}
			
		x += montant_ht + avancement_cumule + avancement_precedent + avancement_present + montant_precedent;
		
		if(facture.getAmountHt() >= 0.0) {
			this.contentStream.beginText();
			if(total_situation_precedente != 0)
				text = NumberTools.getNumberFormat(2).format(total_situation_precedente) + " €";
			else
				text = "";
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(x - textWidth - 10, y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
		}
		
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		if(facture.getAmountHt() >= 0.0) {
			this.contentStream.addRect(x - montant_ht, y-2, montant_ht, 0);
			this.contentStream.stroke();
		}
		x = designation_width + 10 + montant_marche + montant_cumule;
		y -= height;

		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		/* - TOTAL SITU */
		this.contentStream.beginText();
		text = "Total" + (facture.getAmountHt() >= 0.0 ? " Situation" : " HT");
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x + 10, y + (textHeight/2) - 2);
		contentStream.showText(text);
		contentStream.endText();
		x += montant_ht + avancement_cumule + avancement_precedent + avancement_present + montant_precedent;
	
		this.contentStream.beginText();
		text = NumberTools.getNumberFormat(2).format(total_ht) + " €";
		textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(normal_font, fontSize);
		contentStream.newLineAtOffset(x - textWidth - 10, y + (textHeight/2) - 2);
		contentStream.showText(text);
		contentStream.endText();

		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		x = designation_width + 10 + montant_marche + montant_cumule;
		y -= height;

		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		x += montant_ht + avancement_cumule + avancement_precedent + avancement_present + montant_precedent;

		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		x = designation_width + 10 + montant_marche + montant_cumule;
		y -= height;

		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		/* - TVA ou AUTO LIQUIDATION */
		total_net = total_ht;
		if(facture.getCommande().getAutoLiquidation())
		{
			this.contentStream.beginText();
			text = "Autoliquidation - CGI ann. II art.242 nonies A,I-13°";
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(x + 10, y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
			x += montant_ht + avancement_cumule + avancement_precedent + avancement_present + montant_precedent;
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x = designation_width + 10 + montant_marche + montant_cumule;
			y -= height;

			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_ht + avancement_cumule + avancement_precedent + avancement_present + montant_precedent;

			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x = designation_width + 10 + montant_marche + montant_cumule;
			y -= height;

			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			x += montant_ht + avancement_cumule + avancement_precedent + avancement_present + montant_precedent;

			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
		}
		else
		{
			int i = 0;
			for(Map.Entry<Double, Double> tva : tvas.entrySet())
			{
				x = designation_width + 10 + montant_marche + montant_cumule;
				System.out.println(tva.getKey() + " " + tva.getValue());
				this.contentStream.beginText();
				if(tva.getKey() == 0.0) {
					text = "Autoliquidation - CGI ann. II art.242 nonies A,I-13°";
				} else {
					text = "TVA (" + NumberTools.getNumberFormat(2).format(tva.getKey()) + "%)";
				}
				textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(bold_font, fontSize);
				contentStream.newLineAtOffset(x + 10, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += montant_ht + avancement_cumule + avancement_precedent + avancement_present + montant_precedent;
				this.contentStream.beginText();
				if(tva.getKey() == 0.0) {
					text = "";
				} else {
					text = NumberTools.getNumberFormat(2).format(tva.getValue()) + " €";
				}
				textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(normal_font, fontSize);
				contentStream.newLineAtOffset(x - textWidth - 10, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
//				total_net += tva.getValue();
				// Arrondi de tva.getValue() à deux décimales en tant que BigDecimal
				BigDecimal tvaArrondie = new BigDecimal(tva.getValue()).setScale(2, RoundingMode.HALF_UP);
				// Conversion de BigDecimal en double
				double tvaArrondieDouble = tvaArrondie.doubleValue();
				// Addition du double à total_net
				total_net += tvaArrondieDouble;

				i++;
				y -= height;
			}
			y += height;
			while(i < 3)
			{
				x = designation_width + 10 + montant_marche + montant_cumule;
				y -= height;

				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				x += montant_ht + avancement_cumule + avancement_precedent + avancement_present + montant_precedent;

				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				i++;
			}
		}

		x = designation_width + 10 + montant_marche + montant_cumule;
		y -= height;

		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		/* - TOTAL NET */
		this.contentStream.beginText();
		fontSize = 10;
		if(facture.getAmountHt() >= 0.0)
			text = "Net à payer en EUROS ";
		else 
			text = "Avoir " + (facture.getCommande().getAutoLiquidation() ? "HT" : "TTC") + " en EUROS ";
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x + 10, y + (textHeight/2) - 2);
		contentStream.showText(text);
		contentStream.endText();
		
		x += montant_ht + avancement_cumule + avancement_precedent + avancement_present + montant_precedent;
		this.contentStream.beginText();
		text = NumberTools.getNumberFormat(2).format(total_net) + " €";
		textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
		textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
		this.contentStream.setFont(bold_font, fontSize);
		contentStream.newLineAtOffset(x - textWidth - 10, y + (textHeight/2) - 2);
		contentStream.showText(text);
		contentStream.endText();
		
		this.contentStream.addRect(x, y, 0, height);
		this.contentStream.stroke();
		
		x = designation_width + 10 + montant_marche + montant_cumule;
		this.contentStream.addRect(x, y, montant_ht + avancement_cumule + avancement_precedent + avancement_present + montant_precedent, 0);
		this.contentStream.stroke();
		
		y = y_payment_mode;
		x = 10;
		fontSize = 8;
		
		if(facture.getAmountHt() >= 0.0) {
			
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
			this.contentStream.beginText();
			if(facture.getPaymentMode().getShortValue().contains("FACTOR"))
				text = "Réglement à établir au nom :";
			else
				text = "En votre aimable réglement :";
			textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
			this.contentStream.setFont(bold_font, fontSize);
			contentStream.newLineAtOffset(x+2, y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
			
			this.contentStream.addRect(x+2, y-1, textWidth, 0);
			this.contentStream.stroke();
		}
		x += designation_width;
		
		if(facture.getAmountHt() >= 0.0 | facture.getPaymentMode().getShortValue().contains("FACTOR")) {

			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
			
		}
		
		if(facture.getPaymentMode().getShortValue().contains("FACTOR"))
		{
			this.contentStream.beginText();
			text = "Références à rappeler :";
			textHeight = bold_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			textWidth = bold_font.getStringWidth(text) / 1000 * fontSize;
			this.contentStream.setFont(bold_font, fontSize);
			contentStream.newLineAtOffset(x + 2, y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
			
			this.contentStream.addRect(x+2, y-1, textWidth, 0);
			this.contentStream.stroke();
		}
		
		y += 1;
		y -= height;
		x = 10;
		if(facture.getAmountHt() >= 0.0) {

			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
		
			this.contentStream.beginText();
			text = facture.getPaymentMode().getLongValue();
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(x+2, y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
		}
		x += designation_width;
		
		if(facture.getAmountHt() >= 0.0 | facture.getPaymentMode().getShortValue().contains("FACTOR")) {
			this.contentStream.addRect(x, y, 0, height);
			this.contentStream.stroke();
		}
		
		if(facture.getPaymentMode().getShortValue().contains("FACTOR"))
		{
			this.contentStream.beginText();
			if(facture.getCommande().getSociety().getName().toUpperCase().equals("SONDEFOR")) {
				text = "N° Client : 10677";
			} else {
				text = "N° Client : 00843080";
			}
			textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
			this.contentStream.setFont(normal_font, fontSize);
			contentStream.newLineAtOffset(x+2, y + (textHeight/2) - 2);
			contentStream.showText(text);
			contentStream.endText();
		} 
		y -= height;
		
		x = 10;

		if(!facture.getPaymentMode().getCoordonate().isEmpty())
		{
			String[] values = facture.getPaymentMode().getCoordonate().split("<br>");
			if(facture.getAmountHt() < 0.0) values = new String[]{"",""};
			for(int i = 0; i < values.length; i++)
			{
				if(values[i].contains("[RIB]"))
				{
					if(facture.getCommande().getSociety().getName().toUpperCase().equals("SONDEFOR")) {
						if(rfps.get(0).getPayer().getCorridaClient().getSondeforFactorRib().isEmpty())
							values[i] = values[i].replaceAll("\\[RIB\\]", "RIB NON DEFINI");
						else
							values[i] = values[i].replaceAll("\\[RIB\\]", rfps.get(0).getPayer().getCorridaClient().getSondeforFactorRib());
					} else {
						if(rfps.get(0).getPayer().getCorridaClient().getSbfFactorRib().isEmpty())
							values[i] = values[i].replaceAll("\\[RIB\\]", "RIB NON DEFINI");
						else
							values[i] = values[i].replaceAll("\\[RIB\\]", rfps.get(0).getPayer().getCorridaClient().getSbfFactorRib());
					}
				}
			
				if(facture.getAmountHt() >= 0.0) {
					this.contentStream.addRect(x, y, 0, height);
					this.contentStream.stroke();
				}
				this.contentStream.beginText();
				text = values[i];
				textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(normal_font, fontSize);
				contentStream.newLineAtOffset(x+1, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
				
				x += designation_width;
				
				if(facture.getAmountHt() >= 0.0) {
					this.contentStream.addRect(x, y, 0, height);
					this.contentStream.stroke();
				}
				
				y -= height;
				x = 10;
			}
			
			y += (values.length*height);
			y -= height;
			
			if(facture.getPaymentMode().getShortValue().contains("FACTOR"))
			{
				x = 10 + designation_width;
				this.contentStream.beginText();
				if(facture.getCommande().getSociety().getName().toUpperCase().equals("SONDEFOR")) {
					if(rfps.get(0).getPayer().getCorridaClient().getSondeforFactorNumber().isEmpty())
						text = "N° Acheteur : NON DEFINI";
					else
						text = "N° Acheteur : " + rfps.get(0).getPayer().getCorridaClient().getSondeforFactorNumber();
				} else {
					if(rfps.get(0).getPayer().getCorridaClient().getSbfFactorNumber().isEmpty())
						text = "N° Acheteur : NON DEFINI";
					else
						text = "N° Acheteur : " + rfps.get(0).getPayer().getCorridaClient().getSbfFactorNumber();
				}
				textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(normal_font, fontSize);
				contentStream.newLineAtOffset(x+2, y + (textHeight/2) - 2);
				contentStream.showText(text);
				contentStream.endText();
				
				this.contentStream.addRect(x, y-height, 0, height*3);
				this.contentStream.stroke();
				
				y -= height;
				x = 10;
			}
			else
			{
				y -= height;
			}
			
			x = 10;
			
			if(facture.getAmountHt() >= 0.0) {
				
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
				
				this.contentStream.beginText();
	
				this.facture.computeEcheance(rfps.get(0).getPayer().getCorridaClient());
				if(this.facture.getImmediatePayment())
					text = "A la réception de la facture";
				else
					text = "A " + rfps.get(0).getPayer().getCorridaClient().getPaymentDelay() + " jours le " + rfps.get(0).getPayer().getCorridaClient().getPaymentDay() + ", soit le " + facture.getEcheance().getFormattedDate(SDate.FORMAT_FR);
				textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
				textWidth = normal_font.getStringWidth(text) / 1000 * fontSize;
				this.contentStream.setFont(normal_font, fontSize);
				contentStream.newLineAtOffset(x+2, y + (textHeight/2));
				contentStream.showText(text);
				contentStream.endText();
			}
			x += designation_width;
			
			if(facture.getAmountHt() >= 0.0) {
				this.contentStream.addRect(x, y, 0, height);
				this.contentStream.stroke();
			}
			
			if(facture.getAmountHt() >= 0.0) {
				this.contentStream.addRect(10, y, designation_width, 0);
				this.contentStream.stroke();
			}
		}
		else
		{
			if(facture.getAmountHt() >= 0.0) {
				y += height;
				this.contentStream.addRect(10, y, designation_width, 0);
				this.contentStream.stroke();
			}
		}
			
		if(!facture.getPaymentMode().getCoordonate().isEmpty())
		{
			if(facture.getPaymentMode().getShortValue().contains("FACTOR"))
			{
				this.contentStream.addRect(10+designation_width, y, montant_marche+montant_cumule, 0);
				this.contentStream.stroke();
			}
		}
		
		if(facture.getPaymentMode().getShortValue().contains("FACTOR"))
		{
			String[] values = facture.getPaymentMode().getLaw().split("<br>");
			
			for(int i = 0; i < values.length; i++)
			{
				text = values[i];
				int total_width = designation_width + montant_marche + montant_cumule;
				
			    List<String> lines = new ArrayList<String>();
			    int lastSpace = -1;
			    while (text.length() > 0)
			    {
			        int spaceIndex = text.indexOf(' ', lastSpace + 1);
			        if (spaceIndex < 0)
			            spaceIndex = text.length();
			        String subString = text.substring(0, spaceIndex);
			        float size = 6 * normal_font.getStringWidth(subString) / 1000;
			        if (size > total_width)
			        {
			            if (lastSpace < 0)
			                lastSpace = spaceIndex;
			            subString = text.substring(0, lastSpace);
			            lines.add(subString);
			            text = text.substring(lastSpace).trim();
			            lastSpace = -1;
			        }
			        else if (spaceIndex == text.length())
			        {
			            lines.add(text);
			            text = "";
			        }
			        else
			        {
			            lastSpace = spaceIndex;
			        }
			    }

			    fontSize = 6;
			    textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
			    contentStream.beginText();
			    contentStream.setFont(normal_font, fontSize);
			    contentStream.newLineAtOffset(10, y-textHeight);
			    for(int j = 0; j < lines.size(); j++)
			    {
			        contentStream.showText(lines.get(j));
			        contentStream.newLineAtOffset(0, -textHeight);
			        y -= textHeight;
			    }
			    contentStream.endText();
			}
			y-= textHeight;
		}
	}
	
	private void createFoot() throws IOException
	{
		y = 20;
		String text = "En cas de litige, en l'absence de marché, seul le tribunal de commerce de Poitiers sera compétent. Pas d'escompte pour paiement anticipé, passée la date d'échéance, tout paiement différé entraîne l'application d'une pénalité au taux d'intérêt appliqué par la BCE majoré de 10%. (article L441-6 code du commerce) ainsi qu'une indemnité forfaitaire pour frais de recouvrement de 40€ Décret 2012-1115 du 02/10/2012).";
		int total_width = (int) (this.page_width - 20);
		
	    List<String> lines = new ArrayList<String>();
	    int lastSpace = -1;
	    while (text.length() > 0)
	    {
	        int spaceIndex = text.indexOf(' ', lastSpace + 1);
	        if (spaceIndex < 0)
	            spaceIndex = text.length();
	        String subString = text.substring(0, spaceIndex);
	        float size = 6 * normal_font.getStringWidth(subString) / 1000;
	        if (size > total_width)
	        {
	            if (lastSpace < 0)
	                lastSpace = spaceIndex;
	            subString = text.substring(0, lastSpace);
	            lines.add(subString);
	            text = text.substring(lastSpace).trim();
	            lastSpace = -1;
	        }
	        else if (spaceIndex == text.length())
	        {
	            lines.add(text);
	            text = "";
	        }
	        else
	        {
	            lastSpace = spaceIndex;
	        }
	    }

	    int fontSize = 6;
	    float textHeight = normal_font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize;
	    contentStream.beginText();
	    contentStream.setFont(normal_font, fontSize);
	    contentStream.newLineAtOffset(10, y-textHeight);
	    for(int i = 0; i < lines.size(); i++)
	    {
	        contentStream.showText(lines.get(i));
	        contentStream.newLineAtOffset(0, -textHeight);
	        y -= textHeight;
	    }
	    contentStream.endText();
	}

}
