private async Task <BatchReceiptEditorForm> GetForBatch(int batchId) { var expenseTypes = GetReceiptTypes(); var batch = await batches.GetById(batchId) .Where(b => !b.Committed) .Include(b => b.Fund) .Include(b => b.TransactionSubType) .AsNoTracking() .FirstOrDefaultAsync(); if (batch == null) { return(null); } var transactions = await receipts.All() .Include(e => e.ReceiptSource) .Include(e => e.Account) .Where(e => e.TransactionBatchId == batch.Id) .AsNoTracking() .ToArrayAsync(); if (batch.Fund is SubsidiaryFund) { return(SubsidiaryBatchReceiptEditorForm.FromBatch(batch, transactions, expenseTypes)); } if (batch.Fund is ClientFund) { return(ClientBatchReceiptEditorForm.FromBatch(batch, transactions, expenseTypes)); } return(null); }
private async Task <ICommandResult> Add(ClientBatchReceiptEditorForm editor) { var batch = await CreateBatch(editor); await CreateTransactions(editor, batch); return(new SuccessResult(batch.Id)); }
private IEnumerable <Account> GetAccounts(ClientBatchReceiptEditorForm editor) { var ids = editor.Transactions .Select(t => t.AccountId) .ToArray(); return(accounts.All() .Where(a => ids.Contains(a.Id)) .ToArray()); }
private Task CreateTransactions(ClientBatchReceiptEditorForm editor, ReceiptBatch batch) { var batchAccounts = GetAccounts(editor); editor.Transactions .Select(t => t.BuildReceipt(batch, context)) .ForEach(t => { batchAccounts.First(x => x.Id == t.AccountId) .AddReceipt(t); }); return(context.SaveChangesAsync()); }