高章伟
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
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
global class SendConsumableordertosap {
 
    WebService static String sendSAP(String orderId) {
        StaticParameter.EscapeConsumableOrderDetail2Trigger = true;
        List<Consumable_order__c> ord = [
            SELECT
                    Id,
                    Name,
                    Dealer_Info__c,
                    Offers_Price__c,
                    CurrencyIsoCode,
                    Contract_application_decision__c,
                    OwnerId,
                    Order_date__c,
                    SalesManager__c,
                    Total_amount__c,
                    SalesManager__r.Province_Text__c,
                    Order_effective_contact__c,
                    Order_ProType__c,
                    IS_Price_Apply__c // 2018/11/05 CHAN-B686DZ  判断是否价格申请
            FROM
                    Consumable_order__c
            WHERE
                    Id = : orderId
            FOR UPDATE
        ];
        if(null == ord || ord.size() == 0) {
            return '没有消耗品订单:' + orderId + '的数据。';
        }
        Map<Id,String> productCampaign = new Map<Id,String>();
        List<Consumable_Orderdetails__c> odrDetails = [
            SELECT
                    Id,
                    Name,
                    Consumable_product__c,
                    Consumable_product__r.Product2__c,
                    Consumable_product__r.Name__c,
                    Consumable_product__r.Estimation_Entry_Possibility__c,
                    Consumable_product__r.SFDA_Status__c,
                    Consumable_product__r.Intra_Trade_List_RMB__c,
                    Consumable_product__r.Product2__r.Important_product__c,
                    Consumable_count__c,
                    Sum_of_money__c,
                    Intra_Trade_List_RMB__c,
                    Special_Campaign_Price__c
            FROM
                    Consumable_Orderdetails__c
            WHERE
                    Consumable_order__c =: orderId
                    ORDER BY Name
        ];
 
        if(null == odrDetails || odrDetails.size() == 0) {
            return '消耗品订单:' + orderId + '没有明细数据。';
        }
        for(Consumable_Orderdetails__c det1 : odrDetails){
            if(det1.Special_Campaign_Price__c != null){
                productCampaign.put(det1.Consumable_product__c, det1.Consumable_product__r.Name__c);
            }
        }
 
        List<Account> ordContract =[
            SELECT
                    Id,
                    Name,
                    Contract_Decide_Start_Date__c,
                    Contract_Decide_End_Date__c,
                    Agent_Ref__c,
                    RecordtypeId
            FROM
                    Account
            WHERE
                    Contract_Decide_Start_Date__c <= :ord[0].Order_date__c AND
                    Contract_Decide_End_Date__c >= :ord[0].Order_date__c AND
                    Agent_Ref__c = :ord[0].Dealer_Info__c AND
                    RecordtypeId = :System.Label.Consumable_Contract AND
                    Id = :ord[0].Order_effective_contact__c
            Order by  Contract_Decide_Start_Date__c desc
 
        ];
        if(null == ordContract || ordContract.size() == 0){
            return '经销商:' + ord[0].Dealer_Info__c + '没有最新合同。';
        }
        Map<Id,Decimal> productdataMap = new Map<Id,Decimal>();
        //已存在的产品有效促销信息
        List<Dealer_Product__c> dealerProductList = [select Id, Dealer_Product2__c,Dealer_Contact__c,
                                                        Special_Campaign_Price__c,Campaign_StartDate__c,
                                                        Campaign_EndDate__c
                                                    FROM Dealer_Product__c
                                                    WHERE Dealer_Contact__c = :ordContract[0].Id
                                                    AND Campaign_StartDate__c <= :Date.today()
                                                    AND Campaign_EndDate__c >= :Date.today()];
        for(Dealer_Product__c dp :dealerProductList ){
            productdataMap.put(dp.Dealer_Product2__c, dp.Special_Campaign_Price__c);
        }
        for(Id widget : productCampaign.keySet()) {
            if(productdataMap.containsKey(widget)){
                continue;
            }else{
                return '订单产品:' + productCampaign.get(widget) + '促销已经无效。';
            }
        }
 
        List<Id> product2Ids = new List<Id>();
        List<String> product2EstimationIds = new List<String>();
        for(Consumable_Orderdetails__c detail : odrDetails) {
            product2Ids.add(detail.Consumable_product__r.Product2__c);
            if(detail.Consumable_product__r.Estimation_Entry_Possibility__c != '○'){
                product2EstimationIds.add(detail.Consumable_product__r.Name__c);
            }
        }
        if(product2EstimationIds.size() > 0){
            return '产品:' + product2EstimationIds + '无效,无法执行WIN操作。';
        }
 
        //String strRet = OpportunityWebService.checkProRegisterDecide(productCampaign, ord[0].Dealer_Info__c, '');
        //if(strRet != 'OK'){
        //    return strRet;
        //}
 
 
        // GZW 提交产品无效 出错误消息
        Map<String,String> chkMap = OpportunityWebService.MapCheckProRegisterDecide(productCampaign, ord[0].Dealer_Info__c, '');
        if(chkMap.size() > 0){
            if(chkMap.containsKey('agency')){
                return '第一经销商没有有效的医疗器械经营许可证。';
            }
            for(String proId : productCampaign.keySet()){
                if(chkMap.containsKey(proId)){
                    if(chkMap.get(proId) == '1'){
                        return  '产品 ' + productCampaign.get(proId) + ' 没有有效的注册证。';
                    }else if(chkMap.get(proId) == '2'){
                        return  '产品 ' + productCampaign.get(proId) + ' 超过经销商经营范围。';
                    }
                }
            }
            //return null;
        }
 
        Savepoint sp = Database.setSavepoint();
        try {
            Opportunity opp = new Opportunity();
                opp.Name                     = ord[0].Name;
                opp.Opportunity_No__c        = ord[0].Name;
                //TODO account
                opp.AccountId                = ordContract[0].Id;
                // TODO 医院
                opp.Agency1__c               = ord[0].Dealer_Info__c;
                opp.Hospital__c              = ordContract[0].Id;
                if(ord[0].Order_ProType__c == 'ET'){
                    opp.Opportunity_Category__c  = 'ET';
                    opp.Opp_order_Type__c        = 'ET';
                    opp.ET_SP_Consumption__c     = true;
                }else{
                    opp.Opportunity_Category__c  = 'GS';
                    opp.Opp_order_Type__c        = 'ENG';
                    //opp.ENG_Consumption__c       = true;
                }
                //opp.Opportunity_Category__c  = 'ET';
                opp.Strategic_department_new__c = '消化科';
                //CHAN-B6QBS8  ET/SP耗材勾选分开,增加ENG耗材勾选
                //opp.ET_SP_Consumption__c     = true;
                opp.Sales_Root__c            = '販売店';
                opp.Fund_Basis__c            = '病院資金';
                opp.StageName                = '引合';
                opp.Purchase_Type__c         = 'ET24時間販売';
                opp.Promise_Class__c         = '内貿';
                opp.Close_Forecasted_Date__c = ord[0].Order_date__c;
                //CHAN-B42D6F 安徽ET特约经销商来的询价  SAP上传省 紧急对应为北京 以后会修改 TODO
                //CHAN-BTF64C 安徽ENG经销商的询价默认SAP上传省修改 update by rentongxiao 2020-09-14 
                // if(ord[0].SalesManager__r.Province_Text__c == '安徽省'){
                if(ord[0].SalesManager__r.Province_Text__c == '安徽省' && opp.Opportunity_Category__c == 'ET'){
                    opp.SAP_Province__c = '北京市';
                }else{
                    opp.SAP_Province__c          = ord[0].SalesManager__r.Province_Text__c;
                }
                opp.Purchase_Reason__c       = '新期';
                opp.Estimation_Decision__c   = true;
                // SWAG-C3F59K 20210602 Start
                //opp.Sales_Method__c          = '院内招标';
                opp.Sales_Method__c          = '耗材采购';
                opp.ifOpenBid__c = '非公开招标';
                opp.LeadSource = '经销商';
                // SWAG-C3F59K 20210602 End
                //opp.Opportunity_stage__c     = '还没申请预算';
                opp.Opportunity_stage__c     = '中标签约';
                opp.OwnerId                  = ord[0].SalesManager__c;
                //TODO 报价ID
                opp.Estimation_Id__c         = ord[0].Id;
                opp.CloseDate                = Date.today().addDays(30);
                if(ord[0].Offers_Price__c <= 0  || ord[0].Offers_Price__c == null){
                    opp.Wholesale_Price__c   = ord[0].Total_amount__c;
                    opp.OCM_Agent1_Price__c  = ord[0].Total_amount__c;
                }else{
                    opp.Wholesale_Price__c   = ord[0].Offers_Price__c;
                    opp.OCM_Agent1_Price__c  = ord[0].Offers_Price__c;
                }
                opp.NotesApprovedNo__c       =  ord[0].Contract_application_decision__c;
                //TODO  Authorized_DB_No__c
                opp.Authorized_DB_No__c      = ord[0].Name;
                // 2018/11/05 CHAN-B686DZ  是否价格申请这个标识也请传输到生成的询价中 start
                opp.IS_Price_Apply__c        = ord[0].IS_Price_Apply__c;
                //if(ord[0].Order_ProType__c == 'ENG'){
                //    opp.IS_Price_Apply__c    = '是';
                //}
                // 2018/11/05 CHAN-B686DZ  是否价格申请这个标识也请传输到生成的询价中 end
                System.debug('oppren-----::'+opp);
            insert opp;
            List<PriceBookEntry> priceBookList = [
                SELECT
                        Id,
                        Product2Id, 
                        Product2.Id, 
                        Product2.Name 
                FROM
                        PriceBookEntry 
                WHERE
                        Product2Id IN: product2Ids 
                AND     CurrencyIsoCode = :ord[0].CurrencyIsoCode
                AND     IsActive=true
            ];
 
            List<OpportunityLineItem> olis = new List<OpportunityLineItem>();
            Integer i=1;
            for(Consumable_Orderdetails__c detail : odrDetails) {
                OpportunityLineItem oli = new OpportunityLineItem();
                oli.OpportunityId = opp.Id;
                oli.Quantity = detail.Consumable_count__c;
                oli.UnitPrice = detail.Intra_Trade_List_RMB__c;
                oli.UnitPrice__c  = detail.Intra_Trade_List_RMB__c;
                oli.OCM_Sales_Forecast__c = detail.Sum_of_money__c;
                //oli.TotalPrice__c = detail.Sum_of_money__c;
                oli.SFDA_Status__c= detail.Consumable_product__r.SFDA_Status__c;
                oli.Name__c       = detail.Consumable_product__r.Name__c;
                oli.ListPrice__c  = detail.Consumable_product__r.Intra_Trade_List_RMB__c;
                oli.Important_Rroduct__c = detail.Consumable_product__r.Product2__r.Important_product__c;
                oli.Item_Order__c = i;
                for(PriceBookEntry pbe: priceBookList) {
                    if(pbe.Product2.Id == detail.Consumable_product__r.Product2__c) {
                        oli.PriceBookEntryId = pbe.Id;
                        break;
                    }
                }
                 i++;
                olis.add(oli);
            }
            insert olis;
            // 2018/11/05 CHAN-B686DZ  如果选择是,生成的询价不直接win, 助理要进行手动WIN操作,设置这条询价是来自追溯系统 start
            // 2019/06/03 CHAN-BCPDGG  紧急重要:能量耗材追溯订单的合同申请控制
            //if(!'是'.equals(ord[0].IS_Price_Apply__c) && !'ENG'.equals(ord[0].Order_ProType__c)){
            if(!'是'.equals(ord[0].IS_Price_Apply__c)){
                 opp.SAP_Send_OK__c = true;
                
            }else{
                opp.Estimation_Decision__c   = true;
            }
            opp.OpportunitySource__c    = '消耗品系统';
            update opp;
            // 2018/11/05 CHAN-B686DZ  如果选择是,生成的询价不直接win, 助理要进行手动WIN操作 end
 
            List<Opportunity> oppdate = [SELECT Id,Name FROM Opportunity WHERE Name = :ord[0].Name];
            Consumable_order__c cOrder = new Consumable_order__c(Id = orderId);
            cOrder.Opportunity__c = opp.Id;
            cOrder.Approval_Date__c = Date.today();
            update cOrder;
 
            return '';
        } catch (DmlException de) {
            Database.rollback(sp);
            throw de;
        } catch (Exception e) {
            Database.rollback(sp);
            throw e;
        }
    }
}