Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GLReportManager
- {
- public static VoucherManager VoucherManager=new VoucherManager();
- static TransactionManager TransactionManager = new TransactionManager();
- #region Trial Balance
- public static List<TrialBalanceRow> CalculateTrialBalance(int companyId,DateTime tbDate)
- {
- List<TrialBalanceRow> rows = new List<TrialBalanceRow>();
- var charts = ChartManager.GetAll(companyId).OrderBy(c => c.AccountTypeId);
- Company company = CompanyManager.Find(companyId);
- company.CalculateDates(tbDate);
- double sumPL = 0;
- DateTime currentYearStart= (DateTime) company.CurrentYearStart;
- DateTime openingBlanceDate = company.OpeningBlanceDate;
- foreach (Account c in charts)
- {
- var row = new TrialBalanceRow { AccountName = c.AccountName, AccountType = c.AccountType.Type,ConsolidationId = c.ConsolidationId,AccountTypeId = c.AccountTypeId};
- if (c.AccountType.Type == "Income" || c.AccountType.Type == "Expense" ||
- c.AccountType.Type == "Dividend" || c.AccountType.Type == "Acc P&L")
- {
- TrialBalanceCalculateRow(row, currentYearStart, tbDate, c.AccountId);
- sumPL += TrialBalanceGetSumPl(openingBlanceDate,(DateTime)company.LastYearClosing, c.AccountId);
- if (c.AccountType.Type == "Acc P&L")
- {
- if (sumPL > 0)
- { row.Debit += sumPL; }
- else
- { row.Credit += Math.Abs(sumPL); }
- }
- }
- else
- {
- TrialBalanceCalculateRow(row, openingBlanceDate, tbDate, c.AccountId);
- }
- if(row.Debit+row.Credit>0) rows.Add(row);
- }
- ConsolidateRows(companyId, rows);
- if (rows.Count > 0)
- {
- rows.Add(new TrialBalanceRow { AccountName = "Total", Debit = rows.Sum(r => r.Debit), Credit = rows.Sum(r => r.Credit),AccountTypeId = 100});
- }
- rows=rows.OrderBy(r => r.AccountTypeId).ToList();
- return rows;
- }
- private static void TrialBalanceCalculateRow(TrialBalanceRow row, DateTime fromDate, DateTime toDate, int accountId)
- {
- row.Debit = TransactionManager.GetAll(VoucherManager.GetByDateRange(fromDate, toDate)).Where(t => t.AccountId == accountId).Sum(t => t.Debit);
- row.Credit = TransactionManager.GetAll(VoucherManager.GetByDateRange(fromDate, toDate)).Where(t => t.AccountId == accountId).Sum(t => t.Credit);
- AdjustDrCr(row);
- }
- private static void ConsolidateRows(int companyId, List<TrialBalanceRow> rows)
- {
- var conNames = rows.Where(r => r.ConsolidationId > 0).Select(r => r.AccountType).Distinct().ToList();
- foreach (string conName in conNames)
- {
- if(!string.IsNullOrEmpty(conName))
- {
- var acType = AccountTypeManager.GetAccountTypebyName(conName);
- Consolidation con=ConsolidationManager.GetAll(companyId).FirstOrDefault(c => c.AccountTypeId == acType.Id);
- double debit = rows.Where(r=>r.AccountType==conName&&r.ConsolidationId>0).Sum(r => r.Debit);
- double credit = rows.Where(r => r.AccountType == conName && r.ConsolidationId > 0).Sum(r => r.Credit);
- var row = new TrialBalanceRow{AccountName = con.Name,AccountType = acType.Type,Debit = debit,Credit = credit,AccountTypeId = acType.Id,ConsolidationId = 0};
- AdjustDrCr(row);
- rows.Add(row);
- }
- }
- rows.RemoveAll(r => r.ConsolidationId > 0);
- }
- private static double TrialBalanceGetSumPl(DateTime fromDate, DateTime toDate, int accountId)
- {
- double pldSum = TransactionManager.GetAll(VoucherManager.GetByDateRange(fromDate, toDate)).Where(t => t.AccountId == accountId).Sum(t => t.Debit);
- double plcSum = TransactionManager.GetAll(VoucherManager.GetByDateRange(fromDate, toDate)).Where(t => t.AccountId == accountId).Sum(t => t.Credit);
- return (pldSum - plcSum);
- }
- #endregion
- #region Reciept Payment
- public static List<TrialBalanceRow> CalculateRP(int companyId,DateTime fromDate,DateTime toDate,bool consolidation)
- {
- List<TrialBalanceRow> rows = new List<TrialBalanceRow>();
- List<Account> charts = new List<Account>();
- #region Opening Balance
- rows.Add(new TrialBalanceRow { AccountName = "Opening Balance" });
- charts = ChartManager.GetAll(companyId).Where(c => c.AccountType.Type == "Cash" || c.AccountType.Type == "Bank").ToList();
- foreach (var chartOfAccount in charts)
- {
- rows.Add(new TrialBalanceRow
- {
- AccountName = chartOfAccount.AccountName,
- Debit = TransactionManager.GetAll(companyId,chartOfAccount.AccountId).Where(t => t.Voucher.Date < fromDate).Sum(t => t.Debit),
- Credit = TransactionManager.GetAll(companyId, chartOfAccount.AccountId).Where(t => t.Voucher.Date < fromDate).Sum(t => t.Credit),
- ConsolidationId = chartOfAccount.ConsolidationId,
- AccountTypeId = 1,
- AccountType = chartOfAccount.AccountType.Type,
- });
- }
- #endregion
- #region Transaction During period
- charts = ChartManager.GetAll(companyId).Where(c => c.AccountType.Type != "Cash" && c.AccountType.Type != "Bank").ToList();
- rows.Add(new TrialBalanceRow());
- foreach (var chartOfAccount in charts)
- {
- rows.Add(new TrialBalanceRow
- {
- AccountName = chartOfAccount.AccountName,
- Debit = TransactionManager.GetAll(companyId, chartOfAccount.AccountId).Where(t => t.Voucher.Date >= fromDate && t.Voucher.Date <= toDate && t.Credit > 0 && t.Voucher.Type != "Journal").Sum(t => t.Credit),
- Credit = TransactionManager.GetAll(companyId, chartOfAccount.AccountId).Where(t =>t.Voucher.Date >= fromDate && t.Voucher.Date <= toDate && t.Debit > 0 && t.Voucher.Type != "Journal").Sum(t => t.Debit),
- ConsolidationId = chartOfAccount.ConsolidationId,
- AccountTypeId = 2,
- AccountType = chartOfAccount.AccountType.Type,
- });
- }
- Adjustments(rows);
- TrialBalanceRow closingRow = new TrialBalanceRow
- {
- AccountName = "Closing Balance",
- AccountTypeId = 2,
- };
- var closingBalance = rows.Where(r => r.AccountTypeId == 1 || r.AccountTypeId == 2).Sum(r => r.Debit) -
- rows.Where(r => r.AccountTypeId == 1 || r.AccountTypeId == 2).Sum(r => r.Credit);
- if (closingBalance < 0)
- closingRow.Credit = closingBalance;
- else
- closingRow.Debit = Math.Abs(closingBalance);
- rows.Add(closingRow);
- Adjustments(rows);
- rows.Add(new TrialBalanceRow { AccountName = "Total", Debit = rows.Sum(r => r.Debit), Credit = rows.Sum(r => r.Credit) });
- rows.Add(new TrialBalanceRow());
- #endregion
- #region Closing Balance Explanation
- charts = ChartManager.GetAll(companyId).Where(c => c.AccountType.Type == "Cash" || c.AccountType.Type == "Bank").ToList();
- foreach (var chartOfAccount in charts)
- {
- rows.Add(new TrialBalanceRow
- {
- AccountName = chartOfAccount.AccountName,
- Debit = TransactionManager.GetAll(companyId, chartOfAccount.AccountId).Where(t => t.Voucher.Date <= toDate).Sum(t => t.Debit),
- Credit = TransactionManager.GetAll(companyId, chartOfAccount.AccountId).Where(t => t.Voucher.Date <= toDate).Sum(t => t.Credit),
- AccountTypeId = 3,
- ConsolidationId = chartOfAccount.ConsolidationId,
- AccountType = chartOfAccount.AccountType.Type,
- });
- }
- Adjustments(rows);
- rows.RemoveAll(r => r.AccountTypeId > 0 && r.Debit == 0 && r.Credit == 0);
- #endregion
- if(consolidation) ConsolidateRPRows(companyId, rows);
- return rows;
- }
- private static void Adjustments(List<TrialBalanceRow> rows)
- {
- foreach (var trialBalanceRow in rows)
- {
- //break;
- if (trialBalanceRow.AccountTypeId > 0)
- {
- if (trialBalanceRow.Debit > trialBalanceRow.Credit)
- {
- trialBalanceRow.Debit -= trialBalanceRow.Credit;
- trialBalanceRow.Credit = 0;
- }
- else
- {
- trialBalanceRow.Credit -= trialBalanceRow.Debit;
- trialBalanceRow.Debit = 0;
- }
- }
- }
- }
- private static void ConsolidateRPRows(int companyId, List<TrialBalanceRow> rows)
- {
- for (int i = 1; i < 4; i++) {
- var conNames = rows.Where(r => r.ConsolidationId > 0 && r.AccountTypeId==i).Select(r => r.AccountType).Distinct().ToList();
- foreach (string conName in conNames)
- {
- if (!string.IsNullOrEmpty(conName))
- {
- var acType = AccountTypeManager.GetAccountTypebyName(conName);
- Consolidation con = ConsolidationManager.GetAll(companyId).FirstOrDefault(c => c.AccountTypeId == acType.Id);
- double debit = rows.Where(r => r.AccountType == conName && r.ConsolidationId > 0 && r.AccountTypeId == i).Sum(r => r.Debit);
- double credit = rows.Where(r => r.AccountType == conName && r.ConsolidationId > 0 && r.AccountTypeId == i).Sum(r => r.Credit);
- var row = rows.Where(r => r.AccountType == conName && r.ConsolidationId > 0 && r.AccountTypeId == i).FirstOrDefault();
- row.Debit = debit;
- row.Credit = credit;
- row.AccountName = con.Name;
- row.AccountType = acType.Type;
- row.AccountTypeId = i;
- row.ConsolidationId = 0;
- AdjustDrCr(row);
- }
- }
- }
- rows.RemoveAll(r => r.ConsolidationId > 0);
- }
- #endregion
- #region Balance Sheet
- public static List<BalanceSheetRow> CalculateBalanceSheet(int companyId, DateTime tbDate,bool consolidate)
- {
- DateTime balanceDate = tbDate, fromDate = new DateTime();
- Company company = CompanyManager.Find(companyId);
- company.CalculateDates(balanceDate);
- List<BalanceSheetRow> rows = new List<BalanceSheetRow>();
- List<Account> charts = ChartManager.GetAll(companyId).OrderBy(c => c.AccountTypeId).ToList();
- double sumAcPL = 0, sumProfitLoss = 0;
- foreach (Account c in charts)
- {
- BalanceSheetRow row = new BalanceSheetRow
- {
- AccountName = c.AccountName,
- AccountType = c.AccountType.Type,
- ConsolidationId = c.ConsolidationId,
- };
- if (c.AccountType.Type == "Income" || c.AccountType.Type == "Expense" ||
- c.AccountType.Type == "Dividend" || c.AccountType.Type == "Acc P&L")
- {
- fromDate = (DateTime)company.CurrentYearStart;
- var ploss = TransactionManager.GetTransactionData(companyId)
- .Where(t => t.AccountId == c.AccountId &&
- t.Date >= company.OpeningBlanceDate && t.Date <= company.LastYearClosing);
- sumAcPL += ploss.Sum(t => t.Debit - t.Credit);
- }
- else
- {
- fromDate = company.OpeningBlanceDate;
- }
- var transactionList = TransactionManager.GetTransactionData(companyId).
- Where(t => t.AccountId == c.AccountId && t.Date >= fromDate && t.Date <= balanceDate);
- BalanceSheetCalculateRow(row, transactionList, rows, ref sumProfitLoss);
- if (c.AccountType.Type == "Acc P&L")
- {
- if (sumAcPL > 0)
- {
- row.Debit += sumAcPL;
- }
- else
- {
- row.Credit -= sumAcPL;
- }
- }
- }
- BalanceSheetRow profitloss = new BalanceSheetRow
- {
- AccountName = " for the period",
- };
- if (sumProfitLoss >= 0)
- {
- profitloss.Credit = sumProfitLoss;
- profitloss.Debit = 0;
- profitloss.AccountName = "Profit" + profitloss.AccountName;
- }
- else
- {
- profitloss.Debit = Math.Abs(sumProfitLoss);
- profitloss.Credit = 0;
- profitloss.AccountName = "Loss"+profitloss.AccountName;
- }
- rows.Add(profitloss);
- foreach (BalanceSheetRow row in rows)
- {
- if (row.AccountType != "Fixed Asset" && row.AccountType != "Current Asset" && row.AccountType != "Cash" && row.AccountType != "Bank")
- {
- row.Balance = row.Credit - row.Debit;
- }
- else
- {
- row.Balance = row.Debit - row.Credit;
- }
- }
- rows.RemoveAll(r => r.Balance == 0);
- if(consolidate) ConsolidateBSRows(companyId,rows);
- return rows;
- }
- private static void ConsolidateBSRows(int companyId, List<BalanceSheetRow> rows)
- {
- var conNames = rows.Where(r => r.ConsolidationId > 0).Select(r => r.AccountType).Distinct().ToList();
- foreach (string conName in conNames)
- {
- if (!string.IsNullOrEmpty(conName))
- {
- var acType = AccountTypeManager.GetAccountTypebyName(conName);
- Consolidation con = ConsolidationManager.GetAll(companyId).FirstOrDefault(c => c.AccountTypeId == acType.Id);
- double debit = rows.Where(r => r.AccountType == conName && r.ConsolidationId > 0 ).Sum(r => r.Debit);
- double credit = rows.Where(r => r.AccountType == conName && r.ConsolidationId > 0).Sum(r => r.Credit);
- var row = rows.Where(r => r.AccountType == conName && r.ConsolidationId > 0).FirstOrDefault();
- row.Debit = debit;
- row.Credit = credit;
- row.AccountName = con.Name;
- row.AccountType = acType.Type;
- //row.AccountTypeId = i;
- row.ConsolidationId = 0;
- AdjustDrCr(row);
- }
- }
- rows.RemoveAll(r => r.ConsolidationId > 0);
- }
- private static void BalanceSheetCalculateRow( BalanceSheetRow row, IEnumerable<TransactionData> tset, List<BalanceSheetRow> rows, ref double sumProfitLoss)
- {
- row.Debit = tset.Sum(t => t.Debit);
- row.Credit = tset.Sum(t => t.Credit);
- if (row.Debit > row.Credit)
- {
- row.Debit = row.Debit - row.Credit;
- row.Credit = 0;
- }
- else
- {
- row.Credit = Math.Abs(row.Debit - row.Credit);
- row.Debit = 0;
- }
- if (row.AccountType == "Income" || row.AccountType == "Expense")
- {
- sumProfitLoss += row.Credit - row.Debit;
- }
- else
- {
- rows.Add(row);
- }
- }
- #endregion
- #region Income Statement
- public static List<TrialBalanceRow> CalculateIncomeRows(int companyId, DateTime fromDate, DateTime toDate)
- {
- double drSum, crSum;
- var rows = new List<TrialBalanceRow>();
- var charts = ChartManager.GetAll(companyId).Where(c => c.AccountType.Type == "Income" || c.AccountType.Type == "Expense");
- foreach (var chartOfAccount in charts)
- {
- drSum = TransactionManager.GetAll(companyId, chartOfAccount.AccountId).Where(t => t.Voucher.Date >= fromDate && t.Voucher.Date <= toDate).Sum(t => t.Debit);
- crSum = TransactionManager.GetAll(companyId, chartOfAccount.AccountId).Where(t => t.Voucher.Date >= fromDate && t.Voucher.Date <= toDate).Sum(t => t.Credit);
- var row = new TrialBalanceRow
- {
- AccountName = chartOfAccount.AccountName,
- AccountType = chartOfAccount.AccountType.Type,
- Debit = drSum,
- Credit = crSum,
- ConsolidationId = chartOfAccount.ConsolidationId,
- AccountTypeId = chartOfAccount.AccountTypeId
- };
- AdjustDrCr(row);
- if(row.Debit+row.Credit>0) rows.Add(row);
- }
- drSum = rows.Sum(r => r.Debit);
- crSum = rows.Sum(r => r.Credit);
- string status = drSum - crSum > 0 ? "Profit for the period" : "Loss for the period";
- var bottomRow = new TrialBalanceRow
- {
- AccountName = status,
- Debit = drSum,
- Credit = crSum,
- AccountTypeId = 100
- };
- AdjustDrCr(bottomRow);
- rows.Add(bottomRow);
- return rows;
- }
- #endregion
- #region Ledger Report
- public static List<LedgerRow> CalculateLedger(string type, int companyId, DateTime fromDate, DateTime toDate, int accountId)
- {
- Company company = CompanyManager.Find(companyId);
- double openingCreditSum, openingDebitSum;
- List<LedgerRow> rows=new List<LedgerRow>();
- company.CalculateDates(fromDate);
- if (type == "Income" || type == "Expense" || type == "Dividend")
- {
- openingDebitSum = TransactionManager.GetAll(VoucherManager.GetByDateRange((DateTime)company.CurrentYearStart, fromDate.AddDays(-1))).Where(t => t.AccountId == accountId).Sum(t => t.Debit);
- openingCreditSum = TransactionManager.GetAll(VoucherManager.GetByDateRange((DateTime)company.CurrentYearStart, fromDate.AddDays(-1))).Where(t => t.AccountId == accountId).Sum(t => t.Credit);
- }
- else
- {
- openingDebitSum = TransactionManager.GetAll(VoucherManager.GetByDateRange(company.OpeningBlanceDate, fromDate.AddDays(-1))).Where(t => t.AccountId == accountId).Sum(t => t.Debit);
- openingCreditSum = TransactionManager.GetAll(VoucherManager.GetByDateRange(company.OpeningBlanceDate, fromDate.AddDays(-1))).Where(t => t.AccountId == accountId).Sum(t => t.Credit);
- }
- rows.Add(new LedgerRow { VoucherNumber = "Opening Balance", Details = "Opening Balance", Credit = openingCreditSum, Debit = openingDebitSum, Balance = Math.Abs(openingDebitSum - openingCreditSum), Status = GetStatus(openingDebitSum - openingCreditSum) });
- var transactiondata = from transaction in TransactionManager.GetAll(companyId, accountId)
- where transaction.Voucher.Deleted == false &&
- transaction.Voucher.Date >= fromDate && transaction.Voucher.Date <= toDate
- select new LedgerRow
- {
- VoucherNumber = transaction.Voucher.VoucherNumber,
- Details = transaction.Voucher.Details,
- Debit = transaction.Debit,
- Credit = transaction.Credit,
- };
- rows.AddRange(transactiondata);
- double rowBalance = 0;
- foreach (LedgerRow ledgerRow in rows)
- {
- rowBalance = rowBalance + ledgerRow.Debit - ledgerRow.Credit;
- ledgerRow.Balance = Math.Abs(rowBalance);
- ledgerRow.Status = GetStatus(rowBalance);
- List<int> accountIds = new List<int>();
- if (ledgerRow.Debit > 0)
- {
- accountIds = TransactionManager.GetAll(companyId)
- .Where(t => t.VoucherNumber == ledgerRow.VoucherNumber
- && t.AccountId != accountId && t.Credit > 0).Select(r => r.AccountId).ToList();
- }
- else
- {
- accountIds = TransactionManager.GetAll(companyId)
- .Where(t => t.VoucherNumber == ledgerRow.VoucherNumber
- && t.AccountId != accountId && t.Debit > 0).Select(r => r.AccountId).ToList();
- }
- for (int i = 0; i < accountIds.Count; i++)
- {
- ledgerRow.Accounts += ChartManager.Get(accountIds[i], companyId).AccountName;
- if (i < accountIds.Count - 1) ledgerRow.Accounts += ",";
- }
- }
- rows.Add(new LedgerRow { VoucherNumber = "Total", Debit = rows.Sum(r => r.Debit), Credit = rows.Sum(r => r.Credit), Balance = Math.Abs(rowBalance), Status = GetStatus(rowBalance) });
- return rows;
- }
- static string GetStatus(double balance)
- {
- return balance < 0 ? "Cr" : "Dr";
- }
- #endregion
- #region CommonFunctions
- private static void AdjustDrCr(TrialBalanceRow row)
- {
- if (row.Debit > row.Credit)
- {
- row.Debit = row.Debit - row.Credit;
- row.Credit = 0;
- }
- else
- {
- row.Credit = row.Credit - row.Debit;
- row.Debit = 0;
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement