• vishnukant pandey
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
global class MasterPaymentSchedules implements Database.Batchable<sobject>{

  
    global Database.querylocator start(Database.BatchableContext BC){
        String s = String.valueOf(system.today());
        
 
           String Query='select opportunity__c,IsDeleted, Date__c,Payment_Received_Date__c,Open_Principal__c,Total_Pay__c,InterestPmt__c,PrincipalPmt__c,PrincipalPmt_As_Per_Books__c,TDS__c,Net_Payment__c,Clearance__c,ClosingPrincipalBal_As_Per_Books__c,ClosingPrincipalBal__c,Balance_Days_for_NPA__c,Delinquent_Days__c from Payment_Schedules__c WHERE (CreatedDate = TODAY OR LastModifiedDate = TODAY)';
    //QueryResult qr = connection.queryAll(Query);
        return Database.getQueryLocator(Query);
    }
    
    global void execute(Database.BatchableContext BC,List<Payment_Schedules__c> paysch){
         List<Master_Payment_Schedules__c> LstNewMPS = new List<Master_Payment_Schedules__c>();
         set<id> SETOPP=NEW set<id>();
      set<id> setoppid=new set<id>();
        map<id,id> mapaccount=new map<id,id>();
        FOR(Payment_Schedules__c ps2 : paysch){
            Opportunity opp=[select id,accountid from opportunity where id=:ps2.Opportunity__c];
            setopp.add(opp.accountid);
            setoppid.add(opp.id);
            mapaccount.put(opp.id, opp.AccountId);
        }
         List<Master_Payment_Schedules__c> LstMPS = [select id,Open_Principal__c,Total_Pay__c,InterestPmt__c,Clearance__c ,
                                                    PrincipalPmt__c,PrincipalPmt_As_Per_Books__c,TDS__c,Net_Payment__c,ClosingPrincipalBal__c,
                                                    ClosingPrincipalBal_As_Per_Books__c,Date__c,Payment_Received_Date__c,Balance_Days_for_NPA__c,
                                                    Delinquent_Days__c from Master_Payment_Schedules__c  where Account__c in : setopp];
         system.debug('LstMPS ** '+LstMPS);
            if(LstMPS.Size() > 0){
                Delete LstMPS ;
                system.debug('MPS Record Delete** '+LstMPS);
            }
        List<TotalPS> LstTPS = New list<TotalPS>();
        
        for(id opp :setoppid){
           List<Payment_Schedules__c> LstPS = [select Date__c,Opportunity__c,Payment_Received_Date__c,Open_Principal__c,Total_Pay__c,InterestPmt__c,
                                                PrincipalPmt__c,PrincipalPmt_As_Per_Books__c,TDS__c,Net_Payment__c,Clearance__c,
                                                ClosingPrincipalBal_As_Per_Books__c,ClosingPrincipalBal__c,Balance_Days_for_NPA__c,Delinquent_Days__c 
                                                from Payment_Schedules__c  where Opportunity__c =: opp order by Date__c ASC];
            Integer psSize = LstPS.size();                                     
            
            //Checking for List of Opportunities on Paymemnt Schedules            
            for (Integer p = 0; p < psSize; p++) {
                Payment_Schedules__c ps = LstPS[p];
                TotalPS TPS = New TotalPS(); 
                TPS.opportunityid=ps.Opportunity__c;
                
                  Datetime testDate = ps.Date__c;
                  Date DT = Date.newInstance(testDate.year(), testDate.month(), testDate.day());
                  Integer DTA = (testDate.year())*100 + testDate.month();
                     
                  TPS.PSADate = DTA;
                TPS.PSDate = DT;
                TPS.PSPaymentReceivedDate=ps.Payment_Received_Date__c;  
                TPS.PSOpenPrincipal = ps.Open_Principal__c;
                TPS.PSTotalPay = ps.Total_Pay__c;
                TPS.PSInterestPmt = ps.InterestPmt__c;
                TPS.PSPrincipalPmt = ps.PrincipalPmt__c;
                TPS.PSPrincipalPmtAsPerBooks = ps.PrincipalPmt_As_Per_Books__c;
                TPS.PSTDS = ps.TDS__c;
                TPS.PSNetPayment = ps.Net_Payment__c;
                TPS.PSClosingPrincipalBalAsPerBooks = ps.ClosingPrincipalBal_As_Per_Books__c;
                TPS.PSClosingPrincipalBal = ps.ClosingPrincipalBal__c;
                TPS.PSClearance = ps.Clearance__c;
                TPS.PSDelinquentDays = ps.Delinquent_Days__c;
                TPS.PSBalanceDaysforNPA = ps.Balance_Days_for_NPA__c;
                
                system.debug('***TPS'+TPS.PSClosingPrincipalBal);
                system.debug('***TPS'+TPS.opportunityid);
                LstTPS.add(TPS);
                 system.debug('*****LstTPS'+LstTPS[p]);                     
                }
                
        }
         decimal closingbalance = 0;
                decimal openingbalance = 0;
                decimal interestpmt = 0;
                decimal pricipalpmt = 0;
                decimal pricipalbalasperbooks = 0;
                decimal closingbalasperbooks = 0;
                decimal TotalPay = 0;
                decimal NetPay = 0;
                decimal TDS = 0;
                decimal DelinquentDays=0;
                decimal BalanceDaysforNPA=0;
                Date dt;
                Date PRDate;
                Boolean clearence;
                            
               
                system.debug('*****LstTPS'+LstTPS);
        
                Integer LstTPSSize = LstTPS.Size();
        system.debug('***LstTPSSize'+LstTPSSize);
        for(Integer k = 0 ;  k< LstTPSSize ; k++ ){
            system.debug('opporunityid'+LstTPS[k].opportunityid);
            system.debug('***TPS'+LstTPS[k].PSClosingPrincipalBal);
        }
        for(payment_schedules__c ps1:paysch){
        for(Integer J = 0 ;  J< LstTPSSize ; J++ ){
            system.debug('*****LstTPS'+mapaccount.get(LstTPS[J].opportunityid));
            
            if(LstTPS[J].opportunityid==ps1.Opportunity__c){
                    
                        If (J == 0) {
                    
                        if (LstTPS[J].PSClosingPrincipalBal !=NULL) closingbalance = LstTPS[J].PSClosingPrincipalBal;
                        if (LstTPS[J].PSOpenPrincipal!=NULL) openingbalance =  LstTPS[J].PSOpenPrincipal;
                        if (LstTPS[J].PSInterestPmt !=NULL) interestpmt =  LstTPS[J].PSInterestPmt;
                        if (LstTPS[J].PSPrincipalPmt !=NULL) pricipalpmt =  LstTPS[J].PSPrincipalPmt;
                        if (LstTPS[J].PSPrincipalPmtAsPerBooks!=NULL) pricipalbalasperbooks =  LstTPS[J].PSPrincipalPmtAsPerBooks;
                        if (LstTPS[J].PSClosingPrincipalBalAsPerBooks!=NULL) closingbalasperbooks =  LstTPS[J].PSClosingPrincipalBalAsPerBooks;
                        if (LstTPS[J].PSTotalPay !=NULL) TotalPay =  LstTPS[J].PSTotalPay;
                        if (LstTPS[J].PSNetPayment!=NULL) NetPay =  LstTPS[J].PSNetPayment;
                        if (LstTPS[J].PSTDS !=NULL ) TDS =  LstTPS[J].PSTDS;
                        if (LstTPS[J].PSPaymentReceivedDate !=NULL) PRDate = LstTPS[J].PSPaymentReceivedDate ;
                        if (LstTPS[J].PSDate !=NULL) dt = LstTPS[J].PSDate ;
                        if (LstTPS[J].PSClearance !=NULL) clearence= LstTPS[J].PSClearance ;
                        if (LstTPS[J].PSDelinquentDays !=NULL) DelinquentDays= LstTPS[J].PSDelinquentDays ;
                        if (LstTPS[J].PSBalanceDaysforNPA !=NULL) BalanceDaysforNPA= LstTPS[J].PSBalanceDaysforNPA ;
                    }Else {
                        if(LstTPS[J].PSADate == LstTPS[J-1].PSADate ){
                                                              
                            if (LstTPS[J].PSClosingPrincipalBal !=NULL) closingbalance = closingbalance + LstTPS[J].PSClosingPrincipalBal;
                            if (LstTPS[J].PSOpenPrincipal!=NULL ) openingbalance = openingbalance + LstTPS[J].PSOpenPrincipal;
                            if (LstTPS[J].PSInterestPmt !=NULL)  interestpmt = interestpmt + LstTPS[J].PSInterestPmt;
                            if (LstTPS[J].PSPrincipalPmt !=NULL) pricipalpmt = pricipalpmt + LstTPS[J].PSPrincipalPmt;
                            if (LstTPS[J].PSPrincipalPmtAsPerBooks!=NULL) pricipalbalasperbooks = pricipalbalasperbooks  + LstTPS[J].PSPrincipalPmtAsPerBooks;
                            if (LstTPS[J].PSClosingPrincipalBalAsPerBooks!=NULL) closingbalasperbooks = closingbalasperbooks + LstTPS[J].PSClosingPrincipalBalAsPerBooks;
                            if (LstTPS[J].PSTotalPay !=NULL) TotalPay = TotalPay + LstTPS[J].PSTotalPay;
                            if (LstTPS[J].PSNetPayment!=NULL) NetPay = NetPay + LstTPS[J].PSNetPayment;
                            if (LstTPS[J].PSTDS !=NULL ) TDS =  TDS + LstTPS[J].PSTDS;
                            if (LstTPS[J].PSPaymentReceivedDate !=NULL) PRDate = LstTPS[J].PSPaymentReceivedDate;
                            if (LstTPS[J].PSDate !=NULL) dt = LstTPS[J].PSDate ;
                            // Checking True or False(checkbox) value of Clearance(Field) in PaymentSechduels and assign To MasterPaymentSchedules
                            if (LstTPS[J].PSClearance !=NULL)
                            if(LstTPS[J].PSClearance == False){
                              clearence= False ;
                              }
                             // Checking Minimum value of  Balance Days for NPA(Field) in PaymentSechduels and assign To MasterPaymentSchedules
                             if (LstTPS[J].PSBalanceDaysforNPA !=NULL)    
                             if (LstTPS[J].PSBalanceDaysforNPA < LstTPS[J-1].PSBalanceDaysforNPA ){
                              BalanceDaysforNPA = LstTPS[J].PSBalanceDaysforNPA ;
                              }
                           
                             // Checking Maximum value of DelinquentDays (Field) in PaymentSechduels and assign To MasterPaymentSchedules
                            if (LstTPS[J].PSDelinquentDays !=NULL) 
                            if(LstTPS[J].PSDelinquentDays > LstTPS[J-1].PSDelinquentDays ){
                            DelinquentDays = LstTPS[J].PSDelinquentDays ;
                           }
                           
                           
                        }else{ 
                          Master_Payment_Schedules__c MPS = new Master_Payment_Schedules__c();  
                            
                            MPS.ClosingPrincipalBal__c = closingbalance ; 
                            MPS.Open_Principal__c = openingbalance;
                            MPS.InterestPmt__c = interestpmt;
                            MPS.PrincipalPmt__c = pricipalpmt;
                            MPS.PrincipalPmt_As_Per_Books__c = pricipalbalasperbooks ;
                            MPS.ClosingPrincipalBal_As_Per_Books__c = closingbalasperbooks;
                            MPS.Total_Pay__c = TotalPay;
                            MPS.Net_Payment__c = NetPay;
                            MPS.TDS__c = TDS ;
                            MPS.Date__c = dt ;
                            MPS.Payment_Received_Date__c = PRDate;
                            MPS.Clearance__c = clearence;
                            MPS.Delinquent_Days__c = DelinquentDays;
                            MPS.Balance_Days_for_NPA__c = BalanceDaysforNPA;
                            
                            MPS.Account__c = mapaccount.get(ps1.Opportunity__c) ;  
                          
                            LstNewMPS.add(MPS); 
                            system.debug('***LstTPS'+LstTPS[J].opportunityid+'closingbal'+MPS.ClosingPrincipalBal__c);
            system.debug('****ps1'+ps1.Opportunity__c);
                                                      
                            if (LstTPS[J].PSClosingPrincipalBal !=NULL) closingbalance = LstTPS[J].PSClosingPrincipalBal;
                            if (LstTPS[J].PSOpenPrincipal!=NULL ) openingbalance =  LstTPS[J].PSOpenPrincipal;
                            if (LstTPS[J].PSInterestPmt !=NULL) interestpmt =  LstTPS[J].PSInterestPmt;
                            if (LstTPS[J].PSPrincipalPmt !=NULL) pricipalpmt =  LstTPS[J].PSPrincipalPmt;
                            if (LstTPS[J].PSPrincipalPmtAsPerBooks!=NULL) pricipalbalasperbooks =  LstTPS[J].PSPrincipalPmtAsPerBooks;
                            if (LstTPS[J].PSClosingPrincipalBalAsPerBooks!=NULL) closingbalasperbooks =  LstTPS[J].PSClosingPrincipalBalAsPerBooks;
                            if (LstTPS[J].PSTotalPay !=NULL) TotalPay =  LstTPS[J].PSTotalPay;
                            if (LstTPS[J].PSNetPayment!=NULL) NetPay =  LstTPS[J].PSNetPayment;
                            if (LstTPS[J].PSTDS !=NULL ) TDS =  LstTPS[J].PSTDS;
                            if (LstTPS[J].PSPaymentReceivedDate !=NULL) PRDate = LstTPS[J].PSPaymentReceivedDate ;
                            if (LstTPS[J].PSDate !=NULL) dt = LstTPS[J].PSDate ;
                            if (LstTPS[J].PSClearance !=NULL) clearence = LstTPS[J].PSClearance ;
                            if (LstTPS[J].PSDelinquentDays !=NULL) DelinquentDays = LstTPS[J].PSDelinquentDays ;
                            if (LstTPS[J].PSBalanceDaysforNPA !=NULL) BalanceDaysforNPA = LstTPS[J].PSBalanceDaysforNPA ;
                        
                        }  
                    }      
            }
        }
            Master_Payment_Schedules__c MPS = new Master_Payment_Schedules__c();  
                            
                            MPS.ClosingPrincipalBal__c = closingbalance ; 
                            MPS.Open_Principal__c = openingbalance;
                            MPS.InterestPmt__c = interestpmt;
                            MPS.PrincipalPmt__c = pricipalpmt;
                            MPS.PrincipalPmt_As_Per_Books__c = pricipalbalasperbooks ;
                            MPS.ClosingPrincipalBal_As_Per_Books__c = closingbalasperbooks;
                            MPS.Total_Pay__c = TotalPay;
                            MPS.Net_Payment__c = NetPay;
                            MPS.TDS__c = TDS ;
                            MPS.Date__c = dt ;
                            MPS.Payment_Received_Date__c = PRDate;
                            MPS.Clearance__c = clearence;
                            MPS.Delinquent_Days__c = DelinquentDays;
                            MPS.Balance_Days_for_NPA__c = BalanceDaysforNPA;
                            
                            MPS.Account__c = mapaccount.get(ps1.Opportunity__c) ;  
                          
                            LstNewMPS.add(MPS); 
            
        }
             Insert LstNewMPS;
                system.debug('MPS Records Inserted** '+LstNewMPS);
    }
     global void finish(Database.BatchableContext BC){

    }

}
global class MasterPaymentSchedules implements Database.Batchable<sobject>{

  
    global Database.querylocator start(Database.BatchableContext BC){
        String s = String.valueOf(system.today());
        
 
           String Query='select opportunity__c,IsDeleted, Date__c,Payment_Received_Date__c,Open_Principal__c,Total_Pay__c,InterestPmt__c,PrincipalPmt__c,PrincipalPmt_As_Per_Books__c,TDS__c,Net_Payment__c,Clearance__c,ClosingPrincipalBal_As_Per_Books__c,ClosingPrincipalBal__c,Balance_Days_for_NPA__c,Delinquent_Days__c from Payment_Schedules__c WHERE (CreatedDate = TODAY OR LastModifiedDate = TODAY)';
    //QueryResult qr = connection.queryAll(Query);
        return Database.getQueryLocator(Query);
    }
    
    global void execute(Database.BatchableContext BC,List<Payment_Schedules__c> paysch){
         List<Master_Payment_Schedules__c> LstNewMPS = new List<Master_Payment_Schedules__c>();
         set<id> SETOPP=NEW set<id>();
      set<id> setoppid=new set<id>();
        map<id,id> mapaccount=new map<id,id>();
        FOR(Payment_Schedules__c ps2 : paysch){
            Opportunity opp=[select id,accountid from opportunity where id=:ps2.Opportunity__c];
            setopp.add(opp.accountid);
            setoppid.add(opp.id);
            mapaccount.put(opp.id, opp.AccountId);
        }
         List<Master_Payment_Schedules__c> LstMPS = [select id,Open_Principal__c,Total_Pay__c,InterestPmt__c,Clearance__c ,
                                                    PrincipalPmt__c,PrincipalPmt_As_Per_Books__c,TDS__c,Net_Payment__c,ClosingPrincipalBal__c,
                                                    ClosingPrincipalBal_As_Per_Books__c,Date__c,Payment_Received_Date__c,Balance_Days_for_NPA__c,
                                                    Delinquent_Days__c from Master_Payment_Schedules__c  where Account__c in : setopp];
         system.debug('LstMPS ** '+LstMPS);
            if(LstMPS.Size() > 0){
                Delete LstMPS ;
                system.debug('MPS Record Delete** '+LstMPS);
            }
        List<TotalPS> LstTPS = New list<TotalPS>();
        
        for(id opp :setoppid){
           List<Payment_Schedules__c> LstPS = [select Date__c,Opportunity__c,Payment_Received_Date__c,Open_Principal__c,Total_Pay__c,InterestPmt__c,
                                                PrincipalPmt__c,PrincipalPmt_As_Per_Books__c,TDS__c,Net_Payment__c,Clearance__c,
                                                ClosingPrincipalBal_As_Per_Books__c,ClosingPrincipalBal__c,Balance_Days_for_NPA__c,Delinquent_Days__c 
                                                from Payment_Schedules__c  where Opportunity__c =: opp order by Date__c ASC];
            Integer psSize = LstPS.size();                                     
            
            //Checking for List of Opportunities on Paymemnt Schedules            
            for (Integer p = 0; p < psSize; p++) {
                Payment_Schedules__c ps = LstPS[p];
                TotalPS TPS = New TotalPS(); 
                TPS.opportunityid=ps.Opportunity__c;
                
                  Datetime testDate = ps.Date__c;
                  Date DT = Date.newInstance(testDate.year(), testDate.month(), testDate.day());
                  Integer DTA = (testDate.year())*100 + testDate.month();
                     
                  TPS.PSADate = DTA;
                TPS.PSDate = DT;
                TPS.PSPaymentReceivedDate=ps.Payment_Received_Date__c;  
                TPS.PSOpenPrincipal = ps.Open_Principal__c;
                TPS.PSTotalPay = ps.Total_Pay__c;
                TPS.PSInterestPmt = ps.InterestPmt__c;
                TPS.PSPrincipalPmt = ps.PrincipalPmt__c;
                TPS.PSPrincipalPmtAsPerBooks = ps.PrincipalPmt_As_Per_Books__c;
                TPS.PSTDS = ps.TDS__c;
                TPS.PSNetPayment = ps.Net_Payment__c;
                TPS.PSClosingPrincipalBalAsPerBooks = ps.ClosingPrincipalBal_As_Per_Books__c;
                TPS.PSClosingPrincipalBal = ps.ClosingPrincipalBal__c;
                TPS.PSClearance = ps.Clearance__c;
                TPS.PSDelinquentDays = ps.Delinquent_Days__c;
                TPS.PSBalanceDaysforNPA = ps.Balance_Days_for_NPA__c;
                
                system.debug('***TPS'+TPS.PSClosingPrincipalBal);
                system.debug('***TPS'+TPS.opportunityid);
                LstTPS.add(TPS);
                 system.debug('*****LstTPS'+LstTPS[p]);                     
                }
                
        }
         decimal closingbalance = 0;
                decimal openingbalance = 0;
                decimal interestpmt = 0;
                decimal pricipalpmt = 0;
                decimal pricipalbalasperbooks = 0;
                decimal closingbalasperbooks = 0;
                decimal TotalPay = 0;
                decimal NetPay = 0;
                decimal TDS = 0;
                decimal DelinquentDays=0;
                decimal BalanceDaysforNPA=0;
                Date dt;
                Date PRDate;
                Boolean clearence;
                            
               
                system.debug('*****LstTPS'+LstTPS);
        
                Integer LstTPSSize = LstTPS.Size();
        system.debug('***LstTPSSize'+LstTPSSize);
        for(Integer k = 0 ;  k< LstTPSSize ; k++ ){
            system.debug('opporunityid'+LstTPS[k].opportunityid);
            system.debug('***TPS'+LstTPS[k].PSClosingPrincipalBal);
        }
        for(payment_schedules__c ps1:paysch){
        for(Integer J = 0 ;  J< LstTPSSize ; J++ ){
            system.debug('*****LstTPS'+mapaccount.get(LstTPS[J].opportunityid));
            
            if(LstTPS[J].opportunityid==ps1.Opportunity__c){
                    
                        If (J == 0) {
                    
                        if (LstTPS[J].PSClosingPrincipalBal !=NULL) closingbalance = LstTPS[J].PSClosingPrincipalBal;
                        if (LstTPS[J].PSOpenPrincipal!=NULL) openingbalance =  LstTPS[J].PSOpenPrincipal;
                        if (LstTPS[J].PSInterestPmt !=NULL) interestpmt =  LstTPS[J].PSInterestPmt;
                        if (LstTPS[J].PSPrincipalPmt !=NULL) pricipalpmt =  LstTPS[J].PSPrincipalPmt;
                        if (LstTPS[J].PSPrincipalPmtAsPerBooks!=NULL) pricipalbalasperbooks =  LstTPS[J].PSPrincipalPmtAsPerBooks;
                        if (LstTPS[J].PSClosingPrincipalBalAsPerBooks!=NULL) closingbalasperbooks =  LstTPS[J].PSClosingPrincipalBalAsPerBooks;
                        if (LstTPS[J].PSTotalPay !=NULL) TotalPay =  LstTPS[J].PSTotalPay;
                        if (LstTPS[J].PSNetPayment!=NULL) NetPay =  LstTPS[J].PSNetPayment;
                        if (LstTPS[J].PSTDS !=NULL ) TDS =  LstTPS[J].PSTDS;
                        if (LstTPS[J].PSPaymentReceivedDate !=NULL) PRDate = LstTPS[J].PSPaymentReceivedDate ;
                        if (LstTPS[J].PSDate !=NULL) dt = LstTPS[J].PSDate ;
                        if (LstTPS[J].PSClearance !=NULL) clearence= LstTPS[J].PSClearance ;
                        if (LstTPS[J].PSDelinquentDays !=NULL) DelinquentDays= LstTPS[J].PSDelinquentDays ;
                        if (LstTPS[J].PSBalanceDaysforNPA !=NULL) BalanceDaysforNPA= LstTPS[J].PSBalanceDaysforNPA ;
                    }Else {
                        if(LstTPS[J].PSADate == LstTPS[J-1].PSADate ){
                                                              
                            if (LstTPS[J].PSClosingPrincipalBal !=NULL) closingbalance = closingbalance + LstTPS[J].PSClosingPrincipalBal;
                            if (LstTPS[J].PSOpenPrincipal!=NULL ) openingbalance = openingbalance + LstTPS[J].PSOpenPrincipal;
                            if (LstTPS[J].PSInterestPmt !=NULL)  interestpmt = interestpmt + LstTPS[J].PSInterestPmt;
                            if (LstTPS[J].PSPrincipalPmt !=NULL) pricipalpmt = pricipalpmt + LstTPS[J].PSPrincipalPmt;
                            if (LstTPS[J].PSPrincipalPmtAsPerBooks!=NULL) pricipalbalasperbooks = pricipalbalasperbooks  + LstTPS[J].PSPrincipalPmtAsPerBooks;
                            if (LstTPS[J].PSClosingPrincipalBalAsPerBooks!=NULL) closingbalasperbooks = closingbalasperbooks + LstTPS[J].PSClosingPrincipalBalAsPerBooks;
                            if (LstTPS[J].PSTotalPay !=NULL) TotalPay = TotalPay + LstTPS[J].PSTotalPay;
                            if (LstTPS[J].PSNetPayment!=NULL) NetPay = NetPay + LstTPS[J].PSNetPayment;
                            if (LstTPS[J].PSTDS !=NULL ) TDS =  TDS + LstTPS[J].PSTDS;
                            if (LstTPS[J].PSPaymentReceivedDate !=NULL) PRDate = LstTPS[J].PSPaymentReceivedDate;
                            if (LstTPS[J].PSDate !=NULL) dt = LstTPS[J].PSDate ;
                            // Checking True or False(checkbox) value of Clearance(Field) in PaymentSechduels and assign To MasterPaymentSchedules
                            if (LstTPS[J].PSClearance !=NULL)
                            if(LstTPS[J].PSClearance == False){
                              clearence= False ;
                              }
                             // Checking Minimum value of  Balance Days for NPA(Field) in PaymentSechduels and assign To MasterPaymentSchedules
                             if (LstTPS[J].PSBalanceDaysforNPA !=NULL)    
                             if (LstTPS[J].PSBalanceDaysforNPA < LstTPS[J-1].PSBalanceDaysforNPA ){
                              BalanceDaysforNPA = LstTPS[J].PSBalanceDaysforNPA ;
                              }
                           
                             // Checking Maximum value of DelinquentDays (Field) in PaymentSechduels and assign To MasterPaymentSchedules
                            if (LstTPS[J].PSDelinquentDays !=NULL) 
                            if(LstTPS[J].PSDelinquentDays > LstTPS[J-1].PSDelinquentDays ){
                            DelinquentDays = LstTPS[J].PSDelinquentDays ;
                           }
                           
                           
                        }else{ 
                          Master_Payment_Schedules__c MPS = new Master_Payment_Schedules__c();  
                            
                            MPS.ClosingPrincipalBal__c = closingbalance ; 
                            MPS.Open_Principal__c = openingbalance;
                            MPS.InterestPmt__c = interestpmt;
                            MPS.PrincipalPmt__c = pricipalpmt;
                            MPS.PrincipalPmt_As_Per_Books__c = pricipalbalasperbooks ;
                            MPS.ClosingPrincipalBal_As_Per_Books__c = closingbalasperbooks;
                            MPS.Total_Pay__c = TotalPay;
                            MPS.Net_Payment__c = NetPay;
                            MPS.TDS__c = TDS ;
                            MPS.Date__c = dt ;
                            MPS.Payment_Received_Date__c = PRDate;
                            MPS.Clearance__c = clearence;
                            MPS.Delinquent_Days__c = DelinquentDays;
                            MPS.Balance_Days_for_NPA__c = BalanceDaysforNPA;
                            
                            MPS.Account__c = mapaccount.get(ps1.Opportunity__c) ;  
                          
                            LstNewMPS.add(MPS); 
                            system.debug('***LstTPS'+LstTPS[J].opportunityid+'closingbal'+MPS.ClosingPrincipalBal__c);
            system.debug('****ps1'+ps1.Opportunity__c);
                                                      
                            if (LstTPS[J].PSClosingPrincipalBal !=NULL) closingbalance = LstTPS[J].PSClosingPrincipalBal;
                            if (LstTPS[J].PSOpenPrincipal!=NULL ) openingbalance =  LstTPS[J].PSOpenPrincipal;
                            if (LstTPS[J].PSInterestPmt !=NULL) interestpmt =  LstTPS[J].PSInterestPmt;
                            if (LstTPS[J].PSPrincipalPmt !=NULL) pricipalpmt =  LstTPS[J].PSPrincipalPmt;
                            if (LstTPS[J].PSPrincipalPmtAsPerBooks!=NULL) pricipalbalasperbooks =  LstTPS[J].PSPrincipalPmtAsPerBooks;
                            if (LstTPS[J].PSClosingPrincipalBalAsPerBooks!=NULL) closingbalasperbooks =  LstTPS[J].PSClosingPrincipalBalAsPerBooks;
                            if (LstTPS[J].PSTotalPay !=NULL) TotalPay =  LstTPS[J].PSTotalPay;
                            if (LstTPS[J].PSNetPayment!=NULL) NetPay =  LstTPS[J].PSNetPayment;
                            if (LstTPS[J].PSTDS !=NULL ) TDS =  LstTPS[J].PSTDS;
                            if (LstTPS[J].PSPaymentReceivedDate !=NULL) PRDate = LstTPS[J].PSPaymentReceivedDate ;
                            if (LstTPS[J].PSDate !=NULL) dt = LstTPS[J].PSDate ;
                            if (LstTPS[J].PSClearance !=NULL) clearence = LstTPS[J].PSClearance ;
                            if (LstTPS[J].PSDelinquentDays !=NULL) DelinquentDays = LstTPS[J].PSDelinquentDays ;
                            if (LstTPS[J].PSBalanceDaysforNPA !=NULL) BalanceDaysforNPA = LstTPS[J].PSBalanceDaysforNPA ;
                        
                        }  
                    }      
            }
        }
            Master_Payment_Schedules__c MPS = new Master_Payment_Schedules__c();  
                            
                            MPS.ClosingPrincipalBal__c = closingbalance ; 
                            MPS.Open_Principal__c = openingbalance;
                            MPS.InterestPmt__c = interestpmt;
                            MPS.PrincipalPmt__c = pricipalpmt;
                            MPS.PrincipalPmt_As_Per_Books__c = pricipalbalasperbooks ;
                            MPS.ClosingPrincipalBal_As_Per_Books__c = closingbalasperbooks;
                            MPS.Total_Pay__c = TotalPay;
                            MPS.Net_Payment__c = NetPay;
                            MPS.TDS__c = TDS ;
                            MPS.Date__c = dt ;
                            MPS.Payment_Received_Date__c = PRDate;
                            MPS.Clearance__c = clearence;
                            MPS.Delinquent_Days__c = DelinquentDays;
                            MPS.Balance_Days_for_NPA__c = BalanceDaysforNPA;
                            
                            MPS.Account__c = mapaccount.get(ps1.Opportunity__c) ;  
                          
                            LstNewMPS.add(MPS); 
            
        }
             Insert LstNewMPS;
                system.debug('MPS Records Inserted** '+LstNewMPS);
    }
     global void finish(Database.BatchableContext BC){

    }

}