// 只使用一次,用来更新询价行项目和报价行项目的经销商折扣 public with sharing class UpdateOppLineItemAgencySubtotalHandler extends Oly_TriggerHandler { private Map newMap; private Map oldMap; private List newList; private List oldList; public UpdateOppLineItemAgencySubtotalHandler() { this.newMap = (Map) Trigger.newMap; this.oldMap = (Map) Trigger.oldMap; this.newList = (List) Trigger.new; this.oldList = (List) Trigger.old; } protected override void afterUpdate() { updateOpportunityLineItem(); } private void updateOpportunityLineItem() { list unhandleQuotelist = [select id, OpportunityId, opportunity.Sales_Root__c, Estimation_List_Price__c,Dealer_Final_Price__c, OCM_Agent1_Price__c from Quote where id in: newList and AgencyDiscount__c = null ]; List oppIDlist = new List (); for (Quote temQuote : unhandleQuotelist) { oppIDlist.add(temQuote.OpportunityId); } //Estimation_List_Price__c 标准定价; OCM_Agent1_Price__c 第一经销商价格; Dealer_Final_Price__c ;医院的合同金额 list unhandleQuoteLineItemlist = [select id, Quoteid, Quantity__c, ListPrice__c from QuoteLineItem where Quoteid in: unhandleQuotelist]; list unhandleOpportunityLineItemlist = [select id, Opportunityid, Quantity__c, ListPrice__c from OpportunityLineItem where Opportunityid in: oppIDlist]; list updateQuoteList = new list(); list updateOppList = new list(); for (Quote unhandleQuote : unhandleQuotelist) { list currentOliList = new list(); list currentQliList = new list(); for ( QuoteLineItem temqli : unhandleQuoteLineItemlist) { if ( temqli.Quoteid == unhandleQuote.id) { currentQliList.add(temqli); } } for ( OpportunityLineItem temoli : unhandleOpportunityLineItemlist) { if ( temoli.Opportunityid == unhandleQuote.Opportunityid) { currentOliList.add(temoli); } } decimal TotalPrice = 0; decimal TotalPrice_Copy = 0 ; if ( unhandleQuote.opportunity.Sales_Root__c.equals('販売店')) { TotalPrice = unhandleQuote.OCM_Agent1_Price__c; TotalPrice_Copy = unhandleQuote.OCM_Agent1_Price__c; unhandleQuote.AgencyDiscount__c = (TotalPrice / unhandleQuote.Estimation_List_Price__c * 100).setscale(2); updateQuoteList.add(unhandleQuote); Opportunity temOpp = new Opportunity(id = unhandleQuote.opportunityid); temOpp.AgencyDiscount__c = unhandleQuote.AgencyDiscount__c; updateOppList.add(temOpp); } else if ( unhandleQuote.opportunity.Sales_Root__c.equals('OCM直接販売')) { TotalPrice = unhandleQuote.Dealer_Final_Price__c; TotalPrice_Copy = unhandleQuote.Dealer_Final_Price__c; } decimal totalAgencyPrice = TotalPrice; QuoteLineItem lastQuoteLineItem = new QuoteLineItem(); if (TotalPrice != 0) { for ( QuoteLineItem temqli : currentQliList) { decimal stardardprice = temqli.Quantity__c * temqli.ListPrice__c; decimal Agency_UnitPrice = StardardPrice * TotalPrice / (unhandleQuote.Estimation_List_Price__c * temqli.Quantity__c); Agency_UnitPrice = Agency_UnitPrice.setscale(1); decimal AgencySubtotal = Agency_UnitPrice * temqli.Quantity__c; totalAgencyPrice = totalAgencyPrice - AgencySubtotal; temqli.AgencySubtotal__c = AgencySubtotal; temqli.AgencyUnitPrice__c = Agency_UnitPrice; lastQuoteLineItem = temqli; } if (lastQuoteLineItem != null && lastQuoteLineItem.AgencySubtotal__c != null && totalAgencyPrice != null) { lastQuoteLineItem.AgencySubtotal__c += totalAgencyPrice; lastQuoteLineItem.AgencyUnitPrice__c = (lastQuoteLineItem.AgencySubtotal__c / lastQuoteLineItem.Quantity__c).setscale(1); } OpportunityLineItem lastOpportunityLineItem = new OpportunityLineItem(); totalAgencyPrice = TotalPrice; for ( OpportunityLineItem temoli : currentOliList) { decimal stardardprice = temoli.Quantity__c * temoli.ListPrice__c; decimal Agency_UnitPrice = StardardPrice * TotalPrice_Copy / (unhandleQuote.Estimation_List_Price__c * temoli.Quantity__c); Agency_UnitPrice = Agency_UnitPrice.setscale(1); decimal AgencySubtotal = Agency_UnitPrice * temoli.Quantity__c; totalAgencyPrice = totalAgencyPrice - AgencySubtotal; temoli.AgencySubtotal__c = AgencySubtotal; temoli.AgencyUnitPrice__c = Agency_UnitPrice; lastOpportunityLineItem = temoli; } if (lastOpportunityLineItem != null && lastOpportunityLineItem.AgencySubtotal__c != null && totalAgencyPrice != null) { lastOpportunityLineItem.AgencySubtotal__c += totalAgencyPrice; lastOpportunityLineItem.AgencyUnitPrice__c = (lastOpportunityLineItem.AgencySubtotal__c / lastOpportunityLineItem.Quantity__c).setscale(1); } } } if (updateQuoteList.size() > 0) update updateQuoteList; if (updateOppList.size() > 0) update updateOppList; if (unhandleQuoteLineItemlist.size() > 0) update unhandleQuoteLineItemlist; if (unhandleOpportunityLineItemlist.size() > 0) update unhandleOpportunityLineItemlist; } }