高章伟
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
@isTest
private class RepairPCLControllerTest {
 
    static testMethod void myUnitTest() {
        
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Job_Category__c = '销售服务', Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        // 取引先作成
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectHp.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科','診療科 普外科') order by Name desc];
        if (rectDpt.size() == 0) {
            return;
        }
        Account hp1 = new Account(RecordTypeId = rectHp[0].Id, Name = 'testHp1', OwnerId = hpOwner.Id);
        insert hp1;
 
        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp1.Id and Department_Class_Label__c in ('消化科', '呼吸科') order by Department_Class_Label__c];
        
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[1].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = hp1.Id;
        
        insert depart1;
        
        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'Prd1';
        prd1.ProductCode            = 'Prd1';
        prd1.Repair_Product_Code__c = 'Prd1_RP';
        prd1.Name                   = 'Prd1';
        prd1.Manual_Entry__c        = false;
        prd1.Category3__c = '';
        prd1.Category4__c = 'CV';
        
        insert prd1;
        
        Asset ast1 = new Asset();
        ast1.Name                   = '保有設備1';
        ast1.Hospital__c            = hp1.Id;
        ast1.Department_Class__c    = dc1s[0].Id;
        ast1.AccountId              = depart1.Id;
        ast1.Product2Id             = prd1.Id;
        ast1.SerialNumber           = 'SerialNumber1';
        ast1.Guarantee_period_for_products__c = Date.today();
        ast1.InstallDate                      = Date.today();
        
        insert ast1;
        
        Repair__c repair1 = new Repair__c();
        repair1.Service_Repair_No__c = 'repair1';
        repair1.Hospital__c            = hp1.Id;
        repair1.Department_Class__c    = dc1s[0].Id;
        repair1.Account__c             = depart1.Id;
        repair1.Delivered_Product__c = ast1.Id;
        repair1.Repair_List_Price__c = 100;
        repair1.Billing_Amount__c = 10;
        repair1.Paid_Amount__c = 1;
        repair1.Failure_Occurrence_Date__c = Date.today().addDays(-1);
        repair1.Repair_Returned_To_HP_Date__c = Date.today().addDays(3);
        repair1.Repair_Shipped_Date__c = Date.today().addDays(1);
        repair1.Incharge_Staff__c = hpOwner.Id;
        
        Repair__c repair2 = new Repair__c();
        repair2.Service_Repair_No__c = 'repair2';
        repair2.Hospital__c            = hp1.Id;
        repair2.Department_Class__c    = dc1s[0].Id;
        repair2.Account__c             = depart1.Id;
        repair2.Delivered_Product__c = ast1.Id;
        repair2.Repair_List_Price__c = 200;
        repair2.Billing_Amount__c = 20;
        repair2.Paid_Amount__c = 2;
        repair2.Failure_Occurrence_Date__c = Date.today().addDays(-1);
        repair2.Repair_Returned_To_HP_Date__c = Date.today().addDays(5);
        repair2.Repair_Shipped_Date__c = Date.today().addDays(3);
        repair2.Incharge_Staff__c = hpOwner.Id;
        
        System.Test.startTest();
        insert new Repair__c[] {repair1,repair2};
        System.Test.stopTest();
        
        PageReference page = new PageReference('/apex/RepairPCL');
        System.Test.setCurrentPage(page);
        RepairPCLController controller = new RepairPCLController();
        // 初始化测试
        controller.init();
        
        System.assertEquals(2, controller.oppRecords.size());
        
        // 排序测试
        controller.sortKey = '1';
        controller.sortTable();
        controller.sortTable();
        
        // 检索测试
        controller.text = 'S:HP_Name__c';
        controller.condition = 'equals';
        controller.value = 'testHp1';
        controller.searchOpp();
        controller.condition = 'contains';
        controller.value = 'test 1';
        controller.searchOpp();
        
        System.assertEquals(2, controller.oppRecords.size());
        
        // 保存测试
        controller.oppRecords[0].changeFlg = '1';
        controller.save();
        
        // 返回测试
        controller.cancel();
    }
    
    static testMethod void myUnitTest2() {
        
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Job_Category__c = '销售服务', Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        // 取引先作成
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectHp.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科','診療科 普外科') order by Name desc];
        if (rectDpt.size() == 0) {
            return;
        }
        Account hp1 = new Account(RecordTypeId = rectHp[0].Id, Name = 'testHp1', OwnerId = hpOwner.Id);
        insert hp1;
 
        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp1.Id and Department_Class_Label__c in ('消化科', '呼吸科') order by Department_Class_Label__c];
        
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[1].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = hp1.Id;
        
        insert depart1;
        
        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'Prd1';
        prd1.ProductCode            = 'Prd1';
        prd1.Repair_Product_Code__c = 'Prd1_RP';
        prd1.Name                   = 'Prd1';
        prd1.Manual_Entry__c        = false;
        prd1.Category3__c = '';
        prd1.Category4__c = 'CV';
        
        insert prd1;
        
        Asset ast1 = new Asset();
        ast1.Name                   = '保有設備1';
        ast1.Hospital__c            = hp1.Id;
        ast1.Department_Class__c    = dc1s[0].Id;
        ast1.AccountId              = depart1.Id;
        ast1.Product2Id             = prd1.Id;
        ast1.SerialNumber           = 'SerialNumber1';
        ast1.Guarantee_period_for_products__c = Date.today();
        ast1.InstallDate                      = Date.today();
        
        insert ast1;
        
        Repair__c repair1 = new Repair__c();
        repair1.Service_Repair_No__c = 'repair1';
        repair1.Hospital__c            = hp1.Id;
        repair1.Department_Class__c    = dc1s[0].Id;
        repair1.Account__c             = depart1.Id;
        repair1.Delivered_Product__c = ast1.Id;
        repair1.Repair_List_Price__c = 100;
        repair1.Billing_Amount__c = 10;
        repair1.Paid_Amount__c = 1;
        repair1.Failure_Occurrence_Date__c = Date.today().addDays(-1);
        repair1.Repair_Returned_To_HP_Date__c = Date.today().addDays(3);
        repair1.Repair_Shipped_Date__c = Date.today().addDays(1);
        repair1.Incharge_Staff__c = hpOwner.Id;
        
        Repair__c repair2 = new Repair__c();
        repair2.Service_Repair_No__c = 'repair2';
        repair2.Hospital__c            = hp1.Id;
        repair2.Department_Class__c    = dc1s[0].Id;
        repair2.Account__c             = depart1.Id;
        repair2.Delivered_Product__c = ast1.Id;
        repair2.Repair_List_Price__c = 200;
        repair2.Billing_Amount__c = 20;
        repair2.Paid_Amount__c = 2;
        repair2.Failure_Occurrence_Date__c = Date.today().addDays(-1);
        repair2.Repair_Returned_To_HP_Date__c = Date.today().addDays(5);
        repair2.Repair_Shipped_Date__c = Date.today().addDays(3);
        repair2.Incharge_Staff__c = hpOwner.Id;
        
        System.Test.startTest();
        insert new Repair__c[] {repair1,repair2};
        System.Test.stopTest();
        
        PageReference page = new PageReference('/apex/RepairPCL?uid=' + UserInfo.getUserId() + '&hpid=' + hp1.Id);
        System.Test.setCurrentPage(page);
        RepairPCLController controller = new RepairPCLController();
        // 初始化测试
        controller.init();
        
        System.assertEquals(2, controller.oppRecords.size());
        
        // 保存测试
        controller.oppRecords[0].changeFlg = '1';
        controller.saveType = '1';
        controller.save();
        
        controller.oppRecords[0].changeFlg = '1';
        controller.saveType = '2';
        controller.save();
        
        // 检索测试
        controller.text = 'S:HP_Name__c';
        controller.condition = 'notequals';
        controller.value = '';
        controller.searchOpp();
        
        controller.dateField = 'Failure_Occurrence_Date__c';
        controller.con1.Birthdate = Date.today().addDays(-2);
        controller.con2.Birthdate = Date.today();
        controller.text = 'S:HP_Name__c';
        controller.condition = 'starts with';
        controller.value = 'test';
        controller.searchOpp();
        
        System.assertEquals(2, controller.oppRecords.size());
    }
}