public BalanceJournalViewModel RetrieveBalanceJournalHeader(int month, int year)
        {
            BalanceJournal result = _balanceJournalRepository.GetMany(bj => bj.Month == month && bj.Year == year &&
                                                                      bj.Status == (int)DbConstant.DefaultDataStatus.Active).FirstOrDefault();
            BalanceJournalViewModel mappedResult = new BalanceJournalViewModel();

            return(Map(result, mappedResult));
        }
        public BalanceJournalViewModel RetrieveFirstBalance()
        {
            BalanceJournal result = _balanceJournalRepository.GetMany(b => b.IsFirst &&
                                                                      b.Status == (int)DbConstant.DefaultDataStatus.Active).FirstOrDefault();

            BalanceJournalViewModel mappedResult = new BalanceJournalViewModel();

            return(Map(result, mappedResult));
        }
        public List <BalanceHelperItemViewModel> RetrieveBalanceHelper(int year, int month, int journalId)
        {
            List <BalanceHelperItemViewModel> result = new List <BalanceHelperItemViewModel>();

            JournalMaster          currentJournal       = _journalMasterRepository.GetById(journalId);
            JournalMasterViewModel mappedCurrentJournal = new JournalMasterViewModel();

            Map(currentJournal, mappedCurrentJournal);

            DateTime firstDay  = new DateTime(year, month, 1);
            DateTime lastDay   = new DateTime(year, month, DateTime.DaysInMonth(year, month));
            DateTime prevMonth = firstDay.AddDays(-1);

            List <TransactionDetail> listTransaction = _transactionDetailRepository.GetMany(t =>
                                                                                            t.Parent.TransactionDate >= firstDay && t.Parent.TransactionDate <= lastDay &&
                                                                                            t.Parent.Status == (int)DbConstant.DefaultDataStatus.Active).ToList();
            List <TransactionDetailViewModel> mappedListTransaction = new List <TransactionDetailViewModel>();

            Map(listTransaction, mappedListTransaction);

            mappedListTransaction = mappedListTransaction.Where(mt => IsCurrentJournalValid(mt.Journal, mappedCurrentJournal.Code)).ToList();

            BalanceJournal lastJournal = _balanceJournalRepository.GetMany(bj =>
                                                                           bj.Month == prevMonth.Month && bj.Year == prevMonth.Year &&
                                                                           bj.Status == (int)DbConstant.DefaultDataStatus.Active).FirstOrDefault();

            if (lastJournal != null)
            {
                List <BalanceJournalDetail> lastJournalDetail = _balanceJournalDetailRepository.GetMany(bjd =>
                                                                                                        bjd.ParentId == lastJournal.Id).ToList();
                List <BalanceJournalDetailViewModel> mappedLastJournalDetail = new List <BalanceJournalDetailViewModel>();
                Map(lastJournalDetail, mappedLastJournalDetail);
                mappedLastJournalDetail = mappedLastJournalDetail.Where(jDet => IsCurrentJournalValid(jDet.Journal, mappedCurrentJournal.Code)).ToList();
                foreach (var item in mappedLastJournalDetail)
                {
                    BalanceHelperItemViewModel firstBalanceItem = new BalanceHelperItemViewModel();
                    firstBalanceItem.TransactionDate = prevMonth;
                    firstBalanceItem.JournalCode     = item.Journal.Code;
                    firstBalanceItem.JournalName     = item.Journal.Name;
                    firstBalanceItem.Balance         = (item.LastDebit ?? 0) - (item.LastCredit ?? 0);
                    result.Add(firstBalanceItem);
                }
            }

            foreach (var transItem in mappedListTransaction)
            {
                decimal prevBalance = 0;
                if (result.Count > 0)
                {
                    prevBalance = result[result.Count - 1].Balance;
                }

                BalanceHelperItemViewModel transBalanceItem = new BalanceHelperItemViewModel();
                transBalanceItem.TransactionDate = transItem.Parent.TransactionDate;
                transBalanceItem.JournalCode     = transItem.Journal.Code;
                transBalanceItem.JournalName     = transItem.Journal.Name;
                transBalanceItem.MutationDebit   = (transItem.Debit ?? 0);
                transBalanceItem.MutationCredit  = (transItem.Credit ?? 0);
                transBalanceItem.Balance         = (transBalanceItem.MutationDebit - transBalanceItem.MutationCredit) + prevBalance;
                result.Add(transBalanceItem);

                //if (IsCurrentJournalValid(transItem.Journal, mappedCurrentJournal.Code))
                //{
                //    BalanceHelperItemViewModel transBalanceItem = new BalanceHelperItemViewModel();
                //    transBalanceItem.TransactionDate = transItem.Parent.TransactionDate;
                //    transBalanceItem.JournalCode = transItem.Journal.Code;
                //    transBalanceItem.JournalName = transItem.Journal.Name;
                //    transBalanceItem.MutationDebit = (transItem.Debit ?? 0);
                //    transBalanceItem.MutationCredit = (transItem.Credit ?? 0);
                //    transBalanceItem.Balance = (transBalanceItem.MutationDebit - transBalanceItem.MutationCredit) + prevBalance;
                //    result.Add(transBalanceItem);
                //}
            }

            return(result);
        }