View

Wednesday, May 1, 2013

Apex Cod e : Copy Quote Working .............



trigger CopyingQuoteValues on Quote (before update)
{
    //Cloning the Quote: START//
   

    for(Quote qt: Trigger.new)
    {
        //Check for Amendment Type "Product"
        if(qt.Is_Locked__c && qt.Amendment_Type__c != null && qt.Amendment_Type__c == 'Product' && qt.Amended_Lease__c != null)
        {
            mapQuoteProduct.put(qt.Id, qt);
            mapOpptyProduct.put(qt.Id, qt.OpportunityId);
            mapOrgProduct.put(qt.Id, qt.Amended_Lease__c);
   
        }
       
        //Check for Amendment Type "Lessee"    
        if(qt.Is_Locked__c && qt.Amendment_Type__c != null && qt.Amendment_Type__c == 'Lessee' && qt.Amended_Lease__c != null)
        {
            mapQuoteLessee.put(qt.Id, qt);
            mapOpptyLessee.put(qt.Id, qt.OpportunityId);
            mapOrgLessee.put(qt.Id, qt.Amended_Lease__c);
           
        }
       
        //Check for Amendment Type "Production"    
        if(qt.Is_Locked__c && qt.Amendment_Type__c != null && qt.Amendment_Type__c == 'Production' && qt.Amended_Lease__c != null)
        {
            mapQuoteProduction.put(qt.Id, qt);
            mapOpptyProduction.put(qt.Id, qt.OpportunityId);
            mapOrgProduction.put(qt.Id, qt.Amended_Lease__c);
             
        }      
    }

    Map<id, Quote> mAmdQ = new Map<Id, Quote>([SELECT id,
                                                      (SELECT id, Item__c, Quantity__c, Quote__c, Unit_Price__c, CurrencyIsoCode FROM Quote_Lines__r),
                                                      (SELECT Id, Name, CurrencyIsoCode, Quote__c, Annual_Lease_Payment__c, Annual_Lease_Payments_Year__c, Annual_SystemProduction_Guarantee_Year__c, Approximate_Price_per_kWh__c, Base_Monthy_Payment__c, Est_Monthly_Paymt_w_o_formula__c, Est_Mthly_Paymt_includingTax_w_auto_ACH__c, Est_Mthly_Paymt_with_formula__c, Estimated_Monthly_Payment_inclusive_of_T__c, Estimated_Tax_on_Monthly_Payment__c, Guarantee_Year__c, Guaranteed_Range_of_Annual_Production__c, Lease_Payments_Year__c, Monthly_Payments_and_with_automatic_ACH__c, PeGu_Estimate__c, Rate_Per_KWH__c, Record_Count__c, Remaining_Lease_Period__c, Remaining_Lease_Value__c, RoundOff_Base_Monthly_Payment__c, RoundOff_Rate_Per_KWH__c, RoundOff_Remaining_Lease_Value__c, System_Production_Annual_Production__c, Temp_Monthly_Lease_Payments__c, X105_of_S_P_A_P__c, X95_of_S_P_A_P__c, Final_Base_Monthly_Payment__c, Final_Estimated_Tax_on_Monthly__c, Final_Estimated_Monthly_W_Tax_No_ACH__c, Final_Estimated_Monthly_W_Tax_W_ACH__c, Final_Total_Yearly_Page_2__c, Final_Total_Yearly_Page_4__c, Final_Estimated_Monthly_W_Tax_No_ACH_p2__c, Tax_Rate__c, Final_Tax_Rate__c FROM Lease_Progress__r)
                                               FROM Quote
                                               WHERE id IN: (!mapQuoteProduct.isEmpty() ? mapQuoteProduct.keySet() : new Set<id>()) OR
                                                     id IN: (!mapQuoteLessee.isEmpty() ? mapQuoteLessee.keySet() : new Set<id>()) OR
                                                     id IN: (!mapQuoteProduction.isEmpty() ? mapQuoteProduction.keySet() : new Set<id>())]);
   

   
    List<Quote> lstArchievedQuote = new List<Quote>();
    List<Quote> lstCopyAmeInfoQuote = new List<Quote>();
   
    List<Quote_Line__c> lstQLDelete = new List<Quote_Line__c>();
    List<Quote_Line__c> lstQL = new List<Quote_Line__c>();
    List<Lease_Schedule__c> lstLP = new List<Lease_Schedule__c>();
   
       
    //*********Product***********
    if(!mapOrgProduct.isEmpty())
    {
        for(Quote qotsProduct: [SELECT id, name, opportunityId,
                                Module_Oracle_Item_Number__c, Quote_Inverter_Values__c,Module__c,
                                Module_Brand__c, Module_Quantity__c,
                                Module_Type__c, Monitoring_System_Model__c,
                                Monitoring_System_Option__c, Monitoring_System_Quantity__c,
                                Mounting_Option__c, Construction__c,
                                Dealer_Fees__c, Interconnect__c,
                                Inverter_Brand__c, Origination__c,
                                Inverter_Brand_2__c, Inverter_Brand_3__c,
                                Inverter_Brand_4__c, Inverter_Model__c,
                                Inverter_Model_2__c, Inverter_Model_3__c,
                                Inverter_Model_4__c, System_Size__c,
                                (SELECT id, Item__c, Quantity__c, Quote__c, Unit_Price__c, CurrencyIsoCode FROM Quote_Lines__r)                  
                                FROM Quote
                                WHERE id IN: mapOrgProduct.values()])
        {
                               
            for(Id amdProductId : mapOrgProduct.keySet())
            {
                if(mapOrgProduct.get(amdProductId) == qotsProduct.Id)
                {
                    Quote q = qotsProduct.clone(false, true, true, true);
                    q.OpportunityId = qotsProduct.OpportunityId;
                    lstArchievedQuote.add(q);
                   
                    qotsProduct.Module_Oracle_Item_Number__c = mapQuoteProduct.get(amdProductId).Module_Oracle_Item_Number__c;
                    qotsProduct.Module_Quantity__c = mapQuoteProduct.get(amdProductId).Module_Quantity__c;
                    qotsProduct.Quote_Inverter_Values__c = mapQuoteProduct.get(amdProductId).Quote_Inverter_Values__c;
                    qotsProduct.Module__c = mapQuoteProduct.get(amdProductId).Module__c;
                    qotsProduct.Module_Brand__c = mapQuoteProduct.get(amdProductId).Module_Brand__c;
                    qotsProduct.Module_Type__c = mapQuoteProduct.get(amdProductId).Module_Type__c;
                    qotsProduct.Monitoring_System_Model__c = mapQuoteProduct.get(amdProductId).Monitoring_System_Model__c;
                    qotsProduct.Monitoring_System_Option__c = mapQuoteProduct.get(amdProductId).Monitoring_System_Option__c;
                    qotsProduct.Monitoring_System_Quantity__c = mapQuoteProduct.get(amdProductId).Monitoring_System_Quantity__c;
                    qotsProduct.Mounting_Option__c = mapQuoteProduct.get(amdProductId).Mounting_Option__c;
                    qotsProduct.Inverter_Brand__c = mapQuoteProduct.get(amdProductId).Inverter_Brand__c;
                    qotsProduct.Inverter_Brand_2__c = mapQuoteProduct.get(amdProductId).Inverter_Brand_2__c;
                    qotsProduct.Inverter_Brand_3__c = mapQuoteProduct.get(amdProductId).Inverter_Brand_3__c;
                    qotsProduct.Inverter_Brand_4__c = mapQuoteProduct.get(amdProductId).Inverter_Brand_4__c;
                    qotsProduct.Inverter_Model__c = mapQuoteProduct.get(amdProductId).Inverter_Model__c;
                    qotsProduct.Inverter_Model_2__c = mapQuoteProduct.get(amdProductId).Inverter_Model_2__c;
                    qotsProduct.Inverter_Model_3__c = mapQuoteProduct.get(amdProductId).Inverter_Model_3__c;
                    qotsProduct.Inverter_Model_4__c = mapQuoteProduct.get(amdProductId).Inverter_Model_4__c;
                    qotsProduct.Inverter_Quantity__c = mapQuoteProduct.get(amdProductId).Inverter_Quantity__c;
                    qotsProduct.Inverter_Quantity_2__c = mapQuoteProduct.get(amdProductId).Inverter_Quantity_2__c;
                    qotsProduct.Inverter_Quantity_3__c = mapQuoteProduct.get(amdProductId).Inverter_Quantity_3__c;
                    qotsProduct.Inverter_Quantity_4__c = mapQuoteProduct.get(amdProductId).Inverter_Quantity_4__c;
                    qotsProduct.System_Cost__c = mapQuoteProduct.get(amdProductId).System_Cost__c;
                    qotsProduct.Dealer_Fees__c = mapQuoteProduct.get(amdProductId).Dealer_Fees__c;
                    qotsProduct.Origination__c = mapQuoteProduct.get(amdProductId).Origination__c;
                    qotsProduct.Construction__c = mapQuoteProduct.get(amdProductId).Construction__c;
                    qotsProduct.Interconnect__c = mapQuoteProduct.get(amdProductId).Interconnect__c;  
                   
                    lstCopyAmeInfoQuote.add(qotsProduct);

                    List<Quote_Line__c> tempQL = qotsProduct.Quote_Lines__r;
                 
                    if(!tempQL.isEmpty())
                    {
                        lstQLDelete.addAll(tempQL);
                        if(!mAmdQ.isEmpty() && mAmdQ.containsKey(amdProductId))
                        {
                            List<Quote_Line__c> tempAmdQL = mAmdQ.get(amdProductId).Quote_Lines__r;
                            if(!tempAmdQL.isEmpty())
                            {
                                for(Quote_Line__c ql: tempAmdQL)
                                {
                                    lstQL.add(new Quote_Line__c (Quote__c = qotsProduct.Id,
                                                                 Item__c = ql.Item__c,
                                                                 Quantity__c = ql.Quantity__c,
                                                                 Unit_Price__c = ql.Unit_Price__c,
                                                                 CurrencyIsoCode = ql.CurrencyIsoCode));
                                }
                            }
                        }
                       
                     }                  
                }//End If
            }//End For
        }//End For
    }
    //***** END PRODUCT *****//
   
    //******LESSEE Info****
    if(!mapOrgLessee.isEmpty())
    {
        for(Quote qotsLessee: [SELECT id, name, opportunityId,
                                 Module_Oracle_Item_Number__c, Quote_Inverter_Values__c,Module__c,
                                 Module_Brand__c, Module_Quantity__c,
                                 Module_Type__c, Monitoring_System_Model__c,
                                 Monitoring_System_Option__c, Monitoring_System_Quantity__c,
                                 Mounting_Option__c, Construction__c,
                                 Dealer_Fees__c, Interconnect__c,
                                 Inverter_Brand__c, Inverter_Brand_2__c,
                                 Inverter_Brand_3__c, Inverter_Brand_4__c,
                                 Inverter_Model__c, Inverter_Model_2__c,
                                 Inverter_Model_3__c, Inverter_Model_4__c,
                                 Origination__c, System_Size__c
                                 FROM Quote
                                 WHERE id IN: mapOrgLessee.values()])
        {
                               
            for(Id amdLesseeId : mapOrgLessee.keySet())
            {
                if(mapOrgLessee.get(amdLesseeId) == qotsLessee.Id)
                {
                    Quote q = qotsLessee.clone(false, true, true, true);
                    q.OpportunityId = qotsLessee.OpportunityId;
                    lstArchievedQuote.add(q);
                   
                    qotsLessee.Temp_Contact_1__c = mapQuoteLessee.get(amdLesseeId).Temp_Contact_1__c;
                    qotsLessee.Temp_Contact_2__c = mapQuoteLessee.get(amdLesseeId).Temp_Contact_2__c ;
                    qotsLessee.Lessee__c = mapQuoteLessee.get(amdLesseeId).Lessee__c;
                                       lstCopyAmeInfoQuote.add(qotsLessee);
                }
            }
        }
    }
    //***** END LESSEE *****//      
       
    //**************Production*************
    if(!mapOrgProduction.isEmpty())
    {
        for(Quote qotsProduction: [SELECT id, name, opportunityId,
                                 Module_Oracle_Item_Number__c, Quote_Inverter_Values__c,Module__c, Module_Brand__c,
                                 Module_Quantity__c, Module_Type__c, Monitoring_System_Model__c, Monitoring_System_Option__c,
                                 Monitoring_System_Quantity__c, Mounting_Option__c, Construction__c, Dealer_Fees__c, Interconnect__c,
                                 Inverter_Brand__c, Inverter_Brand_2__c, Inverter_Brand_3__c, Inverter_Brand_4__c,
                                 Inverter_Model__c, Inverter_Model_2__c, Inverter_Model_3__c, Inverter_Model_4__c, Origination__c, System_Size__c,
                                 (SELECT id, Item__c, Quantity__c, Quote__c, Unit_Price__c, CurrencyIsoCode FROM Quote_Lines__r),
                                 (SELECT Id, Name, CurrencyIsoCode, Quote__c, Annual_Lease_Payment__c, Annual_Lease_Payments_Year__c, Annual_SystemProduction_Guarantee_Year__c, Approximate_Price_per_kWh__c, Base_Monthy_Payment__c, Est_Monthly_Paymt_w_o_formula__c, Est_Mthly_Paymt_includingTax_w_auto_ACH__c, Est_Mthly_Paymt_with_formula__c, Estimated_Monthly_Payment_inclusive_of_T__c, Estimated_Tax_on_Monthly_Payment__c, Guarantee_Year__c, Guaranteed_Range_of_Annual_Production__c, Lease_Payments_Year__c, Monthly_Payments_and_with_automatic_ACH__c, PeGu_Estimate__c, Rate_Per_KWH__c, Record_Count__c, Remaining_Lease_Period__c, Remaining_Lease_Value__c, RoundOff_Base_Monthly_Payment__c, RoundOff_Rate_Per_KWH__c, RoundOff_Remaining_Lease_Value__c, System_Production_Annual_Production__c, Temp_Monthly_Lease_Payments__c, X105_of_S_P_A_P__c, X95_of_S_P_A_P__c, Final_Base_Monthly_Payment__c, Final_Estimated_Tax_on_Monthly__c, Final_Estimated_Monthly_W_Tax_No_ACH__c, Final_Estimated_Monthly_W_Tax_W_ACH__c, Final_Total_Yearly_Page_2__c, Final_Total_Yearly_Page_4__c, Final_Estimated_Monthly_W_Tax_No_ACH_p2__c, Tax_Rate__c, Final_Tax_Rate__c FROM Lease_Progress__r)
                                 FROM Quote
                                 WHERE id IN: mapOrgProduction.values()])
        {
                               
            for(Id amdProductionId : mapOrgProduction.keySet())
            {
                if(mapOrgProduction.get(amdProductionId) == qotsProduction.Id)
                {
                    Quote q = qotsProduction.clone(false, true, true, true);
                    q.OpportunityId = qotsProduction.OpportunityId;
                    lstArchievedQuote.add(q);
 
                }
               
     
                List<Lease_Schedule__c> tempLP = qotsProduction.Lease_Progress__r;
                //Prashant: 0309: Start              
                List<Lease_Schedule__c> tempAmeLP = mAmdQ.get(amdProductionId).Lease_Progress__r;
                if(!tempLP.isEmpty() && !tempAmeLP.isEmpty())
                {
                    for(Lease_Schedule__c ameLS: tempAmeLP)
                    {

                        for(Lease_Schedule__c orgLS: tempLP)
                        {
                            if(ameLS.Temp_Monthly_Lease_Payments__c== orgLS.Temp_Monthly_Lease_Payments__c)
                            {
                            orgLS.PeGu_Estimate__c = ameLS.PeGu_Estimate__c;
                            orgLS.Guaranteed_Range_of_Annual_Production__c = ameLS.Guaranteed_Range_of_Annual_Production__c;
                            orgLS.System_Production_Annual_Production__c = ameLS.System_Production_Annual_Production__c;
                             lstLP.add(orgLS);                              
                            }
                        }
                    }
                }
            }
        }
    }
   
    if(!lstArchievedQuote.isEmpty())
        insert lstArchievedQuote;

    if(!lstCopyAmeInfoQuote.isEmpty())
        update lstCopyAmeInfoQuote;
   
    if(!lstQL.isEmpty())
        insert lstQL;
   
    if(!lstLP.isEmpty())
        update lstLP;

    if(!lstQLDelete.isEmpty())
        delete lstQLDelete;

}//End Trigger CloneQuote

No comments:

Post a Comment

Share your Comments .............