using System; using System.IO; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using GemBox.Spreadsheet; using Extensiones; using ADSNetLibrary.SIGAPI.Services.CRM; using ADSNetLibrary.SIGAPI.Models.CRM.Request; using ADSNetLibrary.SIGAPI.Models.CRM.Response; using Microsoft.Extensions.Configuration; using MoreLinq; public partial class CRM_CatEmpresasCRM : ClsPaginas { #region "PROPIEDADES" private DataTable TmpDT { get { return (DataTable)ViewState["TmpDT"]; } set { ViewState["TmpDT"] = value; } } #endregion protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { CargaTipoProcesos(); CargaMediosCaptacion(); CargaDatos(); } } #region "CARGA DATOS" protected void BtnConsultar_Click(object sender, EventArgs e) { CargaDatos(); } protected void CargaDatos() { //ClsCRM ObjCRM = new ClsCRM(); ICRMService cRMService = new CRMService(System.Configuration.ConfigurationManager.ConnectionStrings["Conexion"].ConnectionString); RequestEmpresasCRM parametros = new RequestEmpresasCRM(UsuarioActual.CveEmpresa, -1, CveMedioCaptacion: DdlMedioCaptacion.SelectedValue.ToEntero()); var objServicio = cRMService.GetEmpresasCRM(parametros); if (objServicio.Result.Count > 0) { GVDatos.DataSource = objServicio.Result; TmpDT = objServicio.Result.ToDataTable(); } //if (ObjCRM.GetEmpresasCRM(UsuarioActual.CveEmpresa, CveMedioCaptacion: DdlMedioCaptacion.SelectedValue.ToEntero()) && ObjCRM.DtResultados.Rows.Count > 0) //{ // TmpDT = ObjCRM.DtResultados; // GVDatos.DataSource = TmpDT; //} //ObjCRM.Dispose(); GVDatos.DataBind(); UPDatos.Update(); } protected void CargaMediosCaptacion() { ICRMService cRMService = new CRMService(System.Configuration.ConfigurationManager.ConnectionStrings["Conexion"].ConnectionString); RequestMedioCaptacion MedioCaptacion = new RequestMedioCaptacion(UsuarioActual.CveEmpresa, 1); var ObjMedioCaptacion = cRMService.GetMedioCaptacionCRM(MedioCaptacion); if (ObjMedioCaptacion.Result.Count > 0) { DataTable DTMedioCaptacion = ObjMedioCaptacion.Result.ToDataTable(); DdlMedioCaptacion.DataSource = DTMedioCaptacion.FiltraParametros(String.Format("CveTipoProceso = {0}", DdlTipoProceso.SelectedValue)); DdlMedioCaptacion.DataBind(); DdlMedioCaptacion.Items.Insert(0, new ListItem("Seleccione ", "-1")); } //if (ObjCRM.GetMedioCaptacion(UsuarioActual.CveEmpresa, 1)) //{ // DdlMedioCaptacion.DataSource = ObjCRM.DtResultados.FiltraParametros(String.Format("CveTipoProceso = {0}",DdlTipoProceso.SelectedValue)); // DdlMedioCaptacion.DataBind(); // DdlMedioCaptacion.Items.Insert(0, new ListItem("Todos", "-1")); //} //ObjCRM.Dispose(); } protected void CargaTipoProcesos() { ICRMService cRMService = new CRMService(System.Configuration.ConfigurationManager.ConnectionStrings["Conexion"].ConnectionString); RequestTipoProceso tipoProceso = new RequestTipoProceso(UsuarioActual.CveEmpresa); var ObjTipoProceso = cRMService.GetTipoProcesoCRM(tipoProceso); if (ObjTipoProceso.Result.Count > 0) { DdlTipoProceso.DataSource = ObjTipoProceso.Result; DdlTipoProceso.DataBind(); } } #endregion #region "ACCIONES - GRIEDVIEW" protected void GVDatos_RowDeleting(object sender, GridViewDeleteEventArgs e) { int CveEmpresaCRM = GVDatos.Rows[e.RowIndex].DataKey("CveEmpresaCRM").ToEntero(); //Boolean HuboError = true; ICRMService cRMService = new CRMService(System.Configuration.ConfigurationManager.ConnectionStrings["Conexion"].ConnectionString); RequestContactoEmpresaCRM ContactoEmpresaCRM = new RequestContactoEmpresaCRM(CveEmpresaCRM); RequestEmpresasCRM EmpresasCRM = new RequestEmpresasCRM(-1, CveEmpresaCRM); RequestAcercamiento aceramientoGet = new RequestAcercamiento(UsuarioActual.CveEmpresa, CveEmpresaCRM: CveEmpresaCRM); var objAcercamiento = cRMService.GetAceracamientoCRM(aceramientoGet); if (objAcercamiento.Result.Count == 0) { if (cRMService.EmpresasCRMContactoDelete(ContactoEmpresaCRM)) { if (cRMService.EmpresasCRMDelete(EmpresasCRM)) { //HuboError = false; Alert("La Empresa ha sido eliminado.", ModExtensiones.TipoMensaje.MsgInformacion); } } } else { Alert("No se puede eliminar la empresa porque el prospecto cuenta con cotizaciones/facturaciones previa", ModExtensiones.TipoMensaje.MsgAdvertencia); } CargaDatos(); } protected void GVDatos_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.FCHyperLink("HplEmpresa").NavigateUrl = String.Format("/CRM/EmpresaCRM.aspx?CveEmpresaCRM={0}", e.Row.DataKey("CveEmpresaCRM")) ; if (e.Row.DataKey("NoContactos").ToEntero() > 0) { e.Row.FCLabel("lblNoContactos").Text = String.Format("{0}", e.Row.DataKey("NoContactos")); } } } #endregion protected void DdlTipoProcesos_Change(Object sender, EventArgs e) { DdlMedioCaptacion.Items.Clear(); CargaMediosCaptacion(); UPFiltros.Update(); } protected void LnkCargaProspectos_Click(object sender, EventArgs e) { RegistraScript(String.Format("$('#{0}').modal();", "DivCargasMasivas")); } protected void BtnDescargar_Click(object sender, EventArgs e) { string Directorio = ModExtensiones.DirectorioBaseEmpresas + "Empresas\\Grales\\Formatos\\FormatoDeProspectos.xlsx"; FileInfo Archivo = new FileInfo(Directorio); Response.Clear(); Response.ClearHeaders(); Response.ClearContent(); Response.AddHeader("Content-Disposition", "attachment; filename=" + Archivo.Name); Response.AddHeader("Content-Length", Archivo.Length.ToString()); Response.ContentType = "text/plain"; Response.Flush(); Response.TransmitFile(Archivo.FullName); Response.End(); } protected void BtnAgregarProspectos_Click(object sender, EventArgs e) { if( TxtExcel.getfiles().Archivos.Count > 0 ) { Archivo file = TxtExcel.getfiles().Archivos[0]; string Directorio = Server.MapPath("~") + file.path; DataTable dt = ModExtensiones.ReadExcelFile(Directorio + file.filename, true); DataRowCollection Rows = dt.Rows; ICRMService cRMService = new CRMService(System.Configuration.ConfigurationManager.ConnectionStrings["Conexion"].ConnectionString); RequestMedioCaptacion RequestMedioCaptacion = new RequestMedioCaptacion(UsuarioActual.CveEmpresa); var ObjMedioCaptacion = cRMService.GetMedioCaptacionCRM(RequestMedioCaptacion); EmpresasCRMModel cRMModel = new EmpresasCRMModel(); bool TieneError = false; foreach (DataRow row in Rows) { string Proceso = row["Proceso"].ToString(); string NombreEmpresa = row["Nombre de la empresa"].ToString(); string RFC = row["RFC"].ToString(); string Mail = row["Mail"].ToString(); string MedioCaptacion = row["Medio de Captación"].ToString(); string Agente = row["Agente"].ToString(); string Sucursal = row["Sucursal"].ToString(); string Giro = row["Giro"].ToString(); string ContactoPrincipal = row["Contacto Principal"].ToString(); string TelOficina = row["Teléfono de Oficina"].ToString(); string Celular = row["Celular"].ToString(); string Correo = row["Correo"].ToString(); int CveMedioCaptacion = -1; if (MedioCaptacion != "" || Agente != "") { if (ObjMedioCaptacion.Result.Count > 0) { DataTable tempMedio = ObjMedioCaptacion.Result.ToDataTable(); DataRow MediosCaptacion = tempMedio.FiltroPrimero(String.Format("MedioCaptacion = '{0}'",MedioCaptacion)); if(MediosCaptacion != null) { CveMedioCaptacion = MediosCaptacion["CveMedioCaptacion"].ToString().ToEntero(); }else { Alert(String.Format("El medio de captación '{0}' no fue encontrado", MedioCaptacion), ModExtensiones.TipoMensaje.MsgAdvertencia); TieneError = true; break; } } int CveVendedor = -1; DataRow Vendedor = get_GlobalDTVendedores(UsuarioActual.CveEmpresa).FiltroPrimero(String.Format("Vendedor = '{0}'",Agente)); if (Vendedor != null) { CveVendedor = Vendedor["CveVendedor"].ToString().ToEntero(); }else { Alert(String.Format("El vendedor '{0}' no fue encontrado", Agente), ModExtensiones.TipoMensaje.MsgAdvertencia); TieneError = true; break; } int CveSubProyecto = -1; DataRow SubProyecto = get_GlobalDTSubProyectos(-1,CveEmpresa:UsuarioActual.CveEmpresa).FiltroPrimero(String.Format("SubProyecto = '{0}'",Sucursal)); if(SubProyecto != null) { CveSubProyecto = SubProyecto["CveSubProyecto"].ToString().ToEntero(); }else { Alert(String.Format("El SubProyecto '{0}'", Sucursal), ModExtensiones.TipoMensaje.MsgAdvertencia); TieneError = true; break; } int CveGiro = -1; DataRow GiroDt = get_GlobalDTClienteClasificaciones(UsuarioActual.CveEmpresa).FiltroPrimero(String.Format("ClienteClasificacion = '{0}'", Giro)); if (GiroDt != null) { CveGiro = GiroDt["CveClienteClasificacion"].ToString().ToEntero(); } else { Alert(String.Format("El Giro de Empresa '{0}', No está especificado", Giro), ModExtensiones.TipoMensaje.MsgAdvertencia); TieneError = true; break; } cRMModel.Empresa = NombreEmpresa; cRMModel.RFC = RFC; cRMModel.CveMedioCaptacion = CveMedioCaptacion; cRMModel.CveEmpresa = UsuarioActual.CveEmpresa; cRMModel.CveUsuarioIng = UsuarioActual.CveUsuario; cRMModel.CveEncuestaAplicada = -1; cRMModel.CveVendedorAsignado = CveVendedor; cRMModel.CveSubProyecto = CveSubProyecto; cRMModel.Correo = Mail; cRMModel.CveGiroClasificacion = CveGiro; long CveEmpresaCRM = cRMService.EmpresasCRMAdd(cRMModel).ToEntero(); TieneError = CveEmpresaCRM < 0; } } if (TieneError) { }else { Alert("Prospectos agregados correctamente"); TxtExcel.Text = ""; RegistraScript(String.Format("$('#{0}').modal('hide');", "DivCargasMasivas")); } } } //private DataTable ReadExcelFile(string flatFilePath, bool firstRowHasHeaders) //{ // SpreadsheetInfo.SetLicense(ModExtensiones.SerialGemBoxSpreadsheet()); // ExcelFile excelFile = ExcelFile.Load(flatFilePath); // int unnamed = 0; // int cols; // string[] columns; // int curRow = 0; // int curCol = 0; // DataTable dataTable = new DataTable(); // ExcelWorksheet worksheet = excelFile.Worksheets[0]; // for (cols = 0; cols <= worksheet.Rows[0].AllocatedCells.Count - 1; cols++) // { // if (firstRowHasHeaders) // { // if (worksheet.Rows[0].Cells[cols].Value != null) // dataTable.Columns.Add(worksheet.Rows[0].Cells[cols].Value.ToString().Trim()); // else // dataTable.Columns.Add("Unnamed Column " + (System.Threading.Interlocked.Increment(ref unnamed))); // curRow = 1; // } // else // dataTable.Columns.Add("Column " + (cols + 1)); // } // while (curRow < worksheet.Rows.Count) // { // columns = new string[cols - 1 + 1]; // for (curCol = 0; curCol <= cols - 1; curCol++) // { // if (worksheet.Rows[curRow].Cells[curCol].Value == null) // columns[curCol] = ""; // else // columns[curCol] = worksheet.Rows[curRow].Cells[curCol].Value.ToString(); // } // dataTable.Rows.Add(columns); // curRow += 1; // } // return dataTable; //} }