public IList <TransaccionCuota> TraerResumenPorTarjetaAnioYMes(int tarjetaId, int anio, int mes)
        {
            var movimientos = TransaccionCuotaRepository.GetAll().Where(x => x.Transaccion.Tarjeta.Id == tarjetaId &&
                                                                        x.Fecha.Year == anio && x.Fecha.Month == mes);

            return(movimientos.OrderBy(x => x.Fecha).ToList());
        }
        public void GrabarCupon(int tarjetaId, DateTime fechaCupon,
                                int comercioId, int nroCupon, double importe,
                                int cantidadCuotas, string observaciones)
        {
            if (ExisteCupon(tarjetaId, fechaCupon, comercioId, nroCupon))
            {
                throw new Exception("El cupón ya existe");
            }

            var tarjeta = TarjetaRepository.GetById(tarjetaId);

            if (tarjeta == null)
            {
                throw new Exception("La Tarjeta es requerida");
            }

            var comercio = ComercioRepository.GetById(comercioId);

            if (comercio == null)
            {
                throw new Exception("El Comercio es requerido");
            }

            var fechaCierreTarjeta = TarjetaFechaCierreRepository.GetFechaDeCierrePorMesYAnio(tarjeta, fechaCupon.Month, fechaCupon.Year);

            if (fechaCierreTarjeta == null)
            {
                throw new Exception("La fecha de cierre de la Tarjeta es requerida");
            }

            var cuotas = CalcularCuotas(fechaCupon, importe, cantidadCuotas, tarjeta);

            // Creo la cabecera de la transaccion, o sea el cupon por el total del importe
            var transaccion = new Transaccion
            {
                Tarjeta            = tarjeta,
                Comercio           = comercio,
                FechaCompra        = fechaCupon,
                NumeroCupon        = nroCupon,
                Importe            = importe,
                CantidadCuotas     = cantidadCuotas,
                Observaciones      = observaciones,
                TarjetaFechaCierre = fechaCierreTarjeta
            };

            foreach (var cuota in cuotas)
            {
                var transaccionCuota = new TransaccionCuota
                {
                    Transaccion = transaccion,
                    NumeroCuota = cuota.NroCuota,
                    Fecha       = cuota.Fecha,
                    Importe     = cuota.Importe
                };
                TransaccionCuotaRepository.Add(transaccionCuota);
            }
        }
        public void MarcarVerificacionCuota(int cuotaId, bool verificado)
        {
            var cuota = TransaccionCuotaRepository.GetById(cuotaId);

            if (cuota == null)
            {
                return;
            }
            cuota.Verificado = verificado;
        }
        public void ActualizarCupon(int cuponId, int tarjetaId, DateTime fechaCupon,
                                    int comercioId, int nroCupon, double importe,
                                    int cantidadCuotas, string observaciones)
        {
            var cupon = TraerCuponPorId(cuponId);

            if (cupon == null)
            {
                throw new Exception("Cupón inexistente");
            }

            var fechaAnterior          = cupon.FechaCompra;
            var importeAnterior        = cupon.Importe;
            var cantidadCuotasAnterior = cupon.CantidadCuotas;

            var comercio = ComercioRepository.GetById(comercioId);

            if (comercio == null)
            {
                throw new Exception("El Comercio es requerido");
            }
            cupon.Comercio = comercio;

            cupon.FechaCompra    = fechaCupon;
            cupon.NumeroCupon    = nroCupon;
            cupon.Importe        = importe;
            cupon.CantidadCuotas = cantidadCuotas;
            cupon.Observaciones  = observaciones;

            if (fechaAnterior != fechaCupon || importeAnterior != importe ||
                cantidadCuotasAnterior != cantidadCuotas)
            {
                // Acá tengo que actualizar las cuotas
                EliminarCuotasDeCupon(cupon);
                var tarjeta = TarjetaRepository.GetById(tarjetaId);
                if (tarjeta == null)
                {
                    throw new Exception("La Tarjeta es requerida");
                }
                var cuotas = CalcularCuotas(fechaCupon, importe, cantidadCuotas, tarjeta);

                foreach (var cuota in cuotas)
                {
                    var transaccionCuota = new TransaccionCuota
                    {
                        Transaccion = cupon,
                        NumeroCuota = cuota.NroCuota,
                        Fecha       = cuota.Fecha,
                        Importe     = cuota.Importe
                    };
                    TransaccionCuotaRepository.Add(transaccionCuota);
                }
            }
        }
 private void EliminarCuotasDeCupon(Transaccion cupon)
 {
     cupon.TransaccionCuota.ToList().ForEach(c => TransaccionCuotaRepository.Remove(c));
 }