private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                LblClase.Text            = "-";
                LblCliente.Text          = "-";
                LblFechaVencimiento.Text = "-";
                LblFecha.Text            = "-";
                LblNombreProd.Text       = "-";
                LblNuevaClaseMovil.Text  = "-";
                //LblNuevoNombreMovil.Text = "-";
                LblNumeroFactura.Text = "-";
                LblVendedor.Text      = "-";
                NCF = string.Empty;
                TxtCodigoNuevoTelefono.Text = "";
                TxtCodigoTelCliente.Text    = "";

                venta   = new Clases.VentaInmediata();
                cliente = new Clases.Clientes();
                fact    = new Clases.Facturas();
                cp      = new Clases.Clases_Productos();
                NP      = new Clases.Nombres_Productos();
                DB      = new Clases.IM_DB_Entities();

                TxtCodigoTelCliente.Focus();
            }
            catch (Exception)
            {
                return;
            }
        }
        private void TxtCodigoTelCliente_TextChanged(object sender, EventArgs e)
        {
            if (Buscando)
            {
                return;
            }

            Buscando = true;
            Program.wait(2);
            try
            {
                if (TxtCodigoTelCliente.Text.Trim() == "")
                {
                    button1_Click(sender, e);
                    Buscando = false;
                    return;
                }

                venta = new Clases.VentaInmediata();
                venta = DB.VentaInmediata.Where(p => p.IMEI == TxtCodigoTelCliente.Text).First();

                //venta = new Clases.Ventas();
                cliente = new Clases.Clientes();
                fact    = new Clases.Facturas();
                cp      = new Clases.Clases_Productos();
                NP      = new Clases.Nombres_Productos();


                NP = DB.Nombres_Productos.Where(np => np.ID_Nombre_Productos == venta.id_Nombre_Producto).FirstOrDefault();
                //cp = DB.Clases_Productos.Where(clase => clase.ID_CLase == Movil_Cliente.Clase).FirstOrDefault();
                //venta = DB.Ventas.Where(v => v.Producto == Movil_Cliente.ID_Producto).FirstOrDefault();
                fact    = DB.Facturas.Where(f => f.ID_Factura == venta.factura).FirstOrDefault();
                cliente = DB.Clientes.Where(c => c.RNC == fact.Cliente).FirstOrDefault();
                string IDVendedor = fact.vendedor;

                LblCliente.Text          = cliente.Nombre;
                LblFecha.Text            = fact.Fecha.ToString();
                LblClase.Text            = cp.Descripcion;
                LblFechaVencimiento.Text = fact.Fecha.AddDays(15).ToString();
                LblNombreProd.Text       = NP.Descripcion;
                LblNumeroFactura.Text    = fact.ID_Factura.ToString();
                LblVendedor.Text         = IDVendedor;
                Buscando = false;
            }
            catch (Exception es)
            {
                MensajeInfo(es.Message);
                TxtCodigoTelCliente.Text = "";
                TxtCodigoTelCliente.Focus();
                Buscando = false;
                return;
            }
        }
        Clases.Facturas facturaParaSustituir(Clases.Facturas referencia)
        {
            var _db     = new Clases.IM_DB_Entities();
            var prefijo = _db.Parametros_Generales.Where(x => x.NombreParametro.ToLower().Contains(referencia.TipoNCF.ToLower())).First().Valor;
            var fact    = new Clases.Facturas()
            {
                Anulada          = false,
                Cant_Articulos   = referencia.Cant_Articulos,
                Cliente          = referencia.Cliente,
                Fecha            = DateTime.Now,
                ID_Factura       = getSiguiente_Factura(),
                Monto            = referencia.Monto,
                Monto_Factura    = referencia.Monto_Factura,
                NCF              = Refresh_NCF(prefijo),
                vendedor         = referencia.vendedor,
                TipoPago         = referencia.TipoPago,
                TipoNCF          = referencia.TipoNCF,
                ReferenciaDePago = ""
            };

            return(fact);
        }
        private void CmdFacturar_Click(object sender, EventArgs e)
        {
            if (decimal.Parse(LblTotal.Text) <= 0)
            {
                MensajeInfo("El total de la factura esta en 0");
                return;
            }

            Clases.IM_DB_Entities DB = new Clases.IM_DB_Entities();
            Clases.Facturas       fact;
            List <Clases.Ventas>  ListaVentas = new List <Clases.Ventas>();


            try
            {
                if (Lista_Prod_A_Vender.Count > 0)
                {
                    fact = new Clases.Facturas()
                    {
                        Cant_Articulos = Lista_Prod_A_Vender.Count,
                        Cliente        = (string)CmbClientes.SelectedValue,
                        Fecha          = DateTime.Now,
                        Monto_Factura  = decimal.Parse(LblTotal.Text),
                        vendedor       = Program.LoggedUser.Usr_ID.ToString()
                    };

                    DB.Facturas.Add(fact);

                    foreach (ListViewItem prod in LstProds_A_Vender.Items)
                    {
                        ListaVentas.Add(
                            new Clases.Ventas()
                        {
                            Factura      = fact.ID_Factura,
                            Precio_Venta = decimal.Parse(prod.SubItems[3].Text),
                            Producto     = prod.SubItems[0].Text,
                        });
                    }

                    foreach (var item in ListaVentas)
                    {
                        DB.Ventas.Add(item);

                        Clases.Productos Prod = DB.Productos.Where(p => p.ID_Producto == item.Producto).First();

                        DB.Productos_V.Add(new Clases.Productos_V()
                        {
                            Clase          = Prod.Clase,
                            Fecha_Ingreso  = fact.Fecha,
                            ID_Producto    = Prod.ID_Producto,
                            NombreProducto = Prod.NombreProducto,
                            Precio_Costo   = Prod.Precio_Costo
                        });

                        //DB.Productos.Remove(Prod);
                    }
                }

                //DB.SaveChanges();

                GuardarArchivo("Factura_" + CmbClientes.Text + "_" + DateTime.Now.ToString("MMddyyyy_hhmm")
                               , Construir__Factura(ListaVentas, CmbClientes.Text));

                foreach (var item in ListaVentas)
                {
                    Clases.Productos _Prod = DB.Productos.Where(p => p.ID_Producto == item.Producto).First();

                    DB.Productos.Remove(_Prod);
                }

                DB.SaveChanges();
                MensajeInfo("Facturado!");
                Limpiar();
            }
            catch (Exception es)
            {
                MensajeError(es.Message);
                //Limpiar();
            }
        }