/// <summary>
        /// Create Gl Journal
        /// </summary>
        /// <param name="revenueRecognitionPlan">Revenue Recognition Plan</param>
        /// <param name="revenurecognitionRun">Revenue Recognition Run</param>
        /// <param name="recFrequency">Frequency</param>
        /// <returns>Journal object</returns>
        public MJournal CreateJournalHDR(MRevenueRecognitionPlan revenueRecognitionPlan, MRevenueRecognitionRun revenurecognitionRun, string recFrequency)
        {
            journal.SetClientOrg(revenueRecognitionPlan.GetAD_Client_ID(), revenueRecognitionPlan.GetAD_Org_ID());
            journal.SetC_AcctSchema_ID(revenueRecognitionPlan.GetC_AcctSchema_ID());
            journal.SetDescription("Revenue Recognition Run");
            journal.SetPostingType(MJournal.POSTINGTYPE_Actual);

            int GL_Category_ID = Util.GetValueOfInt(DB.ExecuteScalar(@"SELECT GL_Category_ID From GL_Category Where CategoryType='M' 
            AND  AD_Client_ID= " + revenueRecognitionPlan.GetAD_Client_ID() + " Order by GL_Category_ID desc"));

            journal.SetGL_Category_ID(GL_Category_ID);

            journal.SetDateDoc(DateTime.Now);
            DateTime firstOfNextMonth = new DateTime(revenurecognitionRun.GetRecognitionDate().Value.Year, revenurecognitionRun.GetRecognitionDate().Value.Month, 1).AddMonths(1);
            DateTime lastOfThisMonth  = firstOfNextMonth.AddDays(-1);

            if (recFrequency.Equals("D")) //DAY
            {
                //in case of DAY , Account date would be same as Recoganize date
                journal.SetDateAcct(revenurecognitionRun.GetRecognitionDate());
            }
            else
            {
                journal.SetDateAcct(lastOfThisMonth);
            }

            string periodSql = "SELECT C_Period_ID From C_Period pr  INNER JOIN c_year yr ON (yr.c_year_id = pr.c_year_id AND yr.c_calendar_id= " +
                               "(CASE WHEN (SELECT NVL(C_Calendar_ID,0) FROM AD_Orginfo WHERE AD_org_ID =" + revenueRecognitionPlan.GetAD_Org_ID() + " ) =0 THEN (SELECT  NVL(C_Calendar_ID,0) FROM AD_ClientInfo WHERE AD_Client_ID =" + revenueRecognitionPlan.GetAD_Client_ID() + ") ELSE " +
                               "(SELECT NVL(C_Calendar_ID,0) FROM AD_Orginfo WHERE AD_org_ID =" + revenueRecognitionPlan.GetAD_Org_ID() + ") END ) ) WHERE " + GlobalVariable.TO_DATE(revenurecognitionRun.GetRecognitionDate(), true) + " BETWEEN StartDate and EndDate";

            C_Period_ID = Util.GetValueOfInt(DB.ExecuteScalar(periodSql));

            journal.SetC_Period_ID(C_Period_ID);
            journal.SetC_Currency_ID(revenueRecognitionPlan.GetC_Currency_ID());
            int C_ConversionType_ID = Util.GetValueOfInt(DB.ExecuteScalar("SELECT C_ConversionType_ID From C_ConversionType WHERE IsDefault='Y'"));

            journal.SetC_ConversionType_ID(C_ConversionType_ID);
            journal.SetTotalCr(revenueRecognitionPlan.GetTotalAmt());
            journal.SetTotalDr(revenueRecognitionPlan.GetTotalAmt());
            journal.SetDocStatus("DR");
            journal.SetDocAction("CO");

            return(journal);
        }
        /// <summary>
        /// Create GL Journal
        /// </summary>
        /// <param name="mRevenueRecognition">Revenue Recognition</param>
        /// <returns>Message</returns>
        public string CreateJournals(MRevenueRecognition mRevenueRecognition)
        {
            try
            {
                MRevenueRecognitionRun[] mRevenueRecognitionRuns = null;

                MRevenueRecognitionPlan revenueRecognitionPlan = null;
                MInvoiceLine            invoiceLine            = null;
                MInvoice invoice = null;


                mRevenueRecognitionRuns = MRevenueRecognitionRun.GetRecognitionRuns(mRevenueRecognition, _RecognitionDate, _orgId, false);
                journal_ID = new int[mRevenueRecognitionRuns.Length];
                if (mRevenueRecognitionRuns.Length > 0)
                {
                    for (int j = 0; j < mRevenueRecognitionRuns.Length; j++)
                    {
                        MRevenueRecognitionRun revenueRecognitionRun = mRevenueRecognitionRuns[j];
                        revenueRecognitionPlan = new MRevenueRecognitionPlan(GetCtx(), revenueRecognitionRun.GetC_RevenueRecognition_Plan_ID(), Get_TrxName());
                        invoiceLine            = new MInvoiceLine(GetCtx(), revenueRecognitionPlan.GetC_InvoiceLine_ID(), Get_TrxName());
                        invoice = new MInvoice(GetCtx(), invoiceLine.GetC_Invoice_ID(), Get_TrxName());

                        if (revenueRecognitionPlan.GetC_AcctSchema_ID() != _AcctSchema_ID || revenueRecognitionPlan.GetC_Currency_ID() != _Currency_ID || revenueRecognitionRun.GetRecognitionDate() != _RecognizeDate)
                        {
                            if (journal != null)
                            {
                                if (DocNo == null)
                                {
                                    DocNo = journal.GetDocumentNo();
                                }
                                else
                                {
                                    DocNo += ", " + journal.GetDocumentNo();
                                }
                                journal_ID[j - 1] = journal.GetGL_Journal_ID();
                            }
                            journal = new MJournal(GetCtx(), 0, Get_TrxName());

                            journal.SetC_DocType_ID(_DocType);
                            journal = CreateJournalHDR(revenueRecognitionPlan, revenueRecognitionRun, mRevenueRecognition.GetRecognitionFrequency());


                            if (journal.Save())
                            {
                                _AcctSchema_ID = journal.GetC_AcctSchema_ID();
                                _Currency_ID   = journal.GetC_Currency_ID();
                                _RecognizeDate = revenueRecognitionRun.GetRecognitionDate();
                                lineno         = Util.GetValueOfInt(DB.ExecuteScalar("SELECT NVL(MAX(Line), 0)+10  AS DefaultValue FROM GL_JournalLine WHERE GL_Journal_ID=" + journal.GetGL_Journal_ID(), null, invoice.Get_Trx()));
                            }
                            else
                            {
                                pp = VLogger.RetrieveError();
                                if (pp != null)
                                {
                                    errorMsg = pp.GetName();
                                    if (errorMsg == "")
                                    {
                                        errorMsg = pp.GetValue();
                                    }
                                }
                                if (errorMsg == "")
                                {
                                    errorMsg = Msg.GetMsg(GetCtx(), "GLJournalNotCreated");
                                }
                                Get_TrxName().Rollback();
                                return(errorMsg);
                            }
                        }
                        for (int k = 0; k < 2; k++)
                        {
                            journalLine = new MJournalLine(journal);
                            journalLine = GenerateJounalLine(journal, invoice, invoiceLine, revenueRecognitionPlan, revenueRecognitionRun, mRevenueRecognition.GetRecognitionType(), k);
                            if (journalLine.Save())
                            {
                                revenueRecognitionRun.SetGL_Journal_ID(journal.GetGL_Journal_ID());
                                revenueRecognitionRun.Save();
                                lineno += 10;
                            }
                            else
                            {
                                pp = VLogger.RetrieveError();
                                if (pp != null)
                                {
                                    errorMsg = pp.GetName();
                                    if (errorMsg == "")
                                    {
                                        errorMsg = pp.GetValue();
                                    }
                                }
                                if (errorMsg == "")
                                {
                                    errorMsg = Msg.GetMsg(GetCtx(), "GLJournalNotCreated");
                                }
                                Get_TrxName().Rollback();
                                return(errorMsg);
                            }
                        }
                        revenueRecognitionPlan.SetRecognizedAmt(revenueRecognitionRun.GetRecognizedAmt() + revenueRecognitionPlan.GetRecognizedAmt());
                        if (!revenueRecognitionPlan.Save())
                        {
                            pp = VLogger.RetrieveError();
                            if (pp != null)
                            {
                                errorMsg = pp.GetName();
                                if (errorMsg == "")
                                {
                                    errorMsg = pp.GetValue();
                                }
                            }
                            if (errorMsg == "")
                            {
                                errorMsg = Msg.GetMsg(GetCtx(), "GLJournalNotCreated");
                            }
                            Get_TrxName().Rollback();
                            return(errorMsg);
                        }
                    }
                    if (journal != null)
                    {
                        if (DocNo == null)
                        {
                            DocNo = journal.GetDocumentNo();
                        }
                        else
                        {
                            DocNo += ", " + journal.GetDocumentNo();
                        }

                        journal_ID[journal_ID.Length - 1] = journal.GetGL_Journal_ID();
                    }
                }
            }
            catch (Exception ex)
            {
                log.Log(Level.SEVERE, Msg.GetMsg(GetCtx(), "GLJournalnotallocateddueto") + ex.Message);
                Get_TrxName().Rollback();
                return(ex.Message);
            }
            if (DocNo == null)
            {
                return(Msg.GetMsg(GetCtx(), "FoundNoRevenueRecognitionPlan"));
            }

            Get_TrxName().Commit();
            if (journal_ID != null)
            {
                for (int i = 0; i < journal_ID.Length; i++)
                {
                    if (journal_ID[i] > 0)
                    {
                        string result = CompleteOrReverse(GetCtx(), journal_ID[i], 169, "CO");
                        if (!String.IsNullOrEmpty(result))
                        {
                            journalIDS += ", " + journal_ID[i] + " " + result;
                        }
                    }
                }
            }
            if (!String.IsNullOrEmpty(journalIDS))
            {
                return(Msg.GetMsg(GetCtx(), "GLJournalCreated") + DocNo + " " + Msg.GetMsg(GetCtx(), "GLJournalNotCompleted") + journalIDS);
            }
            else
            {
                return(Msg.GetMsg(GetCtx(), "GLJournalCreated") + DocNo);
            }
        }