using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BL;
using BE;
using System.Data;
using System.Text;
using SHDocVw;
using System.Threading;
using System.Globalization;
using System.ComponentModel;
using System.IO;
public partial class FrmAsientosContablesA : System.Web.UI.Page
{ DataTable dtt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
       
        BLAsientosContablesDetalle BL_ACDET = new BLAsientosContablesDetalle();
  
           if (!IsPostBack)
           {          ViewState["Acumulado"] = Request.QueryString["ACU"];     
               Int32 EID=  Convert.ToInt32(Request.QueryString["EID"].ToString());
               String[] F1F2=Meses(Convert.ToInt32(Request.QueryString["m"].ToString())).Split('*');
               DateTime F1 = Convert.ToDateTime(F1F2[0]);
               DateTime F2 = Convert.ToDateTime(F1F2[1]);
           
               String PER=Request.QueryString["pe"].ToString();
               ViewState["UnidadNegocioID"] = Request.QueryString["un"].ToString();
               if (ViewState["UnidadNegocioID"].ToString() != "0")
               {
                   BLUnidadesDeNegocio oBLUnidadesDeNegocio = new BLUnidadesDeNegocio();
                   BEUnidadesDeNegocio oBEUnidadesDeNegocio = new BEUnidadesDeNegocio();
                   oBEUnidadesDeNegocio.EstablecimientoID = Convert.ToInt32(EID.ToString());
                   List<BEUnidadesDeNegocio> loUnidadesDeNegocio;
                   loUnidadesDeNegocio = oBLUnidadesDeNegocio.fnSelUnidadesDeNegocioDin(oBEUnidadesDeNegocio, "and UnidadNegocioID=" + ViewState["UnidadNegocioID"].ToString().Trim(), "");
                   lblUnidadNegocio.Text = loUnidadesDeNegocio[0].NombreUnidadNegocio;
               }
               else
               {
                   lblUnidadNegocio.Text = "Todas";
               }               
               ViewState["AreaID"] = Request.QueryString["ar"].ToString();
               if (ViewState["AreaID"].ToString() != "0")
               {
                   BLAreas oBLAreas = new BLAreas();
                   List<BEAreas> loAreas;
                   loAreas = oBLAreas.fnSelAreasDin("AreaID=" + ViewState["AreaID"].ToString().Trim(), "");
                   lblArea.Text = loAreas[0].NombreArea;
               }
               else
               {
                   lblArea.Text = "Todas";
               }
               ViewState["HotelID"] = Request.QueryString["hot"].ToString();
               if (ViewState["HotelID"].ToString() != "0")
               {
                   BLHoteles oBLHoteles = new BLHoteles();
                   List<BEHoteles> loHoteles;
                   loHoteles = oBLHoteles.fnSelHotelesDin("Where HotelID=" + ViewState["HotelID"].ToString().Trim(), "");
                   lblSucursal.Text = loHoteles[0].NombreHotel;
               }
               else
               {
                   lblSucursal.Text = "Todas";
               }
               String RUC="";
               Int32 SMIN = Convert.ToInt32(Request.QueryString["sm"].ToString());
               Int32 MON = Convert.ToInt32(Request.QueryString["mo"].ToString());
               this.lblMoneda.Text = Utilitaria.Cls_Utilitaria.f_MostraNombreMoneda(Convert.ToInt32(MON), 3);
               String PLANCUENTAID=Request.QueryString["s"].ToString();
                //public DataTable EstadosCuentaSaldos(Int32 EmpresaId, String Ejercicio, DateTime FechaInicio, DateTime FechaFin, String Ruc, Decimal SaldoMinimo, Int32 Moneda, String PlanCuentaListBox)
               dtt = BL_ACDET.EstadosCuentaSaldos(EID, PER, F1, F2, RUC, SMIN, MON, PLANCUENTAID);
               Session["dtvista"] = dtt;                
               GridView1.DataSource = dtt;
               GridView1.DataBind();
           }
    }

    public string Meses (int cod)
    {
        string f1="";        string f2="";
        switch (cod)
        {

              //  fecha1 = new DateTime(fechatemp.Year, fechatemp.Month, 1);
               // fecha2 = new DateTime(fechatemp.Year, fechatemp.Month + 1, 1).AddDays(-1);
            case 0: lblMes.Text = "MES : APERTURA";
                break;
            case 1:
              lblMes.Text=  (ViewState["Acumulado"].ToString() == "1") ?"AL MES DE ENERO" : "MES : ENERO";
                f1 = new DateTime(DateTime.Today.Year, cod, 1).ToShortDateString();
                f2 = new DateTime(DateTime.Today.Year, 2, 1).AddDays(-1).ToShortDateString();
                break;
            case 2: lblMes.Text = (ViewState["Acumulado"].ToString() == "1") ? "AL MES DE FEBRERO" : "MES : FEBRERO";
                f1 = new DateTime(DateTime.Today.Year, cod, 1).ToShortDateString();
                f2 = new DateTime(DateTime.Today.Year, 2, 1).AddDays(-1).ToShortDateString();
                break;
            case 3: lblMes.Text = (ViewState["Acumulado"].ToString() == "1") ? "AL MES DE MARZO" : "MES : MARZO";
                f1 = new DateTime(DateTime.Today.Year, cod, 1).ToShortDateString();
                f2 = new DateTime(DateTime.Today.Year, 2, 1).AddDays(-1).ToShortDateString();
                break;
            case 4: lblMes.Text = (ViewState["Acumulado"].ToString() == "1") ? "AL MES DE ABRIL" : "MES : ABRIL";
                f1 = new DateTime(DateTime.Today.Year, cod, 1).ToShortDateString();
                f2 = new DateTime(DateTime.Today.Year, 2, 1).AddDays(-1).ToShortDateString();
                break;
            case 5: lblMes.Text = (ViewState["Acumulado"].ToString() == "1") ? "AL MES DE MAYO" : "MES : MAYO";
                f1 = new DateTime(DateTime.Today.Year, cod, 1).ToShortDateString();
                f2 = new DateTime(DateTime.Today.Year, 2, 1).AddDays(-1).ToShortDateString();
                break;
            case 6: lblMes.Text = (ViewState["Acumulado"].ToString() == "1") ? "AL MES DE JUNIO" : "MES : JUNIO";
                f1 = new DateTime(DateTime.Today.Year, cod, 1).ToShortDateString();
                f2 = new DateTime(DateTime.Today.Year, 2, 1).AddDays(-1).ToShortDateString();
                break;
            case 7: lblMes.Text = (ViewState["Acumulado"].ToString() == "1") ? "AL MES DE JULIO" : "MES : JULIO";
                f1 = new DateTime(DateTime.Today.Year, cod, 1).ToShortDateString();
                f2 = new DateTime(DateTime.Today.Year, 2, 1).AddDays(-1).ToShortDateString();
                break;
            case 8: lblMes.Text = (ViewState["Acumulado"].ToString() == "1") ? "AL MES DE AGOSTO" : "MES : AGOSTO";
                f1 = new DateTime(DateTime.Today.Year, cod, 1).ToShortDateString();
                f2 = new DateTime(DateTime.Today.Year, 2, 1).AddDays(-1).ToShortDateString();
                break;
            case 9: lblMes.Text = (ViewState["Acumulado"].ToString() == "1") ? "AL MES DE SETIEMBRE" : "MES : SETIEMBRE";
                f1 = new DateTime(DateTime.Today.Year, cod, 1).ToShortDateString();
                f2 = new DateTime(DateTime.Today.Year, 2, 1).AddDays(-1).ToShortDateString();
                break;
            case 10: lblMes.Text = (ViewState["Acumulado"].ToString() == "1") ? "AL MES DE OCTUBRE" : "MES : OCTUBRE";
                f1 = new DateTime(DateTime.Today.Year, cod, 1).ToShortDateString();
                f2 = new DateTime(DateTime.Today.Year, 2, 1).AddDays(-1).ToShortDateString();
                break;
            case 11: lblMes.Text = (ViewState["Acumulado"].ToString() == "1") ? "AL MES DE NOVIEMBRE" : "MES : NOVIEMBRE";
                f1 = new DateTime(DateTime.Today.Year, cod, 1).ToShortDateString();
                f2 = new DateTime(DateTime.Today.Year, 2, 1).AddDays(-1).ToShortDateString();
                break;
            case 12: lblMes.Text = (ViewState["Acumulado"].ToString() == "1") ? "AL MES DE DICIEMBRE" : "MES : DICIEMBRE";
                f1 = new DateTime(DateTime.Today.Year, cod, 1).ToShortDateString();
                f2 = new DateTime(DateTime.Today.Year+1, 1, 1).AddDays(-1).ToShortDateString();
                break;
            case 13: lblMes.Text = "MES : CIERRE";
                break;
        }
       
        string retur = f1 + "*" + f2;
        return retur ;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        filtros();
        Busqueda.Value = "";
    }

    void filtros()
    {
        string indi = Busqueda.Value;
        DataTable table = (DataTable)Session["dtvista"];
        DataView dv = new DataView(table);


        if (indi == "1")
        {
            string ruc = TextBox1.Text.Trim();
            dv.RowFilter = "Ruc like '" + ruc + "%'";
            GridView1.DataSource = dv;
            GridView1.DataBind();

        }
        else if (indi == "2")
        {
            string rs = TextBox2.Text.Trim();
            dv.RowFilter = "RazonSocial like '" + rs + "%'";
            GridView1.DataSource = dv;
            GridView1.DataBind();

        }
        else if (indi == "3")
        {
            string documento = TextBox3.Text.Trim();
            dv.RowFilter = "NumeroDocumento like '" + documento + "%'";
            GridView1.DataSource = dv;
            GridView1.DataBind();
        }
        Busqueda.Value = "";
    }


    public void ExportToExcel()
    {


        DataTable dt = new DataTable(); 
        dt = (DataTable)Session["dtvista"];


        if (dt.Rows.Count > 0)
        {
            //GridView dgGrid = new GridView();
            DataGrid dgGrid = new DataGrid(); 
            dgGrid.HeaderStyle.CssClass = "hed";
            dgGrid.DataSource = dt;
            dgGrid.DataBind();
            dgGrid.Font.Size = 7;

            string attachment = "attachment; filename=EstadoCuenta.xls";
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/ms-excel";
            Response.ContentType.ToString();
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            foreach (DataGridItem itm in dgGrid.Items)
            {
               
                // foreach (TableCell tc in itm.Cells)
                for (int i = 0; i < itm.Cells.Count; i++)
                {
                    TableCell tc = itm.Cells[i];
                    switch (dt.Columns[i].ColumnName.ToString().ToLower().Trim())
                    {
                        case "debesoles":     
                            tc.Attributes.Add("class", "numeric");                            
                            tc.Text =Convert.ToString(tc.Text).Replace(".",",");
                            break;

                        case "habersoles":
                            tc.Attributes.Add("class", "numeric");
                            tc.Text = Convert.ToString(tc.Text).Replace(".", ",");

                            break;

                        case "saldosoles":
                            tc.Attributes.Add("class", "numeric");
                            tc.Text = Convert.ToString(tc.Text).Replace(".", ",");
                            break;

                        case "debedolares":
                            tc.Attributes.Add("class", "numeric");
                            tc.Text = Convert.ToString(tc.Text).Replace(".", ",");
                            break;

                        case "haberdolares":
                            tc.Attributes.Add("class", "numeric");
                            tc.Text = Convert.ToString(tc.Text).Replace(".", ",");
                            break;

                        case "saldodolares":
                            tc.Attributes.Add("class", "numeric");
                            tc.Text = Convert.ToString(tc.Text).Replace(".", ",");
                            break;



                        case "Dia":
                            if (tc.Text == "01/01/1900 12:00:00 a.m.")
                            {
                                tc.Text = "";
                            }
                            break;
                        default:
                            tc.Attributes.Add("class", "text");
                            break;
                    }
                }
            }

            dgGrid.RenderControl(htw);
            //format the excel cells to text format
            string style = @"<style> .text { mso-number-format:\@; } </style> ";
            string customHTML = "<table><tr><td  colspan=\"5\"   align=\"left\" ><b>" + (Session["Compania"].ToString().Trim()) + "</b><br><b>Estado de Cuenta<b><br><b>" + lblMes.Text + "        Moneda: " + lblMoneda.Text + "</b></td><td colspan=\"5\"></td><td align=\"left\"><b>" + DateTime.Now.ToShortDateString() + "</b><br><b>" + DateTime.Now.ToShortTimeString() + "</b><br><b>" + Convert.ToString(Session["Usuario"]) + "</b></td></tr><table><hr>";
           Response.Write(customHTML);
            var isoEncoding = Encoding.GetEncoding("iso-8859-1");
            Response.Charset = isoEncoding.WebName;
            Response.ContentEncoding = isoEncoding;
            Response.Write(style);
            Response.ContentType = "application/text";
            Response.Write(sw.ToString());
            Response.End();

        }
    }
    //protected void expo_Click(object sender, EventArgs e)
    //{
       
       
    //}
    protected void ImageButton_Click(object sender, ImageClickEventArgs e)
    {
 string indi = Busqueda.Value;
        if  (indi =="1" || indi== "2" || indi == "3")
        {
            filtros();
        }
        else
        { ExportToExcel(); }
    }
}