高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
@isTest
private class OCMManagementProvinceBatchTest {
 
    @isTest static void test_method_one() {
        ControllerUtil.EscapeNFM001Trigger = true;
 
        Profile p = [select id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        String loginId = UserInfo.getUserId();
        User sys = [select id from User where Id = :loginId];
 
        User u1 = new User(Test_staff__c = true);
        u1.LastName = '_サンブリッジ';
        u1.FirstName = 'あ';
        u1.Batch_User__c = true;
        u1.Alias = 'あ';
        u1.Email = 'olympusTest01@sunbridge.com';
        u1.Username = 'olympusTest10@sunbridge.com';
        u1.CommunityNickname = 'あ1';
        u1.IsActive = true;
        u1.EmailEncodingKey = 'ISO-2022-JP';
        u1.TimeZoneSidKey = 'Asia/Tokyo';
        u1.LocaleSidKey = 'ja_JP';
        u1.LanguageLocaleKey = 'ja';
        u1.ProfileId = p.id;
        u1.Job_Category__c = '销售服务';
        u1.Province__c = '東京';
 
        User u2 = new User(Test_staff__c = true);
        u2.LastName = '_サンブリッジ';
        u2.FirstName = 'い';
        u2.Batch_User__c = true;
        u2.Alias = 'い';
        u2.Email = 'olympusTest02@sunbridge.com';
        u2.Username = 'olympusTest02@sunbridge.com';
        u2.CommunityNickname = 'い';
        u2.IsActive = true;
        u2.EmailEncodingKey = 'ISO-2022-JP';
        u2.TimeZoneSidKey = 'Asia/Tokyo';
        u2.LocaleSidKey = 'ja_JP';
        u2.LanguageLocaleKey = 'ja';
        u2.ProfileId = p.id;
        u2.Job_Category__c = '销售推广';
        u2.Province__c = '東京';
 
        User u3 = new User(Test_staff__c = true);
        u3.LastName = '_サンブリッジ';
        u3.Batch_User__c = true;
        u3.FirstName = 'う';
        u3.Alias = 'う';
        u3.Email = 'olympusTest03@sunbridge.com';
        u3.Username = 'olympusTest03@sunbridge.com';
        u3.CommunityNickname = 'う';
        u3.IsActive = true;
        u3.EmailEncodingKey = 'ISO-2022-JP';
        u3.TimeZoneSidKey = 'Asia/Tokyo';
        u3.LocaleSidKey = 'ja_JP';
        u3.LanguageLocaleKey = 'ja';
        u3.ProfileId = p.id;
        u3.Job_Category__c = '支援';
        u3.Province__c = '東京';
        u3.Dept__c = '能量事业本部';
 
        insert new User[] {u1, u2, u3};
 
        System.runAs(sys) {
            OCM_Management_Province__c mp1 = new OCM_Management_Province__c();
            mp1.Name = '北京';
            mp1.GI_assistant__c = u1.Id;
            mp1.SP_assistant__c = u1.Id;
            mp1.Energy_assistant__c = u1.Id;
 
            OCM_Management_Province__c mp2 = new OCM_Management_Province__c();
            mp2.Name = '上海';
            mp2.GI_assistant__c = u2.Id;
            mp2.SP_assistant__c = u2.Id;
            mp2.Energy_assistant__c = u2.Id;
 
            OCM_Management_Province__c mp3 = new OCM_Management_Province__c();
            mp3.Name = '深圳';
            mp3.GI_assistant__c = u3.Id;
            mp3.SP_assistant__c = u3.Id;
            mp3.Energy_assistant__c = u3.Id;
 
            OCM_Management_Province__c mp4 = new OCM_Management_Province__c();
            mp4.Name = '特约经销商管理支援本部';
            mp4.GI_assistant__c = loginId;
            mp4.SP_assistant__c = loginId;
            mp4.Energy_assistant__c = loginId;
 
            insert new OCM_Management_Province__c[] {mp1, mp2, mp3, mp4};
 
            Address_Level__c al1 = new Address_Level__c();
            al1.Name = '北京市';
            al1.Level1_Code__c = 'CN-01';
            al1.Level1_Sys_No__c = '353341';
 
            Address_Level__c al2 = new Address_Level__c();
            al2.Name = '上海市';
            al2.Level1_Code__c = 'CN-16';
            al2.Level1_Sys_No__c = '353334';
 
            Address_Level__c al3 = new Address_Level__c();
            al3.Name = '广东省';
            al3.Level1_Code__c = 'CN-22';
            al3.Level1_Sys_No__c = '353343';
 
            insert new Address_Level__c[] {al1, al2, al3};
 
            Address_Level2__c al21 = new Address_Level2__c();
            al21.Level1_Code__c = 'CN-01';
            al21.Level1_Sys_No__c = '353341';
            al21.Level1_Name__c = '北京市';
            al21.Name = '海淀区';
            al21.Level2_Code__c = 'CN-0123';
            al21.Level2_Sys_No__c = '353664';
            al21.Address_Level__c = al1.id;
 
            Address_Level2__c al22 = new Address_Level2__c();
            al22.Level1_Code__c = 'CN-16';
            al22.Level1_Sys_No__c = '353334';
            al22.Level1_Name__c = '上海市';
            al22.Name = '徐汇区';
            al22.Level2_Code__c = 'CN-1631';
            al22.Level2_Sys_No__c = '353572';
            al22.Address_Level__c = al2.id;
 
            Address_Level2__c al23 = new Address_Level2__c();
            al23.Level1_Code__c = 'CN-22';
            al23.Level1_Sys_No__c = '353343';
            al23.Level1_Name__c = '广东省';
            al23.Name = '深圳市';
            al23.Level2_Code__c = 'CN-2259';
            al23.Level2_Sys_No__c = '353700';
            al23.Address_Level__c = al3.id;
 
            insert new Address_Level2__c[] {al21, al22, al23};
 
            RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Hp'];
            List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName IN ('Department_GI', 'Department_GS') order by DeveloperName];
 
            Account acc1 = new Account();
            acc1.RecordTypeId = rectCo.Id;
            acc1.Name = 'HP test1';
            //acc1.OCM_Management_Province__c = mp1.Id;
            acc1.State_Master__c = al1.Id;
            acc1.City_Master__c = al21.Id;
 
            Account acc2 = new Account();
            acc2.RecordTypeId = rectCo.Id;
            acc2.Name = 'HP test2';
            //acc2.OCM_Management_Province__c = mp2.Id;
            acc2.State_Master__c = al2.Id;
            acc2.City_Master__c = al22.Id;
 
            Account acc3 = new Account();
            acc3.RecordTypeId = rectCo.Id;
            acc3.Name = 'HP test3';
            //acc3.OCM_Management_Province__c = mp3.Id;
            acc3.State_Master__c = al3.Id;
            acc3.City_Master__c = al23.Id;
 
            insert new Account[] {acc1, acc2, acc3};
 
            List<Account> dept1 = [select Id, Name from Account where ParentId = :acc1.Id and Department_Class_Label__c = '消化科'];
            List<Account> dept2 = [select Id, Name from Account where ParentId = :acc2.Id and Department_Class_Label__c = '普外科'];
            List<Account> dept3 = [select Id, Name from Account where ParentId = :acc3.Id and Department_Class_Label__c = '消化科'];
 
            dept3[0].OwnerId = u3.Id;
            update dept3[0];
 
            Account depart1 = new Account();
            depart1.RecordTypeId = rectDpt[0].Id;
            depart1.Name         = '*';
            depart1.Department_Name__c  = '診療科1';
            depart1.ParentId            = dept1[0].Id;
            depart1.Department_Class__c = dept1[0].Id;
            depart1.Hospital__c         = acc1.Id;
 
            Account depart2 = new Account();
            depart2.RecordTypeId = rectDpt[1].Id;
            depart2.Name         = '*';
            depart2.Department_Name__c  = '診療科2';
            depart2.ParentId            = dept2[0].Id;
            depart2.Department_Class__c = dept2[0].Id;
            depart2.Hospital__c         = acc2.Id;
 
            Account depart3 = new Account();
            depart3.RecordTypeId = rectDpt[0].Id;
            depart3.Name         = '*';
            depart3.Department_Name__c  = '診療科3';
            depart3.ParentId            = dept3[0].Id;
            depart3.Department_Class__c = dept3[0].Id;
            depart3.Hospital__c         = acc3.Id;
 
            insert new Account[] {depart1, depart2, depart3};
 
            RecordType oppVND = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity'];
 
            System.Test.startTest();
            Opportunity opp1 = new Opportunity(
                Name = 'test opp1',
                StageName = '引合',
                CurrencyIsoCode = 'USD',
                CloseDate = Date.today(),
                AccountId = depart1.Id,
                RecordTypeId = oppVND.Id,
                Closing_Bid_Date__c = Date.today().addDays(-5),
                Hospital__c = acc1.Id,
                Competitor__c = 'A',
                Opportunity_Category__c = 'GI'
            );
            Opportunity opp2 = new Opportunity(
                Name = 'test opp2',
                StageName = '引合',
                CurrencyIsoCode = 'USD',
                CloseDate = Date.today(),
                AccountId = depart2.Id,
                RecordTypeId = oppVND.Id,
                Closing_Bid_Date__c = Date.today().addDays(-5),
                Hospital__c = acc2.Id,
                Competitor__c = 'B',
                Opportunity_Category__c = 'SP'
            );
            Opportunity opp3 = new Opportunity(
                Name = 'test opp3',
                StageName = '引合',
                CurrencyIsoCode = 'USD',
                CloseDate = Date.today(),
                AccountId = depart3.Id,
                RecordTypeId = oppVND.Id,
                Closing_Bid_Date__c = Date.today().addDays(-5),
                Hospital__c = acc3.Id,
                Competitor__c = 'B',
                Opportunity_Category__c = 'GI',
                OwnerId = u3.Id,
                owner_not_automatically_update__c = true
            );
            Opportunity opp4 = new Opportunity(
                Name = 'test opp4',
                StageName = '引合',
                CurrencyIsoCode = 'USD',
                CloseDate = Date.today(),
                AccountId = depart3.Id,
                RecordTypeId = oppVND.Id,
                Closing_Bid_Date__c = Date.today().addDays(-5),
                Hospital__c = acc3.Id,
                Competitor__c = 'B',
                Opportunity_Category__c = 'GI',
                Distributor_InCharge_opp__c = true
            );
            insert new Opportunity[] {opp1, opp2, opp3, opp4};
 
            List<Opportunity> oppList = [select id,Sales_assistant_name_text__c from Opportunity order by Name];
            System.assertEquals(u1.Id, oppList[0].Sales_assistant_name_text__c);
            System.assertEquals(u2.Id, oppList[1].Sales_assistant_name_text__c);
            System.assertEquals(u3.Id, oppList[2].Sales_assistant_name_text__c);
            System.assertEquals(loginId, oppList[3].Sales_assistant_name_text__c);
 
            opp1.Sales_assistant_name_text__c = null;
            opp2.Sales_assistant_name_text__c = null;
            opp3.Sales_assistant_name_text__c = null;
            opp4.Sales_assistant_name_text__c = null;
            update new Opportunity[] {opp1, opp2, opp3, opp4};
 
            OCMManagementProvinceWebService.updateOpportunity();
 
            System.Test.stopTest();
        }
    }
 
    @isTest static void test_method_two() {
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
 
        Product2 product = new Product2( Name='テスト商品');
        product.SFDA_Status__c = '有効';
        product.Intra_Trade_List_RMB_1__c = 100;
        product.Intra_Trade_List_RMB_Date1__c = date.today();
        product.Intra_Trade_Cost_RMB_1__c = 200;
        product.Intra_Trade_Cost_RMB_Date1__c = date.today();
        product.Manual_Entry__c = false;
        product.Asset_Model_No__c = '11111';
        product.ProductCode = 'OTV-SP1H-NA-12E';
        insert product;
 
        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=product.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        insert entry;
 
        Opportunity opp = new Opportunity();
        opp.Name='aiueo';
        opp.StageName='contact';
        opp.Trade__c = '内貿';
        opp.CloseDate=Date.today();
        opp.CurrencyIsoCode = 'CNY';
        opp.Estimation_List_Price__c = 100;
        opp.Wholesale_Price__c = 101;
        opp.Dealer_Final_Price__c = 102;
        opp.OCM_Agent1_Price__c = 103;
        opp.Stocking_Price__c = 104;
        opp.Estimation_No__c = '105';
        opp.Estimation_Name__c = '106';
        opp.Estimation_Id__c = '107';
        insert opp;
 
        OpportunityLineItem oppli = new OpportunityLineItem();
        oppli.OpportunityId = opp.Id;
        oppli.Id__c = '110';
        oppli.SFDA_Status__c = '有効';
        oppli.Name__c = '111';
        oppli.ListPrice__c = 112;
        oppli.Quantity = 113;
        oppli.UnitPrice = 114;
        oppli.UnitPrice__c = 115;
        oppli.Qty_Unit__c = '116';
        oppli.Cost__c = 117;
        oppli.BSS_Category__c = 'G&R';
        oppli.Item_Order__c = 1;
        oppli.PricebookEntryId = entry.Id;
        insert oppli;
 
        Quote quo = new Quote();
        quo.Name = '206';
        quo.OpportunityId = opp.Id;
        quo.Quote_No__c = '205';
        quo.Estimation_List_Price__c = 200;
        quo.Dealer_Final_Price__c = 202;
        quo.Stocking_Price__c = 204;
        quo.OCM_Sales_Forecast__c = 201;
        quo.OCM_Agent1_Price__c = 203;
        quo.Pricebook2Id = pricebookId;
        insert quo;
 
        QuoteLineItem qli = new QuoteLineItem();
        qli.QuoteId = quo.Id;
        qli.Id__c = '210';
        qli.SFDA_Status__c = '有効';
        qli.Name__c = '211';
        qli.ListPrice__c = 212;
        qli.Quantity = 213;
        qli.UnitPrice__c = 215;
        qli.UnitPrice = 0;
        qli.Qty_Unit__c = '216';
        qli.Cost__c = 217;
        qli.BSS_Category__c = 'ET';
        qli.Subtotal__c = 218;
        qli.PricebookEntryId = entry.Id;
        insert qli;
 
        opp.Estimation_Id__c = quo.Id;
        opp.Estimation_Decision__c = true;
        update opp;
 
        opp.Estimation_Decision__c = false;
        update opp;
    }
 
}