private void pdoc_PrintPageOrden(PrintPageEventArgs e, RefOrden orden)
        {
            log.Info("Solicitando pdoc_PrintPageOrden de "+orden.ordn_id);
            SqlDataReader reader = null;
            SqlConnection myConnection = new SqlConnection();
            SqlCommand command = null;
            Graphics graphics = e.Graphics;
            Font font = new Font(sFont, 9, FontStyle.Bold);
            int iMesaCliente = 0;
            string sTienda = "";
            try
            {
                myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["BaseComercial"].ConnectionString;
                myConnection.Open();
                command = new SqlCommand("select mesa_cve from mesa where mesa_id = (select mesa_id from orden where ordn_id=" + orden.ordn_id + ")", myConnection);
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    iMesaCliente = Convert.ToInt32(reader["mesa_cve"].ToString());
                }
                command = new SqlCommand("select succ_des from sucursal where succ_id = (select succ_id from ff_cat_usuario where usr_numempleado =(select ordn_mese from orden where ordn_id = " +orden.ordn_id+") )", myConnection);
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    sTienda = reader["succ_des"].ToString();
                }
            }
            catch (Exception extt)
            {
                log.Error("Error en pdoc_PrintPageOrden-MesaTienda en " + extt.Message);
            }
            //float fontHeight = font.GetHeight();
            double fontHeight_d = 8.7;
            float fontHeight = (float)fontHeight_d;
            int startX = 15;
            int startY = 15;
            int Offset = 10;
            try { graphics.DrawImage(Image.FromFile(".\\citymark2.png"), 38, 0, 230, 100); }
            catch (Exception ex) {
                log.Error("Error en pdoc_PrintPageOrden logo en "+ex.Message);
            }

            Offset = Offset + 80;
            graphics.DrawString(sTienda, new Font(sFont, (float)8.6, FontStyle.Bold),
                                Brushes.Black, CentrarTexto(graphics, sTienda), startY + Offset);
            Offset = Offset + 20;

            String sTitulo = "Tiendas Comercial Mexicana" ;
            graphics.DrawString(sTitulo, new Font(sFont, (float)8.6, FontStyle.Bold),
                                Brushes.Black, CentrarTexto(graphics, sTitulo), startY + Offset);
            Offset = Offset + 20;
            String numOrden = "No. de Orden " + orden.ordn_id;
            graphics.DrawString(numOrden, new Font(sFont, (float)8.6, FontStyle.Bold),
                                Brushes.Black, CentrarTexto(graphics, numOrden), startY + Offset);
            Offset = Offset + 20;
            graphics.DrawString("Cliente / Mesa:" + iMesaCliente,
                        new Font(sFont, (float)8.5, FontStyle.Regular), Brushes.Black, startX - 6, startY + Offset);
            Offset = Offset + 20;
            graphics.DrawString("Can Articulo",
                        new Font(sFont, (float)8.5, FontStyle.Regular),
                        Brushes.Black, startX - 8, startY + Offset);
            graphics.DrawString("Precio",
                        new Font(sFont, (float)8.5, FontStyle.Regular),
                        Brushes.Black, CentrarImporte(graphics,"Precio"), startY + Offset);
            graphics.DrawString("Total",
                        new Font(sFont, (float)8.5, FontStyle.Regular),
                        Brushes.Black, CentrarTotal(graphics,"Total"), startY + Offset);
            Offset = Offset + 20;
            String underLine = "---------------------------------------";
            graphics.DrawString(underLine, new Font(sFont, (float)8.5, FontStyle.Regular),
                        new SolidBrush(Color.Black), startX - 6, startY + Offset);

            int iCantidadArticulos = 0;
            string sMesero = "";
            string sRestaurante = "";
            List<DetalleOrden> listaDetallesOrden = new List<DetalleOrden>();

            try
            {
                command = new SqlCommand("select  art_ean, SUM(ORDN_CANT) as ordn_cant,SUM(ORDN_IMPUNI) as ordn_impuni, SUM(ORDN_IMPART) as ordn_impart from orden_articulo where ordn_id= "+ orden.ordn_id+" group by  ART_EAN" , myConnection);
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    DetalleOrden ordart = new DetalleOrden();
                    ordart.art_ean = Convert.ToDecimal(reader["art_ean"].ToString());
                    ordart.ordn_cant = Convert.ToInt32(reader["ordn_cant"].ToString());
                    //ordart.ordn_id = Convert.ToInt32(reader["ordn_id"].ToString());
                    ordart.ordn_id = orden.ordn_id;
                    ordart.ordn_impart = Convert.ToDecimal(reader["ordn_impart"].ToString());
                    ordart.ordn_impuni = Convert.ToDecimal(reader["ordn_impuni"].ToString());
                    //ordart.ordn_obsv = (reader["ordn_obsv"].ToString());
                    listaDetallesOrden.Add(ordart);
                    iCantidadArticulos+=ordart.ordn_cant;
                }
            }catch(Exception excv){
                log.Error("error en busqueda de detalle de orden en "+excv.Message);
            }
            decimal totaltotales = 0;
            foreach (DetalleOrden artOrd in listaDetallesOrden )
            {

                string sArticulo = "";

                try//EL SIGUEINTE BLOQUE ES PARA EXTRAER LAS CATEGORIAS
                {
                    command = new SqlCommand("select art_des from articulo where art_ean = " + artOrd.art_ean, myConnection);
                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        sArticulo = reader["art_des"].ToString().Replace("\n", " ");
                    }
                    //command = new SqlCommand("select agru_des from agrupacion_modif where agru_id = " + artOrd.agru_id + " and agru_consec=" + artOrd.agru_consec, myConnection);
                    //reader = command.ExecuteReader();
                    //while (reader.Read())
                    //{
                    //    sModificador = reader["agru_des"].ToString();
                    //}

                }
                catch (Exception exc)
                {
                    log.Error("Error en pdoc_PrintPageOrden-sArticulo en "+exc.Message);
                }
                if (sArticulo.Length>18)
                {
                    sArticulo=sArticulo.Substring(0, 18);
                }
                Offset = Offset + 10;
                graphics.DrawString(artOrd.ordn_cant.ToString(), new Font(sFont, (float)8, FontStyle.Regular),
                        new SolidBrush(Color.Black), startX + 5, startY + Offset);
                graphics.DrawString(sArticulo, new Font(sFont, (float)8, FontStyle.Regular),
                        new SolidBrush(Color.Black), startX + 25, startY + Offset);
                string sImportUni = artOrd.ordn_impuni.ToString();
                graphics.DrawString(sImportUni, new Font(sFont, (float)8, FontStyle.Regular),
                        new SolidBrush(Color.Black), CentrarImporte(graphics, sImportUni), startY + Offset);//new SolidBrush(Color.Black), startX + 170, startY + Offset);
                decimal totl = artOrd.ordn_impuni * artOrd.ordn_cant;
                totaltotales += totl;
                string svonvert= Convert.ToString(totl);
                graphics.DrawString(svonvert, new Font(sFont, (float)8, FontStyle.Regular),
                        new SolidBrush(Color.Black), CentrarTotal(graphics, svonvert), startY + Offset);
                //if (artOrd.ordn_obsv != null && artOrd.ordn_obsv.Length > 3)
                //{
                //    Offset = Offset + 10;
                //    graphics.DrawString("O: " + artOrd.ordn_obsv, new Font("Courier New", (float)7.8, FontStyle.Regular),
                //        new SolidBrush(Color.Black), startX + 35, startY + Offset);
                //}
                //if (artOrd.hasModif)
                //{
                //    Offset = Offset + 10;
                //    graphics.DrawString("M: " + sModificador, new Font("Courier New", (float)7.8, FontStyle.Regular),
                //        new SolidBrush(Color.Black), startX + 35, startY + Offset);
                //}
                //iCantidadArticulos += artOrd.ordn_cant;
            }//end foreach
            try
            {
                command = new SqlCommand("select usr_nombre from ff_cat_usuario where usr_numempleado = (select ordn_mese from orden where ordn_id=" + orden.ordn_id + ")", myConnection);
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    sMesero = reader["usr_nombre"].ToString();
                }

                command = new SqlCommand("select rest_des from restaurant where rest_id = (select rest_id from mesa where mesa_id=(select mesa_id from orden where ordn_id=" + orden.ordn_id + "))", myConnection);
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    sRestaurante = reader["rest_des"].ToString();
                }
            }
            catch (Exception ex)
            {
                log.Error("Error en pdoc_PrintPageOrden meserorestaurant en "+ex.Message);
            }
            finally
            {
                myConnection.Close();
            }
            Offset = Offset + 20;
            graphics.DrawString(underLine, new Font(sFont, (float)8.5, FontStyle.Regular),
                        new SolidBrush(Color.Black), startX - 6, startY + Offset);
            Offset = Offset + 10;
            graphics.DrawString("Total ", new Font(sFont, (float)7.5, FontStyle.Regular),
                        new SolidBrush(Color.Black), startX +170, startY + Offset);
            string stottales = "" + totaltotales;
            graphics.DrawString("" + totaltotales, new Font(sFont, (float)7.5, FontStyle.Regular),
                        new SolidBrush(Color.Black), CentrarTotal(graphics, stottales), startY + Offset);
            Offset = Offset + 20;
            graphics.DrawString("No. de Articulos:" + iCantidadArticulos, new Font(sFont, (float)7.5, FontStyle.Regular),
                        new SolidBrush(Color.Black), startX - 6, startY + Offset);
            Offset = Offset + 10;
            graphics.DrawString("Atendio:" + sMesero, new Font(sFont, (float)7.5, FontStyle.Regular),
                        new SolidBrush(Color.Black), startX - 6, startY + Offset);
            Offset = Offset + 10;
            graphics.DrawString("Origen:" + sRestaurante, new Font(sFont, (float)7.5, FontStyle.Regular),
                        new SolidBrush(Color.Black), startX - 6, startY + Offset);
            Offset = Offset + 25;

            string leyendaa = "Ticket No Valido";
            e.Graphics.DrawString(leyendaa, new Font(sFont, (float)8.6, FontStyle.Bold),
                                Brushes.Black, CentrarTexto(graphics, leyendaa), startY + Offset);
            Offset = Offset + 12;
            string leyendab = "Como Comprobante de Pago";
            e.Graphics.DrawString(leyendab, new Font(sFont, (float)8.6, FontStyle.Bold),
                                Brushes.Black, CentrarTexto(graphics, leyendab), startY + Offset);
            Offset = Offset + 20;
            DateTime nw = DateTime.Now;
            string fechahora = "" + nw;
            e.Graphics.DrawString(fechahora, new Font(sFont, (float)8.6, FontStyle.Regular),
                                Brushes.Black, CentrarTexto(graphics, fechahora), startY + Offset);
            Offset = Offset + 20;
        }
        public ActionResult finOrden(RefOrden orden)
        {
            log.Info("Solicitando finOrden de orden " + orden.ordn_id);
            string sNombreRest = null;
            Respuesta resp = new Respuesta();
            SqlDataReader reader = null;
            SqlConnection myConnection = new SqlConnection();
            try
            {
                myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["BaseComercial"].ConnectionString;
                myConnection.Open();
                SqlCommand command = new SqlCommand("select REST_DES from  RESTAURANT where REST_ID =(select rest_id from MESA where MESA_id = (select MESA_ID from ORDEN where ORDN_ID= " +orden.ordn_id+"))", myConnection);
                log.Debug("Obteniendo Informacion de Base-Restaurante");
                reader = command.ExecuteReader();
                while(reader.Read()){
                    sNombreRest = reader["REST_DES"].ToString();
                    sNombreRest = sNombreRest.Replace(" ", "");
                    log.Debug("Base de Orden =" + sNombreRest);
                }
                command = new SqlCommand("UPDATE ORDEN SET ORDN_STAT = 'FINC' WHERE ORDN_ID = @ORDEN", myConnection);
                command.Parameters.AddWithValue("@ORDEN", orden.ordn_id);
                if (0 < command.ExecuteNonQuery())
                {
                    command = new SqlCommand(" UPDATE ORDEN_CTRL SET ORDN_STAT = 'FINC' WHERE ORDN_ID = @ORDEN", myConnection);
                    command.Parameters.AddWithValue("@ORDEN", orden.ordn_id);
                    if (0 < command.ExecuteNonQuery())
                    {
                        command = new SqlCommand(" UPDATE MESA SET MESA_STAT = 'ALTA' WHERE MESA_ID=@MESAID", myConnection);
                        command.Parameters.AddWithValue("@MESAID", orden.mesa_id);
                        if (0 < command.ExecuteNonQuery())
                        {
                            command = new SqlCommand(" select * from orden_articulo where ordn_id = " + orden.ordn_id, myConnection);
                            reader = command.ExecuteReader();
                            while (reader.Read())
                            {//INSERTA EN LA DB CORRESPONDIENTE LA VENTA

                                if (insertaVenta(reader, sNombreRest))
                                {
                                    resp.success = true;
                                    resp.message = "OK";
                                }
                                else
                                {
                                    resp.success = false;
                                    resp.message = "ERROR INSERTANDO EN BASE DE RESTAURANT";
                                    return Json(resp);
                                }
                            }

                        }
                        else
                        {
                            resp.success = false;
                            resp.message = "NO SE COMPLETO FINALIZAR ORDEN-3 " + orden.ordn_id;
                            return Json(resp);
                        }
                    }
                    else
                    {
                        resp.success = false;
                        resp.message = "NO SE COMPLETO FINALIZAR ORDEN-2 " + orden.ordn_id;
                        return Json(resp);
                    }
                }
                else
                {
                    resp.success = false;
                    resp.message = "NO SE COMPLETO FINALIZAR ORDEN " + orden.ordn_id;
                    return Json(resp);
                }
            }catch(Exception ex){
                log.Error("Error en finOrden en " + ex.Message);
                resp.success = false;
                resp.message ="ERROR "+ex.Message;
            }
            finally
            {
                myConnection.Close();
            }
            try {
                printOrden(orden);
            }
            catch(Exception ex)
            {
                log.Error("Error printOrden en " + ex.Message);
                log.Error(ex.StackTrace);
            }
            return Json(resp);
        }
        public void printOrden(RefOrden ordn)
        {
            log.Info("Solicitando printOrden de orden " + ordn.ordn_id);
            SqlDataReader reader = null;
            SqlConnection myConnection = new SqlConnection();
            SqlCommand command = null;
            PrintDocument pdoc = null;
            PrintDialog pd = new PrintDialog();
            pdoc = new PrintDocument();
            string sNombreImpresora = "";
            int iubicConsecOrden = 0;
            int irestOrden =0;
            try
            {
                myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["BaseComercial"].ConnectionString;
                myConnection.Open();
                command = new SqlCommand("select (select MESA.UBIC_CONSEC from MESA where MESA.MESA_ID = ORDEN.MESA_ID) as UBIC_CONSEC, (select MESA.REST_ID FROM MESA  where MESA.MESA_ID = ORDEN.MESA_ID) as REST_ID from ORDEN where ORDEN.ORDN_ID =" + ordn.ordn_id, myConnection);
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    iubicConsecOrden = Convert.ToInt32(reader["UBIC_CONSEC"].ToString());
                    irestOrden = Convert.ToInt32(reader["REST_ID"].ToString());
                }//

                command = new SqlCommand("select top 1 impr_conf from IMPRESORA where IMPRESORA.UBIC_CONSEC = " + iubicConsecOrden + " OR IMPRESORA.REST_ID=" + irestOrden, myConnection);
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    sNombreImpresora = reader["impr_conf"].ToString();
                }//fin while
            }
            catch (Exception ex) {
                log.Error("Error en printOrden " + ex.Message);
            }
            finally {
                myConnection.Close();
            }
            pdoc.PrinterSettings.PrinterName = sNombreImpresora;
            PrinterSettings ps = new PrinterSettings();
            Font font = new Font(sFont, 11);
            PaperSize psize = new PaperSize("Custom", 1000, 20);
            ps.DefaultPageSettings.PaperSize = psize;
            pd.Document = pdoc;
            pd.Document.DefaultPageSettings.PaperSize = psize;
            try
            {
                pdoc.PrintPage += (sender, e) => pdoc_PrintPageOrden(e, ordn);
                pdoc.Print();
            }catch(Exception ex){
                log.Error("Error en printOrden en " + ex.Message);
            }
        }