高章伟
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
@isTest
private class MCLastMContractControllerTest {
 
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    static Asset createAsset(String input, String accountid, String dcId, String hpId, String serialNo, String prId,String lcId) {
        Asset asset = new Asset();
        asset.Name = input;
        asset.AccountId = accountid;
        asset.Department_Class__c = dcId; 
        asset.Hospital__c = hpId;
        asset.SerialNumber = serialNo;
        asset.Product2Id = prId;
        asset.CurrentContract_F__c = lcId;
        asset.InstallDate = Date.today();
        insert asset;
        return asset;
    }
 
    public class AssetInfo {
        public Integer lineNo {get; private set;}
        public Boolean rec_checkBox_c {get; set;}
        public Asset rec { get; set; }
        public Maintenance_Contract_Asset_Estimate__c mcae { get; set; }
        public Boolean isManual { get; set; }
        public Decimal orgPrice {get; private set;}
        public Id getRecId() {
            Id rtn = null;
            if (rec != null) {
                rtn = rec.Id;
            }
            return rtn;
        }
        public void setRecId(Id value) {
            // なにもしない
        }
        
        // Manual専用
        public AssetInfo(Integer lineNo) {
            this.lineNo = lineNo;
            this.rec = null;
            this.mcae = new Maintenance_Contract_Asset_Estimate__c(
                isNew__c = true
            );
            this.isManual = true;
            this.rec_checkBox_c = false;
            this.orgPrice = 0;
        }
    }
 
 
    static testMethod void testMethod1() {
        ControllerUtil.EscapeNFM001Trigger = true;
        User u3 = new User();
        u3.LastName = '_サンブリッジ';
        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 = System.Label.ProfileId_SystemAdmin;
        u3.Job_Category__c = '销售服务';
        u3.Province__c = '北京市';
        u3.Use_Start_Date__c = Date.today().addMonths(-6);
        insert u3;
 
        
        system.runAs(u3) {
            // 病院を作る
            
            Account hospital = new Account();
            hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
            hospital.Name = 'test hospital';
            insert hospital;
            
            // 戦略科室を得る
            List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
            StaticParameter.EscapeSyncProduct2Trigger = true;
            StaticParameter.EscapeAccountTrigger = true;
            StaticParameter.EscapeMaintenanceContractAfterUpdateTrigger = true;
            // 診療科を作る
            Account dep = new Account();
            dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'].id;
            dep.Name = 'test dep';
            dep.ParentId = strategicDep[0].Id;
            dep.Department_Class__c = strategicDep[0].Id;
            dep.Hospital__c = hospital.Id;
            insert dep;
            
            // 製品を作る
            Product2 productA = new Product2( Name='テスト商品', Maintenance_Price_Year__c = 12000, Manual_Entry__c = false);
            insert productA;
            
            // 価格表エントリを作成する        
            PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=productA.Id);
            entry.UnitPrice = 0;
            entry.IsActive = true;
            entry.UseStandardPrice = false;
            entry.CurrencyIsoCode = 'CNY';
            entry.Product2Id = productA.Id;
            insert entry;
 
            // 上期维修合同
            Maintenance_Contract__c lastcontract = new Maintenance_Contract__c();
            lastcontract.Name = 'tect contract';
            lastcontract.Not_Upper_limit_reason__c = 'tect contract';
            lastcontract.RecordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract__c' and DeveloperName = 'NewMaintenance_Contract'].id;
            lastcontract.Hospital__c = hospital.Id;
            lastcontract.Department_Class__c = strategicDep[0].Id;
            lastcontract.Department__c = dep.Id;
            lastcontract.Service_Contract_Staff__c = u3.Id;
            insert lastcontract;
            
            // 納入機器を作成する
            Asset asset01 = createAsset('asset01', dep.Id, strategicDep[0].Id, hospital.Id, '00001', productA.Id,lastcontract.Id);
            Asset asset02 = createAsset('asset02', dep.Id, strategicDep[0].Id, hospital.Id, '00002', productA.Id,lastcontract.Id);
            Asset asset03 = createAsset('asset03', dep.Id, strategicDep[0].Id, hospital.Id, '00003', productA.Id,lastcontract.Id);
            Asset asset04 = createAsset('asset04', dep.Id, strategicDep[0].Id, hospital.Id, '00004', productA.Id,lastcontract.Id);
            Asset asset05 = createAsset('asset05', dep.Id, strategicDep[0].Id, hospital.Id, '00005', productA.Id,lastcontract.Id);
            
            // 上期维修合同保有设备
            Maintenance_Contract_Asset__c lastmca1 = new Maintenance_Contract_Asset__c();
            lastmca1.Asset__c = asset01.Id;
            lastmca1.Estimate_List_Price__c = 100;
            lastmca1.Maintenance_Contract__c = lastcontract.Id;
            
            // insert new Maintenance_Contract_Asset__c[] {lastmca1};
            insert lastmca1;
 
 
            // 维修合同を作成する
            Maintenance_Contract__c contract = new Maintenance_Contract__c();
            contract.Name = 'tect contract';
            contract.Not_Upper_limit_reason__c = 'tect contract';
            contract.RecordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract__c' and DeveloperName = 'NewMaintenance_Contract'].id;
            contract.Hospital__c = hospital.Id;
            contract.Department_Class__c = strategicDep[0].Id;
            contract.Department__c = dep.Id;
            contract.LastMContract1__c = lastcontract.Id;
            contract.LastMContract1_ConCount__c = 5;
            contract.LastMContract2__c = lastcontract.Id;
            contract.LastMContract2_ConCount__c = 5;
            contract.LastMContract3__c = lastcontract.Id;
            contract.LastMContract3_ConCount__c = 5;
            contract.LastMContract4__c = lastcontract.Id;
            contract.LastMContract4_ConCount__c = 5;
            contract.LastMContract5__c = lastcontract.Id;
            contract.LastMContract5_ConCount__c = 5;
            contract.Service_Contract_Staff__c = u3.Id;
            insert contract;
 
            Maintenance_Contract_Asset__c mca1 = new Maintenance_Contract_Asset__c();
            mca1.Asset__c = asset01.Id;
            mca1.Estimate_List_Price__c = 100;
            mca1.Maintenance_Contract__c = contract.Id;
            mca1.LastMContract__c = lastcontract.Id;
            
            insert new Maintenance_Contract_Asset__c[] {mca1};
            
            // 维修合同报价を作成する
            Maintenance_Contract_Estimate__c contactEsti1 = new Maintenance_Contract_Estimate__c(
                Name = 'contract estimate 1',
                RecordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract_Estimate__c' and DeveloperName = 'NewMaintenance_Quote'].id,
                Contract_Esti_Start_Date__c = Date.today(),
                Contract_Range__c = 2,
                Maintenance_Contract__c = contract.Id,
                Request_quotation_Amount__c = 1,
                EndUserType__c = '既有用户',
                mainTalksTime__c = 1,
                talksStartDate__c = Date.today(),
                Discount_reason__c = 'test',
                Improve_ConsumptionRate_Idea__c = 'test',
                LastMContract1__c = lastcontract.Id,
                LastMContract1_ConCount__c = 5,
                LastMContract2__c = lastcontract.Id,
                LastMContract2_ConCount__c = 5,
                LastMContract3__c = lastcontract.Id,
                LastMContract3_ConCount__c = 5,
                LastMContract4__c = lastcontract.Id,
                LastMContract4_ConCount__c = 5,
                LastMContract5__c = lastcontract.Id,
                LastMContract5_ConCount__c = 5
            );
            insert contactEsti1;
            //维修合同报价/保有设备
            Maintenance_Contract_Asset_Estimate__c MCAE = new     Maintenance_Contract_Asset_Estimate__c(
                Maintenance_Contract_Estimate__c = contactEsti1.Id,
                CurrencyIsoCode = 'CNY',
                Asset__c = asset01.Id,
                Last_MContract__c = lastcontract.Id
            );
            insert MCAE;
 
            System.Test.startTest();
            // 新規
            
 
            Maintenance_Contract__c mc = [select id,LastMContract1__c,LastMContract2__c,LastMContract3__c,LastMContract4__c,LastMContract5__c FROM Maintenance_Contract__c WHERE id = :contract.Id];
            System.assertEquals(lastcontract.Id, mc.LastMContract1__c);
            System.assertEquals(lastcontract.Id, mc.LastMContract2__c);
            System.assertEquals(lastcontract.Id, mc.LastMContract3__c);
            System.assertEquals(lastcontract.Id, mc.LastMContract4__c);
            System.assertEquals(lastcontract.Id, mc.LastMContract5__c);
 
            
            // 维修合同报价1
            Apexpages.currentPage().getParameters().put('id', contactEsti1.Id);
            MCLastMContractController controller = new MCLastMContractController();
            controller.init();
            //System.assertEquals(5, controller.productCount2);
 
            System.Test.stopTest();
        }
    }
 
    // static testMethod void testMethod2(){            
    //     Account hospital = new Account();
    //     hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
    //     hospital.Name = 'test hospital';
    //     insert hospital;
    //     List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
    //     Account dep = new Account();
    //     dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'].id;
    //     dep.Name = 'test dep';
    //     dep.ParentId = strategicDep[0].Id;
    //     dep.Department_Class__c = strategicDep[0].Id;
    //     dep.Hospital__c = hospital.Id;
    //     insert dep;
    //     //1.创建当前合同
    //     Maintenance_Contract__c contract = new Maintenance_Contract__c();
    //     contract.Name = 'tect contract';
    //     contract.Not_Upper_limit_reason__c = 'tect contract';
    //     contract.RecordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract__c' and DeveloperName = 'NewMaintenance_Contract'].id;
    //     contract.Hospital__c = hospital.Id;
    //     contract.Department_Class__c = strategicDep[0].Id;
    //     contract.Department__c = dep.Id;
    //     insert contract;
 
    //     // 维修合同报价を作成する
    //         Maintenance_Contract_Estimate__c contactEsti1 = new Maintenance_Contract_Estimate__c(
    //             Name = 'contract estimate 1',
    //             RecordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract_Estimate__c' and DeveloperName = 'NewMaintenance_Quote'].id,
    //             Contract_Esti_Start_Date__c = Date.today(),
    //             Contract_Range__c = 2,
    //             Maintenance_Contract__c = contract.Id,
    //             Request_quotation_Amount__c = 1,
    //             EndUserType__c = '既有用户',
    //             mainTalksTime__c = 1,
    //             talksStartDate__c = Date.today(),
    //             Discount_reason__c = 'test',
    //             Improve_ConsumptionRate_Idea__c = 'test',
    //             // LastMContract1__c = lastcontract.Id,
    //             // LastMContract1_ConCount__c = 5,
    //             // LastMContract2__c = lastcontract.Id,
    //             // LastMContract2_ConCount__c = 5,
    //             // LastMContract3__c = lastcontract.Id,
    //             // LastMContract3_ConCount__c = 5,
    //             // LastMContract4__c = lastcontract.Id,
    //             // LastMContract4_ConCount__c = 5,
    //             // LastMContract5__c = lastcontract.Id,
    //             // LastMContract5_ConCount__c = 5
    //         );
    //         insert contactEsti1;
 
    //     Product2 productA = new Product2( Name='テスト商品', Maintenance_Price_Year__c = 12000, Manual_Entry__c = false);
    //     insert productA;
    //     Asset asset01 = createAsset('asset01', dep.Id, strategicDep[0].Id, hospital.Id, '00001', productA.Id,contract.Id);
    //     Asset asset02 = createAsset('asset02', dep.Id, strategicDep[0].Id, hospital.Id, '00002', productA.Id,contract.Id);
 
    //     //2.为当前合同下的保有设备创建上一期维修合同
    //     Maintenance_Contract__c lastcontract1 = new Maintenance_Contract__c();
    //     lastcontract1.Name = 'tect contract1';
    //     lastcontract1.Not_Upper_limit_reason__c = 'tect contract1';
    //     lastcontract1.RecordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract__c' and DeveloperName = 'NewMaintenance_Contract'].id;
    //     lastcontract1.Hospital__c = hospital.Id;
    //     lastcontract1.Department_Class__c = strategicDep[0].Id;
    //     lastcontract1.Department__c = dep.Id;
    //     lastcontract1.Management_Code__c = 'teste11111';
    //     lastcontract1.Contract_Start_Date__c = Date.today();
    //     lastcontract1.Contract_End_Date__c = Date.today();
    //     insert lastcontract1;
 
    //     Maintenance_Contract__c lastcontract2 = new Maintenance_Contract__c();
    //     lastcontract2.Name = 'tect contract2';
    //     lastcontract2.Not_Upper_limit_reason__c = 'tect contract2';
    //     lastcontract2.RecordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Maintenance_Contract__c' and DeveloperName = 'NewMaintenance_Contract'].id;
    //     lastcontract2.Hospital__c = hospital.Id;
    //     lastcontract2.Department_Class__c = strategicDep[0].Id;
    //     lastcontract2.Department__c = dep.Id;
    //     lastcontract2.Management_Code__c = 'teste22222';
    //     lastcontract2.Contract_Start_Date__c = Date.today();
    //     lastcontract2.Contract_End_Date__c = Date.today();
    //     insert lastcontract2;
 
    //     //3.为当前合同创建保有设备
    //     Maintenance_Contract_Asset__c mca1 = new Maintenance_Contract_Asset__c();
    //     mca1.Asset__c = asset01.Id;
    //     mca1.Estimate_List_Price__c = 100;
    //     mca1.Maintenance_Contract__c = contract.Id;
    //     mca1.LastMContract__c = lastcontract1.Id;
    //     insert mca1;
 
    //     Maintenance_Contract_Asset__c mca2 = new Maintenance_Contract_Asset__c();
    //     mca2.Asset__c = asset02.Id;
    //     mca2.Estimate_List_Price__c = 100;
    //     mca2.Maintenance_Contract__c = contract.Id;
    //     mca2.LastMContract__c = lastcontract1.Id;
    //     insert mca2;
 
 
    //     Maintenance_Contract_Asset__c mca3 = new Maintenance_Contract_Asset__c();
    //     mca3.Asset__c = asset02.Id;
    //     mca3.Estimate_List_Price__c = 100;
    //     mca3.Maintenance_Contract__c = contract.Id;
    //     // mca3.LastMContract__c = lastcontract1.Id;
    //     insert mca3;
 
 
    //     Maintenance_Contract_Asset__c mca4 = new Maintenance_Contract_Asset__c();
    //     mca4.Asset__c = asset01.Id;
    //     mca4.Estimate_List_Price__c = 100;
    //     mca4.Maintenance_Contract__c = contract.Id;
    //     mca3.LastMContract__c = lastcontract2.Id;
    //     insert mca4;
 
 
    //     Maintenance_Contract_Asset__c mca5 = new Maintenance_Contract_Asset__c();
    //     mca5.Asset__c = asset01.Id;
    //     mca5.Estimate_List_Price__c = 100;
    //     mca5.Maintenance_Contract__c = contract.Id;
    //     mca5.LastMContract__c = lastcontract2.Id;
    //     insert mca5;
 
    //     System.Test.startTest();
    //     Apexpages.currentPage().getParameters().put('id', contactEsti1.Id);
    //     MCLastMContractController controller = new MCLastMContractController();
    //     controller.init();
    //     System.Test.stopTest();
    // }
}