Exemple #1
0
        public void OrgAccToChangeBalance(FinOpViewModel finOpModel, decimal originalAmount)
        {
            var orgAccTo = _unitOfWork.OrganizationAccountRepository.GetOrgAccountById(finOpModel.AccToId);

            orgAccTo.CurrentBalance += finOpModel.Amount - originalAmount;
            _unitOfWork.OrganizationAccountRepository.Edit(orgAccTo);
        }
Exemple #2
0
 public FinOpViewModel CreateIncome(FinOpViewModel finOpModel)
 {
     FinOpInputDataValidation(finOpModel);
     try
     {
         var orgAcc = _unitOfWork.OrganizationAccountRepository.GetOrgAccountByName(finOpModel.OrgId, finOpModel.CardTo);
         var finOp  = new FinOp
         {
             Amount      = finOpModel.Sum,
             AccToId     = orgAcc.Id,
             Description = finOpModel.Description,
             TargetId    = finOpModel.TargetId,
             FinOpDate   = DateTime.Now,
         };
         _unitOfWork.FinOpRepository.Create(finOp);
         orgAcc.CurrentBalance += finOpModel.Sum;
         _unitOfWork.OrganizationAccountRepository.Edit(orgAcc);
         _unitOfWork.SaveChanges();
         return(finOpModel);
     }
     catch (Exception ex)
     {
         throw new BusinessLogicException("Щось пішло не так....О_о", ex);
     }
 }
Exemple #3
0
 public FinOpViewModel CreateTransfer(FinOpViewModel finOpModel)
 {
     FinOpInputDataValidation(finOpModel);
     try
     {
         var orgAccFrom = _unitOfWork.OrganizationAccountRepository.GetOrgAccountByName(finOpModel.OrgId, finOpModel.CardFrom);
         var orgAccTo   = _unitOfWork.OrganizationAccountRepository.GetOrgAccountByName(finOpModel.OrgId, finOpModel.CardTo);
         if (finOpModel.Sum > orgAccFrom.CurrentBalance)
         {
             throw new ArgumentException("Витрати не можуть перебільшувати баланс рахунку");
         }
         var finOp = new FinOp
         {
             Amount      = finOpModel.Sum,
             AccToId     = orgAccTo.Id,
             AccFromId   = orgAccFrom.Id,
             Description = finOpModel.Description,
             FinOpDate   = DateTime.Now,
         };
         _unitOfWork.FinOpRepository.Create(finOp);
         orgAccFrom.CurrentBalance -= finOpModel.Sum;
         _unitOfWork.OrganizationAccountRepository.Edit(orgAccFrom);
         orgAccTo.CurrentBalance += finOpModel.Sum;
         _unitOfWork.OrganizationAccountRepository.Edit(orgAccTo);
         _unitOfWork.SaveChanges();
         return(finOpModel);
     }
     catch (Exception ex)
     {
         throw new BusinessLogicException("Щось пішло не так....О_о", ex);
     }
 }
Exemple #4
0
 public FinOpViewModel CreateTransfer(FinOpViewModel finOpModel)
 {
     FinOpInputDataValidation(finOpModel);
     try
     {
         var orgAccFrom = _unitOfWork.OrganizationAccountRepository.GetOrgAccountById(finOpModel.AccFromId);
         var orgAccTo   = _unitOfWork.OrganizationAccountRepository.GetOrgAccountById(finOpModel.AccToId);
         if (finOpModel.Amount > orgAccFrom.CurrentBalance)
         {
             throw new ArgumentException(ErrorMessages.SpendingIsExceeded);
         }
         var finOp = new FinOp
         {
             Amount      = finOpModel.Amount,
             AccToId     = orgAccTo.Id,
             AccFromId   = orgAccFrom.Id,
             Description = finOpModel.Description,
             FinOpDate   = finOpModel.Date,
             FinOpType   = finOpModel.FinOpType,
             UserId      = finOpModel.UserId
         };
         _unitOfWork.FinOpRepository.Create(finOp);
         orgAccFrom.CurrentBalance -= finOpModel.Amount;
         _unitOfWork.OrganizationAccountRepository.Edit(orgAccFrom);
         orgAccTo.CurrentBalance += finOpModel.Amount;
         _unitOfWork.OrganizationAccountRepository.Edit(orgAccTo);
         _unitOfWork.SaveChanges();
         return(finOpModel);
     }
     catch (Exception ex)
     {
         throw new BusinessLogicException(ErrorMessages.OperationTransferError, ex);
     }
 }
Exemple #5
0
 /// <Summary>
 /// Gets the fin ops by id.
 /// </Summary>
 /// <param name="id">The fin ops identifier.</param>
 /// <returns></returns>
 public FinOpViewModel GetFinOpsById(int id)
 {
     try
     {
         if (id <= 0)
         {
             throw new BusinessLogicException(ErrorMessages.InvalidIdentificator);
         }
         var finOpFromDataBase = _unitOfWork.FinOpRepository.GetById(id);
         var finOp             = new FinOpViewModel
         {
             Id          = finOpFromDataBase.Id,
             AccFromId   = finOpFromDataBase.AccFromId.GetValueOrDefault(0),
             AccToId     = finOpFromDataBase.AccToId.GetValueOrDefault(0),
             Date        = finOpFromDataBase.FinOpDate,
             Description = finOpFromDataBase.Description,
             Amount      = finOpFromDataBase.Amount,
             TargetId    = finOpFromDataBase.TargetId,
             Target      = finOpFromDataBase.Target?.TargetName,
             FinOpType   = finOpFromDataBase.FinOpType,
             DonationId  = finOpFromDataBase.DonationId,
             IsEditable  = true
         };
         return(finOp);
     }
     catch (Exception ex)
     {
         throw new BusinessLogicException(ErrorMessages.EmptyFinOpList, ex);
     }
 }
Exemple #6
0
 ///
 public FinOpViewModel CreateIncome(FinOpViewModel finOpModel)
 {
     FinOpInputDataValidation(finOpModel);
     try
     {
         var orgAccTo = _unitOfWork.OrganizationAccountRepository.GetOrgAccountById(finOpModel.AccToId);
         var finOp    = new FinOp
         {
             Amount      = finOpModel.Amount,
             AccToId     = orgAccTo.Id,
             Description = finOpModel.Description,
             TargetId    = finOpModel.TargetId,
             FinOpDate   = finOpModel.Date,
             FinOpType   = finOpModel.FinOpType,
             UserId      = finOpModel.UserId
         };
         _unitOfWork.FinOpRepository.Create(finOp);
         orgAccTo.CurrentBalance += finOpModel.Amount;
         _unitOfWork.OrganizationAccountRepository.Edit(orgAccTo);
         _unitOfWork.SaveChanges();
         return(finOpModel);
     }
     catch (Exception ex)
     {
         throw new BusinessLogicException(ErrorMessages.OperationIncomeError, ex);
     }
 }
Exemple #7
0
 private void FinOpInputDataValidation(FinOpViewModel finOpModel)
 {
     if (finOpModel.Sum <= 0 || finOpModel.Sum > 1000000)
     {
         throw new ArgumentException(ErrorMessages.MoneyFinOpLimit);
     }
 }
Exemple #8
0
 //[Authorize(Roles = "admin, moderator")]
 public IActionResult Spending([FromBody] FinOpViewModel spendingModel)
 {
     if (spendingModel == null)
     {
         return(BadRequest());
     }
     return(Ok(_service.CreateSpending(spendingModel)));
 }
Exemple #9
0
 public IActionResult BindDonationAndFinOp([FromBody] FinOpViewModel finOp)
 {
     if (finOp == null)
     {
         return(BadRequest());
     }
     return(Ok(_service.BindDonationAndFinOp(finOp)));
 }
Exemple #10
0
 public IActionResult Edit([FromBody] FinOpViewModel finOp)
 {
     if (finOp == null)
     {
         return(BadRequest());
     }
     return(Ok(_service.EditFinOperation(finOp)));
 }
Exemple #11
0
 //[Authorize(Roles = "admin, moderator")]
 public IActionResult Income([FromBody] FinOpViewModel incomeModel)
 {
     if (incomeModel == null)
     {
         return(BadRequest());
     }
     return(Ok(_service.CreateIncome(incomeModel)));
 }
Exemple #12
0
 //[Authorize(Roles = "admin, moderator")]
 public IActionResult Transfer([FromBody] FinOpViewModel transferModel)
 {
     if (transferModel == null)
     {
         return(BadRequest());
     }
     return(Ok(_service.CreateTransfer(transferModel)));
 }
Exemple #13
0
        public void Deposite(FinOpViewModel finOpModel, FinOp finOp)
        {
            var orgAccFrom = _unitOfWork.OrganizationAccountRepository.GetOrgAccountById(finOpModel.AccFromId);

            orgAccFrom.CurrentBalance -= finOpModel.Amount;
            finOp.FinOpType            = finOpModel.FinOpType;
            finOp.AccToId              = finOpModel.AccToId;
            _unitOfWork.OrganizationAccountRepository.Edit(orgAccFrom);
        }
Exemple #14
0
        public void Windtrhaw(FinOpViewModel finOpModel, FinOp finOp)
        {
            var orgAccTo = _unitOfWork.OrganizationAccountRepository.GetOrgAccountById(finOpModel.AccToId);

            orgAccTo.CurrentBalance += finOpModel.Amount;
            finOp.FinOpType          = finOpModel.FinOpType;
            finOp.AccToId            = finOpModel.AccToId;
            _unitOfWork.OrganizationAccountRepository.Edit(orgAccTo);
        }
Exemple #15
0
        /// <Summary>
        /// Edit the finance operation.
        /// </Summary>
        /// <param name="finOpModel">The finance operation model.</param>
        /// <returns></returns>
        public FinOpViewModel EditFinOperation(FinOpViewModel finOpModel)
        {
            try
            {
                var finOp = _unitOfWork.FinOpRepository.GetById(finOpModel.Id);
                if (finOpModel.Amount != finOp.Amount)
                {
                    switch (finOpModel.FinOpType)
                    {
                    case Constants.FinOpTypeSpending:
                        OrgAccFromChangeBalance(finOpModel, finOp.Amount);
                        break;

                    case Constants.FinOpTypeIncome:
                        OrgAccToChangeBalance(finOpModel, finOp.Amount);
                        break;

                    case Constants.FinOpTypeTransfer:
                        OrgAccFromChangeBalance(finOpModel, finOp.Amount);
                        OrgAccToChangeBalance(finOpModel, finOp.Amount);
                        break;

                    default:
                        throw new BusinessLogicException(ErrorMessages.InvalidFinanceOperation);
                    }
                }
                if (finOpModel.FinOpType == Constants.FinOpTypeTransfer)
                {
                    if (finOp.FinOpType == Constants.FinOpTypeSpending)
                    {
                        Windtrhaw(finOpModel, finOp);
                    }

                    if (finOp.FinOpType == Constants.FinOpTypeIncome)
                    {
                        Deposite(finOpModel, finOp);
                    }
                }

                finOp.Description = finOpModel.Description;
                finOp.TargetId    = finOpModel.TargetId;
                finOp.FinOpDate   = finOpModel.Date;
                finOp.UserId      = finOpModel.UserId;
                finOp.DonationId  = finOpModel.DonationId;
                finOp.Amount      = finOpModel.Amount;
                _unitOfWork.FinOpRepository.Update(finOp);
                _unitOfWork.SaveChanges();
                return(finOpModel);
            }
            catch (Exception ex)
            {
                throw new BusinessLogicException(ErrorMessages.UpdateDataError, ex);
            }
        }
Exemple #16
0
        private FinOpViewModel ConvertFinOpFromBankViewModelToFinOpViewModel(FinOpFromBankViewModel finOpModel)
        {
            var finOp = new FinOpViewModel
            {
                Amount      = finOpModel.Amount,
                AccFromId   = finOpModel.CardFromId.GetValueOrDefault(0),
                AccToId     = finOpModel.CardToId.GetValueOrDefault(0),
                Description = finOpModel.Description,
                Date        = finOpModel.FinOpDate,
                FinOpType   = finOpModel.FinOpType,
                UserId      = finOpModel.UserId,
                OrgId       = finOpModel.OrgId,
                TargetId    = finOpModel.TargetId
            };

            return(finOp);
        }
        public async Task Create_Transfer_Operation_Ok_Response()
        {
            //Arrange
            var dbContext = _testContext.GetClearDbContext();

            FinOpViewModel transferModel = new FinOpViewModel()
            {
                Amount = 100, AccFromId = 1, AccToId = 2, Description = "TestTransferDescription", TargetId = null, Date = new DateTime(2017, 11, 3), FinOpType = 2, UserId = 1
            };
            var notModifiedOrgAccountFirst  = _testContext.TestFinOpRepository.GetTestOrgAccountById(1);
            var notModifiedOrgAccountSecond = _testContext.TestFinOpRepository.GetTestOrgAccountById(2);

            var testOrgAccounts = _testContext.TestFinOpRepository.GetTestOrgAccounts();

            _testContext.TestFinOpRepository.AddRangeOfOrgAccountsToDb(testOrgAccounts);

            //Act
            var content  = new StringContent(JsonConvert.SerializeObject(transferModel), Encoding.UTF8, "application/json");
            var response = await _testContext.Client.PostAsync($"/api/FinOp/Transfer", content);

            var stream = await response.Content.ReadAsStreamAsync();

            var reader       = new StreamReader(stream, Encoding.UTF8);
            var resultFinOps = new JsonSerializer().Deserialize <FinOpViewModel>(new JsonTextReader(reader));

            //Assert
            response.EnsureSuccessStatusCode();

            var modifiedOrgAccountFirst  = dbContext.OrgAccounts.Where(acc => acc.Id == 1).FirstOrDefault();
            var modifiedOrgAccountSecond = dbContext.OrgAccounts.Where(acc => acc.Id == 2).FirstOrDefault();


            Assert.True(HttpStatusCode.OK == response.StatusCode);
            Assert.NotNull(resultFinOps);
            Assert.True(dbContext.FinOps.Count() == 1);
            Assert.False(dbContext.FinOps.Count() == 8);
            Assert.True(modifiedOrgAccountFirst.CurrentBalance == 900);
            Assert.True(modifiedOrgAccountSecond.CurrentBalance == 1100);
            Assert.NotEqual(notModifiedOrgAccountFirst, modifiedOrgAccountFirst);
            Assert.NotEqual(notModifiedOrgAccountSecond, modifiedOrgAccountSecond);
            Assert.True(modifiedOrgAccountFirst.CurrentBalance == notModifiedOrgAccountFirst.CurrentBalance - resultFinOps.Amount);
            Assert.True(modifiedOrgAccountSecond.CurrentBalance == notModifiedOrgAccountSecond.CurrentBalance + resultFinOps.Amount);
        }
Exemple #18
0
 public FinOpViewModel BindDonationAndFinOp(FinOpViewModel finOp)
 {
     try
     {
         var donation = _unitOfWork.DonationRepository.Get((int)finOp.DonationId);
         donation.UserId = finOp.UserId;
         _unitOfWork.DonationRepository.Update(donation);
         var finOpEntity = _unitOfWork.FinOpRepository.GetById(finOp.Id);
         finOpEntity.UserId     = finOp.UserId;
         finOpEntity.DonationId = finOp.DonationId;
         _unitOfWork.FinOpRepository.Update(finOpEntity);
         _unitOfWork.SaveChanges();
         return(finOp);
     }
     catch (Exception e)
     {
         throw new BusinessLogicException(ErrorMessages.BindingDonationToFinOp, e);
     }
 }
Exemple #19
0
 public FinOpViewModel Transfer([FromBody] FinOpViewModel transferModel)
 {
     return(this._service.CreateTransfer(transferModel));
 }
Exemple #20
0
 public FinOpViewModel Spending([FromBody] FinOpViewModel spendingModel)
 {
     return(this._service.CreateSpending(spendingModel));
 }
Exemple #21
0
 public FinOpViewModel Income([FromBody] FinOpViewModel incomeModel)
 {
     return(this._service.CreateIncome(incomeModel));
 }