public void CreateFinancialSalesOrManufacturerExpenseDetail(
            Session session,
            ETL_SalesOrManufacturerExpenseDetail Detail,
            string MainAccountCode, string HighestAccountCode)
        {
            try
            {
                if (Detail == null ||
                    MainAccountCode.Equals(string.Empty) ||
                    Detail.OwnerOrgId.Equals(Guid.Empty) ||
                    Detail.IssueDate == null)
                {
                    return;
                }

                #region prepare Summary header data
                Util            util         = new Util();
                ETLAccountingBO accountingBO = new ETLAccountingBO();
                FinancialSalesOrManufactureExpenseSummary_Fact summary =
                    GetFinancialSalesOrManufactureExpenseSummary(
                        session,
                        Detail.OwnerOrgId,
                        Detail.IssueDate);

                FinancialSalesOrManufactureExpenseDetail newDetail = new FinancialSalesOrManufactureExpenseDetail(session);
                if (summary == null)
                {
                    summary =
                        CreateFinancialSalesOrManufactureExpenseSummary(
                            session,
                            Detail.OwnerOrgId,
                            Detail.IssueDate,
                            Detail.IsBalanceForward);
                    if (summary == null)
                    {
                        return;
                    }
                }
                #endregion

                #region prepare group data
                CorrespondFinancialAccountDim correspondFinancialAccountDim = null;
                FinancialAccountDim           financialAccountDim           = null;
                FinancialAccountDim           GroupAccountDim = null;

                if (!HighestAccountCode.Equals(string.Empty))
                {
                    GroupAccountDim = util.GetXpoObjectByFieldName <FinancialAccountDim, string>(
                        session,
                        "Code",
                        HighestAccountCode,
                        BinaryOperatorType.Equal);
                }

                if (!MainAccountCode.Equals(string.Empty))
                {
                    financialAccountDim = util.GetXpoObjectByFieldName <FinancialAccountDim, string>(
                        session,
                        "Code",
                        MainAccountCode,
                        BinaryOperatorType.Equal);
                }

                if (!Detail.CorrespondAccountCode.Equals(string.Empty))
                {
                    correspondFinancialAccountDim = util.GetXpoObjectByFieldName <CorrespondFinancialAccountDim, string>(
                        session,
                        "Code",
                        Detail.CorrespondAccountCode,
                        BinaryOperatorType.Equal);
                }

                if (GroupAccountDim == null && !HighestAccountCode.Equals(string.Empty))
                {
                    GroupAccountDim = accountingBO.CreateFinancialAccountDim(session, HighestAccountCode);
                }


                if (financialAccountDim == null && !MainAccountCode.Equals(string.Empty))
                {
                    financialAccountDim = accountingBO.CreateFinancialAccountDim(session, MainAccountCode);
                }

                if (correspondFinancialAccountDim == null && !Detail.CorrespondAccountCode.Equals(string.Empty))
                {
                    correspondFinancialAccountDim = accountingBO.CreateCorrespondFinancialAccountDim(session, Detail.CorrespondAccountCode);
                }

                SalesOrManufactureExpenseByGroup group =
                    GetSalesOrManufactureExpenseByGroup(
                        session,
                        summary.FinancialSalesOrManufactureExpenseSummary_FactId,
                        HighestAccountCode);

                if (group == null)
                {
                    group = CreateSalesOrManufactureExpenseByGroup(
                        session,
                        summary.FinancialSalesOrManufactureExpenseSummary_FactId,
                        HighestAccountCode);

                    if (group == null)
                    {
                        return;
                    }
                }

                #endregion

                #region prepare Detail
                CurrencyDim currencyDim = util.GetXpoObjectByFieldName <CurrencyDim, string>(
                    session,
                    "Code",
                    Detail.CurrencyCode,
                    BinaryOperatorType.Equal);

                if (currencyDim == null && !Detail.CurrencyCode.Equals(string.Empty))
                {
                    currencyDim = accountingBO.CreateCurrencyDim(session, Detail.CurrencyCode);
                }

                FinancialTransactionDim financialTransactionDim = util.GetXpoObjectByFieldName <FinancialTransactionDim, Guid>(
                    session,
                    "RefId",
                    Detail.TransactionId,
                    BinaryOperatorType.Equal);

                if (financialTransactionDim == null)
                {
                    financialTransactionDim = accountingBO.CreateFinancialTransactionDim(session, Detail.TransactionId);
                    if (financialTransactionDim == null)
                    {
                        return;
                    }
                }
                newDetail.Credit                             = Detail.Credit;
                newDetail.Debit                              = Detail.Debit;
                newDetail.CurrencyDimId                      = currencyDim;
                newDetail.FinancialTransactionDimId          = financialTransactionDim;
                newDetail.FinancialAccountDimId              = financialAccountDim;
                newDetail.CorrespondFinancialAccountDimId    = correspondFinancialAccountDim;
                newDetail.SalesOrManufactureExpenseByGroupId = group;
                CorrespondFinancialAccountDim defaultCorrespondindAcc = CorrespondFinancialAccountDim.GetDefault(session, CorrespondFinancialAccountDimEnum.NAAN_DEFAULT);
                FinancialAccountDim           defaultFinancialAcc     = FinancialAccountDim.GetDefault(session, FinancialAccountDimEnum.NAAN_DEFAULT);

                if (newDetail.FinancialAccountDimId == null)
                {
                    newDetail.FinancialAccountDimId = defaultFinancialAcc;
                }
                if (newDetail.CorrespondFinancialAccountDimId == null)
                {
                    newDetail.CorrespondFinancialAccountDimId = defaultCorrespondindAcc;
                }

                newDetail.RowStatus = Constant.ROWSTATUS_ACTIVE;
                newDetail.Save();
                #endregion

                if (Detail.IsBalanceForward)
                {
                    group.SumExpense = Detail.Credit;
                }
                else
                {
                    group.SumExpense = group.FinancialSalesOrManufactureExpenseDetails.Where(i => i.RowStatus >= 1 && i.Debit > 0).
                                       Sum(r => r.Debit);
                    group.Save();
                }

                group.Save();
                summary.Save();
            }
            catch (Exception)
            {
                return;
            }
        }
        protected ETL_SalesOrManufacturerExpenseTransformData TransformTransactionSalesOrManufacturerExpenseDetail
        (
            Session session,
            ETL_SalesOrManufacturerExpenseTransaction transaction,
            string AccountCode)
        {
            Util util = new Util();
            ETL_SalesOrManufacturerExpenseTransformData result = new ETL_SalesOrManufacturerExpenseTransformData();
            Account account = util.GetXpoObjectByFieldName <Account, string>(session, "Code", AccountCode, BinaryOperatorType.Equal);

            if (account == null)
            {
                return(null);
            }
            if (transaction == null)
            {
                return(null);
            }
            ETL_Transaction etlTransaction = transaction;

            string subMainAccount = string.Empty;

            List <ETL_SalesOrManufacturerExpenseDetail> detail = new List <ETL_SalesOrManufacturerExpenseDetail>();

            try
            {
                ETLAccountingBO           accountingBO      = new ETLAccountingBO();
                FinancialLiabilityBO      liabilityBO       = new FinancialLiabilityBO();
                List <ETL_GeneralJournal> JournalListJoined = JoinJournal(session, etlTransaction.GeneralJournalList, AccountCode);
                List <ETL_GeneralJournal> FinishJournalList = liabilityBO.ClearJournalList(session, JournalListJoined, account.AccountId);
                foreach (ETL_GeneralJournal journal in FinishJournalList)
                {
                    ETL_SalesOrManufacturerExpenseDetail temp = new ETL_SalesOrManufacturerExpenseDetail();
                    temp.AccountCode           = string.Empty;
                    temp.CorrespondAccountCode = string.Empty;
                    if (accountingBO.IsRelateAccount(session, account.AccountId, journal.AccountId))
                    {
                        temp.AccountCode = subMainAccount = session.GetObjectByKey <Account>(journal.AccountId).Code;
                    }
                    else
                    {
                        temp.CorrespondAccountCode = session.GetObjectByKey <Account>(journal.AccountId).Code;
                    }
                    temp.CurrencyCode     = session.GetObjectByKey <Currency>(journal.CurrencyId).Code;
                    temp.IsBalanceForward = etlTransaction.IsBalanceForward;
                    temp.IssueDate        = etlTransaction.IssuedDate;
                    temp.OwnerOrgId       = etlTransaction.OwnerOrgId;
                    temp.TransactionId    = etlTransaction.TransactionId;
                    temp.Credit           = (decimal)journal.Credit;
                    temp.Debit            = (decimal)journal.Debit;
                    detail.Add(temp);
                }

                result.ETL_DetailList     = detail;
                result.MainAccountCode    = subMainAccount;
                result.HighestAccountCode = AccountCode;
            }
            catch (Exception)
            {
                return(null);
            }
            return(result);
        }