// 只使用一次，用来更新询价行项目和报价行项目的经销商折扣

public with sharing class UpdateOppLineItemAgencySubtotalHandler extends Oly_TriggerHandler {

	private Map<Id, Quote> newMap;
	private Map<Id, Quote> oldMap;
	private List<Quote> newList;
	private List<Quote> oldList;
	public UpdateOppLineItemAgencySubtotalHandler() {
		this.newMap = (Map<Id, Quote>) Trigger.newMap;
		this.oldMap = (Map<Id, Quote>) Trigger.oldMap;
		this.newList = (List<Quote>) Trigger.new;
		this.oldList = (List<Quote>) Trigger.old;

	}
	protected override void afterUpdate() {
		updateOpportunityLineItem();
	}

	private void updateOpportunityLineItem() {

		list<Quote> 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<ID> oppIDlist = new List<ID> ();
		for (Quote temQuote : unhandleQuotelist) {
			oppIDlist.add(temQuote.OpportunityId);
		}

//Estimation_List_Price__c 标准定价; OCM_Agent1_Price__c 第一经销商价格； Dealer_Final_Price__c ;医院的合同金额

		list<QuoteLineItem> unhandleQuoteLineItemlist =
		    [select id, Quoteid, Quantity__c, ListPrice__c
		     from QuoteLineItem
		     where Quoteid in: unhandleQuotelist];

		list<OpportunityLineItem> unhandleOpportunityLineItemlist =
		    [select id, Opportunityid, Quantity__c, ListPrice__c
		     from OpportunityLineItem
		     where Opportunityid in: oppIDlist];
		list<Quote> updateQuoteList =  new list<Quote>();
		list<Opportunity> updateOppList =  new list<Opportunity>();

		for (Quote unhandleQuote : unhandleQuotelist) {
			list<OpportunityLineItem> currentOliList = new list<OpportunityLineItem>();
			list<QuoteLineItem> currentQliList = new list<QuoteLineItem>();
			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;
	}
}