protected void LkBDelete_Click(object sender, EventArgs e)
    {
        // Obtenemos el Doc A Cambiar de Estado
        String val = HdFDeleteItem.Value.ToString();
        // TAMBIEN ANULAR SOLO LOS PEDIDOS PENDIENTES    04 = ANULADO
        String xstatu = HdFStatus.Value.ToString(); // TRAEMOS EL ESTADO

        tb_cxc_pedidocabBL BL = new tb_cxc_pedidocabBL();
        tb_cxc_pedidocab BE = new tb_cxc_pedidocab();
        DataTable dt = new DataTable();

        BE.tipdoc = val.ToString().Substring(0, 2);
        BE.serdoc = HdFSerdoc.Value.ToString();
        BE.numdoc = Equivalencias.Right(val.ToString().Trim(), 10);
        BE.usuar = Session["ssUsuar"].ToString().Trim();

        if (xstatu.Equals("11")) // 11 = PENDIENTE/NUEVO/EN EDICION
        {
            if (BL.Update_Estado(Session["ssEmpresaID"].ToString(), BE))
            {
                cargar_grilla();
                Response.Redirect("~/Comercial/Listados/Lst_PedidosPend.aspx");
            }
        }
    }
    protected void btn_BuscarProforma_Click(object sender, ImageClickEventArgs e)
    {
        tb_cxc_pedidocabBL BL = new tb_cxc_pedidocabBL();
        tb_cxc_pedidocab BE = new tb_cxc_pedidocab();
        DataTable dt = new DataTable();
        BE.tipdoc = txt_tipdoc.Text.ToUpper();
        BE.serdoc = txt_serdoc.Text.Trim();
        BE.numdoc = txt_numdoc.Text.PadLeft(10, '0');
        dt = BL.GetAll(Session["ssEmpresaID"].ToString(), BE).Tables[0];
        if (dt.Rows.Count > 0)
        {
            txt_numdoc.Text = dt.Rows[0]["numdoc"].ToString();
            txt_numdoc2.Text = dt.Rows[0]["numdoc"].ToString();
            cmb_tipventa.SelectedValue = dt.Rows[0]["tipventaid"].ToString();
            // cmb_tipventa.SelectedIndexChanged += new System.EventHandler(cmb_tipventa_SelectedIndexChanged);
            // Para Cargar el Combo PlazoDay
            // cmb_tipventa.AutoPostBack = true;
            fechdoc.Text = dt.Rows[0]["fechdoc"].ToString().Substring(0,10);
            fechentrega.Text = dt.Rows[0]["fechentrega"].ToString().Substring(0,10);
            txt_vendorid.Text = dt.Rows[0]["vendorid"].ToString();
            txt_vendorname.Text = dt.Rows[0]["vendorname"].ToString();
            txt_ctacte.Text = dt.Rows[0]["ctacte"].ToString();
            ValidaCliente(txt_ctacte.Text);
            txt_ctactename.Text = dt.Rows[0]["ctactename"].ToString();
            txt_ruc.Text = dt.Rows[0]["nmruc"].ToString();
            replegal_dni.Text = dt.Rows[0]["replegal_dni"].ToString();
            replegal_name.Text = dt.Rows[0]["replegal_name"].ToString();
            direc_entrega.Text = dt.Rows[0]["direc_entrega"].ToString();
            cmb_condventaid.SelectedValue = dt.Rows[0]["condventaid"].ToString();
            cmb_plazoday.SelectedValue = dt.Rows[0]["plazoday"].ToString();
            Double impb = Convert.ToDouble(dt.Rows[0]["impobruto"].ToString());
            txt_impobruto.Text = impb.ToString("##,###,##0.00");
            // Rangoid
            Double tdsct = Convert.ToDouble(dt.Rows[0]["tasadescto"].ToString());
            txt_tasadescto.Text = tdsct.ToString("##,###,##0.00");
            Double impn = Convert.ToDouble(dt.Rows[0]["imponeto"].ToString());
            txt_imponeto.Text = impn.ToString("##,###,##0.00");
            imponeto2.Text = impn.ToString("##,###,##0.00");
            cmb_moneda.SelectedValue = dt.Rows[0]["moneda"].ToString();
            // Validar el IGV
            if (Convert.ToBoolean(dt.Rows[0]["incluye_igv"]))
            {
                cmb_igv.Value = "S";
            }
            else
            {
                cmb_igv.Value = "N";
            }

            cmb_mediopagoid.Value = dt.Rows[0]["mediopagoid"].ToString();
            spe_numdocs.Text = dt.Rows[0]["numdocs"].ToString();
            if (dt.Rows[0]["aprob_status"].ToString() != "")
            {
               // aprob_status.SelectedValue = dt.Rows[0]["aprob_status"].ToString();
            }
            //aprob_obs.Text = dt.Rows[0]["aprob_obser"].ToString();
            //aprob_fech.Date = Convert.ToDateTime(dt.Rows[0]["aprob_fech"].ToString());

            //*************************************************** Ahora Vamos a Cargar los Detalles
            CargarDetalleProfor(txt_tipdoc.Text.ToUpper(), txt_serdoc.Text, txt_numdoc.Text.PadLeft(10, '0'));
            CargarDetalleCrono(txt_tipdoc.Text.ToUpper(), txt_serdoc.Text, txt_numdoc.Text.PadLeft(10, '0'));

            //Activar Los Botones Segun La Busqueda
            //Lock_or_Unlock_Boton("G", false, "_disabled");
            //Lock_or_Unlock_Boton("C", false, "_disabled");
            //Lock_or_Unlock_Boton("E", true, "");
            //Lock_or_Unlock_Boton("D", true, "");
            //Lock_or_Unlock_Boton("I", true, "");
        }
    }
    private void Rpt_Proforma()
    {
        try
        {
            //Se crea el documento PDF
            PdfDocument document = new PdfDocument();

            //Se configura los datos dela pagina
            document.Info.Title = "»» PROFORMA DE PEDIDOS ««";
            document.Info.Author = "HVR";

            //Se crea la pagina A4
            PdfPage page = document.AddPage();
            page.Orientation = PageOrientation.Portrait;

            Int16 intFila = 20;
            Int16 intColumna = 20;
            Int16 npag = 1;
            Int16 numFilas = 0;
            Int16 numColumna = 0;
            String imagenn = "";
            XFont font2;

            //Se crea el objeto dibujo
            XGraphics gfx = XGraphics.FromPdfPage(page);
            XFont font = new XFont("Times New Roman", 10, XFontStyle.Bold);
            XTextFormatter tf = new XTextFormatter(gfx);

            //fuente de texto
            string fuente = "MS Mincho";
            string fuente02 = "CCode39";
            string XTEXTO = "";
            //string fuente02 = "CCode39";

            //Se crea e tipo de letra
            try
            {
                font = new XFont(fuente, 10, XFontStyle.Regular, new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always));
            }
            catch (Exception exc)
            {
            }
            // XFont font = new XFont(fuente, 9, XFontStyle.Regular);
            XPen pen = new XPen(XColors.Navy, 1);
            string anio = Convert.ToString(DateTime.Now).Substring(6, 10);

            #region OBTENIENDO VALORES DE SESSION
            // DECLARANDO VARIBALES QUE RECIBIMOS DE SESSION
            string XEMPRESANAME = Session["ssEmpresaName"].ToString();
            string XEMPRESARUC = Session["ssEmpresaRuc"].ToString();
            string XGERENTEGENE = Session["ssGerenteGeneral"].ToString();
            string XGERENTEDNI = Session["ssGerenteNrodni"].ToString();
            string XPARTELECTRO = Session["ssPartElectro"].ToString();

            Double XIMPOBRUTO = 0, XIMPONETO = 0;
            string XNUMDOC = "", XSERDOC = "", XTASADESCTO = "", XPLAZODAY = "", XNUMDOCS = "", XMEDIOPAGO = "", XNDIAS = "",
                   XVENDORNAME = "", XCTACTENAME = "", XCLINMRUC = "", XDIREC = "", XREPLEGALNAME = "", XREPLEGALDNI = "",
                   XFECHA = "", XCO = "", XCR = "", XNMONE = "", XIGV = "";
            //VARIABLES RECIEN AGREGADAS EN LA BD
            string XREGPUBLICO = "", XPARTELECTRO2 = "", XESTCIVIL = "";

            tb_cxc_pedidocabBL BL = new tb_cxc_pedidocabBL();
            tb_cxc_pedidocab BE = new tb_cxc_pedidocab();
            DataTable TablaProfor = new DataTable();
            BE.tipdoc = Session["xtipdoc"].ToString();
            BE.serdoc = Session["xserdoc"].ToString();
            BE.numdoc = Session["xnumdoc"].ToString();
            TablaProfor = BL.GetAll2(Session["ssEmpresaID"].ToString(), BE).Tables[0];
            foreach (DataRow fila in TablaProfor.Rows)
            {
                XNUMDOC = fila["numdoc"].ToString();
                XSERDOC = fila["serdoc"].ToString();
                XTASADESCTO = fila["tasadescto"].ToString();
                XPLAZODAY = fila["plazoday"].ToString();
                XNUMDOCS = fila["numdocs"].ToString();
                XMEDIOPAGO = fila["descripcion"].ToString();
                XNDIAS = fila["ndias"].ToString();
                XVENDORNAME = fila["vendorname"].ToString();
                XCTACTENAME = fila["ctactename"].ToString();
                XCLINMRUC = fila["nmruc"].ToString();
                XDIREC = fila["direc"].ToString();
                XREPLEGALNAME = fila["replegal_name"].ToString();
                XREPLEGALDNI = fila["replegal_dni"].ToString();
                XFECHA = fila["fecha"].ToString();
                if (fila["condventaid"].ToString() == "02")
                    XCR = "X";
                else
                    XCO = "X";
                XNMONE = fila["precios"].ToString();
                XIGV = fila["incluye_igv"].ToString();
                XREGPUBLICO = fila["regpublico"].ToString();
                XPARTELECTRO2 = fila["partelectro"].ToString();
                XESTCIVIL = fila["estcivil"].ToString();
                XIMPOBRUTO = Convert.ToDouble(fila["impobruto"].ToString());
                XIMPONETO = Convert.ToDouble(fila["imponeto"].ToString());
            }
            #endregion

            #region HOJA 01
            font = new XFont("Arial", 11, XFontStyle.Underline);
            gfx.DrawString("CONTRATO DE COMPRA VENTA A CRÉDITO", font, XBrushes.Black, new XRect(intColumna + 170, intFila + 15, 10, 10), XStringFormats.TopLeft);

            font = new XFont("Arial", 9, XFontStyle.Bold);
            gfx.DrawString(("CONTRATO Nº " + XNUMDOC + "-VH/PIEERS-" + XSERDOC + ":").PadRight(25, ' '),
                  font, XBrushes.Black, new XRect(intColumna + 30, intFila + 40, 10, 10), XStringFormats.TopLeft);

            // Cabecera Cambia Deacuerdo al NUMERO DE RUC
            if (Equivalencias.Left(XCLINMRUC, 1) == "2")
            {
                #region PERSONA JURIDICA
                XTEXTO ="Conste por el presente documento, que celebran de una parte " + XEMPRESANAME + " con "+
                        "RUC No." + XEMPRESARUC + ", con domicilio legal en Av. Abancay No. 186 Cercado de Lima, representado "+
                        "por su Gerente General, Srta. " + XGERENTEGENE + ", identificada "+
                        "con DNI No." + XGERENTEDNI + "según poder inscrito en la partida No." + XPARTELECTRO + " del Registro de Personas "+
                        "Jurídicas de Lima y Callao, a quien en adelante se le denominará LA EMPRESA, y de otra parte la Empresa "+
                        "" + XCTACTENAME + " con RUC Nº " + XCLINMRUC + " "+
                        "con domicilio en JR." + XDIREC + " "+
                        "debidamente representado por su Gerente General, el Sr(a)." + XREPLEGALNAME + ", según poder Nº" + XPARTELECTRO2 + " "+
                        "inscritos en los Registros Públicos de " + XREGPUBLICO + " identificado con DNI Nº." + XREPLEGALDNI + ", a quien en adelante "+
                        "se le denominara EL CLIENTE en los términos y condiciones siguientes: ";
                #endregion
            }
            else
            {
                #region PERSONA NATURAL
                String estado = "";
                if (XESTCIVIL.ToString() == "1")
                {
                    estado = "CASADO";
                }
                else
                {
                    estado = "SOLTERO";
                }

                XTEXTO = "Conste por el presente documento, que celebran de una parte " + XEMPRESANAME + " con " +
                         "RUC No." + XEMPRESARUC + ", con domicilio legal en Av. Abancay No. 186 Cercado de Lima, representado " +
                         "por su Gerente General, Srta. " + XGERENTEGENE + ", identificada con DNI No." + XGERENTEDNI + " " +
                         "según poder inscrito en la partida No." + XPARTELECTRO + " del Registro de Personas Jurídicas de Lima y Callao, " +
                         "a quien en adelante se le denominará LA EMPRESA y de otra parte " +
                         "el Señor(a)." + XCTACTENAME.Trim() + " con DNI Nº." + Equivalencias.Mid(XCLINMRUC, 3, 8) + ", estado civil " + estado + " " +
                         "y domiciliado en " + XDIREC.Trim() + " a quien en adelante " +
                         "se le denominara EL CLIENTE en los términos y condiciones siguientes: ";
                #endregion
            }

            XRect rect = new XRect(intColumna + 30, intFila + 60, 500, 100);
            gfx.DrawRectangle(XBrushes.White, rect);
            tf.Alignment = XParagraphAlignment.Justify;
            tf.DrawString(XTEXTO, font, XBrushes.Black, rect, XStringFormats.TopLeft);

            /* DECLARANDO EL TEXTO 02 */
            gfx.DrawString(("PRIMERO.-  LA EMPRESA, es una Institución que se dedica a la confección y venta de prendas de vestir.").PadRight(19, ' '),
                  font, XBrushes.Black, new XRect(intColumna + 30, intFila + 170, 10, 10), XStringFormats.TopLeft);

            rect = new XRect(50, 210, 500, 100);
            gfx.DrawRectangle(XBrushes.White, rect);
            tf.Alignment = XParagraphAlignment.Justify;
            tf.DrawString("SEGUNDO.- Por el presente contrato, LA EMPRESA vende a EL CLIENTE los  productos de su fabricación que " +
                          "se detallan en el anexo 01 que forma parte del presente contrato.",font, XBrushes.Black, rect, XStringFormats.TopLeft);

            rect = new XRect(50, 240, 500, 100);
            gfx.DrawRectangle(XBrushes.White, rect);
            tf.Alignment = XParagraphAlignment.Justify;
            tf.DrawString("TERCERO.- LA EMPRESA y EL CLIENTE de común acuerdo establecen que la presente venta es al crédito "+
                          "con las siguientes condiciones comerciales: ", font, XBrushes.Black, rect, XStringFormats.TopLeft);

            gfx.DrawString("1)  El valor total de la venta, con el " + XTASADESCTO + "% de descuento es de S/. " + XIMPONETO.ToString("#,###,#00.00") + "(nuevos soles), incluido IGV.",
                font, XBrushes.Black, new XRect(intColumna + 30, intFila + 240, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString("2)  El plazo del crédito es de " + XPLAZODAY + " días ",
                font, XBrushes.Black, new XRect(intColumna + 30, intFila + 250, 10, 10), XStringFormats.TopLeft);

            rect = new XRect(intColumna + 30, intFila + 260, 500, 100);
            gfx.DrawRectangle(XBrushes.White, rect);
            tf.Alignment = XParagraphAlignment.Justify;
            tf.DrawString("3)  EL CLIENTE, pagará el crédito total con " + XNUMDOCS + " " + XMEDIOPAGO + " por los montos y fechas " +
                          "de vencimiento que se indican a continuación:", font, XBrushes.Black, rect, XStringFormats.TopLeft);

            //START RECTANGULO****************************************************************************************************************************************

            pen = new XPen(XColors.LightBlue, 5);
            gfx.DrawRoundedRectangle(pen, intColumna + 70, intFila + 300, 245, 100, 25, 25);

            // Pintamos los Datos de la Tabla de Session
            DataTable tabla01 = new DataTable();
            tabla01 = (DataTable)Session["Tabla01"];

            // AQUI DIBUJAMOS LAS CABECERAS DEL DETALLE
            font = new XFont("Abadi MT Condensed Light", 10, XFontStyle.Bold);
            gfx.DrawString(("DOC. DE PAGO").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 75, intFila + 305, 10, 10), XStringFormats.TopLeft);

            gfx.DrawString(("MONTO (SOLES)").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 166, intFila + 305, 10, 10), XStringFormats.TopLeft);

            gfx.DrawString(("FECHA VCTO.").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 250, intFila + 305, 10, 10), XStringFormats.TopLeft);

            //Trasamos las Lineas de la Cabecera
            pen = new XPen(XColors.LightBlue, 2);
            //pen.LineCap = XLineCap.Flat;
            gfx.DrawLine(pen, 92, 335, 335, 335);
            //gfx.DrawLine(XPens.Black, 92, 335, 335, 335);

            //CARGAMOS EL DETALLE
            int n = 15;
            foreach (DataRow fila in tabla01.Rows)
            {
                Double monto = Convert.ToDouble(fila["monto"].ToString());
                font = new XFont("Abadi MT Condensed Light", 9, XFontStyle.Bold);
                gfx.DrawString((fila["num_interno"].ToString()).PadRight(19, ' '),
                    font, XBrushes.Black, new XRect(intColumna + 75, intFila + 305 + n, 10, 10), XStringFormats.TopLeft);
                gfx.DrawString((monto.ToString("##,###,##0.00")).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(intColumna + 170, intFila + 305 + n, 10, 10), XStringFormats.TopLeft);
                gfx.DrawString(Equivalencias.Left((fila["fechven"].ToString()), 10),
                   font, XBrushes.Black, new XRect(intColumna + 250, intFila + 305 + n, 10, 10), XStringFormats.TopLeft);
                n = n + 10;
            }

            // PIE DEL RECTANGULO
            gfx.DrawLine(pen, 92, 405, 335, 405);
            gfx.DrawString(("TOTAL CREDITO :                   " + XIMPONETO.ToString("##,###,##0.00")).PadRight(19, ' '),
                    font, XBrushes.Black, new XRect(intColumna + 72, intFila + 385, 10, 10), XStringFormats.TopLeft);

            //END RECTANGULO *********************************************************************************************************************************************

            font = new XFont("Arial", 9, XFontStyle.Bold);

            rect = new XRect(intColumna + 30, intFila + 410, 500, 100);
            gfx.DrawRectangle(XBrushes.White, rect);
            tf.Alignment = XParagraphAlignment.Justify;
            tf.DrawString("4)  El pago de las letras se efectuara en el Banco de Crédito del Perú, para este efecto EL CLIENTE " +
                          "deberá mencionar el 'número único' de la respectiva letra de cambio.", font, XBrushes.Black, rect, XStringFormats.TopLeft);

            rect = new XRect(intColumna + 30, intFila + 430, 500, 100);
            gfx.DrawRectangle(XBrushes.White, rect);
            tf.Alignment = XParagraphAlignment.Justify;
            tf.DrawString("5) LA EMPRESA, efectuará el despacho de los productos a los " + XNDIAS + " días de haberse suscrito el presente " +
                          "contrato, en la agencia de transporte que indique EL CLIENTE.", font, XBrushes.Black, rect, XStringFormats.TopLeft);

            rect = new XRect(intColumna + 30, intFila + 450, 500, 100);
            gfx.DrawRectangle(XBrushes.White, rect);
            tf.Alignment = XParagraphAlignment.Justify;
            tf.DrawString("6) EL CLIENTE, asume los costos de transporte de los productos adquiridos siendo de su entera responsabilidad " +
                          "el traslado de los mismos desde la agencia de transporte hasta el lugar o local comercial de destino.", font, XBrushes.Black, rect, XStringFormats.TopLeft);

            rect = new XRect(intColumna + 30, intFila + 480, 500, 100);
            gfx.DrawRectangle(XBrushes.White, rect);
            tf.Alignment = XParagraphAlignment.Justify;
            tf.DrawString("CUARTO.- Si EL CLIENTE incumpliera con el pago de sus letras, LA EMPRESA podrá  ejecutar dichas letras " +
                          "por la vía legal.", font, XBrushes.Black, rect, XStringFormats.TopLeft);

            rect = new XRect(intColumna + 30, intFila + 510, 500, 100);
            gfx.DrawRectangle(XBrushes.White, rect);
            tf.Alignment = XParagraphAlignment.Justify;
            tf.DrawString("QUINTO.-  Si EL CLIENTE pagara el crédito total antes del plazo acordado, se actualizará  las tasas de " +
                          "descuento al plazo que corresponda mediante notas de crédito.", font, XBrushes.Black, rect, XStringFormats.TopLeft);

            rect = new XRect(intColumna + 30, intFila + 540, 500, 100);
            gfx.DrawRectangle(XBrushes.White, rect);
            tf.Alignment = XParagraphAlignment.Justify;
            tf.DrawString("SEXTO.- Si EL CLIENTE incumpliera con el pago total de los créditos concedidos en los plazos acordados, " +
                          "se actualizará las tasas de descuento a los plazos que corresponda mediante notas de débito.", font, XBrushes.Black, rect, XStringFormats.TopLeft);

            rect = new XRect(intColumna + 30, intFila + 570, 500, 100);
            gfx.DrawRectangle(XBrushes.White, rect);
            tf.Alignment = XParagraphAlignment.Justify;
            tf.DrawString("SÉPTIMO.- Si EL CLIENTE, por motivos injustificados, demore con realizar el pago total o lo que resta " +
                          "de su deuda por más de 90 días perderá el 50 % del descuento concedido.", font, XBrushes.Black, rect, XStringFormats.TopLeft);

            rect = new XRect(intColumna + 30, intFila + 600, 500, 100);
            gfx.DrawRectangle(XBrushes.White, rect);
            tf.Alignment = XParagraphAlignment.Justify;
            tf.DrawString("OCTAVO.- Las partes acuerdan que para todo lo relacionado con el cumplimiento de las cláusulas del " +
                          "presente contrato de  compra venta, se someten a la jurisdicción  de los jueces y tribunales de Lima, "+
                          "o mediante arbitraje, señalando sus domicilios en la parte introductoria del presente contrato.", font, XBrushes.Black, rect, XStringFormats.TopLeft);

            gfx.DrawString(("" + XFECHA + "").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 310, intFila + 660, 10, 10), XStringFormats.TopLeft);

            // VALIDAMOS PRIMERO QUE TIPO DE PERSONA ES NATURAL O  JURIDICA
            // 2 = JURIDICA
            // 1 = NATURAL
            if (Equivalencias.Left(XCLINMRUC, 1) == "2")
            {
                // VALIDAMOS EL ESTADO CIVIL DEL CLIENTE
                // 1 = CASADO
                // 2 = SOLTERO
                if (XESTCIVIL.ToString() == "1")
                {
                    gfx.DrawString(("LA EMPRESA").PadRight(19, ' '),
                        font, XBrushes.Black, new XRect(intColumna + 100, intFila + 700, 10, 10), XStringFormats.TopLeft);
                    gfx.DrawString(("EL CLIENTE").PadRight(19, ' '),
                        font, XBrushes.Black, new XRect(intColumna + 310, intFila + 700, 10, 10), XStringFormats.TopLeft);
                }
                else
                {
                    gfx.DrawString(("LA EMPRESA").PadRight(19, ' '),
                        font, XBrushes.Black, new XRect(intColumna + 100, intFila + 700, 10, 10), XStringFormats.TopLeft);
                    gfx.DrawString(("EL CLIENTE").PadRight(19, ' '),
                        font, XBrushes.Black, new XRect(intColumna + 310, intFila + 700, 10, 10), XStringFormats.TopLeft);
                }
            }

            if (Equivalencias.Left(XCLINMRUC, 1) == "1")
            {
                if (XESTCIVIL.ToString() == "1")
                {
                    gfx.DrawString(("LA EMPRESA").PadRight(19, ' '),
                        font, XBrushes.Black, new XRect(intColumna + 100, intFila + 700, 10, 10), XStringFormats.TopLeft);
                    gfx.DrawString(("EL CLIENTE").PadRight(19, ' '),
                        font, XBrushes.Black, new XRect(intColumna + 210, intFila + 700, 10, 10), XStringFormats.TopLeft);
                    gfx.DrawString(("EL CONYUGUE").PadRight(19, ' '),
                        font, XBrushes.Black, new XRect(intColumna + 320, intFila + 700, 10, 10), XStringFormats.TopLeft);
                }
                else
                {
                    gfx.DrawString(("LA EMPRESA").PadRight(19, ' '),
                        font, XBrushes.Black, new XRect(intColumna + 100, intFila + 700, 10, 10), XStringFormats.TopLeft);
                    gfx.DrawString(("EL CLIENTE").PadRight(19, ' '),
                        font, XBrushes.Black, new XRect(intColumna + 310, intFila + 700, 10, 10), XStringFormats.TopLeft);
                }
            }

            #endregion

            //SE CREA LA PAGINA A4 NUMERO 2
            PdfPage page2 = document.AddPage();
            page2.Orientation = PageOrientation.Portrait;
            XGraphics gfx2 = XGraphics.FromPdfPage(page2);
            tf = new XTextFormatter(gfx2);

            font = new XFont("Arial", 9.5, XFontStyle.Bold);
            gfx2.DrawString("ANEXO 01", font, XBrushes.Black, new XRect(intColumna + 260, intFila + 15, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString("DETALLE DE PRODUCTOS VENDIDOS", font, XBrushes.Black, new XRect(intColumna + 200, intFila + 25, 10, 10), XStringFormats.TopLeft);

            font = new XFont("Arial", 10, XFontStyle.Bold);
            font2 = new XFont("Arial", 9);
            gfx2.DrawString("VENDEDOR", font, XBrushes.Black, new XRect(intColumna + 30, intFila + 50, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(": " + XVENDORNAME + "", font2, XBrushes.Black, new XRect(intColumna + 90, intFila + 50, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString("CLIENTE", font, XBrushes.Black, new XRect(intColumna + 30, intFila + 60, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(": " + XCTACTENAME + "", font2, XBrushes.Black, new XRect(intColumna + 90, intFila + 60, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString("RUC", font, XBrushes.Black, new XRect(intColumna + 30, intFila + 70, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(": " + XCLINMRUC + "", font2, XBrushes.Black, new XRect(intColumna + 90, intFila + 70, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString("DIRECC", font, XBrushes.Black, new XRect(intColumna + 30, intFila + 80, 10, 10), XStringFormats.TopLeft);
            //gfx2.DrawString(": " + XDIREC + "", font2, XBrushes.Black, new XRect(intColumna + 90, intFila + 90, 10, 10), XStringFormats.TopLeft);

            rect = new XRect(intColumna + 90, intFila + 80, 430, 100);
            gfx2.DrawRectangle(XBrushes.White, rect);
            tf.Alignment = XParagraphAlignment.Justify;
            tf.DrawString(": " + XDIREC + "", font2, XBrushes.Black, rect, XStringFormats.TopLeft);

            gfx2.DrawString("NUM-CONTRATO", font, XBrushes.Black, new XRect(intColumna + 30, intFila + 100, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(": " + XNUMDOC + "", font2, XBrushes.Black, new XRect(intColumna + 120, intFila + 100, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString("COND-VENTA : ", font, XBrushes.Black, new XRect(intColumna + 30, intFila + 110, 10, 10), XStringFormats.TopLeft);

            // DIBUJAR UN RECTANGULO
            pen = new XPen(XColors.LightBlue, 2);
            gfx2.DrawRoundedRectangle(pen, intColumna + 57, intFila + 125, 90, 30, 25, 25);
            gfx2.DrawString("CONTADO", font, XBrushes.Black, new XRect(intColumna + 60, intFila + 128, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(": " + XCO + "", font2, XBrushes.Black, new XRect(intColumna + 120, intFila + 128, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString("CREDITO", font, XBrushes.Black, new XRect(intColumna + 60, intFila + 139, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(": " + XCR + "", font2, XBrushes.Black, new XRect(intColumna + 120, intFila + 139, 10, 10), XStringFormats.TopLeft);

            gfx2.DrawString("PLAZO EN DIAS", font, XBrushes.Black, new XRect(intColumna + 150, intFila + 139, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(": " + XPLAZODAY + " DIAS", font2, XBrushes.Black, new XRect(intColumna + 240, intFila + 139, 10, 10), XStringFormats.TopLeft);

            gfx2.DrawString("VENTA TOTAL A PRECIO DE LISTA", font, XBrushes.Black, new XRect(intColumna + 30, intFila + 160, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(": " + XIMPOBRUTO.ToString("##,###,##0.00") + "", font2, XBrushes.Black, new XRect(intColumna + 240, intFila + 160, 10, 10), XStringFormats.TopLeft);

            gfx2.DrawString("TASA DE DESCUENTO", font, XBrushes.Black, new XRect(intColumna + 30, intFila + 170, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(": " + XTASADESCTO + "%", font2, XBrushes.Black, new XRect(intColumna + 240, intFila + 170, 10, 10), XStringFormats.TopLeft);

            gfx2.DrawString("VENTA TOTAL CON DESCUENTO", font, XBrushes.Black, new XRect(intColumna + 30, intFila + 180, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(": " + XIMPONETO.ToString("##,###,##0.00") + "", font2, XBrushes.Black, new XRect(intColumna + 240, intFila + 180, 10, 10), XStringFormats.TopLeft);

            gfx2.DrawString("PRECIOS DE LOS PRODUCTOS", font, XBrushes.Black, new XRect(intColumna + 30, intFila + 190, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(": " + XNMONE + "", font2, XBrushes.Black, new XRect(intColumna + 240, intFila + 190, 10, 10), XStringFormats.TopLeft);

            gfx2.DrawString("LOS PRECIOS INCLUYEN IGV", font, XBrushes.Black, new XRect(intColumna + 30, intFila + 200, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(": " + XIGV + "", font2, XBrushes.Black, new XRect(intColumna + 240, intFila + 200, 10, 10), XStringFormats.TopLeft);

            //START RECTANGULO II ****************************************************************************************************************************************

            pen = new XPen(XColors.LightBlue, 3);
            //gfx2.DrawRoundedRectangle(pen, intColumna + 0, intFila + 220, 550, 40, 25, 25);

            // Pintamos los Datos de la Tabla de Session
            DataTable tabla02 = new DataTable();
            tabla02 = (DataTable)Session["Tabla02"];

            // AQUI DIBUJAMOS LAS CABECERAS DEL DETALLE
            font = new XFont("Abadi MT Condensed Light", 8.5, XFontStyle.Bold);
            gfx2.DrawString(("CODIGO").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 5, intFila + 228, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(("MARCA").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 35, intFila + 228, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(("PRODUCTO").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 80, intFila + 228, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(("COLOR").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 250, intFila + 228, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(("TALLA").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 295, intFila + 228, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(("CANTIDAD").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 318, intFila + 228, 10, 10), XStringFormats.TopLeft);

            gfx2.DrawString(("PRECIOS DE").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 350, intFila + 225, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(("LISTA").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 358, intFila + 235, 10, 10), XStringFormats.TopLeft);

            gfx2.DrawString(("VENTAS A").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 400, intFila + 225, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(("PRECIOS DE LISTA").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 390, intFila + 235, 10, 10), XStringFormats.TopLeft);

            gfx2.DrawString(("PRECIOS CON").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 450, intFila + 225, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(("DESCUENTO").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 451, intFila + 235, 10, 10), XStringFormats.TopLeft);

            gfx2.DrawString(("VENTAS CON").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 500, intFila + 225, 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(("DESCUENTO").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 501, intFila + 235, 10, 10), XStringFormats.TopLeft);

            pen = new XPen(XColors.LightBlue, 2);
            gfx2.DrawLine(pen, 20, 265, 570, 265);

            //CARGAMOS EL DETALLE
            int n2 = 15;
            int nfilas = 40;
            foreach (DataRow fila in tabla02.Rows)
            {
                Double precbruto = Convert.ToDouble(fila["precbruto"].ToString());
                Double impobruto = Convert.ToDouble(fila["impobruto"].ToString());
                Double precneto = Convert.ToDouble(fila["precneto"].ToString());
                Double imponeto = Convert.ToDouble(fila["imponeto"].ToString());

                font = new XFont("Abadi MT Condensed Light", 7.5, XFontStyle.Bold);
                gfx2.DrawString((fila["articidold"].ToString()).PadRight(19, ' '),
                    font, XBrushes.Black, new XRect(intColumna + 5, intFila + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx2.DrawString((fila["marcaname"].ToString()).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(intColumna + 35, intFila + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx2.DrawString((fila["articname"].ToString()).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(intColumna + 80, intFila + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx2.DrawString((fila["colorname"].ToString()).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(intColumna + 250, intFila + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx2.DrawString((fila["talla"].ToString()).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(intColumna + 299, intFila + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx2.DrawString((fila["cantidad"].ToString()).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(intColumna + 318, intFila + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx2.DrawString((precbruto.ToString("##,###,##0.00")).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(intColumna + 358, intFila + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx2.DrawString((impobruto.ToString("##,###,##0.00")).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(intColumna + 400, intFila + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx2.DrawString((precneto.ToString("##,###,##0.00")).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(intColumna + 458, intFila + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx2.DrawString((imponeto.ToString("##,###,##0.00")).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(intColumna + 508, intFila + 235 + n2, 10, 10), XStringFormats.TopLeft);
                n2 = n2 + 10;
                nfilas = nfilas + 12;
            }

            //nfilas = nfilas;

            gfx2.DrawRoundedRectangle(pen, intColumna + 0, intFila + 220, 550, nfilas, 25, 25);
            //PIE DEL RECTANGULO
            gfx2.DrawLine(pen, 20, intFila + 220 + (nfilas - 12), 570, intFila + 220 + (nfilas - 12));
            font = new XFont("Abadi MT Condensed Light", 8, XFontStyle.Bold);
            gfx2.DrawString(("TOTAL").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 80, intFila + 220 + (nfilas - 10), 10, 10), XStringFormats.TopLeft);

            gfx2.DrawString((XIMPOBRUTO.ToString("##,###,##0.00")).PadRight(19, ' '),
                font, XBrushes.Black, new XRect(intColumna + 400, intFila + 220 + (nfilas - 10), 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString((XIMPONETO.ToString("##,###,##0.00")).PadRight(19, ' '),
               font, XBrushes.Black, new XRect(intColumna + 508, intFila + 220 + (nfilas - 10), 10, 10), XStringFormats.TopLeft);

            //END RECTANGULO *********************************************************************************************************************************************

            gfx2.DrawString(("FIRMA GERENCIA GENERAL").PadRight(19, ' '),
               font, XBrushes.Black, new XRect(intColumna + 80, intFila + 220 + (nfilas + 30), 10, 10), XStringFormats.TopLeft);
            gfx2.DrawString(("FIRMA CLIENTE").PadRight(19, ' '),
               font, XBrushes.Black, new XRect(intColumna + 295, intFila + 220 + (nfilas + 30), 10, 10), XStringFormats.TopLeft);

            //Se envia el PDF al browser
            MemoryStream stream = new MemoryStream();
            document.Save(stream, false);
            Response.Clear();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-length", stream.Length.ToString());
            Response.BinaryWrite(stream.ToArray());
            Response.Flush();
            stream.Close();
            Response.End();

        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
    }
    private void Update()
    {
        try
        {
            var BL = new tb_cxc_pedidocabBL();
            var BE = new tb_cxc_pedidocab();

            var Detalle = new tb_cxc_pedidocab.Item();
            var ListaItems = new List<tb_cxc_pedidocab.Item>();

            var Detalle2 = new tb_cxc_pedidocab.Crono();
            var ListaCrono = new List<tb_cxc_pedidocab.Crono>();

            #region Datos Cabecera
            BE.tipdoc = txt_tipdoc.Text;
            BE.serdoc = txt_serdoc.Text;
            BE.numdoc = txt_numdoc.Text;

            BE.canalventaid = Session["ssCanalVentaId"].ToString();
            BE.tipventaid = cmb_tipventa.SelectedValue.ToString();
            BE.fechdoc = Convert.ToDateTime(fechdoc.Text);
            BE.fechentrega = Convert.ToDateTime(fechentrega.Text);
            BE.vendorid = txt_vendorid.Text;
            BE.ctacte = txt_ctacte.Text;
            BE.replegal_dni = replegal_dni.Text;
            BE.replegal_name = replegal_name.Text;
            BE.direc_entrega = direc_entrega.Text;
            BE.condventaid = cmb_condventaid.SelectedValue.ToString();
            BE.plazoday = Convert.ToInt32(cmb_plazoday.SelectedValue.ToString());
            BE.impobruto = Convert.ToDecimal(txt_impobruto.Text);
            BE.rangoid = "05";// *****************************************************************
            BE.tasadescto = Convert.ToDecimal(txt_tasadescto.Text);
            BE.imponeto = Convert.ToDecimal(txt_imponeto.Text);
            BE.moneda = cmb_moneda.SelectedValue.ToString();
            if (cmb_igv.Value.ToString() == "S")
            {
                BE.incluye_igv = true;
            }
            else
            {
                BE.incluye_igv = false;
            }
            BE.observacion = txt_obs.Text;
            BE.mediopagoid = cmb_mediopagoid.Value.ToString();
            BE.numdocs = Convert.ToInt32(spe_numdocs.Text);
            BE.aprob_status = "11";
            //BE.aprob_obser = aprob_obs.Text;
            //if (aprob_fech.Text.Length > 0)
            //{ BE.aprob_fech = Convert.ToDateTime(aprob_fech.Text); }
            BE.usuar = Session["ssUsuar"].ToString();
            #endregion

            #region Datos Detalle
            TablaDetalle = (DataTable)Session["TablaDetalle"];
            foreach (DataRow fila in TablaDetalle.Rows)
            {
                Detalle = new tb_cxc_pedidocab.Item();
                Detalle.tipdoc = txt_tipdoc.Text;
                Detalle.serdoc = txt_serdoc.Text;
                Detalle.numdoc = txt_numdoc.Text;
                Detalle.articid = fila["articid"].ToString();
                Detalle.articidold = fila["articidold"].ToString();
                Detalle.colorid = fila["colorid"].ToString();
                Detalle.tallaid = fila["tallaid"].ToString();
                Detalle.coltall = fila["talla"].ToString();
                Detalle.cantidad = Convert.ToDecimal(fila["cantidad"].ToString());
                Detalle.precbruto = Convert.ToDecimal(fila["precbruto"].ToString());
                Detalle.impobruto = Convert.ToDecimal(fila["impobruto"].ToString());
                Detalle.precneto = Convert.ToDecimal(fila["precneto"].ToString());
                Detalle.imponeto = Convert.ToDecimal(fila["imponeto"].ToString());
                Detalle.usuar = Session["ssUsuar"].ToString().Trim();
                Detalle.canalventaid = Session["ssCanalVentaId"].ToString();
                Detalle.local = Session["ssLocal"].ToString();

                if (Convert.ToDecimal(fila["imponeto"].ToString()) > 0)
                {
                    ListaItems.Add(Detalle);
                }
                else
                {
                    ClientMessage("DOCUMENTO DETALLE PROFORMA EN FORMATO INCORRECTO ...!!!");
                    return;
                }
            }
            BE.ListaItems = ListaItems;
            #endregion

            #region Datos Detalle CronoGramaPagos
            TablaCronoPagos = (DataTable)Session["TablaCronoPagos"];
            foreach (DataRow fila in TablaCronoPagos.Rows)
            {
                Detalle2 = new tb_cxc_pedidocab.Crono();
                Detalle2.tipdoc = txt_tipdoc.Text;
                Detalle2.serdoc = txt_serdoc.Text;
                Detalle2.numdoc = txt_numdoc.Text;
                Detalle2.mediopagoid = cmb_mediopagoid.Value.ToString();
                Detalle2.item = fila["item"].ToString();
                Detalle2.num_interno = fila["num_interno"].ToString();
                Detalle2.num_unico = fila["num_unico"].ToString();
                Detalle2.monto = Convert.ToDecimal(fila["monto"].ToString());
                String xfecha = Equivalencias.Left(fila["fechven"].ToString(), 10);
                Detalle2.fechven = Convert.ToDateTime(xfecha);
                Detalle2.usuar = Session["ssUsuar"].ToString().Trim();

                if (Convert.ToDecimal(fila["monto"].ToString()) > 0)
                {
                    ListaCrono.Add(Detalle2);
                }
                else
                {
                    ClientMessage("DOCUMENTO DETALLE CRONOGRAMA PAGOS EN FORMATO INCORRECTO ...!!!");
                    return;
                }
            }
            BE.ListaCrono = ListaCrono;
            #endregion

            // PASAR A LLAMAR AL METODO DE INSERCION DE PROFORMA
            if (BL.Update(Session["ssEmpresaID"].ToString(), BE))
            {
                //ClientMessage("Registros Modificados Correctamente !!!");
                MensajeQuery2("Registros Modificados Correctamente !!!");

                LimpiarDocumento();
                CargarCorrelativoDoc();
                Lock_or_Unlock_Boton("N", true, "");
                Lock_or_Unlock_Boton("C", false, "_disabled");
                Lock_or_Unlock_Boton("G", false, "_disabled");
                Lock_or_UnLock_Paneles("01", false, "fondo_bloqueo", "_disabled");
                Lock_or_UnLock_Paneles("02", false, "fondo_bloqueo", "_disabled");
                Lock_or_UnLock_Paneles("03", false, "fondo_bloqueo", "_disabled");

                btn_Imprimir.Enabled = true;
                btn_Imprimir.CssClass = "btn btn-primary";
                //btn_grabar.Enabled = false;
                //btn_grabar.CssClass = "btn btn-default";
            }
        }
        catch (Exception ex)
        {
            //ClientMessage(ex.Message);
            Response.Redirect("ErrorPage/Frm_PaginaError_405.aspx");
        }
    }
    private void Insert()
    {
        try
        {
            var BL = new tb_cxc_pedidocabBL();
            var BE = new tb_cxc_pedidocab();

            var Detalle = new tb_cxc_pedidocab.Item();
            var ListaItems = new List<tb_cxc_pedidocab.Item>();

            var Detalle2 = new tb_cxc_pedidocab.Crono();
            var ListaCrono = new List<tb_cxc_pedidocab.Crono>();

            #region Datos Cabecera
            BE.tipdoc = txt_tipdoc.Text;
            BE.serdoc = txt_serdoc.Text;
            BE.numdoc = txt_numdoc.Text;
            BE.canalventaid = Session["ssCanalVentaId"].ToString();
            BE.tipventaid = cmb_tipventa.SelectedValue.ToString();
            BE.fechdoc = Convert.ToDateTime(fechdoc.Text);
            BE.fechentrega = Convert.ToDateTime(fechentrega.Text);
            BE.vendorid = txt_vendorid.Text;
            BE.ctacte = txt_ctacte.Text;
            BE.direcnume = "";
            BE.replegal_dni = replegal_dni.Text;
            BE.replegal_name = replegal_name.Text;
            BE.direc_entrega = direc_entrega.Text;
            BE.condventaid = cmb_condventaid.SelectedValue.ToString();
            BE.plazoday = Convert.ToInt32(cmb_plazoday.SelectedValue.ToString());
            BE.impobruto = Convert.ToDecimal(txt_impobruto.Text);
            BE.rangoid = "05";// *****************************************************************
            BE.tasadescto = Convert.ToDecimal(txt_tasadescto.Text);
            BE.imponeto = Convert.ToDecimal(txt_imponeto.Text);
            BE.moneda = cmb_moneda.SelectedValue.ToString();
            if (cmb_igv.Value.ToString() == "S")
            {
                BE.incluye_igv = true;
            }
            else
            {
                BE.incluye_igv = false;
            }
            BE.observacion = txt_obs.Text;
            BE.mediopagoid = cmb_mediopagoid.Value.ToString();
            BE.numdocs = Convert.ToInt32(spe_numdocs.Text);
            BE.aprob_status = AproStatus.ToString() ;//"11";
            //BE.aprob_obser = aprob_obs.Text;
            //if (aprob_fech.Text.Length > 0)
            //{ BE.aprob_fech = Convert.ToDateTime(aprob_fech.Text); }
            BE.usuar = Session["ssUsuar"].ToString().Trim();
            BE.local = Session["ssLocal"].ToString().Trim();
            BE.moduloid = Session["ssModuloId"].ToString().Trim();

            String xmod = Session["ssModo"].ToString();

            switch (Session["ssModo"].ToString())
            {
                case "NEW": BE.accion = "INS"; break;
                case "EDI": BE.accion = "ALT"; break;
                case "DEL": BE.accion = "DEL"; break;
            }
            #endregion

            #region Datos Detalle
            TablaDetalle = (DataTable)Session["TablaDetalle"];
            foreach (DataRow fila in TablaDetalle.Rows)
            {
                Detalle = new tb_cxc_pedidocab.Item();
                Detalle.tipdoc = txt_tipdoc.Text;
                Detalle.serdoc = txt_serdoc.Text;
                Detalle.numdoc = txt_numdoc.Text;

                Detalle.articid = fila["articid"].ToString();
                Detalle.articidold = fila["articidold"].ToString();
                Detalle.colorid = fila["colorid"].ToString();
                Detalle.tallaid = fila["tallaid"].ToString();
                Detalle.coltall = Equivalencias.Right(fila["coltall"].ToString(), 2);
                Detalle.cantidad = Convert.ToDecimal(fila["cantidad"].ToString());
                Detalle.precbruto = Convert.ToDecimal(fila["precbruto"].ToString());
                Detalle.impobruto = Convert.ToDecimal(fila["impobruto"].ToString());
                Detalle.precneto = Convert.ToDecimal(fila["precneto"].ToString());
                Detalle.imponeto = Convert.ToDecimal(fila["imponeto"].ToString());
                Detalle.usuar = Session["ssUsuar"].ToString().Trim();
                Detalle.canalventaid = Session["ssCanalVentaId"].ToString();
                Detalle.local = Session["ssLocal"].ToString();

                if (Convert.ToDecimal(fila["imponeto"].ToString()) > 0)
                {
                    ListaItems.Add(Detalle);
                }
                else
                {
                    ClientMessage("DOCUMENTO DETALLE PROFORMA EN FORMATO INCORRECTO ...!!!");
                    return;
                }
            }
            BE.ListaItems = ListaItems;
            #endregion

            #region Datos Detalle CronoGramaPagos
            TablaCronoPagos = (DataTable)Session["TablaCronoPagos"];
            foreach (DataRow fila in TablaCronoPagos.Rows)
            {
                Detalle2 = new tb_cxc_pedidocab.Crono();
                Detalle2.tipdoc = txt_tipdoc.Text;
                Detalle2.serdoc = txt_serdoc.Text;
                Detalle2.numdoc = txt_numdoc.Text;
                Detalle2.mediopagoid = cmb_mediopagoid.Value.ToString();
                Detalle2.item = fila["item"].ToString();
                Detalle2.num_interno = fila["num_interno"].ToString();
                Detalle2.num_unico = fila["num_unico"].ToString();
                Detalle2.monto = Convert.ToDecimal(fila["monto"].ToString());
                String xfecha = Equivalencias.Left(fila["fechven"].ToString(), 10);
                Detalle2.fechven = Convert.ToDateTime(xfecha);
                Detalle2.usuar = Session["ssUsuar"].ToString().Trim();

                if (Convert.ToDecimal(fila["monto"].ToString()) > 0)
                {
                    ListaCrono.Add(Detalle2);
                }
                else
                {
                    ClientMessage("DOCUMENTO DETALLE CRONOGRAMA PAGOS EN FORMATO INCORRECTO ...!!!");
                    return;
                }
            }
            BE.ListaCrono = ListaCrono;
            #endregion

            #region Inserción de Datos en Tabla Stock diario
            var BLStk = new tb_me_stockdiariocabBL();
            var BEStk = new tb_me_stockdiariocab();
            //var BLStk = new tb_cxc_pedidocabBL();
            //var BEStk = new tb_cxc_pedidocab();

            //switch (Session["ssModo"].ToString())
            //{
            //    case "NEW": BE.accion = "INS"; break;
            //    case "EDIT": BE.accion = "ALT"; break;
            //    case "DEL": BE.accion = "DEL"; break;
            //}

            //BEStk.almacaccionid = "21";
            #endregion

            #region Inserción de Datos en Tabla Stock Diario Detalle
            decimal canti = 0;
            var DetalleStk = new tb_cxc_pedidocab.ItemsStkDeta();
            var ListaStkItems = new List<tb_cxc_pedidocab.ItemsStkDeta>();
            TablaDetalleStk = (DataTable)Session["TablaDetalle"];
            foreach (DataRow fila in TablaDetalleStk.Rows)
            {
                DetalleStk = new tb_cxc_pedidocab.ItemsStkDeta();
                DetalleStk.fecha = Convert.ToDateTime(DateTime.Today.ToString()); //Convert.ToDateTime(Session["bd_fecha"].ToString());
                DetalleStk.articid = fila["articid"].ToString();
                DetalleStk.colorid = fila["colorid"].ToString();
                DetalleStk.coltall = Equivalencias.Right(fila["coltall"].ToString(), 2);
                DetalleStk.almacaccionid = "21";
                DetalleStk.tipodoc = txt_tipdoc.Text;
                DetalleStk.serdoc = txt_serdoc.Text;
                DetalleStk.numdoc = txt_numdoc.Text;
                canti = Convert.ToDecimal(fila["cantidad"].ToString());
                DetalleStk.cantidad = canti;
                DetalleStk.ctacte = txt_ctacte.Text;
                DetalleStk.direcnume = Session["direcnume"].ToString();//direc_entrega.Text;
                DetalleStk.tipoperacionid = "10";
                DetalleStk.tiporef = txt_tipdoc.Text;
                DetalleStk.serref = txt_serdoc.Text;
                DetalleStk.numref = txt_numdoc.Text;
                DetalleStk.estado = 1;
                DetalleStk.atendido = 0;
                DetalleStk.vendorid = txt_vendorid.Text;
                DetalleStk.usuar = Session["ssUsuar"].ToString().Trim();
                DetalleStk.canalventaid = Session["ssCanalVentaId"].ToString();
                DetalleStk.local = Session["ssLocal"].ToString();
                DetalleStk.dominioid = Session["ssDominioid"].ToString();
                DetalleStk.moduloid = Session["ssModuloid"].ToString();

                if (TablaDetalle.Rows.Count > 0)
                {
                    ListaStkItems.Add(DetalleStk);
                }
                else
                {
                    ClientMessage("DOCUMENTO DETALLE INCORRECTO ...!!!");
                    return;
                }
            }
            BE.listaItemsStkDeta = ListaStkItems;
            #endregion

            //BLStk.InsertStk(Session["ssEmpresaID"].ToString(), BEStk);

            // PASAR A LLAMAR AL METODO DE INSERCION DE PEDIDO
            if (BL.Insert(Session["ssEmpresaID"].ToString(), BE))
            {
                ClientMessage("Registros Grabados Correctamente !!!");
                LimpiarDocumento();
                CargarCorrelativoDoc();

                //Lock_or_Unlock_Boton("N", true, "");
                //Lock_or_Unlock_Boton("C", false, "_disabled");
                //Lock_or_Unlock_Boton("G", false, "_disabled");
                //Lock_or_UnLock_Paneles("01", false, "fondo_bloqueo", "_disabled");
                //Lock_or_UnLock_Paneles("02", false, "fondo_bloqueo", "_disabled");
                //Lock_or_UnLock_Paneles("03", false, "fondo_bloqueo", "_disabled");

                btn_Imprimir.Enabled = true;
                btn_Imprimir.CssClass = "btn btn-primary";
                //btn_grabar.Enabled = false;
                //btn_grabar.CssClass = "btn btn-default";
            }
        }
        catch (Exception ex)
        {
            //ClientMessage(ex.Message);
            Response.Redirect("ErrorPage/Frm_PaginaError_405.aspx");
        }
    }
    private void CargarCorrelativoDoc()
    {
        tb_cxc_pedidocabBL BL = new tb_cxc_pedidocabBL();
        tb_cxc_pedidocab BE = new tb_cxc_pedidocab();
        DataTable dt = new DataTable();

        BE.moduloid = Session["ssModuloid"].ToString();
        BE.tipdoc = "PD";
        BE.local = Session["ssLocal"].ToString();
        BE.canalventaid = Session["ssCanalVentaId"].ToString();

        dt = BL.GetNewNumDoc(Session["ssEmpresaID"].ToString(), BE).Tables[0];
        if (dt.Rows.Count > 0)
        {
            txt_tipdoc.Text = dt.Rows[0]["tipodoc"].ToString();
            txt_serdoc.Text = dt.Rows[0]["serdoc"].ToString();
            txt_numdoc.Text = dt.Rows[0]["numero"].ToString();
            txt_proforma.Text = dt.Rows[0]["tipodoc"].ToString() + "-" + dt.Rows[0]["numero"].ToString();
            //txt_proforma.Text = dt.Rows[0]["tipodoc"].ToString() + "-" + dt.Rows[0]["serdoc"].ToString() + (Equivalencias.Right(dt.Rows[0]["numero"].ToString(), 4)).PadLeft(6, '0');

            txt_tipdoc2.Text = dt.Rows[0]["tipodoc"].ToString();
            txt_serdoc2.Text = dt.Rows[0]["serdoc"].ToString();
            txt_numdoc2.Text = dt.Rows[0]["numero"].ToString();
            txt_proforma2.Text = dt.Rows[0]["tipodoc"].ToString() + "-" + dt.Rows[0]["numero"].ToString();
            //txt_proforma2.Text = dt.Rows[0]["tipodoc"].ToString() + "-" + dt.Rows[0]["serdoc"].ToString() + (Equivalencias.Right(dt.Rows[0]["numero"].ToString(), 4)).PadLeft(6,'0');
        }
    }
    private void Rpt_Pedido()
    {
        try
        {
            //Se crea el documento PDF
            PdfDocument document = new PdfDocument();

            //Se configura los datos dela pagina
            document.Info.Title = "»» PEDIDO ««";
            document.Info.Author = "HVR";

            //Se crea la pagina A4
            PdfPage page = document.AddPage();
            page.Orientation = PageOrientation.Portrait;

            //Se crea el objeto dibujo
            XGraphics gfx = XGraphics.FromPdfPage(page);
            XPen pen = new XPen(XColors.Navy, 1);
            string anio = Convert.ToString(DateTime.Now).Substring(6, 10);
            XRect rect;
            XFont fontB18 = new XFont("Abadi MT Condensed Light", 20, XFontStyle.Bold);
            XFont fontB12 = new XFont("Abadi MT Condensed Light", 12, XFontStyle.Bold);
            XFont fontB9 = new XFont("Abadi MT Condensed Light", 9, XFontStyle.Bold);
            XFont font9 = new XFont("Abadi MT Condensed Light", 9);
            XFont font; XFont font2;
            XTextFormatter tf = new XTextFormatter(gfx);

            //define margins
            double leftMargin = 40.0;
            double rightMargin = 570.0;
            double topMargin = 20.0;
            double lineTop = 0d;
            double lineFooter = 780d;
            double rowStep = 12d;
            double newRow = topMargin + rowStep;
            double beginRow = newRow;
            double currentRow = beginRow;
            double currentRowCol2 = 0;
            double firstCustomerRow = 100d;
            double txtShift = 8d;
            string hcString = string.Empty; //---added 6-26-13
            string sim1 = "[";
            string sim2 = "]";
            string space = "   ";

            #region OBTENIENDO VALORES DE SESSION
            // DECLARANDO VARIBALES QUE RECIBIMOS DE SESSION
            string XEMPRESANAME = Session["ssEmpresaName"].ToString();
            string XEMPRESARUC = Session["ssEmpresaRuc"].ToString();
            string XGERENTEGENE = Session["ssGerenteGeneral"].ToString();
            string XGERENTEDNI = Session["ssGerenteNrodni"].ToString();
            string XPARTELECTRO = Session["ssPartElectro"].ToString();

            Double XIMPOBRUTO = 0, XIMPONETO = 0;
            string XNUMDOC = "", XSERDOC = "", XTASADESCTO = "", XPLAZODAY = "", XNUMDOCS = "", XMEDIOPAGO = "", XNDIAS = "",
                   XVENDORNAME = "", XCTACTENAME = "", XCLINMRUC = "", XDIREC = "", XDIRECENTREGA = "", XREPLEGALNAME = "", XREPLEGALDNI = "",
                   XFECHA = "", XCO = "", XCR = "", XNMONE = "", XIGV = "";
            //VARIABLES RECIEN AGREGADAS EN LA BD
            string XREGPUBLICO = "", XPARTELECTRO2 = "", XESTCIVIL = "";

            tb_cxc_pedidocabBL BL = new tb_cxc_pedidocabBL();
            tb_cxc_pedidocab BE = new tb_cxc_pedidocab();
            DataTable TablaProfor = new DataTable();
            BE.tipdoc = Session["xtipdoc"].ToString();
            BE.serdoc = Session["xserdoc"].ToString();
            BE.numdoc = Session["xnumdoc"].ToString();
            TablaProfor = BL.GetAll2(Session["ssEmpresaID"].ToString(), BE).Tables[0];
            foreach (DataRow fila in TablaProfor.Rows)
            {
                XNUMDOC = fila["numdoc"].ToString();
                XSERDOC = fila["serdoc"].ToString();
                XTASADESCTO = fila["tasadescto"].ToString();
                XPLAZODAY = fila["plazoday"].ToString();
                XNUMDOCS = fila["numdocs"].ToString();
                XMEDIOPAGO = fila["descripcion"].ToString();
                XNDIAS = fila["ndias"].ToString();
                XVENDORNAME = fila["vendorname"].ToString();
                XCTACTENAME = fila["ctactename"].ToString();
                XCLINMRUC = fila["nmruc"].ToString();
                XDIREC = fila["direc"].ToString();
                XDIRECENTREGA = fila["direc_entrega"].ToString();
                XREPLEGALNAME = fila["replegal_name"].ToString();
                XREPLEGALDNI = fila["replegal_dni"].ToString();
                XFECHA = fila["fechdoc"].ToString();
                if (fila["condventaid"].ToString() == "02")
                    XCR = "X";
                else
                    XCO = "X";
                XNMONE = fila["precios"].ToString();
                XIGV = fila["incluye_igv"].ToString();
                XREGPUBLICO = fila["regpublico"].ToString();
                XPARTELECTRO2 = fila["partelectro"].ToString();
                XESTCIVIL = fila["estcivil"].ToString();
                XIMPOBRUTO = Convert.ToDouble(fila["impobruto"].ToString());
                XIMPONETO = Convert.ToDouble(fila["imponeto"].ToString());
            }
            #endregion

            #region HOJA 01

            // TITULO
            //gfx.DrawString("EMPRESA", fontB9, XBrushes.Black, leftMargin, topMargin + 20);
            //gfx.DrawString(": " + XEMPRESANAME, font9, XBrushes.Black, leftMargin + 30, topMargin + 20);
            gfx.DrawString("CANAL DE VENTAS", fontB9, XBrushes.Black, leftMargin, topMargin + 30);
            gfx.DrawString(": " + "VENTAS HORIZONTALES", font9, XBrushes.Black, leftMargin + 60, topMargin + 30);
            //gfx.DrawString("RUC", fontB9, XBrushes.Black, leftMargin, topMargin + 40);
            //gfx.DrawString(": " + XEMPRESARUC, font9, XBrushes.Black, leftMargin + 30, topMargin + 40);

            gfx.DrawString("PEDIDOS", fontB12, XBrushes.Black, leftMargin + 230, topMargin + 20);

            pen = new XPen(XColors.Black, 2);
            gfx.DrawRoundedRectangle(pen, leftMargin + 400, topMargin + 10, 140, 30, 15, 15);

            // CUADRO
            gfx.DrawString("PEDIDO", fontB9, XBrushes.Black, leftMargin + 410, topMargin + 20);
            gfx.DrawString(": PD-" + Session["xnumdoc"].ToString(), font9, XBrushes.Black, leftMargin + 450, topMargin + 20);
            gfx.DrawString("FECHA", fontB9, XBrushes.Black, leftMargin + 410, topMargin + 30);
            gfx.DrawString(": " + Equivalencias.Left(XFECHA, 10), font9, XBrushes.Black, leftMargin + 450, topMargin + 30);

            font = new XFont("Abadi MT Condensed Light", 10, XFontStyle.Bold);
            font2 = new XFont("Abadi MT Condensed Light", 9);
            gfx.DrawString("VENDEDOR", font, XBrushes.Black, new XRect(leftMargin, topMargin + 50, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(": " + XVENDORNAME + "", font2, XBrushes.Black, new XRect(leftMargin + 40, topMargin + 50, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString("CLIENTE", font, XBrushes.Black, new XRect(leftMargin, topMargin + 60, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(": " + XCTACTENAME + "", font2, XBrushes.Black, new XRect(leftMargin + 40, topMargin + 60, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString("RUC", font, XBrushes.Black, new XRect(leftMargin, topMargin + 70, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(": " + XCLINMRUC + "", font2, XBrushes.Black, new XRect(leftMargin + 40, topMargin + 70, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString("DIRECC", font, XBrushes.Black, new XRect(leftMargin, topMargin + 80, 10, 10), XStringFormats.TopLeft);

            rect = new XRect(leftMargin + 40, topMargin + 80, 200, 100);
            gfx.DrawRectangle(XBrushes.White, rect);
            tf.Alignment = XParagraphAlignment.Justify;
            tf.DrawString(": " + XDIREC + "", font2, XBrushes.Black, rect, XStringFormats.TopLeft);

            gfx.DrawString("DIR ENTREGA", font, XBrushes.Black, new XRect(leftMargin, topMargin + 100, 10, 10), XStringFormats.TopLeft);
            //gfx.DrawString(": " + XDIRECENTREGA + "", font2, XBrushes.Black, new XRect(leftMargin + 60, topMargin + 100, 10, 10), XStringFormats.TopLeft);

            rect = new XRect(leftMargin + 60, topMargin + 100, 200, 100);
            gfx.DrawRectangle(XBrushes.White, rect);
            tf.Alignment = XParagraphAlignment.Justify;
            tf.DrawString(": " + XDIRECENTREGA + "", font2, XBrushes.Black, rect, XStringFormats.TopLeft);

            gfx.DrawString("NUM-CONTRATO", font, XBrushes.Black, new XRect(leftMargin, topMargin + 110, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(": " + XNUMDOC + "", font2, XBrushes.Black, new XRect(leftMargin + 60, topMargin + 110, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString("COND-VENTA : ", font, XBrushes.Black, new XRect(leftMargin, topMargin + 120, 10, 10), XStringFormats.TopLeft);

            // DIBUJAR UN RECTANGULO
            pen = new XPen(XColors.Black, 2);
            gfx.DrawRoundedRectangle(pen, leftMargin + 57, topMargin + 125, 90, 30, 25, 25);
            gfx.DrawString("CONTADO", font, XBrushes.Black, new XRect(leftMargin + 70, topMargin + 128, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(": " + XCO + "", font2, XBrushes.Black, new XRect(leftMargin + 120, topMargin + 128, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString("CREDITO", font, XBrushes.Black, new XRect(leftMargin + 70, topMargin + 139, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(": " + XCR + "", font2, XBrushes.Black, new XRect(leftMargin + 120, topMargin + 139, 10, 10), XStringFormats.TopLeft);

            gfx.DrawString("PLAZO EN DIAS", font, XBrushes.Black, new XRect(leftMargin + 150, topMargin + 139, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(": " + XPLAZODAY + " DIAS", font2, XBrushes.Black, new XRect(leftMargin + 210, topMargin + 139, 10, 10), XStringFormats.TopLeft);

            gfx.DrawString("VENTA TOTAL A PRECIO DE LISTA", font, XBrushes.Black, new XRect(leftMargin, topMargin + 160, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(": " + XIMPOBRUTO.ToString("##,###,##0.00") + "", font2, XBrushes.Black, new XRect(leftMargin + 120, topMargin + 160, 10, 10), XStringFormats.TopLeft);

            gfx.DrawString("TASA DE DESCUENTO", font, XBrushes.Black, new XRect(leftMargin, topMargin + 170, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(": " + XTASADESCTO + "%", font2, XBrushes.Black, new XRect(leftMargin + 120, topMargin + 170, 10, 10), XStringFormats.TopLeft);

            gfx.DrawString("VENTA TOTAL CON DESCUENTO", font, XBrushes.Black, new XRect(leftMargin, topMargin + 180, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(": " + XIMPONETO.ToString("##,###,##0.00") + "", font2, XBrushes.Black, new XRect(leftMargin + 120, topMargin + 180, 10, 10), XStringFormats.TopLeft);

            gfx.DrawString("PRECIOS DE LOS PRODUCTOS", font, XBrushes.Black, new XRect(leftMargin, topMargin + 190, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(": " + XNMONE + "", font2, XBrushes.Black, new XRect(leftMargin + 120, topMargin + 190, 10, 10), XStringFormats.TopLeft);

            gfx.DrawString("LOS PRECIOS INCLUYEN IGV", font, XBrushes.Black, new XRect(leftMargin, topMargin + 200, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(": " + XIGV + "", font2, XBrushes.Black, new XRect(leftMargin + 120, topMargin + 200, 10, 10), XStringFormats.TopLeft);

            //START RECTANGULO****************************************************************************************************************************************

            pen = new XPen(XColors.Black, 2);
            gfx.DrawRoundedRectangle(pen, leftMargin + 300, topMargin + 80, 245, 100, 25, 25);

            // Pintamos los Datos de la Tabla de Session
            DataTable tabla01 = new DataTable();
            tabla01 = (DataTable)Session["Tabla01"];

            // AQUI DIBUJAMOS LAS CABECERAS DEL DETALLE
            font = new XFont("Abadi MT Condensed Light", 10, XFontStyle.Bold);
            gfx.DrawString(("DOC. DE PAGO").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 305, topMargin + 85, 10, 10), XStringFormats.TopLeft);

            gfx.DrawString(("MONTO (SOLES)").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 396, topMargin + 85, 10, 10), XStringFormats.TopLeft);

            gfx.DrawString(("FECHA VCTO.").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 487, topMargin + 85, 10, 10), XStringFormats.TopLeft);

            //Trasamos las Lineas de la Cabecera
            pen = new XPen(XColors.Black, 2);
            //pen.LineCap = XLineCap.Flat;
            gfx.DrawLine(pen, leftMargin + 300, topMargin + 97, leftMargin + 546, topMargin + 97);
            //gfx.DrawLine(pen, x1, y1, x2, y2);

            //CARGAMOS EL DETALLE
            int n = 15;
            foreach (DataRow fila in tabla01.Rows)
            {
                Double monto = Convert.ToDouble(fila["monto"].ToString());
                font = new XFont("Abadi MT Condensed Light", 9, XFontStyle.Bold);
                gfx.DrawString((fila["num_interno"].ToString()).PadRight(19, ' '),
                    font, XBrushes.Black, new XRect(leftMargin + 305, topMargin + 85 + n, 10, 10), XStringFormats.TopLeft);
                gfx.DrawString((monto.ToString("##,###,##0.00")).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(leftMargin + 396, topMargin + 85 + n, 10, 10), XStringFormats.TopLeft);
                gfx.DrawString(Equivalencias.Left((fila["fechven"].ToString()), 10),
                   font, XBrushes.Black, new XRect(leftMargin + 487, topMargin + 85 + n, 10, 10), XStringFormats.TopLeft);
                n = n + 10;
            }

            // PIE DEL RECTANGULO
            gfx.DrawLine(pen, leftMargin + 300, topMargin + 165, leftMargin + 546, topMargin + 165);
            gfx.DrawString(("TOTAL CREDITO :                   " + XIMPONETO.ToString("##,###,##0.00")).PadRight(19, ' '),
                    font, XBrushes.Black, new XRect(leftMargin + 310, topMargin + 170, 10, 10), XStringFormats.TopLeft);

            //END RECTANGULO *********************************************************************************************************************************************

            pen = new XPen(XColors.Black, 3);
            //gfx2.DrawRoundedRectangle(pen, leftMargin + 0, topMargin + 220, 550, 40, 25, 25);
            // Pintamos los Datos de la Tabla de Session
            DataTable tabla02 = new DataTable();
            tabla02 = (DataTable)Session["Tabla02"];

            // AQUI DIBUJAMOS LAS CABECERAS DEL DETALLE
            font = new XFont("Abadi MT Condensed Light", 8.5, XFontStyle.Bold);
            gfx.DrawString(("CODIGO").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 5, topMargin + 228, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(("MARCA").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 35, topMargin + 228, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(("PRODUCTO").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 80, topMargin + 228, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(("COLOR").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 250, topMargin + 228, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(("TALLA").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 295, topMargin + 228, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(("CANTIDAD").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 318, topMargin + 228, 10, 10), XStringFormats.TopLeft);

            gfx.DrawString(("PRECIOS DE").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 350, topMargin + 225, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(("LISTA").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 358, topMargin + 235, 10, 10), XStringFormats.TopLeft);

            gfx.DrawString(("VENTAS A").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 400, topMargin + 225, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(("PRECIOS DE LISTA").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 390, topMargin + 235, 10, 10), XStringFormats.TopLeft);

            gfx.DrawString(("PRECIOS CON").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 450, topMargin + 225, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(("DESCUENTO").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 451, topMargin + 235, 10, 10), XStringFormats.TopLeft);

            gfx.DrawString(("VENTAS CON").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 500, topMargin + 225, 10, 10), XStringFormats.TopLeft);
            gfx.DrawString(("DESCUENTO").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 501, topMargin + 235, 10, 10), XStringFormats.TopLeft);

            pen = new XPen(XColors.Black, 2);
            gfx.DrawLine(pen, 40, 265, 580, 265);

            //CARGAMOS EL DETALLE
            int n2 = 15;
            int nfilas = 40;
            foreach (DataRow fila in tabla02.Rows)
            {
                Double precbruto = Convert.ToDouble(fila["precbruto"].ToString());
                Double impobruto = Convert.ToDouble(fila["impobruto"].ToString());
                Double precneto = Convert.ToDouble(fila["precneto"].ToString());
                Double imponeto = Convert.ToDouble(fila["imponeto"].ToString());

                font = new XFont("Abadi MT Condensed Light", 7.5, XFontStyle.Bold);
                gfx.DrawString((fila["articidold"].ToString()).PadRight(19, ' '),
                    font, XBrushes.Black, new XRect(leftMargin + 5, topMargin + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx.DrawString((fila["marcaname"].ToString()).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(leftMargin + 35, topMargin + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx.DrawString((fila["articname"].ToString()).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(leftMargin + 80, topMargin + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx.DrawString((fila["colorname"].ToString()).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(leftMargin + 250, topMargin + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx.DrawString((fila["talla"].ToString()).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(leftMargin + 299, topMargin + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx.DrawString((fila["cantidad"].ToString()).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(leftMargin + 318, topMargin + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx.DrawString((precbruto.ToString("##,###,##0.00")).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(leftMargin + 358, topMargin + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx.DrawString((impobruto.ToString("##,###,##0.00")).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(leftMargin + 400, topMargin + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx.DrawString((precneto.ToString("##,###,##0.00")).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(leftMargin + 458, topMargin + 235 + n2, 10, 10), XStringFormats.TopLeft);
                gfx.DrawString((imponeto.ToString("##,###,##0.00")).PadRight(19, ' '),
                   font, XBrushes.Black, new XRect(leftMargin + 508, topMargin + 235 + n2, 10, 10), XStringFormats.TopLeft);
                n2 = n2 + 10;
                nfilas = nfilas + 12;
            }

            //nfilas = nfilas;

            gfx.DrawRoundedRectangle(pen, leftMargin + 0, topMargin + 220, 540, nfilas, 25, 25);
            //PIE DEL RECTANGULO
            gfx.DrawLine(pen, 40, topMargin + 220 + (nfilas - 12), 570, topMargin + 220 + (nfilas - 12));
            font = new XFont("Abadi MT Condensed Light", 8, XFontStyle.Bold);
            gfx.DrawString(("TOTAL").PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 80, topMargin + 220 + (nfilas - 10), 10, 10), XStringFormats.TopLeft);

            gfx.DrawString((XIMPOBRUTO.ToString("##,###,##0.00")).PadRight(19, ' '),
                font, XBrushes.Black, new XRect(leftMargin + 400, topMargin + 220 + (nfilas - 10), 10, 10), XStringFormats.TopLeft);
            gfx.DrawString((XIMPONETO.ToString("##,###,##0.00")).PadRight(19, ' '),
               font, XBrushes.Black, new XRect(leftMargin + 508, topMargin + 220 + (nfilas - 10), 10, 10), XStringFormats.TopLeft);

            #endregion

            //Se envia el PDF al browser
            MemoryStream stream = new MemoryStream();
            document.Save(stream, false);
            Response.Clear();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-length", stream.Length.ToString());
            Response.BinaryWrite(stream.ToArray());
            Response.Flush();
            stream.Close();
            Response.End();

        }
        catch (Exception ex)
        {
            //Label1.Text = ex.Message;
        }
    }
    private void Rpt_EvalCrediticia()
    {
        try
        {
            //Se crea el documento PDF
            PdfDocument document = new PdfDocument();

            //Se configura los datos dela pagina
            document.Info.Title = "»» EVALUACIÓN CREDITICIA ««";
            document.Info.Author = "HVR";

            //Se crea la pagina A4
            PdfPage page = document.AddPage();
            page.Orientation = PageOrientation.Portrait;

            //Se crea el objeto dibujo
            XGraphics gfx = XGraphics.FromPdfPage(page);
            XPen pen = new XPen(XColors.Navy, 1);
            string anio = Convert.ToString(DateTime.Now).Substring(6, 10);
            XRect rect;
            XFont fontB18 = new XFont("Times", 20, XFontStyle.Bold);
            XFont fontB12 = new XFont("Times", 12, XFontStyle.Bold);
            XFont fontB9 = new XFont("Times", 9, XFontStyle.Bold);
            XFont font9 = new XFont("Times", 9);

            #region OBTENIENDO VALORES DE SESSION
            // DECLARANDO VARIBALES QUE RECIBIMOS DE SESSION
            string XEMPRESANAME = Session["ssEmpresaName"].ToString();
            string XEMPRESARUC = Session["ssEmpresaRuc"].ToString();
            string XGERENTEGENE = Session["ssGerenteGeneral"].ToString();
            string XGERENTEDNI = Session["ssGerenteNrodni"].ToString();
            string XPARTELECTRO = Session["ssPartElectro"].ToString();
            string XUSUAR = Session["ssUsuar"].ToString();

            int XPUNTAJE = 0;
            Boolean XESJURIDICA = false,
                    XCOPIACONST = false,
                    XCOPIARUC = false,
                    XCOPIADNI = false,
                    XLICFUNC = false,
                    XTITLOCAL = false,
                    XCONTLOCAL = false,
                    XRECIBO = false,
                    XTIENLETR = false,
                    XTIENMORO = false,
                    XMOROSO = false,
                    XBIENMUEB = false,
                    XBIENINMUEB = false;

            string XNUMDOC = "", XSERDOC = "", XCTACTENAME = "", XCLINMRUC = "", XFECHA = "", XREFCOMER = "",
                   XREFBANCA = "",XEVALCXCOB ="",XOBSCXCOB="",XOBSGEREN="",XAPROBGERE="";

            tb_cxc_evalcredBL BL = new tb_cxc_evalcredBL();
            tb_cxc_evalcred BE = new tb_cxc_evalcred();
            DataTable TablaEval = new DataTable();
            BE.tipdoc = Session["xtipdoc"].ToString();
            BE.serdoc = Session["xserdoc"].ToString();
            BE.numdoc = Session["xnumdoc"].ToString();
            TablaEval = BL.GetAll(Session["ssEmpresaID"].ToString(), BE).Tables[0];
            foreach (DataRow fila in TablaEval.Rows)
            {
                XNUMDOC = fila["numdoc"].ToString();
                XSERDOC = fila["serdoc"].ToString();
                XCTACTENAME = fila["ctactename"].ToString();
                XCLINMRUC = fila["nmruc"].ToString();
                XFECHA = fila["fechdoc"].ToString();
                XESJURIDICA = Convert.ToBoolean(fila["es_persjuridica"].ToString());
                XCOPIACONST = Convert.ToBoolean(fila["copia_constitucionempresa"].ToString());
                XCOPIARUC = Convert.ToBoolean(fila["copia_ruc"].ToString());
                XCOPIADNI = Convert.ToBoolean(fila["copia_dni"].ToString());
                XLICFUNC = Convert.ToBoolean(fila["lic_func"].ToString());
                XTITLOCAL = Convert.ToBoolean(fila["titulo_localcom"].ToString());
                XCONTLOCAL = Convert.ToBoolean(fila["contra_localcom"].ToString());
                XRECIBO = Convert.ToBoolean(fila["recibo_agualuz"].ToString());
                XTIENLETR = Convert.ToBoolean(fila["tiene_letraprotestada"].ToString());
                XTIENMORO = Convert.ToBoolean(fila["tiene_morosidad"].ToString());
                XMOROSO = Convert.ToBoolean(fila["moroso_infocorp"].ToString());
                XREFCOMER = fila["refe_comerc"].ToString();
                XREFBANCA = fila["refe_banca"].ToString();
                XBIENMUEB = Convert.ToBoolean(fila["bienes_bienmueble"].ToString());
                XBIENINMUEB = Convert.ToBoolean(fila["bienes_inmuebles"].ToString());

                XPUNTAJE = Convert.ToInt32(fila["puntaje"].ToString());
                XEVALCXCOB = fila["eval_cxcob"].ToString();
                XOBSCXCOB = fila["obs_cxcob"].ToString();
                XAPROBGERE = fila["aprob_gerencial"].ToString();
                XOBSGEREN = fila["obs_gerencial"].ToString();
            }
            #endregion

            Double XIMPOBRUTO = 0, XIMPONETO = 0;
            String XVENDORNAME = "";

            //VARIABLES RECIEN AGREGADAS EN LA BD

            tb_cxc_pedidocabBL BL2 = new tb_cxc_pedidocabBL();
            tb_cxc_pedidocab BE2 = new tb_cxc_pedidocab();
            DataTable TablaProfor = new DataTable();
            BE2.tipdoc = Session["xtipdoc"].ToString();
            BE2.serdoc = Session["xserdoc"].ToString();
            BE2.numdoc = Session["xnumdoc"].ToString();
            TablaProfor = BL2.GetAll2(Session["ssEmpresaID"].ToString(), BE2).Tables[0];
            if (TablaProfor.Rows.Count > 0)
            {
                XVENDORNAME = TablaProfor.Rows[0]["vendorname"].ToString();
            }

            //define margins
            double leftMargin = 40.0;
            double rightMargin = 570.0;
            double topMargin = 20.0;
            double lineTop = 0d;
            double lineFooter = 780d;
            double rowStep = 12d;
            double newRow = topMargin + rowStep;
            double beginRow = newRow;
            double currentRow = beginRow;
            double currentRowCol2 = 0;
            double firstCustomerRow = 100d;
            double txtShift = 8d;
            string hcString = string.Empty; //---added 6-26-13
            string sim1 = "[";
            string sim2 = "]";
            string space = "   ";

            // TITULO
            gfx.DrawString("EVALUACION CREDITICIA", fontB18, XBrushes.Black, leftMargin + 20, topMargin + 40);

            pen = new XPen(XColors.Black, 2);
            gfx.DrawRoundedRectangle(pen, leftMargin + 300, topMargin + 15, 240, 50, 15, 15);

            // CUADRO
            gfx.DrawString("PEDIDO", fontB9, XBrushes.Black, leftMargin + 310, topMargin + 30);
            gfx.DrawString(": PD-"+XNUMDOC, font9, XBrushes.Black, leftMargin + 370, topMargin + 30);
            gfx.DrawString("FECHA", fontB9, XBrushes.Black, leftMargin + 440, topMargin + 30);
            gfx.DrawString(": " + Equivalencias.Left(XFECHA, 10), font9, XBrushes.Black, leftMargin + 480, topMargin + 30);
            gfx.DrawString("CLIENTE", fontB9, XBrushes.Black, leftMargin + 310, topMargin + 40);
            gfx.DrawString(": "+Equivalencias.Left(XCTACTENAME,30) , font9, XBrushes.Black, leftMargin + 370, topMargin + 40);
            gfx.DrawString("RUC", fontB9, XBrushes.Black, leftMargin + 310, topMargin + 50);
            gfx.DrawString(": "+XCLINMRUC, font9, XBrushes.Black, leftMargin + 370, topMargin + 50);
            gfx.DrawString("VENDEDOR", fontB9, XBrushes.Black, leftMargin + 310, topMargin + 60);
            gfx.DrawString(": " + XVENDORNAME, font9, XBrushes.Black, leftMargin + 370, topMargin + 60);

            // CONTENIDO
            gfx.DrawString("I.", fontB9, XBrushes.Black, leftMargin + 20, topMargin + 80);
            gfx.DrawString("DOCUMENTACION PARA LA EVALUACION CREDITICIA :", fontB9, XBrushes.Black, leftMargin + 50, topMargin + 80);

            gfx.DrawString("1.", font9, XBrushes.Black, leftMargin + 50, topMargin + 95);
            gfx.DrawString("CONDICION JURIDICA DEL CLIENTE :", font9, XBrushes.Black, leftMargin + 70, topMargin + 95);
            if (XESJURIDICA)
            {
                gfx.DrawString(sim1 + space + sim2, font9, XBrushes.Black, leftMargin + 70, topMargin + 108);
                gfx.DrawString("PERSONA NATURAL", font9, XBrushes.Black, leftMargin + 90, topMargin + 108);
                gfx.DrawString(sim1 + "X" + sim2, font9, XBrushes.Black, leftMargin + 70, topMargin + 118);
                gfx.DrawString("PERSONA JURIDICA", font9, XBrushes.Black, leftMargin + 90, topMargin + 118);
            }
            else {
                gfx.DrawString(sim1 + "X" + sim2, font9, XBrushes.Black, leftMargin + 70, topMargin + 108);
                gfx.DrawString("PERSONA NATURAL", font9, XBrushes.Black, leftMargin + 90, topMargin + 108);
                gfx.DrawString(sim1 + space + sim2, font9, XBrushes.Black, leftMargin + 70, topMargin + 118);
                gfx.DrawString("PERSONA JURIDICA", font9, XBrushes.Black, leftMargin + 90, topMargin + 118);
            }

            gfx.DrawString("2.", font9, XBrushes.Black, leftMargin + 50, topMargin + 133);
            gfx.DrawString("DOCUMENTOS PRESENTADOS POR EL CLIENTE :", font9, XBrushes.Black, leftMargin + 70, topMargin + 133);
            // COLUMNA 1
            if(XCOPIACONST)
                gfx.DrawString(sim1 + "X" + sim2, font9, XBrushes.Black, leftMargin + 70, topMargin + 146);
            else
                gfx.DrawString(sim1 + space + sim2, font9, XBrushes.Black, leftMargin + 70, topMargin + 146);
            gfx.DrawString("CONSTITUCION DE LA EMPRESA", font9, XBrushes.Black, leftMargin + 90, topMargin + 146);

            if(XCOPIARUC)
                gfx.DrawString(sim1 + "X" + sim2, font9, XBrushes.Black, leftMargin + 70, topMargin + 156);
            else
                gfx.DrawString(sim1 + space + sim2, font9, XBrushes.Black, leftMargin + 70, topMargin + 156);
            gfx.DrawString("RUC", font9, XBrushes.Black, leftMargin + 90, topMargin + 156);

            if(XCOPIADNI)
                gfx.DrawString(sim1 + "X" + sim2, font9, XBrushes.Black, leftMargin + 70, topMargin + 166);
            else
                gfx.DrawString(sim1 + space + sim2, font9, XBrushes.Black, leftMargin + 70, topMargin + 166);
            gfx.DrawString("DNI", font9, XBrushes.Black, leftMargin + 90, topMargin + 166);

            // COLUMNA 2
            if(XLICFUNC)
                gfx.DrawString(sim1 + "X" + sim2, font9, XBrushes.Black, leftMargin + 300, topMargin + 146);
            else
                gfx.DrawString(sim1 + space + sim2, font9, XBrushes.Black, leftMargin + 300, topMargin + 146);
            gfx.DrawString("LIC. DE FUNCIONAMIENTO", font9, XBrushes.Black, leftMargin + 320, topMargin + 146);

            if(XTITLOCAL)
                gfx.DrawString(sim1 + "X" + sim2, font9, XBrushes.Black, leftMargin + 300, topMargin + 156);
            else
                gfx.DrawString(sim1 + space + sim2, font9, XBrushes.Black, leftMargin + 300, topMargin + 156);
            gfx.DrawString("TITULO DE PROPIEDAD", font9, XBrushes.Black, leftMargin + 320, topMargin + 156);

            if(XCONTLOCAL)
                gfx.DrawString(sim1 + "X" + sim2, font9, XBrushes.Black, leftMargin + 300, topMargin + 166);
            else
                gfx.DrawString(sim1 + space + sim2, font9, XBrushes.Black, leftMargin + 300, topMargin + 166);
            gfx.DrawString("CONTRATO DE LOCAL", font9, XBrushes.Black, leftMargin + 320, topMargin + 166);

            if(XRECIBO)
                gfx.DrawString(sim1 + "X" + sim2, font9, XBrushes.Black, leftMargin + 300, topMargin + 176);
            else
                gfx.DrawString(sim1 + space + sim2, font9, XBrushes.Black, leftMargin + 300, topMargin + 176);
            gfx.DrawString("RECIBO AGUA O LUZ", font9, XBrushes.Black, leftMargin + 320, topMargin + 176);

            gfx.DrawString("II.", fontB9, XBrushes.Black, leftMargin + 20, topMargin + 200);
            gfx.DrawString("FACTORES DE EVALUACION CREDITICIA:", fontB9, XBrushes.Black, leftMargin + 50, topMargin + 200);

            // COLUMNA 1
            gfx.DrawString("3.", font9, XBrushes.Black, leftMargin + 50, topMargin + 215);
            gfx.DrawString("COMPORTAMIENTO DE PAGO", font9, XBrushes.Black, leftMargin + 70, topMargin + 215);

            gfx.DrawString("3.1", font9, XBrushes.Black, leftMargin + 70, topMargin + 228);
            gfx.DrawString("PROTESTOS DE LETRA", font9, XBrushes.Black, leftMargin + 90, topMargin + 228);
            string x1 = space, x2 = space;
            if (XTIENLETR)
                x1 = "X";
            else
                x2 = "X";
            gfx.DrawString(sim1 + x1 + sim2, font9, XBrushes.Black, leftMargin + 90, topMargin + 241);
            gfx.DrawString("SI TIENE", font9, XBrushes.Black, leftMargin + 110, topMargin + 241);
            gfx.DrawString(sim1 + x2 + sim2, font9, XBrushes.Black, leftMargin + 90, topMargin + 251);
            gfx.DrawString("NO TIENE", font9, XBrushes.Black, leftMargin + 110, topMargin + 251);

            gfx.DrawString("3.2", font9, XBrushes.Black, leftMargin + 70, topMargin + 264);
            gfx.DrawString("INCUMPLIMIENTO DE PAGO", font9, XBrushes.Black, leftMargin + 90, topMargin + 264);
            string x3 = space, x4 = space;
            if (XTIENMORO)
                x3 = "X";
            else
                x4 = "X";
            gfx.DrawString(sim1 + x3 + sim2, font9, XBrushes.Black, leftMargin + 90, topMargin + 277);
            gfx.DrawString("SI TIENE", font9, XBrushes.Black, leftMargin + 110, topMargin + 277);
            gfx.DrawString(sim1 + x4 + sim2, font9, XBrushes.Black, leftMargin + 90, topMargin + 287);
            gfx.DrawString("NO TIENE", font9, XBrushes.Black, leftMargin + 110, topMargin + 287);

            gfx.DrawString("4.", font9, XBrushes.Black, leftMargin + 50, topMargin + 310);
            gfx.DrawString("ANTECEDENTES INFOCORP", font9, XBrushes.Black, leftMargin + 70, topMargin + 310);
            string x5 = space, x6 = space;
            if (XMOROSO)
                x5 = "X";
            else
                x6 = "X";
            gfx.DrawString(sim1 + x5 + sim2, font9, XBrushes.Black, leftMargin + 90, topMargin + 323);
            gfx.DrawString("SI TIENE", font9, XBrushes.Black, leftMargin + 110, topMargin + 323);
            gfx.DrawString(sim1 + x6 + sim2, font9, XBrushes.Black, leftMargin + 90, topMargin + 333);
            gfx.DrawString("NO TIENE", font9, XBrushes.Black, leftMargin + 110, topMargin + 333);

            // COLUMNA 2
            gfx.DrawString("5.", font9, XBrushes.Black, leftMargin + 300, topMargin + 215);
            gfx.DrawString("REFERENCIAS COMERCIALES", font9, XBrushes.Black, leftMargin + 320, topMargin + 215);
            string xva1 = space, xva2 = space, xva3 = space;
            if (XREFCOMER.Length > 0)
            {
                if (XREFCOMER.ToString() == "B")
                    xva1 = "X";
                else if (XREFCOMER.ToString() == "R")
                    xva2 = "X";
                else if (XREFCOMER.ToString() == "M")
                    xva3 = "X";
            }
            gfx.DrawString(sim1 + xva1 + sim2, font9, XBrushes.Black, leftMargin + 320, topMargin + 228);
            gfx.DrawString("BUENA", font9, XBrushes.Black, leftMargin + 340, topMargin + 228);
            gfx.DrawString(sim1 + xva2 + sim2, font9, XBrushes.Black, leftMargin + 320, topMargin + 238);
            gfx.DrawString("REGULAR", font9, XBrushes.Black, leftMargin + 340, topMargin + 238);
            gfx.DrawString(sim1 + xva3 + sim2, font9, XBrushes.Black, leftMargin + 320, topMargin + 248);
            gfx.DrawString("MALA", font9, XBrushes.Black, leftMargin + 340, topMargin + 248);

            gfx.DrawString("6.", font9, XBrushes.Black, leftMargin + 300, topMargin + 271);
            gfx.DrawString("REFERENCIAS BANCARIAS", font9, XBrushes.Black, leftMargin + 320, topMargin + 271);
            string xva4 = space, xva5 = space, xva6 = space;
            if (XREFBANCA.Length > 0)
            {
                if (XREFBANCA.ToString() == "B")
                    xva4 = "X";
                else if (XREFBANCA.ToString() == "R")
                    xva5 = "X";
                else if (XREFBANCA.ToString() == "M")
                    xva6 = "X";
            }
            gfx.DrawString(sim1 + xva4 + sim2, font9, XBrushes.Black, leftMargin + 320, topMargin + 284);
            gfx.DrawString("BUENA", font9, XBrushes.Black, leftMargin + 340, topMargin + 284);
            gfx.DrawString(sim1 + xva5 + sim2, font9, XBrushes.Black, leftMargin + 320, topMargin + 294);
            gfx.DrawString("REGULAR", font9, XBrushes.Black, leftMargin + 340, topMargin + 294);
            gfx.DrawString(sim1 + xva6 + sim2, font9, XBrushes.Black, leftMargin + 320, topMargin + 304);
            gfx.DrawString("MALA", font9, XBrushes.Black, leftMargin + 340, topMargin + 304);

            gfx.DrawString("7.", font9, XBrushes.Black, leftMargin + 300, topMargin + 327);
            gfx.DrawString("EVALUACION PATRIMONIAL", font9, XBrushes.Black, leftMargin + 320, topMargin + 327);

            gfx.DrawString("7.1", font9, XBrushes.Black, leftMargin + 320, topMargin + 340);
            gfx.DrawString("BIENES MUEBLES", font9, XBrushes.Black, leftMargin + 340, topMargin + 340);
            string x7 = space, x8 = space;
            if (XBIENMUEB)
                x7 = "X";
            else
                x8 = "X";
            gfx.DrawString(sim1 + x7 + sim2, font9, XBrushes.Black, leftMargin + 340, topMargin + 351);
            gfx.DrawString("SI TIENE", font9, XBrushes.Black, leftMargin + 360, topMargin + 351);
            gfx.DrawString(sim1 + x8 + sim2, font9, XBrushes.Black, leftMargin + 340, topMargin + 361);
            gfx.DrawString("NO TIENE", font9, XBrushes.Black, leftMargin + 360, topMargin + 361);

            gfx.DrawString("7.2", font9, XBrushes.Black, leftMargin + 320, topMargin + 374);
            gfx.DrawString("BIENES INMUEBLES", font9, XBrushes.Black, leftMargin + 340, topMargin + 374);
            string x9 = space, x10 = space;
            if (XBIENINMUEB)
                x9 = "X";
            else
                x10 = "X";
            gfx.DrawString(sim1 + x9 + sim2, font9, XBrushes.Black, leftMargin + 340, topMargin + 387);
            gfx.DrawString("SI TIENE", font9, XBrushes.Black, leftMargin + 360, topMargin + 387);
            gfx.DrawString(sim1 + x10 + sim2, font9, XBrushes.Black, leftMargin + 340, topMargin + 397);
            gfx.DrawString("NO TIENE", font9, XBrushes.Black, leftMargin + 360, topMargin + 397);

            //----------
            gfx.DrawString("III.", fontB9, XBrushes.Black, leftMargin + 20, topMargin + 420);
            gfx.DrawString("OPINION AREA DE VENTAS HORIZONTALES", fontB9, XBrushes.Black, leftMargin + 50, topMargin + 420);
            string xcli1 = space, xcli2 = space, xcli3 = space;
            if (XEVALCXCOB.Length > 0)
            {
                if (XEVALCXCOB.ToString() == "CA")
                    xcli1 = "X";
                else if (XEVALCXCOB.ToString() == "CN")
                    xcli2 = "X";
                else if (XEVALCXCOB.ToString() == "CD")
                    xcli3 = "X";
            }
            gfx.DrawString(sim1 + xcli1 + sim2, font9, XBrushes.Black, leftMargin + 70, topMargin + 435);
            gfx.DrawString("CLIENTE ACEPTABLE", font9, XBrushes.Black, leftMargin + 90, topMargin + 435);
            gfx.DrawString(sim1 + xcli2 + sim2, font9, XBrushes.Black, leftMargin + 70, topMargin + 445);
            gfx.DrawString("CLIENTE NO ACEPTABLE", font9, XBrushes.Black, leftMargin + 90, topMargin + 445);
            gfx.DrawString(sim1 + xcli3 + sim2, font9, XBrushes.Black, leftMargin + 70, topMargin + 455);
            gfx.DrawString("CLIENTE DUDOSO", font9, XBrushes.Black, leftMargin + 90, topMargin + 455);

            gfx.DrawString("OTROS COMENTARIOS:", font9, XBrushes.Black, leftMargin + 50, topMargin + 475);
            gfx.DrawString("» "+XOBSCXCOB, font9, XBrushes.Black, leftMargin + 80, topMargin + 490);

            gfx.DrawString("IV.", fontB9, XBrushes.Black, leftMargin + 20, topMargin + 520);
            gfx.DrawString("APROBACIÓN GERENCIA GENERAL", fontB9, XBrushes.Black, leftMargin + 50, topMargin + 520);
            string x11 = space, x12 = space;
            if (XAPROBGERE == "32")
                x11 = "X";
            else if (XAPROBGERE == "39")
                x12 = "X";
            gfx.DrawString(sim1 + x11 + sim2, font9, XBrushes.Black, leftMargin + 200, topMargin + 533);
            gfx.DrawString("APROBADO", font9, XBrushes.Black, leftMargin + 220, topMargin + 533);
            gfx.DrawString(sim1 + x12 + sim2, font9, XBrushes.Black, leftMargin + 300, topMargin + 533);
            gfx.DrawString("NO APROBADO", font9, XBrushes.Black, leftMargin + 320, topMargin + 533);

            gfx.DrawString("OBSERVACIONES:", font9, XBrushes.Black, leftMargin + 50, topMargin + 553);
            gfx.DrawString("» " + XOBSGEREN, font9, XBrushes.Black, leftMargin + 80, topMargin + 568);
            string texto = "NOTA: A ESTE FORMATO SE DEBERA ANEXAR LA DOCUMENTACIÓN CORRESPONDIENTE";
            gfx.DrawString(texto, fontB9, XBrushes.Black, leftMargin + 50, topMargin + 598);

            //SE ENVIA EL PDF AL BROWSER
            MemoryStream stream = new MemoryStream();
            document.Save(stream, false);
            Response.Clear();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-length", stream.Length.ToString());
            Response.BinaryWrite(stream.ToArray());
            Response.Flush();
            stream.Close();
            Response.End();

        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
    }