using System;
using System.Web;
//using System.Web.Services;
//using System.Web.Services.Protocols;
using System.Text;
using System.Data;
using System.Collections;
using System.Configuration;
using PDFGenerator.BusinessLayer.DataSection;
using PDFGenerator.BusinessLayer;
using System.Collections.Generic;
using PDFGenerator;


public class DSS43ProdottiAreeBisogno : IDataSection
{

    public DataSectionResult getDataSection(List<SessionStruct> tabelleSessione, string querySql, DataThread dataThread)
    {
        try
        {
            DataSectionResult dsr = new DataSectionResult();

            FormatNum conv = new FormatNum();
            string area = string.Empty;
            string nomeProgetto = string.Empty;
            string ordinamentoProgetto = string.Empty;
            DataSetS43 ds43 = new DataSetS43();

            ds43.ProdottiAreeBisogno.Columns["Controvalore"].Caption = "Controvalore <br> (€) &nbsp; &nbsp; &nbsp; &nbsp;";

            DataTable dt = SectionManager.GetDataSection(tabelleSessione, querySql, dataThread);

            DataSetS43.ProdottiAreeBisognoRow dr;
            DataSetS43.ProdottiTotaleRow drTot;
            DataSetS43.DettaglioProgettiRow drProgetti;

            foreach (DataRow row in dt.Rows)
            {
                dr = ds43.ProdottiAreeBisogno.NewProdottiAreeBisognoRow();

                //Se cambia l'area popolo la tabella dei totali. GESTIRE IL CASO DEI PROGETTI PER Inv
                if (area != row["needarea"].ToString())
                {
                    drTot = ds43.ProdottiTotale.NewProdottiTotaleRow();
                    drTot.AreaBisogno = row["needareades"].ToString();
                    if (row["needarea"].ToString().ToLower() == "na")
                        drTot.Totale = "TOTALE RISORSE " + row["needareades"].ToString().ToUpper();
                    else
                        drTot.Totale = "TOTALE " + row["needareades"].ToString().ToUpper();
                    drTot.CodiceAreaBisogno = row["needarea"].ToString();
                    drTot.Controvalore = Convert.ToDecimal(row["somma_controval_needarea"]);

                    if (row["var_needareaString"] != DBNull.Value)
                        drTot.Var = row["var_needareaString"].ToString();
                    else
                        drTot.Var = conv.ConvertNum(row["var_needarea"]);


                    ds43.ProdottiTotale.AddProdottiTotaleRow(drTot);
                }


                area = row["needarea"].ToString();

                //V
                //if (area == "Inv" && nomeProgetto != row["nome_progetto"].ToString())
                if (dataThread.Periodico)
                {
                    if (area == "Inv" && ordinamentoProgetto != row["ORDINAMENTO_PROGETTO"].ToString())
                    {
                        drProgetti = ds43.DettaglioProgetti.NewDettaglioProgettiRow();
                        drProgetti.NomeProgetto = row["nome_progetto"].ToString();
                        drProgetti.Controvalore = Convert.ToDecimal(row["somma_controval_need_prog"]);
                        drProgetti.OrdinamentoProgetto = row["ORDINAMENTO_PROGETTO"].ToString();
                        if (row["var_need_progString"] != DBNull.Value)
                            drProgetti.Var = row["var_need_progString"].ToString();
                        else
                            drProgetti.Var = conv.ConvertNum(row["var_need_prog"]);


                        ds43.DettaglioProgetti.AddDettaglioProgettiRow(drProgetti);
                    }
                    else
                    {
                        if (area == "Inv" && nomeProgetto != row["nome_progetto"].ToString())
                        {
                            drProgetti = ds43.DettaglioProgetti.NewDettaglioProgettiRow();
                            drProgetti.NomeProgetto = row["nome_progetto"].ToString();
                            drProgetti.Controvalore = Convert.ToDecimal(row["somma_controval_need_prog"]);
                            drProgetti.OrdinamentoProgetto = row["ORDINAMENTO_PROGETTO"].ToString();
                            if (row["var_need_progString"] != DBNull.Value)
                                drProgetti.Var = row["var_need_progString"].ToString();
                            else
                                drProgetti.Var = conv.ConvertNum(row["var_need_prog"]);


                            ds43.DettaglioProgetti.AddDettaglioProgettiRow(drProgetti);
                        }
                    }
                }
                else
                {
                    if (area == "Inv" && nomeProgetto != row["nome_progetto"].ToString())
                    {
                        drProgetti = ds43.DettaglioProgetti.NewDettaglioProgettiRow();
                        drProgetti.NomeProgetto = row["nome_progetto"].ToString();
                        drProgetti.Controvalore = Convert.ToDecimal(row["somma_controval_need_prog"]);
                        //drProgetti.OrdinamentoProgetto = row["ORDINAMENTO_PROGETTO"].ToString();
                        if (row["var_need_progString"] != DBNull.Value)
                            drProgetti.Var = row["var_need_progString"].ToString();
                        else
                            drProgetti.Var = conv.ConvertNum(row["var_need_prog"]);


                        ds43.DettaglioProgetti.AddDettaglioProgettiRow(drProgetti);
                    }
                    else
                    {
                        if (area == "Inv" && nomeProgetto != row["nome_progetto"].ToString())
                        {
                            drProgetti = ds43.DettaglioProgetti.NewDettaglioProgettiRow();
                            drProgetti.NomeProgetto = row["nome_progetto"].ToString();
                            drProgetti.Controvalore = Convert.ToDecimal(row["somma_controval_need_prog"]);
                            //drProgetti.OrdinamentoProgetto = row["ORDINAMENTO_PROGETTO"].ToString();
                            if (row["var_need_progString"] != DBNull.Value)
                                drProgetti.Var = row["var_need_progString"].ToString();
                            else
                                drProgetti.Var = conv.ConvertNum(row["var_need_prog"]);


                            ds43.DettaglioProgetti.AddDettaglioProgettiRow(drProgetti);
                        }
                    }
                }
                //V Modifica fatta in trimestrale per gestire il doppio nome progetto uguale in monitoraggio
                if(dataThread.Periodico)
                    ordinamentoProgetto = row["ORDINAMENTO_PROGETTO"].ToString();
                nomeProgetto = row["nome_progetto"].ToString();

                //V Modifica fatta in trimestrale per gestire il doppio nome progetto uguale in monitoraggio
                if (dataThread.Periodico)
                    dr.OrdinamentoProgetto = row["ORDINAMENTO_PROGETTO"].ToString();

                dr.AreaBisogno = row["needareades"].ToString();
                dr.Progetto = nomeProgetto;
                dr.CodiceAreaBisogno = row["needarea"].ToString();
                dr.DataSottoscrizione = row["dataSottoscrizione"].ToString();
                //Hazem
                //dr.Contratto = row["contrattoDossier"].ToString();
                dr.Descrizione = row["descrizione"].ToString();

                //if (row["PartitaViaggiante"] != DBNull.Value && Convert.ToDouble(row["PartitaViaggiante"]) > 0)
                //    dr.__IMAGE_ = UtilityManager.getAppSetting("PartiteViaggianti");
                //else

                dr.__IMAGE_ = string.Empty;

                dr.Controvalore = Convert.ToDecimal(row["controvalore"]);

                if (row["varProdottoString"] != DBNull.Value)
                    dr.Var = row["varProdottoString"].ToString();
                else
                    dr.Var = conv.ConvertNum(row["varProdotto"]);

                //Hazem
                if (row["CreditRiskString"] != DBNull.Value)
                    dr.CreditRisk = row["CreditRiskString"].ToString();
                else
                    dr.CreditRisk = UtilityBusinessLayer.GetRiskCreditEmitClassName(row["CreditRiskEmit"].ToString());

                dr.PerformanceYTD = conv.ConvertNum(row["PerformanceYTD"]);

                dr.PerformanceDS = conv.ConvertNum(row["performanceDS"]);
                dr.CodiceAreaBisogno = row["needarea"].ToString();

                // Nota sul grado di copertura
                if (!string.IsNullOrEmpty(row["copertura_needareaString"].ToString()))
                    dr.Copertura = row["copertura_needareaString"].ToString();

                //Bido Jan 2011 release.
                //Section 43.
                //if (area == "Liq" && dataThread.ReportType().TipoReport == TipoReport.MONITORAGGIO)
                //{
                //    dr.CreditRisk = "n.a.";
                //    dr.Var = "0,00";
                //}
                //End Bido Jan 2011 release

                ds43.ProdottiAreeBisogno.AddProdottiAreeBisognoRow(dr);
            }


            #region conti correnti saldo negativo
            DataTable dt_A = null;

            if (dataThread.Periodico)
                dt_A = SectionManager.GetDataSection(tabelleSessione, "[C6MartPeriodico].[PL_S43ContoCorrenteBF]", dataThread);
            else
                dt_A = SectionManager.GetDataSection(tabelleSessione, "[C6Mart].[PL_S43ContoCorrenteBF]", dataThread);

            DataSetS43.ContoCorrenteRow dr_A;
            foreach (DataRow row in dt_A.Rows)
            {
                dr_A = ds43.ContoCorrente.NewContoCorrenteRow();
                dr_A.Data = row["datadiSottoscrizione"].ToString();
                dr_A.Numero = row["conto"].ToString();

                if (row["ControValore"] != DBNull.Value)
                    dr_A.SaldoContabile = decimal.Parse(row["ControValore"].ToString());

                if (row["SALDO_DISPONIBILE"] != DBNull.Value)
                    dr_A.SaldoDisponibile = decimal.Parse(row["SALDO_DISPONIBILE"].ToString());

                dr_A.CreditRisk = "n.a.";
                dr_A.Var = "0.00";

                ds43.ContoCorrente.AddContoCorrenteRow(dr_A);
            }
            #endregion

                #region conti correnti saldo negativo
                DataTable dt_B = null;

                if (dataThread.Periodico)
                    dt_B = SectionManager.GetDataSection(tabelleSessione, "[C6MartPeriodico].[PL_S43LineaSelfBF]", dataThread);
                else
                    dt_B = SectionManager.GetDataSection(tabelleSessione, "[C6Mart].[PL_S43LineaSelfBF]", dataThread);

                DataSetS43.LineaSelfRow dr_B;
                foreach (DataRow row in dt_B.Rows)
                {
                    dr_B = ds43.LineaSelf.NewLineaSelfRow();
                    dr_B.Numero = row["NumContratto"].ToString();
                    dr_B.SaldoContabile = decimal.Parse(row["ControValore"].ToString());
                    dr_B.Data = row["DataSott"].ToString();
                    dr_B.NomeProdotto = row["NomeProdotto"].ToString();  
                    ds43.LineaSelf.AddLineaSelfRow(dr_B);
                }
                #endregion

            dsr.DatiSezione = ds43;
            dsr.Esito = ds43.ProdottiAreeBisogno.Rows.Count;
            return dsr;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }


}