public async Task <ActionResult <BudgetDto> > CreateBudgetAsync([FromBody] CreateBudgetDto budgetForCreateDto)
        {
            var newBudget = mapper.Map <Budget>(budgetForCreateDto);

            newBudget.TaxLiability = new TaxLiability();

            if (!IsUserAuthorizedForResource(newBudget))
            {
                return(Unauthorized("You can only create budgets for yourself."));
            }

            budgetRepository.Add(newBudget);

            var saveResult = await budgetRepository.SaveAllAsync();

            if (!saveResult)
            {
                return(BadRequest("Unable to create budget."));
            }

            var budgetForReturn = mapper.Map <BudgetDto>(newBudget);

            return(CreatedAtRoute("GetBudgetAsync", new { id = budgetForReturn.Id }, budgetForReturn));
        }
        public async Task <ActionResult <Budget> > Post(Budget entity)
        {
            await _repository.Add(entity);

            return(CreatedAtAction("Get", new { id = entity.Id }, entity));
        }
Example #3
0
        public ActionResult Budget(CreateBudgetModel form)
        {
            try
            {
                // 1. Validate account.
                Account account;
                using (var accRepo = new AccountRepository())
                {
                    account = accRepo.GetById(form.AccountID);
                    // if not then, Add to database
                    if (account == null)
                    {
                        account = new Account()
                        {
                            AccountID   = form.AccountID,
                            AccountName = form.AccountName,
                            Status      = RecordStatus.Active
                        };
                        accRepo.Add(account);
                        accRepo.Save();
                    }
                }

                // 2. Check Budget.
                Budget budget;
                using (var budgetRepo = new BudgetRepository())
                {
                    // 2.1 check budget is already in database
                    budget = budgetRepo.Get().Where(
                        b =>
                        b.Year == form.Year &&
                        b.AccountID == form.AccountID &&
                        b.CostCenterID == form.CostCenterID &&
                        b.Status == BudgetStatus.Active
                        ).FirstOrDefault();

                    if (budget != null)
                    {
                        throw new Exception("งบประมาณนี้มีอยู่แล้วในระบบ");
                    }
                    else // Add new budget
                    {
                        budget = new Budget()
                        {
                            BudgetID       = Guid.NewGuid(),
                            AccountID      = form.AccountID,
                            CostCenterID   = form.CostCenterID,
                            Sequence       = 0,
                            Year           = form.Year,
                            BudgetAmount   = form.Amount,
                            WithdrawAmount = 0,
                            RemainAmount   = 0,
                            Status         = BudgetStatus.Active
                        };

                        budgetRepo.Add(budget);
                        budgetRepo.Save();
                    }
                }

                // 3. Set return value
                returnobj.SetSuccess(budget);
            }
            catch (Exception ex)
            {
                returnobj.SetError(ex.Message);
            }

            return(Content(returnobj.ToJson(), "application/json"));
        }