Esempio n. 1
0
        protected void btn_informe_Click(object sender, EventArgs e)
        {
            //ScriptManager.RegisterStartupScript(Page, this.GetType(), "teeee", "<script> new Tablesort(document.getElementById('ContentPlaceHolder_Contenido_G_PRODUCTOS')); </script>", false);

            //if (l_vendedores.Text != "" && l_clientes.Text != "" && txt_desde.Text != "" && txt_hasta.Text != "" && l_grupos.Text != "")
            //{
            string vendedores = agregra_comillas(l_vendedores.Text);
            string clientes   = agregra_comillas(l_clientes.Text);
            string desde      = txt_desde.Text;
            string hasta      = txt_hasta.Text;
            string grupos     = agregra_comillas(l_grupos.Text);;

            string grupos_del_usuario = agregra_comillas(ReporteRNegocio.grupos_usuario(User.Identity.Name.ToString()));

            if (grupos_del_usuario == "")
            {
                grupos_del_usuario = agregra_comillas(ReporteRNegocio.grupos_usuario_v_report(User.Identity.Name.ToString()));
            }


            DataTable dt2; DataView dtv = new DataView();

            dt2 = ReporteRNegocio.carga_grupos(txt_desde.Text, txt_hasta.Text, grupos_del_usuario);
            if (dt2.Rows.Count <= 0)
            {
                ScriptManager.RegisterStartupScript(Page, this.GetType(), "teeee", "<script language='javascript'>NO_GRUPOS();</script>", false);
            }
            else
            {
                string es_vendedor = ReporteRNegocio.esvendedor(USER);
                string where = " where FechaFactura >= CONVERT(datetime,'" + desde + "', 103)  and FechaFactura <= CONVERT(datetime,'" + hasta + "',103) ";
                if (grupos != "")
                {
                    where = where + " and user1 in (" + grupos + ") ";
                }
                else if (es_vendedor != "2")
                {
                    where = where + " and user1 in (" + agregra_comillas(ReporteRNegocio.grupos_usuario(USER)) + ")";
                }
                else if (es_vendedor == "2")
                {
                    where = where + " and user1 in (" + agregra_comillas(ReporteRNegocio.grupos_usuario_v_report(USER)) + ")";
                }

                if (clientes != "")
                {
                    where = where + " and rutcliente in (" + clientes + ") ";
                }
                if (vendedores != "")
                {
                    where = where + " and codvendedor in (" + vendedores + ") ";
                }
                if (es_vendedor == "2")
                {
                    where += " and codvendedor in ('" + USER + "')";
                }
                div_report.Visible               = true;
                cont_periodos                    = 0;
                G_INFORME_VENDEDOR.Visible       = false;
                G_INFORME_TOTAL_VENDEDOR.Visible = true;
                string periodos = ReporteRNegocio.listar_periodos_(where);
                //aux = ReporteRNegocio.listar_resumen_periodo(where + " and periodo in ("+agregra_comillas(periodos)+")");
                totales = new DataTable();

                List <string> periodos_list = periodos.Split(',').ToList();

                totales.Columns.Add("FACTORES/Periodos");
                int colum = periodos_list.Count;

                foreach (string r in periodos_list)
                {
                    totales.Columns.Add(r);
                }
                DataRow       row;
                List <string> nombre_factores = get_factores();

                // CAPI
                //totales.Columns.Add("Total general");

                for (int i = 0; i <= 6; i++)
                {
                    row = totales.NewRow();
                    if (nombre_factores[i].ToString().Contains("Percentil "))
                    {
                        row["FACTORES/Periodos"] = nombre_factores[i] + t_percentil.Text + "%";
                    }
                    else
                    {
                        row["FACTORES/Periodos"] = nombre_factores[i];
                    }
                    totales.Rows.Add(row);

                    // CAPI
                    for (int y = 0; y < colum; y++)
                    {
                        if (i == 0)
                        {
                            if (y == colum)
                            {
                                // CAPI
                                // totales.Rows[i][y + 1] = ReporteRNegocio.Facturación_Mes(agregra_comillas(periodos), where).ToString("N0");
                            }
                            else
                            {
                                string periodo = totales.Columns[y + 1].ColumnName;
                                totales.Rows[i][y + 1] = ReporteRNegocio.Facturación_Mes(periodo, where).ToString("N0");
                            }
                        }
                        if (i == 1)
                        {
                            if (y == colum)
                            {
                                // CAPI
                                //totales.Rows[i][y + 1] = ReporteRNegocio._cltes_con_vta(agregra_comillas(periodos), where).ToString("N0");
                            }
                            else
                            {
                                string periodo = totales.Columns[y + 1].ColumnName;
                                totales.Rows[i][y + 1] = ReporteRNegocio._cltes_con_vta(periodo, where).ToString("N0");
                            }
                        }
                        if (i == 2)
                        {
                            totales.Rows[i][y + 1] = (+(Double.Parse(totales.Rows[0][y + 1].ToString().Replace(".", ""))) / (Double.Parse(totales.Rows[1][y + 1].ToString().Replace(".", "")))).ToString("N0");
                        }
                        if (i == 3)
                        {
                            if (y == colum)
                            {
                                // CAPI
                                //List<int> percen = (ReporteRNegocio.Datos_para_percentil(agregra_comillas(periodos), where)).ToList();
                                //if (percen.Count == 0) { totales.Rows[i][y + 1] = "0"; }
                                //else
                                //{
                                //    Double por_percentil;
                                //    if (t_percentil.Text == "") { por_percentil = 0.5; }
                                //    else
                                //    {
                                //        por_percentil = Math.Round(Double.Parse(t_percentil.Text) / 100, 2);
                                //    }
                                //    totales.Rows[i][y + 1] = Math.Round(Percentile(percen.ToArray(), por_percentil)).ToString("N0");
                                //}
                            }
                            else
                            {
                                string      periodo = totales.Columns[y + 1].ColumnName;
                                List <long> percen  = ReporteRNegocio.Datos_para_percentil(periodo, where);
                                if (percen.Count == 0)
                                {
                                    totales.Rows[i][y + 1] = "0";
                                }
                                else
                                {
                                    Double por_percentil;
                                    if (t_percentil.Text == "")
                                    {
                                        por_percentil = 0.5;
                                    }
                                    else
                                    {
                                        por_percentil = Math.Round(Double.Parse(t_percentil.Text) / 100, 2);
                                    }
                                    totales.Rows[i][y + 1] = Math.Round(Percentile(percen.ToArray(), por_percentil)).ToString("N0");
                                }
                            }
                        }

                        if (i == 4)
                        {
                            if (y == colum)
                            {
                                // CAPI
                                //totales.Rows[i][y + 1] = ReporteRNegocio.sum_sobre_este_percentil(agregra_comillas(periodos), where, Double.Parse(totales.Rows[3][y + 1].ToString().Replace(".", ""))).ToString("N0");
                            }
                            else
                            {
                                string periodo = totales.Columns[y + 1].ColumnName;
                                totales.Rows[i][y + 1] = ReporteRNegocio.sum_sobre_este_percentil(periodo, where, Double.Parse(totales.Rows[3][y + 1].ToString().Replace(".", ""))).ToString("N0");
                            }
                        }

                        if (i == 5)
                        {
                            Double sumatoria             = (Double.Parse(totales.Rows[0][y + 1].ToString().Replace(".", "")));
                            Double sum_vent_sobre_percen = (Double.Parse(totales.Rows[4][y + 1].ToString().Replace(".", "")));
                            totales.Rows[i][y + 1] = Math.Round((sum_vent_sobre_percen / sumatoria * 100)).ToString() + " %";
                        }

                        if (i == 6)
                        {
                            if (y <= colum - 2)
                            {
                                Double sum_mes          = (Double.Parse(totales.Rows[0][y + 1].ToString().Replace(".", "")));
                                Double sum_mes_anterior = (Double.Parse(totales.Rows[0][y + 2].ToString().Replace(".", "")));
                                totales.Rows[i][y + 1] = (Math.Round((sum_mes * 100 / sum_mes_anterior)) - 100).ToString() + " %";
                            }
                        }
                    }
                }

                G_INFORME_TOTAL_VENDEDOR.DataSource = totales;
                G_INFORME_TOTAL_VENDEDOR.DataBind();

                //VOLVER A CARGAR LOS MULTISELECT

                DataTable dt = new DataTable();

                try
                {
                    dt = ReporteRNegocio.carga_grupos(desde, hasta, grupos_del_usuario);
                    //dt.Rows.Add(new Object[] { "-1", "-- Todos --" });
                    DataView dv2 = dt.DefaultView;
                    dv2.Sort = "user1";
                    dt       = dv2.ToTable();
                    d_grupos_usuario.DataSource     = dt;
                    d_grupos_usuario.DataTextField  = "user1";
                    d_grupos_usuario.DataValueField = "user1";
                    //d_vendedor_.SelectedIndex = -1;
                    d_grupos_usuario.DataBind();


                    foreach (ListItem item in d_grupos_usuario.Items)
                    {
                        if (l_grupos.Text.Contains(item.Value.ToString()))
                        {
                            item.Selected = true;
                        }
                    }
                }
                catch { }

                string where2 = " where FechaFactura >= CONVERT(datetime,'" + desde + "', 103) " +
                                " and FechaFactura <= CONVERT(datetime,'" + hasta + "',103) ";

                if (grupos != "")
                {
                    where2 = where2 + " and user1 in (" + grupos + ") ";
                }
                else if (es_vendedor != "2")
                {
                    where2 = where2 + " and user1 in (" + agregra_comillas(ReporteRNegocio.grupos_usuario(USER)) + ")";
                }
                else if (es_vendedor == "2")
                {
                    where2 = where2 + " and user1 in (" + agregra_comillas(ReporteRNegocio.grupos_usuario_v_report(USER)) + ")";
                }

                if (es_vendedor == "2")
                {
                    where2 += " and codvendedor in ('" + USER + "')";
                }

                try
                {
                    dt = ReporteRNegocio.listar_ALL_vendedores(where2);
                    //dt.Rows.Add(new Object[] { "-1", "-- Todos --" });
                    DataView dv2 = dt.DefaultView;
                    dv2.Sort = "cod_vend";
                    dt       = dv2.ToTable();
                    d_vendedor_.DataSource     = dt;
                    d_vendedor_.DataTextField  = "nom_vend";
                    d_vendedor_.DataValueField = "cod_vend";
                    //d_vendedor_.SelectedIndex = -1;
                    d_vendedor_.DataBind();


                    foreach (ListItem item in d_vendedor_.Items)
                    {
                        if (l_vendedores.Text.Contains(item.Value.ToString()))
                        {
                            item.Selected = true;
                        }
                    }
                }
                catch { }
                where2 = "";
                where2 = " where FechaFactura >= CONVERT(datetime,'" + desde + "', 103) " +
                         " and FechaFactura <= CONVERT(datetime,'" + hasta + "',103) ";

                if (grupos != "")
                {
                    where2 = where2 + " and user1 in (" + grupos + ") ";
                }
                else if (es_vendedor != "2")
                {
                    where2 = where2 + " and user1 in (" + agregra_comillas(ReporteRNegocio.grupos_usuario(USER)) + ")";
                }
                else if (es_vendedor == "2")
                {
                    where2 = where2 + " and user1 in (" + agregra_comillas(ReporteRNegocio.grupos_usuario_v_report(USER)) + ")";
                }
                if (vendedores != "")
                {
                    where2 += " and codvendedor in (" + vendedores + ")";
                }
                if (es_vendedor == "2")
                {
                    where2 += " and codvendedor in ('" + USER + "')";
                }

                try
                {
                    dt = ReporteRNegocio.listar_ALL_cliente2(where2);
                    //dt.Rows.Add(new Object[] { "-1", "-- Todos --" });
                    DataView dv3 = dt.DefaultView;
                    dv3.Sort                 = "nom_cliente";
                    dt                       = dv3.ToTable();
                    d_cliente.DataSource     = dt;
                    d_cliente.DataTextField  = "nom_cliente";
                    d_cliente.DataValueField = "rut_cliente";
                    //d_vendedor_.SelectedIndex = -1;
                    d_cliente.DataBind();


                    foreach (ListItem item in d_cliente.Items)
                    {
                        if (l_clientes.Text.Contains(item.Value.ToString()))
                        {
                            item.Selected = true;
                        }
                    }
                }
                catch { }
            }
            ScriptManager.RegisterStartupScript(Page, this.GetType(), "teeee", "<script> new Tablesort(document.getElementById('ContentPlaceHolder_Contenido_G_PRODUCTOS')); </script>", false);
        }