private SalesOrManufactureExpenseByGroup CreateSalesOrManufactureExpenseByGroup(Session session, int summaryFactId, string HighestAccountCode)
        {
            SalesOrManufactureExpenseByGroup result = new SalesOrManufactureExpenseByGroup(session);

            try
            {
                Util util = new Util();
                FinancialAccountDim GroupAccountDim = null;
                FinancialSalesOrManufactureExpenseSummary_Fact summary = session.GetObjectByKey <FinancialSalesOrManufactureExpenseSummary_Fact>(summaryFactId);

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

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

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

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

                result.RowStatus = Constant.ROWSTATUS_ACTIVE;
                result.FinancialSalesOrManufactureExpenseSummary_FactId = summary;
                result.FinancialAccountDimId = GroupAccountDim;
                result.Save();
                return(result);
            }
            catch (Exception)
            {
                return(null);
            }
        }
        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;
            }
        }