using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using ceTe.DynamicPDF.Text;
using ceTe.DynamicPDF;
using PDFGenerator.Presentation.Section;
using PDFGenerator;
using PDFGenerator.Presentation.Section.Tables;
using PDFGenerator.Presentation.Section.Charts;
using PDFGenerator.BusinessLayer;
using PDFGenerator.BusinessLayer.DataSection;
using ceTe.DynamicPDF.PageElements;
using ContrattoSei.Utilities;

/// <summary>
/// Summary description for S80ALT
/// </summary>
/// 
public class S80ALT : ISezione
{
    string Titolo = string.Empty;
    string _testotitolo;
    string _testointroduttivo;

    /// <summary>
    /// Testo introduttivo della sezione.
    /// </summary>
    public string TestoIntroduttivo
    {
        get
        {
            return _testointroduttivo;
        }
        set
        {
            _testointroduttivo = value;
        }
    }

    /// <summary>
    /// Testo del titolo della sezione.
    /// </summary>
    public string TestoTitolo
    {
        get
        {
            return _testotitolo;
        }
        set
        {
            _testotitolo = value;
        }
    }


    public void setTitolo(string label)
    {
        Titolo = label;
    }

    public S80ALT()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public void writeSezione(DataThread dataThread)
    {

        DocumentPDF document = dataThread.DocumentPDF;
        DatiTabella datitab;
        DataSetS80ALT set = (DataSetS80ALT)dataThread.Data.DatiSezione;

        //setta la datatable che contiene i dati per disegnare la tabella Dati
        datitab = new DatiTabella();
        datitab.table = set.Tables["GraficoPiramide"];

        string captionFase1 = datitab.table.Columns["Fase1"].Caption;
        string captionFase2 = datitab.table.Columns["Fase2"].Caption;
        string captionFase3 = datitab.table.Columns["Fase3"].Caption;

        // Aggiungo una pagina se la tabella non entra nella corrente.
        if (document.checkMargin(130))
            document.addPage();

        //document.setSezTitolo(Titolo);

        document.setLastPos(-40);
        document.setChapterHeader(_testointroduttivo, 0, 520, 8);


        #region Definizione delle Vars

        DataRow rowTemplate;

        //float TotalePatrimonio;
        float Ypos = 0;
        float totaleTabellaAlttez = 0;

        int larghezzaColonnaDescrizione = 0; //è variabile e da calcolare in base allo spazio disponibile
        int larghezzaColonnaFase1 = 130;
        int larghezzaColonnaFase2 = 150;
        int larghezzaColonnaFase3 = 130;
        int larghezzaUltimaColonna = 0;

        #endregion

        #region Piramide
        //disegno la parte della piramide
        Piramide modello = new Piramide();
        modello.writeSezione(document);
        Ypos = document.getLastPos();
        #endregion

        #region Disegno Tabella Fase 1,2,3

        int nFasi = 0;

        datitab = new DatiTabella();
        datitab.table = set.Tables["GraficoPiramide"];
        rowTemplate = datitab.table.Rows[0];
        Tabella TabellaAreaBisogno = new Tabella();
        TabellaAreaBisogno.LineeTabella = false;
        TabellaAreaBisogno.Y = Ypos;
        TabellaAreaBisogno.X = document.getMargineLeft();
        TabellaAreaBisogno.Header = true;
        TabellaAreaBisogno.HeaderFont = 8;
        TabellaAreaBisogno.SaltoPagina = false;
        TabellaAreaBisogno.Datasource = datitab.table;
        TabellaAreaBisogno.AltezzaCella = 30;

        larghezzaColonnaDescrizione = 512;

        // Definizione delle colonne
        foreach (DataColumn column in TabellaAreaBisogno.Datasource.Columns)
        {
            if (column.ColumnName.ToLower() == "fase1" && rowTemplate["fase1"].ToString() != string.Empty)
            {
                larghezzaColonnaDescrizione -= larghezzaColonnaFase1;
                larghezzaUltimaColonna = larghezzaColonnaFase1;
                TabellaAreaBisogno.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase1, TipoAllineamento.DESTRA, false, 8, false)); // -25 è la larghezza della colonna che conterrà l'immagine
                nFasi++;
            }
            if (column.ColumnName.ToLower() == "fase2" && rowTemplate["fase2"].ToString() != string.Empty)
            {
                larghezzaColonnaDescrizione -= larghezzaColonnaFase2;
                larghezzaUltimaColonna = larghezzaColonnaFase2;
                TabellaAreaBisogno.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase2, TipoAllineamento.DESTRA, false, 8, false));// -25 è la larghezza della colonna che conterrà l'immagine                                         
                nFasi++;
            }
            if (column.ColumnName.ToLower() == "fase3" && rowTemplate["fase3"].ToString() != string.Empty)
            {
                larghezzaColonnaDescrizione -= larghezzaColonnaFase3;
                larghezzaUltimaColonna = larghezzaColonnaFase3;
                TabellaAreaBisogno.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase3, TipoAllineamento.DESTRA, false, 8, false));// -25 è la larghezza della colonna che conterrà l'immagine                    
                nFasi++;
            }
        }

        TabellaAreaBisogno.Colonne.Insert(0, new Colonna("Descrizione", "", larghezzaColonnaDescrizione, TipoAllineamento.SINISTRA, false, 8, false));

        TabellaAreaBisogno.Draw(datitab, document);

        Ypos += TabellaAreaBisogno.AltezzaTabella;
        totaleTabellaAlttez += TabellaAreaBisogno.AltezzaTabella;

        #endregion

        #region Disegno Tabella Totale Risorse Allocate


        Ypos += 40;

        rowTemplate = set.Tables["RisorseNonAllocate"].Rows[0];

        if ((rowTemplate["fase1"].ToString() != "0,00" && rowTemplate["fase1"].ToString() != string.Empty) || (rowTemplate["fase2"].ToString() != "0,00" && rowTemplate["fase2"].ToString() != string.Empty) || (rowTemplate["fase3"].ToString() != "0,00" && rowTemplate["fase3"].ToString() != string.Empty))
        {
            datitab = new DatiTabella();
            datitab.table = set.Tables["RisorseAllocate"];
            rowTemplate = datitab.table.Rows[0];
            Tabella tabellaTotale = new Tabella();
            tabellaTotale.X = document.getMargineLeft();
            tabellaTotale.Y = Ypos;
            tabellaTotale.Header = false;
            tabellaTotale.SaltoPagina = false;
            tabellaTotale.Datasource = datitab.table;

            larghezzaColonnaDescrizione = 512;

            // Definizione delle colonne
            foreach (DataColumn column in tabellaTotale.Datasource.Columns)
            {
                if (column.ColumnName.ToLower() == "fase1" && rowTemplate["fase1"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase1;
                    tabellaTotale.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase1, TipoAllineamento.DESTRA, false, 8, true));
                }
                if (column.ColumnName.ToLower() == "fase2" && rowTemplate["fase2"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase2;
                    tabellaTotale.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase2, TipoAllineamento.DESTRA, false, 8, true));
                }
                if (column.ColumnName.ToLower() == "fase3" && rowTemplate["fase3"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase3;
                    tabellaTotale.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase3, TipoAllineamento.DESTRA, false, 8, true));
                }
            }

            tabellaTotale.Colonne.Insert(0, new Colonna("Descrizione", "", larghezzaColonnaDescrizione, TipoAllineamento.SINISTRA, false, 8, true));

            tabellaTotale.Draw(datitab, document);

            Ypos += tabellaTotale.AltezzaTabella;
            totaleTabellaAlttez += tabellaTotale.AltezzaTabella;
        }

        #endregion

        #region Disegno Tabella Risorse non allocate

        rowTemplate = set.Tables["RisorseNonAllocate"].Rows[0];

        if ((rowTemplate["fase1"].ToString() != "0,00" && rowTemplate["fase1"].ToString() != string.Empty) || (rowTemplate["fase2"].ToString() != "0,00" && rowTemplate["fase2"].ToString() != string.Empty) || (rowTemplate["fase3"].ToString() != "0,00" && rowTemplate["fase3"].ToString() != string.Empty))
        {
            //setta la datatable che contiene i Dati per disegnare la tabella del Risorse non allocate
            datitab = new DatiTabella();
            datitab.table = set.Tables["RisorseNonAllocate"];
            rowTemplate = datitab.table.Rows[0];
            Tabella tabellaRisorseNonAllocate = new Tabella();
            tabellaRisorseNonAllocate.X = document.getMargineLeft();
            tabellaRisorseNonAllocate.Y = Ypos;
            tabellaRisorseNonAllocate.Header = false;
            tabellaRisorseNonAllocate.SaltoPagina = false;
            tabellaRisorseNonAllocate.Datasource = datitab.table;

            larghezzaColonnaDescrizione = 512;

            // Definizione delle colonne
            foreach (DataColumn column in tabellaRisorseNonAllocate.Datasource.Columns)
            {
                if (column.ColumnName.ToLower() == "fase1" && rowTemplate["fase1"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase1;
                    tabellaRisorseNonAllocate.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase1, TipoAllineamento.DESTRA, false, 8, false));
                }
                if (column.ColumnName.ToLower() == "fase2" && rowTemplate["fase2"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase2;
                    tabellaRisorseNonAllocate.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase2, TipoAllineamento.DESTRA, false, 8, false));
                }
                if (column.ColumnName.ToLower() == "fase3" && rowTemplate["fase3"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase3;
                    tabellaRisorseNonAllocate.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase3, TipoAllineamento.DESTRA, false, 8, false));
                }
            }

            tabellaRisorseNonAllocate.Colonne.Insert(0, new Colonna("Descrizione", "", larghezzaColonnaDescrizione, TipoAllineamento.SINISTRA, false, 8, false));

            tabellaRisorseNonAllocate.Draw(datitab, document);

            Ypos += tabellaRisorseNonAllocate.AltezzaTabella;
            totaleTabellaAlttez += tabellaRisorseNonAllocate.AltezzaTabella;
        }
        #endregion

        #region Disegno TOTALE RISORSE FINANZIARIE

        DataRow rowTemplateCC = set.Tables["ContoCorrente"].Rows[0];
        DataRow rowTemplateSelf = set.Tables["LineaSelfNeg"].Rows[0];
        DataRow rowTemplateOC = set.Tables["OperazioniInCorso"].Rows[0];

        if (
            ((rowTemplateCC["fase1"].ToString() != "-" && rowTemplateCC["fase1"].ToString() != string.Empty) || (rowTemplateCC["fase2"].ToString() != "-" && rowTemplateCC["fase2"].ToString() != string.Empty) || (rowTemplateCC["fase3"].ToString() != "-" && rowTemplateCC["fase3"].ToString() != string.Empty))
            ||
            ((rowTemplateOC["fase1"].ToString() != "0,00" && rowTemplateOC["fase1"].ToString() != string.Empty) || (rowTemplateOC["fase2"].ToString() != "0,00" && rowTemplateOC["fase2"].ToString() != string.Empty) || (rowTemplateOC["fase3"].ToString() != "0,00" && rowTemplateOC["fase3"].ToString() != string.Empty))
             ||
            ((rowTemplateSelf["fase1"].ToString() != "-" && rowTemplateSelf["fase1"].ToString() != string.Empty) || (rowTemplateSelf["fase2"].ToString() != "-" && rowTemplateSelf["fase2"].ToString() != string.Empty) || (rowTemplateSelf["fase3"].ToString() != "-" && rowTemplateSelf["fase3"].ToString() != string.Empty))
            )
        {
            //setta la datatable che contiene i Dati per disegnare la tabella del Risorse non allocate
            datitab = new DatiTabella();
            datitab.table = set.Tables["RisorseFinanziarie"];
            rowTemplate = datitab.table.Rows[0];
            Tabella tabellaRisorseFinanziarie = new Tabella();
            tabellaRisorseFinanziarie.X = document.getMargineLeft();
            tabellaRisorseFinanziarie.Y = Ypos;
            tabellaRisorseFinanziarie.Header = false;
            tabellaRisorseFinanziarie.SaltoPagina = false;
            tabellaRisorseFinanziarie.Datasource = datitab.table;

            larghezzaColonnaDescrizione = 512;

            // Definizione delle colonne
            foreach (DataColumn column in tabellaRisorseFinanziarie.Datasource.Columns)
            {
                if (column.ColumnName.ToLower() == "fase1" && rowTemplate["fase1"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase1;
                    tabellaRisorseFinanziarie.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase1, TipoAllineamento.DESTRA, false, 8, true));
                }
                if (column.ColumnName.ToLower() == "fase2" && rowTemplate["fase2"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase2;
                    tabellaRisorseFinanziarie.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase2, TipoAllineamento.DESTRA, false, 8, true));
                }
                if (column.ColumnName.ToLower() == "fase3" && rowTemplate["fase3"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase3;
                    tabellaRisorseFinanziarie.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase3, TipoAllineamento.DESTRA, false, 8, true));
                }
            }

            tabellaRisorseFinanziarie.Colonne.Insert(0, new Colonna("Descrizione", "", larghezzaColonnaDescrizione, TipoAllineamento.SINISTRA, false, 8, true));

            tabellaRisorseFinanziarie.Draw(datitab, document);

            Ypos += tabellaRisorseFinanziarie.AltezzaTabella;
            totaleTabellaAlttez += tabellaRisorseFinanziarie.AltezzaTabella;
        }
        #endregion

        #region Disegno CONTO CORRENTE A SALDO NEGATIVO

        rowTemplate = set.Tables["ContoCorrente"].Rows[0];

        if ((rowTemplate["fase1"].ToString() != "-" && rowTemplate["fase1"].ToString() != string.Empty) || (rowTemplate["fase2"].ToString() != "-" && rowTemplate["fase2"].ToString() != string.Empty) || (rowTemplate["fase3"].ToString() != "-" && rowTemplate["fase3"].ToString() != string.Empty))
        {

            //setta la datatable che contiene i Dati per disegnare la tabella del Risorse non allocate
            datitab = new DatiTabella();
            datitab.table = set.Tables["ContoCorrente"];
            rowTemplate = datitab.table.Rows[0];
            Tabella tabellaContoCorrente = new Tabella();
            tabellaContoCorrente.X = document.getMargineLeft();
            tabellaContoCorrente.Y = Ypos;
            tabellaContoCorrente.Header = false;
            tabellaContoCorrente.SaltoPagina = false;
            tabellaContoCorrente.Datasource = datitab.table;

            larghezzaColonnaDescrizione = 512;

            // Definizione delle colonne
            foreach (DataColumn column in tabellaContoCorrente.Datasource.Columns)
            {
                if (column.ColumnName.ToLower() == "fase1" && rowTemplate["fase1"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase1;
                    tabellaContoCorrente.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase1, TipoAllineamento.DESTRA, false, 8, false));
                }
                if (column.ColumnName.ToLower() == "fase2" && rowTemplate["fase2"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase2;
                    tabellaContoCorrente.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase2, TipoAllineamento.DESTRA, false, 8, false));
                }
                if (column.ColumnName.ToLower() == "fase3" && rowTemplate["fase3"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase3;
                    tabellaContoCorrente.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase3, TipoAllineamento.DESTRA, false, 8, false));
                }
            }

            tabellaContoCorrente.Colonne.Insert(0, new Colonna("Descrizione", "", larghezzaColonnaDescrizione, TipoAllineamento.SINISTRA, false, 8, false));

            tabellaContoCorrente.Draw(datitab, document);

            Ypos += tabellaContoCorrente.AltezzaTabella;
            totaleTabellaAlttez += tabellaContoCorrente.AltezzaTabella;
        }

        #endregion

        #region Disegno Linea Self Negativa

        rowTemplate = set.Tables["LineaSelfNeg"].Rows[0];

        if ((rowTemplate["fase1"].ToString() != "-" && rowTemplate["fase1"].ToString() != string.Empty) || (rowTemplate["fase2"].ToString() != "-" && rowTemplate["fase2"].ToString() != string.Empty) || (rowTemplate["fase3"].ToString() != "-" && rowTemplate["fase3"].ToString() != string.Empty))
        {
            //setta la datatable che contiene i Dati per disegnare la tabella del Risorse non allocate
            datitab = new DatiTabella();
            datitab.table = set.Tables["LineaSelfNeg"];
            rowTemplate = datitab.table.Rows[0];
            Tabella tabellaLineaSelfNeg = new Tabella();
            tabellaLineaSelfNeg.X = document.getMargineLeft();
            tabellaLineaSelfNeg.Y = Ypos;
            tabellaLineaSelfNeg.Header = false;
            tabellaLineaSelfNeg.SaltoPagina = false;
            tabellaLineaSelfNeg.Datasource = datitab.table;
            //tabellaOperazioniInCorso.AltezzaCella = 30;

            larghezzaColonnaDescrizione = 512;

            // Definizione delle colonne
            foreach (DataColumn column in tabellaLineaSelfNeg.Datasource.Columns)
            {
                if (column.ColumnName.ToLower() == "fase1" && rowTemplate["fase1"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase1;
                    tabellaLineaSelfNeg.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase1, TipoAllineamento.DESTRA, false, 8, false));
                }
                if (column.ColumnName.ToLower() == "fase2" && rowTemplate["fase2"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase2;
                    tabellaLineaSelfNeg.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase2, TipoAllineamento.DESTRA, false, 8, false));
                }
                if (column.ColumnName.ToLower() == "fase3" && rowTemplate["fase3"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase3;
                    tabellaLineaSelfNeg.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase3, TipoAllineamento.DESTRA, false, 8, false));
                }
            }

            tabellaLineaSelfNeg.Colonne.Insert(0, new Colonna("Descrizione", "", larghezzaColonnaDescrizione, TipoAllineamento.SINISTRA, false, 8, false));

            tabellaLineaSelfNeg.Draw(datitab, document);

            Ypos += tabellaLineaSelfNeg.AltezzaTabella;
            totaleTabellaAlttez += tabellaLineaSelfNeg.AltezzaTabella;
        }

        #endregion

        #region Disegno Operazioni in corso

        rowTemplate = set.Tables["OperazioniInCorso"].Rows[0];

        if ((rowTemplate["fase1"].ToString() != "0,00" && rowTemplate["fase1"].ToString() != string.Empty) || (rowTemplate["fase2"].ToString() != "0,00" && rowTemplate["fase2"].ToString() != string.Empty) || (rowTemplate["fase3"].ToString() != "0,00" && rowTemplate["fase3"].ToString() != string.Empty))
        {
            //setta la datatable che contiene i Dati per disegnare la tabella del Risorse non allocate
            datitab = new DatiTabella();
            datitab.table = set.Tables["OperazioniInCorso"];
            rowTemplate = datitab.table.Rows[0];
            Tabella tabellaOperazioniInCorso = new Tabella();
            tabellaOperazioniInCorso.X = document.getMargineLeft();
            tabellaOperazioniInCorso.Y = Ypos;
            tabellaOperazioniInCorso.Header = false;
            tabellaOperazioniInCorso.SaltoPagina = false;
            tabellaOperazioniInCorso.Datasource = datitab.table;
            //tabellaOperazioniInCorso.AltezzaCella = 30;

            larghezzaColonnaDescrizione = 512;

            // Definizione delle colonne
            foreach (DataColumn column in tabellaOperazioniInCorso.Datasource.Columns)
            {
                if (column.ColumnName.ToLower() == "fase1" && rowTemplate["fase1"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase1;
                    tabellaOperazioniInCorso.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase1, TipoAllineamento.DESTRA, false, 8, false));
                }
                if (column.ColumnName.ToLower() == "fase2" && rowTemplate["fase2"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase2;
                    tabellaOperazioniInCorso.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase2, TipoAllineamento.DESTRA, false, 8, false));
                }
                if (column.ColumnName.ToLower() == "fase3" && rowTemplate["fase3"].ToString() != string.Empty)
                {
                    larghezzaColonnaDescrizione -= larghezzaColonnaFase3;
                    tabellaOperazioniInCorso.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase3, TipoAllineamento.DESTRA, false, 8, false));
                }
            }

            tabellaOperazioniInCorso.Colonne.Insert(0, new Colonna("Descrizione", "", larghezzaColonnaDescrizione, TipoAllineamento.SINISTRA, false, 8, false));

            tabellaOperazioniInCorso.Draw(datitab, document);

            Ypos += tabellaOperazioniInCorso.AltezzaTabella;
            totaleTabellaAlttez += tabellaOperazioniInCorso.AltezzaTabella;
        }

        #endregion

        

        #region Disegno Totale Patrimonto

        //setta la datatable che contiene i Dati per disegnare la tabella del Risorse non allocate
        datitab = new DatiTabella();
        datitab.table = set.Tables["DatiTotale"];
        rowTemplate = set.Tables["DatiTotale"].Rows[0];

        Tabella tabellaDatiTotale = new Tabella();
        tabellaDatiTotale.X = document.getMargineLeft();
        tabellaDatiTotale.Y = Ypos;
        tabellaDatiTotale.Header = false;
        tabellaDatiTotale.SaltoPagina = false;
        tabellaDatiTotale.Datasource = datitab.table;

        larghezzaColonnaDescrizione = 512;

        // Definizione delle colonne
        foreach (DataColumn column in tabellaDatiTotale.Datasource.Columns)
        {
            if (column.ColumnName.ToLower() == "fase1" && rowTemplate["fase1"].ToString() != string.Empty)
            {
                larghezzaColonnaDescrizione -= larghezzaColonnaFase1;
                tabellaDatiTotale.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase1, TipoAllineamento.DESTRA, true, 8, true));
            }
            if (column.ColumnName.ToLower() == "fase2" && rowTemplate["fase2"].ToString() != string.Empty)
            {
                larghezzaColonnaDescrizione -= larghezzaColonnaFase2;
                tabellaDatiTotale.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase2, TipoAllineamento.DESTRA, true, 8, true));
            }
            if (column.ColumnName.ToLower() == "fase3" && rowTemplate["fase3"].ToString() != string.Empty)
            {
                larghezzaColonnaDescrizione -= larghezzaColonnaFase3;
                tabellaDatiTotale.Colonne.Add(new Colonna(column.ColumnName, column.Caption, larghezzaColonnaFase3, TipoAllineamento.DESTRA, true, 8, true));
            }
        }

        tabellaDatiTotale.Colonne.Insert(0, new Colonna("Descrizione", "", larghezzaColonnaDescrizione, TipoAllineamento.SINISTRA, true, 8, true));

        tabellaDatiTotale.Draw(datitab, document);
        Ypos += tabellaDatiTotale.AltezzaTabella;
        totaleTabellaAlttez += tabellaDatiTotale.AltezzaTabella;

        #endregion

        #region Disegno Dettaglio Movimenti Dopo Dati Totale
        //La minima larghezza necessaria per la descrizione di questa nuova tabella è 159, quindi:
        //512 - 73 - 150 - 130 = 159.
        int dettaglioMovimentiLarghezzaColonnaDescrizione = 512;
        int dettaglioMovimentiLarghezzaColonnaFase1 = 73;
        int dettaglioMovimentiLarghezzaColonnaFase2 = 150;
        int dettaglioMovimentiLarghezzaColonnaFase3 = 130;
        int numeroFasiVisibili = 0;
        bool presenzaFase3 = false;
        int incrementoPixelRettangoloFase3 = 0;
        //Ypos -= 5;
        

        //setta la datatable che contiene i Dati per stampare righe ulteriori sotto al totale.
        datitab = new DatiTabella();
        datitab.table = set.Tables["RigheDopoDatiTotale"];
        rowTemplate = set.Tables["RigheDopoDatiTotale"].Rows[0];

        Tabella tabellaRigheDopoDatiTotale = new Tabella();
        tabellaRigheDopoDatiTotale.X = document.getMargineLeft();
        tabellaRigheDopoDatiTotale.Y = Ypos;
        tabellaRigheDopoDatiTotale.Header = false;
        tabellaRigheDopoDatiTotale.SaltoPagina = false;
        tabellaRigheDopoDatiTotale.Datasource = datitab.table;

        foreach (DataColumn column in tabellaRigheDopoDatiTotale.Datasource.Columns)
        {
            if (column.ColumnName.ToLower() == "fase1" && rowTemplate["fase1"].ToString() != string.Empty)
            {
                dettaglioMovimentiLarghezzaColonnaDescrizione -= dettaglioMovimentiLarghezzaColonnaFase1;
                if (rowTemplate["fase1"].ToString().Equals("notvisible"))
                {
                    tabellaRigheDopoDatiTotale.Colonne.Add(new Colonna("Fase1BlankValue",
                                                                        string.Empty,
                                                                        dettaglioMovimentiLarghezzaColonnaFase1,
                                                                        TipoAllineamento.DESTRA, false, 8, false));
                }
                else
                {
                    ++numeroFasiVisibili;
                    tabellaRigheDopoDatiTotale.Colonne.Add(new Colonna(column.ColumnName,
                                                                        column.Caption,
                                                                        dettaglioMovimentiLarghezzaColonnaFase1,
                                                                        TipoAllineamento.DESTRA, false, 8, false));
                }
            }
            if (column.ColumnName.ToLower() == "fase2" && rowTemplate["fase2"].ToString() != string.Empty)
            {
                dettaglioMovimentiLarghezzaColonnaDescrizione -= dettaglioMovimentiLarghezzaColonnaFase2;
                if (rowTemplate["fase2"].ToString().Equals("notvisible"))
                {
                    tabellaRigheDopoDatiTotale.Colonne.Add(new Colonna("Fase2BlankValue",
                                                                        string.Empty,
                                                                        dettaglioMovimentiLarghezzaColonnaFase2,
                                                                        TipoAllineamento.DESTRA, false, 8, false));
                }
                else
                {
                    ++numeroFasiVisibili;

                    //if (rowTemplate["fase2"].ToString() != "n.d.")
                    //{
                        tabellaRigheDopoDatiTotale.Colonne.Add(new Colonna(column.ColumnName,
                                                                            column.Caption,
                                                                            dettaglioMovimentiLarghezzaColonnaFase2,
                                                                            TipoAllineamento.DESTRA, false, 8, false));
                    //}
                    //else
                    //{
                    //    tabellaRigheDopoDatiTotale.Colonne.Add(new Colonna(column.ColumnName,
                    //                                                        column.Caption,
                    //                                                        dettaglioMovimentiLarghezzaColonnaFase2 - 50,
                    //                                                        TipoAllineamento.SINISTRA, true, 8, false));
                    //}
                }
            }
            if (column.ColumnName.ToLower() == "fase3" && rowTemplate["fase3"].ToString() != string.Empty)
            {
                dettaglioMovimentiLarghezzaColonnaDescrizione -= dettaglioMovimentiLarghezzaColonnaFase3;
                if (rowTemplate["fase3"].ToString().Equals("notvisible"))
                {
                    tabellaRigheDopoDatiTotale.Colonne.Add(new Colonna("Fase3BlankValue",
                                                                        string.Empty,
                                                                        dettaglioMovimentiLarghezzaColonnaFase3,
                                                                        TipoAllineamento.DESTRA, false, 8, false));
                }
                else
                {
                    ++numeroFasiVisibili;
                    tabellaRigheDopoDatiTotale.Colonne.Add(new Colonna(column.ColumnName,
                                                                        column.Caption,
                                                                        dettaglioMovimentiLarghezzaColonnaFase3,
                                                                        TipoAllineamento.DESTRA, false, 8, false));
                    presenzaFase3 = true;
                }
            }
        }

        if (numeroFasiVisibili > 0)
        {
            tabellaRigheDopoDatiTotale.Colonne.Insert(0, new Colonna("Descrizione", "", dettaglioMovimentiLarghezzaColonnaDescrizione, TipoAllineamento.SINISTRA, false, 8, false));
            if (presenzaFase3) incrementoPixelRettangoloFase3 += 14;
        }

        tabellaRigheDopoDatiTotale.Draw(datitab, document);
        Ypos += tabellaRigheDopoDatiTotale.AltezzaTabella;
        totaleTabellaAlttez += tabellaRigheDopoDatiTotale.AltezzaTabella;
        #endregion

        #region Disegno riquadro su Situazione Corrente e freccia

        //float totaleTabellaAltteza = TabellaAreaBisogno.AltezzaTabella + tabellaTotale.AltezzaCella + tabellaContoCorrente.AltezzaCella + tabellaOperazioniInCorso.AltezzaCella + tabellaRisorseNonAllocate.AltezzaCella + tabellaRisorseFinanziarie.AltezzaCella + tabellaDatiTotale.AltezzaTabella + 10;        

        // riquadro intorno la colonna Situazione Corrente
        float xRiquadro = 528 - larghezzaUltimaColonna + (larghezzaUltimaColonna / 2);

        ceTe.DynamicPDF.Merger.ImportedPage page = document.getCurrentPage();
        Rectangle rettangoloSuSituazioneCorrente;
        //rettangoloSuSituazioneCorrente = new Rectangle(xRiquadro, TabellaAreaBisogno.Y - 10, larghezzaUltimaColonna - 35, totaleTabellaAlttez - 13 + incrementoPixelRettangoloFase3);
        rettangoloSuSituazioneCorrente = new Rectangle(xRiquadro, TabellaAreaBisogno.Y - 10, larghezzaUltimaColonna - 35, totaleTabellaAlttez - 7 + incrementoPixelRettangoloFase3);
        rettangoloSuSituazioneCorrente.CornerRadius = 5;
        page.Elements.Add(rettangoloSuSituazioneCorrente);


        System.Reflection.Assembly thisExe;
        thisExe = System.Reflection.Assembly.GetExecutingAssembly();
        System.IO.Stream file = thisExe.GetManifestResourceStream(UtilityManager.getAppSetting("FrecciaS80"));

        switch (nFasi)
        {
            case 2: // Una freccia tra "attivazione monitoraggio" e "situazione corrente" o tra "Precedente invio report" e "situazione corrente"
                page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(file, document.getMargineLeft() + 395, TabellaAreaBisogno.Y - 10, 0.57F));
                break;

            case 3: // Una freccia tra "attivazione monitoraggio" e  "precedente invio report" e tra  "precedente invio report" e "situazione corrente"
                page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(file, document.getMargineLeft() + 245, TabellaAreaBisogno.Y - 10, 0.57F));
                page.Elements.Add(new ceTe.DynamicPDF.PageElements.Image(file, document.getMargineLeft() + 395, TabellaAreaBisogno.Y - 10, 0.57F));
                break;

            default:
                break;
        }



        #endregion

        #region Disegno Didascalie Note Dettaglio Movimenti
        Ypos -= 13;

        DataTable dtRighe = set.Tables["RigheDopoDatiTotale"];
        DataTable dtDidascalieDB = set.Tables["DettaglioMovimentiDidascalieNote"];

        int fase1NumeroAsterischi = 0;
        int fase2NumeroAsterischi = 0;
        int fase3NumeroAsterischi = 0;
        if (!string.IsNullOrEmpty(dtRighe.Rows[0]["Fase1NumeroAsterischiNota"].ToString())) fase1NumeroAsterischi = Convert.ToInt16(dtRighe.Rows[0]["Fase1NumeroAsterischiNota"]);
        if (!string.IsNullOrEmpty(dtRighe.Rows[0]["Fase2NumeroAsterischiNota"].ToString())) fase2NumeroAsterischi = Convert.ToInt16(dtRighe.Rows[0]["Fase2NumeroAsterischiNota"]);
        if (!string.IsNullOrEmpty(dtRighe.Rows[0]["Fase3NumeroAsterischiNota"].ToString())) fase3NumeroAsterischi = Convert.ToInt16(dtRighe.Rows[0]["Fase3NumeroAsterischiNota"]);

        DataTable dtDidascalieInUso = new DataTable();
        dtDidascalieInUso.Columns.Add(new DataColumn("SequenzaAsterischi", typeof(string)));
        dtDidascalieInUso.Columns.Add(new DataColumn("Descrizione", typeof(string)));
        DataRow dr;
        DataRow[] righeDidascalia;
        string accodamento = string.Empty;

        // lele
        if (fase1NumeroAsterischi > 0)
        {
            righeDidascalia = dtDidascalieDB.Select(string.Format(" NumeroAsterischi = {0}", fase1NumeroAsterischi));
            if (righeDidascalia.Length > 0)
            {
                dr = dtDidascalieInUso.NewRow();
                accodamento = "(";
                for (int i = 0; i < fase1NumeroAsterischi; i++)
                {
                    accodamento = string.Format("{0}{1}", accodamento, "*");
                }
                accodamento = string.Format("{0})", accodamento);
                dr["SequenzaAsterischi"] = accodamento;
                dr["Descrizione"] = righeDidascalia[0]["Descrizione"].ToString();
                dtDidascalieInUso.Rows.Add(dr);
            }
        }

        if (fase2NumeroAsterischi > 0)
        {
            righeDidascalia = dtDidascalieDB.Select(string.Format(" NumeroAsterischi = {0}", fase2NumeroAsterischi));
            if (righeDidascalia.Length > 0)
            {
                dr = dtDidascalieInUso.NewRow();
                accodamento = "(";
                for (int i = 0; i < fase2NumeroAsterischi; i++)
                {
                    accodamento = string.Format("{0}{1}", accodamento, "*");
                }
                accodamento = string.Format("{0})", accodamento);
                if (fase3NumeroAsterischi == 0)
                {
                    dr["SequenzaAsterischi"] = accodamento;
                    dr["Descrizione"] = righeDidascalia[0]["Descrizione"].ToString();
                    
                }
                dtDidascalieInUso.Rows.Add(dr);
            }
        }

        if (fase3NumeroAsterischi > 0)
        {
            righeDidascalia = dtDidascalieDB.Select(string.Format(" NumeroAsterischi = {0}", fase3NumeroAsterischi));
            if (righeDidascalia.Length > 0)
            {
                dr = dtDidascalieInUso.NewRow();
                accodamento = "(";
                for (int i = 0; i < fase3NumeroAsterischi; i++)
                {
                    accodamento = string.Format("{0}{1}", accodamento, "*");
                }
                accodamento = string.Format("{0})", accodamento);
               
                    dr["SequenzaAsterischi"] = accodamento + " " + righeDidascalia[0]["Descrizione"].ToString();
                    //remmare questa se non si vuole far uscire didascalia note?
                    dr["Descrizione"] = righeDidascalia[0]["Descrizione"].ToString();
              
               
                dtDidascalieInUso.Rows.Add(dr);
            }
        }

        datitab = new DatiTabella();
        datitab.table = dtDidascalieInUso;

        Tabella tabellaDidascalieNote = new Tabella();
        tabellaDidascalieNote.X = document.getMargineLeft();
        tabellaDidascalieNote.Y = Ypos;
        tabellaDidascalieNote.Header = false;
        tabellaDidascalieNote.SaltoPagina = false;
        tabellaDidascalieNote.DimensioneLinea = 0;
        tabellaDidascalieNote.AltezzaCella = 10;
        tabellaDidascalieNote.DisegnaHeaderAlCambioPagina = false;
        tabellaDidascalieNote.Datasource = datitab.table;

        //ATTENZIONE:
        //A causa del tipo Tabella, per la colonna "SequenzaAsterischi" (con dimensione del testo pari ad 8)
        //un valore di larghezza inferiore a 25 renderà imposibile visualizzare correttamente le sequenze di asterischi
        //che verranno visualizzate correttamente solo nel caso siano (*), (**), (***).
        //512 è il valore della larghezza standard delle tabelle usate in questo documento pdf.
        tabellaDidascalieNote.Colonne.Add(new Colonna("SequenzaAsterischi",
                                                    string.Empty,
                                                    512,
                                                    TipoAllineamento.SINISTRA, false, 6, false));

/*        tabellaDidascalieNote.Colonne.Add(new Colonna("Descrizione",
                                                            string.Empty,
                                                            512 - 12,
                                                            TipoAllineamento.SINISTRA, false, 6, false));*/

        tabellaDidascalieNote.Draw(datitab, document);
        Ypos += tabellaDidascalieNote.AltezzaTabella;
        totaleTabellaAlttez += tabellaDidascalieNote.AltezzaTabella;
        #endregion

        document.setLastPos(totaleTabellaAlttez);
    }
}