public static MRevenueRecognitionRun[] GetRecognitionRuns(MRevenueRecognitionPlan revenueRecognitionPlan, DateTime?recognitionDate, bool reverse)
        {
            List <MRevenueRecognitionRun> list = new List <MRevenueRecognitionRun>();
            string sql = "Select * from C_RevenueRecognition_Run Where C_RevenueRecognition_Plan_ID =" + revenueRecognitionPlan.GetC_RevenueRecognition_Plan_ID();

            if (reverse)
            {
                sql += "And INT15_RecognitionDate <=" + GlobalVariable.TO_DATE(recognitionDate, true);
            }
            sql += " And NVL(GL_Journal_ID,0) <= 0";

            DataTable   dt  = null;
            IDataReader idr = null;

            try
            {
                idr = DB.ExecuteReader(sql, null, revenueRecognitionPlan.Get_Trx());
                dt  = new DataTable();
                dt.Load(idr);
                idr.Close();
                foreach (DataRow dr in dt.Rows)
                {
                    list.Add(new MRevenueRecognitionRun(revenueRecognitionPlan.GetCtx(), dr, revenueRecognitionPlan.Get_Trx()));
                }
            }
            catch (Exception e)
            {
                if (idr != null)
                {
                    idr.Close();
                }
                _log.Log(Level.SEVERE, sql, e);
            }
            finally
            {
                dt = null;
            }

            MRevenueRecognitionRun[] retValue = new MRevenueRecognitionRun[list.Count];
            retValue = list.ToArray();
            return(retValue);
        }
        public static string CreateRevenueRecognitionPlan(int C_InvoiceLine_ID, int C_RevenueRecognition_ID, MInvoice Invoice)
        {
            try
            {
                MRevenueRecognitionRun revenueRecognitionRun = null;
                DateTime?           RecognizationDate        = null;
                MRevenueRecognition revenueRecognition       = new MRevenueRecognition(Invoice.GetCtx(), C_RevenueRecognition_ID, Invoice.Get_Trx());
                int defaultAccSchemaOrg_ID = GetDefaultActSchema(Invoice.GetAD_Org_ID());
                if (defaultAccSchemaOrg_ID <= 0)
                {
                    _log.Log(Level.SEVERE, "Default Schema not found for the oraganization");
                    return("Default Schema not found for the oraganization");
                }
                MINT15AccountingSchemaOrg accountingSchemaOrg = new MINT15AccountingSchemaOrg(Invoice.GetCtx(), defaultAccSchemaOrg_ID, Invoice.Get_Trx());

                MRevenueRecognitionPlan revenueRecognitionPlan = new MRevenueRecognitionPlan(Invoice.GetCtx(), 0, Invoice.Get_Trx());
                MInvoiceLine            invoiceLine            = new MInvoiceLine(Invoice.GetCtx(), C_InvoiceLine_ID, Invoice.Get_Trx());
                MInvoice invoice = new MInvoice(Invoice.GetCtx(), invoiceLine.GetC_Invoice_ID(), Invoice.Get_Trx());

                string sql = "Select INT15_StartDate From C_InvoiceLine Where C_InvoiceLine_ID=" + invoiceLine.GetC_InvoiceLine_ID();
                RecognizationDate = Util.GetValueOfDateTime(DB.ExecuteScalar(sql));
                if (RecognizationDate == null)
                {
                    RecognizationDate = invoice.GetDateInvoiced();
                }

                revenueRecognitionPlan.SetRecognitionPlan(invoiceLine, invoice, C_RevenueRecognition_ID);
                revenueRecognitionPlan.SetC_AcctSchema_ID(accountingSchemaOrg.GetC_AcctSchema_ID());
                revenueRecognitionPlan.SetRecognizedAmt(0);
                if (revenueRecognition.GetINT15_RecognizeType() == "R")
                {
                    revenueRecognitionPlan.SetUnEarnedRevenue_Acct(RecognitionCombination(accountingSchemaOrg, "UR", invoiceLine));
                    revenueRecognitionPlan.SetP_Revenue_Acct(RecognitionCombination(accountingSchemaOrg, "TR", invoiceLine));
                }
                else
                {
                    revenueRecognitionPlan.SetINT15_PrepaidExpense(RecognitionCombination(accountingSchemaOrg, "DE", invoiceLine));
                    revenueRecognitionPlan.SetINT15_ProductExpense(RecognitionCombination(accountingSchemaOrg, "PE", invoiceLine));
                }
                revenueRecognitionPlan.Save();
                if (!revenueRecognition.IsTimeBased())
                {
                    MINT15RevenueService[] revenueService = GetServices(revenueRecognition);
                    for (int i = 0; i < revenueService.Length; i++)
                    {
                        MINT15RevenueService revenueserviceline = revenueService[i];
                        revenueRecognitionRun = new MRevenueRecognitionRun(Invoice.GetCtx(), 0, Invoice.Get_Trx());
                        revenueRecognitionRun.SetRecognitionRun(revenueRecognition, revenueserviceline, revenueRecognitionPlan);
                        Decimal recognizedAmt = (revenueRecognitionPlan.GetTotalAmt() * revenueserviceline.GetINT15_Percentage()) / 100;
                        revenueRecognitionRun.SetRecognizedAmt(recognizedAmt);
                        revenueRecognitionRun.SetINT15_RevenueService_ID(revenueserviceline.GetINT15_RevenueService_ID());

                        revenueRecognitionRun.Save();
                    }
                }
                else
                {
                    if (revenueRecognition.GetRecognitionFrequency() == "M")
                    {
                        //Decimal recognizedAmt = revenueRecognitionPlan.GetTotalAmt() / revenueRecognition.GetNoMonths();
                        double   totaldays     = (RecognizationDate.Value.AddMonths(revenueRecognition.GetNoMonths()) - RecognizationDate.Value.Date).TotalDays;
                        decimal  perdayAmt     = revenueRecognitionPlan.GetTotalAmt() / Convert.ToDecimal(totaldays);
                        decimal  recognizedAmt = 0;
                        DateTime?lastdate      = null;
                        int      days          = 0;
                        for (int i = 0; i < revenueRecognition.GetNoMonths() + 1; i++)
                        {
                            if (i == 0)
                            {
                                if (RecognizationDate.Value.Month == 12)
                                {
                                    lastdate = new DateTime(RecognizationDate.Value.Year, RecognizationDate.Value.Month, 1).AddMonths(1).AddDays(-1);
                                }
                                else
                                {
                                    lastdate = new DateTime(RecognizationDate.Value.Year, RecognizationDate.Value.Month + 1, 1).AddDays(-1);
                                }
                                days  = Util.GetValueOfInt((lastdate.Value.Date - RecognizationDate.Value.Date).TotalDays);
                                days += 1;
                            }
                            else if (i == (revenueRecognition.GetNoMonths()))
                            {
                                DateTime EndDate   = RecognizationDate.Value.AddMonths(i);
                                var      startDate = new DateTime(EndDate.Year, EndDate.Month, 1);
                                days = Util.GetValueOfInt((EndDate.Date - startDate.Date).TotalDays);
                            }
                            else
                            {
                                DateTime startdate = RecognizationDate.Value.AddMonths(i);
                                days = DateTime.DaysInMonth(startdate.Year, startdate.Month);
                            }
                            recognizedAmt         = Convert.ToDecimal(days) * perdayAmt;
                            revenueRecognitionRun = new MRevenueRecognitionRun(Invoice.GetCtx(), 0, Invoice.Get_Trx());
                            revenueRecognitionRun.SetRecognitionRun(revenueRecognition, null, revenueRecognitionPlan);
                            revenueRecognitionRun.SetRecognizedAmt(recognizedAmt);
                            revenueRecognitionRun.SetINT15_RecognitionDate(RecognizationDate.Value.AddMonths(i));
                            revenueRecognitionRun.Save();
                            recognizedAmt = 0;
                        }
                    }
                    else if (revenueRecognition.GetRecognitionFrequency() == "D")
                    {
                        Decimal recognizedAmt = revenueRecognitionPlan.GetTotalAmt() / revenueRecognition.GetNoMonths();
                        int     days          = 0;
                        for (int i = 0; i < revenueRecognition.GetNoMonths(); i++)
                        {
                            revenueRecognitionRun = new MRevenueRecognitionRun(Invoice.GetCtx(), 0, Invoice.Get_Trx());
                            revenueRecognitionRun.SetRecognitionRun(revenueRecognition, null, revenueRecognitionPlan);
                            revenueRecognitionRun.SetRecognizedAmt(recognizedAmt);
                            revenueRecognitionRun.SetINT15_RecognitionDate(RecognizationDate.Value.AddDays(days));
                            days += 1;
                            revenueRecognitionRun.Save();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(ex.ToString());
            }
            return("Revenue Recognition Created");
        }