protected override string DoIt()
        {
            MProfitLoss PL = new MProfitLoss(GetCtx(), GetRecord_ID(), null);

            prof = new MProfitLoss(GetCtx(), GetRecord_ID(), Get_Trx());

            stDate = Util.GetValueOfDateTime(DB.ExecuteScalar("select p.startdate from c_period p  inner join c_year y on p.c_year_id=y.c_year_id where p.periodno='1' and p.c_year_id= " + prof.GetC_Year_ID() + " and y.ad_client_id= " + GetAD_Client_ID(), null, null));
            eDate  = Util.GetValueOfDateTime(DB.ExecuteScalar("select p.enddate from c_period p  inner join c_year y on p.c_year_id=y.c_year_id where p.periodno='12' and p.c_year_id= " + prof.GetC_Year_ID() + " and y.ad_client_id= " + GetAD_Client_ID(), null, null));

            // lock record
            lock (lockRecord)
            {
                sql.Clear();
                sql.Append("SELECT distinct CP.* FROM C_ProfitLoss CP INNER JOIN Fact_Acct ft ON ft.C_AcctSchema_ID = Cp.C_AcctSchema_ID                             "
                           + " INNER JOIN c_elementvalue ev ON ft.account_id         =ev.c_elementvalue_id                                                           "
                           + " WHERE CP.ad_client_id    = " + GetAD_Client_ID());

                if (prof.Get_Value("PostingType") != null)
                {
                    sql.Append(" and CP.PostingType = '" + prof.Get_Value("PostingType") + "' ");
                }
                sql.Append(" AND (( " + GlobalVariable.TO_DATE(prof.GetDateFrom(), true) + " > = CP.DateFrom "
                           + " AND " + GlobalVariable.TO_DATE(prof.GetDateFrom(), true) + " < = CP.DateTo "
                           + " OR " + GlobalVariable.TO_DATE(prof.GetDateTo(), true) + " < = CP.DateFrom "
                           + " AND " + GlobalVariable.TO_DATE(prof.GetDateTo(), true) + " < = CP.DateTo ))  "
                           + " AND (ev.accounttype      ='E' OR ev.accounttype        ='R')     "
                           + " AND ev.isintermediatecode='N' AND CP.AD_Org_ID        IN (    (SELECT Ad_Org_ID   FROM AD_Org   WHERE isactive      = 'Y'             "
                           + " AND (legalentityorg =" + PL.GetAD_Org_ID() + "  OR Ad_Org_ID = " + PL.GetAD_Org_ID() + ")  )) AND DOCstatus in ('CO', 'CL') ");

                if (Util.GetValueOfInt(PL.Get_Value("C_AcctSchema_ID")) > 0)
                {
                    sql.Append(" AND Cp.C_AcctSchema_ID=" + Util.GetValueOfInt(PL.Get_Value("C_AcctSchema_ID")));
                }
                ds1 = DB.ExecuteDataset(sql.ToString(), null, Get_Trx());
                if (ds1 != null && ds1.Tables[0].Rows.Count > 0)
                {
                    return(Msg.GetMsg(GetCtx(), "VIS_RecordsAlreadyGenerated"));
                }

                // Get currentNext for generating key
                C_ProfitLossLines_ID  = MSequence.GetNextID(GetAD_Client_ID(), "C_ProfitLossLines", Get_Trx());
                C_ProfitLossLines_ID -= 1;

                // Delete All Record from line
                DB.ExecuteQuery("DELETE FROM C_ProfitLossLines WHERE C_ProfitLoss_ID=" + GetRecord_ID());

                insert.Clear();
                insert.Append(@"INSERT INTO C_ProfitLossLines (C_ProfitLossLines_ID , AD_Client_ID , AD_Org_ID , C_ProfitLoss_ID , C_ProfitAndLoss_ID , C_AcctSchema_ID , PostingType ,
                            AccountCredit ,AccountDebit,Account_ID, C_SubAcct_ID,  C_BPartner_ID , M_Product_ID , C_Project_ID , C_SalesRegion_ID ,  C_Campaign_ID , AD_OrgTrx_ID ,
                           C_LocFrom_ID , C_LocTo_ID , C_Activity_ID, User1_ID , User2_ID , UserElement1_ID, UserElement2_ID, UserElement3_ID, UserElement4_ID,
                          UserElement5_ID, UserElement6_ID, UserElement7_ID, UserElement8_ID, UserElement9_ID , GL_Budget_ID, C_ProjectPhase_ID, C_ProjectTask_ID, LedgerCode,LedgerName, Line ) ");

                qry.Clear();
                qry.Append(@"select " + C_ProfitLossLines_ID + " + rownum AS C_ProfitLossLines_id, ft.AD_Client_ID , ft.AD_Org_ID , " + PL.GetC_ProfitLoss_ID() + " , " + prof.GetC_ProfitAndLoss_ID() + ",  ft.C_AcctSchema_ID,ft.PostingType,ft.AmtAcctDr,ft.AmtAcctCr,ft.Account_ID,ft.C_SubAcct_ID,ft.C_BPartner_ID,ft.M_Product_ID,ft.C_Project_ID,ft.C_SalesRegion_ID,ft.C_Campaign_ID,ft.AD_OrgTrx_ID,ft.C_LocFrom_ID,ft.C_LocTo_ID,ft.C_Activity_ID,ft.User1_ID,ft.User2_ID,ft.UserElement1_ID,ft.UserElement2_ID,"
                           + " ft.UserElement3_ID,ft.UserElement4_ID, ft.UserElement5_ID, ft.UserElement6_ID, ft.UserElement7_ID,ft.UserElement8_ID, ft.UserElement9_ID,ft.GL_Budget_ID,ft.C_ProjectPhase_ID,ft.C_ProjectTask_ID,"
                           + @" ev.Value as LedgerCode,ev.Name as LedgerName , (SELECT NVL(MAX(Line),0) FROM C_ProfitLossLines   WHERE C_ProfitLoss_ID=" + PL.GetC_ProfitLoss_ID() + "   ) + (rownum *10) AS lineno from Fact_Acct ft inner join c_elementvalue ev on ft.account_id=ev.c_elementvalue_id where ft.ad_client_id= " + GetAD_Client_ID());


                // Added by SUkhwinder on 27 Nov 2017, for filtering query on the basis of postingtype. And string variable converted to stringBuilder also.
                if (prof.Get_Value("PostingType") != null)
                {
                    qry.Append(" and ft.PostingType = '" + prof.Get_Value("PostingType") + "'");
                }

                qry.Append(" and ft.DateAcct >=" + GlobalVariable.TO_DATE(prof.GetDateFrom(), true) + " AND ft.DateAcct <= " + GlobalVariable.TO_DATE(prof.GetDateTo(), true) + " AND (ev.accounttype='E' OR ev.accounttype='R') and ev.isintermediatecode='N'"
                           + "   AND ft.AD_Org_ID IN ( (SELECT Ad_Org_ID FROM AD_Org WHERE isactive = 'Y' AND (legalentityorg =" + PL.GetAD_Org_ID() + " OR Ad_Org_ID = " + PL.GetAD_Org_ID() + ")))");

                if (Util.GetValueOfInt(PL.Get_Value("C_AcctSchema_ID")) > 0)
                {
                    qry.Append(" AND ft.C_AcctSchema_ID=" + Util.GetValueOfInt(PL.Get_Value("C_AcctSchema_ID")));
                }
                insert.Append(qry.ToString());

                int no = Util.GetValueOfInt(DB.ExecuteQuery(insert.ToString(), null, Get_Trx()));
                if (no > 0)
                {
                    // Update curentNext in AD_Sequence
                    C_ProfitLossLines_ID += (no + 1);
                    String updateSQL = "UPDATE AD_Sequence SET  CurrentNext = " + C_ProfitLossLines_ID + ", CurrentNextSys = " + C_ProfitLossLines_ID + " "
                                       + " WHERE Upper(Name)=Upper('C_ProfitLossLines')"
                                       + " AND IsActive='Y' AND IsTableID='Y' AND IsAutoSequence='Y' ";
                    DB.ExecuteQuery(updateSQL, null, Get_Trx());

                    // Update Profit Before tax on Header
                    ProfitBeforeTax = Util.GetValueOfDecimal(DB.ExecuteScalar("SELECT SUM(AccountDebit) - SUM(AccountCredit) FROM C_ProfitLossLines WHERE  C_ProfitAndLoss_ID > 0 AND  IsActive='Y' AND C_ProfitLoss_ID=" + GetRecord_ID(), null, Get_Trx()));
                    prof.SetProfitBeforeTax(ProfitBeforeTax);
                    if (!prof.Save(Get_Trx()))
                    {
                        ds.Dispose();
                        Rollback();
                        // return GetReterivedError(prof, "ProfitNotSaved");
                        return(Msg.GetMsg(GetCtx(), "ProfitNotSaved"));
                    }
                    else
                    {
                        Get_Trx().Commit();

                        // insert record against Income summary acct
                        InsertProfitLossLine(prof);
                    }
                    return(Msg.GetMsg(GetCtx(), "LinesGenerated"));
                }
                else
                {
                    return(Msg.GetMsg(GetCtx(), "RecordNoFound"));
                }
            }
        }
Ejemplo n.º 2
0
        protected override string DoIt()
        {
            DB.ExecuteQuery("DELETE FROM C_ProfitLossLines WHERE C_ProfitLoss_ID=" + GetRecord_ID());
            prof = new MProfitLoss(GetCtx(), GetRecord_ID(), Get_TrxName());

            stDate = Util.GetValueOfDateTime(DB.ExecuteScalar("select p.startdate from c_period p  inner join c_year y on p.c_year_id=y.c_year_id where p.periodno='1' and p.c_year_id= " + prof.GetC_Year_ID() + " and y.ad_client_id= " + GetCtx().GetAD_Client_ID(), null, null));
            eDate  = Util.GetValueOfDateTime(DB.ExecuteScalar("select p.enddate from c_period p  inner join c_year y on p.c_year_id=y.c_year_id where p.periodno='12' and p.c_year_id= " + prof.GetC_Year_ID() + " and y.ad_client_id= " + GetCtx().GetAD_Client_ID(), null, null));

            qry = @"select ft.C_AcctSchema_ID,ft.PostingType,ft.AmtAcctDr,ft.AmtAcctCr,ft.Account_ID,ft.C_SubAcct_ID,ft.C_BPartner_ID,ft.M_Product_ID,ft.C_Project_ID,ft.C_SalesRegion_ID,ft.C_Campaign_ID,ft.AD_OrgTrx_ID,ft.C_LocFrom_ID,ft.C_LocTo_ID,ft.C_Activity_ID,ft.User1_ID,ft.User2_ID,ft.UserElement1_ID,ft.UserElement2_ID,ft.GL_Budget_ID,ft.C_ProjectPhase_ID,ft.C_ProjectTask_ID," +
                  "ev.Value as LedgerCode,ev.Name as LedgerName from fact_acct_balance ft inner join c_elementvalue ev on ft.account_id=ev.c_elementvalue_id where ft.ad_client_id=" + GetAD_Client_ID() + " and ft.DateAcct >=" + GlobalVariable.TO_DATE(stDate, true) + " AND ft.DateAcct <= " + GlobalVariable.TO_DATE(eDate, true) + " AND (ev.accounttype='E' OR ev.accounttype='R') and ev.isintermediatecode='N'";
            ds = DB.ExecuteDataset(qry, null, Get_TrxName());
            if (ds != null)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        pLine = new MProfitLossLines(GetCtx(), 0, Get_TrxName());
                        pLine.SetAD_Client_ID(GetAD_Client_ID());
                        pLine.SetAD_Org_ID(GetCtx().GetAD_Org_ID());
                        pLine.SetC_ProfitLoss_ID(GetRecord_ID());
                        pLine.SetC_ProfitAndLoss_ID(prof.GetC_ProfitAndLoss_ID());
                        pLine.SetC_AcctSchema_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["C_AcctSchema_ID"]));
                        pLine.SetPostingType(Util.GetValueOfString(ds.Tables[0].Rows[i]["PostingType"]));
                        pLine.SetLedgerCode(Util.GetValueOfString(ds.Tables[0].Rows[i]["LedgerCode"]));
                        pLine.SetLedgerName(Util.GetValueOfString(ds.Tables[0].Rows[i]["LedgerName"]));
                        if (Util.GetValueOfDecimal(ds.Tables[0].Rows[i]["AmtAcctDr"]) > 0)
                        {
                            pLine.SetAccountCredit(Util.GetValueOfDecimal(ds.Tables[0].Rows[i]["AmtAcctDr"]));
                        }
                        if (Util.GetValueOfDecimal(ds.Tables[0].Rows[i]["AmtAcctCr"]) > 0)
                        {
                            pLine.SetAccountDebit(Util.GetValueOfDecimal(ds.Tables[0].Rows[i]["AmtAcctCr"]));
                        }
                        pLine.SetC_SubAcct_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["C_SubAcct_ID"]));
                        pLine.SetAccount_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["Account_ID"]));
                        pLine.SetC_BPartner_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["C_BPartner_ID"]));
                        pLine.SetM_Product_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["M_Product_ID"]));
                        pLine.SetC_Project_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["C_Project_ID"]));
                        pLine.SetC_SalesRegion_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["C_SalesRegion_ID"]));
                        pLine.SetC_Campaign_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["C_Campaign_ID"]));
                        pLine.SetAD_OrgTrx_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["AD_OrgTrx_ID"]));
                        pLine.SetC_LocFrom_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["C_LocFrom_ID"]));
                        pLine.SetC_LocTo_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["C_LocTo_ID"]));
                        pLine.SetC_Activity_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["C_Activity_ID"]));
                        pLine.SetUser1_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["User1_ID"]));
                        pLine.SetUser2_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["User2_ID"]));
                        pLine.SetUserElement1_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["UserElement1_ID"]));
                        pLine.SetUserElement2_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["UserElement2_ID"]));
                        pLine.SetGL_Budget_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["GL_Budget_ID"]));
                        pLine.SetC_ProjectPhase_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["C_ProjectPhase_ID"]));
                        pLine.SetC_ProjectTask_ID(Util.GetValueOfInt(ds.Tables[0].Rows[i]["C_ProjectTask_ID"]));
                        int lineno = Util.GetValueOfInt(DB.ExecuteScalar("SELECT NVL(MAX(Line),0)+10 FROM C_ProfitLossLines WHERE C_ProfitLoss_ID=" + GetRecord_ID(), null, Get_TrxName()));
                        pLine.SetLine(lineno);
                        if (!pLine.Save())
                        {
                            ds.Dispose();
                            return(Msg.GetMsg(GetCtx(), "ProfitLinesNotSaved"));
                        }
                    }

                    expense = Util.GetValueOfDecimal(DB.ExecuteScalar("select SUM(ft.AmtAcctCr+ft.AmtAcctDr) from fact_acct_balance ft inner join c_elementvalue ev on ft.account_id=ev.c_elementvalue_id where ft.ad_client_id=" + GetAD_Client_ID() + " and ft.DateAcct >=" + GlobalVariable.TO_DATE(stDate, true) + " AND ft.DateAcct <= " + GlobalVariable.TO_DATE(eDate, true) + " AND ev.accounttype='E' and ev.isintermediatecode='N'"));
                    revenue = Util.GetValueOfDecimal(DB.ExecuteScalar("select SUM(ft.AmtAcctCr+ft.AmtAcctDr) from fact_acct_balance ft inner join c_elementvalue ev on ft.account_id=ev.c_elementvalue_id where ft.ad_client_id=" + GetAD_Client_ID() + " and ft.DateAcct >=" + GlobalVariable.TO_DATE(stDate, true) + " AND ft.DateAcct <= " + GlobalVariable.TO_DATE(eDate, true) + " AND ev.accounttype='R' and ev.isintermediatecode='N'"));
                    prof.SetProfitBeforeTax(revenue - expense);
                    if (!prof.Save())
                    {
                        ds.Dispose();
                        Rollback();
                        return(Msg.GetMsg(GetCtx(), "ProfitNotSaved"));
                    }
                    ds.Dispose();
                    return(Msg.GetMsg(GetCtx(), "LinesGenerated"));
                }
            }
            return(Msg.GetMsg(GetCtx(), "RecordNoFound"));
        }