高章伟
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
@isTest //TODO resend unit test
private class NFM104WebServiceTest {
 
    public static Integer count = 0;
 
    // 使用testSetup定义初始化函数
    @testSetup 
    static void setup() {
        List<RecordType> rects   = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account'];
        List<RecordType> rectCo  = new List<RecordType>();
        List<RecordType> rectSct = new List<RecordType>();
        List<RecordType> rectDpt = new List<RecordType>();
        List<RecordType> rectAg  = new List<RecordType>();
        for(RecordType rect : rects){
            if('病院'.equals(rect.Name)){
                rectCo.add(rect);
            }
            if('戦略科室分類 消化科'.equals(rect.Name)){
                rectSct.add(rect);
            }
            if('診療科 消化科'.equals(rect.Name)){
                rectDpt.add(rect);
            }
            if('販売店'.equals(rect.Name)){
                rectAg.add(rect);
            }
        }
        if (rectCo.size() == 0) {
            return;
        }
        if (rectSct.size() == 0) {
            return;
        }
        if (rectDpt.size() == 0) {
            return;
        }
       if (rectAg.size() == 0) {
            return;
        }        
        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM104TestCompany';
        upsert company;
 
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = 'NFM104TestSection';
        section.ParentId     = company.Id;
        upsert section;
        list<Account> sections = [Select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];
        section = sections[0];
        
        Account depart = new Account();
        depart.RecordTypeId        = rectDpt[0].Id;
        depart.Name                = '*';
        depart.Department_Name__c  = 'NFM104TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;
        Account agency1 = new Account(name='Testhosp001',AgentCode_Ext__c = '0000021598', Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        upsert agency1;
        // 再取得
        List<Account> accList = new List<Account>();
        company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id];
        accList.add(company);
        accList.add(section);
        depart = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart.Id];
        accList.add(depart);
 
        List<Product2> prdList = new List<Product2>();
        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'NFM104Prd1';
        prd1.ProductCode            = 'NFM104Prd1';
        prd1.Repair_Product_Code__c = 'NFM104Prd1_RP';
        prd1.Name                   = 'NFM104Prd1';
        prd1.Manual_Entry__c        = false;
        prdList.add(prd1);
        Product2 prd2 = new Product2();
        prd2.ProductCode_Ext__c     = 'NFM104Prd2';
        prd2.ProductCode            = 'NFM104Prd2';
        prd2.Repair_Product_Code__c = 'NFM104Prd2_RP';
        prd2.Name                   = 'NFM104Prd2';
        prd2.Manual_Entry__c        = false;
        prdList.add(prd2);
        insert prdList;
        System.Test.startTest();
        Asset ast = new Asset();
        ast.Name                             = 'NFM104Ast1';
        ast.AccountId                        = depart.Id;
        ast.Department_Class__c              = section.Id;
        ast.Hospital__c                      = company.Id;
        ast.Product2Id                       = prd1.Id;
        ast.SerialNumber                     = 'NFM104SerialNumber';
        ast.Guarantee_period_for_products__c = Date.today();
        ast.InstallDate                      = Date.today();
        insert ast;
        /*ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c, Department_Class__r.Management_Code_Auto__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber
                                 from Asset
                                where Id = :ast.Id];
        */
        Repair__c rpr = new Repair__c();
        rpr.SAPRepairNo__c         = 'NFM104Rpr1';
        rpr.Account__c             = depart.Id;
        rpr.Department_Class__c    = section.Id;
        rpr.Hospital__c            = company.Id;
        rpr.Delivered_Product__c   = ast.Id;
        rpr.SalesOfficeCode_selection__c = '北京';
        rpr.Status__c              = '1.受理完毕';
        rpr.Incharge_Staff__c = Userinfo.getUserId();
        rpr.Repair_Detail__c = 'test';
        insert rpr;
 
        System.Test.stopTest();   
    }
 
    static testMethod void testNFM104() {
        Repair__c rpr = [select Id, Name, Account__c,Department_Class__c,
            Delivered_Product__c,Hospital__c,SalesOfficeCode_selection__c,
             SAPRepairNo__c,Status__c,Incharge_Staff__c,Repair_Detail__c 
             from Repair__c];
 
        Repair_Quotation__c rq = new Repair_Quotation__c();
        rq.Name                  = 'testNFM104';
        rq.Repair__c             = rpr.Id;
        rq.MessageGroupNumber__c = '1';
        insert rq;
        // そのままreturn
        NFM104WebServiceTest.testNFM104_void();
       
        System.Test.startTest();
        //TODO insert
        NFM104WebServiceTest.testNFM104_insert(rpr);
        //TODO upsert
        //NFM104WebServiceTest.testNFM104_upsert();
        System.Test.stopTest();
    }
 
    static void testNFM104_void() {
        // null
        NFM104WebService.NFM104(null);
 
        NFM104WebService.RepairQuotation RepairQuotation                         = new NFM104WebService.RepairQuotation();
        NFM104WebService.QuotationHeaderInformation QuotationHeaderInformation   = new NFM104WebService.QuotationHeaderInformation();
        NFM104WebService.DiscountInformation DiscountInformation                 = new NFM104WebService.DiscountInformation();
        NFM104WebService.InspectionResultInformation InspectionResultInformation = new NFM104WebService.InspectionResultInformation();
        NFM104WebService.FailureInformation FailureInformation                   = new NFM104WebService.FailureInformation();
        NFM104WebService.ActivityInformation ActivityInformation                 = new NFM104WebService.ActivityInformation();
 
        QuotationHeaderInformation.DiscountInformation         = new NFM104WebService.DiscountInformation[] { DiscountInformation };
        QuotationHeaderInformation.InspectionResultInformation = new NFM104WebService.InspectionResultInformation[] { InspectionResultInformation };
        QuotationHeaderInformation.FailureInformation          = new NFM104WebService.FailureInformation[] { FailureInformation };
        QuotationHeaderInformation.ActivityInformation         = new NFM104WebService.ActivityInformation[] { ActivityInformation };
        RepairQuotation.QuotationHeaderInformation             = new NFM104WebService.QuotationHeaderInformation[] { QuotationHeaderInformation };
 
        // Monitoringをセットしない
        NFM104WebService.NFM104(RepairQuotation);
 
        Datetime nowDT = Datetime.now();
        String nowStr  = nowDT.format('yyyyMMddHHmm');
        RepairQuotation.Monitoring = new NFMUtil.Monitoring();
        RepairQuotation.Monitoring.MessageGroupNumber = nowStr + count + '1';
        // 必須項目をセットしない
        NFM104WebService.NFM104(RepairQuotation);
 
        // 商品コードが存在しない
        RepairQuotation.Monitoring.MessageGroupNumber = nowStr + count + '2';
        count++;
        QuotationHeaderInformation.SAPQuotationNo = 'NFM104X';
        NFM104WebService.NFM104(RepairQuotation);
 
        Repair_Quotation__c[] rslts = [select Id from Repair_Quotation__c where Name = :QuotationHeaderInformation.SAPQuotationNo];
        System.assertEquals(0, rslts.size());
    }
 
    static void testNFM104_insert(Repair__c rpr) {
        NFM104WebService.RepairQuotation RepairQuotation                         = new NFM104WebService.RepairQuotation();
        NFM104WebService.QuotationHeaderInformation QuotationHeaderInformation   = new NFM104WebService.QuotationHeaderInformation();
        NFM104WebService.DiscountInformation DiscountInformation                 = new NFM104WebService.DiscountInformation();
        NFM104WebService.InspectionResultInformation InspectionResultInformation = new NFM104WebService.InspectionResultInformation();
        NFM104WebService.FailureInformation FailureInformation                   = new NFM104WebService.FailureInformation();
        NFM104WebService.FailureInformation FailureInformation2                  = new NFM104WebService.FailureInformation();
        NFM104WebService.ActivityInformation ActivityInformation                 = new NFM104WebService.ActivityInformation();
 
        QuotationHeaderInformation.DiscountInformation         = new NFM104WebService.DiscountInformation[] { DiscountInformation };
        QuotationHeaderInformation.InspectionResultInformation = new NFM104WebService.InspectionResultInformation[] { InspectionResultInformation };
        QuotationHeaderInformation.FailureInformation          = new NFM104WebService.FailureInformation[] { FailureInformation };
        QuotationHeaderInformation.ActivityInformation         = new NFM104WebService.ActivityInformation[] { ActivityInformation };
        RepairQuotation.QuotationHeaderInformation             = new NFM104WebService.QuotationHeaderInformation[] { QuotationHeaderInformation };
 
        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        RepairQuotation.Monitoring = new NFMUtil.Monitoring();
        RepairQuotation.Monitoring.MessageGroupNumber = nowStr + count + '1';
        // insert
        QuotationHeaderInformation.SFDCRepairNo          = rpr.Name;
        QuotationHeaderInformation.SAPRepairNo           = rpr.SAPRepairNo__c;
        QuotationHeaderInformation.SAPQuotationNo        = 'NFM104I';
        QuotationHeaderInformation.FirstQuotationDate    = '20140101';
        QuotationHeaderInformation.LatestQuotationDate   = '20140101';
        QuotationHeaderInformation.TotalPrice            = '64300.34';
        QuotationHeaderInformation.RepairRank            = 'A';
        QuotationHeaderInformation.CafeteriaFlag         = '0000021598';
 
        DiscountInformation.DiscountType                 = '11';
        DiscountInformation.DiscountAmount               = '32000.12';
 
        InspectionResultInformation.InspectionResultFlag = '0000021598';
        InspectionResultInformation.InspectionComment    = 'CCD线长度: 248MM  把手、S盖脱漆、磨损,旋纽划痕,吸引口、水气口、1号外皮轻磨损。';
        InspectionResultInformation.FailureCause         = '先端部金属磨损严重,插入部发黄结晶,角度不足,钳子口磨损,2、3号外皮变色,水气不畅,CCD玻璃缺损大于1/10,涂胶后无阴影。B级不修理故障:CCD玻璃缺损大于1/10';
 
        FailureInformation.LocationGroupName             = '内镜连接座部';
        FailureInformation.LocationName                  = 'LG导光杆部';
        FailureInformation.PhenomenonName                = 'IG导光束异常(红裂缝、折损、彩虹斑)';
        FailureInformation.CauseName                     = '劣化, 老化';
        FailureInformation.DescriptionName               = '由于送气・送水圆筒的刮痕导致气囊送水量不能达到规格值。';
 
        ActivityInformation.ActivityDescription          = '插入部护套更换';
 
        //NFM104WebService.NFM104(RepairQuotation);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(RepairQuotation.Monitoring, 'NFM104', RepairQuotation.QuotationHeaderInformation);
        if (String.isBlank(rowData.Log__c) == false) 
            NFM104WebService.execute(rowData.Id);
        rpr = [select Id, Name, Delivered_Product__c, Repair_Quotation_Id__c, SAPRepairNo__c from Repair__c]; 
        List<Repair_Quotation__c> quotationList = [select Id, Name, Repair_Rank__c, Repair__c
                                                          , InspectionComment__c, InspectionResultFlag__c
                                                          , InspectionFailureCause__c
                                                          , LatestQuotationDate__c
                                                          , FirstQuotationDate__c, TotalPrice__c
                                                          , sales_discount__c, maintenance_pj__c
                                                          , CafeteriaFlag__c, MessageGroupNumber__c
                                                     from Repair_Quotation__c
                                                    where MessageGroupNumber__c =: RepairQuotation.Monitoring.MessageGroupNumber
                                                      and Name = :QuotationHeaderInformation.SAPQuotationNo ];
        count++;
        //System.assertEquals(0, quotationList.size());
        if (quotationList.size() == 1) {
            System.assertEquals(QuotationHeaderInformation.SAPQuotationNo, quotationList[0].Name);
            System.assertEquals(NFMUtil.parseStr2Date(QuotationHeaderInformation.FirstQuotationDate), quotationList[0].FirstQuotationDate__c);
            System.assertEquals(NFMUtil.parseStr2Date(QuotationHeaderInformation.LatestQuotationDate),quotationList[0].LatestQuotationDate__c);
            System.assertEquals(QuotationHeaderInformation.RepairRank, quotationList[0].Repair_Rank__c);
            System.assertEquals(null, quotationList[0].maintenance_pj__c);
            System.assertEquals(InspectionResultInformation.InspectionResultFlag == 'X' ? 'OK' : 'NG', quotationList[0].InspectionResultFlag__c);
            System.assertEquals(InspectionResultInformation.InspectionComment, quotationList[0].InspectionComment__c);
            System.assertEquals(InspectionResultInformation.FailureCause, quotationList[0].InspectionFailureCause__c);
            System.assertEquals(RepairQuotation.Monitoring.MessageGroupNumber, quotationList[0].MessageGroupNumber__c);
            System.assertEquals(rpr.Id, quotationList[0].Repair__c);
            System.assertEquals(rpr.Repair_Quotation_Id__c, quotationList[0].Id);
            System.assertEquals(rpr.SAPRepairNo__c, QuotationHeaderInformation.SAPRepairNo);
            
            List<Repair_quotation_reason__c> qrsnList = [select ActivityDescription__c, Repair_quotation__c, LocationGroupName__c, LocationName__c, PhenomenonName__c, CauseName__c, DescriptionName__c from Repair_quotation_reason__c];
            System.assertEquals(2, qrsnList.size());
            System.assertEquals(quotationList[0].Id, qrsnList[0].Repair_quotation__c);
            System.assertEquals(ActivityInformation.ActivityDescription, qrsnList[0].ActivityDescription__c);
            System.assertEquals(quotationList[0].Id, qrsnList[1].Repair_quotation__c);
            //System.assertEquals(FailureInformation.LocationGroupName, qrsnList[1].LocationGroupName__c);
            System.assertEquals(FailureInformation.LocationName, qrsnList[1].LocationName__c);
            System.assertEquals(FailureInformation.PhenomenonName, qrsnList[1].PhenomenonName__c);
            System.assertEquals(FailureInformation.CauseName, qrsnList[1].CauseName__c);
            System.assertEquals(FailureInformation.DescriptionName, qrsnList[1].DescriptionName__c);
 
            List<Repair_reason__c> rrsnList = [select Repair__c, Repair_quotation__c, LocationGroupName__c, LocationName__c, PhenomenonName__c, CauseName__c, DescriptionName__c from Repair_reason__c];
            System.assertEquals(0, rrsnList.size());
        }
        
    }
 
}