public void ProcessRequest(HttpContext context)
        {
            DBConn oConn = new DBConn();

            if (oConn.Open())
            {
                Web oWeb = new Web();

                //cProductosContrato oProductosContrato = new cProductosContrato(ref oConn);
                //oProductosContrato.NumContrato = oWeb.GetData("pCodContrato");
                //DataTable dtProdCont = oProductosContrato.GetByExcel();

                cRoyaltyContrato oRoyaltyContrato = new cRoyaltyContrato(ref oConn);
                oRoyaltyContrato.NumContrato = oWeb.GetData("pCodContrato");
                DataTable dtProdCont = oRoyaltyContrato.GetByExcel();
                oConn.Close();

                System.Web.HttpResponse oResponse = System.Web.HttpContext.Current.Response;

                oResponse.Clear();
                oResponse.AddHeader("content-disposition", "attachment;filename=FileName.csv");
                oResponse.Charset = "";
                oResponse.Cache.SetCacheability(HttpCacheability.NoCache);
                oResponse.ContentType = "application/vnd.ms-excel";

                oResponse.Output.Write(ToCSV(dtProdCont));
                oResponse.Flush();
                oResponse.End();
            }
        }
        protected void rdRoyaltyBDI_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            DBConn oConn = new DBConn();

            if (oConn.Open())
            {
                cRoyaltyContrato RoyaltyContrato = new cRoyaltyContrato(ref oConn);
                RoyaltyContrato.NumContrato = num_contrato.Value;
                rdRoyaltyBDI.DataSource     = RoyaltyContrato.GetByExcel();
            }
            oConn.Close();
        }
        protected void getDataQInvoce(string sQ, string sYear)
        {
            detFactura oFactura = new detFactura();
            DBConn     oConn    = new DBConn();

            if (oConn.Open())
            {
                string        arrMes           = string.Empty;
                string        arrReporteVenta  = string.Empty;
                cReporteVenta oReportForInvoce = new cReporteVenta(ref oConn);
                oReportForInvoce.NumContrato = hdd_num_contrato.Value;
                oReportForInvoce.Periodo     = sQ;
                oReportForInvoce.AnoReporte  = sYear;
                DataTable dtReporteVenta = oReportForInvoce.GetReportForInvoice();
                foreach (DataRow oRowReporte in dtReporteVenta.Rows)
                {
                    arrReporteVenta = (string.IsNullOrEmpty(arrReporteVenta) ? oRowReporte["cod_reporte_venta"].ToString() : arrReporteVenta + ";" + oRowReporte["cod_reporte_venta"].ToString());
                    arrMes          = (string.IsNullOrEmpty(arrMes) ? oRowReporte["mes_reporte"].ToString() : arrMes + ";" + oRowReporte["mes_reporte"].ToString());
                }
                arrayReporteVenta = arrReporteVenta.Split(';');
                arrayMes          = arrMes.Split(';');

                string sNomMesUno  = oWeb.getMes(int.Parse(arrayMes[0].ToString())).ToString();
                string sNomMesDos  = oWeb.getMes(int.Parse(arrayMes[1].ToString())).ToString();
                string sNomMesTres = oWeb.getMes(int.Parse(arrayMes[2].ToString())).ToString();

                oFactura.MesNomUno  = sNomMesUno;
                oFactura.MesNomDos  = sNomMesDos;
                oFactura.MesNomTres = sNomMesTres;
                oFactura.getMakeTable();

                cDetalleVenta oDetalleVenta;

                //cProductosContrato oProductosContrato = new cProductosContrato(ref oConn);
                //oProductosContrato.NumContrato = hdd_num_contrato.Value;

                cRoyaltyContrato oRoyaltyContrato = new cRoyaltyContrato(ref oConn);
                oRoyaltyContrato.NumContrato = hdd_num_contrato.Value;

                DataTable dtProdCont = oRoyaltyContrato.GetByInvoce();
                if (dtProdCont != null)
                {
                    foreach (DataRow oRow in dtProdCont.Rows)
                    {
                        bool   bVenta      = false;
                        string pCodRotalty = string.Empty;
                        double sRoyalty    = 0;
                        double sBdi        = 0;
                        string sPeriodo    = string.Empty;
                        string arrMntMes   = string.Empty;

                        foreach (DataRow oRowReporte in dtReporteVenta.Rows)
                        {
                            sPeriodo = oRowReporte["periodo_q"].ToString() + "/" + oRowReporte["ano_reporte"].ToString();

                            oDetalleVenta = new cDetalleVenta(ref oConn);
                            oDetalleVenta.CodigoReporteVenta = oRowReporte["cod_reporte_venta"].ToString();
                            oDetalleVenta.Marca        = oRow["cod_marca"].ToString();
                            oDetalleVenta.Categoria    = oRow["cod_categoria"].ToString();
                            oDetalleVenta.SubCategoria = oRow["cod_subcategoria"].ToString();
                            oDetalleVenta.CodRoyalty   = oRow["cod_royalty"].ToString();
                            DataTable detVenta = oDetalleVenta.GetByFactura();
                            if (detVenta != null)
                            {
                                if (detVenta.Rows.Count > 0)
                                {
                                    //int intPrecioUni = int.Parse(detVenta.Rows[0]["precio_uni"].ToString());
                                    //int intCantidadVenta = int.Parse(detVenta.Rows[0]["cantidad_venta"].ToString());
                                    //int intPrecioUniDevol = int.Parse(detVenta.Rows[0]["precio_uni_devol"].ToString());
                                    //int intCantDescueDevol = int.Parse(detVenta.Rows[0]["cant_descue_devol"].ToString());
                                    bVenta      = true;
                                    pCodRotalty = detVenta.Rows[0]["cod_royalty"].ToString();
                                    sRoyalty    = double.Parse(detVenta.Rows[0]["royalty"].ToString());
                                    sBdi        = double.Parse(detVenta.Rows[0]["bdi"].ToString());

                                    //int intTotalMes = ((intPrecioUni * intCantidadVenta) - (intPrecioUniDevol * intCantDescueDevol));
                                    double intTotalMes = double.Parse(detVenta.Rows[0]["totalmes"].ToString());

                                    arrMntMes = (string.IsNullOrEmpty(arrMntMes) ? intTotalMes.ToString() : arrMntMes + ";" + intTotalMes.ToString());
                                }
                                else
                                {
                                    arrMntMes = (string.IsNullOrEmpty(arrMntMes) ? "0" : arrMntMes + ";0");
                                }
                            }
                            detVenta = null;
                        }

                        if (bVenta)
                        {
                            string[] arrayMntMes = arrMntMes.Split(';');


                            oFactura.Marca        = oRow["cod_marca"].ToString();
                            oFactura.Categoria    = oRow["cod_categoria"].ToString();
                            oFactura.SubCategoria = oRow["cod_subcategoria"].ToString();
                            oFactura.MesUno       = arrayMntMes[0].ToString();
                            oFactura.MesDos       = arrayMntMes[1].ToString();
                            oFactura.MesTres      = arrayMntMes[2].ToString();
                            oFactura.CodRoyalty   = pCodRotalty;
                            oFactura.Royalty      = sRoyalty.ToString();
                            oFactura.Bdi          = sBdi.ToString();
                            oFactura.Periodo      = sPeriodo;

                            double MntRoyUno  = double.Parse(arrayMntMes[0].ToString()) / getValueUsd(arrayMes[0].ToString(), sYear, pCodRotalty);
                            double MntRoyDos  = double.Parse(arrayMntMes[1].ToString()) / getValueUsd(arrayMes[1].ToString(), sYear, pCodRotalty);
                            double MntRoyTres = double.Parse(arrayMntMes[2].ToString()) / getValueUsd(arrayMes[2].ToString(), sYear, pCodRotalty);

                            double MntTotal = MntRoyUno + MntRoyDos + MntRoyTres;

                            oFactura.MontoRoyaltyUsd = (MntTotal * sRoyalty).ToString();

                            double MntDbiUno  = ((double.Parse(arrayMntMes[0].ToString()) / getValueUsd(arrayMes[0].ToString(), sYear, pCodRotalty)) * (sBdi));
                            double MntDbiDos  = ((double.Parse(arrayMntMes[1].ToString()) / getValueUsd(arrayMes[1].ToString(), sYear, pCodRotalty)) * (sBdi));
                            double MntDbiTres = ((double.Parse(arrayMntMes[2].ToString()) / getValueUsd(arrayMes[2].ToString(), sYear, pCodRotalty)) * (sBdi));

                            oFactura.MontoDbiUsd = (MntDbiUno + MntDbiDos + MntDbiTres).ToString();

                            string iSaldo = string.Empty;
                            oFactura.SaldoAdvanceUsd = "0";
                            oFactura.Saldo           = "0";
                            cAdvanceContrato oAdvanceContrato = new cAdvanceContrato(ref oConn);
                            oAdvanceContrato.NumContrato     = hdd_num_contrato.Value;
                            oAdvanceContrato.CodMarca        = oRow["cod_marca"].ToString();
                            oAdvanceContrato.CodCategoria    = oRow["cod_categoria"].ToString();
                            oAdvanceContrato.CodSubCategoria = oRow["cod_subcategoria"].ToString();
                            oAdvanceContrato.Facturado       = true;
                            DataTable dtAdvanceSaldo = oAdvanceContrato.Get();
                            if (dtAdvanceSaldo != null)
                            {
                                if (dtAdvanceSaldo.Rows.Count > 0)
                                {
                                    if (string.IsNullOrEmpty(dtAdvanceSaldo.Rows[0]["saldo"].ToString()))
                                    {
                                        if (double.Parse(oFactura.MontoRoyaltyUsd) > double.Parse(dtAdvanceSaldo.Rows[0]["valor_original"].ToString()))
                                        {
                                            oFactura.SaldoAdvanceUsd = double.Parse(dtAdvanceSaldo.Rows[0]["valor_original"].ToString()).ToString();
                                            oFactura.Saldo           = "0";
                                        }
                                        else
                                        {
                                            if (double.Parse(oFactura.MontoRoyaltyUsd) > 0)
                                            {
                                                oFactura.SaldoAdvanceUsd = double.Parse(oFactura.MontoRoyaltyUsd).ToString();
                                                oFactura.Saldo           = (double.Parse(dtAdvanceSaldo.Rows[0]["valor_original"].ToString()) - double.Parse(oFactura.MontoRoyaltyUsd)).ToString();
                                            }
                                            else
                                            {
                                                oFactura.SaldoAdvanceUsd = "0";
                                                oFactura.Saldo           = "0";
                                            }
                                        }
                                    }
                                    else if (double.Parse(oFactura.MontoRoyaltyUsd) > double.Parse(dtAdvanceSaldo.Rows[0]["saldo"].ToString()))
                                    {
                                        oFactura.SaldoAdvanceUsd = (double.Parse(dtAdvanceSaldo.Rows[0]["saldo"].ToString())).ToString();
                                        oFactura.Saldo           = "0";
                                    }
                                    else
                                    {
                                        if (double.Parse(oFactura.MontoRoyaltyUsd) > 0)
                                        {
                                            oFactura.SaldoAdvanceUsd = double.Parse(oFactura.MontoRoyaltyUsd).ToString();
                                            oFactura.Saldo           = (double.Parse(dtAdvanceSaldo.Rows[0]["saldo"].ToString()) - double.Parse(oFactura.MontoRoyaltyUsd)).ToString();
                                        }
                                        else
                                        {
                                            oFactura.SaldoAdvanceUsd = "0";
                                            oFactura.Saldo           = "0";
                                        }
                                    }
                                }
                            }
                            dtAdvanceSaldo = null;

                            oFactura.FacturaUsd = (double.Parse(oFactura.MontoRoyaltyUsd) + double.Parse(oFactura.MontoDbiUsd) - double.Parse(oFactura.SaldoAdvanceUsd)).ToString();

                            oFactura.AddRow();
                        }
                    }
                }
                dtReporteVenta = null;
                dtProdCont     = null;

                DataTable dtFactura = oFactura.Get();

                RadGrid oGridFractura = new RadGrid();
                oGridFractura.ID                  = "rdGridFactura";
                oGridFractura.ShowStatusBar       = true;
                oGridFractura.ShowFooter          = true;
                oGridFractura.AutoGenerateColumns = false;
                oGridFractura.Skin                = "Sitefinity";
                oGridFractura.ItemDataBound      += oGridFractura_ItemDataBound;
                oGridFractura.ItemCommand        += oGridFractura_ItemCommand;


                oGridFractura.ExportSettings.HideStructureColumns = true;

                oGridFractura.MasterTableView.AutoGenerateColumns      = false;
                oGridFractura.MasterTableView.ShowHeader               = true;
                oGridFractura.MasterTableView.TableLayout              = GridTableLayout.Fixed;
                oGridFractura.MasterTableView.ShowHeadersWhenNoRecords = true;
                oGridFractura.MasterTableView.ShowFooter               = true;
                oGridFractura.MasterTableView.CommandItemDisplay       = GridCommandItemDisplay.Top;
                oGridFractura.MasterTableView.CommandItemSettings.ShowExportToExcelButton = true;
                oGridFractura.MasterTableView.CommandItemSettings.ShowRefreshButton       = false;
                oGridFractura.MasterTableView.CommandItemSettings.ShowAddNewRecordButton  = false;

                GridBoundColumn oGridBoundColumn;
                oGridBoundColumn            = new GridBoundColumn();
                oGridBoundColumn.DataField  = "Marca";
                oGridBoundColumn.HeaderText = "Marca";
                oGridBoundColumn.UniqueName = "Marca";
                oGridFractura.MasterTableView.Columns.Add(oGridBoundColumn);

                oGridBoundColumn            = new GridBoundColumn();
                oGridBoundColumn.DataField  = "Categoría";
                oGridBoundColumn.HeaderText = "Categoría";
                oGridBoundColumn.UniqueName = "Categoría";
                oGridFractura.MasterTableView.Columns.Add(oGridBoundColumn);

                oGridBoundColumn            = new GridBoundColumn();
                oGridBoundColumn.DataField  = "Subcategoría";
                oGridBoundColumn.HeaderText = "Subcategoría";
                oGridBoundColumn.UniqueName = "Subcategoría";
                oGridFractura.MasterTableView.Columns.Add(oGridBoundColumn);

                oGridBoundColumn                             = new GridBoundColumn();
                oGridBoundColumn.DataField                   = sNomMesUno;
                oGridBoundColumn.HeaderText                  = sNomMesUno;
                oGridBoundColumn.UniqueName                  = sNomMesUno;
                oGridBoundColumn.FooterText                  = "Total " + sNomMesUno;
                oGridBoundColumn.DataFormatString            = "{0:N0}";
                oGridBoundColumn.FooterAggregateFormatString = "{0:N0}";
                oGridBoundColumn.Aggregate                   = GridAggregateFunction.Sum;
                oGridFractura.MasterTableView.Columns.Add(oGridBoundColumn);

                oGridBoundColumn                             = new GridBoundColumn();
                oGridBoundColumn.DataField                   = sNomMesDos;
                oGridBoundColumn.HeaderText                  = sNomMesDos;
                oGridBoundColumn.UniqueName                  = sNomMesDos;
                oGridBoundColumn.FooterText                  = "Total " + sNomMesDos;
                oGridBoundColumn.DataFormatString            = "{0:N0}";
                oGridBoundColumn.FooterAggregateFormatString = "{0:N0}";
                oGridBoundColumn.Aggregate                   = GridAggregateFunction.Sum;
                oGridFractura.MasterTableView.Columns.Add(oGridBoundColumn);

                oGridBoundColumn                             = new GridBoundColumn();
                oGridBoundColumn.DataField                   = sNomMesTres;
                oGridBoundColumn.HeaderText                  = sNomMesTres;
                oGridBoundColumn.UniqueName                  = sNomMesTres;
                oGridBoundColumn.FooterText                  = "Total " + sNomMesTres;
                oGridBoundColumn.DataFormatString            = "{0:N0}";
                oGridBoundColumn.FooterAggregateFormatString = "{0:N0}";
                oGridBoundColumn.Aggregate                   = GridAggregateFunction.Sum;
                oGridFractura.MasterTableView.Columns.Add(oGridBoundColumn);

                oGridBoundColumn            = new GridBoundColumn();
                oGridBoundColumn.DataField  = "Royalty (%)";
                oGridBoundColumn.HeaderText = "Royalty (%)";
                oGridBoundColumn.UniqueName = "Royalty (%)";
                oGridFractura.MasterTableView.Columns.Add(oGridBoundColumn);

                oGridBoundColumn            = new GridBoundColumn();
                oGridBoundColumn.DataField  = "BDI (%)";
                oGridBoundColumn.HeaderText = "BDI (%)";
                oGridBoundColumn.UniqueName = "BDI (%)";
                oGridFractura.MasterTableView.Columns.Add(oGridBoundColumn);

                oGridBoundColumn            = new GridBoundColumn();
                oGridBoundColumn.DataField  = "Periodo";
                oGridBoundColumn.HeaderText = "Periodo";
                oGridBoundColumn.UniqueName = "Periodo";
                oGridFractura.MasterTableView.Columns.Add(oGridBoundColumn);

                oGridBoundColumn                             = new GridBoundColumn();
                oGridBoundColumn.DataField                   = "Monto Royalty USD";
                oGridBoundColumn.HeaderText                  = "Monto Royalty USD";
                oGridBoundColumn.UniqueName                  = "Monto Royalty USD";
                oGridBoundColumn.FooterText                  = "Total Royalty USD";
                oGridBoundColumn.DataFormatString            = "{0:N0}";
                oGridBoundColumn.FooterAggregateFormatString = "{0:N0}";
                oGridBoundColumn.Aggregate                   = GridAggregateFunction.Sum;
                oGridFractura.MasterTableView.Columns.Add(oGridBoundColumn);

                oGridBoundColumn                             = new GridBoundColumn();
                oGridBoundColumn.DataField                   = "Monto BDI USD";
                oGridBoundColumn.HeaderText                  = "Monto BDI USD";
                oGridBoundColumn.UniqueName                  = "Monto BDI USD";
                oGridBoundColumn.FooterText                  = "Total BDI USD";
                oGridBoundColumn.DataFormatString            = "{0:N0}";
                oGridBoundColumn.FooterAggregateFormatString = "{0:N0}";
                oGridBoundColumn.Aggregate                   = GridAggregateFunction.Sum;
                oGridFractura.MasterTableView.Columns.Add(oGridBoundColumn);

                /*oGridBoundColumn = new GridBoundColumn();
                 * oGridBoundColumn.DataField = "Saldo Advance USD";
                 * oGridBoundColumn.HeaderText = "Saldo Advance USD";
                 * oGridFractura.MasterTableView.Columns.Add(oGridBoundColumn);*/

                oGridBoundColumn            = new GridBoundColumn();
                oGridBoundColumn.DataField  = "Saldo Advance USD";
                oGridBoundColumn.HeaderText = "Saldo Advance USD";
                oGridBoundColumn.UniqueName = "Saldo Advance USD";
                oGridFractura.MasterTableView.Columns.Add(oGridBoundColumn);

                oGridBoundColumn            = new GridBoundColumn();
                oGridBoundColumn.DataField  = "Factura USD";
                oGridBoundColumn.HeaderText = "Factura USD";
                oGridBoundColumn.UniqueName = "Factura USD";
                oGridBoundColumn.FooterText = "Total Factura USD";
                //oGridBoundColumn.DataFormatString = "{0:N0}";
                oGridBoundColumn.FooterAggregateFormatString = "{0:N0}";
                oGridBoundColumn.Aggregate = GridAggregateFunction.Sum;
                oGridFractura.MasterTableView.Columns.Add(oGridBoundColumn);

                oGridFractura.DataSource = dtFactura;
                idGrilla.Visible         = true;
                idGrilla.Controls.Add(oGridFractura);
                idBtnSave.Visible = true;

                ViewState["dtFactura"]         = dtFactura;
                ViewState["arrayMes"]          = arrayMes;
                ViewState["arrayReporteVenta"] = arrayReporteVenta;

                RadAjaxManager oRadAjaxManager = new RadAjaxManager();
                oRadAjaxManager.ClientEvents.OnRequestStart = "onRequestStart";

                AjaxUpdatedControl oAjaxUpdatedControl = new AjaxUpdatedControl();
                oAjaxUpdatedControl.ControlID = "rdGridFactura";

                AjaxSetting oAjaxSetting = new AjaxSetting();
                oAjaxSetting.AjaxControlID = "rdGridFactura";
                oAjaxSetting.UpdatedControls.Add(oAjaxUpdatedControl);

                oRadAjaxManager.AjaxSettings.Add(oAjaxSetting);
                idGrilla.Controls.Add(oRadAjaxManager);
            }
            oConn.Close();
        }