binxie
2024-01-18 0e0dd1e20e7211f3c3c11d77a41090d998dfd06c
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
trigger NFM007 on Opportunity (before update, after update) {         // after insert を考慮しない
    
    //deloitte-zhj 20231124 本地化导入 start
    if((!Test.isRunningTest())&&System.Label.ByPassTrigger.contains(UserInfo.getUserId())){
        return;
    }
    //deloitte-zhj 20231124 本地化导入 end
    
    // SWAG-CE55BX 预测优化 start
    if (StaticParameter.EscapeOppandStaTrigger) {
        return;
    }
    // SWAG-CE55BX 预测优化 end
    if (StaticParameter.EscapeNFM007Trigger) {
System.debug('Escape、EscapeNFM007Trigger:::::' + StaticParameter.EscapeNFM007Trigger);
        return;
    }
    
    // 更新対象 Id をセット
    List<String> oppIds = new List<String>();
    Map<String, String> purposeOfAdviceMap = new Map<String, String>();    // 1:Delete 2:Add(Insert) 3:Change(Update)
//    if (Trigger.isInsert) {
//        if (NFM007Controller.isRunning == false) {
//            for(Opportunity opp : Trigger.new) {
//                if (opp.SAP_Send_OK__c == true) {
//                    NFM007Controller.isRunning = true;
//                    oppIds.add(opp.Id);
//                    purposeOfAdviceMap.put(opp.Id, '2');
//                    NFM007Controller.debug_msg = 'NFM007_callout_insert1';
//                }
//            }
//        }
//    }
//
//  else 
     if (Trigger.isUpdate) {
        if (NFM007Controller.isRunning == false) {
            if (Trigger.isBefore) {
                List<String> quoteIds = new List<String>();
                for(Opportunity opp : Trigger.new) {
                    // 古いデータの更新を禁止
                    Boolean noChange;
                    for (String colApiName : ControllerUtil.oppColumnList) {
                        colApiName = colApiName.trim();
                        if (opp.oldData_flg__c && Trigger.oldMap.get(opp.Id).get(colApiName) != Trigger.newMap.get(opp.Id).get(colApiName)) {
                            noChange = true;  break;
                        }
                    }
                    noChange = false;
                    if (noChange) {
                        opp.addError('不能修改旧数据');  continue;
                    }
                    // SAPWINはボタンで動くじゃなく、手動でチェック⇒保存だから、js無理
                    if (Trigger.oldMap.get(opp.Id).get('SAP_Send_OK__c') == false && opp.SAP_Send_OK__c == true) {
                       //SWAG-CG88AG【委托】提出价格申请是在协议有效期内,允许WIN fy start 
                       if(string.isNotBlank(opp.Estimation_No__c)){
                            Boolean judge = OpportunityWebService.judgeIsAssistantAppliedOutTime(opp.Estimation_No__c);
                            if(!judge){
                                opp.addError('经销商协议已过期,请重新做报价计算和合同申请');
                            }
                       }
                       //SWAG-CG88AG【委托】提出价格申请是在协议有效期内,允许WIN fy end 
                        //CHAN-BCNCRB XHL If_Have_EPT1__c
                        if (opp.If_Have_EPT1__c == true && opp.If_UploadT1Detailed__c == false) {
                            opp.addError('询价产品中包含EP-T1产品,但没上传T1清单,无法进行SAP上传(WIN),请上传T1清单');
                            continue;
                        }
                        //CHAN-BCNCRB XHL 
                        //20161118新增规则,产品中存在失效产品,不能进行sap上传
                        //lastbuy  2022/2/9 fy start
                        Map<String,QuoteLineItem> quotlinitMap = new Map<String,QuoteLineItem>();
                        //lastbuy  2022/2/9 fy end
                        List<QuoteLineItem> items = 
                        [Select 
                            Id,
                            SFDA_Status__c,
                            PricebookEntry.Product2Id
                            //lastbuy  2022/2/9 fy start
                            ,Asset_Model_No__c
                            //lastbuy  2022/2/9 fy end
                            // CHAN-BTF4XQ start
                            // 额外检索产品标准定价和产品成本
                            ,Product_Cost__c,Product_ListPrice__c,Quantity__c
                            // CHAN-BTF4XQ end
                        From 
                            QuoteLineItem 
                        where 
                            Quoteid = :opp.Estimation_Id__c ];
                            System.debug('opp.Estimation_Id__c+++'+opp.Estimation_Id__c);
                            System.debug('items+++'+items);
                        List<Id> ChechProId  = new List<Id>();
                        Integer CntSFDC = 0, CntCostOrListPrice = 0;
                        for(QuoteLineItem qli : items){
                            ChechProId.add(qli.PricebookEntry.Product2Id);
                            // CHAN-BTF4XQ start
                            //累积产品标准定价和成本是0的数据
                            if(qli.Product_Cost__c <= 0 || qli.Product_ListPrice__c<= 0){
                                CntCostOrListPrice++;
                            }
                            // CHAN-BTF4XQ end
                            //lastbuy  2022/2/9 fy start
                            if(qli.PricebookEntry.Product2Id!=null){
                                if(quotlinitMap.containsKey(qli.PricebookEntry.Product2Id)){
                                    QuoteLineItem quoteLine = quotlinitMap.get(qli.PricebookEntry.Product2Id);
                                    quoteLine.Quantity__c +=qli.Quantity__c;
                                    quotlinitMap.put(qli.PricebookEntry.Product2Id,quoteLine);
                                }else{
                                    quotlinitMap.put(qli.PricebookEntry.Product2Id,qli);
                                }
                            }
                            // quotlinitMap.put(qli.PricebookEntry.Product2Id,qli);
                            //lastbuy  2022/2/9 fy end
                        } 
                        if( CntCostOrListPrice > 0){
                            opp.addError('产品定价/成本为0,请联系产品本部。');
                        }
                        //CHAN-BWH2YW 20201221 you 加 字段 Correspond_Code__r start
                        //lastbuy  2022/2/9 fy
                        //新增预留产品字段LastbuyProductFLG__c
                        List<String> lastProductFLGIdList= new List<String>();
                        List<Product2> lastProductFLGList = new List<Product2>();
                        List<Product2> lastProductFLGListtest = new List<Product2>();
                        for (Product2 prd2: [Select Id,LastbuyProductFLG__c, Estimation_Entry_Possibility__c, SFDA_Status__c,Packing_list_manual__c,Correspond_Code__r.Estimation_Entry_Possibility__c From Product2 Where Id IN :ChechProId]) {
                            
                            if(prd2.Estimation_Entry_Possibility__c != '○'){ 
                                 //CHAN-BWH2YW 20201221 you 加 字段 Correspond_Code__r start
                                
                                if(String.isBlank(prd2.Correspond_Code__r.Estimation_Entry_Possibility__c) || (String.isNotBlank(prd2.Correspond_Code__r.Estimation_Entry_Possibility__c) && prd2.Correspond_Code__r.Estimation_Entry_Possibility__c != '○')){
                                    CntSFDC = CntSFDC+1;
                                } 
                                 //CHAN-BWH2YW 20201221 you 加 字段 Correspond_Code__r end 
                            }
                            lastProductFLGListtest.add(prd2);
                            //lastbuy  2022/2/9 fy start
                            if(prd2.LastbuyProductFLG__c){
                                lastProductFLGIdList.add(prd2.Id);
                                lastProductFLGList.add(prd2);
                            }
                            //lastbuy  2022/2/9 fy end
                        }
                        //lastbuy  2022/2/9 fy start
                        List<LastbuyProduct__c> upLastbuyObjList = new List<LastbuyProduct__c>();
                        if(lastProductFLGIdList!=null && lastProductFLGIdList.size() !=0){
                            List<LastbuyProduct__c> LastbuyObjList=[select id,LastbuyQuantity__c,InquiryCode__c,ProductName__c,effectiveFLG__c from LastbuyProduct__c where InquiryCode__c= : opp.Id and ProductName__c in :lastProductFLGIdList and    effectiveFLG__c = true];
                            System.debug('bbbbbbbbbbbbbbb==='+lastProductFLGIdList);
                            Map<string,LastbuyProduct__c> LastbuyObjMap = new Map<string,LastbuyProduct__c>();
                            if(LastbuyObjList!=null&&LastbuyObjList.size()!=0){
                                for(LastbuyProduct__c lastbuypr :LastbuyObjList){
                                  LastbuyObjMap.put(lastbuypr.ProductName__c,lastbuypr);
                                }
                                System.debug('fffffffffffffff==='+LastbuyObjList);
                                if(lastProductFLGList!=null && lastProductFLGList.size() !=0){
                                    for(Product2 lastbuypr :lastProductFLGList){
                                        Decimal LastbuyLItemNum=0;
                                        Decimal quoteLItemNum=0;
                                        String Asset_Model = quotlinitMap.get(lastbuypr.Id).Asset_Model_No__c;
                                        if(LastbuyObjMap.containsKey(lastbuypr.Id)){
                                            LastbuyProduct__c lastbuyobj = LastbuyObjMap.get(lastbuypr.Id);
                                            if(quotlinitMap.containsKey(lastbuypr.Id)){
                                                LastbuyLItemNum=LastbuyObjMap.get(lastbuypr.Id).LastbuyQuantity__c;
                                                quoteLItemNum=quotlinitMap.get(lastbuypr.Id).Quantity__c;
                                                if(quoteLItemNum>LastbuyLItemNum){
                                                    opp.addError(Asset_Model+'产品数量不可超过产品预留数量');
                                                }else{
                                                    lastbuyobj.ActualQuantity__c=quoteLItemNum;
                                                    lastbuyobj.effectiveFLG__c=false;
                                                    upLastbuyObjList.add(lastbuyobj);
                                                }
                                            }
                                        }else{
                                            opp.addError('预留产品'+Asset_Model+'未录入预留产品表');
                                            System.debug('asdasdfasdfadsf错误提示');
                                        }
                                        System.debug('cccccccccccccc==='+lastbuypr);
                                    }
                                    System.debug('aaaaaaaaaaaaaa==='+LastbuyObjList);
                                    
                                }
                            }else{
                                opp.addError('预留产品表中没有该询价,请通过本部窗口联系营业管理课');
                            }
                            
                        }
                        System.debug('upLastbuyObjList==='+upLastbuyObjList);
                        if(upLastbuyObjList!=null){
                            upsert upLastbuyObjList;           
                        }
                        
                        //lastbuy  2022/2/9 fy end
                        if(CntSFDC>0){
                            opp.addError('报价中存在失效产品,无法进行SAP上传(WIN),请更新报价');
                           
                        }else{
                            // Acecide
                            if (opp.Is_Corrosion__c) {
                                String str = OpportunityWebService.checkDangerItem(opp.agency1__c);
                                if (str != 'OK') {
                                   opp.addError(str); CntSFDC = CntSFDC+1;
                               } 
                            } else if (opp.Trade__c == '内貿' && opp.Sales_Root__c == '販売店') {
                                String str = OpportunityWebService.oppCheck(opp.id, '');
                                if (str != 'OK') {
                                   opp.addError(str); CntSFDC = CntSFDC+1;
                               } 
                            } else{
                                // CHAN-BHGBRD WIN操作报错的解决
                                //OpportunityWebService.updReg(opp.id);
                            }
                            opp.SAP_Send_OK_Date__c = System.now();
                            // 20150728 xudan 决定的报价のIdを持って、报价の印刷日を検索
                            if (String.isBlank(opp.Estimation_Id__c) == false && opp.Estimation_Decision__c == true) {
                                quoteIds.add(opp.Estimation_Id__c);
                            } 
                        }
                        //20200304 add WIN的配置单号与合同审批完成的报价是否一致,如果不一致,不可以做WIN start 
                        if (opp.SPOApprovedQuote__c != opp.Estimation_No__c && opp.If_Need_PriceApply__c==true && opp.Estimation_Id__c.substring(0, 3) != 'a2K') {
                            opp.addError('决定的报价与合同审批完成的报价不一致,无法进行SAP上传(WIN),请检查!');
                        }
                        //20200304 add WIN的配置单号与合同审批完成的报价是否一致,如果不一致,不可以做WIN end
                    }
                }
                
                if (quoteIds.size() > 0) {
                    Map<String, Quote> oppQuote = new Map<String, Quote>();
                    for (Quote q : [select OpportunityId, Quote_Print_Date__c from Quote where Id in :quoteIds]) {
                        oppQuote.put(q.OpportunityId, q);
                    }
                    // FIXMI
                    // 無効の製品の検索ロジックをNewQuoteEntryControllerと共通化したいですが、無理
                    // QuoteLineItemからProduct2Idを取得できない(SOQLエラー)
                    // なのに、数式からProduct2.xxxか可能です
                    Map<String, List<QuoteLineItem>> oppQuoteLineItem = new Map<String, List<QuoteLineItem>>();
                     //CHAN-BWH2YW 20210111 you 加 字段 Correspond_Code_Entry_Possibility__c
                    
                    for (QuoteLineItem qli : [select Correspond_Code_Entry_Possibility__c,QuoteId, Quote.OpportunityId, Product_Estimation_Entry_Possibility__c
                                              from QuoteLineItem where QuoteId in :quoteIds]) {
                        List<QuoteLineItem> tmpQli = new List<QuoteLineItem>();
                        if (oppQuoteLineItem.containsKey(qli.Quote.OpportunityId)) {tmpQli = oppQuoteLineItem.get(qli.Quote.OpportunityId); }
                       
                        // ×の製品をマップに入れる
                        // NewQuoteEntryController.dataCheck のチェックと一緒
                        //CHAN-BWH2YW 20210111 you 加 判断条件
                        if (qli.Product_Estimation_Entry_Possibility__c == '×'  && qli.Correspond_Code_Entry_Possibility__c == '×') {tmpQli.add(qli);}
                        oppQuoteLineItem.put(qli.Quote.OpportunityId, tmpQli);
                    }
                    
                    Set<String> winProfiles = new Set<String> (System.Label.WIN_Profile.split(','));
                    for (Opportunity opp : Trigger.new) {
                        if (Trigger.oldMap.get(opp.Id).get('SAP_Send_OK__c') == false && opp.SAP_Send_OK__c == true) {
                            
                            if (String.isBlank(opp.Estimation_Id__c) == false && opp.Estimation_Decision__c == true) {
                                // check印刷日
                                if (oppQuote.containsKey(opp.Id)) {
                                    /* 委托】【优先对应】询价SH-GI-SH0615989 WIN报错 去掉这一验证
                                    Date printDate = oppQuote.get(opp.Id).Quote_Print_Date__c;
                                    // 印刷日の3ヶ月以内
                                    if (Date.today().addMonths(-3) > printDate && winProfiles.contains(UserInfo.getProfileId()) == false) {
                                        opp.addError(System.Label.Error_Message_Cannot_Win);continue;
                                    }
                                    */
                                }
                                // check製品状態
                                if (oppQuoteLineItem.containsKey(opp.Id)) {
 
                                     List<QuoteLineItem> qliList = oppQuoteLineItem.get(opp.Id);
                                     // ×の製品があればエラー
                                     if (qliList.size()> 0) {opp.addError(System.Label.Error_Message_X_Product);continue;}
                                }
                            }
                        }
                    }
                }
                
            } else {
                for(Opportunity opp : Trigger.new) {
                    // NFM007の送信項目として、isChanged かを確認
                    if (Trigger.oldMap.get(opp.Id).get('SAP_Send_OK__c') == false && opp.SAP_Send_OK__c == true) {
 
                        NFM007Controller.isRunning = true;
                        
                        oppIds.add(opp.Id);
                        if (Trigger.oldMap.get(opp.Id).get('SAP_Send_OK_Date__c') == null && opp.SAP_Send_OK_Date__c != null) {
                            purposeOfAdviceMap.put(opp.Id, '2');
                           
                            NFM007Controller.debug_msg = 'NFM007_callout_insert2';
                        } else {
                            purposeOfAdviceMap.put(opp.Id, '3');
                            NFM007Controller.debug_msg = 'NFM007_callout_update';
                        }
                    }
                }
            }
        }
    }
    if (oppIds.size() > 0) {
        // MessageGroupNumber の採番
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = 'NFM007';
        iflog.Log__c  = 'callout start\n';
        insert iflog;
        iflog = [Select Id, Name from BatchIF_Log__c where Id = :iflog.Id];
        System.debug(Logginglevel.DEBUG, 'NFM007_' + iflog.Name + ' start');                  // callout の中 end のlogを出します
        
        // 20220921 ljh SWAG-CG2A7S update start
        // NFM007Controller.callout(iflog.Id, oppIds, purposeOfAdviceMap);
        String uid = UserInfo.getUserId();
        String iUid = System.Label.interfaceUserID;
        if (uid.substring(0,15) == iUid.substring(0,15)) {
            NFM007Controller.calloutNotfuture(iflog.Id, oppIds, purposeOfAdviceMap);
        }else{
            NFM007Controller.callout(iflog.Id, oppIds, purposeOfAdviceMap);
        }
        // 20220921 ljh SWAG-CG2A7S update end
        
    }
 
    if(System.Test.isRunningTest()){
        Integer i=0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
 
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
 
    }
}