public void Can_calculate_ledgerTransaction_totalCreditAmount() { var transaction = new LedgerTransaction { Documentation = "Test", Description = "Test", PostingDate = DateTime.Now }; var transactionLine1 = new LedgerTransactionLine { Id = 1, Amount = 2000, IsDebit = true }; var transactionLine2 = new LedgerTransactionLine { Id = 2, Amount = 2000, IsDebit = false }; var transactionLine3 = new LedgerTransactionLine { Id = 3, Amount = 1000, IsDebit = true }; transaction.LedgerTransactionLines.Add(transactionLine1); transaction.LedgerTransactionLines.Add(transactionLine2); transaction.LedgerTransactionLines.Add(transactionLine3); var totalCredit = transaction.GetTotalCreditAmount(); Assert.Equal(2000, totalCredit); }
private void UpdateDisplayedLines() { DisplayedLines.Clear(); SetBeginningBalance(); _endingBalance = _beginningBalance; using (var context = new ERPContext()) { var lines = context.Ledger_Transaction_Lines .Include("LedgerAccount") .Include("LedgerTransaction") .Where(line => line.LedgerAccount.Name.Equals("Cash") && line.LedgerTransaction.Date.Equals(_date)) .ToList(); var account = new LedgerAccount { ID = -1, Name = "Sales Receipt", Class = "Asset" }; var transaction = new LedgerTransaction { ID = -1, Date = _date, Description = "Sales Receipt", Documentation = "Sales Receipt" }; var salesreceiptLine = new LedgerTransactionLine { LedgerTransaction = transaction, LedgerAccount = account, Seq = "Debit" }; foreach (var line in lines) { var lineVM = new LedgerTransactionLineVM { Model = line }; foreach (var oppositeLine in lineVM.OpposingLines) { if (!oppositeLine.Description.Equals("Sales Transaction Receipt")) { if (line.Seq == "Credit") { oppositeLine.Amount = -oppositeLine.Amount; } DisplayedLines.Add(new LedgerTransactionLineVM { Model = oppositeLine.Model, Balance = _beginningBalance + oppositeLine.Amount }); } else { salesreceiptLine.Amount += oppositeLine.Amount; } _endingBalance += oppositeLine.Amount; oppositeLine.Balance = _endingBalance; } } if (salesreceiptLine.Amount > 0) { DisplayedLines.Add(new LedgerTransactionLineVM { Model = salesreceiptLine, Balance = _endingBalance }); } } OnPropertyChanged("EndingBalance"); }
/// <summary> /// Determines if this line increases or decreases its associated account balance /// </summary> /// <param name="ledgerTransactionLine">Ledger transaction line</param> /// <returns>true - increases, false - decreases</returns> public static bool IsIncrement(this LedgerTransactionLine ledgerTransactionLine) { var isDebit = ledgerTransactionLine.IsDebit; var type = ledgerTransactionLine.LedgerAccount.Type; return(isDebit && (type == LedgerAccountType.Asset || type == LedgerAccountType.Expense || type == LedgerAccountType.ContraLiability || type == LedgerAccountType.ContraEquity || type == LedgerAccountType.ContraRevenue) || !isDebit && (type == LedgerAccountType.Liability || type == LedgerAccountType.Equity || type == LedgerAccountType.Revenue || type == LedgerAccountType.ContraAsset || type == LedgerAccountType.ContraExpense)); }
public void Can_create_ledgerTransaction() { var transactionLine = new LedgerTransactionLine { LedgerTransactionId = 1, LedgerAccountId = 1, Amount = 2000, IsDebit = true }; Assert.Equal(1, transactionLine.LedgerTransactionId); Assert.Equal(1, transactionLine.LedgerAccountId); Assert.Equal(2000, transactionLine.Amount); Assert.True(transactionLine.IsDebit); }
public void Can_add_ledgerTransactionLine() { var transaction = new LedgerTransaction { Documentation = "Test", Description = "Test", PostingDate = DateTime.Now }; var transactionLine1 = new LedgerTransactionLine { Id = 1 }; transaction.LedgerTransactionLines.Add(transactionLine1); Assert.Equal(1, transaction.LedgerTransactionLines.Count); Assert.Equal(1, transaction.LedgerTransactionLines.First().Id); }
public static void AddTransactionLineToDatabase(ERPContext context, LedgerTransaction transaction, string accountName, string seq, decimal amount) { var transactionLine = new LedgerTransactionLine(); var account = context.Ledger_Accounts.First(e => e.Name.Equals(accountName)); var ledgerAccount = context.Ledger_General.First(e => e.ID.Equals(account.ID)); transactionLine.LedgerTransaction = transaction; transactionLine.LedgerAccount = account; transactionLine.Seq = seq; transactionLine.Amount = amount; // Update the amount of the account if (seq == "Debit") ledgerAccount.Debit += amount; else if (seq == "Credit") ledgerAccount.Credit += amount; context.Ledger_Transaction_Lines.Add(transactionLine); }
public void Can_determine_is_increment() { var transactionLine = new LedgerTransactionLine { LedgerTransactionId = 1, LedgerAccount = new LedgerAccount { Type = LedgerAccountType.Asset }, Amount = 2000, IsDebit = true }; Assert.True(transactionLine.IsIncrement()); transactionLine.IsDebit = false; Assert.False(transactionLine.IsIncrement()); transactionLine.LedgerAccount.Type = LedgerAccountType.Equity; }
private static void RemoveLineFromDatabase(LedgerTransactionLine deletedLine) { using (var context = new ERPContext()) { var firstLine = context.Ledger_Transaction_Lines .Include("LedgerAccount") .Single(line => line.LedgerTransactionID.Equals(deletedLine.LedgerTransaction.ID) && line.LedgerAccountID.Equals(deletedLine.LedgerAccount.ID)); var oppostieLine = context.Ledger_Transaction_Lines .Include("LedgerAccount") .Single(line => line.LedgerTransactionID.Equals(deletedLine.LedgerTransaction.ID) && !line.LedgerAccountID.Equals(deletedLine.LedgerAccount.ID)); var transactionFromDatabase = context.Ledger_Transactions .Single(transaction => transaction.ID.Equals(deletedLine.LedgerTransaction.ID)); var ledgerGeneralFromDatabase = context.Ledger_General .Single(ledgerGeneral => ledgerGeneral.ID.Equals(deletedLine.LedgerAccount.ID)); var oppositeLedgerGeneralFromDatabase = context.Ledger_General .Single(ledgerGeneral => ledgerGeneral.ID.Equals(oppostieLine.LedgerAccount.ID)); if (!transactionFromDatabase.Date.Month.Equals(context.Ledger_General.First().Period)) { MessageBox.Show("This line cannot be deleted as the period has been closed.", "Invalid Command", MessageBoxButton.OK); } context.Ledger_Transactions.Remove(transactionFromDatabase); context.Ledger_Transaction_Lines.Remove(firstLine); context.Ledger_Transaction_Lines.Remove(oppostieLine); if (deletedLine.Seq.Equals("Credit")) { ledgerGeneralFromDatabase.Debit -= deletedLine.Amount; oppositeLedgerGeneralFromDatabase.Credit -= deletedLine.Amount; } else { ledgerGeneralFromDatabase.Credit -= deletedLine.Amount; oppositeLedgerGeneralFromDatabase.Debit -= deletedLine.Amount; } context.SaveChanges(); } }
private void UpdateDisplayedLines() { DisplayedLines.Clear(); SetBeginningBalance(); _endingBalance = _beginningBalance; using (var context = new ERPContext()) { var lines = context.Ledger_Transaction_Lines .Include("LedgerAccount") .Include("LedgerTransaction") .Where(line => line.LedgerAccount.Name.Equals("Cash") && line.LedgerTransaction.Date.Equals(_date)) .ToList(); var account = new LedgerAccount { ID = -1, Name = "Sales Receipt", Class = "Asset" }; var transaction = new LedgerTransaction { ID = -1, Date = _date, Description = "Sales Receipt", Documentation = "Sales Receipt" }; var salesreceiptLine = new LedgerTransactionLine { LedgerTransaction = transaction, LedgerAccount = account, Seq = "Debit" }; foreach (var line in lines) { var lineVM = new LedgerTransactionLineVM { Model = line }; foreach (var oppositeLine in lineVM.OpposingLines) { if (!oppositeLine.Description.Equals("Sales Transaction Receipt")) { if (line.Seq == "Credit") oppositeLine.Amount = -oppositeLine.Amount; DisplayedLines.Add(new LedgerTransactionLineVM { Model = oppositeLine.Model, Balance = _beginningBalance + oppositeLine.Amount }); } else salesreceiptLine.Amount += oppositeLine.Amount; _endingBalance += oppositeLine.Amount; oppositeLine.Balance = _endingBalance; } } if (salesreceiptLine.Amount > 0) DisplayedLines.Add(new LedgerTransactionLineVM { Model = salesreceiptLine, Balance = _endingBalance }); } OnPropertyChanged("EndingBalance"); }
public static void AddTransactionLineToDatabase(ERPContext context, LedgerTransaction transaction, string accountName, string seq, decimal amount) { var transactionLine = new LedgerTransactionLine(); var account = context.Ledger_Accounts.First(e => e.Name.Equals(accountName)); var ledgerAccount = context.Ledger_General.First(e => e.ID.Equals(account.ID)); transactionLine.LedgerTransaction = transaction; transactionLine.LedgerAccount = account; transactionLine.Seq = seq; transactionLine.Amount = amount; // Update the amount of the account if (seq == "Debit") { ledgerAccount.Debit += amount; } else if (seq == "Credit") { ledgerAccount.Credit += amount; } context.Ledger_Transaction_Lines.Add(transactionLine); }
public void Can_check_whether_there_are_duplicate_ledgerAccounts() { var transaction = new LedgerTransaction { Documentation = "Test", Description = "Test", PostingDate = DateTime.Now }; var transactionLine1 = new LedgerTransactionLine { Id = 1, LedgerAccountId = 1, Amount = 2000, IsDebit = true }; var transactionLine2 = new LedgerTransactionLine { Id = 2, LedgerAccountId = 1, Amount = 2000, IsDebit = false }; var transactionLine3 = new LedgerTransactionLine { Id = 3, LedgerAccountId = 3, Amount = 1000, IsDebit = true }; transaction.LedgerTransactionLines.Add(transactionLine1); transaction.LedgerTransactionLines.Add(transactionLine2); transaction.LedgerTransactionLines.Add(transactionLine3); var areThereDuplicateLedgerAccounts = transaction.AreThereDuplicateAccounts(); Assert.True(areThereDuplicateLedgerAccounts); }
/// <summary> /// Closes a chart of accounts current ledger period /// </summary> /// <param name="coaId">Chart of accounts identifier</param> public virtual void CloseLedgerPeriod(int coaId) { var coa = GetChartOfAccountsById(coaId); if (coa == null) { throw new ArgumentException("Chart of accounts does not exist."); } var year = coa.CurrentLedgerPeriodStartDate.Year; var month = coa.CurrentLedgerPeriodStartDate.Month; // Close expense and revenue accounts to retained earnings var expenseRevenueAccounts = GetExpenseRevenueAccounts(coa.LedgerAccounts); var retainedEarningsAccount = GetRetainedEarningsAccount(coa.LedgerAccounts); var amountClosedToRetainedEarnings = 0m; foreach (var account in expenseRevenueAccounts) { var closingAmount = CalculatePeriodLedgerAccountBalanceChange(account, year, month); if (closingAmount == 0) { continue; } var closingEntry = new LedgerTransaction { Documentation = "Closing Entry", Description = "Closing Entry", PostingDate = coa.CurrentLedgerPeriodStartDate.AddMonths(1).AddDays(-1), // last day of the month IsEditable = false, IsClosing = true, ChartOfAccountsId = coaId }; var closingEntryLine = new LedgerTransactionLine { LedgerAccountId = account.Id, Amount = closingAmount, IsDebit = account.Type.Equals(LedgerAccountType.Revenue) || account.Type.Equals(LedgerAccountType.ContraExpense) }; // take into account if the amount is negative closingEntryLine.IsDebit = closingAmount < 0 ? !closingEntryLine.IsDebit : closingEntryLine.IsDebit; var closingEntryRetainedEarningsLine = new LedgerTransactionLine { LedgerAccountId = retainedEarningsAccount.Id, Amount = closingAmount, IsDebit = !closingEntryLine.IsDebit }; amountClosedToRetainedEarnings += closingEntryRetainedEarningsLine.IsDebit ? -closingEntryRetainedEarningsLine.Amount : closingEntryRetainedEarningsLine.Amount; closingEntry.LedgerTransactionLines.Add(closingEntryLine); closingEntry.LedgerTransactionLines.Add(closingEntryRetainedEarningsLine); _repository.Create(closingEntry); } var balanceSheetAccounts = GetBalanceSheetLedgerAccounts(coa.LedgerAccounts); foreach (var account in balanceSheetAccounts) { bool isCreate; var accountBalance = GetPeriodLedgerAccountBalance(account.Id, year, month, out isCreate); // Get the period starting balance var accountPeriodStartingBalance = accountBalance.GetMonthBalance(month - 1); // Calculate the period ending balance decimal accountPeriodEndingBalance; if (!account.Name.Equals("Retained Earnings")) { accountPeriodEndingBalance = accountPeriodStartingBalance + CalculatePeriodLedgerAccountBalanceChange(account, year, month); } else { accountPeriodEndingBalance = accountPeriodStartingBalance + amountClosedToRetainedEarnings; } // Update the ending balance accountBalance.SetMonthBalance(month, accountPeriodEndingBalance); if (isCreate) { _repository.Create(accountBalance); } else { _repository.Update(accountBalance); } } // Advance and update the current ledger period coa.AdvanceLedgerPeriod(); _repository.Update(coa); // Persist changes _repository.Save(); }