//Private //TODO: Refactor private async Task <bool> ValidateGeneralLedgerEntry(GeneralLedgerHeader glEntry) { if (!glEntry.DrCrEqualityValidated()) { throw new InvalidOperationException("Debit/Credit are not equal."); } if (!glEntry.NoLineAmountIsEqualToZero()) { throw new InvalidOperationException("One or more line(s) amount is zero."); } var duplicateAccounts = glEntry.GeneralLedgerLines .GroupBy(gl => gl.AccountId) .Where(gl => gl.Count() > 1); if (duplicateAccounts.Any()) { throw new InvalidOperationException("Duplicate account id in a collection."); } foreach (var line in glEntry.GeneralLedgerLines) { var account = await _db.Accounts.FirstOrDefaultAsync(a => a.Id == line.AccountId); if (!account.CanPost()) { throw new InvalidOperationException("One of the account is not valid for posting"); } } if (!glEntry.ValidateAccountingEquation()) { throw new InvalidOperationException("One of the account not equal."); } return(true); }