public bool TransferSpend(TransferSpendRequest transferSpendRequest)
        {
            string sql = @"UPDATE [dbo].[spends] SET
                        budget_id = (select id from budgets where reference_id = @transfertoreferenceid)
                        WHERE id = @spendid";

            DynamicParameters dp = new DynamicParameters();

            dp.Add("transfertoreferenceid", transferSpendRequest.TransferToBudgetReferenceId, DbType.Guid, ParameterDirection.Input, 60);
            dp.Add("spendid", transferSpendRequest.TransferFromSpendId, DbType.Int32, ParameterDirection.Input);

            using (IDbConnection conn = new SqlConnection(_connectionString))
            {
                conn.Open();
                conn.Execute(sql, dp);
                return(true);
            }
        }
Esempio n. 2
0
        public IHttpActionResult TransferSpend(TransferSpendRequest transferSpendRequest)
        {
            if (!GuidConverter.IsValid(transferSpendRequest.TransferToBudgetReferenceId.ToString()))
            {
                return(BadRequest("From budget referenceid is missing."));
            }
            if (transferSpendRequest.TransferFromSpendId <= 0)
            {
                return(BadRequest("To spend id is missing."));
            }

            // Ensure we are not tranfering within the SAME budget
            var budgetTo = _budgetService.GetBudgetListResponse(transferSpendRequest.TransferToBudgetReferenceId);
            var found    = budgetTo.Spends.Where(sp => sp.Id == transferSpendRequest.TransferFromSpendId).FirstOrDefault();

            if (found != null)
            {
                return(BadRequest("Invalid request. You are trying to transer withing the same budget."));
            }

            var loggedInUser = HttpContext.Current.User as SecurityPrincipal;

            logger.Info($"Spend is transfered from spend id {transferSpendRequest.TransferFromSpendId} to budget {transferSpendRequest.TransferToBudgetReferenceId} by {loggedInUser.ForeName}");

            var inserted = _budgetService.TransferSpend(transferSpendRequest);
            // add comments
            SpendComments spendComments = new SpendComments()
            {
                SpendId     = transferSpendRequest.TransferFromSpendId,
                Comments    = transferSpendRequest.Comments,
                CreatedById = loggedInUser.Id,
                Status      = Status.Completed
            };

            _budgetService.InsertSpendComment(spendComments);

            return(Ok(inserted));
        }
 public bool TransferSpend(TransferSpendRequest transferSpendRequest)
 {
     return(_spendDataProvider.TransferSpend(transferSpendRequest));
 }