/// <summary>
        /// Gets the opening account entries.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        public OpeningAccountEntryResponse GetOpeningAccountEntries(OpeningAccountEntryRequest request)
        {
            var response = new OpeningAccountEntryResponse();

            if (request.LoadOptions.Contains("OpeningAccountEntries"))
            {
                response.OpeningAccountEntries = OpeningAccountEntryDao.GetOpeningAccountEntries();
            }
            if (request.LoadOptions.Contains("OpeningAccountEntry"))
            {
                var openingAccountEntry = OpeningAccountEntryDao.GetOpeningAccountEntryEntityByAccountCode(request.AccountCode) ?? new OpeningAccountEntryEntity();
                if (request.LoadOptions.Contains("IncludeDetail"))
                {
                    openingAccountEntry.OpeningAccountEntryDetails = OpeningAccountEntryDetailDao.GetOpeningAccountEntryDetailsByRefId(openingAccountEntry.RefId);
                }
                response.OpeningAccountEntry = openingAccountEntry;
            }
            return(response);
        }
        /// <summary>
        /// Sets the opening account entries.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        public OpeningAccountEntryResponse SetOpeningAccountEntries(OpeningAccountEntryRequest request)
        {
            var response = new OpeningAccountEntryResponse();

            var openingAccountEntry = request.OpeningAccountEntry;

            //var auditingLog = new AudittingLogEntity { ComponentName = "SO DU DAU KY TSCD", EventAction = (int)request.Action };
            if (request.Action != PersistType.Delete)
            {
                if (!openingAccountEntry.Validate())
                {
                    foreach (var error in openingAccountEntry.ValidationErrors)
                    {
                        response.Message += error + Environment.NewLine;
                    }
                    response.Acknowledge = AcknowledgeType.Failure;
                    return(response);
                }
            }
            try
            {
                switch (request.Action)
                {
                case PersistType.Insert:
                    break;

                case PersistType.Update:
                    using (var scope = new TransactionScope())
                    {
                        //insert or update master
                        var account = AccountDao.GetAccountByAccountCode(openingAccountEntry.AccountCode);
                        var openingAccountEntryForUpdate = OpeningAccountEntryDao.GetOpeningAccountEntryEntityByAccountCode(openingAccountEntry.AccountCode);
                        if (openingAccountEntryForUpdate != null)
                        {
                            openingAccountEntryForUpdate.TotalAccountBeginningDebitAmountOC  = 0;
                            openingAccountEntryForUpdate.TotalAccountBeginningCreditAmountOC = 0;
                            openingAccountEntryForUpdate.TotalDebitAmountOC  = 0;
                            openingAccountEntryForUpdate.TotalCreditAmountOC = 0;
                            openingAccountEntryForUpdate.TotalAccountBeginningDebitAmountExchange  = 0;
                            openingAccountEntryForUpdate.TotalAccountBeginningCreditAmountExchange = 0;
                            openingAccountEntryForUpdate.TotalDebitAmountExchange  = 0;
                            openingAccountEntryForUpdate.TotalCreditAmountExchange = 0;

                            foreach (var openingAccountEntryDetailEntity in openingAccountEntry.OpeningAccountEntryDetails)
                            {
                                openingAccountEntryForUpdate.TotalAccountBeginningDebitAmountOC  += openingAccountEntryDetailEntity.AccountBeginningDebitAmountOC;
                                openingAccountEntryForUpdate.TotalAccountBeginningCreditAmountOC += openingAccountEntryDetailEntity.AccountBeginningCreditAmountOC;
                                openingAccountEntryForUpdate.TotalDebitAmountOC  += openingAccountEntryDetailEntity.DebitAmountOC;
                                openingAccountEntryForUpdate.TotalCreditAmountOC += openingAccountEntryDetailEntity.CreditAmountOC;
                                openingAccountEntryForUpdate.TotalAccountBeginningDebitAmountExchange  += openingAccountEntryDetailEntity.AccountBeginningDebitAmountExchange;
                                openingAccountEntryForUpdate.TotalAccountBeginningCreditAmountExchange += openingAccountEntryDetailEntity.AccountBeginningCreditAmountExchange;
                                openingAccountEntryForUpdate.TotalDebitAmountExchange  += openingAccountEntryDetailEntity.DebitAmountExchange;
                                openingAccountEntryForUpdate.TotalCreditAmountExchange += openingAccountEntryDetailEntity.CreditAmountExchange;
                            }
                            response.Message = OpeningAccountEntryDao.UpdateOpeningAccountEntry(openingAccountEntryForUpdate);
                            if (response.Message != null)
                            {
                                response.Acknowledge = AcknowledgeType.Failure;
                                scope.Dispose();
                                return(response);
                            }
                            openingAccountEntry.RefId = openingAccountEntryForUpdate.RefId;
                        }
                        else
                        {
                            foreach (var openingAccountEntryDetailEntity in openingAccountEntry.OpeningAccountEntryDetails)
                            {
                                openingAccountEntry.TotalAccountBeginningDebitAmountOC  += openingAccountEntryDetailEntity.AccountBeginningDebitAmountOC;
                                openingAccountEntry.TotalAccountBeginningCreditAmountOC += openingAccountEntryDetailEntity.AccountBeginningCreditAmountOC;
                                openingAccountEntry.TotalDebitAmountOC  += openingAccountEntryDetailEntity.DebitAmountOC;
                                openingAccountEntry.TotalCreditAmountOC += openingAccountEntryDetailEntity.CreditAmountOC;
                                openingAccountEntry.TotalAccountBeginningDebitAmountExchange  += openingAccountEntryDetailEntity.AccountBeginningDebitAmountExchange;
                                openingAccountEntry.TotalAccountBeginningCreditAmountExchange += openingAccountEntryDetailEntity.AccountBeginningCreditAmountExchange;
                                openingAccountEntry.TotalDebitAmountExchange  += openingAccountEntryDetailEntity.DebitAmountExchange;
                                openingAccountEntry.TotalCreditAmountExchange += openingAccountEntryDetailEntity.CreditAmountExchange;
                            }
                            openingAccountEntry.RefId = OpeningAccountEntryDao.InsertOpeningAccountEntry(openingAccountEntry);
                            if (openingAccountEntry.RefId == 0)
                            {
                                response.Acknowledge = AcknowledgeType.Failure;
                                scope.Dispose();
                                return(response);
                            }
                        }

                        //delete JournalEntryAccount
                        response.Message = JournalEntryAccountDao.DeleteJournalEntryAccount(openingAccountEntry.RefId, openingAccountEntry.RefTypeId);
                        if (response.Message != null)
                        {
                            response.Acknowledge = AcknowledgeType.Failure;
                            scope.Dispose();
                            return(response);
                        }

                        //delete & insert detail
                        response.Message = OpeningAccountEntryDetailDao.DeleteOpeningAccountEntryDetailByRefId(openingAccountEntry.RefId);
                        if (response.Message != null)
                        {
                            response.Acknowledge = AcknowledgeType.Failure;
                            scope.Dispose();
                            return(response);
                        }
                        foreach (var openingAccountEntryDetailEntity in openingAccountEntry.OpeningAccountEntryDetails)
                        {
                            openingAccountEntryDetailEntity.RefId       = openingAccountEntry.RefId;
                            openingAccountEntryDetailEntity.RefDetailId = OpeningAccountEntryDetailDao.InsertOpeningAccountEntryDetail(openingAccountEntryDetailEntity);
                            if (openingAccountEntryDetailEntity.RefDetailId == 0)
                            {
                                response.Acknowledge = AcknowledgeType.Failure;
                                scope.Dispose();
                                return(response);
                            }
                            //insert JournalEntryAccount
                            var journalEntryAccount = AddJournalEntryAccount(openingAccountEntry, openingAccountEntryDetailEntity, account.BalanceSide);
                            if (!journalEntryAccount.Validate())
                            {
                                foreach (var error in journalEntryAccount.ValidationErrors)
                                {
                                    response.Message += error + Environment.NewLine;
                                }
                                response.Acknowledge = AcknowledgeType.Failure;
                                return(response);
                            }
                            journalEntryAccount.RefId = JournalEntryAccountDao.InsertJournalEntryAccount(journalEntryAccount);
                            if (journalEntryAccount.RefId == 0)
                            {
                                response.Acknowledge = AcknowledgeType.Failure;
                                scope.Dispose();
                                return(response);
                            }

                            //insert AccountBalance
                            //InsertAccountBalance(openingAccountEntry, openingAccountEntryDetailEntity, account.BalanceSide);
                        }
                        //insert log
                        //auditingLog.Reference = "Cập nhật CT số dư đầu kỳ cho tài khoản";
                        //auditingLog.Amount = 0;
                        //AudittingLogDao.InsertAudittingLog(auditingLog);

                        scope.Complete();
                    }
                    break;

                default:
                    using (var scope = new TransactionScope())
                    {
                        //insert log
                        //auditingLog.Reference = "Xóa CT số dư đầu kỳ cho tài khoản ";
                        //auditingLog.Amount = 0;
                        //AudittingLogDao.InsertAudittingLog(auditingLog);

                        scope.Complete();
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                response.Acknowledge = AcknowledgeType.Failure;
                response.Message     = ex.Message;
                return(response);
            }

            response.RefId       = openingAccountEntry.RefId;
            response.Acknowledge = response.Message != null ? AcknowledgeType.Failure : AcknowledgeType.Success;
            return(response);
        }