高章伟
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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
@isTest
public class AgencyOpportunityHandlerTest {
 
    public static Account account1 = new Account();
    public static Account account2 = new Account();
    public static Account account3 = new Account();
    public static Contact contact1 = new Contact();
    public static Contact contact2 = new Contact();
    public static Contact contact3 = new Contact();
    public static User user = new User();
    public static User user2 = new User();
    public static Agency_Hospital_Link__c agency_hospital_link = new Agency_Hospital_Link__c();
    public static ProductTypes__c product_types = new ProductTypes__c();
    public static Agency_Opportunity__c agency_opportunity = new Agency_Opportunity__c();
    public static Agency_Opportunity__c agency_opportunity2 = new Agency_Opportunity__c();
    public static OlympusCalendar__c olympus_calendar = new OlympusCalendar__c();
    public static String report_id;
    public static String report_header_id;
    // 病院。コンタクト、ユーザー、カレンダーなどの初期データ作る
    public static Account hospital;
    public static Account agency1;
    public static Account agency2;
    public static Account agency3;
    public static String oldAgencyCode;
    public static String newAgencyCode;
    public static String oldAgencyId15;
    public static String newAgencyId15;
 
    public static Contact contact4;
    public static Contact contact5;
    public static User user1;
    public static User user3;
    public static Agency_Hospital_Link__c a1HPLink;
    public static Agency_Hospital_Link__c a2HPLink;
    public static Agency_Hospital_Link__c a3HPLink;
    
    private static void testInit() {
        // 取引先
 
        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        insert account1;
        List<Account> acclist=[select RecordTypeId,Name 
                                from Account
                                ];
        
 
        
        account2.Name = 'test1经销商';
        account2.RecordTypeId = '01210000000Qem1';
        insert account2;
 
        List<Account> acc2list=[select RecordTypeId,Name 
                                from Account
                                ];
 
        System.assertEquals(10,acc2list.size());
        
 
        // 取引先責任者
        contact1.AccountId = account1.Id;
        contact1.FirstName = '責任者';
        contact1.LastName = 'test1医院';
        insert contact1;
        List<Contact> contactlist=[select Id,FirstName 
                                from Contact
                                ];
 
        System.assertEquals('責任者',contactlist[0].FirstName);
 
        contact2.AccountId = account2.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        insert contact2;
        
        List<Contact> contact2list=[select Id,FirstName 
                                    from Contact
                                    ];
 
        System.assertEquals('責任者',contact2list[0].FirstName);
 
 
        // ユーザー
        //Profile p = [select Id from Profile where Name = '901_经销商周报'];
        Profile p = [select Id from Profile where Name = '901_经销商活动系统'];
        user.ProfileId = p.Id;
        user.ContactId = contact2.Id;
        user.FirstName = 'ユーザー';
        user.LastName = 'テスト';
        user.Email = 'test_user@example.com';
        user.emailencodingkey='UTF-8';
        user.languagelocalekey='zh_CN';
        user.localesidkey='ja_JP';
        user.timezonesidkey='Asia/Shanghai';
        user.Username = 'test_user@example.com' + System.now().millisecond();
        user.Alias = 'テユ';
        user.CommunityNickname = 'tu' + System.now().millisecond();
        insert user;
        List<user> users = [select Id,Name,LastName, FirstName from User where LastName='テスト'];
        //System.assertEquals('テスト',users[0].LastName);
        System.assertEquals('ユーザー',users[0].FirstName );
 
 
        agency_hospital_link.Name = 'test1代理店医院';
        agency_hospital_link.Hospital__c = account1.Id;
        agency_hospital_link.Agency__c = account2.Id;
        agency_hospital_link.OwnerId = user.Id;
        agency_hospital_link.Agency_Campaign_Obj__c = true;
        insert agency_hospital_link;
 
        List<Agency_Hospital_Link__c> agencylist=[select Id,Name from Agency_Hospital_Link__c where Id =: agency_hospital_link.Id];
        //System.assertEquals(null, agencylist.size());
        System.assertEquals('test1医院',agencylist[0].Name);
 
        //Id rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Opportunity'].Id;
        Id rtId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        // Schema.SobjectType.Agency_Opportunity__c.getRecordTypeInfosByName().get('询价').getRecordTypeId();
        agency_opportunity.Agency_Person__c = contactlist[0].id;
        agency_opportunity.RecordTypeId = rtId;
        agency_opportunity.Name = '引合';
        agency_opportunity.Hospital_Target__c = account1.Id;
        agency_opportunity.Agency__c = account2.Id;
        agency_opportunity.Agency_Hospital__c = agency_hospital_link.Id;
        agency_opportunity.StageName__c = '还没申请预算';
        agency_opportunity.OwnerId = user.Id;
        agency_opportunity.NewOpportunity_Agency_Apply_Status__c = '批准';
        agency_opportunity.Department_Cateogy__c = 'ET';
 
        agency_opportunity.Sales_Manager__c = user.Id;
        insert agency_opportunity;
 
 
        List<Agency_Opportunity__c> opportunitylist=[select Id,Name from Agency_Opportunity__c];
 
        System.assertEquals(1,opportunitylist.size());
 
        //rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Target'].Id;
        rtId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        //Schema.SobjectType.Agency_Opportunity__c.getRecordTypeInfosByName().get('目标').getRecordTypeId();
        agency_opportunity2.Agency_Person__c = contact2list[0].id;
        agency_opportunity2.RecordTypeId = rtId;
        agency_opportunity2.Name = '引合2';
        agency_opportunity2.Hospital_Target__c = account1.Id;
        agency_opportunity2.Agency__c = account2.Id;
        agency_opportunity2.OwnerId = user.Id;
        agency_opportunity2.NewOpportunity_Agency_Apply_Status__c = '草案中';
        agency_opportunity.Department_Cateogy__c = 'GS';
 
        insert agency_opportunity2;
 
        System.debug(agency_opportunity2.Name);
 
        List<Agency_Opportunity__c> opportunitylist2=[select Id,Name from Agency_Opportunity__c];
 
        System.assertEquals(2,opportunitylist2.size());
 
        olympus_calendar.Date__c = Date.valueOf('2017-04-10');
        olympus_calendar.OwnerId = user.Id;
        insert olympus_calendar;
 
        List<OlympusCalendar__c> olympusCalendarlist=[select Id,Name,Date__c from OlympusCalendar__c];
        System.assertEquals(Date.valueOf('2017-04-10'),olympusCalendarlist[0].Date__c);
 
    }
    
    // @isTest
    // public static void selectOpportunityByIdAndHospitalLinkIdTest() {
    //     Test.startTest();
    //    testInit();
    //     Test.stopTest();
    //    System.runAs(user) {
    //    List<Agency_Opportunity__c> opportunity_data = WeeklyReportCmp.selectOpportunityByIdAndHospitalLinkId(agency_opportunity.Id, agency_hospital_link.Id);
    
    //       List<Agency_Opportunity__c> agencyopportunity=[select Id,Name,Agency__c,Agency_ID__c,Owner_Agency_ID__c,
    //                                                     Agency_Hospital_All__c,Agency_Hospital__c,Agency_Hospital_Target__c
    //                                                     from Agency_Opportunity__c ];
 
 
    //      System.assertEquals('::'+agency_opportunity.Name, agencyopportunity[0].Name);
 
    //      System.assertEquals(agency_opportunity.Agency__c, agencyopportunity[0].Owner_Agency_ID__c);
        
    //      System.assertEquals(agency_opportunity.Agency_Hospital_All__c, agencyopportunity[0].Agency_Hospital_Target__c);
 
    //      //System.assertEquals(agency_opportunity2.Agency_Hospital_All__c, agencyopportunity[0].Agency_Hospital__c);
 
    //      //System.assertEquals(agency_opportunity2.Agency_ID__c, String.valueOf(agencyopportunity[0].Owner_Agency_ID__c).substring(0,15));
 
    //    }
    // }
 
    @isTest static void test_setAgency_Contact_Share_ToRole() {
        Oly_TriggerHandler.bypass('AgencyReportHeaderHandler');
        Oly_TriggerHandler.bypass('AgencyHospitalHandler');
        //MergeAgencyActivityBatchTest.makeNormalData(true);
        makeNormalData(true);
 
        // assert
        List<Agency_Opportunity__c> tList = [SELECT Name, NewOpportunity_Agency_Apply_Status__c, Agency__c
                FROM Agency_Opportunity__c ORDER BY Id LIMIT 2];
        System.assertEquals(2, tList.size());
        System.assertEquals(agency1.Id, tList[0].Agency__c);
        System.assertEquals(agency1.Id, tList[1].Agency__c);
 
        tList[0].NewOpportunity_Agency_Apply_Status__c = '填写完毕';
        tList[1].NewOpportunity_Agency_Apply_Status__c = '填写完毕';
        update tList;
 
        tList[0].NewOpportunity_Agency_Apply_Status__c = '批准';
        tList[1].NewOpportunity_Agency_Apply_Status__c = '批准';
        update tList;
 
        // List<Agency_Opportunity__Share> tsList = [SELECT Id
        //         FROM Agency_Opportunity__Share WHERE ParentId = :tList AND RowCause = 'Manual'];
        // System.assertEquals(2, tsList.size());
 
        Test.startTest();
        Delete tList[0];
        Test.stopTest();
        // tsList = [SELECT Id
        //         FROM Agency_Opportunity__Share WHERE ParentId = :tList AND RowCause = 'Manual'];
        // System.assertEquals(1, tsList.size());
 
        UnDelete tList[0];
        // tsList = [SELECT Id
        //         FROM Agency_Opportunity__Share WHERE ParentId = :tList AND RowCause = 'Manual'];
        // System.assertEquals(2, tsList.size());
    }
 
    //agency_opportunityのinsert
    @isTest
    public static void tst_shareToOSCM() {
        // 取引先
        
        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        account2.Name = 'test1经销商';
        account2.RecordTypeId = '01210000000Qem1';
        Id AgencyRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        account3.Name = 'test2经销商';
        account3.RecordTypeId = AgencyRecordTypeId;
 
        insert new List<Account> {account1, account2, account3};
 
        // 取引先責任者
        contact1.AccountId = account1.Id;
        contact1.FirstName = '責任者';
        contact1.LastName = 'test1医院';
        contact2.AccountId = account2.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        contact3.AccountId = account2.Id;
        contact3.FirstName = '責任者';
        contact3.LastName = 'test2经销商';
        insert new List<contact> {contact1,contact2,contact3};
 
        Test.startTest();
        // ユーザー
        //Profile p = [select Id from Profile where Name = '901_经销商周报'];
        Profile p = [select Id from Profile where Name = '901_经销商活动系统'];
        user.ProfileId = p.Id;
        user.ContactId = contact2.Id;
        user.FirstName = 'ユーザー';
        user.LastName = 'テスト';
        user.Email = 'test_user@example.com';
        user.emailencodingkey='UTF-8';
        user.languagelocalekey='zh_CN';
        user.localesidkey='ja_JP';
        user.timezonesidkey='Asia/Shanghai';
        user.Username = 'test_user@example.com' + System.now().millisecond();
        user.Alias = 'テユ';
        user.CommunityNickname = 'tu1' + System.now().millisecond();
 
        user2.ProfileId = p.Id;
        user2.ContactId = contact3.Id;
        user2.FirstName = 'ユーザー2';
        user2.LastName = 'テスト2';
        user2.Email = 'test_user2@example.com';
        user2.emailencodingkey='UTF-8';
        user2.languagelocalekey='zh_CN';
        user2.localesidkey='ja_JP';
        user2.timezonesidkey='Asia/Shanghai';
        user2.Username = 'test_user2@example.com' + System.now().millisecond();
        user2.Alias = 'テユ2';
        user2.CommunityNickname = 'tu2' + System.now().millisecond();
 
        insert new List<User> {user, user2};
 
        agency_hospital_link.Name = 'test1代理店医院';
        agency_hospital_link.Hospital__c = account1.Id;
        agency_hospital_link.Agency__c = account2.Id;
        agency_hospital_link.OwnerId = user.Id;
        agency_hospital_link.Agency_Campaign_Obj__c = true;
        insert agency_hospital_link;
 
        ProductTypes__c productTypes = new ProductTypes__c();
        productTypes.Name ='290系统';
        productTypes.Name2__c ='290系统';
        productTypes.Department_Cateogy__c = 'GI;BF;GS';
        insert  productTypes;
 
        List<Contact> contactlist=[select Id,FirstName from Contact];
 
        //Id rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Opportunity'].Id;
        Id rtId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        // Schema.SobjectType.Agency_Opportunity__c.getRecordTypeInfosByName().get('询价').getRecordTypeId();
        agency_opportunity.Agency_Person__c = contactlist[0].id;
        agency_opportunity.RecordTypeId = rtId;
        agency_opportunity.Name = '引合1';
        agency_opportunity.Hospital_Target__c = account1.Id;
        agency_opportunity.Agency__c = account2.Id;
        agency_opportunity.Agency_Hospital__c = agency_hospital_link.Id;
        agency_opportunity.StageName__c = '还没申请预算';
        agency_opportunity.OwnerId = user.Id;
        agency_opportunity.NewOpportunity_Agency_Apply_Status__c = '批准';
        agency_opportunity.OCM_Change_To_Opportunity_Flg__c = true;
        agency_opportunity.Department_Cateogy__c = 'GI';
        agency_opportunity.Product_Category1_P__c ='290系统';
        //agency_opportunity.Product_Category2_P__c ='170系统';
        //agency_opportunity.Product_Category3_P__c ='170系统';
 
        agency_opportunity.Sales_Manager__c = user.Id;
        insert agency_opportunity;
 
        Opportunity opp = new Opportunity();
        opp.Agency_Opportunity__c = agency_opportunity.Id;
        opp.Name = 'test询价';
        opp.StageName = '目標';
        //opp.AgencyOpp_FromOCM__c = true;
        opp.SAP_Send_OK__c = false;
        opp.CloseDate = Date.newInstance(2222, 7, 15);
        insert opp;
        opp.SAP_Send_OK__c = true;
 
        agency_opportunity2.RecordTypeId = rtId;
        agency_opportunity2.Name = '引合2';
        agency_opportunity2.Hospital_Target__c = account1.Id;
        agency_opportunity2.Agency__c = account2.Id;
        agency_opportunity2.Agency_Hospital__c = agency_hospital_link.Id;
        //oObj.OCSM_Owner__c != nObj.OCSM_Owner__c
        agency_opportunity2.OwnerId = user2.Id;
        agency_opportunity2.NewOpportunity_Agency_Apply_Status__c = '批准';
        agency_opportunity2.OCM_Change_To_Opportunity_Flg__c = false;
        insert agency_opportunity2;
 
        Test.stopTest();
        Opportunity opp2 = new Opportunity();
        opp2.Agency_Opportunity__c = agency_opportunity2.Id;
        opp2.Name = 'test询价2';
        opp2.StageName = '目標2';
        opp2.SAP_Send_OK__c = false;
        opp2.CloseDate = Date.newInstance(2222, 7, 15);
        opp2.SAP_Send_OK__c = true;
        
        // Test.startTest();
        insert opp2;
        // Test.stopTest();
 
        String rowCause = Schema.Agency_Opportunity__Share.RowCause.OCSM_Owner_c_User__c;
        List<Agency_Opportunity__Share> agencyopportunityShare=[SELECT
                    RowCause,ParentId,UserOrGroupId
                FROM Agency_Opportunity__Share
                WHERE ParentId=:agency_opportunity.Id and RowCause =:rowCause];
 
        System.assertEquals('OCSM_Owner_c_User__c', agencyopportunityShare[0].RowCause);
 
        agencyopportunityShare=[SELECT
                    RowCause,ParentId,UserOrGroupId
                FROM Agency_Opportunity__Share
                WHERE ParentId=:agency_opportunity.Id and RowCause <>:rowCause];
        System.assertEquals(user.Id, agencyopportunityShare[0].UserOrGroupId);
    }
 
    // 经销商所有者テスト
    @isTest
    public static void tst_Owner1() {
 
        // 取引先
        // 
        StaticParameter.EscapeAccountTrigger = true;
        StaticParameter.EscapeNFM007Trigger = true;
        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
        StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;
 
        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        account2.Name = 'test1经销商';
        account2.RecordTypeId = '01210000000Qem1';
        Id AgencyRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        account3.Name = 'test2经销商';
        account3.RecordTypeId = AgencyRecordTypeId;
 
        insert new List<Account> {account1, account2, account3};
 
        // 取引先責任者
        contact1.AccountId = account1.Id;
        contact1.FirstName = '責任者';
        contact1.LastName = 'test1医院';
        contact2.AccountId = account2.Id;
        contact2.FirstName = '責任者';
        contact2.Agency_User__c = true;
        contact2.LastName = 'test1经销商';
        contact3.AccountId = account2.Id;
        contact3.FirstName = '責任者';
        contact3.LastName = 'test2经销商';
        insert new List<contact> {contact1,contact2,contact3};
        //insert new List<contact> {contact1,contact2};
 
 
        // ユーザー
        //Profile p = [select Id from Profile where Name = '901_经销商周报'];
        Profile p = [select Id from Profile where Name = '901_经销商活动系统'];
        user.ProfileId = p.Id;
        user.ContactId = contact2.Id;
        user.FirstName = 'ユーザー';
        user.LastName = 'テスト';
        user.Email = 'test_user@example.com';
        user.emailencodingkey='UTF-8';
        user.languagelocalekey='zh_CN';
        user.localesidkey='ja_JP';
        user.timezonesidkey='Asia/Shanghai';
        user.Username = 'test_user@example.com' + System.now().millisecond();
        user.Alias = 'テユ';
        user.CommunityNickname = 'tu1' + System.now().millisecond();
 
        user2.ProfileId = p.Id;
        user2.ContactId = contact3.Id;
        user2.FirstName = 'ユーザー2';
        user2.LastName = 'テスト2';
        user2.Email = 'test_user2@example.com';
        user2.emailencodingkey='UTF-8';
        user2.languagelocalekey='zh_CN';
        user2.localesidkey='ja_JP';
        user2.timezonesidkey='Asia/Shanghai';
        user2.Username = 'test_user2@example.com' + System.now().millisecond();
        user2.Alias = 'テユ2';
        user2.CommunityNickname = 'tu2' + System.now().millisecond();
 
        insert new List<User> {user, user2};
 
        agency_hospital_link.Name = 'test1代理店医院';
        agency_hospital_link.Hospital__c = account1.Id;
        agency_hospital_link.Agency__c = account2.Id;
        agency_hospital_link.OwnerId = user.Id;
        agency_hospital_link.Agency_Campaign_Obj__c = true;
        insert agency_hospital_link;
 
        ProductTypes__c productTypes = new ProductTypes__c();
        productTypes.Name ='290系统';
        productTypes.Name2__c ='290系统';
        productTypes.Department_Cateogy__c = 'GI;BF;GS';
        insert  productTypes;
 
        List<Contact> contactlist=[select Id,FirstName from Contact];
 
        //Id rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Opportunity'].Id;
        Id rtId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        // Schema.SobjectType.Agency_Opportunity__c.getRecordTypeInfosByName().get('询价').getRecordTypeId();
        agency_opportunity.Agency_Person__c = contactlist[0].id;
        agency_opportunity.RecordTypeId = rtId;
        agency_opportunity.Name = '引合1';
        agency_opportunity.Hospital_Target__c = account1.Id;
        agency_opportunity.Agency__c = account2.Id;
        agency_opportunity.Agency_Hospital__c = agency_hospital_link.Id;
        agency_opportunity.StageName__c = '还没申请预算';
        agency_opportunity.OwnerId = user.Id;
        agency_opportunity.NewOpportunity_Agency_Apply_Status__c = '批准';
        agency_opportunity.OCM_Change_To_Opportunity_Flg__c = true;
        agency_opportunity.Department_Cateogy__c = 'GI';
        agency_opportunity.Product_Category1_P__c ='290系统';
        //agency_opportunity.Product_Category2_P__c ='170系统';
        //agency_opportunity.Product_Category3_P__c ='170系统';
 
        agency_opportunity.Sales_Manager__c = user.Id;
        insert agency_opportunity;
 
        Opportunity opp = new Opportunity();
        opp.Agency_Opportunity__c = agency_opportunity.Id;
        opp.Name = 'test询价';
        opp.StageName = '目標';
        //opp.AgencyOpp_FromOCM__c = true;
        opp.SAP_Send_OK__c = false;
        opp.CloseDate = Date.newInstance(2222, 7, 15);
        opp.OwnerId = user.Id;
        insert opp;
 
        List<Opportunity> oppList = [SELECT Id,
                                            OwnerId
                                       FROM Opportunity];
        System.assertEquals(1, oppList.size());
        System.assertEquals(user.Id, oppList[0].OwnerId);
 
        List<User> owners = [select Id from User where AccountId =:account2.Id and isActive = true and Contact.Agency_User__c = true order by CreatedDate limit 1];
        System.assertEquals(1, owners.size());
 
        agency_opportunity2.RecordTypeId = rtId;
        agency_opportunity2.Name = '引合2';
        agency_opportunity2.Hospital_Target__c = account1.Id;
        agency_opportunity2.Agency__c = account2.Id;
        agency_opportunity2.Agency_Hospital__c = agency_hospital_link.Id;
        agency_opportunity2.NewOpportunity_Agency_Apply_Status__c = '批准';
        agency_opportunity2.OCM_Change_To_Opportunity_Flg__c = false;
        insert agency_opportunity2;
 
 
        List<Agency_Opportunity__c> aoList = [SELECT Id,
                                                     OwnerId,
                                                     Sales_Manager_F__c,
                                                     Sales_Manager__c
                                                FROM Agency_Opportunity__c
                                               WHERE Id = :agency_opportunity2.Id];
        List<User> userList = [SELECT Id,
                                      SalesManager__c
                                 FROM User
                                WHERE Id = :UserInfo.getUserId()];
        System.assertEquals(1, aoList.size());
        System.assertEquals(UserInfo.getUserId(), aoList[0].OwnerId);
        System.assertEquals(userList[0].SalesManager__c, aoList[0].Sales_Manager_F__c);
        //System.assertEquals(aoList[0].Sales_Manager_F__c, aoList[0].Sales_Manager__c);
    }
 
   // 普通所有者テスト
    @isTest
    public static void tst_Owner2() {
 
        // 取引先
 
        account1.Name = 'test1医院';
        account1.RecordTypeId = '01210000000QemG';
        account2.Name = 'test1经销商';
        account2.RecordTypeId = '01210000000Qem1';
        Id AgencyRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        account3.Name = 'test2经销商';
        account3.RecordTypeId = AgencyRecordTypeId;
 
        insert new List<Account> {account1, account2, account3};
 
        // 取引先責任者
        contact1.AccountId = account1.Id;
        contact1.FirstName = '責任者';
        contact1.LastName = 'test1医院';
        contact2.AccountId = account2.Id;
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        contact2.Agency_User__c = true;
        contact3.AccountId = account2.Id;
        contact3.FirstName = '責任者';
        contact3.LastName = 'test2经销商';
        insert new List<contact> {contact1,contact2,contact3};
 
        // ユーザー
        //Profile p = [select Id from Profile where Name = '901_经销商周报'];
        Profile p = [select Id from Profile where Name = '901_经销商活动系统'];
        user.ProfileId = p.Id;
        user.ContactId = contact2.Id;
        user.FirstName = 'ユーザー';
        user.LastName = 'テスト';
        user.Email = 'test_user@example.com';
        user.emailencodingkey='UTF-8';
        user.languagelocalekey='zh_CN';
        user.localesidkey='ja_JP';
        user.timezonesidkey='Asia/Shanghai';
        user.Username = 'test_user@example.com' + System.now().millisecond();
        user.Alias = 'テユ';
        user.CommunityNickname = 'tu1' + System.now().millisecond();
 
        user2.ProfileId = p.Id;
        user2.ContactId = contact3.Id;
        user2.FirstName = 'ユーザー2';
        user2.LastName = 'テスト2';
        user2.Email = 'test_user2@example.com';
        user2.emailencodingkey='UTF-8';
        user2.languagelocalekey='zh_CN';
        user2.localesidkey='ja_JP';
        user2.timezonesidkey='Asia/Shanghai';
        user2.Username = 'test_user2@example.com' + System.now().millisecond();
        user2.Alias = 'テユ2';
        user2.CommunityNickname = 'tu2' + System.now().millisecond();
 
        // ユーザー
        User user3 = new User();
        user3.ProfileId = UserInfo.getProfileId();
        user3.FirstName = 'ユーザー3';
        user3.LastName = 'テスト3';
        user3.Email = 'test_user3@example.com';
        user3.emailencodingkey='UTF-8';
        user3.languagelocalekey='zh_CN';
        user3.localesidkey='ja_JP';
        user3.timezonesidkey='Asia/Shanghai';
        user3.Username = 'test_user3@example.com' + System.now().millisecond();
        user3.Alias = 'テユ4';
        user3.CommunityNickname = 'tu3' + System.now().millisecond();
 
        User user4 = new User();
        user4.ProfileId = UserInfo.getProfileId();
        user4.FirstName = 'ユーザー4';
        user4.LastName = 'テスト4';
        user4.Email = 'test_user4@example.com';
        user4.emailencodingkey='UTF-8';
        user4.languagelocalekey='zh_CN';
        user4.localesidkey='ja_JP';
        user4.timezonesidkey='Asia/Shanghai';
        user4.Username = 'test_user4@example.com' + System.now().millisecond();
        user4.Alias = 'テユ4';
        user4.CommunityNickname = 'tu4' + System.now().millisecond();
 
        insert new List<User> {user, user2, user3, user4};
        user3.SalesManager__c = user4.Id;
        update user3;
 
        agency_hospital_link.Name = 'test1代理店医院';
        agency_hospital_link.Hospital__c = account1.Id;
        agency_hospital_link.Agency__c = account2.Id;
        agency_hospital_link.OwnerId = user.Id;
        agency_hospital_link.Agency_Campaign_Obj__c = true;
        insert agency_hospital_link;
 
        ProductTypes__c productTypes = new ProductTypes__c();
        productTypes.Name ='290系统';
        productTypes.Name2__c ='290系统';
        productTypes.Department_Cateogy__c = 'GI;BF;GS';
        insert  productTypes;
 
        //Id rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Opportunity'].Id;
        Id rtId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
        // Schema.SobjectType.Agency_Opportunity__c.getRecordTypeInfosByName().get('询价').getRecordTypeId();
 
        Opportunity opp = new Opportunity();
        opp.Agency_Opportunity__c = agency_opportunity.Id;
        opp.Name = 'test询价';
        opp.StageName = '目標';
        //opp.AgencyOpp_FromOCM__c = true;
        opp.SAP_Send_OK__c = false;
        opp.CloseDate = Date.newInstance(2222, 7, 15);
        opp.OwnerId = user3.Id;
        insert opp;
 
        List<Opportunity> oppList = [SELECT Id,
                                            OwnerId
                                       FROM Opportunity];
 
        List<Contact> contactlist=[select Id,FirstName from Contact];
        System.assertEquals(1, oppList.size());
        System.assertEquals(user3.Id, oppList[0].OwnerId);
        agency_opportunity.Agency_Person__c = contactlist[0].id;
        agency_opportunity.RecordTypeId = rtId;
        agency_opportunity.Name = '引合1';
        agency_opportunity.Hospital_Target__c = account1.Id;
        agency_opportunity.Agency__c = account2.Id;
        agency_opportunity.Agency_Hospital__c = agency_hospital_link.Id;
        agency_opportunity.StageName__c = '还没申请预算';
        agency_opportunity.OwnerId = user4.Id;
        agency_opportunity.NewOpportunity_Agency_Apply_Status__c = '批准';
        agency_opportunity.OCM_Change_To_Opportunity_Flg__c = true;
        agency_opportunity.Department_Cateogy__c = 'GI';
        agency_opportunity.Product_Category1_P__c ='290系统';
        agency_opportunity.Change_To_Opportunity__c = opp.Id;
        //agency_opportunity.Product_Category2_P__c ='170系统';
        //agency_opportunity.Product_Category3_P__c ='170系统';
        agency_opportunity.Agency_ID__c = '0011000000V9RIs';
        //agency_opportunity.Agency_Hospital_Target__c = agency_hospital_link.Id;
 
        agency_opportunity.Sales_Manager__c = user.Id;
        insert agency_opportunity;
 
        List<Agency_Opportunity__c> aoList = [SELECT Id,
                                                     OwnerId,
                                                     Sales_Manager_F__c,
                                                     Sales_Manager__c
                                                FROM Agency_Opportunity__c];
        List<User> userList = [SELECT Id,
                                      SalesManager__c
                                 FROM User
                                WHERE Id = :user3.Id];
        System.assertEquals(1, aoList.size());
        System.assertEquals(userList[0].Id, aoList[0].OwnerId);
        System.assertEquals(userList[0].SalesManager__c, aoList[0].Sales_Manager_F__c);
        //System.assertEquals(aoList[0].Sales_Manager_F__c, aoList[0].Sales_Manager__c);
    }
 
    public static void makeNormalData(boolean withAgencyDetail) {
        ControllerUtil.EscapeNFM001Trigger = true;
        ControllerUtil.EscapeMaintenanceContractAfterUpdateTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger2 = true;
        StaticParameter.EscapeMaintenanceContractAfterUpdateTrigger = true;
        StaticParameter.EscapeAccountTrigger = true;
        StaticParameter.EscapeNFM007Trigger = true;
        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
        StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;
        System.runAs(new User(Id = UserInfo.getUserId())) {
            // create 病院
            hospital = new Account();
            hospital.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId();
            hospital.Name = 'test hospital_1';
            // create 经销商
            agency1 = new Account();
            agency1.Name = 'test1经销商';
            agency1.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            agency2 = new Account();
            agency2.Name = 'test2经销商';
            agency2.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            agency3 = new Account();
            agency3.Name = 'test3经销商';
            agency3.RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            insert new List<Account> {hospital, agency1, agency2,agency3};
            List<Account> agencyList = [SELECT Name, Management_Code__c
                    FROM Account WHERE (Id = :agency1.Id OR Id = :agency2.Id)
                    ORDER BY ID];
            oldAgencyCode = agencyList[0].Management_Code__c;
            oldAgencyId15 = ('' + agencyList[0].Id).mid(0, 15);
            newAgencyCode = agencyList[1].Management_Code__c;
            newAgencyId15 = ('' + agencyList[1].Id).mid(0, 15);
 
            // 取引先責任者
            contact4 = new Contact();
            contact4.AccountId = agency1.Id;
            contact4.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact4.FirstName = '責任者';
            contact4.LastName = 'test1经销商';
            contact4.Agency_User__c = true;
            Contact contact12 = new Contact();
            contact12.AccountId = agency1.Id;
            contact12.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact12.FirstName = '社員2';
            contact12.LastName = 'test1经销商';
            contact12.Agency_User__c = true;
            contact5 = new Contact();
            contact5.AccountId = agency2.Id;
            contact5.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact5.FirstName = '責任者';
            contact5.LastName = 'test2经销商';
            contact5.Agency_User__c = true;
            Contact contact22 = new Contact();
            contact22.AccountId = agency2.Id;
            contact22.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact22.FirstName = '社員2';
            contact22.LastName = 'test1经销商';
            contact22.Agency_User__c = true;
            Contact contact23 = new Contact();
            contact23.AccountId = agency2.Id;
            contact23.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact23.FirstName = '社員3';
            contact23.LastName = 'test2经销商';
            contact23.Agency_User__c = true;
 
            Contact contact3 = new Contact();
            contact3.AccountId = agency3.Id;
            contact3.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
            contact3.FirstName = '社員3';
            contact3.LastName = 'test2经销商';
            contact3.Agency_User__c = true;
            insert new List<Contact> {contact4, contact12, contact5, contact22, contact23,contact3};
            // User (link to ContactId)
            Profile p = [SELECT Id FROM Profile WHERE Name = '901_经销商活动系统'];
            user1 = new User();
            user1.ProfileId = p.Id;
            user1.ContactId = contact4.Id;
            user1.FirstName = 'ユーザー';
            user1.LastName = 'テスト';
            user1.Email = 'test_user1@example.com';
            user1.Username = 'test_user1@sb1.example.com' + System.now().millisecond();
            user1.Alias = 'テユa1';
            user1.CommunityNickname = 'tu1' + System.now().millisecond();
            user1.PortalRole = 'Worker';
            user1.emailencodingkey='UTF-8';
            user1.languagelocalekey='zh_CN';
            user1.localesidkey='ja_JP';
            user1.timezonesidkey='Asia/Shanghai';
            user2 = new User();
            user2.ProfileId = p.Id;
            user2.ContactId = contact5.Id;
            user2.FirstName = 'ユーザー';
            user2.LastName = 'テスト';
            user2.Email = 'test_user2@example.com';
            user2.Username = 'test_user2@sb1.example.com' + System.now().millisecond();
            user2.Alias = 'テユa2';
            user2.CommunityNickname = 'tu2' + System.now().millisecond();
            user2.PortalRole = 'Worker';
            user2.emailencodingkey='UTF-8';
            user2.languagelocalekey='zh_CN';
            user2.localesidkey='ja_JP';
            user2.timezonesidkey='Asia/Shanghai';
 
            user3 = new User();
            user3.ProfileId = p.Id;
            user3.ContactId = contact3.Id;
            user3.FirstName = 'ユーザー';
            user3.LastName = 'テスト';
            user3.Email = 'test_user3@example.com';
            user3.Username = 'test_user3@sb1.example.com' + System.now().millisecond();
            user3.Alias = 'テユa3';
            user3.CommunityNickname = 'tu3' + System.now().millisecond();
            user3.PortalRole = 'Worker';
            user3.emailencodingkey='UTF-8';
            user3.languagelocalekey='zh_CN';
            user3.localesidkey='ja_JP';
            user3.timezonesidkey='Asia/Shanghai';
            insert new List<User> {user1, user2,user3};
            user2 = [SELECT FirstName, LastName, ContactId, Contact.AccountId, UserRoleId FROM User WHERE Id = :user2.Id];
            System.assertEquals(agency2.Id, user2.Contact.AccountId);
            UserRole rolesAcc = [SELECT Id, Name, PortalAccountId, PortalRole
                    FROM UserRole
                    WHERE PortalAccountId = :agency2.Id
                    AND PortalRole = 'Worker'];
            UserRole rolesUser = [SELECT Id, Name, PortalAccountId, PortalRole
                    FROM UserRole
                    WHERE Id = :user2.UserRoleId];
            System.assertEquals(rolesAcc.PortalAccountId, rolesUser.PortalAccountId);
            System.assertEquals(rolesAcc.PortalRole, rolesUser.PortalRole);
            System.assertEquals(rolesAcc.Name, rolesUser.Name);
            System.assertEquals(rolesAcc.Id, rolesUser.Id);
        }
        System.runAs(new User(Id = UserInfo.getUserId())) {
            // UserにPermissionSetを設定
            PermissionSet ps = [SELECT ID FROM PermissionSet WHERE Name = 'P002_Partner'];
            insert new List<PermissionSetAssignment> {
                new PermissionSetAssignment(AssigneeId = user1.id, PermissionSetId = ps.Id),
                new PermissionSetAssignment(AssigneeId = user2.id, PermissionSetId = ps.Id)
            };
        }
 
        System.runAs(new User(Id = UserInfo.getUserId())) {
            // 营销关系
            a1HPLink = new Agency_Hospital_Link__c();
            a1HPLink.Name = 'test1经销商医院';
            a1HPLink.Hospital__c = hospital.Id;
            a1HPLink.Agency__c = agency1.Id;
            a1HPLink.Agency_Campaign_Obj__c = true;
            insert a1HPLink;
            a2HPLink = new Agency_Hospital_Link__c();
            a2HPLink.Name = 'test2经销商医院';
            a2HPLink.Hospital__c = hospital.Id;
            a2HPLink.Agency__c = agency2.Id;
            a2HPLink.Agency_Campaign_Obj__c = true;
            insert a2HPLink;
 
            a3HPLink = new Agency_Hospital_Link__c();
            a3HPLink.Name = 'test2经销商医院01';
            a3HPLink.Hospital__c = hospital.Id;
            a3HPLink.Agency__c = agency3.Id;
            a3HPLink.Agency_Campaign_Obj__c = true;
            insert a3HPLink;
 
            a3HPLink.Agency_Campaign_Obj__c = false;
            update a3HPLink; 
 
            if (withAgencyDetail) {
                //.客户人员 (agency1)
                Agency_Contact__c aContact1 = new Agency_Contact__c();
                aContact1.Agency_Hospital__c = a1HPLink.Id;
                aContact1.Type__c = '医生';
                aContact1.Name = 'testAgency医生';
                aContact1.OwnerId = user1.Id;
                Agency_Contact__c aContact2 = new Agency_Contact__c();
                aContact2.Agency_Hospital__c = a1HPLink.Id;
                aContact2.Type__c = '护士';
                aContact2.Name = 'testAgency护士';
                aContact2.OwnerId = user1.Id;
                insert new List<Agency_Contact__c> {aContact1, aContact2};
 
                // 经销商询价 (agency1)
                Agency_Opportunity__c agency_opportunity1 = new Agency_Opportunity__c();
                agency_opportunity1.RecordTypeId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
                agency_opportunity1.Name = '引合1';
                agency_opportunity1.Hospital_Target__c = hospital.Id;
                agency_opportunity1.Agency__c = agency1.Id;
                agency_opportunity1.Agency_Hospital__c = a1HPLink.Id;
                agency_opportunity1.StageName__c = '还没申请预算';
                agency_opportunity1.NewOpportunity_Agency_Apply_Status__c = '批准';
                agency_opportunity1.OwnerId = user1.Id;
                Agency_Opportunity__c agency_opportunity2 = new Agency_Opportunity__c();
                agency_opportunity2.RecordTypeId = Schema.SObjectType.Agency_Opportunity__c.getRecordTypeInfosByDeveloperName().get('Opportunity').getRecordTypeId();
                agency_opportunity2.Name = '引合2';
                agency_opportunity2.Hospital_Target__c = hospital.Id;
                agency_opportunity2.Agency__c = agency1.Id;
                agency_opportunity2.Agency_Hospital__c = a1HPLink.Id;
                agency_opportunity2.StageName__c = '还没申请预算';
                agency_opportunity2.NewOpportunity_Agency_Apply_Status__c = '批准';
                agency_opportunity2.OwnerId = user1.Id;
                insert new List<Agency_Opportunity__c> {agency_opportunity1, agency_opportunity2};
            }
        }
    }
}