public List<clsResultado> consulta(DateTime fecha)
        {
            try
            {

                EntitiesContabilidad2 ent = new EntitiesContabilidad2();
                List<clsResultado> lista = new List<clsResultado>();
                var sel = ent.reporteResultado(fecha);
                foreach (var item in sel)
                {
                    clsResultado clas = new clsResultado();
                    clas.Saldo = item.Saldo;
                    if (item.Codigo.Length > 22)
                    {
                        clas.Codigo = item.Codigo.Substring(0,20);

                    }
                    else
                    {
                        clas.Codigo = item.Codigo;
                    }
                    clas.Cuenta = item.Cuenta;
                    lista.Add(clas);
                }
                return lista;
            }
            catch (Exception)
            {
                return null;
            }
        }
 public clsCabeceraComprobante consultaComprobante(decimal NumeroCom, int idEmpresa)
 {
     try
     {
         clsCabeceraComprobante cabecera = new clsCabeceraComprobante();
         EntitiesContabilidad2 ent = new EntitiesContabilidad2();
         var cab = (from q in ent.CabeceraComprobante where q.IdEmpresa == idEmpresa && q.numero_comprobante == NumeroCom select q).First();
         cabecera.fecha = Convert.ToDateTime(cab.fecha);
         cabecera.glosa = cab.glosa;
         cabecera.IdEmpresa = cab.IdEmpresa;
         cabecera.numero_comprobante = cab.numero_comprobante;
         cabecera.periodo_contable = Convert.ToDecimal(cab.periodo_contable);
         cabecera.periodo_contable_IdAFiscal = Convert.ToDecimal(cab.periodo_contable_IdAFiscal);
         ////////////////////////////////
         var det = (from q in ent.DetalleComprobante where q.IdEmpresa == idEmpresa && q.cabecera_comprobante == NumeroCom select q);
         List<clsDetalleComprobante> lista = new List<clsDetalleComprobante>();
         foreach (var item in det)
         {
             clsDetalleComprobante detalle = new clsDetalleComprobante();
             detalle.cuenta = item.cuenta;
             detalle.debe = item.debe;
             detalle.haber = item.haber;
             detalle.linea_comprobante = item.linea_comprobante;
             lista.Add(detalle);
         }
         cabecera.Detalle = lista;
         return cabecera;
     }
     catch (Exception)
     {
         return null;
     }
 }
        //public List<clsTipoTransaccion> Consulta(int Idtransaccion)
        public List<clsTipoTransaccion> Consulta()
        {
            try
            {
                List<clsTipoTransaccion> lista = new List<clsTipoTransaccion>();
                EntitiesContabilidad2 ent = new EntitiesContabilidad2();

                //where tt.IdTransaccion.Equals(Idtransaccion)
                var tipotran = from tt in ent.vwTipoTransaccion select tt;

                foreach (var van in tipotran)
                {
                    clsTipoTransaccion ctipo = new clsTipoTransaccion();
                    //las variables puden ser nulas u no soportan el tipo de dato int por eso se realiza el convert
                    ctipo.IdTransaccion = Convert.ToInt32(van.IdTransaccion);
                    ctipo.modulo = van.modulo;
                    ctipo.nombremodulo = van.NombreModulo;
                    ctipo.TipoDocumento = van.TipoDocumento;
                    ctipo.descripcion = van.descripcion;
                    lista.Add(ctipo);

                }
                return lista;
            }
            catch (Exception)
            {

                return null;
            }
        }
        public List<clsCuenta> ConsultaCuentaComprobante()
        {
            try
             {
                 List<clsCuenta> lista = new List<clsCuenta>();

                 EntitiesContabilidad2 entity = new EntitiesContabilidad2();

                 var plancuenta = from cuenta in entity.Cuenta where cuenta.IdNivelCuenta == 5 select cuenta;

                 foreach (var item in plancuenta)
                 {
                     clsCuenta ccuenta = new clsCuenta();
                     ccuenta.IdEmpresa = item.IdEmpresa;
                     ccuenta.IdCuenta = item.IdCuenta;
                     ccuenta.codigo_padre = item.codigo_padre;
                     ccuenta.nombre = item.nombre;
                     ccuenta.descripcion = item.descripcion;
                     ccuenta.IdNivelCuenta = item.IdNivelCuenta;
                     ccuenta.IdTipoCuenta = item.IdTipoCuenta;
                     ccuenta.naturaleza_cuenta = item.naturaleza_cuenta;
                     ccuenta.IdUsuario = Convert.ToInt32(item.IdUsuario);
                     ccuenta.FechaModficacion = Convert.ToDateTime(item.FechaModificacion);
                     lista.Add(ccuenta);
                 }

                 return lista;
             }
             catch (Exception)
             {

                 return null;
             }
        }
 //MANTENIMIENTO ELIMIAR
 public void eliminar(clsTipoTransaccion tiptran)
 {
     using (EntitiesContabilidad2 ent = new EntitiesContabilidad2())
     {
         var x = (from q in ent.TipoTransaccion where q.IdTransaccion == tiptran.IdTransaccion select q).First();
         ent.DeleteObject(x);
         ent.SaveChanges();
     }
 }
        public Boolean GuardarCabecera(ref clsCabeceraComprobante dato)
        {
            try
            {
                if (dato.Detalle.Sum(q => q.debe) != dato.Detalle.Sum(q => q.haber))
                {
                    return false;
                }
                EntitiesContabilidad2 enti =new EntitiesContabilidad2();
                decimal numero=0;
                try
                {
                    numero = (from q in enti.CabeceraComprobante where q.IdEmpresa==1 select q.numero_comprobante).Max()+1;
                }
                catch (Exception)
                {
                    numero = 1;
                }
                dato.numero_comprobante = numero;
                using (EntitiesContabilidad2 ent =new EntitiesContabilidad2())
                {
                    CabeceraComprobante cab = new CabeceraComprobante() {
                    IdEmpresa=dato.IdEmpresa,
                    numero_comprobante=numero,
                    fecha=dato.fecha,
                    glosa=dato.glosa,
                    TipoTransaccion = (dato.TipoTransaccion==0)?null:dato.TipoTransaccion,

                    periodo_contable = Convert.ToDecimal((from q in ent.PeriodoContable where q.FechaInicio < DateTime.Now && q.FechaFin > DateTime.Now select q.IdPeriodoContable).First()),
                    periodo_contable_IdAFiscal = DateTime.Now.Year,
                    //periodo_contable
                    //periodo_contable_IdAFiscal
                    //IdUsuario
                    //FechaModificacion

                    };
                    ent.AddToCabeceraComprobante(cab);
                    ent.SaveChanges();
                }
                GuardarDetalle(dato);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        //MANTENIMIENTO GUARDAR
        public void guardar(clsTipoTransaccion tiptran)
        {
            using (EntitiesContabilidad2 ent = new EntitiesContabilidad2())
            {
                TipoTransaccion tt = new TipoTransaccion()
                {
                    IdTransaccion = tiptran.IdTransaccion,
                    modulo = tiptran.modulo,
                    TipoDocumento = tiptran.TipoDocumento,
                    descripcion = tiptran.descripcion

                };
                ent.AddToTipoTransaccion(tt);
                ent.SaveChanges();

            }
        }
        public List<clsvwlibrodiario> Consulta(DateTime _fechaini, DateTime _fechafin)
        {
            try
            {
                List<clsvwlibrodiario> lista = new List<clsvwlibrodiario>();

                EntitiesContabilidad2 ent = new EntitiesContabilidad2();
                var datos = from tt in ent.vwlibrodiario select tt;
                //var datos = from tt in ent.vwlibrodiario where (tt.fecha >= _fechaini && tt.fecha <= _fechafin ) select tt;

                foreach (var item in datos)
                {
                    clsvwlibrodiario clase = new clsvwlibrodiario();
                    if ((item.fecha) >= _fechaini && (item.fecha) <= _fechafin)
                        {
                        clase.IdEmpresa = Convert.ToInt32(item.IdEmpresa);
                        clase.numero_comprobante = Convert.ToInt32(item.numero_comprobante);
                        clase.fecha = Convert.ToDateTime(item.fecha);
                        clase.glosa = item.glosa;
                        clase.cuenta = Convert.ToInt32(item.cuenta);
                        clase.nombre = item.nombre;
                        clase.debe = Convert.ToDecimal(item.debe);
                        clase.haber = Convert.ToDecimal(item.haber);
                        clase.tipoTransaccion = Convert.ToInt32(item.TipoTransaccion);
                        clase.nombreModulo = Convert.ToString(item.NombreModulo);
                        lista.Add(clase);
                        }
                }
                return lista;
            }
            catch (Exception)
            {

                return null;
            }
        }
        public Boolean RevertirC(decimal NumeroCom, int idEmpresa,ref decimal  NumComRevert)
        {
            try
            {
                clsCabeceraComprobante dato = new clsCabeceraComprobante();
                dato=consultaComprobante(NumeroCom, idEmpresa);
                if (dato.Detalle.Sum(q => q.debe) != dato.Detalle.Sum(q => q.haber))
                {
                    return false;
                }
                EntitiesContabilidad2 enti = new EntitiesContabilidad2();
                decimal numero = 0;
                try
                {
                    numero = (from q in enti.CabeceraComprobante where q.IdEmpresa == 1 select q.numero_comprobante).Max()+1;
                }
                catch (Exception)
                {
                    numero = 1;
                }
                using (EntitiesContabilidad2 ent = new EntitiesContabilidad2())
                {
                    CabeceraComprobante cab = new CabeceraComprobante()
                    {
                        IdEmpresa = dato.IdEmpresa,
                        numero_comprobante = numero,
                        fecha = dato.fecha,
                        glosa = "Reverso del Comprobante # "+Convert.ToString(dato.numero_comprobante)+" => "+dato.glosa
                        //periodo_contable
                        //periodo_contable_IdAFiscal
                        //IdUsuario
                        //FechaModificacion

                    };
                    dato.numero_comprobante = numero;
                    ent.AddToCabeceraComprobante(cab);
                    ent.SaveChanges();
                    NumComRevert = numero;
                }
                RevertirD(dato);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        private Boolean RevertirD(clsCabeceraComprobante dato)
        {
            try
            {
                int secuencia = 0;
                foreach (var item in dato.Detalle)
                {
                    secuencia = secuencia + 1;
                    using (EntitiesContabilidad2 ent = new EntitiesContabilidad2())
                    {
                        DetalleComprobante det = new DetalleComprobante()
                        {
                            IdEmpresa = dato.IdEmpresa,
                            cabecera_comprobante = dato.numero_comprobante,
                            linea_comprobante = secuencia,
                            cuenta = item.cuenta,
                            debe = item.haber,
                            haber = item.debe
                        };
                        ent.AddToDetalleComprobante(det);
                        ent.SaveChanges();
                    }

                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public Boolean Modificar(clsCuenta cuenta)
        {
            try
             {
                 EntitiesContabilidad2 ent = new EntitiesContabilidad2();
                     var mod = (from q in ent.Cuenta where q.IdEmpresa == cuenta.IdEmpresa && q.IdCuenta == cuenta.IdCuenta select q).First();
                     mod.descripcion = cuenta.descripcion;
                     mod.nombre = cuenta.nombre;
                     mod.FechaModificacion = DateTime.Now;
                     ent.SaveChanges();

                 return true;
             }
             catch (Exception)
             {
                 return false;
             }
        }
 public Boolean Eliminar(clsCuenta cuenta)
 {
     try
      {
          EntitiesContabilidad2 ent = new EntitiesContabilidad2();
          var regcuenta = (from q in ent.Cuenta where q.IdEmpresa == cuenta.IdEmpresa && q.IdCuenta == cuenta.IdCuenta select q).First();
          var count = (from q in ent.Cuenta where q.codigo_padre == cuenta.IdCuenta select q).Count();
          if (count == 0)
          {
              ent.DeleteObject(regcuenta);
              ent.SaveChanges();
          }
          else { return false; }
          return true;
      }
      catch (Exception)
      {
          return false;
      }
 }
 //MANTENIMIENTO MODIFICAR
 public void modificar(clsTipoTransaccion tiptran)
 {
     using (EntitiesContabilidad2 ent = new EntitiesContabilidad2())
     {
         var x = (from q in ent.TipoTransaccion where q.IdTransaccion == tiptran.IdTransaccion select q).First();
         x.modulo = tiptran.modulo;
         x.TipoDocumento = tiptran.TipoDocumento;
         x.descripcion = tiptran.descripcion;
         ent.SaveChanges();
     }
 }
        public List<clsConsultaComprobante> ListaCom()
        {
            try
            {
                EntitiesContabilidad2 ent = new EntitiesContabilidad2();
                var sel = from q in ent.CabeceraComprobante
                          join w in ent.DetalleComprobante on q.numero_comprobante equals w.cabecera_comprobante
                          where q.IdEmpresa == w.IdEmpresa
                          select new { q.IdEmpresa,q.numero_comprobante,q.glosa,q.fecha ,w.debe,w.haber};
                var sel2 = from q in sel
                           group q by new { q.numero_comprobante, q.glosa,q.fecha,q.IdEmpresa } into g
                           select new { g.Key.numero_comprobante, g.Key.IdEmpresa, g.Key.glosa, g.Key.fecha, debe = g.Sum(r => r.debe), haber = g.Sum(r => r.debe) };
                List<clsConsultaComprobante> lista = new List<clsConsultaComprobante>();

                foreach (var item in sel2)
                {
                    clsConsultaComprobante clase = new clsConsultaComprobante();
                    clase.IdEmpresa = item.IdEmpresa;
                    clase.glosa = item.glosa;
                    clase.numero_comprobante = item.numero_comprobante;
                    clase.fecha = item.fecha;
                    clase.haber = item.haber;
                    clase.debe = item.debe;
                    lista.Add(clase);
                }
                return lista;
            }
            catch (Exception)
            {
                return null;
            }
        }
        public Boolean insert(List<clsvwComprobanteMayorizar> lista)
        {
            try
            {
                using (EntitiesContabilidad2 ent =new EntitiesContabilidad2())
                {
                    foreach (var item in lista)
                    {
                        var s =from q in ent.DetalleComprobante where q.IdEmpresa==item.IdEmpresa && q.cabecera_comprobante==item.numero_comprobante select q;
                        foreach (var item2 in s)
                        {
                            try
                            {
                                EntitiesContabilidad2 ent2 = new EntitiesContabilidad2();
                                DateTime fecha=(from q in ent2.PeriodoContable where q.FechaInicio < item.fecha && q.FechaFin > item.fecha select q.FechaFin).First();
                                if ((from q in ent.Saldo where q.fecha == fecha select q).Count() > 0)
                                {
                                    var saldo = (from q in ent2.Saldo where q.fecha == fecha && q.cuenta == item2.cuenta select q).First();
                                    saldo.saldo_deudor = saldo.saldo_deudor + item2.debe;
                                    saldo.saldo_acreedor = saldo.saldo_acreedor + item2.haber;
                                    ent2.SaveChanges();
                                }
                                else {
                                    Saldo sald = new Saldo()
                                    {
                                        IdEmpresa = 1,
                                        cuenta = item2.cuenta,
                                        //esta fecha debe ser la fechafinal del periodo
                                        fecha = Convert.ToDateTime((from q in ent.PeriodoContable where q.FechaInicio < item.fecha && q.FechaFin > item.fecha select q.FechaFin).First()),
                                        //el debe y el haber se debe acumular segun la cuenta y la fecha del periodo si ya existe en la base
                                        saldo_deudor = item2.debe,
                                        saldo_acreedor = item2.haber,
                                        IdUsuario = 1,
                                        FechaModificacion = DateTime.Now
                                    };
                                    ent.AddToSaldo(sald);
                                }
                            }
                            catch (Exception)
                            {
                                Saldo sald = new Saldo()
                                {
                                    IdEmpresa = 1,
                                    cuenta = item2.cuenta,
                                    //esta fecha debe ser la fechafinal del periodo
                                    fecha = Convert.ToDateTime((from q in ent.PeriodoContable where q.FechaInicio < item.fecha && q.FechaFin > item.fecha select q.FechaFin).First()),
                                    //el debe y el haber se debe acumular segun la cuenta y la fecha del periodo si ya existe en la base
                                    saldo_deudor = item2.debe,
                                    saldo_acreedor = item2.haber,
                                    IdUsuario = 1,
                                    FechaModificacion = DateTime.Now
                                };
                                ent.AddToSaldo(sald);
                            }

                        }
                        SaldoxComprobante xc = new SaldoxComprobante()
                        {
                            IdEmpresa=1,
                            numero_comprobante=item.numero_comprobante
                        };
                        ent.AddToSaldoxComprobante(xc);
                    }

                    ent.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public List<clsvwComprobanteMayorizar> select(DateTime desde,DateTime hasta)
        {
            try
            {
                List<clsvwComprobanteMayorizar> lista = new List<clsvwComprobanteMayorizar>();
                EntitiesContabilidad2 ent = new EntitiesContabilidad2();
                var s = from q in ent.vwComprobanteMayorizar where q.IdEmpresa == 1 && q.fecha >= desde && q.fecha <= hasta select q;
                foreach (var item in s)
                {
                    clsvwComprobanteMayorizar clase = new clsvwComprobanteMayorizar();
                    clase.debe = Convert.ToDecimal(item.debe);
                    clase.fecha = item.fecha;
                    clase.haber = Convert.ToDecimal(item.haber);
                    clase.IdEmpresa = item.IdEmpresa;
                    clase.glosa = item.glosa;
                    clase.numero_comprobante = item.numero_comprobante;
                    lista.Add(clase);
                }

                return lista;
            }
            catch (Exception)
            {
                return null;
            }
        }
 public Boolean Guardar(clsCuenta cuenta)
 {
     try
      {
          using (EntitiesContabilidad2 ent =new EntitiesContabilidad2())
          {
              Cuenta ct = new Cuenta() {
              IdEmpresa=cuenta.IdEmpresa,
              IdCuenta = cuenta.IdCuenta,
              codigo_padre = cuenta.codigo_padre,
              nombre = cuenta.nombre,
              descripcion = cuenta.descripcion,
              IdNivelCuenta = cuenta.IdNivelCuenta,
              IdTipoCuenta = cuenta.IdTipoCuenta,
              naturaleza_cuenta = cuenta.naturaleza_cuenta,
              IdUsuario = 1,
              //FechaModificacion = cuenta.IdEmpresa
              };
              ent.AddToCuenta(ct);
              ent.SaveChanges();
          }
          return true;
      }
      catch (Exception)
      {
          return false;
      }
 }