private PaymentViewModel GetLastOccurence(PaymentViewModel paymentVm)
        {
            var transcationList = paymentRepository.Data
                .Where(x => x.RecurringPayment == paymentVm.RecurringPayment)
                .OrderBy(x => x.Date)
                .ToList();

            return transcationList.LastOrDefault();
        }
        private async void Delete(PaymentViewModel paymentViewModel)
        {
            if (!await
                dialogService.ShowConfirmMessage(Strings.DeleteTitle, Strings.DeletePaymentViewModelConfirmationMessage))
            {
                return;
            }

            paymentRepository.Delete(paymentViewModel);
            LoadedCommand.Execute(null);
        }
 private double HandleTransferAmount(PaymentViewModel PaymentViewModel, double balance)
 {
     if (AccountRepository.Selected == PaymentViewModel.ChargedAccount)
     {
         balance -= PaymentViewModel.Amount;
     }
     else
     {
         balance += PaymentViewModel.Amount;
     }
     return balance;
 }
        public async Task<bool> CheckForRecurringPayment(PaymentViewModel payment)
        {
            if (!payment.IsRecurring)
            {
                return false;
            }

            return
                await
                    dialogService.ShowConfirmMessage(Strings.ChangeSubsequentPaymentTitle,
                        Strings.ChangeSubsequentPaymentMessage,
                        Strings.RecurringLabel, Strings.JustThisLabel);
        }
 /// <summary>
 ///     Creates an recurring PaymentViewModel based on the Financial PaymentViewModel.
 /// </summary>
 /// <param name="paymentViewModel">The financial PaymentViewModel the reuccuring shall be based on.</param>
 /// <param name="isEndless">If the recurrence is infinite or not.</param>
 /// <param name="recurrence">How often the PaymentViewModel shall be repeated.</param>
 /// <param name="enddate">Enddate for the recurring PaymentViewModel if it's not endless.</param>
 /// <returns>The new created recurring PaymentViewModel</returns>
 public static RecurringPaymentViewModel GetRecurringFromPaymentViewModel(PaymentViewModel paymentViewModel,
     bool isEndless,
     PaymentRecurrence recurrence,
     DateTime enddate = new DateTime())
 {
     return new RecurringPaymentViewModel
     {
         ChargedAccount = paymentViewModel.ChargedAccount,
         TargetAccount = paymentViewModel.TargetAccount,
         StartDate = paymentViewModel.Date,
         EndDate = enddate,
         IsEndless = isEndless,
         Amount = paymentViewModel.Amount,
         Category = paymentViewModel.Category,
         Type = paymentViewModel.Type,
         Recurrence = recurrence,
         Note = paymentViewModel.Note
     };
 }
        /// <summary>
        ///     Save a new payment or update an existin one.
        /// </summary>
        /// <param name="paymentVm">item to save</param>
        public void Save(PaymentViewModel paymentVm)
        {
            paymentVm.IsCleared = paymentVm.ClearPaymentNow;

            //delete recurring payment if isRecurring is no longer set.
            if (paymentVm.IsRecurring)
            {
                recurringDataAccess.Delete(paymentVm.RecurringPayment);
                paymentVm.RecurringPayment.Id = 0;
            }

            if (paymentVm.Id == 0)
            {
                data.Add(paymentVm);
                dataAccess.Add(paymentVm.GetPayment());
            }
            else
            {
                dataAccess.Update(paymentVm.GetPayment());
            }
        }
        //TODO: check if can be used with a recurring payment view model.
        /// <summary>
        ///     Checks if the recurring PaymentViewModel is up for a repetition based on the passed PaymentViewModel
        /// </summary>
        /// <param name="recurringPayment">Recurring PaymentViewModel to check.</param>
        /// <param name="relatedPaymentViewModel">PaymentViewModel to compare.</param>
        /// <returns>True or False if the PaymentViewModel have to be repeated.</returns>
        public static bool CheckIfRepeatable(RecurringPayment recurringPayment, PaymentViewModel relatedPaymentViewModel)
        {
            if (!relatedPaymentViewModel.IsCleared)
            {
                return false;
            }

            switch (new RecurringPaymentViewModel(recurringPayment).Recurrence)
            {
                case PaymentRecurrence.Daily:
                    return DateTime.Today.Date != relatedPaymentViewModel.Date.Date;

                case PaymentRecurrence.DailyWithoutWeekend:
                    return DateTime.Today.Date != relatedPaymentViewModel.Date.Date
                           && DateTime.Today.DayOfWeek != DayOfWeek.Saturday
                           && DateTime.Today.DayOfWeek != DayOfWeek.Sunday;

                case PaymentRecurrence.Weekly:
                    var daysWeekly = DateTime.Now - relatedPaymentViewModel.Date;
                    return daysWeekly.Days >= 7;

                case PaymentRecurrence.Biweekly:
                    var daysBiweekly = DateTime.Now - relatedPaymentViewModel.Date;
                    return daysBiweekly.Days >= 14;

                case PaymentRecurrence.Monthly:
                    return DateTime.Now.Month != relatedPaymentViewModel.Date.Month;

                case PaymentRecurrence.Yearly:
                    return (DateTime.Now.Year != relatedPaymentViewModel.Date.Year
                            && DateTime.Now.Month >= relatedPaymentViewModel.Date.Month)
                           || DateTime.Now.Year - relatedPaymentViewModel.Date.Year > 1;

                default:
                    return false;
            }
        }
        private void DeleteRecurringPaymentIfLastAssociated(PaymentViewModel item)
        {
            if (Data.All(x => x.RecurringPayment != item.RecurringPayment))
            {
                var recurringList = recurringDataAccess.GetList(x => x.Id == item.RecurringPayment.Id).ToList();

                foreach (var recTrans in recurringList)
                {
                    recurringDataAccess.Delete(recTrans);
                }
            }
        }
        /// <summary>
        ///     Deletes the passed recurring payment
        /// </summary>
        /// <param name="paymentToDelete">Recurring payment to delete.</param>
        public void DeleteRecurring(PaymentViewModel paymentToDelete)
        {
            var payments = Data.Where(x => x.Id == paymentToDelete.Id).ToList();

            recurringDataAccess.Delete(paymentToDelete.RecurringPayment);

            foreach (var payment in payments)
            {
                payment.RecurringPayment = null;
                payment.IsRecurring = false;
                Save(payment);
            }
        }
 private void SetDefaultPaymentViewModel(PaymentType paymentType)
 {
     SelectedPayment = new PaymentViewModel()
     {
         Type = paymentType,
         Date = DateTime.Now,
         // Assign empty category to reset the GUI
         Category = new Category()
     };
 }
        /// <summary>
        ///     Adds the PaymentViewModel amount from the selected account
        /// </summary>
        /// <param name="paymentVm">PaymentViewModel to add the account from.</param>
        public void AddPaymentAmount(PaymentViewModel paymentVm)
        {
            if (!paymentVm.IsCleared)
            {
                return;
            }

            PrehandleAddIfTransfer(paymentVm);

            Func<double, double> amountFunc = x =>
                paymentVm.Type == PaymentType.Income
                    ? x
                    : -x;

            if (paymentVm.ChargedAccount == null)
            {
                paymentVm.ChargedAccount = data.FirstOrDefault(x => x.Id == paymentVm.ChargedAccount.Id);
            }

            HandlePaymentViewModelAmount(paymentVm, amountFunc, GetChargedAccountFunc(paymentVm.ChargedAccount));
        }
 private void PrehandleAddIfTransfer(PaymentViewModel paymentVm)
 {
     if (paymentVm.Type == PaymentType.Transfer)
     {
         Func<double, double> amountFunc = x => x;
         HandlePaymentViewModelAmount(paymentVm, amountFunc, GetTargetAccountFunc());
     }
 }
        private void HandlePaymentViewModelAmount(PaymentViewModel paymentVm,
            Func<double, double> amountFunc,
            Func<PaymentViewModel, Account> getAccountFunc)
        {
            var account = getAccountFunc(paymentVm);
            if (account == null)
            {
                return;
            }

            account.CurrentBalance += amountFunc(paymentVm.Amount);
            Save(account);
        }
 /// <summary>
 ///     Removes the PaymentViewModel Amount from the charged account of this PaymentViewModel
 /// </summary>
 /// <param name="paymentVm">PaymentViewModel to remove the account from.</param>
 public void RemovePaymentAmount(PaymentViewModel paymentVm)
 {
     RemovePaymentAmount(paymentVm, paymentVm.ChargedAccount);
 }
        private void Edit(PaymentViewModel paymentViewModel)
        {
            paymentRepository.Selected = paymentViewModel;

            navigationService.NavigateTo(NavigationConstants.MODIFY_PAYMENT_VIEW, paymentViewModel);
        }
        private async void DeletePaymentViewModel(PaymentViewModel PaymentViewModel)
        {
            if (!await
                dialogService.ShowConfirmMessage(Strings.DeleteTitle, Strings.DeletePaymentViewModelConfirmationMessage))
            {
                return;
            }

            if (await paymentManager.CheckForRecurringPayment(PaymentViewModel))
            {
                paymentRepository.DeleteRecurring(PaymentViewModel);
            }

            accountRepository.RemovePaymentAmount(PaymentViewModel);
            paymentRepository.Delete(PaymentViewModel);
            LoadCommand.Execute(null);
        }
        /// <summary>
        ///     Deletes the passed payment and removes the item from cache
        /// </summary>
        /// <param name="paymentVmToDelete">Payment to delete.</param>
        public void Delete(PaymentViewModel paymentVmToDelete)
        {
            var payments = Data.Where(x => x.Id == paymentVmToDelete.Id).ToList();

            foreach (var payment in payments)
            {
                data.Remove(payment);
                dataAccess.Delete(paymentVmToDelete.GetPayment());

                // If this accountToDelete was the last finacial accountToDelete for the linked recurring accountToDelete
                // delete the db entry for the recurring accountToDelete.
                DeleteRecurringPaymentIfLastAssociated(payment);
            }
        }
        /// <summary>
        ///     Removes the PaymentViewModel Amount from the selected account
        /// </summary>
        /// <param name="paymentVm">PaymentViewModel to remove.</param>
        /// <param name="account">Account to remove the amount from.</param>
        public void RemovePaymentAmount(PaymentViewModel paymentVm, Account account)
        {
            if (!paymentVm.IsCleared)
            {
                return;
            }

            PrehandleRemoveIfTransfer(paymentVm);

            Func<double, double> amountFunc = x =>
                paymentVm.Type == PaymentType.Income
                    ? -x
                    : x;

            HandlePaymentViewModelAmount(paymentVm, amountFunc, GetChargedAccountFunc(account));
        }