protected virtual IResult <TransactionType> GetTransactionType(Guid id)
        {
            try
            {
                var internalList = GetInternalList();
                var internalItem = internalList.FirstOrDefault(i => i.Id == id);

                if (null == internalItem)
                {
                    return(_resultFactory.Create <TransactionType>(
                               ResultCode.NoContent,
                               new List <Error>()
                    {
                        new Error(String.Format("No TransactionType found with Id {0}", id))
                    }, null));
                }
                else
                {
                    return(_resultFactory.Success(internalItem));
                }
            }
            catch (Exception ex)
            {
                return(_resultFactory.Exception <TransactionType>(ApplicationSegment.Database, 0, ex));
            }
        }
Пример #2
0
 public Result ValidateTransaction(Transaction transaction)
 {
     if (transaction.Entries.Sum(e => e.Amount) == transaction.NetDifference)
     {
         return(_resultFactory.Success());
     }
     else
     {
         return(_resultFactory.Create(new ResultCode(), new Error("Transaction does not balance")));
     }
 }
        public async Task <IResult <Transaction> > ApplyTransaction(Transaction transaction)
        {
            // TODO: Validation? Check accountholder status? account status? All entries applied?
            try
            {
                await Task.Run(() =>
                {
                    if (!transaction.IsValid())
                    {
                        throw new Exception("Transaction does not balance");
                    }

                    // Credit Account
                    if (transaction.TransactionType == TransactionType.Deposit ||
                        transaction.TransactionType == TransactionType.Transfer)
                    {
                        CreditAccount.ApplyEntries(transaction.Entries.Where(e => e.AccountId == CreditAccount.AccountId));
                    }

                    // Invoice Accounts
                    if (transaction.TransactionType == TransactionType.Transfer ||
                        transaction.TransactionType == TransactionType.Payment)
                    {
                        foreach (var invoiceAccount in InvoiceAccounts)
                        {
                            invoiceAccount.ApplyEntries(transaction.Entries.Where(e => e.AccountId == invoiceAccount.AccountId));
                        }
                    }

                    // global accts
                    if (transaction.TransactionType == TransactionType.WriteOff)
                    {
                        // TODO: WriteOffAccount? Or Apply externally?

                        //foreach (var unappliedEntry in transaction.Entries.Where(e => e.IsApplied == false))
                        //{
                        //    var account = InvoiceAccounts.FirstOrDefault(a => a.AccountId == unappliedEntry.AccountId);
                        //    if (null == account)
                        //        throw new InvalidOperationException("Unrecognized AccountId in transaction");
                        //    ((Account)account).ApplyEntry(unappliedEntry);
                        //}
                    }
                });

                return(_resultFactory.Success <Transaction>(transaction));
            }
            catch (Exception ex)
            {
                return(_resultFactory.Exception <Transaction>(ApplicationSegment.None, 0, ex));
            }
        }
Пример #4
0
        public async Task <IResult <LiveFeedData> > GetLiveFeedDataAsync()
        {
            try
            {
                var feed = await _apiClient.GetLiveFeedAsync();

                var mapped = _mapper.Map <LiveFeedData>(feed);

                return(_resultFactory.Success(mapped));
            }
            catch (Exception ex)
            {
                return(_resultFactory.Exception <LiveFeedData>(ex));
            }
        }
Пример #5
0
        /// <summary>
        /// Adds a purchase to an account.
        /// </summary>
        /// <param name="accountHolder"></param>
        /// <param name="purchase"></param>
        /// <returns></returns>
        //public async Task<Result> MakePurchase(AccountHolder accountHolder, Purchase purchase)
        //{
        //    _logger.LogDebug("Beginning A/R Purchase");
        //    // TODO: Save the changes
        //    try
        //    {
        //        Transaction transaction = TransactionFactory.GetPurchaseTransaction(
        //            accountHolder.PurchaseAccount.AccountId, purchase.Amount, purchase.RefId);

        //        if (!transaction.IsValid())
        //            return _resultFactory.Create(new ResultCode(), new Error("Transaction does not balance"));

        //        accountHolder.PurchaseAccount.Purchases.Add(purchase);
        //        await accountHolder.ApplyTransaction(transaction);
        //        return _resultFactory.Success();
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.ToString());
        //        return _resultFactory.Exception(ApplicationSegment.None, 0, ex);
        //    }
        //}

        /// <summary>
        /// Applies a payment to an invoice
        /// </summary>
        /// <param name="invoice"></param>
        /// <param name="amount"></param>
        /// <param name="refId"></param>
        /// <returns></returns>
        //public async Task<Result> MakeDeposit(AccountHolder accountHolder, Decimal amount, Guid? refId)
        //{
        //    _logger.LogDebug("Beginning A/R Deposit");
        //    // TODO: Save the changes
        //    try
        //    {
        //        Transaction transaction = TransactionFactory.GetDepositTransaction(
        //            accountHolder.CreditAccount.AccountId, amount, refId);

        //        if (!transaction.IsValid())
        //            return _resultFactory.Create(new ResultCode(), new Error("Transaction does not balance"));

        //        await accountHolder.ApplyTransaction(transaction);
        //        return _resultFactory.Success();
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.ToString());
        //        return _resultFactory.Exception(ApplicationSegment.None, 0, ex);
        //    }
        //}

        /// <summary>
        /// Applies a payment to an invoice
        /// </summary>
        /// <param name="invoice"></param>
        /// <param name="amount"></param>
        /// <param name="refId"></param>
        /// <returns></returns>
        //public async Task<Result> MakePayment(AccountHolder accountHolder, IInvoice invoice, Decimal amount, Guid? refId)
        //{
        //    _logger.LogDebug("Beginning A/R Payment");
        //    // TODO: Save the changes
        //    try
        //    {
        //        var transaction = TransactionFactory.GetPaymentTransaction(invoice.AccountId, amount, null);

        //        if (!transaction.IsValid())
        //            return _resultFactory.Create(new ResultCode(), new Error("Transaction does not balance"));

        //        await accountHolder.ApplyTransaction(transaction);
        //        return _resultFactory.Success();
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.ToString());
        //        return _resultFactory.Exception(ApplicationSegment.None, 0, ex);
        //    }
        //}

        /// <summary>
        /// Writes off all the value on an account
        /// </summary>
        /// <param name="accountHolder"></param>
        /// <param name="accountId"></param>
        /// <returns></returns>
        //public async Task<Result> WriteOffAccount(AccountHolder accountHolder, Guid accountId)
        //{
        //    _logger.LogDebug("Beginning A/R Write-Off");
        //    // TODO: Save the changes
        //    var transaction = TransactionFactory.GetWriteOffTransaction(Common.WriteOffAccountId, accountId, 0, null);

        //    if (!transaction.IsValid())
        //        return _resultFactory.Create(new ResultCode(), new Error("Transaction does not balance"));

        //    await accountHolder.ApplyTransaction(transaction);
        //    return _resultFactory.Success();
        //}

        /// <summary>
        /// Creates a new invoice account for all uninvoiced purchases.
        /// </summary>
        /// <param name="accountHolder">The AccountHolder to process</param>
        /// <returns></returns>
        //public async Task<Result> InvoiceOutstandingPurchases(AccountHolder accountHolder)
        //{
        //    _logger.LogDebug("Beginning A/R Invoice Purchases");
        //    // TODO: Save the changes
        //    try
        //    {
        //        return await Task.Run(() =>
        //        {
        //            if (accountHolder.PurchaseAccount.Purchases.Count > 0)
        //                accountHolder.InvoiceAccounts.Add(new InvoiceAccount(accountHolder.PurchaseAccount, DateTime.Now));

        //            accountHolder.PurchaseAccount = new UninvoicedPurchaseAccount();
        //            return _resultFactory.Success();
        //        });
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine(ex.ToString());
        //        return _resultFactory.Exception(ApplicationSegment.None, 0, ex);
        //    }
        //}

        /// <summary>
        /// Creates a payment schedule for the invoiced account
        /// </summary>
        /// <param name="accountHolder"></param>
        /// <param name="invoice"></param>
        /// <returns></returns>
        public async Task <IResult> CreatePaymentSchedule(AccountHolder accountHolder, IInvoice invoice)
        {
            _logger.LogDebug("Beginning A/R Create Payment Schedule");
            // TODO: Save the changes
            try
            {
                return(await Task.Run(() =>
                {
                    // TODO: Create the schedule
                    return _resultFactory.Success();
                }));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return(_resultFactory.Exception(ApplicationSegment.None, 0, ex));
            }
        }
Пример #6
0
 public virtual async Task <IResult <T> > GetDataAsync()
 {
     return(await Task.FromResult(_resultFactory.Success(_data[Position++])));
 }