public async Task <IActionResult> Post(long Id, [FromBody] AddEntryViewModel model) { if (model == null) { return(BadRequest()); } if (model == null) { return(BadRequest()); } if (_periodManager.GetPeriod() == Guid.Empty) { ModelState.AddModelError("", Resources.Global.Common.FinancialPeriodMustBeSpecified); return(BadRequest(ModelState.GetWithErrorsKey())); } var financialPeriod = await _financialPeriodRepo.FindAsync(_periodManager.GetPeriod()); if (financialPeriod == null) { return(NotFound("FinancialPeriodNotFound")); } if (!financialPeriod.CheckIfDateInPeriod(model.Date)) { ModelState.AddModelError("Date", Resources.Global.Common.DateOutCurrenctPeriod); return(BadRequest(ModelState.GetWithErrorsKey())); } #region checks Guid currencyId; var currency = await _currencyRepo.GetAsync(model.CurrencyId); if (currency != null) { currencyId = currency.Id; model.CurrencyValue = currency.Value; } else { currencyId = _defaultKeysOptions.Value.CurrencyId; model.CurrencyValue = 1; } Guid?costCenterId = null; if (model.CostCenterId.HasValue) { var costCenter = await _costCenterRepo.GetAsync(model.CostCenterId.Value); if (costCenter == null) { return(NotFound(Resources.CostCenters.CostCenterResource.CostCenterNotFound)); } costCenterId = costCenter.Id; } Guid?branchId = null; if (model.BranchId.HasValue) { var branch = await _branchRepo.GetAsync(model.BranchId.Value); if (branch == null) { return(NotFound(Resources.Branchs.BranchResource.BranchNotFound)); } branchId = branch.Id; } // get new number value if the value from model is exist if (!await _entryRepo.CheckIfNotExistAsync(model.Number)) { model.Number = await _entryRepo.GetNextNumberAsync(); } #endregion var entry = new Entry(model.Number, currencyId, model.CurrencyValue, branchId, model.Note); var itemsIndex = 0; // check form items if not found foreach (var item in model.Items) { itemsIndex++; Guid accountId; var account = await _accountRepo.GetAsync(item.AccountId); if (account == null) { return(NotFound($"account in entry item {itemsIndex} not found")); } accountId = account.Id; Guid itemCurrencyId; if (model.CurrencyId == item.CurrencyId) { itemCurrencyId = currencyId; item.CurrencyValue = model.CurrencyValue; } else { var itemCurrency = await _currencyRepo.GetAsync(item.CurrencyId.Value); if (itemCurrency != null) { itemCurrencyId = itemCurrency.Id; item.CurrencyValue = itemCurrency.Value; } } Guid?itemCostCenterId = null; if (model.CostCenterId.HasValue && item.CostCenterId.HasValue && model.CostCenterId == item.CostCenterId) { itemCostCenterId = costCenterId; } else { if (item.CostCenterId.HasValue) { var costCenter = await _costCenterRepo.GetAsync(item.CostCenterId.Value); if (costCenter == null) { return(NotFound(Resources.CostCenters.CostCenterResource.CostCenterNotFound)); } itemCostCenterId = costCenter.Id; } } var entryItem = new EntryItem(accountId, item.Debit, item.Credit, itemCurrencyId, item.CurrencyValue.Value, costCenterId, item.Date.Value.UtcDateTime, item.Note); entry.Items.Add(entryItem); } await _accountBalanceService.PostEntryToAccounts(entry); var affectedRows = await _entryRepo.AddAsync(entry); if (affectedRows > 0) { var viewModel = AutoMapper.Mapper.Map <EntryViewModel>(entry); return(CreatedAtRoute("GetEntry", new { id = entry.Number }, viewModel)); } return(BadRequest()); }