GWY
2022-05-21 a3460549533111815e7f73d6cef601a58031525d
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
@ isTest (SeeAllData = true)
private class OpportunityProductControllerTest {
 
 
    static testMethod void myUnitTest() {
        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        List<RecordType> rectOpp = [select Id from RecordType where IsActive = true and SobjectType = 'Opportunity' and Name = 'SSBD'];
        Id pricebookId = Test.getStandardPricebookId();
        
        Pricebook2 pricebook = new Pricebook2(
            Name = 'IE',
            ProductSegment__c = 'IE',
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            MachineParts__c = 'Machine',
            CurrencyIsoCode = 'CNY'
        );
        insert pricebook;
        
        Product2 product1 = new Product2();
        product1.Name = 'product1';
        product1.IsActive = true;
        product1.ProductCode = 'product1';
        product1.ProductStatus__c = '1';
        product1.NMPAStatus_one__c = 'Z1';
        product1.MaterialStatus_one_Start__c = Date.today().addDays(-22);
        product1.MaterialStatus_one_End__c = Date.today().addDays(22);
        
        Product2 product2 = new Product2();
        product2.Name = 'product2';
        product2.ProductStatus__c = '1';
        product2.IsActive = true;
        product2.ProductCode = 'product2';
        product2.NMPAStatus_one__c = 'Z1';
        product2.MaterialStatus_one_Start__c = Date.today().addDays(-22);
        product2.MaterialStatus_one_End__c = Date.today().addDays(22);
        
        Product2 product3 = new Product2();
        product3.Name = 'product3';
        product3.ProductStatus__c  = '1';
        product3.IsActive = true;
        product3.ProductCode = 'product3';
        product3.NMPAStatus_one__c = 'Z1';
        product3.MaterialStatus_one_Start__c = Date.today().addDays(-22);
        product3.MaterialStatus_one_End__c = Date.today().addDays(22);
        insert new Product2[] {product1,product2,product3};
        
        PricebookEntry standardPrice1 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product1.Id,
            UnitPrice = 0,
            IsActive = true,
            CurrencyIsoCode = 'CNY'
        );
        
        PricebookEntry standardPrice2 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product2.Id,
            UnitPrice = 0,
            IsActive = true,
            CurrencyIsoCode = 'CNY'
        );
        
        PricebookEntry standardPrice3 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product3.Id,
            UnitPrice = 0,
            IsActive = true,
            CurrencyIsoCode = 'CNY'
        );
        
        insert new PricebookEntry[] {standardPrice1,standardPrice2,standardPrice3};
        //List<Pricebook2> pris = [select Id from Pricebook2 where IsStandard  = true And isActive = true limit 1];
        PricebookEntry entry1 = new PricebookEntry( Pricebook2Id=pricebook.Id, Product2Id=product1.Id);
        entry1.UnitPrice = 0;
        entry1.IsActive = true;
        entry1.UseStandardPrice = false;
        entry1.CurrencyIsoCode = 'CNY';
            
        PricebookEntry entry2 = new PricebookEntry( Pricebook2Id=pricebook.Id, Product2Id=product2.Id);
        entry2.UnitPrice = 0;
        entry2.IsActive = true;
        entry2.UseStandardPrice = false;
        entry2.CurrencyIsoCode = 'CNY';
        
        PricebookEntry entry3 = new PricebookEntry( Pricebook2Id=pricebook.Id, Product2Id=product3.Id);
        entry3.UnitPrice = 0;
        entry3.IsActive = true;
        entry3.UseStandardPrice = false;
        entry3.CurrencyIsoCode = 'CNY';
        
        insert new PricebookEntry[] {entry1,entry2,entry3};
        
        Account acc1 = new Account(
            name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c = 'Customer IE',
            FacilityName__c = 'abc',
            PostCode__c='000000'
        );
        insert acc1;
        
        Opportunity opp1 = new Opportunity(
            name = 'test opp1',
            RecordTypeId = rectOpp[0].Id,
            OwnerId = UserInfo.getUserId(),
            AccountId = acc1.Id,
            StageName = 'Phase3',
            ProductSegment__c = 'IE',
            CurrencyIsoCode = 'CNY',
            Pricebook2Id = pricebook.Id,
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            Machine_Parts__c = 'Machine',
            //CloseDate = Date.valueof('1900-01-01')
            CloseDate = Date.today().addDays(22),
            ExpectedOrderDate__c = Date.today().addDays(2),
            IE_Custom_Price__c = 1000
        );
        insert opp1;
        
        // 询价产品新建test
        PageReference page = new PageReference('/apex/OpportunityProduct?oppid=' + opp1.Id);
        System.Test.setCurrentPage(page);
        OpportunityProductController controller = new OpportunityProductController();
        System.assertEquals(opp1.Id, controller.opp.id);
        
        controller.init();
        //controller.addLine();
        //controller.lineNo = 1;
        //controller.deleteLine();
        
        controller.OPInfoList[0].productName = product1.Name;
        controller.OPInfoList[0].productId = product1.Id;
        controller.OPInfoList[0].productCode = product1.ProductCode;
        controller.OPInfoList[0].oli.QuantityD__c = 2;
        controller.OPInfoList[0].oli.UnitPriceD__c = 111;
        controller.OPInfoList[0].oli.DescriptionD__c = 'test1';
        
        controller.OPInfoList[1].productName = product2.Name;
        controller.OPInfoList[1].productId = product2.Id;
        controller.OPInfoList[1].productCode = product2.ProductCode;
        controller.OPInfoList[1].oli.QuantityD__c = 1;
        controller.OPInfoList[1].oli.UnitPriceD__c = 200;
        controller.OPInfoList[1].oli.DescriptionD__c = 'test2';
 
        controller.opp.IE_Custom_Price__c = 1000;
        
        controller.saveLine();
        
        List<OpportunityLineItem> oppList = [select Id, Product2Id from OpportunityLineItem where OpportunityId = :opp1.Id order by DescriptionD__c];
        //System.assertEquals(2, oppList.size());
        //System.assertEquals(product1.Id, oppList[0].Product2Id);
        //System.assertEquals(product2.Id, oppList[1].Product2Id);
        
        //询价产品更新test
        controller = new OpportunityProductController(new ApexPages.StandardController(opp1));
        controller.init();
        controller.testI();
        controller.OPInfoList[0].productName = product3.Name;
        controller.OPInfoList[0].productId = product3.Id;
        controller.OPInfoList[0].productCode = product3.ProductCode;
        controller.OPInfoList[0].oli.QuantityD__c = 3;
        controller.OPInfoList[0].oli.UnitPriceD__c = 111;
        controller.OPInfoList[0].oli.DescriptionD__c = 'test3';
 
        controller.OPInfoList[1].oli.IsDelete__c = true;
        
        controller.saveLine();
        
        List<OpportunityLineItem> oppList2 = [select Id, Product2Id, DealerSelectProduct__c from OpportunityLineItem where OpportunityId = :opp1.Id];
        //System.assertEquals(1, oppList2.size());
        //System.assertEquals(product3.Id, oppList2[0].Product2Id);
    }
 
    @isTest static void test_init2() {
        Id pricebookId = Test.getStandardPricebookId();
 
        Pricebook2 pricebook = new Pricebook2(
            Name = 'IE',
            ProductSegment__c = 'IE',
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            MachineParts__c = 'Machine',
            isActive = true
        );
        insert pricebook;
 
        Product2 product1 = new Product2();
        product1.Name = 'product1';
        product1.ProductCode = 'product1';
        product1.Product_ECCode__c = 'product1';
        product1.IsActive = true;
        product1.ProductStatus__c  = '1';
 
        Product2 product2 = new Product2();
        product2.Name = 'product2';
        product2.ProductCode = 'product2';
        product2.Product_ECCode__c = 'product2';
        product2.IsActive = true;
        product2.ProductStatus__c  = '1';
 
        Product2 product3 = new Product2();
        product3.Name = 'product3';
        product3.ProductCode = 'product3';
        product3.Product_ECCode__c = 'product3';
        product3.IsActive = true;
        product3.ProductStatus__c  = '1';
 
        insert new Product2[] {product1, product2, product3};
 
        PricebookEntry standardPrice1 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product1.Id,
            UnitPrice = 0,
            IsActive = true,
            CurrencyIsoCode = 'CNY'
        );
 
        PricebookEntry standardPrice2 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product2.Id,
            UnitPrice = 0,
            IsActive = true,
            CurrencyIsoCode = 'CNY'
        );
 
        PricebookEntry standardPrice3 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product3.Id,
            UnitPrice = 0,
            IsActive = true,
            CurrencyIsoCode = 'CNY'
        );
 
        insert new PricebookEntry[] {standardPrice1, standardPrice2, standardPrice3};
 
        PricebookEntry entry1 = new PricebookEntry(
            Pricebook2Id = pricebook.Id,
            Product2Id = product1.Id,
            UnitPrice = 0,
            IsActive = true,
            UseStandardPrice = false,
            CurrencyIsoCode = 'CNY',
            CostPrice1__c = 100,
            EffectiveDateFrom1__c = Date.today().addDays(-1),
            EffectiveDateTo1__c = Date.today().addDays(1),
            SalesPrice1__c = 200,
            EffectiveDateFrom1_sales__c = Date.today().addDays(-1),
            EffectiveDateTo1_sales__c = Date.today().addDays(1)
        );
 
        PricebookEntry entry2 = new PricebookEntry(
            Pricebook2Id = pricebook.Id,
            Product2Id = product2.Id,
            UnitPrice = 0,
            IsActive = true,
            UseStandardPrice = false,
            CurrencyIsoCode = 'CNY',
            CostPrice1__c = 100,
            EffectiveDateFrom1__c = Date.today().addDays(-1),
            EffectiveDateTo1__c = Date.today().addDays(1),
            SalesPrice1__c = 200,
            EffectiveDateFrom1_sales__c = Date.today().addDays(-1),
            EffectiveDateTo1_sales__c = Date.today().addDays(1)
        );
 
        PricebookEntry entry3 = new PricebookEntry(
            Pricebook2Id = pricebook.Id,
            Product2Id = product3.Id,
            UnitPrice = 0,
            IsActive = true,
            UseStandardPrice = false,
            CurrencyIsoCode = 'CNY',
            CostPrice1__c = 100,
            EffectiveDateFrom1__c = Date.today().addDays(-1),
            EffectiveDateTo1__c = Date.today().addDays(1),
            SalesPrice1__c = 200,
            EffectiveDateFrom1_sales__c = Date.today().addDays(-1),
            EffectiveDateTo1_sales__c = Date.today().addDays(1)
        );
 
        insert new PricebookEntry[] {entry1, entry2, entry3};
 
        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        Account user = new Account(
            Name = '*',
            FacilityName__c = 'user',
            PostCode__c = '123456',
            RecordTypeId = rectIE[0].Id
        );
        insert user;
 
        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = user.Id,
            StageName = 'Phase3',
            CurrencyIsoCode = 'CNY',
            ProductSegment__c = 'IE',
            CloseDate = Date.today(),
            NewInquiryDate__c = Date.today().addDays(-2),
            ExpectedOrderDate__c = Date.today().addDays(2),
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            Machine_Parts__c = 'Machine',
            Pricebook2Id = pricebook.Id
        );
        insert opp;
 
        PageReference page = new PageReference('/apex/OpportunityProduct?oppid=' + opp.Id);
        System.Test.setCurrentPage(page);
        OpportunityProductController controller = new OpportunityProductController();
 
        controller.init();
 
        String strblob = '"套装","产品code","产品名称","数量","折扣"\n"set02","product1","product1","2.00","0.00"\n';
 
        controller.contentFile = Blob.valueOf(strblob);
 
        controller.csvRead();
 
        strblob = '套装,产品code,产品名称,数量,折扣\nset01,product1,product1,2,49\nset01,product2,product2,2,49';
 
        controller.contentFile = Blob.valueOf(strblob);
 
        controller.csvRead();
        controller.testI();
 
        controller.fileName = 'aaa';
        controller.csvExport();
    }
}