Example #1
0
        public ActionResult TrialBalance(string date1, string date2)
        {
            try
            {
                var transactions = db.Transactions.ToList();

                if (!(String.IsNullOrEmpty(date1) || String.IsNullOrEmpty(date2)))
                {
                    transactions = transLogic.GetTrialBalanceTransactions(Convert.ToDateTime(date1), Convert.ToDateTime(date2));
                }
                transactions = transactions.OrderBy(t => t.MainCategory).ToList();

                var     viewModel = new List <TrialBalanceViewModel>();
                decimal totalDebit = 0, totalCredit = 0;
                foreach (var transaction in transactions)
                {
                    var model = viewModel.FirstOrDefault(i => i.AccountName.ToLower().Equals((transaction.AccountName.ToLower())));
                    if (model == null)   //add new
                    {
                        model              = new TrialBalanceViewModel();
                        model.AccountName  = transaction.AccountName;
                        model.SubCategory  = transaction.SubCategory;
                        model.MainCategory = transaction.MainCategory.ToString();
                        model.TotalCredit  = transaction.TransactionType == TransactionType.Credit ? transaction.Amount : 0;
                        model.TotalDebit   = transaction.TransactionType == TransactionType.Debit ? transaction.Amount : 0;
                        viewModel.Add(model);

                        totalCredit += model.TotalCredit;
                        totalDebit  += model.TotalDebit;
                    }
                    else    //continue with the item
                    {
                        decimal amt = transaction.Amount;
                        if (transaction.TransactionType == TransactionType.Credit)
                        {
                            model.TotalCredit += amt;
                            totalCredit       += amt;
                        }
                        else if (transaction.TransactionType == TransactionType.Debit)
                        {
                            model.TotalDebit += amt;
                            totalDebit       += amt;
                        }
                    }
                }//end foreach

                ViewBag.TotalCredit = totalCredit;
                ViewBag.TotalDebit  = totalDebit;
                return(View(viewModel));
            }
            catch (Exception)
            {
                //ErrorLogger.Log("Message= " + ex.Message + "\nInner Exception= " + ex.InnerException + "\n");
                return(PartialView("Error"));
            }
        }