public void SendEmailSolicitante(client cliente)
        {
            var configurations = (from c in sql.configurations
                                  select c).First();

            if ((Boolean)configurations.smtp_enabled)
            {
                if (cliente.status.Contains("FINISHED") || cliente.status.Contains("DESAPPROVED"))
                {
                    try
                    {
                        MailMessage Email = new MailMessage();

                        Email.To.Add(cliente.solicitante);
                        HttpContext _context = System.Web.HttpContext.Current;
                        String body = getHtmlFromTemplate(_context.Server.MapPath("/") + "/Views/templates/template_solicitante.htm");
                        body = body.Replace("{CLIENT_NAME}", cliente.razon_social);

                        if (cliente.status.Contains("DESAPPROVED"))
                        {
                            body = body.Replace("{CLIENT_STATUS}", "fu&eacute; rechazada.<br/>Observaci&oacute;: " + cliente.observacion_desapproved);
                            Email.Subject = "Clientes Abbottlab - ALTA RECHAZADA";
                        }
                        else
                        {
                            body = body.Replace("{CLIENT_STATUS}", "fu&eacute; aprovada rechazada.");
                            Email.Subject = "Clientes Abbottlab - ALTA SATISFACTORIA FINALIZADA";
                        }

                        Email.Body = body;
                        Email.From = new MailAddress(configurations.smtp_from);
                        Email.IsBodyHtml = true;

                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = configurations.smtp_host;
                        smtp.Send(Email);
                    }
                    catch (Exception ex)
                    {
                        ReportError(ex.ToString());
                    }
                }
            }
        }
		private void detach_clients(client entity)
		{
			this.SendPropertyChanging();
			entity.term = null;
		}
        public void SendEmail(String role, String division, String fileTemplate, String subject, client cliente)
        {
            var configurations = (from c in sql.configurations
                                  select c).First();

            if ((Boolean)configurations.smtp_enabled)
            {
                try
                {
                    IQueryable<user> users;
                    if (role != "DISTRIBUCION" && role != "REGULATORIOS")
                    {
                        users = (from u in sql.users
                                 where u.role == role
                                 select u);
                    }
                    else
                    {
                        users = (from u in sql.users
                                 where u.role == role && (u.division.Contains(division) || u.division == null)
                                 select u);
                    }

                    MailMessage Email = new MailMessage();

                    if (users.Count() > 0)
                    {
                        foreach (user userEmail in users)
                        {
                            if (!String.IsNullOrEmpty(userEmail.email))
                            {
                                Email.To.Add(userEmail.email);
                            }
                        }

                        HttpContext _context = System.Web.HttpContext.Current;
                        String body = getHtmlFromTemplate(_context.Server.MapPath("/") + "/Views/templates/template_" + fileTemplate + ".htm");
                        body = body.Replace("{CLIENT_NAME}", cliente.razon_social);

                        if (cliente.status.Contains("DESAPPROVED"))
                        {
                            body = body.Replace("{CLIENT_STATUS}", "rechazado");
                        }
                        else if (cliente.status.Contains("APPROVED"))
                        {
                            body = body.Replace("{CLIENT_STATUS}", "aprobado");
                        }

                        Email.Subject = subject;
                        Email.Body = body;
                        Email.From = new MailAddress(configurations.smtp_from);
                        Email.IsBodyHtml = true;

                        SmtpClient smtp = new SmtpClient();
                        smtp.Host = configurations.smtp_host;
                        //smtp.Port =  Convert.ToInt32(configurations.smtp_port);
                        smtp.Send(Email);
                    }
                }
                catch(Exception ex)
                {
                    ReportError(ex.ToString());
                }
            }
        }
 partial void Deleteclient(client instance);
		private void attach_clients(client entity)
		{
			this.SendPropertyChanging();
			entity.term = this;
		}
 partial void Insertclient(client instance);
 partial void Updateclient(client instance);
		private void attach_clients(client entity)
		{
			this.SendPropertyChanging();
			entity.division1 = this;
		}
		private void detach_clients(client entity)
		{
			this.SendPropertyChanging();
			entity.warehouse1 = null;
		}
		private void attach_clients(client entity)
		{
			this.SendPropertyChanging();
			entity.warehouse1 = this;
		}
        public ActionResult Edit(client clientEdited)
        {
            user userSession = (user)Session["user"];

            if (Request.Files["documentacion_impositiva"] != null)
            {
                HttpPostedFileBase file = Request.Files["documentacion_impositiva"];
                if (file.ContentLength > 0)
                {
                    string fileName = DateTime.Now.Ticks.ToString() + "_" + Path.GetFileName(file.FileName);
                    string filePath = Path.Combine(HttpContext.Server.MapPath("/Uploads"), fileName);
                    file.SaveAs(filePath);
                    clientEdited.documentacion_impositiva = fileName;
                }
            }

            if (!(clientEdited.id > 0))
            {
                if (Request.Form["client_type"] == "entity")
                {
                    clientEdited.client_parent = null;
                }
                else
                {
                    clientEdited.client_parent = Convert.ToInt64(Request.Form["clientParent"]);
                }

                sql.clients.InsertOnSubmit(clientEdited);
            }
            else
            {
                var clientEdit = (from c in sql.clients
                                  where c.id == Convert.ToInt64(Request.Form["id"])
                                  select c).First();

                clientEdit.limite_credito = clientEdited.limite_credito;
                clientEdit.moneda = clientEdited.moneda;
                clientEdit.calle_nro = clientEdited.calle_nro;
                clientEdit.codigo_abc = clientEdited.codigo_abc;
                clientEdit.codigo_comision = clientEdited.codigo_comision;
                clientEdit.codigo_descuento = clientEdited.codigo_descuento;
                clientEdit.codigo_grupo1 = clientEdited.codigo_grupo1;
                clientEdit.codigo_grupo2 = clientEdited.codigo_grupo2;
                clientEdit.codigo_postal = clientEdited.codigo_postal;
                clientEdit.codigo_region = clientEdited.codigo_region;
                clientEdit.codigo_sucursal = clientEdited.codigo_sucursal;
                clientEdit.codigo_venta = clientEdited.codigo_venta;
                clientEdit.compania = clientEdited.compania;
                clientEdit.condicion_venta = clientEdited.condicion_venta;
                clientEdit.dias_credito = clientEdited.dias_credito;
                clientEdit.division = clientEdited.division;
                clientEdit.email_2_cobranzas = clientEdited.email_2_cobranzas;
                clientEdit.email_cobranzas = clientEdited.email_cobranzas;
                clientEdit.email_otro = clientEdited.email_otro;
                clientEdit.empresa_cobranza = clientEdited.empresa_cobranza;
                clientEdit.estado_cuenta = clientEdited.estado_cuenta;
                clientEdit.exento_impuestos = clientEdited.exento_impuestos;
                clientEdit.forma_pago = clientEdited.forma_pago;
                clientEdit.nro_consolidador = clientEdited.nro_consolidador;
                clientEdit.nro_cuit = clientEdited.nro_cuit;
                clientEdit.nro_iibb = clientEdited.nro_iibb;
                clientEdit.observacion_razon_social = clientEdited.observacion_razon_social;
                clientEdit.politica_falta_producto = clientEdited.politica_falta_producto;
                clientEdit.provincia = clientEdited.provincia;
                clientEdit.razon_social = clientEdited.razon_social;
                clientEdit.responsable_cuenta = clientEdited.responsable_cuenta;
                clientEdit.tax_code = clientEdited.tax_code;
                clientEdit.telefono = clientEdited.telefono;
                clientEdit.terms_code = clientEdited.terms_code;
                clientEdit.via_despacho = clientEdited.via_despacho;
                clientEdit.warehouse = clientEdited.warehouse;
                clientEdit.zona_reparto = clientEdited.zona_reparto;
                clientEdit.solicitante = clientEdited.solicitante;
                clientEdit.contacto_comercial = clientEdited.contacto_comercial;
                clientEdit.participe = clientEdited.participe;
                clientEdit.codigo_gln = clientEdited.codigo_gln;
                clientEdit.pais = clientEdited.pais;
                clientEdit.client_type = (String.IsNullOrEmpty(Request["tipo_cliente"]) ? null : (from ct in sql.client_types where ct.type == Convert.ToInt16(Request["tipo_cliente"]) select ct).Single());

                if (!String.IsNullOrEmpty(clientEdited.documentacion_impositiva))
                {
                    clientEdit.documentacion_impositiva = clientEdited.documentacion_impositiva;
                }

                switch (userSession.role)
                {
                    case "COMERCIAL":
                        clientEdit.observaciones_comercial = clientEdited.observaciones_comercial;
                        break;
                    case "DISTRIBUCION":
                        clientEdit.observaciones_distribucion = clientEdited.observaciones_distribucion;
                        break;
                    case "REGULATORIOS":
                        clientEdit.observaciones_regulatorios = clientEdited.observaciones_regulatorios;
                        break;
                    case "IMPUESTOS":
                        clientEdit.observaciones_impuestos = clientEdited.observaciones_impuestos;
                        break;
                    case "BEX":
                        clientEdit.observaciones_bex = clientEdited.observaciones_bex;
                        break;
                }
            }

            sql.SubmitChanges();

            return Approve(clientEdited.id.ToString());
        }
        public String getHtmlInfo(client clientData, String status)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<tr><td colspan='6' style='border-bottom:1px solid #999;'>&nbsp;</td></tr>");
            sb.Append("<tr><td colspan='6'><strong>DATOS DEL CLIENTE:</strong></td></tr>");
            sb.Append("<tr><td colspan='6'>&nbsp;</td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Razón Social:</strong></td><td colspan='5' style='text-align:left;'>" + clientData.razon_social + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Observaciones razón social:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.observacion_razon_social) ? "-" : clientData.observacion_razon_social) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Calle y nro:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.calle_nro) ? "-" : clientData.calle_nro.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Provincia/País:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.provincia) ? "-" : clientData.provincia.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Código postal:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.codigo_postal) ? "-" : clientData.codigo_postal.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Código país:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.pais) ? "-" : clientData.pais.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Código provincia/sucursal:</strong></td><td colspan='5' style='text-align:left;'>" + clientData.codigo_sucursal + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Teléfono:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.telefono) ? "-" : clientData.telefono.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Email ventas:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.email_cobranzas) ? "-" : clientData.email_cobranzas) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Email 2 cobranzas:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.email_2_cobranzas) ? "-" : clientData.email_2_cobranzas) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Email 3 otros:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.email_otro) ? "-" : clientData.email_otro) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Código ABC:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.codigo_abc) ? "-" : clientData.codigo_abc.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Días crédito:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.dias_credito) ? "-" : clientData.dias_credito.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Estado de cuenta:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.estado_cuenta) ? "-" : clientData.estado_cuenta.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Límite de crédito:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.limite_credito) ? "-" : clientData.limite_credito.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Compañia:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.compania) ? "-" : clientData.compania.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Moneda:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.moneda) ? "-" : clientData.moneda.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Tipo de cliente:</strong></td><td colspan='5' style='text-align:left;'>" + clientData.client_type.description + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Nro. consolidador:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.nro_consolidador) ? "-" : clientData.nro_consolidador.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Código comisión:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.codigo_comision) ? "-" : clientData.codigo_comision.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Warehouse:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.warehouse) ? "-" : clientData.warehouse1.description.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Polit. falta de prod.:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.politica_falta_producto) ? "-" : clientData.politica_falta_producto.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Responsable de cuenta:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.responsable_cuenta) ? "-" : clientData.responsable_cuenta.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Código descuento:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.codigo_descuento) ? "-" : clientData.codigo_descuento.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Condición de venta:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.condicion_venta) ? "-" : clientData.condicion_venta.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>División:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.division) ? "-" : clientData.division.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Forma de pago:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.forma_pago) ? "-" : clientData.forma_pago.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Código región:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.codigo_region) ? "-" : clientData.codigo_region.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Código grupo 1:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.codigo_grupo1) ? "-" : clientData.codigo_grupo1.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Código grupo 2:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.codigo_grupo2) ? "-" : clientData.codigo_grupo2.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Terms code:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.terms_code) ? "-" : clientData.term.description.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Empresa de cobranza:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.empresa_cobranza) ? "-" : clientData.empresa_cobranza.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Nro. de CUIT:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.nro_cuit) ? "-" : clientData.nro_cuit.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Tax code:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.tax_code) ? "-" : clientData.tax_code.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Exento de impuestos:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.exento_impuestos) ? "-" : clientData.exento_impuestos.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Nro. IIBB:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.nro_iibb) ? "-" : clientData.nro_iibb.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Zona de reparto:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.zona_reparto) ? "-" : clientData.zona_reparto.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Via despacho:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.via_despacho) ? "-" : clientData.via_despacho.ToUpper()) + "<br/></td></tr>");
            sb.Append("<tr><td style='text-align:left;'><strong>Código GLN:</strong></td><td colspan='5' style='text-align:left;'>" + (String.IsNullOrEmpty(clientData.codigo_gln) ? "-" : clientData.codigo_gln.ToUpper()) + "<br/></td></tr>");

            if (!String.IsNullOrEmpty(clientData.observaciones_comercial))
            {
                sb.Append("<tr><td style='text-align:left;'><strong>Observaciones Créditos:</strong></td><td colspan='5' style='text-align:left;'>" + clientData.observaciones_comercial + "<br/></td></tr>");
            }

            if (!String.IsNullOrEmpty(clientData.observaciones_regulatorios))
            {
                sb.Append("<tr><td style='text-align:left;'><strong>Observaciones Regulatorios:</strong></td><td colspan='5' style='text-align:left;'>" + clientData.observaciones_regulatorios + "<br/></td></tr>");
            }

            if (!String.IsNullOrEmpty(clientData.observaciones_impuestos))
            {
                sb.Append("<tr><td style='text-align:left;'><strong>Observaciones Impuestos:</strong></td><td colspan='5' style='text-align:left;'>" + clientData.observaciones_impuestos + "<br/></td></tr>");
            }

            if (!String.IsNullOrEmpty(clientData.observaciones_distribucion))
            {
                sb.Append("<tr><td style='text-align:left;'><strong>Observaciones Créditos:</strong></td><td colspan='5' style='text-align:left;'>" + clientData.observaciones_distribucion + "<br/></td></tr>");
            }

            if (!String.IsNullOrEmpty(clientData.observaciones_bex))
            {
                sb.Append("<tr><td style='text-align:left;'><strong>Observaciones Compliance:</strong></td><td colspan='5' style='text-align:left;'>" + clientData.observaciones_bex + "<br/></td></tr>");
            }

            if (!String.IsNullOrEmpty(clientData.observacion_desapproved) && status == "DESAPPROVED")
            {
                sb.Append("<tr><td style='text-align:left;'><strong>Observaciones de desaprovación:</strong></td><td colspan='5' style='text-align:left;'>" + clientData.observacion_desapproved + "<br/></td></tr>");
            }

            sb.Append("<tr><td colspan='6'>&nbsp;</td></tr>");
            sb.Append("<tr><td colspan='6'><strong>PARTICIPANTES:</strong></td></tr>");
            sb.Append("<tr><td colspan='6'>&nbsp;</td></tr>");

            return sb.ToString();
        }