高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// 只使用一次,用来更新询价行项目和报价行项目的经销商折扣
 
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;
    }
}