Ejemplo n.º 1
0
        public IEnumerable <IResult> DeleteTransaction(PostingRecord transactionRecord)
        {
            var openConfirmationResult = new OpenConfirmationResult(eventAggregator)
            {
                Message =
                    string.Format(Resources.Strings.PostingViewModelBase_DeleteConfirmation_Message, transactionRecord.TransactionId),
                Title   = Resources.Strings.PostingViewModelBase_DeleteConfirmation_Title,
                Options = MessageBoxOptions.Yes | MessageBoxOptions.Cancel,
            };

            yield return(openConfirmationResult);

            if (openConfirmationResult.Selected == MessageBoxOptions.Yes)
            {
                // Load transaction from server (used below to determine if the deleted posting was transfer)
                var request = new GetPostingResult(AccountId, transactionRecord.TransactionId, eventAggregator);
                yield return(request);

                // Remove transaction on server
                var request2 = new DeleteTransactionResult(AccountId,
                                                           transactionRecord.TransactionId);
                yield return(request2);

                // Update accounts balance
                accountsRepository.Download(AccountId);

                // Update category usage
                if (transactionRecord.Category != null)
                {
                    categoriesRepository.Download(transactionRecord.Category.Id);
                }

                // For transfer the 2-nd account should also be updated
                if (request.Transaction is TransferDTO)
                {
                    int secondAccountId = ((TransferDTO)request.Transaction).SecondAccountId.Value;
                    accountsRepository.Download(secondAccountId);
                }

                // Remove transaction locally
                var transactionToDelete =
                    TransactionRecords.Where(record => record.TransactionId == transactionRecord.TransactionId).Single();
                var index = TransactionRecords.IndexOf(transactionToDelete);
                TransactionRecords.Remove(transactionToDelete);

                // Correct remained balance for following transactions
                if (TransactionRecords.Count > 0 && index < TransactionRecords.Count)
                {
                    var deletedAmount = transactionToDelete.Income > 0
                                                                ? -transactionToDelete.Income
                                                                : transactionToDelete.Expense;

                    for (int i = index; i < TransactionRecords.Count; i++)
                    {
                        ((PostingRecord)TransactionRecords[i]).Balance += deletedAmount;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public IEnumerable <IResult> Save()
        {
            if (IsEditMode)
            {
                // To not update date if it was not changed;
                // Add time of day to updated date only.
                DateTime date = OperationDate.Kind == DateTimeKind.Unspecified
                                                        ? DateTime.SpecifyKind(OperationDate, DateTimeKind.Local) + DateTime.Now.TimeOfDay
                                                        : OperationDate;

                var request = new EditTransactionResult(
                    transactionId.Value,
                    AccountId,
                    date.ToUniversalTime(),
                    decimal.Parse(Price.Trim()),
                    decimal.Parse(Quantity.Trim()),
                    Comment != null ? Comment.Trim() : null,
                    CurrentCategory != null
                                                ? CurrentCategory.Id
                                                : (int?)null,
                    IsDeposite,
                    eventAggregator
                    );


                yield return(request);
            }
            else
            {
                // Add time of day to date.
                DateTime date = OperationDate.Kind == DateTimeKind.Unspecified
                                                        ? DateTime.SpecifyKind(OperationDate, DateTimeKind.Local) + DateTime.Now.TimeOfDay
                                                        : OperationDate.Date + DateTime.Now.TimeOfDay;

                var request = new AddTransactionResult(AccountId,
                                                       date.ToUniversalTime(),
                                                       decimal.Parse(Price.Trim()),
                                                       decimal.Parse(Quantity.Trim()),
                                                       Comment != null ? Comment.Trim() : null,
                                                       CurrentCategory != null
                                                ? CurrentCategory.Id
                                                : (int?)null,
                                                       IsDeposite,
                                                       eventAggregator
                                                       );

                yield return(request);
            }

            accountsRepository.Download(AccountId);

            if (CurrentCategory != null)
            {
                categoryRepository.Download(CurrentCategory.Id);
            }

            if (previousCategory != null && CurrentCategory != null && CurrentCategory.Id != previousCategory.Id)
            {
                categoryRepository.Download(previousCategory.Id);
            }

            Cancel();
        }