public async Task <IActionResult> Put(long id, [FromBody] EditEntryViewModel model) { if (model == null) { return(BadRequest()); } if (model.Id != id) { return(BadRequest()); } var entry = await _entryRepo.GetAsync(id); if (entry == null) { return(NotFound("Entry Not Found!")); } #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; } #endregion await _accountBalanceService.PostEntryToAccounts(entry, true); entry.CurrencyId = currencyId; entry.CurrencyValue = model.CurrencyValue; entry.BranchId = branchId; entry.Note = model.Note; entry.Items = new HashSet <EntryItem>(); var itemsIndex = 0; // check form items if not found foreach (var item in model.EntryItems) { itemsIndex++; Guid accountId; var account = await _accountRepo.GetAsync(item.AccountId); if (account == null) { return(NotFound("account 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, itemCostCenterId, item.Date.Value.UtcDateTime, item.Note); entry.Items.Add(entryItem); } await _accountBalanceService.PostEntryToAccounts(entry); var affectedRows = await _entryRepo.EditAsync(entry); if (affectedRows > 0) { var viewModel = AutoMapper.Mapper.Map <EntryViewModel>(entry); return(CreatedAtRoute("GetEntry", new { id = entry.Number }, viewModel)); } return(BadRequest()); }
public async Task <IActionResult> Put(long id, [FromBody] EditPayEntryViewModel model) { if (model == null) { return(BadRequest()); } if (model.Id != id) { return(BadRequest()); } // why ? you have to get PayEntry by number (id) var entry = await _entryRepo.GetAsync(id); if (entry == null) { return(NotFound("Entry Not Found!")); } var payType = entry.PayEntry.PayType; var debitSum = payType.ShowItemDebitField ? model.Items.Sum(p => p.Debit) : 0; var creditSum = payType.ShowItemDebitField ? model.Items.Sum(p => p.Credit) : 0; Guid?payAccountId = null; var payAccount = await _accountRepo.GetAsync(model.PayAccountId.Value); if (payAccount != null) { payAccountId = payAccount.Id; } #region checks var ShowItemDebitField = model.Items.ToList().All(e => e.Debit != 0); var ShowItemCreditField = model.Items.ToList().All(e => e.Credit != 0); if (payAccount != null && ((ShowItemDebitField && creditSum != 0) || (ShowItemCreditField && debitSum != 0))) { return(NotFound("Error in writing entry Details ")); } if (ShowItemDebitField && ShowItemCreditField && payAccount != null) { return(NotFound("Error payAccount must be leaved empty in Daily or Beginning Entry ! ")); } if ((payType.ShowItemDebitField && payType.ShowItemCreditField) && payAccount == null) { var isBalance = debitSum == creditSum ? true : false; return(NotFound("Entry is not Balanced ! ")); } if ((ShowItemDebitField || ShowItemCreditField) && payAccount == null) { return(NotFound("Error payAccount must be Filled !! ")); } 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; } #endregion if (payType.AutoPostToAccounts) { await _accountBalanceService.PostEntryToAccounts(entry, true); } entry.CurrencyId = currencyId; entry.CurrencyValue = model.CurrencyValue; entry.BranchId = branchId; entry.Note = model.Note; entry.Items = new HashSet <EntryItem>(); if ((payType.ShowItemDebitField && payType.ShowItemCreditField)) { var itemsIndex = 0; // check form items if not found foreach (var item in model.Items) { itemsIndex++; Guid account1Id; var account1 = await _accountRepo.GetAsync(item.AccountId); if (account1 == null) { return(NotFound($"account in entry item {itemsIndex} not found")); } account1Id = account1.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(account1Id, item.Debit, item.Credit, itemCurrencyId, item.CurrencyValue.Value, costCenterId, item.Date.Value.UtcDateTime, item.Note); entry.Items.Add(entryItem); } } else if (payType.ShowItemDebitField || payType.ShowItemCreditField) { var itemsIndex = 0; // check form items if not found foreach (var item in model.Items) { itemsIndex++; Guid account1Id; var account1 = await _accountRepo.GetAsync(item.AccountId); if (account1 == null) { return(NotFound($"account in entry item {itemsIndex} not found")); } account1Id = account1.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(account1Id, item.Debit, item.Credit, itemCurrencyId, item.CurrencyValue.Value, costCenterId, item.Date.Value.UtcDateTime, item.Note); entry.Items.Add(entryItem); } var entryItem1 = new EntryItem(payAccountId.Value, creditSum, debitSum, currencyId, model.CurrencyValue, costCenterId, model.Date.UtcDateTime, model.Note); entry.Items.Add(entryItem1); } if (payType.AutoPostToAccounts) { await _accountBalanceService.PostEntryToAccounts(entry); } var affectedRows = await _entryRepo.EditAsync(entry); if (affectedRows > 0) { var viewModel = AutoMapper.Mapper.Map <EntryViewModel>(entry); return(CreatedAtRoute("GetPayEntry", new { id = entry.Number }, viewModel)); } return(BadRequest()); }