public List <ETL_FinancialOnTheWayBuyingGoodDetail> TransformTransactionOnTheWayBuyingGoodDetail(Session session, ETL_Transaction transaction, string AccountCode)
        {
            Util    util    = new Util();
            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;
            List <ETL_FinancialOnTheWayBuyingGoodDetail> detailList = new List <ETL_FinancialOnTheWayBuyingGoodDetail>();

            try
            {
                ETLAccountingBO      accountingBO          = new ETLAccountingBO();
                FinancialLiabilityBO liabilityBO           = new FinancialLiabilityBO();
                ETL_FinancialOnTheWayBuyingGoodDetail temp = null;
                List <ETL_GeneralJournal>             JournalListJoined = liabilityBO.JoinJournal(session, etlTransaction.GeneralJournalList);
                List <ETL_GeneralJournal>             FinishJournalList = liabilityBO.ClearJournalList(session, JournalListJoined, account.AccountId);
                foreach (ETL_GeneralJournal journal in JournalListJoined)
                {
                    temp                       = new ETL_FinancialOnTheWayBuyingGoodDetail();
                    temp.AccountCode           = "";
                    temp.CorrespondAccountCode = "";
                    if (accountingBO.IsRelateAccount(session, account.AccountId, journal.AccountId))
                    {
                        temp.AccountCode = session.GetObjectByKey <Account>(journal.AccountId).Code;
                    }
                    else
                    {
                        temp.CorrespondAccountCode = session.GetObjectByKey <Account>(journal.AccountId).Code;
                    }
                    temp.ActualPrice             = (transaction as ETL_TransactionS04a6DN).ActualPrice;
                    temp.BookedPrice             = (transaction as ETL_TransactionS04a6DN).BookedPrice;
                    temp.IssueDate               = etlTransaction.IssuedDate;
                    temp.OwnerOrgId              = etlTransaction.OwnerOrgId;
                    temp.TransactionId           = etlTransaction.TransactionId;
                    temp.Credit                  = journal.Credit;
                    temp.Debit                   = journal.Debit;
                    temp.Credit                  = journal.Credit;
                    temp.Debit                   = journal.Debit;
                    temp.CurrencyCode            = session.GetObjectByKey <Currency>(journal.CurrencyId).Code;
                    temp.TransactionId           = etlTransaction.TransactionId;
                    temp.IsBalanceForward        = etlTransaction.IsBalanceForward;
                    temp.PurchaseInvoiceId       = (transaction as ETL_TransactionS04a6DN).PurchaseInvoiceId;
                    temp.InputInventoryCommandId = (transaction as ETL_TransactionS04a6DN).InputInventoryCommandId;
                    detailList.Add(temp);
                }
            }
            catch (Exception)
            {
                return(null);
            }
            return(detailList);
        }
        public void CreateFinancialOnTheWayBuyingGoodDetail(Session session, ETL_FinancialOnTheWayBuyingGoodDetail Detail, string MainAccountCode)
        {
            try
            {
                if (Detail == null ||
                    MainAccountCode.Equals(string.Empty) ||
                    Detail.PurchaseInvoiceId.Equals(Guid.Empty) ||
                    Detail.InputInventoryCommandId.Equals(Guid.Empty) ||
                    Detail.OwnerOrgId.Equals(Guid.Empty) ||
                    Detail.IssueDate == null)
                {
                    return;
                }

                //bool flgNewSummary = false;
                Util util = new Util();
                CorrespondFinancialAccountDim defaultCorrespondindAcc = CorrespondFinancialAccountDim.GetDefault(session, CorrespondFinancialAccountDimEnum.NAAN_DEFAULT);
                FinancialAccountDim           defaultFinancialAcc     = FinancialAccountDim.GetDefault(session, FinancialAccountDimEnum.NAAN_DEFAULT);
                ETLAccountingBO accountingBO = new ETLAccountingBO();
                FinancialOnTheWayBuyingGoodSummary summary   = GetFinancialOnTheWayBuyingGoodSummary(session, Detail.OwnerOrgId, Detail.IssueDate, MainAccountCode);
                FinancialOnTheWayBuyingGoodDetail  newDetail = new FinancialOnTheWayBuyingGoodDetail(session);
                if (summary == null)
                {
                    summary = CreateFinancialOnTheWayBuyingGoodSummary(session, Detail.OwnerOrgId, Detail.IssueDate, MainAccountCode);
                    if (summary == null)
                    {
                        return;
                    }
                }
                else
                {
                    var date = new DateTime(Detail.IssueDate.Year, Detail.IssueDate.Month, 1);
                    FinancialOnTheWayBuyingGoodSummary previousSummary = GetFinancialOnTheWayBuyingGoodSummary(session,
                                                                                                               Detail.OwnerOrgId, date.AddMonths(-1), MainAccountCode);

                    if (previousSummary != null)
                    {
                        summary.BeginBalance = previousSummary.EndBalance;
                    }
                }
                /*2014/02/22 Duc.Vo MOD START*/
                CorrespondFinancialAccountDim correspondFinancialAccountDim = null;
                FinancialAccountDim           financialAccountDim           = null;
                InventoryCommand InputCommand = null;
                NAS.DAL.Invoice.PurchaseInvoice PurchaseInvoice = null;

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

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

                if (!Detail.InputInventoryCommandId.Equals(Guid.Empty))
                {
                    InputCommand = util.GetXpoObjectByFieldName <InventoryCommand, Guid>(session, "InventoryCommandId", Detail.InputInventoryCommandId, BinaryOperatorType.Equal);
                    if (InputCommand == null)
                    {
                        return;
                    }
                }
                if (!Detail.PurchaseInvoiceId.Equals(Guid.Empty))
                {
                    PurchaseInvoice = util.GetXpoObjectByFieldName <NAS.DAL.Invoice.PurchaseInvoice, Guid>(session, "BillId", Detail.PurchaseInvoiceId, BinaryOperatorType.Equal);
                    if (PurchaseInvoice == null)
                    {
                        return;
                    }
                }

                OnTheWayBuyingGoodArtifact artifact = GetOnTheWayBuyingGoodArtifact(
                    session,
                    summary.FinancialOnTheWayBuyingGoodSummaryId,
                    PurchaseInvoice.BillId,
                    InputCommand.InventoryCommandId);

                if (artifact == null)
                {
                    artifact = CreateOnTheWayBuyingGoodArtifact(
                        session,
                        summary.FinancialOnTheWayBuyingGoodSummaryId,
                        PurchaseInvoice.BillId,
                        InputCommand.InventoryCommandId);

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

                /*2014/02/22 Duc.Vo INS START*/
                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);
                }

                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.CorrespondFinancialAccountDimId = correspondFinancialAccountDim;
                newDetail.Credit                       = Detail.Credit;
                newDetail.Debit                        = Detail.Debit;
                newDetail.ActuaPrice                   = Detail.ActualPrice;
                newDetail.BookingPrice                 = Detail.BookedPrice;
                newDetail.FinancialAccountDimId        = financialAccountDim;
                newDetail.OnTheWayBuyingGoodArtifactId = artifact;
                newDetail.FinancialTransactionDimId    = financialTransactionDim;
                newDetail.CurrencyDimId                = currencyDim;
                /*2014-02-22 ERP-1417 Duc.Vo INS START*/

                if (newDetail.FinancialAccountDimId == null)
                {
                    newDetail.FinancialAccountDimId = defaultFinancialAcc;
                }
                if (newDetail.CorrespondFinancialAccountDimId == null)
                {
                    newDetail.CorrespondFinancialAccountDimId = defaultCorrespondindAcc;
                }
                /*2014-02-22 ERP-1417 Duc.Vo INS END*/
                newDetail.RowStatus = Constant.ROWSTATUS_ACTIVE;
                newDetail.Save();

                if (Detail.IsBalanceForward)
                {
                    summary.BeginBalance = summary.EndBalance = (decimal)Detail.Debit;
                    summary.CreditSum    = summary.DebitSum = 0;
                }
                else
                {
                    CorrespondFinancialAccountDim defaultAccDim = CorrespondFinancialAccountDim.GetDefault(session, CorrespondFinancialAccountDimEnum.NAAN_DEFAULT);
                    summary.CreditSum = (decimal)summary.OnTheWayBuyingGoodArtifacts.SelectMany(t => t.FinancialOnTheWayBuyingGoodDetails).
                                        Where(i => i.RowStatus == 1 &&
                                              i.Credit > 0 && i.CorrespondFinancialAccountDimId == defaultCorrespondindAcc).Sum(i => i.Credit);

                    summary.DebitSum = (decimal)summary.OnTheWayBuyingGoodArtifacts.SelectMany(t => t.FinancialOnTheWayBuyingGoodDetails).
                                       Where(i => i.RowStatus == 1 &&
                                             i.Debit > 0 && i.CorrespondFinancialAccountDimId == defaultCorrespondindAcc).Sum(i => i.Debit);

                    summary.BeginBalance = summary.BeginBalance + summary.DebitSum - summary.CreditSum;
                }

                summary.Save();
            }
            catch (Exception)
            {
                return;
            }
        }