private async Task <BatchExpenseEditorForm> GetForBatch(int batchId) { var expenseTypes = GetExpenseTypes(); var batch = await batches.GetById(batchId) .Where(b => !b.Committed) .Include(b => b.Fund) .Include(b => b.Payee) .Include(b => b.TransactionSubType) .AsNoTracking() .FirstOrDefaultAsync(); if (batch == null) { return(null); } var transactions = await expenses.All() .Include(e => e.ExpenseCategory) .Include(e => e.Account) .Where(e => e.TransactionBatchId == batch.Id) .AsNoTracking() .ToArrayAsync(); if (batch.Fund is SubsidiaryFund) { return(SubsidiaryBatchExpenseEditorForm.FromBatch(batch, transactions, expenseTypes)); } if (batch.Fund is ClientFund) { return(ClientBatchExpenseEditorForm.FromBatch(batch, transactions, expenseTypes)); } return(null); }
private async Task <ICommandResult> Add(SubsidiaryBatchExpenseEditorForm editor) { var batch = await CreateBatch(editor); await CreateTransactions(editor, batch); return(new SuccessResult(batch.Id)); }
private IEnumerable <Account> GetAccounts(SubsidiaryBatchExpenseEditorForm editor) { var ids = editor.Transactions .Select(t => t.AccountId) .ToArray(); return(accounts.All() .Where(a => ids.Contains(a.Id)) .ToArray()); }
private Task CreateTransactions(SubsidiaryBatchExpenseEditorForm editor, ExpenseBatch batch) { var batchAccounts = GetAccounts(editor); editor.Transactions .Select(t => t.BuildExpense(batch, context)) .ForEach(t => { batchAccounts.First(x => x.Id == t.AccountId) .AddExpense(t); }); return(context.SaveChangesAsync()); }