protected void Page_Load(object sender, EventArgs e)
        {
            DBConn oConn = new DBConn();

            if (oConn.Open())
            {
                cLogEventos oLogEventos = new cLogEventos(ref oConn);
                DataTable   dt          = oLogEventos.Get();

                XLWorkbook wb = new XLWorkbook();
                wb.Worksheets.Add(dt, "LogEventos");
                oConn.Close();

                Response.Clear();
                Response.Buffer      = true;
                Response.Charset     = "";
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("content-disposition", "attachment;filename=SqlExport.xlsx");
                using (MemoryStream MyMemoryStream = new MemoryStream())
                {
                    wb.SaveAs(MyMemoryStream);
                    MyMemoryStream.WriteTo(Response.OutputStream);
                    Response.Flush();
                    Response.End();
                }
            }
        }
Exemple #2
0
        protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            DBConn oConn = new DBConn();

            if (oConn.Open())
            {
                cLogEventos oLogEventos = new cLogEventos(ref oConn);
                oLogEventos.NkeyDeudor = hdd_nkey_deudor.Value;
                oLogEventos.NoContrato = txt_no_contrato.Text;
                oLogEventos.CodFlujo   = dropdownflujo.SelectedValue;

                if ((RadDatePicker1.SelectedDate != null) && (RadDatePicker2.SelectedDate != null))
                {
                    oLogEventos.FchDesdeLog = RadDatePicker1.SelectedDate.Value.ToString("yyyyMMdd") + " 00:00:00";
                    oLogEventos.FchHastaLog = RadDatePicker2.SelectedDate.Value.ToString("yyyyMMdd") + " 23:59:59";
                }

                RadGrid1.DataSource = oLogEventos.Get();
            }
            oConn.Close();
        }
        public void ProcessRequest(HttpContext context)
        {
            DBConn oConn = new DBConn();

            if (oConn.Open())
            {
                cLogEventos oLogEventos = new cLogEventos(ref oConn);
                oLogEventos.NkeyDeudor = oWeb.GetData("sNkeyDeudor");
                oLogEventos.NoContrato = oWeb.GetData("sNoContrato");
                oLogEventos.CodFlujo   = oWeb.GetData("sCodFlujo");

                if ((!string.IsNullOrEmpty(oWeb.GetData("sRadDatePicker1"))) && (!string.IsNullOrEmpty(oWeb.GetData("sRadDatePicker2"))))
                {
                    oLogEventos.FchDesdeLog = oWeb.GetData("sRadDatePicker1");
                    oLogEventos.FchHastaLog = oWeb.GetData("sRadDatePicker2");
                }
                DataTable dt = oLogEventos.Get();

                XLWorkbook wb = new XLWorkbook();
                wb.Worksheets.Add(dt, "LogEventos");
                oConn.Close();

                context.Response.Clear();
                context.Response.Buffer      = true;
                context.Response.Charset     = "";
                context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                context.Response.AddHeader("content-disposition", "attachment;filename=LogEventos.xlsx");
                using (MemoryStream MyMemoryStream = new MemoryStream())
                {
                    wb.SaveAs(MyMemoryStream);
                    MyMemoryStream.WriteTo(context.Response.OutputStream);
                    context.Response.Flush();
                    context.Response.End();
                }
            }
        }
        protected void btnAceptar_Click(object sender, EventArgs e)
        {
            //Session["bLicenciatario"] = string.Empty;
            string sRutEmpresa = txt_box_empresa.Text;
            string sLogin      = txt_rut.Text;

            if (Page.IsValid)
            {
                try
                {
                    bool   bExito = false;
                    string sError = string.Empty;
                    string sPwd   = oWeb.Crypt(txt_password.Text);

                    if (sRutEmpresa.IndexOf("-") >= 0)
                    {
                        string sRut = sRutEmpresa.Substring(0, sRutEmpresa.IndexOf("-"));
                        string sDv  = sRutEmpresa.Substring(sRutEmpresa.IndexOf("-") + 1);

                        DBConn oConn = new DBConn();
                        if (oConn.Open())
                        {
                            cSysUsuario oSysUsuario = new cSysUsuario(ref oConn);
                            oSysUsuario.LoginUser = sLogin;
                            oSysUsuario.PwdUser   = sPwd;
                            DataTable dtUser = oSysUsuario.Get();
                            if (dtUser != null)
                            {
                                if (dtUser.Rows.Count > 0)
                                {
                                    oUsuario                  = oWeb.GetObjUsuario();
                                    oUsuario.CodUsuario       = dtUser.Rows[0]["cod_user"].ToString();
                                    oUsuario.Nombres          = (dtUser.Rows[0]["nom_user"].ToString() + " " + dtUser.Rows[0]["ape_user"].ToString()).Trim();
                                    oUsuario.Pais             = cmbox_pais.SelectedValue;
                                    oUsuario.Email            = dtUser.Rows[0]["eml_user"].ToString();
                                    oUsuario.CodTipoUsuario   = dtUser.Rows[0]["cod_tipo"].ToString();
                                    oUsuario.RutLicenciatario = sRutEmpresa;
                                    oUsuario.RutUsuario       = sLogin;


                                    cSysPerfilesUsuarios oSysPerfilesUsuarios = new cSysPerfilesUsuarios(ref oConn);
                                    oSysPerfilesUsuarios.CodUser = dtUser.Rows[0]["cod_user"].ToString();
                                    DataTable dtUserPerfil = oSysPerfilesUsuarios.Get();
                                    if (dtUserPerfil != null)
                                    {
                                        foreach (DataRow oRow in dtUserPerfil.Rows)
                                        {
                                            if (oRow["cod_perfil"].ToString() == "4")
                                            {
                                                cSysUserCliente oSysUserCliente = new cSysUserCliente(ref oConn);
                                                oSysUserCliente.CodUser = dtUser.Rows[0]["cod_user"].ToString();
                                                oSysUserCliente.Rut     = sRut;
                                                oSysUserCliente.Dv      = sDv;
                                                DataTable dtUserCliente = oSysUserCliente.GetByLogin();
                                                if (dtUserCliente != null)
                                                {
                                                    if (dtUserCliente.Rows.Count > 0)
                                                    {
                                                        bExito = true;
                                                        oUsuario.NKeyUsuario = dtUserCliente.Rows[0]["nkey_cliente"].ToString();
                                                        oUsuario.VistaMenu   = dtUserCliente.Rows[0]["tipo_cliente"].ToString();
                                                        oUsuario.Tipo        = string.Empty;
                                                    }
                                                }
                                                dtUserCliente = null;
                                            }
                                            else if (oRow["cod_perfil"].ToString() == "5")
                                            {
                                                cSysUserDeudor oSysUserDeudor = new cSysUserDeudor(ref oConn);
                                                oSysUserDeudor.CodUser = dtUser.Rows[0]["cod_user"].ToString();
                                                oSysUserDeudor.Rut     = sRut;
                                                oSysUserDeudor.Dv      = sDv;
                                                DataTable dtUserDeudor = oSysUserDeudor.GetByLogin();
                                                if (dtUserDeudor != null)
                                                {
                                                    if (dtUserDeudor.Rows.Count > 0)
                                                    {
                                                        bExito = true;
                                                        oUsuario.NKeyUsuario   = dtUserDeudor.Rows[0]["nkey_deudor"].ToString();
                                                        oUsuario.NKeyDeudor    = dtUserDeudor.Rows[0]["nkey_deudor"].ToString();
                                                        oUsuario.Licenciatario = dtUserDeudor.Rows[0]["nombre_deudor"].ToString();
                                                        oUsuario.VistaMenu     = string.Empty;
                                                        oUsuario.Tipo          = "1";
                                                        //Session["bLicenciatario"] = 1;
                                                    }
                                                }
                                                dtUserDeudor = null;
                                            }
                                        }
                                    }
                                    dtUserPerfil = null;
                                }
                                else
                                {
                                    sError = "Login o Password incorrecto.";
                                }
                            }
                            dtUser = null;
                        }
                        oConn.Close();
                    }
                    if (bExito)
                    {
                        Session["USUARIO"] = oUsuario;

                        DBConn oConn = new DBConn();
                        if (oConn.Open())
                        {
                            cLogEventos oLogEventos = new cLogEventos(ref oConn);
                            oLogEventos.AccionLog  = "LOGIN";
                            oLogEventos.CodCanal   = "2";
                            oLogEventos.CodFlujo   = "1";
                            oLogEventos.NomFlujo   = "LOGIN";
                            oLogEventos.NkeyDeudor = (!string.IsNullOrEmpty(oUsuario.NKeyDeudor)? oUsuario.NKeyDeudor: string.Empty);
                            oLogEventos.RutDeudor  = (!string.IsNullOrEmpty(oUsuario.RutLicenciatario) ? oUsuario.RutLicenciatario : string.Empty);
                            oLogEventos.NomDeudor  = (!string.IsNullOrEmpty(oUsuario.Licenciatario) ? oUsuario.Licenciatario : string.Empty);
                            oLogEventos.CodUser    = (!string.IsNullOrEmpty(oUsuario.CodUsuario) ? oUsuario.CodUsuario : string.Empty);
                            oLogEventos.RutUser    = (!string.IsNullOrEmpty(oUsuario.RutUsuario) ? oUsuario.RutUsuario : string.Empty);
                            oLogEventos.NomUser    = (!string.IsNullOrEmpty(oUsuario.Nombres) ? oUsuario.Nombres : string.Empty);
                            oLogEventos.ObsLog     = "El usuario se ha logeado con exito.";
                            oLogEventos.IpLog      = (!string.IsNullOrEmpty(oWeb.GetIpUsuario()) ? oWeb.GetIpUsuario() : string.Empty);
                            oLogEventos.Accion     = "CREAR";
                            oLogEventos.Put();

                            if (!string.IsNullOrEmpty(oLogEventos.Error))
                            {
                                Response.Write("error 1:" + oLogEventos.Error);

                                Response.Write("NKeyDeudor  " + oUsuario.NKeyDeudor + "<br>");
                                Response.Write("RutLicenciatario  " + oUsuario.RutLicenciatario + "<br>");
                                Response.Write("Licenciatario  " + oUsuario.Licenciatario + "<br>");
                                Response.Write("CodUsuario  " + oUsuario.CodUsuario + "<br>");
                                Response.Write("RutUsuario  " + oUsuario.RutUsuario + "<br>");
                                Response.Write("Nombres  " + oUsuario.Nombres + "<br>");
                                Response.Write("GetIpUsuario  " + oWeb.GetIpUsuario() + "<br>");

                                Response.End();
                            }
                        }
                        oConn.Close();

                        Response.Redirect("licenciatarios.aspx", false);
                    }
                    else
                    {
                        DBConn oConn = new DBConn();
                        if (oConn.Open())
                        {
                            sError = (!string.IsNullOrEmpty(sError) ? sError : "Usted no tiene los roles necesarios para acceder a este sistema, por favor contacte al administrador del sitio para que le otorgen el acceso.");

                            cLogEventos oLogEventos = new cLogEventos(ref oConn);
                            oLogEventos.AccionLog  = "LOGIN";
                            oLogEventos.CodCanal   = "2";
                            oLogEventos.CodFlujo   = "1";
                            oLogEventos.NomFlujo   = "LOGIN";
                            oLogEventos.NkeyDeudor = oUsuario.NKeyDeudor;
                            oLogEventos.RutDeudor  = oUsuario.RutLicenciatario;
                            oLogEventos.NomDeudor  = oUsuario.Licenciatario;
                            oLogEventos.CodUser    = oUsuario.CodUsuario;
                            oLogEventos.RutUser    = oUsuario.RutUsuario;
                            oLogEventos.NomUser    = oUsuario.Nombres;
                            oLogEventos.ObsLog     = sError;
                            oLogEventos.IpLog      = oWeb.GetIpUsuario();
                            oLogEventos.Accion     = "CREAR";
                            oLogEventos.Put();

                            //if (!string.IsNullOrEmpty(oLogEventos.Error))
                            //{
                            //  Response.Write("error 2:" + oLogEventos.Error);
                            //  Response.Write("NKeyDeudor  " + oUsuario.NKeyDeudor + "<br>");
                            //  Response.Write("RutLicenciatario  " + oUsuario.RutLicenciatario + "<br>");
                            //  Response.Write("Licenciatario  " + oUsuario.Licenciatario + "<br>");
                            //  Response.Write("CodUsuario  " + oUsuario.CodUsuario + "<br>");
                            //  Response.Write("RutUsuario  " + oUsuario.RutUsuario + "<br>");
                            //  Response.Write("Nombres  " + oUsuario.Nombres + "<br>");
                            //  Response.Write("GetIpUsuario  " + oWeb.GetIpUsuario() + "<br>");
                            //  Response.End();
                            //}
                        }
                        oConn.Close();


                        StringBuilder js = new StringBuilder();
                        js.Append("function LgRespuesta() {");
                        js.Append(" window.radalert('" + sError + ".', 400, 200,'Atención'); ");
                        js.Append(" Sys.Application.remove_load(LgRespuesta); ");
                        js.Append("};");
                        js.Append("Sys.Application.add_load(LgRespuesta);");
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "LgRespuesta", js.ToString(), true);
                    }
                }
                catch (Exception Ex) {
                    DBConn oConn = new DBConn();
                    if (oConn.Open())
                    {
                        string sError = "El usuario " + sLogin + ", de la empresa " + sRutEmpresa + ", no se ha podido logear.";

                        cLogEventos oLogEventos = new cLogEventos(ref oConn);
                        oLogEventos.AccionLog   = "LOGIN";
                        oLogEventos.CodCanal    = "2";
                        oLogEventos.CodFlujo    = "1";
                        oLogEventos.NomFlujo    = "LOGIN";
                        oLogEventos.NkeyDeudor  = oUsuario.NKeyDeudor;
                        oLogEventos.RutDeudor   = oUsuario.RutLicenciatario;
                        oLogEventos.NomDeudor   = oUsuario.Licenciatario;
                        oLogEventos.NomUser     = oUsuario.Nombres;
                        oLogEventos.RutUser     = oUsuario.RutUsuario;
                        oLogEventos.ObsLog      = sError;
                        oLogEventos.ObsErrorLog = Ex.Message + " / " + Ex.Source;
                        oLogEventos.IpLog       = oWeb.GetIpUsuario();
                        oLogEventos.Accion      = "CREAR";
                        oLogEventos.Put();

                        //if (!string.IsNullOrEmpty(oLogEventos.Error))
                        //{
                        //  Response.Write("error 3:" + oLogEventos.Error);
                        //  Response.Write("NKeyDeudor  " + oUsuario.NKeyDeudor + "<br>");
                        //    Response.Write("RutLicenciatario  " + oUsuario.RutLicenciatario + "<br>");
                        //    Response.Write("Licenciatario  " + oUsuario.Licenciatario + "<br>");
                        //    Response.Write("CodUsuario  " + oUsuario.CodUsuario + "<br>");
                        //    Response.Write("RutUsuario  " + oUsuario.RutUsuario + "<br>");
                        //    Response.Write("Nombres  " + oUsuario.Nombres + "<br>");
                        //    Response.Write("GetIpUsuario  " + oWeb.GetIpUsuario() + "<br>");
                        //  Response.End();
                        //}

                        StringBuilder js = new StringBuilder();
                        js.Append("function LgRespuesta() {");
                        js.Append(" window.radalert('" + sError + ".', 400, 200,'Atención'); ");
                        js.Append(" Sys.Application.remove_load(LgRespuesta); ");
                        js.Append("};");
                        js.Append("Sys.Application.add_load(LgRespuesta);");
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "LgRespuesta", js.ToString(), true);
                    }
                }
            }
        }
        protected void btnAprobar_Click(object sender, EventArgs e)
        {
            string        sNumInvoce = string.Empty;
            string        sPeriodo   = string.Empty;
            StringBuilder sHtml      = new StringBuilder();

            sHtml.Append(File.ReadAllText(Server.MapPath("invoice.html")));
            StringBuilder sDataProductos      = new StringBuilder();
            StringBuilder sDataValorProductos = new StringBuilder();
            double        dTotalFacturaUsd    = 0;
            bool          bExito = false;

            if (ViewState["dtFactura"] != null)
            {
                arrayMes = ViewState["arrayMes"] as string[];
                DataTable dtFactura = ViewState["dtFactura"] as DataTable;
                if (dtFactura != null)
                {
                    DBConn oConn = new DBConn();
                    if (oConn.Open())
                    {
                        cFactura oFactura = new cFactura(ref oConn);
                        oFactura.NumContrato = hdd_num_contrato.Value;
                        oFactura.Territory   = "CHILE";
                        oFactura.DateInvoce  = DateTime.Now.ToString("yyyy-MM-dd");
                        oFactura.DueDate     = DateTime.Now.AddMonths(1).ToString("yyyy-MM-dd");
                        oFactura.TipoFactura = "Q";
                        oFactura.Accion      = "CREAR";
                        oFactura.Put();

                        if (string.IsNullOrEmpty(oFactura.Error))
                        {
                            bExito = true;
                        }

                        string pCodFactura = oFactura.CodFactura;

                        cDetalleFactura oDetalleFactura = new cDetalleFactura(ref oConn);
                        oDetalleFactura.CodigoFactura = pCodFactura;

                        foreach (DataRow oRow in dtFactura.Rows)
                        {
                            string    sDescripcionMarca = string.Empty;
                            DataTable dtMarca           = getMarca(oRow["Marca"].ToString());
                            if (dtMarca != null)
                            {
                                if (dtMarca.Rows.Count > 0)
                                {
                                    sDescripcionMarca = dtMarca.Rows[0]["descripcion"].ToString();
                                }
                            }
                            dtMarca = null;

                            string pCodCategoria         = null;
                            string sDescripcionCategoria = string.Empty;

                            string pCodSubCategoria         = null;
                            string sDescripcionSubCategoria = string.Empty;

                            if (!string.IsNullOrEmpty(oRow["Categoría"].ToString()))
                            {
                                pCodCategoria = oRow["Categoría"].ToString();
                                DataTable dtCategoria = getCategoria(oRow["Categoría"].ToString());
                                if (dtCategoria != null)
                                {
                                    if (dtCategoria.Rows.Count > 0)
                                    {
                                        sDescripcionCategoria = dtCategoria.Rows[0]["descripcion"].ToString();
                                    }
                                }
                                dtCategoria = null;
                            }
                            if (!string.IsNullOrEmpty(oRow["Subcategoría"].ToString()))
                            {
                                pCodSubCategoria = oRow["Subcategoría"].ToString();
                                DataTable dtSubCategoria = getSubCategoria(oRow["Subcategoría"].ToString());
                                if (dtSubCategoria != null)
                                {
                                    if (dtSubCategoria.Rows.Count > 0)
                                    {
                                        sDescripcionSubCategoria = dtSubCategoria.Rows[0]["descripcion"].ToString();
                                    }
                                }
                                dtSubCategoria = null;
                            }

                            sDataProductos.Append("<div style=\"padding-top:10px;padding-bottom:10px;\">");
                            sDataProductos.Append("<div><font style=\"font-family:Arial; color:#000000; font-size:10pt;font-weight:bold;\">");
                            sDataProductos.Append(sDescripcionMarca);

                            if (!string.IsNullOrEmpty(sDescripcionCategoria))
                            {
                                sDataProductos.Append(" / " + sDescripcionCategoria);
                            }
                            if (!string.IsNullOrEmpty(sDescripcionSubCategoria))
                            {
                                sDataProductos.Append(" / " + sDescripcionSubCategoria);
                            }

                            sDataProductos.Append(" ").Append(String.Format("{0:P1}", double.Parse(oRow["Royalty (%)"].ToString()))).Append("</font></div>");
                            //sDataProductos.Append(" ").Append(oRow["Royalty (%)"].ToString()).Append("</font></div>");
                            sDataProductos.Append("<div><font style=\"font-family:Arial; color:#000000; font-size:10pt;\">Royalties</font></div>");
                            sDataProductos.Append("<div><font style=\"font-family:Arial; color:#000000; font-size:10pt;\">BDI</font></div>");
                            if (double.Parse(oRow["Saldo Advance USD"].ToString()) > 0)
                            {
                                sDataProductos.Append("<div><font style=\"font-family:Arial; color:#000000; font-size:10pt;\">Advance</font></div>");
                            }
                            sDataProductos.Append("</div>");

                            oDetalleFactura.CodMarca        = oRow["Marca"].ToString();
                            oDetalleFactura.CodCategoria    = pCodCategoria;
                            oDetalleFactura.CodSubcategoria = pCodSubCategoria;
                            oDetalleFactura.MesNomUno       = arrayMes[0].ToString();
                            oDetalleFactura.MesMntUno       = oRow[oWeb.getMes(int.Parse(arrayMes[0].ToString())).ToString()].ToString();

                            oDetalleFactura.MesNomDos = arrayMes[1].ToString();
                            oDetalleFactura.MesMntDos = oRow[oWeb.getMes(int.Parse(arrayMes[1].ToString())).ToString()].ToString();

                            oDetalleFactura.MesNomTres = arrayMes[2].ToString();
                            oDetalleFactura.MesMntTres = oRow[oWeb.getMes(int.Parse(arrayMes[2].ToString())).ToString()].ToString();

                            oDetalleFactura.CodRoyalty      = oRow["CodRoyalty"].ToString();
                            oDetalleFactura.Royalty         = oRow["Royalty (%)"].ToString();
                            oDetalleFactura.Bdi             = oRow["BDI (%)"].ToString();
                            oDetalleFactura.Periodo         = oRow["Periodo"].ToString();
                            oDetalleFactura.MontoRoyaltyUsd = double.Parse(oRow["Monto Royalty USD"].ToString()).ToString();
                            oDetalleFactura.MontoBdiUsd     = double.Parse(oRow["Monto BDI USD"].ToString()).ToString();
                            oDetalleFactura.SaldoAdvanceUsd = double.Parse(oRow["Saldo Advance USD"].ToString()).ToString();
                            oDetalleFactura.FacturaUsd      = double.Parse(oRow["Factura USD"].ToString()).ToString();
                            oDetalleFactura.Accion          = "CREAR";
                            oDetalleFactura.Put();

                            if (!string.IsNullOrEmpty(oDetalleFactura.Error))
                            {
                                bExito = false;
                            }

                            cAdvanceContrato oAdvanceContrato = new cAdvanceContrato(ref oConn);
                            oAdvanceContrato.NumContrato     = hdd_num_contrato.Value;
                            oAdvanceContrato.CodMarca        = oRow["Marca"].ToString();
                            oAdvanceContrato.CodCategoria    = pCodCategoria;
                            oAdvanceContrato.CodSubCategoria = pCodSubCategoria;
                            if (double.Parse(oRow["Factura USD"].ToString()) > 0)
                            {
                                oAdvanceContrato.Saldo = oRow["Saldo"].ToString();
                            }
                            oAdvanceContrato.Accion = "EDITAR";
                            oAdvanceContrato.Put();

                            sDataValorProductos.Append("<div style=\"padding-top:10px;padding-bottom:10px; padding-left:10px;\">");
                            sDataValorProductos.Append("<div style=\"float:left;width:30px;\"><font style=\"font-family:Arial; color:#000000; font-size:10pt;\">&nbsp;</font></div>");
                            sDataValorProductos.Append("<div style=\"text-align:right;\"><font style=\"font-family:Arial; color:#000000; font-size:10pt;\">&nbsp;</font></div>");
                            sDataValorProductos.Append("<div style=\"float:left;width:30px;\"><font style=\"font-family:Arial; color:#000000; font-size:10pt;\">$</font></div>");
                            sDataValorProductos.Append("<div style=\"text-align:right;\"><font style=\"font-family:Arial; color:#000000; font-size:10pt;\">").Append(double.Parse(oRow["Monto Royalty USD"].ToString()).ToString("#,##0.00")).Append("</font></div>");
                            sDataValorProductos.Append("<div style=\"float:left;width:30px;\"><font style=\"font-family:Arial; color:#000000; font-size:10pt;\">$</font></div>");
                            sDataValorProductos.Append("<div style=\"text-align:right;\"><font style=\"font-family:Arial; color:#000000; font-size:10pt;\">").Append(double.Parse(oRow["Monto BDI USD"].ToString()).ToString("#,##0.00")).Append("</font></div>");
                            sDataValorProductos.Append("<div style=\"float:left;width:30px;\"><font style=\"font-family:Arial; color:#000000; font-size:10pt;\">&nbsp;</font></div>");
                            if (double.Parse(oRow["Saldo Advance USD"].ToString()) > 0)
                            {
                                sDataValorProductos.Append("<div style=\"text-align:right;\"><font style=\"font-family:Arial; color:#ff0000; font-size:10pt;\">(").Append(double.Parse(oRow["Saldo Advance USD"].ToString()).ToString("#,##0.00")).Append(")</font></div>");
                            }
                            sDataValorProductos.Append("</div>");

                            dTotalFacturaUsd = dTotalFacturaUsd + double.Parse(oRow["Factura USD"].ToString());
                        }

                        string strNomDeudor   = string.Empty;
                        string sNomContacto   = string.Empty;
                        string sEmailContacto = string.Empty;
                        string sDireccion     = string.Empty;
                        string sComuna        = string.Empty;
                        string sCiudad        = string.Empty;

                        cContratos oContrato = new cContratos(ref oConn);
                        oContrato.NumContrato = hdd_num_contrato.Value;
                        DataTable dtContrato = oContrato.Get();
                        if (dtContrato != null)
                        {
                            if (dtContrato.Rows.Count > 0)
                            {
                                cDeudor oDeudor = new cDeudor(ref oConn);
                                oDeudor.NKeyDeudor = dtContrato.Rows[0]["nKey_Deudor"].ToString();
                                DataTable dtDeudor = oDeudor.Get();
                                if (dtDeudor != null)
                                {
                                    if (dtDeudor.Rows.Count > 0)
                                    {
                                        strNomDeudor = dtDeudor.Rows[0]["snombre"].ToString();
                                    }
                                }
                                dtDeudor = null;

                                cContactosDeudor oContactosDeudor = new cContactosDeudor(ref oConn);
                                oContactosDeudor.NKeyDeudor  = dtContrato.Rows[0]["nKey_Deudor"].ToString();
                                oContactosDeudor.NkeyCliente = oUsuario.NKeyUsuario;
                                DataTable dtContacto = oContactosDeudor.Get();
                                if (dtContacto != null)
                                {
                                    if (dtContacto.Rows.Count > 0)
                                    {
                                        sNomContacto   = dtContacto.Rows[0]["sNombre"].ToString();
                                        sEmailContacto = dtContacto.Rows[0]["sEmail"].ToString();
                                    }
                                }
                                dtContacto = null;

                                cDirecciones oDirecciones = new cDirecciones(ref oConn);
                                oDirecciones.NKeyDeudor  = dtContrato.Rows[0]["nKey_Deudor"].ToString();
                                oDirecciones.NkeyCliente = oUsuario.NKeyUsuario;
                                DataTable dtDireccion = oDirecciones.Get();
                                if (dtDireccion != null)
                                {
                                    if (dtDireccion.Rows.Count > 0)
                                    {
                                        sDireccion = dtDireccion.Rows[0]["sDireccion"].ToString();
                                        sComuna    = dtDireccion.Rows[0]["sComuna"].ToString();
                                        sCiudad    = dtDireccion.Rows[0]["sCiudad"].ToString();
                                    }
                                }
                                dtDireccion = null;
                            }
                        }
                        dtContrato = null;

                        //string sNumInvoce = strNomDeudor.Substring(0, 3).ToUpper();
                        //sNumInvoce = sNumInvoce.ToUpper();
                        //string sNumInvoce = "FE" + int.Parse(pCodFactura).ToString("D10");
                        sNumInvoce = "FE" + pCodFactura;

                        oFactura.Total      = dTotalFacturaUsd.ToString();
                        oFactura.Periodo    = hdd_periodo.Value + "/" + hdd_ano_periodo.Value;
                        oFactura.NumInvoice = sNumInvoce;
                        oFactura.Accion     = "EDITAR";
                        oFactura.Put();

                        cReporteVenta cReporteVenta;
                        arrayReporteVenta = ViewState["arrayReporteVenta"] as string[];
                        foreach (string pCodReporte in arrayReporteVenta)
                        {
                            cReporteVenta = new cReporteVenta(ref oConn);
                            cReporteVenta.CodigoReporteVenta = pCodReporte;
                            cReporteVenta.Facturado          = "F";
                            cReporteVenta.CodigoFactura      = pCodFactura;
                            cReporteVenta.Accion             = "EDITAR";
                            cReporteVenta.Put();
                        }

                        if (double.Parse(dTotalFacturaUsd.ToString()) > 0)
                        {
                            sHtml.Replace("[#TITLE]", "INVOICE");
                        }
                        else
                        {
                            sHtml.Replace("[#TITLE]", "CREDIT NOTE");
                        }

                        sHtml.Replace("[#NUMFACTURA]", sNumInvoce);
                        sHtml.Replace("[#NOMEMPRESA]", strNomDeudor);
                        sHtml.Replace("[#NOMCONTACTO]", sNomContacto);
                        sHtml.Replace("[#EMAILCONTACTO]", sEmailContacto);
                        sHtml.Replace("[#DIRECCION]", sDireccion + (!string.IsNullOrEmpty(sComuna) ? ", " + sComuna : string.Empty));
                        sHtml.Replace("[#CIUDAD]", sCiudad);
                        sHtml.Replace("[#PAIS]", "");
                        sHtml.Replace("[#FECHA]", DateTime.Now.ToString("d-MMM-yy", CultureInfo.CreateSpecificCulture("en-US")));
                        sHtml.Replace("[#NUMCONTRATO]", hdd_no_contrato.Value);
                        sHtml.Replace("[#PERIODOQ]", oDetalleFactura.Periodo);
                        sPeriodo = oDetalleFactura.Periodo;
                        sHtml.Replace("[#PROPERTY]", "");
                        sHtml.Replace("[#TERRITORIO]", "CHILE");
                        sHtml.Replace("[#DETALLEPRODUCTOS]", sDataProductos.ToString());
                        sHtml.Replace("[#DETALLEVALORPRODUCTOS]", sDataValorProductos.ToString());
                        sHtml.Replace("[#DUEDATE]", DateTime.Now.AddMonths(1).ToString("d-MMM-yy", CultureInfo.CreateSpecificCulture("en-US")));
                        //sHtml.Replace("[#TOTAL]", double.Parse(dTotalFacturaUsd.ToString()).ToString("#,##0.00"));
                        sHtml.Replace("[#TOTAL]", double.Parse(dTotalFacturaUsd.ToString()).ToString("N0"));

                        if (!Directory.Exists(Server.MapPath("Facturas/") + hdd_no_contrato.Value + "/"))
                        {
                            Directory.CreateDirectory(Server.MapPath("Facturas/") + hdd_no_contrato.Value + "/");
                        }

                        string sFileHtml = Server.MapPath("Facturas/") + hdd_no_contrato.Value + "/" + sNumInvoce + ".html";
                        File.WriteAllText(sFileHtml, sHtml.ToString(), Encoding.UTF8);
                    }
                    oConn.Close();
                }
                dtFactura = null;
            }

            if (bExito)
            {
                //cmbox_contrato.SelectedValue = "0";
                idGrilla.Visible  = false;
                idBtnSave.Visible = false;

                DBConn oConn = new DBConn();
                if (oConn.Open())
                {
                    cLogEventos oLogEventos = new cLogEventos(ref oConn);
                    oLogEventos.AccionLog   = "APROBAR POR DETALLE PRE-FACTURA Q";
                    oLogEventos.CodCanal    = "2";
                    oLogEventos.CodFlujo    = "5";
                    oLogEventos.NomFlujo    = "FACTURACION Q";
                    oLogEventos.NumContrato = hdd_num_contrato.Value;
                    oLogEventos.NoContrato  = hdd_no_contrato.Value;
                    oLogEventos.PeriodoLog  = sPeriodo;
                    //oLogEventos.RutDeudor = oUsuario.RutLicenciatario;
                    oLogEventos.CodUser = oUsuario.CodUsuario;
                    oLogEventos.RutUser = oUsuario.RutUsuario;
                    oLogEventos.NomUser = oUsuario.Nombres;
                    oLogEventos.ObsLog  = "Se ha autorizado correctamente la Factura Q " + sNumInvoce;
                    oLogEventos.IpLog   = oWeb.GetIpUsuario();
                    oLogEventos.Accion  = "CREAR";
                    oLogEventos.Put();
                }
                oConn.Close();

                StringBuilder js = new StringBuilder();
                js.Append("function LgRespuesta() {");
                js.Append(" window.radalert('La factura ha sido emitida con éxito.', 330, 210); ");
                js.Append(" Sys.Application.remove_load(LgRespuesta); ");
                js.Append("};");
                js.Append("Sys.Application.add_load(LgRespuesta);");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", js.ToString(), true);
            }
        }
Exemple #6
0
        protected void btnGuardar_Click(object sender, EventArgs e)
        {
            string FileName        = string.Empty;
            string FilePath        = string.Empty;
            string sNoContrato     = string.Empty;
            string sPeriodo        = ddlmesventa.Items[ddlmesventa.SelectedIndex].Text.ToUpper();
            string pCodComprobante = string.Empty;

            try
            {
                DBConn oConn = new DBConn();
                if (oConn.Open())
                {
                    cContratos oContrato = new cContratos(ref oConn);
                    oContrato.NumContrato = cmbox_contrato.SelectedValue;
                    DataTable tbContrato = oContrato.Get();
                    if (tbContrato != null)
                    {
                        if (tbContrato.Rows.Count > 0)
                        {
                            sNoContrato = tbContrato.Rows[0]["no_contrato"].ToString();
                        }
                    }
                    tbContrato = null;
                }
                oConn.Close();

                if (chkbox_declaracion.Checked)
                {
                    bool bExito = false;
                    if (RadUpload1.UploadedFiles.Count > 0)
                    {
                        oConn = new DBConn();
                        if (oConn.Open())
                        {
                            foreach (UploadedFile ofile in RadUpload1.UploadedFiles)
                            {
                                //string FileName = ofile.GetName();
                                FileName = cmbox_contrato.SelectedValue + "_" + ddlmesventa.Items[ddlmesventa.SelectedIndex].Text.Replace("/", "_") + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
                                //FilePath = FilePath + FileName;
                                //FilePath = Server.MapPath("rps_licenciatariosmattel/") + cmbox_contrato.Items[cmbox_contrato.SelectedIndex].Text + "\\" + FileName;
                                //if (!Directory.Exists(Server.MapPath("rps_licenciatariosmattel/") + cmbox_contrato.Items[cmbox_contrato.SelectedIndex].Text + "\\"))
                                //  Directory.CreateDirectory(Server.MapPath("rps_licenciatariosmattel/") + cmbox_contrato.Items[cmbox_contrato.SelectedIndex].Text + "\\");

                                FilePath = Server.MapPath("rps_licenciatariosmattel/") + "\\" + FileName;
                                if (!Directory.Exists(Server.MapPath("rps_licenciatariosmattel/") + "\\"))
                                {
                                    Directory.CreateDirectory(Server.MapPath("rps_licenciatariosmattel/") + "\\");
                                }

                                ofile.SaveAs(FilePath);

                                cComprobanteImpuesto oComprobanteImpuesto = new cComprobanteImpuesto(ref oConn);
                                oComprobanteImpuesto.NumContrato        = cmbox_contrato.SelectedValue;
                                oComprobanteImpuesto.Periodo            = ddlmesventa.Items[ddlmesventa.SelectedIndex].Text;
                                oComprobanteImpuesto.NomComprobante     = FileName;
                                oComprobanteImpuesto.DeclaraMovimiento  = "S";
                                oComprobanteImpuesto.RepositorioArchivo = "L";
                                oComprobanteImpuesto.Accion             = "CREAR";
                                oComprobanteImpuesto.Put();

                                cFactura oFactura = new cFactura(ref oConn);
                                oFactura.CodFactura     = ddlmesventa.SelectedValue;
                                oFactura.CodComprobante = oComprobanteImpuesto.CodComprobante;
                                oFactura.Accion         = "EDITAR";
                                oFactura.Put();

                                if (string.IsNullOrEmpty(oComprobanteImpuesto.Error))
                                {
                                    pCodComprobante = oComprobanteImpuesto.CodComprobante;
                                    bExito          = true;
                                }
                            }
                            oConn.Close();
                        }
                    }

                    if (bExito)
                    {
                        oConn = new DBConn();
                        if (oConn.Open())
                        {
                            cLogEventos oLogEventos = new cLogEventos(ref oConn);
                            oLogEventos.AccionLog           = "SUBIR ARCHIVO COMPROBANTE DE IMPUESTO";
                            oLogEventos.CodCanal            = "2";
                            oLogEventos.CodFlujo            = "3";
                            oLogEventos.NomFlujo            = "COMPROBANTE DE IMPUESTO";
                            oLogEventos.ArchLog             = FileName;
                            oLogEventos.NumContrato         = cmbox_contrato.SelectedValue;
                            oLogEventos.NoContrato          = sNoContrato;
                            oLogEventos.PeriodoLog          = sPeriodo;
                            oLogEventos.RutDeudor           = oUsuario.RutLicenciatario;
                            oLogEventos.CodUser             = oUsuario.CodUsuario;
                            oLogEventos.RutUser             = oUsuario.RutUsuario;
                            oLogEventos.NomUser             = oUsuario.Nombres;
                            oLogEventos.ObsLog              = "Se a cargado el archivo de comprobante de impuesto con éxito.";
                            oLogEventos.IpLog               = oWeb.GetIpUsuario();
                            oLogEventos.IndReporsitorioArch = "L";
                            oLogEventos.Accion              = "CREAR";
                            oLogEventos.Put();
                        }
                        oConn.Close();

                        StringBuilder js = new StringBuilder();
                        js.Append("function LgRespuesta() {");
                        js.Append(" window.radalert('El comprobante pago de impuesto a sido cargado con éxito.', 330, 210); ");
                        js.Append(" Sys.Application.remove_load(LgRespuesta); ");
                        js.Append("};");
                        js.Append("Sys.Application.add_load(LgRespuesta);");
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", js.ToString(), true);

                        chkbox_declaracion.Checked = false;
                        ddlmesventa.Items.Clear();
                        cmbox_contrato.Items.Clear();
                        loadContrato();
                    }
                }
                else
                {
                    oConn = new DBConn();
                    if (oConn.Open())
                    {
                        cComprobanteImpuesto oComprobanteImpuesto = new cComprobanteImpuesto(ref oConn);
                        oComprobanteImpuesto.NumContrato       = cmbox_contrato.SelectedValue;
                        oComprobanteImpuesto.Periodo           = ddlmesventa.Items[ddlmesventa.SelectedIndex].Text;
                        oComprobanteImpuesto.NomComprobante    = string.Empty;
                        oComprobanteImpuesto.DeclaraMovimiento = "N";
                        oComprobanteImpuesto.Accion            = "CREAR";
                        oComprobanteImpuesto.Put();

                        cFactura oFactura = new cFactura(ref oConn);
                        oFactura.CodFactura     = ddlmesventa.SelectedValue;
                        oFactura.CodComprobante = oComprobanteImpuesto.CodComprobante;
                        oFactura.Accion         = "EDITAR";
                        oFactura.Put();

                        chkbox_declaracion.Checked = false;
                        ddlmesventa.Items.Clear();
                        cmbox_contrato.Items.Clear();
                        loadContrato();

                        oConn = new DBConn();
                        if (oConn.Open())
                        {
                            cLogEventos oLogEventos = new cLogEventos(ref oConn);
                            oLogEventos.AccionLog   = "SE DECLARA EL PERIODO SIN MOVIMIENTOS";
                            oLogEventos.CodCanal    = "2";
                            oLogEventos.CodFlujo    = "3";
                            oLogEventos.NomFlujo    = "COMPROBANTE DE IMPUESTO";
                            oLogEventos.NumContrato = cmbox_contrato.SelectedValue;
                            oLogEventos.NoContrato  = sNoContrato;
                            oLogEventos.PeriodoLog  = sPeriodo + " / " + ViewState["ano_reporte"].ToString();
                            oLogEventos.RutDeudor   = oUsuario.RutLicenciatario;
                            oLogEventos.CodUser     = oUsuario.CodUsuario;
                            oLogEventos.RutUser     = oUsuario.RutUsuario;
                            oLogEventos.NomUser     = oUsuario.Nombres;
                            oLogEventos.ObsLog      = "Se a declarado el periodo sin movimientos con éxito.";
                            oLogEventos.IpLog       = oWeb.GetIpUsuario();
                            oLogEventos.Accion      = "CREAR";
                            oLogEventos.Put();
                        }
                        oConn.Close();

                        StringBuilder js = new StringBuilder();
                        js.Append("function LgRespuesta() {");
                        js.Append(" window.radalert('Se ha declarado el periodo sin movimientos con éxito.', 330, 210); ");
                        js.Append(" Sys.Application.remove_load(LgRespuesta); ");
                        js.Append("};");
                        js.Append("Sys.Application.add_load(LgRespuesta);");
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "radalert", js.ToString(), true);
                    }
                    oConn.Close();
                }
            }
            catch (Exception Ex)
            {
                DBConn oConn = new DBConn();
                if (oConn.Open())
                {
                    cLogEventos oLogEventos = new cLogEventos(ref oConn);
                    oLogEventos.AccionLog   = "ERROR SUBIR ARCHIVO COMPROBANTE DE IMPUESTO";
                    oLogEventos.CodCanal    = "2";
                    oLogEventos.CodFlujo    = "3";
                    oLogEventos.NomFlujo    = "COMPROBANTE DE IMPUESTO";
                    oLogEventos.PeriodoLog  = sPeriodo;
                    oLogEventos.RutDeudor   = oUsuario.RutLicenciatario;
                    oLogEventos.CodUser     = oUsuario.CodUsuario;
                    oLogEventos.RutUser     = oUsuario.RutUsuario;
                    oLogEventos.NomUser     = oUsuario.Nombres;
                    oLogEventos.ObsErrorLog = Ex.Message + " / " + Ex.Source;
                    oLogEventos.IpLog       = oWeb.GetIpUsuario();
                    oLogEventos.Accion      = "CREAR";
                    oLogEventos.Put();
                }
                oConn.Close();

                Session["Error"] = "Error en la carga de archivo de comprobante de impuesto. Por favor comuniquese con el administrador.";
                Response.Redirect("error_ingreso_venta.aspx?sTypeError=2", true);
            }
        }
Exemple #7
0
        protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
            case "cmdDelete":
                DBConn oConn = new DBConn();
                if (oConn.Open())
                {
                    string pCodFactura  = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["codigo_factura"].ToString();
                    string pNumContrato = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["num_contrato"].ToString();
                    string pTipoFactura = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["tipo_factura"].ToString();

                    if (pTipoFactura == "A")
                    {
                        cDetalleFacturaAdvance oDetalleFacturaAdvance = new cDetalleFacturaAdvance(ref oConn);
                        oDetalleFacturaAdvance.CodigoFactura = pCodFactura;
                        oDetalleFacturaAdvance.Accion        = "ELIMINAR";
                        oDetalleFacturaAdvance.Put();

                        cContratos oContratos = new cContratos(ref oConn);
                        oContratos.NumContrato = pNumContrato;
                        oContratos.Accion      = "ELIMINAADVANCE";
                        oContratos.Put();
                    }
                    else if (pTipoFactura == "Q")
                    {
                        cDetalleFactura oDetalleFactura = new cDetalleFactura(ref oConn);
                        oDetalleFactura.CodigoFactura = pCodFactura;
                        DataTable dt = oDetalleFactura.Get();
                        if (dt != null)
                        {
                            if (dt.Rows.Count > 0)
                            {
                                foreach (DataRow oRow in dt.Rows)
                                {
                                    if (!string.IsNullOrEmpty(oRow["saldo_advance_usd"].ToString()))
                                    {
                                        cAdvanceContrato oAdvanceContrato = new cAdvanceContrato(ref oConn);
                                        oAdvanceContrato.NumContrato     = pNumContrato;
                                        oAdvanceContrato.CodMarca        = oRow["cod_marca"].ToString();
                                        oAdvanceContrato.CodCategoria    = oRow["cod_categoria"].ToString();
                                        oAdvanceContrato.CodSubCategoria = oRow["cod_subcategoria"].ToString();
                                        DataTable dtAdvance = oAdvanceContrato.Get();
                                        if (dtAdvance != null)
                                        {
                                            if (dtAdvance.Rows.Count > 0)
                                            {
                                                oAdvanceContrato.Saldo  = (!string.IsNullOrEmpty(dtAdvance.Rows[0]["saldo"].ToString()) ? (double.Parse(dtAdvance.Rows[0]["saldo"].ToString()) + double.Parse(oRow["saldo_advance_usd"].ToString())).ToString() : oRow["saldo_advance_usd"].ToString());
                                                oAdvanceContrato.Accion = "EDITAR";
                                                oAdvanceContrato.Put();
                                            }
                                        }
                                        dtAdvance = null;
                                    }
                                }
                            }
                        }
                        dt = null;

                        oDetalleFactura.Accion = "ELIMINAR";
                        oDetalleFactura.Put();



                        cReporteVenta oReporteVenta = new cReporteVenta(ref oConn);
                        oReporteVenta.CodigoFactura = pCodFactura;
                        oReporteVenta.Accion        = "ELIMINAFACTURA";
                        oReporteVenta.Put();
                    }
                    else if (pTipoFactura == "S")
                    {
                        cDetFactShortfall oDetFactShortfall = new cDetFactShortfall(ref oConn);
                        oDetFactShortfall.CodigoFactura = pCodFactura;
                        oDetFactShortfall.Accion        = "ELIMINAR";
                        oDetFactShortfall.Put();

                        cMinimoContrato oMinimoContrato = new cMinimoContrato(ref oConn);
                        oMinimoContrato.CodFactShortFall = pCodFactura;
                        oMinimoContrato.Accion           = "ELIMINAFACTURA";
                        oMinimoContrato.Put();
                    }

                    cFactura oFactura = new cFactura(ref oConn);
                    oFactura.CodFactura = pCodFactura;
                    oFactura.Accion     = "ELIMINAR";
                    oFactura.Put();

                    cLogEventos oLogEventos = new cLogEventos(ref oConn);
                    oLogEventos.AccionLog   = "ELIMINACION DE FACTURA DE " + (pTipoFactura == "A" ? "ADVANCE" : (pTipoFactura == "Q" ? "PERIODO" : "SHORTFALL")) + " FE" + pCodFactura;
                    oLogEventos.CodCanal    = "2";
                    oLogEventos.CodFlujo    = "9";
                    oLogEventos.NomFlujo    = "ELIMINACION DE FACTURA";
                    oLogEventos.NumContrato = pNumContrato;
                    oLogEventos.CodUser     = oUsuario.CodUsuario;
                    oLogEventos.RutUser     = oUsuario.RutUsuario;
                    oLogEventos.NomUser     = oUsuario.Nombres;
                    oLogEventos.IpLog       = oWeb.GetIpUsuario();
                    oLogEventos.Accion      = "CREAR";
                    oLogEventos.Put();
                }
                oConn.Close();
                break;
            }

            RadGrid1.Rebind();
        }
        protected void btnGuardar_Click(object sender, EventArgs e)
        {
            string sPeriodo = oWeb.getPeriodoVenta(oWeb.getMes(int.Parse(hddMesReporte.Value)).ToUpper());

            if (Page.IsValid)
            {
                try
                {
                    DBConn oConn = new DBConn();
                    if (oConn.Open())
                    {
                        cReporteVenta oReporteVenta = new cReporteVenta(ref oConn);
                        oReporteVenta.CodigoReporteVenta = hddCodReporteVenta.Value;
                        oReporteVenta.FechaReporte       = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        oReporteVenta.Facturado          = "N";
                        oReporteVenta.EstReporte         = "L";
                        oReporteVenta.Periodo            = sPeriodo;
                        oReporteVenta.DeclaraMovimiento  = "S";
                        oReporteVenta.Accion             = "EDITAR";
                        oReporteVenta.Put();

                        cLogEventos oLogEventos = new cLogEventos(ref oConn);
                        oLogEventos.AccionLog   = "AUTORIZACIÓN OK DE REPORTE DE VENTAS";
                        oLogEventos.CodCanal    = "2";
                        oLogEventos.CodFlujo    = "2";
                        oLogEventos.NomFlujo    = "INGRESO DE VENTAS LICENCIATARIO";
                        oLogEventos.NumContrato = hddNumContrato.Value;
                        oLogEventos.NoContrato  = hddNoContrato.Value;
                        oLogEventos.PeriodoLog  = sPeriodo + " / " + hddAnoReporte.Value;
                        oLogEventos.NkeyDeudor  = oUsuario.NKeyDeudor;
                        oLogEventos.RutDeudor   = oUsuario.RutLicenciatario;
                        oLogEventos.NomDeudor   = oUsuario.Licenciatario;
                        oLogEventos.CodUser     = oUsuario.CodUsuario;
                        oLogEventos.RutUser     = oUsuario.RutUsuario;
                        oLogEventos.NomUser     = oUsuario.Nombres;
                        oLogEventos.ObsLog      = "Se a autorizado correctamente el inhgreso de ventas.";
                        oLogEventos.IpLog       = oWeb.GetIpUsuario();
                        oLogEventos.Accion      = "CREAR";
                        oLogEventos.Put();
                    }
                    oConn.Close();

                    StringBuilder sUrl = new StringBuilder();
                    sUrl.Append("ventas_ingresadas.aspx?MesReporte=").Append(hddMesReporte.Value);
                    sUrl.Append("&AnoReporte=").Append(hddAnoReporte.Value);
                    sUrl.Append("&NoContrato=").Append(hddNoContrato.Value);
                    Response.Redirect(sUrl.ToString(), false);
                }
                catch (Exception Ex)
                {
                    DBConn oConn = new DBConn();
                    if (oConn.Open())
                    {
                        cLogEventos oLogEventos = new cLogEventos(ref oConn);
                        oLogEventos.AccionLog   = "ERROR EN AUTORIZACION DE INGRESO DE REPORTE DE VENTAS";
                        oLogEventos.CodCanal    = "2";
                        oLogEventos.CodFlujo    = "2";
                        oLogEventos.NomFlujo    = "INGRESO DE VENTAS LICENCIATARIO";
                        oLogEventos.PeriodoLog  = sPeriodo + " / " + hddAnoReporte.Value;
                        oLogEventos.NkeyDeudor  = oUsuario.NKeyDeudor;
                        oLogEventos.RutDeudor   = oUsuario.RutLicenciatario;
                        oLogEventos.NomDeudor   = oUsuario.Licenciatario;
                        oLogEventos.CodUser     = oUsuario.CodUsuario;
                        oLogEventos.RutUser     = oUsuario.RutUsuario;
                        oLogEventos.NomUser     = oUsuario.Nombres;
                        oLogEventos.ObsErrorLog = Ex.Message + " / " + Ex.Source;
                        oLogEventos.IpLog       = oWeb.GetIpUsuario();
                        oLogEventos.Accion      = "CREAR";
                        oLogEventos.Put();

                        cReporteVenta oReporteVenta = new cReporteVenta(ref oConn);
                        oReporteVenta.CodigoReporteVenta = hddCodReporteVenta.Value;
                        oReporteVenta.FechaReporte       = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        oReporteVenta.Facturado          = "N";
                        oReporteVenta.EstReporte         = "P";
                        oReporteVenta.Periodo            = sPeriodo;
                        oReporteVenta.Accion             = "EDITAR";
                        oReporteVenta.Put();
                    }
                    oConn.Close();

                    Response.Redirect("error_ingreso_venta.aspx", false);
                }
            }
        }
        protected void putAprobarAdvanceAll(string pNumContrato, string pNoContrato)
        {
            StringBuilder sHtml = new StringBuilder();

            sHtml.Append(File.ReadAllText(Server.MapPath("invoice.html")));
            StringBuilder sDataProductos      = new StringBuilder();
            StringBuilder sDataValorProductos = new StringBuilder();
            double        dTotalFacturaUsd    = 0;

            if (ViewState["FacturaAdvance_" + pNumContrato] != null)
            {
                DataTable dtFactura = ViewState["FacturaAdvance_" + pNumContrato] as DataTable;
                if (dtFactura != null)
                {
                    if (dtFactura.Rows.Count > 0)
                    {
                        DBConn oConn = new DBConn();
                        if (oConn.Open())
                        {
                            cFactura oFactura = new cFactura(ref oConn);
                            oFactura.NumContrato = pNumContrato;
                            oFactura.Territory   = "CHILE";
                            oFactura.DateInvoce  = DateTime.Now.ToString("yyyy-MM-dd");
                            oFactura.DueDate     = DateTime.Now.AddMonths(1).ToString("yyyy-MM-dd");
                            oFactura.TipoFactura = "A";
                            oFactura.Accion      = "CREAR";
                            oFactura.Put();

                            string pCodFactura = oFactura.CodFactura;

                            foreach (DataRow oRow in dtFactura.Rows)
                            {
                                string sDescripcionMarca = oRow["Marca"].ToString();
                                sDataProductos.Append("<div style=\"padding-top:10px;padding-bottom:10px;\">");
                                sDataProductos.Append("<div><font style=\"font-family:Arial; color:#000000; font-size:10pt;font-weight:bold;\">");
                                sDataProductos.Append(oRow["Marca"].ToString());

                                string pCodCategoria    = null;
                                string pCodSubCategoria = null;

                                if (!string.IsNullOrEmpty(oRow["CodCategoria"].ToString()))
                                {
                                    pCodCategoria = oRow["CodCategoria"].ToString();
                                    sDataProductos.Append(" / " + oRow["Categoria"].ToString());
                                }

                                if (!string.IsNullOrEmpty(oRow["CodSubCategoria"].ToString()))
                                {
                                    pCodSubCategoria = oRow["CodSubCategoria"].ToString();
                                    sDataProductos.Append(" / " + oRow["SubCategoria"].ToString());
                                }

                                sDataProductos.Append("</font></div>");
                                sDataProductos.Append("</div>");

                                cDetalleFacturaAdvance oDetalleFacturaAdvance = new cDetalleFacturaAdvance(ref oConn);
                                oDetalleFacturaAdvance.CodigoFactura   = pCodFactura;
                                oDetalleFacturaAdvance.CodMarca        = oRow["CodMarca"].ToString();
                                oDetalleFacturaAdvance.CodCategoria    = pCodCategoria;
                                oDetalleFacturaAdvance.CodSubCategoria = pCodSubCategoria;
                                oDetalleFacturaAdvance.AdvanceUsd      = double.Parse(oRow["Advance USD"].ToString()).ToString();
                                oDetalleFacturaAdvance.Accion          = "CREAR";
                                oDetalleFacturaAdvance.Put();

                                sDataValorProductos.Append("<div style=\"padding-top:10px;padding-bottom:10px; padding-left:10px;\">");
                                sDataValorProductos.Append("<div style=\"float:left;width:30px;\"><font style=\"font-family:Arial; color:#000000; font-size:10pt;\">$</font></div>");
                                sDataValorProductos.Append("<div style=\"text-align:right;\"><font style=\"font-family:Arial; color:#000000; font-size:10pt;\">").Append(double.Parse(oRow["Advance USD"].ToString()).ToString("#,##0.00")).Append("</font></div>");
                                sDataValorProductos.Append("</div>");

                                dTotalFacturaUsd = dTotalFacturaUsd + double.Parse(oRow["Advance USD"].ToString());
                            }

                            string strNomDeudor   = string.Empty;
                            string sNomContacto   = string.Empty;
                            string sEmailContacto = string.Empty;
                            string sDireccion     = string.Empty;
                            string sComuna        = string.Empty;
                            string sCiudad        = string.Empty;

                            cContratos oContrato = new cContratos(ref oConn);
                            oContrato.NumContrato = pNumContrato;
                            DataTable dtContrato = oContrato.Get();
                            if (dtContrato != null)
                            {
                                if (dtContrato.Rows.Count > 0)
                                {
                                    cDeudor oDeudor = new cDeudor(ref oConn);
                                    oDeudor.NKeyDeudor = dtContrato.Rows[0]["nKey_Deudor"].ToString();
                                    DataTable dtDeudor = oDeudor.Get();
                                    if (dtDeudor != null)
                                    {
                                        if (dtDeudor.Rows.Count > 0)
                                        {
                                            strNomDeudor = dtDeudor.Rows[0]["snombre"].ToString();
                                        }
                                    }
                                    dtDeudor = null;

                                    cContactosDeudor oContactosDeudor = new cContactosDeudor(ref oConn);
                                    oContactosDeudor.NKeyDeudor  = dtContrato.Rows[0]["nKey_Deudor"].ToString();
                                    oContactosDeudor.NkeyCliente = oUsuario.NKeyUsuario;
                                    DataTable dtContacto = oContactosDeudor.Get();
                                    if (dtContacto != null)
                                    {
                                        if (dtContacto.Rows.Count > 0)
                                        {
                                            sNomContacto   = dtContacto.Rows[0]["sNombre"].ToString();
                                            sEmailContacto = dtContacto.Rows[0]["sEmail"].ToString();
                                        }
                                    }
                                    dtContacto = null;

                                    cDirecciones oDirecciones = new cDirecciones(ref oConn);
                                    oDirecciones.NKeyDeudor  = dtContrato.Rows[0]["nKey_Deudor"].ToString();
                                    oDirecciones.NkeyCliente = oUsuario.NKeyUsuario;
                                    DataTable dtDireccion = oDirecciones.Get();
                                    if (dtDireccion != null)
                                    {
                                        if (dtDireccion.Rows.Count > 0)
                                        {
                                            sDireccion = dtDireccion.Rows[0]["sDireccion"].ToString();
                                            sComuna    = dtDireccion.Rows[0]["sComuna"].ToString();
                                            sCiudad    = dtDireccion.Rows[0]["sCiudad"].ToString();
                                        }
                                    }
                                    dtDireccion = null;
                                }
                            }
                            dtContrato = null;

                            //string sNumInvoce = strNomDeudor.Substring(0, 3).ToUpper();
                            //sNumInvoce = sNumInvoce.ToUpper();
                            //string sNumInvoce = "FE" + int.Parse(pCodFactura).ToString("D10");
                            string sNumInvoce = "FE" + pCodFactura;

                            oFactura.Total      = dTotalFacturaUsd.ToString();
                            oFactura.Periodo    = "Advance " + DateTime.Now.Year.ToString();
                            oFactura.NumInvoice = sNumInvoce;
                            oFactura.Accion     = "EDITAR";
                            oFactura.Put();

                            cContratos oContratos = new cContratos(ref oConn);
                            oContratos.NumContrato = pNumContrato;
                            oContratos.Accion      = "EDITAR";
                            oContratos.Put();

                            sHtml.Replace("[#NUMFACTURA]", sNumInvoce);
                            sHtml.Replace("[#NOMEMPRESA]", strNomDeudor);
                            sHtml.Replace("[#NOMCONTACTO]", sNomContacto);
                            sHtml.Replace("[#EMAILCONTACTO]", sEmailContacto);
                            sHtml.Replace("[#DIRECCION]", sDireccion + (!string.IsNullOrEmpty(sComuna) ? ", " + sComuna : string.Empty));
                            sHtml.Replace("[#CIUDAD]", sCiudad);
                            sHtml.Replace("[#PAIS]", "");
                            sHtml.Replace("[#FECHA]", DateTime.Now.ToString("d-MMM-yy", CultureInfo.CreateSpecificCulture("en-US")));
                            sHtml.Replace("[#NUMCONTRATO]", pNoContrato);
                            sHtml.Replace("[#PERIODOQ]", "Advance " + DateTime.Now.Year.ToString());
                            sHtml.Replace("[#PROPERTY]", "");
                            sHtml.Replace("[#TERRITORIO]", "CHILE");
                            sHtml.Replace("[#DETALLEPRODUCTOS]", sDataProductos.ToString());
                            sHtml.Replace("[#DETALLEVALORPRODUCTOS]", sDataValorProductos.ToString());
                            sHtml.Replace("[#DUEDATE]", DateTime.Now.AddMonths(1).ToString("d-MMM-yy", CultureInfo.CreateSpecificCulture("en-US")));
                            //sHtml.Replace("[#TOTAL]", double.Parse(dTotalFacturaUsd.ToString()).ToString("#,##0.00"));
                            sHtml.Replace("[#TOTAL]", double.Parse(dTotalFacturaUsd.ToString()).ToString("N0"));

                            if (!Directory.Exists(Server.MapPath("Facturas_Advance/") + pNoContrato + "/"))
                            {
                                Directory.CreateDirectory(Server.MapPath("Facturas_Advance/") + pNoContrato + "/");
                            }

                            string sFileHtml = Server.MapPath("Facturas_Advance/") + pNoContrato + "/" + sNumInvoce + ".html";
                            File.WriteAllText(sFileHtml, sHtml.ToString(), Encoding.UTF8);

                            cLogEventos oLogEventos = new cLogEventos(ref oConn);
                            oLogEventos.AccionLog   = "APROBACION MASIVA PRE-FACTURA ADVANCE";
                            oLogEventos.CodCanal    = "2";
                            oLogEventos.CodFlujo    = "4";
                            oLogEventos.NomFlujo    = "FACTURACION ADVANCE";
                            oLogEventos.NumContrato = pNumContrato;
                            oLogEventos.NoContrato  = pNoContrato;
                            oLogEventos.PeriodoLog  = "Advance " + DateTime.Now.Year.ToString();
                            //oLogEventos.RutDeudor = oUsuario.RutLicenciatario;
                            oLogEventos.CodUser = oUsuario.CodUsuario;
                            oLogEventos.RutUser = oUsuario.RutUsuario;
                            oLogEventos.NomUser = oUsuario.Nombres;
                            oLogEventos.ObsLog  = "Se ha autorizado correctamente la Factura Advance " + sNumInvoce;
                            oLogEventos.IpLog   = oWeb.GetIpUsuario();
                            oLogEventos.Accion  = "CREAR";
                            oLogEventos.Put();
                        }
                        oConn.Close();
                    }
                }
                dtFactura = null;
            }
        }