private static MINT15RevenueService[] GetServices(MRevenueRecognition MRevenueRecognition)
        {
            List <MINT15RevenueService> list = new List <MINT15RevenueService>();
            String  sql = "SELECT * FROM INT15_RevenueService WHERE C_RevenueRecognition_ID=" + MRevenueRecognition.GetC_RevenueRecognition_ID();
            DataSet ds  = new DataSet();

            try
            {
                ds = DB.ExecuteDataset(sql, null, MRevenueRecognition.Get_TrxName());
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    MINT15RevenueService il = new MINT15RevenueService(MRevenueRecognition.GetCtx(), dr, MRevenueRecognition.Get_TrxName());
                    list.Add(il);
                }
                ds = null;
            }
            catch (Exception e)
            {
                // log.Log(Level.SEVERE, "getServices", e);
            }

            MINT15RevenueService[] lines = new MINT15RevenueService[list.Count];
            lines = list.ToArray();
            return(lines);
        }
 public void SetRecognitionRun(MRevenueRecognition revenueRecognition, MINT15RevenueService revenueService, MRevenueRecognitionPlan revenueRecognitionPlan)
 {
     SetAD_Client_ID(revenueRecognitionPlan.GetAD_Client_ID());
     SetAD_Org_ID(revenueRecognitionPlan.GetAD_Org_ID());
     SetC_RevenueRecognition_Plan_ID(revenueRecognitionPlan.GetC_RevenueRecognition_Plan_ID());
     if (revenueService != null)
     {
         SetINT15_RevenueService_ID(revenueService.GetINT15_RevenueService_ID());
     }
 }
        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");
        }