public List <BalanceSheetDetailViewModel> RetrieveProfitLoss(int headerId)
        {
            List <JournalMaster> allJournalMaster = _journalMasterRepository.GetAll().ToList();

            List <BalanceJournalDetailViewModel> mappedResult    = base.RetrieveBalanceJournalDetailsByHeaderId(headerId);
            List <BalanceSheetDetailViewModel>   formattedResult = new List <BalanceSheetDetailViewModel>();

            Reference catServiceJournal = _referenceRepository.GetMany(r => r.Code == DbConstant.REF_CAT_JOURNAL_SERVICE).FirstOrDefault();
            Reference catCostJournal    = _referenceRepository.GetMany(r => r.Code == DbConstant.REF_CAT_JOURNAL_COST).FirstOrDefault();
            Reference catIncomeJournal  = _referenceRepository.GetMany(r => r.Code == DbConstant.REF_CAT_JOURNAL_INCOME).FirstOrDefault();

            List <Reference> listServiceJournal = _referenceRepository.GetMany(r => r.ParentId == catServiceJournal.Id).ToList();
            List <Reference> listCostJournal    = _referenceRepository.GetMany(r => r.ParentId == catCostJournal.Id).ToList();
            List <Reference> listIncomeJournal  = _referenceRepository.GetMany(r => r.ParentId == catIncomeJournal.Id).ToList();

            BalanceSheetViewModel headerService = new BalanceSheetViewModel();

            headerService.GroupName = "1. Penjualan";

            foreach (var item in listServiceJournal)
            {
                BalanceSheetDetailViewModel detail = new BalanceSheetDetailViewModel();
                detail.Header = headerService;
                JournalMaster currentJournal = allJournalMaster.Where(j => j.Code == item.Value).FirstOrDefault();
                detail.Name = currentJournal.Name;

                List <int> cachedItems = new List <int>();
                foreach (var itemBalance in mappedResult.Where(m => !m.IsChecked))
                {
                    if (base.IsCurrentJournalValid(itemBalance.Journal, item.Value))
                    {
                        decimal currentAmount = (itemBalance.LastCredit ?? 0) - (itemBalance.LastDebit ?? 0);
                        detail.Amount += currentAmount;

                        cachedItems.Add(itemBalance.Id);
                    }
                }

                foreach (var iCache in cachedItems)
                {
                    BalanceJournalDetailViewModel current = mappedResult.Where(m => m.Id == iCache).FirstOrDefault();
                    int iCacheIndex = mappedResult.IndexOf(current);
                    current.IsChecked         = true;
                    mappedResult[iCacheIndex] = current;
                }

                formattedResult.Add(detail);
            }

            BalanceSheetViewModel headerCost = new BalanceSheetViewModel();

            headerCost.GroupName = "2. Biaya Umum & Administrasi";

            foreach (var item in listCostJournal)
            {
                BalanceSheetDetailViewModel detail = new BalanceSheetDetailViewModel();
                detail.Header = headerCost;
                JournalMaster currentJournal = allJournalMaster.Where(j => j.Code == item.Value).FirstOrDefault();
                detail.Name = currentJournal.Name;

                List <int> cachedItems = new List <int>();
                foreach (var itemBalance in mappedResult.Where(m => !m.IsChecked))
                {
                    if (base.IsCurrentJournalValid(itemBalance.Journal, item.Value))
                    {
                        decimal currentAmount = (itemBalance.LastCredit ?? 0) - (itemBalance.LastDebit ?? 0);
                        detail.Amount += currentAmount;

                        cachedItems.Add(itemBalance.Id);
                    }
                }

                foreach (var iCache in cachedItems)
                {
                    BalanceJournalDetailViewModel current = mappedResult.Where(m => m.Id == iCache).FirstOrDefault();
                    int iCacheIndex = mappedResult.IndexOf(current);
                    current.IsChecked         = true;
                    mappedResult[iCacheIndex] = current;
                }

                formattedResult.Add(detail);
            }

            BalanceSheetViewModel headerIncome = new BalanceSheetViewModel();

            headerIncome.GroupName = "3. Pendapatan";

            foreach (var item in listIncomeJournal)
            {
                BalanceSheetDetailViewModel detail = new BalanceSheetDetailViewModel();
                detail.Header = headerIncome;
                JournalMaster currentJournal = allJournalMaster.Where(j => j.Code == item.Value).FirstOrDefault();
                detail.Name = currentJournal.Name;

                List <int> cachedItems = new List <int>();
                foreach (var itemBalance in mappedResult.Where(m => !m.IsChecked))
                {
                    if (base.IsCurrentJournalValid(itemBalance.Journal, item.Value))
                    {
                        decimal currentAmount = (itemBalance.LastCredit ?? 0) - (itemBalance.LastDebit ?? 0);
                        detail.Amount += currentAmount;

                        cachedItems.Add(itemBalance.Id);
                    }
                }

                foreach (var iCache in cachedItems)
                {
                    BalanceJournalDetailViewModel current = mappedResult.Where(m => m.Id == iCache).FirstOrDefault();
                    int iCacheIndex = mappedResult.IndexOf(current);
                    current.IsChecked         = true;
                    mappedResult[iCacheIndex] = current;
                }

                formattedResult.Add(detail);
            }

            return(formattedResult);
        }
        public List <BalanceSheetDetailViewModel> RetrieveBalance(int headerId, bool isActiva)
        {
            List <JournalMaster> allJournalMaster = _journalMasterRepository.GetAll().ToList();

            List <BalanceJournalDetailViewModel> mappedResult    = base.RetrieveBalanceJournalDetailsByHeaderId(headerId);
            List <BalanceSheetDetailViewModel>   formattedResult = new List <BalanceSheetDetailViewModel>();

            Reference catCurrentAssetJournal = _referenceRepository.GetMany(r => r.Code == DbConstant.REF_CAT_JOURNAL_CURRENTASSET).FirstOrDefault();
            Reference catFixedAssetJournal   = _referenceRepository.GetMany(r => r.Code == DbConstant.REF_CAT_JOURNAL_FIXEDASSET).FirstOrDefault();
            Reference catObligationJournal   = _referenceRepository.GetMany(r => r.Code == DbConstant.REF_CAT_JOURNAL_OBLIGATION).FirstOrDefault();
            Reference catFundJournal         = _referenceRepository.GetMany(r => r.Code == DbConstant.REF_CAT_JOURNAL_FUND).FirstOrDefault();

            List <Reference> listCurrentAssetJournal = _referenceRepository.GetMany(r => r.ParentId == catCurrentAssetJournal.Id).ToList();
            List <Reference> listFixedAssetJournal   = _referenceRepository.GetMany(r => r.ParentId == catFixedAssetJournal.Id).ToList();
            List <Reference> listObligationJournal   = _referenceRepository.GetMany(r => r.ParentId == catObligationJournal.Id).ToList();
            List <Reference> listFundJournal         = _referenceRepository.GetMany(r => r.ParentId == catFundJournal.Id).ToList();

            if (isActiva)
            {
                BalanceSheetViewModel headerCurrentAsset = new BalanceSheetViewModel();
                headerCurrentAsset.GroupName = "Aktiva Lancar";

                foreach (var item in listCurrentAssetJournal)
                {
                    BalanceSheetDetailViewModel detail = new BalanceSheetDetailViewModel();
                    detail.Header = headerCurrentAsset;
                    JournalMaster currentJournal = allJournalMaster.Where(j => j.Code == item.Value).FirstOrDefault();
                    detail.Name = currentJournal.Name;

                    List <int> cachedItems = new List <int>();
                    foreach (var itemBalance in mappedResult.Where(m => !m.IsChecked))
                    {
                        if (base.IsCurrentJournalValid(itemBalance.Journal, item.Value))
                        {
                            decimal currentAmount = (itemBalance.LastDebit ?? 0) - (itemBalance.LastCredit ?? 0);
                            detail.Amount += currentAmount;

                            cachedItems.Add(itemBalance.Id);
                        }
                    }

                    foreach (var iCache in cachedItems)
                    {
                        BalanceJournalDetailViewModel current = mappedResult.Where(m => m.Id == iCache).FirstOrDefault();
                        int iCacheIndex = mappedResult.IndexOf(current);
                        current.IsChecked         = true;
                        mappedResult[iCacheIndex] = current;
                    }

                    formattedResult.Add(detail);
                }

                BalanceSheetViewModel headerFixedAsset = new BalanceSheetViewModel();
                headerFixedAsset.GroupName = "Aktiva Tetap";

                foreach (var item in listFixedAssetJournal)
                {
                    BalanceSheetDetailViewModel detail = new BalanceSheetDetailViewModel();
                    detail.Header = headerFixedAsset;
                    JournalMaster currentJournal = allJournalMaster.Where(j => j.Code == item.Value).FirstOrDefault();
                    detail.Name = currentJournal.Name;

                    List <int> cachedItems = new List <int>();
                    foreach (var itemBalance in mappedResult.Where(m => !m.IsChecked))
                    {
                        if (base.IsCurrentJournalValid(itemBalance.Journal, item.Value))
                        {
                            decimal currentAmount = (itemBalance.LastDebit ?? 0) - (itemBalance.LastCredit ?? 0);
                            detail.Amount += currentAmount;

                            cachedItems.Add(itemBalance.Id);
                        }
                    }

                    foreach (var iCache in cachedItems)
                    {
                        BalanceJournalDetailViewModel current = mappedResult.Where(m => m.Id == iCache).FirstOrDefault();
                        int iCacheIndex = mappedResult.IndexOf(current);
                        current.IsChecked         = true;
                        mappedResult[iCacheIndex] = current;
                    }

                    formattedResult.Add(detail);
                }
            }
            else
            {
                BalanceSheetViewModel headerObligation = new BalanceSheetViewModel();
                headerObligation.GroupName = "Kewajiban";

                foreach (var item in listObligationJournal)
                {
                    BalanceSheetDetailViewModel detail = new BalanceSheetDetailViewModel();
                    detail.Header = headerObligation;
                    JournalMaster currentJournal = allJournalMaster.Where(j => j.Code == item.Value).FirstOrDefault();
                    detail.Name = currentJournal.Name;

                    List <int> cachedItems = new List <int>();
                    foreach (var itemBalance in mappedResult.Where(m => !m.IsChecked))
                    {
                        if (base.IsCurrentJournalValid(itemBalance.Journal, item.Value))
                        {
                            decimal currentAmount = (itemBalance.LastDebit ?? 0) - (itemBalance.LastCredit ?? 0);
                            detail.Amount += Math.Abs(currentAmount);

                            cachedItems.Add(itemBalance.Id);
                        }
                    }

                    foreach (var iCache in cachedItems)
                    {
                        BalanceJournalDetailViewModel current = mappedResult.Where(m => m.Id == iCache).FirstOrDefault();
                        int iCacheIndex = mappedResult.IndexOf(current);
                        current.IsChecked         = true;
                        mappedResult[iCacheIndex] = current;
                    }

                    formattedResult.Add(detail);
                }

                BalanceSheetViewModel headerFund = new BalanceSheetViewModel();
                headerFund.GroupName = "Modal";

                foreach (var item in listFundJournal)
                {
                    BalanceSheetDetailViewModel detail = new BalanceSheetDetailViewModel();
                    detail.Header = headerFund;
                    JournalMaster currentJournal = allJournalMaster.Where(j => j.Code == item.Value).FirstOrDefault();
                    detail.Name = currentJournal.Name;

                    List <int> cachedItems = new List <int>();
                    foreach (var itemBalance in mappedResult.Where(m => !m.IsChecked))
                    {
                        if (base.IsCurrentJournalValid(itemBalance.Journal, item.Value))
                        {
                            decimal currentAmount = (itemBalance.LastDebit ?? 0) - (itemBalance.LastCredit ?? 0);
                            detail.Amount += Math.Abs(currentAmount);

                            cachedItems.Add(itemBalance.Id);
                        }
                    }

                    foreach (var iCache in cachedItems)
                    {
                        BalanceJournalDetailViewModel current = mappedResult.Where(m => m.Id == iCache).FirstOrDefault();
                        int iCacheIndex = mappedResult.IndexOf(current);
                        current.IsChecked         = true;
                        mappedResult[iCacheIndex] = current;
                    }

                    formattedResult.Add(detail);
                }
            }

            return(formattedResult);
        }