public BalanceJournalViewModel RetrieveFirstBalance(int id)
        {
            BalanceJournal result = _balanceJournalRepository.GetById(id);
            BalanceJournalViewModel mappedResult = new BalanceJournalViewModel();

            return Map(result, mappedResult);
        }
        public void InsertFirstBalance(BalanceJournalViewModel parent,
            List<BalanceJournalDetailViewModel> details, int userId)
        {
            BalanceJournal entity = new BalanceJournal();
            Map(parent, entity);
            entity.IsFirst = true;
            entity.Status = (int)DbConstant.DefaultDataStatus.Active;
            entity.CreateUserId = entity.ModifyUserId = userId;
            entity.CreateDate = entity.ModifyDate = DateTime.Now;
            _balanceJournalRepository.AttachNavigation<User>(entity.CreateUser);
            _balanceJournalRepository.AttachNavigation<User>(entity.ModifyUser);
            entity = _balanceJournalRepository.Add(entity);
            _unitOfWork.SaveChanges();

            foreach (var balanceDetail in details)
            {
                BalanceJournalDetail newDetailEntity = new BalanceJournalDetail();
                Map(balanceDetail, newDetailEntity);
                newDetailEntity.ParentId = entity.Id;
                _balanceJournalDetailRepository.AttachNavigation<BalanceJournal>(newDetailEntity.Parent);
                _balanceJournalDetailRepository.AttachNavigation<JournalMaster>(newDetailEntity.Journal);
                _balanceJournalDetailRepository.Add(newDetailEntity);
            }
            _unitOfWork.SaveChanges();
        }
 public BalanceJournalViewModel RetrieveBalanceJournalHeaderById(int headerId)
 {
     BalanceJournal result = _balanceJournalRepository.GetById(headerId);
     BalanceJournalViewModel mappedResult = new BalanceJournalViewModel();
     return Map(result, mappedResult);
 }
 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 void UpdateFirstBalance(BalanceJournalViewModel parent,
            List<BalanceJournalDetailViewModel> details, int userId)
        {
            BalanceJournal entity = _balanceJournalRepository.GetById(parent.Id);
            Map(parent, entity);
            entity.ModifyUserId = userId;
            entity.ModifyDate = DateTime.Now;
            _balanceJournalRepository.AttachNavigation<User>(entity.CreateUser);
            _balanceJournalRepository.AttachNavigation<User>(entity.ModifyUser);
            _balanceJournalRepository.Update(entity);
            _unitOfWork.SaveChanges();

            foreach (var balanceDetail in details)
            {
                if(balanceDetail.Id > 0)
                {
                    BalanceJournalDetail detailEntity = _balanceJournalDetailRepository.GetById(balanceDetail.Id);
                    Map(balanceDetail, detailEntity);
                    _balanceJournalDetailRepository.AttachNavigation<BalanceJournal>(detailEntity.Parent);
                    _balanceJournalDetailRepository.AttachNavigation<JournalMaster>(detailEntity.Journal);
                    _balanceJournalDetailRepository.Update(detailEntity);
                }
                else
                {
                    BalanceJournalDetail detailEntity = new BalanceJournalDetail();
                    Map(balanceDetail, detailEntity);
                    detailEntity.ParentId = entity.Id;
                    _balanceJournalDetailRepository.AttachNavigation<BalanceJournal>(detailEntity.Parent);
                    _balanceJournalDetailRepository.AttachNavigation<JournalMaster>(detailEntity.Journal);
                    _balanceJournalDetailRepository.Add(detailEntity);
                }
            }
            _unitOfWork.SaveChanges();
        }