liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an org are executed whenever Apex code is deployed
 * to a production org to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production org. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the org size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class OPDAutomaticSortBeforeBatchTest {
 
    static testMethod void testMethod1() {
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        Oly_TriggerHandler.bypass(ContactTriggerHandler.class.getName());
        Oly_TriggerHandler.bypass(AgencyHospitalHandler.class.getName());
        Oly_TriggerHandler.bypass(AssetHandler.class.getName());
        Date toDate = Date.today();
        Boolean userProfileCheck = false;
        // 获取从本月14日至次月13日的日期
        Date startDate = toDate.toStartOfMonth().addDays(13);
        Date endDate = toDate.addMonths(1).toStartOfMonth().addDays(12);
        
        opp2AuxiliarySort__c oas1 = new opp2AuxiliarySort__c();
        oas1.Name = '北京市';
        oas1.type__c = 1;
        insert oas1;
        // opp2AuxiliarySort__c oas2 = new opp2AuxiliarySort__c();
        // oas2.Name = '山东';
        // oas2.type__c = 1;
        // insert oas2;
        // opp2AuxiliarySort__c oas3 = new opp2AuxiliarySort__c();
        // oas3.Name = '河北';
        // oas3.type__c = 1;
        // insert oas3;
        // opp2AuxiliarySort__c oas4 = new opp2AuxiliarySort__c();
        // oas4.Name = '上海';
        // oas4.type__c = 1;
        // insert oas4;
 
        // opp2AuxiliarySort__c oas5 = new opp2AuxiliarySort__c();
        // oas5.Name = '1.华北';
        // oas5.type__c = 2;
        // insert oas5;
        Opportunity opp = new Opportunity(
        Name='Opp',
        StageName='引合',
        CloseDate=Date.today().addDays(10),
        Close_Forecasted_Date__c=Date.today(),
            CurrencyIsoCode = 'CNY'
        );
        insert opp;
        Statu_Achievements__c Sac = new Statu_Achievements__c(
            name = 'zhucan_one',
            Opportunity__c = opp.id,
            DeliveryDate__c = Date.today(),
            ContractNO__c = 'ContractNO1',
            ContractAmount__c = 1000
        );
        insert Sac;
        
        RecordType rectCam =
        [SELECT Id FROM RecordType
         WHERE IsActive = true AND SobjectType = 'Campaign'
                          AND DeveloperName = 'ServiceEngineerTraining'];
        Campaign cam = new Campaign();
        cam.Name = 'cam';
        cam.Name2__c = '1234';
        cam.RecordTypeId = rectCam.Id;
        cam.StartDate = startDate.addDays(10);
        cam.EndDate = startDate.addDays(15);
        cam.Mailflg_after45__c = true;
        cam.Mailflg_cancel__c = true;
        cam.Mailflg_before15__c = true;
        cam.Mailflg_before7__c = true;
        cam.Mailflg_after3__c = true;
        insert cam;
 
 
        Address_Level__c al = new Address_Level__c();
        al.Name = '北京市';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '北京市';
        al2.Name = '朝阳区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        insert al2;
 
 
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        hospital.Is_Active__c = '有効';
        hospital.Attribute_Type__c = '卫生部';
        hospital.Speciality_Type__c = '综合医院';
        hospital.Grade__c = '一级';
        hospital.OCM_Category__c = 'SLTV';
        hospital.Is_Medical__c = '医疗机构';
        hospital.State_Master__c = al.id;
        hospital.City_Master__c = al2.id;
        hospital.Town__c = '北京';
        insert hospital;
        Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_OTH'];
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;
        insert dep;
 
        Contact contact2 = new Contact();
        contact2.AccountId = dep.Id;
        contact2.RecordTypeId = Schema.SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId();
        contact2.FirstName = '責任者';
        contact2.LastName = 'test1经销商';
        contact2.Agency_User__c = true;
        insert contact2;
        // システム管理者
        User user = new User(Test_staff__c = true);
        user.LastName = '_サンブリッジ';
        user.FirstName = 'う';
        user.Alias = 'う';
        user.Email = 'olympusTest03@sunbridge.com';
        user.Username = 'olympusTest03@sunbridge.com';
        user.CommunityNickname = 'う';
        user.IsActive = true;
        user.EmailEncodingKey = 'ISO-2022-JP';
        user.TimeZoneSidKey = 'Asia/Tokyo';
        user.LocaleSidKey = 'ja_JP';
        user.LanguageLocaleKey = 'ja';
        user.ProfileId = System.Label.ProfileId_SystemAdmin;
        user.Job_Category__c = '销售推广';
        user.Province__c = '北京市';
        user.Dept__c='医疗华北营业本部';
        user.Use_Start_Date__c = Date.today().addMonths(-6);
        user.SalesManager__c = UserInfo.getUserId();
        user.BuchangApprovalManagerSales__c = UserInfo.getUserId();
        user.JingliApprovalManager__c = UserInfo.getUserId();
        user.BuchangApprovalManager__c = UserInfo.getUserId();
        user.ZongjianApprovalManager__c = UserInfo.getUserId();
 
        // MIXED_DML_OPERATION, DML operation on setup object is not permitted Error
        System.runAs(new User(Id = Userinfo.getUserId())) {
            Date nowday = Date.today();
            OPDPlan__c oPDPlan = new OPDPlan__c();
            oPDPlan.Status__c = '计划中';
            oPDPlan.OPDPlan_ImplementDate__c =  Date.today().addMonths(1).toStartOfMonth().addDays(20);
            oPDPlan.OPDType__c = '询价';
            oPDPlan.Related_Opportunity1_ID__c = opp.Id;
            oPDPlan.NoOpp_Reason__c = null;
            // oPDPlan.OPDPlan_ImplementDate__c = startDate.addDays(3);
            oPDPlan.Campaign__c = cam.Id;
            oPDPlan.PlanProdDetail__c = '1000*1;';
            oPDPlan.If_AutoSort__c = 0;
            oPDPlan.ApprovePassed__c = nowday;
            insert oPDPlan;
 
            OPDPlan__c oPDPlan2 = new OPDPlan__c();
            oPDPlan2.Status__c = '计划中';
            oPDPlan2.OPDPlan_ImplementDate__c =  Date.today().addMonths(1).toStartOfMonth().addDays(15);
 
            oPDPlan2.OPDType__c = '询价';
            oPDPlan2.Related_Opportunity1_ID__c = opp.Id;
            oPDPlan2.NoOpp_Reason__c = null;
            // oPDPlan2.OPDPlan_ImplementDate__c = startDate;
            oPDPlan2.PlanProdDetail__c = '1000*1;';
            oPDPlan2.If_AutoSort__c = 0;
            oPDPlan2.ApprovePassed__c = nowday;
            insert oPDPlan2;
 
            OPDPlan__c oPDPlan3 = new OPDPlan__c();
            oPDPlan3.Status__c = '计划中';
            oPDPlan3.OPDPlan_ImplementDate__c =  Date.today().addMonths(1).toStartOfMonth().addDays(22);
            oPDPlan3.OPDType__c = '询价';
            oPDPlan3.Related_Opportunity1_ID__c = opp.Id;
            oPDPlan3.NoOpp_Reason__c = null;
            // oPDPlan3.OPDPlan_ImplementDate__c = startDate;
            oPDPlan3.PlanProdDetail__c = '1000*1;';
            oPDPlan3.If_AutoSort__c = 0;
            oPDPlan3.ApprovePassed__c = nowday;
 
            insert oPDPlan3;
            Database.executeBatch(new OPDAutomaticSortBeforeBatch(true),1);
            Database.executeBatch(new OPDAutomaticSortBeforeBatch(Date.today().addDays(-1)),1);
            List<String> ids = new List<String>();
            ids.add(oPDPlan.id);
            ids.add(oPDPlan2.id);
            ids.add(oPDPlan3.id);
            Database.executeBatch(new OPDAutomaticSortBeforeBatch(ids),1);
            Database.executeBatch(new OPDAutomaticSortBeforeBatch(true,Date.today().addDays(-1)),1);
        }
    }
}