liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//OLY订货
global class DealerConsumableManageBatch1 implements Database.Batchable<sObject> {
 public Date currentMonth;
    public Date endDate;
    public Date startDate;
    public String queryStr;
    public String year;
    public Boolean isIncrementOnly;
    global DealerConsumableManageBatch1(Date startDate,Date endDate,Boolean isIncrementOnly) {
        //当年
        this.startDate=startDate;
        this.endDate=endDate;
        this.isIncrementOnly=isIncrementOnly;
        //1.OCM_Sales_Forecast_F__c
        //2.
 
        queryStr = 'SELECT id,OCM_Sales_Forecast_F__c,Opportunity.OCM_man_province_no_dealer__c,Opportunity.Contract_recognized_day_1__c,Opportunity.Agency1__c';
        queryStr +=',Opportunity.Estimation_Id__c,Product2.OT_CODE_no_link__c,Opportunity.ID';
        queryStr +=',Opportunity.SPOApprovedNo__c,Product2.ConsumCategory2__c,Opportunity.Opportunity_Category__c';
        queryStr +=' FROM OpportunityLineItem ' ;
        queryStr +=' WHERE ( (Opportunity.StageName in (\'出荷\',\'完了\',\'注残\') ';
        queryStr +=' AND Product2.ENG_New__c=\'ENG2\' ';
 
        queryStr +=' AND ( (Opportunity.Estimation_Id__c like \'a15%\' AND Product2.OT_CODE_no_link__c!=\'\' ) OR ';
        queryStr +='  ( Opportunity.SPOApprovedNo__c!=\'\' ) ) ) ';
 
        queryStr +=' OR ( Opportunity.Opportunity_Category__c=\'ET\' AND  Opportunity.RecordType.Name in (\'引合\',\'SI询价\') ) )';
 
        queryStr +=' AND Opportunity.Contract_recognized_day_1__c>=:startDate AND Opportunity.Contract_recognized_day_1__c<=:endDate';
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        if(!isIncrementOnly){
            Integer year = startDate.year();
            Integer FYear = year;
            if(startDate>=Date.newInstance(year, 4, 1)){
                FYear = FYear+1;
            }
            String  OCSMYear= 'FY'+FYear;
            List<Dealer_ConsumableManage__c> dcmList= [SELECT ET_ENG__c,FY__c,Dealer_Name__c
                                                 ,FY_O_1__c,FY_O_2__c,FY_O_3__c,FY_O_4__c,FY_O_5__c,FY_O_6__c,FY_O_7__c,FY_O_8__c,FY_O_9__c,FY_O_10__c,FY_O_11__c,FY_O_12__c  
                                                 FROM Dealer_ConsumableManage__c WHERE FY__c = :OCSMYear];
            for(Dealer_ConsumableManage__c dcm:dcmList){
                dcm.FY_O_1__c=0;
                dcm.FY_O_2__c=0;
                dcm.FY_O_3__c=0;
                dcm.FY_O_4__c=0;
                dcm.FY_O_5__c=0;
                dcm.FY_O_6__c=0;
                dcm.FY_O_7__c=0;
                dcm.FY_O_8__c=0;
                dcm.FY_O_9__c=0;
                dcm.FY_O_10__c=0;
                dcm.FY_O_11__c=0;
                dcm.FY_O_12__c=0;
            }
            update dcmList;
        }
        
        return Database.getQueryLocator(queryStr);
    }
 
    global void execute(Database.BatchableContext BC, list<OpportunityLineItem> scope) {
        Integer year = startDate.year();
        Integer FYear = year;
        if(startDate>=Date.newInstance(year, 4, 1)){
            FYear = FYear+1;
        }
        String  OCSMYear= 'FY'+FYear;
        Map<String,List<OpportunityLineItem>> saMap = new Map<String,List<OpportunityLineItem>>();
        List<String> accIdList=new List<String>();
        List<String> oppIdList=new List<String>();
        for(OpportunityLineItem sa:scope){
            oppIdList.add(sa.Opportunity.Id);
        }
 
        List<Quote> quoteList=[SELECT Quote_Decision__c,OpportunityId from Quote where OpportunityId in :oppIdList and Quote_Decision__c='√'];
        Map<String,Quote> quoteMap=new Map<String,Quote>();
        for(Quote q:quoteList){
            if(!quoteMap.containsKey(q.OpportunityId)){
                quoteMap.put(q.OpportunityId,q);
            }
        }
 
        Set<String> dcmKeySet=new Set<String>();
        for(OpportunityLineItem sa:scope){
            String hpId=sa.Opportunity.Agency1__c;
            accIdList.add(sa.Opportunity.Agency1__c);
            String key=hpId;
            if(sa.Opportunity.Opportunity_Category__c=='ET'){
                key+='-ET';
                dcmKeySet.add(sa.Opportunity.Agency1__c+'-ET-'+OCSMYear+'-'+sa.Opportunity.OCM_man_province_no_dealer__c);
            }else{
                String eId=sa.Opportunity.Estimation_Id__c;
                if(eId!=null){
                    eId=eId.substring(0,3);
                }
                if( ( eId!='a15' || sa.Product2.OT_CODE_no_link__c=='') && (sa.Opportunity.SPOApprovedNo__c!='' )  ){
                    if(!quoteMap.containsKey(sa.Opportunity.Id)){
                        continue;
                    }
                }
                key+='-ENG';
                dcmKeySet.add(sa.Opportunity.Agency1__c+'-ENG-'+OCSMYear+'-'+sa.Opportunity.OCM_man_province_no_dealer__c);
            }
            key+='-'+OCSMYear+'-'+sa.Opportunity.OCM_man_province_no_dealer__c;
            if(saMap.containsKey(key)){
                saMap.get(key).add(sa);
            }else{
                List<OpportunityLineItem> arList = new List<OpportunityLineItem>();
                arList.add(sa);
                saMap.put(key,arList);
            }
        }
 
        List<Dealer_ConsumableManage__c> dcmList= [SELECT TMS_Key__c,TMS_Province__c,ET_ENG__c,FY__c,Dealer_Name__c
                                                 ,FY_O_1__c,FY_O_2__c,FY_O_3__c,FY_O_4__c,FY_O_5__c,FY_O_6__c,FY_O_7__c,FY_O_8__c,FY_O_9__c,FY_O_10__c,FY_O_11__c,FY_O_12__c  
                                                 FROM Dealer_ConsumableManage__c WHERE  TMS_Key__c in : dcmKeySet];
        for(Dealer_ConsumableManage__c dcm:dcmList){
            String key=dcm.TMS_Key__c;
            if(dcm.FY_O_1__c==null){
                dcm.FY_O_1__c=0;
            }
            if(dcm.FY_O_2__c==null){
                dcm.FY_O_2__c=0;
            }
            if(dcm.FY_O_3__c==null){
                dcm.FY_O_3__c=0;
            }
            if(dcm.FY_O_4__c==null){
                dcm.FY_O_4__c=0;
            }
            if(dcm.FY_O_5__c==null){
                dcm.FY_O_5__c=0;
            }
            if(dcm.FY_O_6__c==null){
                dcm.FY_O_6__c=0;
            }
            if(dcm.FY_O_7__c==null){
                dcm.FY_O_7__c=0;
            }
            if(dcm.FY_O_8__c==null){
                dcm.FY_O_8__c=0;
            }
            if(dcm.FY_O_9__c==null){
                dcm.FY_O_9__c=0;
            }
            if(dcm.FY_O_10__c==null){
                dcm.FY_O_10__c=0;
            }
            if(dcm.FY_O_11__c==null){
                dcm.FY_O_11__c=0;
            }
            if(dcm.FY_O_12__c==null){
                dcm.FY_O_12__c=0;
            }
            if(saMap.containsKey(key)){
                List<OpportunityLineItem> saList=saMap.get(key);
                for(OpportunityLineItem sa:saList){
 
                    if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear, 1, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear, 2, 1)){
                        dcm.FY_O_1__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear, 2, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear, 3, 1)){
                        dcm.FY_O_2__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear, 3, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear, 4, 1)){
                        dcm.FY_O_3__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 4, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 5, 1)){
                        dcm.FY_O_4__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 5, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 6, 1)){
                        dcm.FY_O_5__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 6, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 7, 1)){
                        dcm.FY_O_6__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 7, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 8, 1)){
                        dcm.FY_O_7__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 8, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 9, 1)){
                        dcm.FY_O_8__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 9, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 10, 1)){
                        dcm.FY_O_9__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 10, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 11, 1)){
                        dcm.FY_O_10__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 11, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 12, 1)){
                        dcm.FY_O_11__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 12, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear, 1, 1)){
                        dcm.FY_O_12__c+=sa.OCM_Sales_Forecast_F__c/1000;
                    }
                }
            }
            saMap.remove(key);
        }
 
 
        List<Dealer_ConsumableManage__c> newDCM=new List<Dealer_ConsumableManage__c>();
        for(String key:saMap.keySet()){
            List<OpportunityLineItem> saList=saMap.get(key);
 
            Dealer_ConsumableManage__c dcm=new Dealer_ConsumableManage__c();
            dcm.Dealer_Name__c = saList[0].Opportunity.Agency1__c;
            if(saList[0].Opportunity.Opportunity_Category__c=='ET'){
                dcm.ET_ENG__c='ET';
            }else{
                dcm.ET_ENG__c='ENG';
            }
            dcm.FY__c = OCSMYear;
            dcm.TMS_Key__c = dcm.Dealer_Name__c+'-'+dcm.ET_ENG__c+'-'+dcm.FY__c+'-'+saList[0].Opportunity.OCM_man_province_no_dealer__c;
            if(dcm.FY_O_1__c==null){
                dcm.FY_O_1__c=0;
            }
            if(dcm.FY_O_2__c==null){
                dcm.FY_O_2__c=0;
            }
            if(dcm.FY_O_3__c==null){
                dcm.FY_O_3__c=0;
            }
            if(dcm.FY_O_4__c==null){
                dcm.FY_O_4__c=0;
            }
            if(dcm.FY_O_5__c==null){
                dcm.FY_O_5__c=0;
            }
            if(dcm.FY_O_6__c==null){
                dcm.FY_O_6__c=0;
            }
            if(dcm.FY_O_7__c==null){
                dcm.FY_O_7__c=0;
            }
            if(dcm.FY_O_8__c==null){
                dcm.FY_O_8__c=0;
            }
            if(dcm.FY_O_9__c==null){
                dcm.FY_O_9__c=0;
            }
            if(dcm.FY_O_10__c==null){
                dcm.FY_O_10__c=0;
            }
            if(dcm.FY_O_11__c==null){
                dcm.FY_O_11__c=0;
            }
            if(dcm.FY_O_12__c==null){
                dcm.FY_O_12__c=0;
            }
            for(OpportunityLineItem sa:saList){
 
                if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear, 1, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear, 2, 1)){
                    dcm.FY_O_1__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear, 2, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear, 3, 1)){
                    dcm.FY_O_2__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear, 3, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear, 4, 1)){
                    dcm.FY_O_3__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 4, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 5, 1)){
                    dcm.FY_O_4__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 5, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 6, 1)){
                    dcm.FY_O_5__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 6, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 7, 1)){
                    dcm.FY_O_6__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 7, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 8, 1)){
                    dcm.FY_O_7__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 8, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 9, 1)){
                    dcm.FY_O_8__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 9, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 10, 1)){
                    dcm.FY_O_9__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 10, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 11, 1)){
                    dcm.FY_O_10__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 11, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear-1, 12, 1)){
                    dcm.FY_O_11__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }else if(sa.Opportunity.Contract_recognized_day_1__c >= Date.newInstance(FYear-1, 12, 1) && sa.Opportunity.Contract_recognized_day_1__c < Date.newInstance(FYear, 1, 1)){
                    dcm.FY_O_12__c+=sa.OCM_Sales_Forecast_F__c/1000;
                }
            }
            newDCM.add(dcm);
        }
        if(dcmList.size()>0){
            update dcmList;
        }
        if(newDCM.size()>0){
            insert newDCM;
        }
    }
 
    global void finish(Database.BatchableContext BC) {
        Database.executeBatch(new DealerConsumableManageBatch2(startDate,endDate,this.isIncrementOnly),200);
 
    }
}