高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
@isTest
private class SpareIsLoanBatchTest {
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    @isTest
    static  void setupTestData() {
        
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        ControllerUtil.EscapeNFM001Trigger = true;
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        
        List<RecordType> rectOPD = [select Id,name from RecordType where IsActive = true and SobjectType = 'Report__c' and Name = '跟台'];
        if (rectOPD.size() == 0) {
            return;
        }
 
        
        
        Date today = Date.today();
        Date lastDate1 = Date.today().addMonths(1);
        Date lastDate2 = Date.today().addMonths(2);
        Date lastDate3 = Date.today().addMonths(3);
 
        // 省
        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'];
        // 診療科を作る 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;
 
        Opportunity opp = new Opportunity();
        opp.AccountId           = dep.Id;
        opp.Department_Class__c = dep.Id;
        opp.Hospital__c         = hospital.Id;
        opp.SAP_Send_OK__c      = false;
        opp.Name                = 'GZ-SP-NFM009_0';
        opp.Trade__c            = '内貿';
        opp.StageName           = '引合';
        opp.CloseDate           = today.addDays(22);
        opp.Close_Forecasted_Date__c = lastDate1;
        opp.Stock_apply_status__c = '批准';
        insert opp;
        // 产品
        Product2 pro5 = new Product2(Name='name05',IsActive=true,Family='SP',
                Fixture_Model_No__c='n05',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'CV290',Asset_Model_No__c = 'CV290',
                ProductCode_Ext__c='pc05',Manual_Entry__c=false);
        insert pro5;
 
        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=pro5.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        insert entry;
 
 
        OpportunityLineItem oppli = new OpportunityLineItem();
        oppli.OpportunityId = opp.Id;
        oppli.Id__c = '110';
        oppli.SFDA_Status__c = '有効';
        oppli.Name__c = '一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十';
        oppli.ListPrice__c = 112;
        oppli.Quantity = 113;
        oppli.UnitPrice = 114;
        oppli.UnitPrice__c = 115;
        oppli.Qty_Unit__c = '116';
        oppli.Cost__c = 117;
        oppli.BSS_Category__c = 'G&R';
        oppli.Item_Order__c = 1;
        oppli.PricebookEntryId = entry.Id;
        insert oppli;
        
        OPDPlan__c oPDPlan = new OPDPlan__c();
        oPDPlan.Status__c = '计划中';
        oPDPlan.OPDPlan_ImplementDate__c = Date.today().addDays(5);
        oPDPlan.NoOpp_Reason__c = 'HCP对应';
        oPDPlan.Rental_Apply2__c =null;
        oPDPlan.Rental_Apply1__c =null;
        oPDPlan.Rental_Apply3__c = null;
        insert oPDPlan;
        System.Test.StartTest();
        // 备品配套(只有附属品)
        Fixture_Set__c fsObjC1 = new Fixture_Set__c();
        fsObjC1.Name = 'setC1';
        fsObjC1.Fixture_Set_Body_Model_No__c = 'modelNoC1';
        fsObjC1.Loaner_name__c = 'nameC1';
        insert fsObjC1;
        
        //备品借出申请
        Rental_Apply__c raObj = new Rental_Apply__c();
        raObj.Name = 'testra';
        raObj.OPDPlan__c = oPDPlan.Id;
        raObj.Product_category__c = 'GI';
        raObj.Demo_purpose1__c = '产品试用';
        raObj.demo_purpose2__c = '试用(无询价)';
        raObj.direct_send__c = '医疗机构';
        raObj.Loaner_received_staff__c = '王五';
        raObj.Loaner_received_staff_phone__c = '110';
        raObj.direct_shippment_address__c = '北京市';
        raObj.Hospital__c = hospital.Id;
        raObj.Strategic_dept__c = strategicDep[0].Id;
        raObj.Account__c = dep.Id;
        raObj.Request_shipping_day__c = Date.toDay();
        raObj.Request_return_day__c = Date.toDay();
        raObj.Follow_UP_Opp__c = opp.Id;
        raObj.Status_Text__c = '已批准';
        raObj.demo_purpose2__c = FixtureUtil.raDemo_purpose2MAP.get('shiyongwuxunjia');
        raObj.direct_send__c = '医疗机构';
        insert raObj;
 
        Rental_Apply_Equipment_Set__c raesObjC1 = new Rental_Apply_Equipment_Set__c();
        raesObjC1.Rental_Apply__c = raObj.Id;
        raesObjC1.Fixture_Set__c = fsObjC1.Id;
        raesObjC1.Cancel_Select__c = false;
        raesObjC1.Shipment_request_time__c = Date.toDay();
        raesObjC1.Rental_Start_Date__c = Date.toDay();
        raesObjC1.Rental_End_Date__c = Date.toDay();
        raesObjC1.IndexFromUniqueKey__c = 1;
        raesObjC1.UniqueKey__c = '1:'+ fsObjC1.Id + ':1';
        insert raesObjC1;
 
        // 保有设备
        Asset asset = new Asset(Asset_Owner__c = 'Olympus');
        asset.RecordTypeId = System.Label.Asset_RecordType;
        asset.SerialNumber = 'asset';
        asset.Name = 'asset';
        asset.AccountId = dep.Id;
        asset.Department_Class__c = strategicDep[0].Id;
        asset.Hospital__c = hospital.Id;
        asset.Product2Id = pro5.Id;
        asset.Quantity = 1;
        asset.Status = '有库存';
        asset.Manage_type__c = '个体管理';
        asset.Internal_asset_location__c = '北京 备品中心';
        asset.Loaner_accsessary__c = true;
        asset.Delete_Flag__c = false;
        asset.Freeze_sign__c = false;
        asset.Out_of_wh__c = 0;
        insert new Asset[] {asset};
 
        // 备品配套明细(只有附属品)
        Fixture_Set_Detail__c fsdObjC1 = new Fixture_Set_Detail__c();
        fsdObjC1.Name = 'detailAccessory';
        fsdObjC1.Name_CHN_Created__c = 'detailAccessoryName';
        fsdObjC1.Product2__c = pro5.Id;
        fsdObjC1.Fixture_Set__c = fsObjC1.Id;
        fsdObjC1.Is_Body__c = false;
        fsdObjC1.Is_Optional__c = true;
        fsdObjC1.UniqueKey__c = fsObjC1.Id + ':' + pro5.Id;
        fsdObjC1.SortInt__c = 1;
        // fsdObjC1.Fixture_Model_No_text__c = 'CV290';
        insert fsdObjC1;
 
        
 
        // 借出备品配套一览明细
        Rental_Apply_Equipment_Set_Detail__c raesdObjC1 = new Rental_Apply_Equipment_Set_Detail__c();
        raesdObjC1.Rental_Apply__c = raObj.Id;
        raesdObjC1.Fixture_Set_Detail__c = fsdObjC1.Id;
        raesdObjC1.Rental_Num__c = 1;
        raesdObjC1.Queue_Number__c = null;
        raesdObjC1.Is_Body__c = false;
        raesdObjC1.Rental_Apply_Equipment_Set__c = raesObjC1.Id;
        raesdObjC1.Asset__c = asset.Id;
        raesdObjC1.IndexFromUniqueKey__c = 1;
        raesdObjC1.UniqueKey__c = '1:'+ raesObjC1.Id + ':' + fsdObjC1.Id + ':1';
        raesdObjC1.Salesdepartment_before__c = '1.华北营业本部';
        raesdObjC1.Internal_asset_location_before__c = '北京 备品中心';
        raesdObjC1.Product_category_text__c = 'GI';
        raesdObjC1.Equipment_Type_text__c = '产品试用';
        raesdObjC1.Cancel_Select__c = true;
        raesdObjC1.Arrival_in_wh__c = false;
        raesdObjC1.Check_lost_Item_Final__c = '欠品';
        raesdObjC1.Check_lost_Item__c = '欠品';
        raesdObjC1.Lost_item_giveup__c = false;
        raesdObjC1.Cancel_Reason__c = '主动取消';
        raesdObjC1.Shippment_loaner_time__c = Date.toDay();
        // raesdObjC1.Fixture_Model_No_text__c = 'CV290';
        insert raesdObjC1;
 
        Daily_Report__c dr1 = new Daily_Report__c();
        dr1.Reporter__c = UserInfo.getUserId();
        dr1.Reported_Date__c = date.today();
        dr1.Status__c = '批准';
        
        insert dr1;
 
        Report__c rep1 = new Report__c();
        rep1.CurrencyIsoCode         = 'CNY';
        rep1.RecordTypeId            = rectOPD[0].Id;
        rep1.Status__c               = '申請中';
        rep1.Daily_Report__c         = dr1.Id;
        rep1.Purpose1__c             = 'OPD';
        rep1.Purpose2__c             = '引合追及活動';
        rep1.Reason__c               = '実施背景';
        rep1.Appeal_Point__c         = '実施目的・訴求点';
        rep1.Strategic_Department_Class__c = '1.消化科';
        rep1.Technical_Category1__c  = '消化道检查';
        rep1.Technical_Category2__c  = '普通胃镜检查';
        rep1.Patient_Status__c       = '患者状況';
        rep1.Operation_Process__c    = '手術プロセス';
        rep1.Operation_Task__c       = '手術上の課題';
        rep1.Product_Evaluation_Advantage__c = '製品評価利点';
        rep1.Product_Evaluation_Faults__c = '製品評価欠点';
        rep1.OPD_Summarize__c        = 'OPD実施総括';
        rep1.Activity_Plan__c        = '今後の活動計画';
        rep1.Opportunity_Situation__c = '引合無';
        rep1.OPD_ProductCategory1__c ='3D System';
        rep1.OPD_ProductCategory2__c ='OR Imaging Products';
        rep1.Hospital_Reference__c   = hospital.Id;
        rep1.Department_Class_Ref__c = strategicDep[0].Id;
        rep1.Hospital_Department__c  = dep.Id;
        rep1.Date__c                 = Date.today().addDays(-1);
        rep1.Loaner_request_no1__c = raObj.Id;
        rep1.Status__c = '批准';
        insert rep1;
        raObj.Status__c = '已批准';
        update raObj;
        
        List<String> rA_statusList = new List<String>();
        rA_statusList.add('已出库');rA_statusList.add('医院已装机确认');rA_statusList.add('已回寄');
        rA_statusList.add('欠品中');rA_statusList.add('申请者已收货');rA_statusList.add('已回收');
        rA_statusList.add('完了');
        List<Rental_Apply__c> raList =[Select Id,Follow_UP_Opp__c,RA_Status__c,ShelfUp_ng_num__c,Follow_UP_Opp__r.Spare_Application_Singular__c 
            from Rental_Apply__c where Follow_UP_Opp__c = :opp.Id ];
        System.assertEquals('完了', raList[0].RA_Status__c);
        System.assertEquals(1, raList.size());
        
        
        Id execBTId = null;
        List<String> oppIdList = new List<String>();
        oppIdList.add(opp.Id);
        execBTId = Database.executeBatch(new SpareIsLoanBatch(oppIdList));
        System.Test.StopTest(); 
        Opportunity  opp2 = [select Id,Spare_Application_Singular__c 
            from Opportunity where Id = :opp.Id];
        System.assertEquals(1, opp2.Spare_Application_Singular__c);
        
    
    }    
    @isTest
    static  void setupTestData1() {
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        ControllerUtil.EscapeNFM001Trigger = true;
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        
        List<RecordType> rectOPD = [select Id,name from RecordType where IsActive = true and SobjectType = 'Report__c' and Name = '跟台'];
        if (rectOPD.size() == 0) {
            return;
        }
        
        Date today = Date.today();
        Date lastDate1 = Date.today().addMonths(1);
        Date lastDate2 = Date.today().addMonths(2);
        Date lastDate3 = Date.today().addMonths(3);
 
        // 省
        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'];
        // 診療科を作る 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;
 
        Opportunity opp = new Opportunity();
        opp.AccountId           = dep.Id;
        opp.Department_Class__c = dep.Id;
        opp.Hospital__c         = hospital.Id;
        opp.SAP_Send_OK__c      = false;
        opp.Name                = 'GZ-SP-NFM009_0';
        opp.Trade__c            = '内貿';
        opp.StageName           = '引合';
        opp.CloseDate           = today.addDays(22);
        opp.Close_Forecasted_Date__c = lastDate1;
        opp.Stock_apply_status__c = '批准';
        opp.Spare_Application_Singular__c = 5;
        insert opp;
 
        
 
        // 产品
        Product2 pro5 = new Product2(Name='name05',IsActive=true,Family='SP',
                Fixture_Model_No__c='n05',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n05',
                ProductCode_Ext__c='pc05',Manual_Entry__c=false);
        insert pro5;
 
        OPDPlan__c oPDPlan = new OPDPlan__c();
        oPDPlan.Status__c = '计划中';
        oPDPlan.OPDPlan_ImplementDate__c = Date.today().addDays(5);
        oPDPlan.NoOpp_Reason__c = 'HCP对应';
        oPDPlan.Rental_Apply2__c =null;
        oPDPlan.Rental_Apply1__c =null;
        oPDPlan.Rental_Apply3__c = null;
        insert oPDPlan;
        Test.startTest();
        //备品借出申请
        Rental_Apply__c raObj = new Rental_Apply__c();
        raObj.Name = 'testra';
        raObj.OPDPlan__c = oPDPlan.Id;
        raObj.Product_category__c = 'GI';
        raObj.Demo_purpose1__c = '产品试用';
        raObj.demo_purpose2__c = FixtureUtil.raDemo_purpose2MAP.get('shiyongwuxunjia');
        raObj.direct_send__c = '医疗机构';
        raObj.Loaner_received_staff__c = '王五';
        raObj.Loaner_received_staff_phone__c = '110';
        raObj.direct_shippment_address__c = '北京市';
        raObj.Hospital__c = hospital.Id;
        raObj.Strategic_dept__c = strategicDep[0].Id;
        raObj.Account__c = dep.Id;
        raObj.Request_shipping_day__c = Date.toDay();
        raObj.Request_return_day__c = Date.toDay();
        raObj.Follow_UP_Opp__c = opp.Id;
        raObj.Status_Text__c = '已批准';
        raObj.OPDAmount__c = 1;
        raObj.direct_send__c = '医疗机构';
        insert raObj;
 
        // 保有设备
        Asset asset = new Asset(Asset_Owner__c = 'Olympus');
        asset.RecordTypeId = System.Label.Asset_RecordType;
        asset.SerialNumber = 'asset';
        asset.Name = 'asset';
        asset.AccountId = dep.Id;
        asset.Department_Class__c = strategicDep[0].Id;
        asset.Hospital__c = hospital.Id;
        asset.Product2Id = pro5.Id;
        asset.Quantity = 1;
        asset.Status = '有库存';
        asset.Manage_type__c = '个体管理';
        asset.Internal_asset_location__c = '北京 备品中心';
        asset.Loaner_accsessary__c = true;
        asset.Delete_Flag__c = false;
        asset.Freeze_sign__c = false;
        asset.Out_of_wh__c = 0;
        insert new Asset[] {asset};
 
        // 备品配套(只有附属品)
        Fixture_Set__c fsObjC1 = new Fixture_Set__c();
        fsObjC1.Name = 'setC1';
        fsObjC1.Fixture_Set_Body_Model_No__c = 'modelNoC1';
        fsObjC1.Loaner_name__c = 'nameC1';
        insert fsObjC1;
 
        // 备品配套明细(只有附属品)
        Fixture_Set_Detail__c fsdObjC1 = new Fixture_Set_Detail__c();
        fsdObjC1.Name = 'detailAccessory';
        fsdObjC1.Name_CHN_Created__c = 'detailAccessoryName';
        fsdObjC1.Product2__c = pro5.Id;
        fsdObjC1.Fixture_Set__c = fsObjC1.Id;
        fsdObjC1.Is_Body__c = false;
        fsdObjC1.Is_Optional__c = true;
        fsdObjC1.UniqueKey__c = fsObjC1.Id + ':' + pro5.Id;
        fsdObjC1.SortInt__c = 1;
        insert fsdObjC1;
 
        // 借出备品配套一览
        Rental_Apply_Equipment_Set__c raesObjC1 = new Rental_Apply_Equipment_Set__c();
        raesObjC1.Rental_Apply__c = raObj.Id;
        raesObjC1.Fixture_Set__c = fsObjC1.Id;
        raesObjC1.Shipment_request_time__c = Date.toDay();
        raesObjC1.Rental_Start_Date__c = Date.toDay();
        raesObjC1.Rental_End_Date__c = Date.toDay();
        raesObjC1.IndexFromUniqueKey__c = 1;
        raesObjC1.UniqueKey__c = '1:'+ fsObjC1.Id + ':1';
        raesObjC1.Cancel_Reason__c = '主动取消';
        insert raesObjC1;
 
        // 借出备品配套一览明细
        Rental_Apply_Equipment_Set_Detail__c raesdObjC1 = new Rental_Apply_Equipment_Set_Detail__c();
        raesdObjC1.Rental_Apply__c = raObj.Id;
        raesdObjC1.Fixture_Set_Detail__c = fsdObjC1.Id;
        raesdObjC1.Rental_Num__c = 1;
        raesdObjC1.Queue_Number__c = null;
        raesdObjC1.Is_Body__c = false;
        raesdObjC1.Rental_Apply_Equipment_Set__c = raesObjC1.Id;
        raesdObjC1.Asset__c = asset.Id;
        raesdObjC1.IndexFromUniqueKey__c = 1;
        raesdObjC1.UniqueKey__c = '1:'+ raesObjC1.Id + ':' + fsdObjC1.Id + ':1';
        raesdObjC1.Salesdepartment_before__c = '1.华北营业本部';
        raesdObjC1.Internal_asset_location_before__c = '北京 备品中心';
        raesdObjC1.Product_category_text__c = 'GI';
        raesdObjC1.Equipment_Type_text__c = '产品试用';
        raesdObjC1.Cancel_Select__c = true;
        raesdObjC1.Arrival_in_wh__c = false;
        raesdObjC1.Check_lost_Item_Final__c = '欠品';
        raesdObjC1.Check_lost_Item__c = '欠品';
        raesdObjC1.Lost_item_giveup__c = false;
        raesdObjC1.Shippment_loaner_time__c = Date.toDay();
         raesdObjC1.Cancel_Reason__c = '主动取消';
        insert raesdObjC1;
 
        Daily_Report__c dr1 = new Daily_Report__c();
        dr1.Reporter__c = UserInfo.getUserId();
        dr1.Reported_Date__c = date.today();
        dr1.Status__c = '批准';
        
        insert dr1;
 
        Report__c rep1 = new Report__c();
        rep1.CurrencyIsoCode         = 'CNY';
        rep1.RecordTypeId            = rectOPD[0].Id;
        rep1.Status__c               = '申請中';
        rep1.Daily_Report__c         = dr1.Id;
        rep1.Purpose1__c             = 'OPD';
        rep1.Purpose2__c             = '引合追及活動';
        rep1.Reason__c               = '実施背景';
        rep1.Appeal_Point__c         = '実施目的・訴求点';
        rep1.Strategic_Department_Class__c = '1.消化科';
        rep1.Technical_Category1__c  = '消化道检查';
        rep1.Technical_Category2__c  = '普通胃镜检查';
        rep1.Patient_Status__c       = '患者状況';
        rep1.Operation_Process__c    = '手術プロセス';
        rep1.Operation_Task__c       = '手術上の課題';
        rep1.Product_Evaluation_Advantage__c = '製品評価利点';
        rep1.Product_Evaluation_Faults__c = '製品評価欠点';
        rep1.OPD_Summarize__c        = 'OPD実施総括';
        rep1.Activity_Plan__c        = '今後の活動計画';
        rep1.Opportunity_Situation__c = '引合無';
        rep1.OPD_ProductCategory1__c ='3D System';
        rep1.OPD_ProductCategory2__c ='OR Imaging Products';
        rep1.Hospital_Reference__c   = hospital.Id;
        rep1.Department_Class_Ref__c = strategicDep[0].Id;
        rep1.Hospital_Department__c  = dep.Id;
        rep1.Date__c                 = Date.today().addDays(-1);
        rep1.Loaner_request_no1__c = raObj.Id;
        rep1.Status__c = '批准';
        insert rep1;
        raObj.Status__c = '已批准';
        update raObj;
        
        List<String> rA_statusList = new List<String>();
        rA_statusList.add('已出库');rA_statusList.add('医院已装机确认');rA_statusList.add('已回寄');
        rA_statusList.add('欠品中');rA_statusList.add('申请者已收货');rA_statusList.add('已回收');
        rA_statusList.add('完了');
        List<Rental_Apply__c> raList =[Select Id,Follow_UP_Opp__c,RA_Status__c,ShelfUp_ng_num__c,Follow_UP_Opp__r.Spare_Application_Singular__c 
            from Rental_Apply__c where Follow_UP_Opp__c = :opp.Id ];
        
        System.assertEquals('完了', raList[0].RA_Status__c);
        System.assertEquals(1, raList.size());
        Id execBTId = null;
        execBTId = Database.executeBatch(new SpareIsLoanBatch());
        Opportunity  opp2 = [select Id,Spare_Application_Singular__c 
            from Opportunity where Id = :opp.Id];
        System.assertEquals(5, opp2.Spare_Application_Singular__c);
    }    
    @isTest
    static  void setupTestData3() {
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        ControllerUtil.EscapeNFM001Trigger = true;
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        
        List<RecordType> rectOPD = [select Id,name from RecordType where IsActive = true and SobjectType = 'Report__c' and Name = '跟台'];
        if (rectOPD.size() == 0) {
            return;
        }
        
        Date today = Date.today();
        Date lastDate1 = Date.today().addMonths(1);
        Date lastDate2 = Date.today().addMonths(2);
        Date lastDate3 = Date.today().addMonths(3);
 
        // 省
        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'];
        // 診療科を作る 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;
 
        Opportunity opp = new Opportunity();
        opp.AccountId           = dep.Id;
        opp.Department_Class__c = dep.Id;
        opp.Hospital__c         = hospital.Id;
        opp.SAP_Send_OK__c      = false;
        opp.Name                = 'GZ-SP-NFM009_0';
        opp.Trade__c            = '内貿';
        opp.StageName           = '引合';
        opp.CloseDate           = today.addDays(22);
        opp.Close_Forecasted_Date__c = lastDate1;
        opp.Stock_apply_status__c = '批准';
        insert opp;
        // 产品
        Product2 pro5 = new Product2(Name='name05',IsActive=true,Family='SP',
                Fixture_Model_No__c='n05',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n05',
                ProductCode_Ext__c='pc05',Manual_Entry__c=false);
        insert pro5;
 
        OPDPlan__c oPDPlan = new OPDPlan__c();
        oPDPlan.Status__c = '计划中';
        oPDPlan.OPDPlan_ImplementDate__c = Date.today().addDays(5);
        oPDPlan.NoOpp_Reason__c = 'HCP对应';
        oPDPlan.Rental_Apply2__c =null;
        oPDPlan.Rental_Apply1__c =null;
        oPDPlan.Rental_Apply3__c = null;
        insert oPDPlan;
        System.Test.StartTest();
        //备品借出申请
        Rental_Apply__c raObj = new Rental_Apply__c();
        raObj.Name = 'testra';
        raObj.OPDPlan__c = oPDPlan.Id;
 
        raObj.OPDPlan__c = oPDPlan.Id;
        raObj.Product_category__c = 'GI';
        raObj.Demo_purpose1__c = '产品试用';
        raObj.direct_send__c = '医疗机构';
        raObj.Loaner_received_staff__c = '王五';
        raObj.Loaner_received_staff_phone__c = '110';
        raObj.direct_shippment_address__c = '北京市';
        raObj.Hospital__c = hospital.Id;
        raObj.Strategic_dept__c = strategicDep[0].Id;
        raObj.Account__c = dep.Id;
        raObj.Request_shipping_day__c = Date.toDay();
        raObj.Request_return_day__c = Date.toDay();
        raObj.Follow_UP_Opp__c = opp.Id;
        raObj.Status_Text__c = '已批准';
        raObj.demo_purpose2__c = FixtureUtil.raDemo_purpose2MAP.get('shiyongwuxunjia');
        // raObj.Bollow_Date__c = Date.today();
        raObj.direct_send__c = '医疗机构';
        //raObj.Status__c = '已批准';
        raObj.OPDAmount__c = 1;
        insert raObj;
 
        // 保有设备
        Asset asset = new Asset(Asset_Owner__c = 'Olympus');
        asset.RecordTypeId = System.Label.Asset_RecordType;
        asset.SerialNumber = 'asset';
        asset.Name = 'asset';
        asset.AccountId = dep.Id;
        asset.Department_Class__c = strategicDep[0].Id;
        asset.Hospital__c = hospital.Id;
        asset.Product2Id = pro5.Id;
        asset.Quantity = 1;
        asset.Status = '有库存';
        asset.Manage_type__c = '个体管理';
        asset.Internal_asset_location__c = '北京 备品中心';
        asset.Loaner_accsessary__c = true;
        asset.Delete_Flag__c = false;
        asset.Freeze_sign__c = false;
        asset.Out_of_wh__c = 0;
        insert new Asset[] {asset};
 
        // 备品配套(只有附属品)
        Fixture_Set__c fsObjC1 = new Fixture_Set__c();
        fsObjC1.Name = 'setC1';
        fsObjC1.Fixture_Set_Body_Model_No__c = 'modelNoC1';
        fsObjC1.Loaner_name__c = 'nameC1';
        insert fsObjC1;
 
        // 备品配套明细(只有附属品)
        Fixture_Set_Detail__c fsdObjC1 = new Fixture_Set_Detail__c();
        fsdObjC1.Name = 'detailAccessory';
        fsdObjC1.Name_CHN_Created__c = 'detailAccessoryName';
        fsdObjC1.Product2__c = pro5.Id;
        fsdObjC1.Fixture_Set__c = fsObjC1.Id;
        fsdObjC1.Is_Body__c = false;
        fsdObjC1.Is_Optional__c = true;
        fsdObjC1.UniqueKey__c = fsObjC1.Id + ':' + pro5.Id;
        fsdObjC1.SortInt__c = 1;
        insert fsdObjC1;
 
        // 借出备品配套一览
        Rental_Apply_Equipment_Set__c raesObjC1 = new Rental_Apply_Equipment_Set__c();
        raesObjC1.Rental_Apply__c = raObj.Id;
        raesObjC1.Fixture_Set__c = fsObjC1.Id;
        //raesObjC1.Cancel_Select__c = false;
        raesObjC1.Shipment_request_time__c = Date.toDay();
        raesObjC1.Rental_Start_Date__c = Date.toDay();
        raesObjC1.Rental_End_Date__c = Date.toDay();
        raesObjC1.IndexFromUniqueKey__c = 1;
        raesObjC1.UniqueKey__c = '1:'+ fsObjC1.Id + ':1';
        raesObjC1.Cancel_Reason__c = '主动取消';
        insert raesObjC1;
 
        // 借出备品配套一览明细
        Rental_Apply_Equipment_Set_Detail__c raesdObjC1 = new Rental_Apply_Equipment_Set_Detail__c();
        raesdObjC1.Rental_Apply__c = raObj.Id;
        raesdObjC1.Fixture_Set_Detail__c = fsdObjC1.Id;
        raesdObjC1.Rental_Num__c = 1;
        raesdObjC1.Queue_Number__c = null;
        raesdObjC1.Is_Body__c = false;
        raesdObjC1.Rental_Apply_Equipment_Set__c = raesObjC1.Id;
        raesdObjC1.Asset__c = asset.Id;
        raesdObjC1.IndexFromUniqueKey__c = 1;
        raesdObjC1.UniqueKey__c = '1:'+ raesObjC1.Id + ':' + fsdObjC1.Id + ':1';
        raesdObjC1.Salesdepartment_before__c = '1.华北营业本部';
        raesdObjC1.Internal_asset_location_before__c = '北京 备品中心';
        raesdObjC1.Product_category_text__c = 'GI';
        raesdObjC1.Equipment_Type_text__c = '产品试用';
        raesdObjC1.Cancel_Select__c = true;
        raesdObjC1.Arrival_in_wh__c = false;
        raesdObjC1.Check_lost_Item_Final__c = '欠品';
        raesdObjC1.Check_lost_Item__c = '欠品';
        raesdObjC1.Lost_item_giveup__c = false;
        raesdObjC1.Shippment_loaner_time__c = Date.toDay();
        //raesdObjC1.Detail_Finish__c = false;
         raesdObjC1.Cancel_Reason__c = '主动取消';
        insert raesdObjC1;
 
        Daily_Report__c dr1 = new Daily_Report__c();
        dr1.Reporter__c = UserInfo.getUserId();
        dr1.Reported_Date__c = date.today();
        dr1.Status__c = '批准';
        
        insert dr1;
 
        Report__c rep1 = new Report__c();
        rep1.CurrencyIsoCode         = 'CNY';
        rep1.RecordTypeId            = rectOPD[0].Id;
        rep1.Status__c               = '申請中';
        rep1.Daily_Report__c         = dr1.Id;
        rep1.Purpose1__c             = 'OPD';
        rep1.Purpose2__c             = '引合追及活動';
        rep1.Reason__c               = '実施背景';
        rep1.Appeal_Point__c         = '実施目的・訴求点';
        rep1.Strategic_Department_Class__c = '1.消化科';
        rep1.Technical_Category1__c  = '消化道检查';
        rep1.Technical_Category2__c  = '普通胃镜检查';
        rep1.Patient_Status__c       = '患者状況';
        rep1.Operation_Process__c    = '手術プロセス';
        rep1.Operation_Task__c       = '手術上の課題';
        rep1.Product_Evaluation_Advantage__c = '製品評価利点';
        rep1.Product_Evaluation_Faults__c = '製品評価欠点';
        rep1.OPD_Summarize__c        = 'OPD実施総括';
        rep1.Activity_Plan__c        = '今後の活動計画';
        rep1.Opportunity_Situation__c = '引合無';
        rep1.OPD_ProductCategory1__c ='3D System';
        rep1.OPD_ProductCategory2__c ='OR Imaging Products';
        rep1.Hospital_Reference__c   = hospital.Id;
        rep1.Department_Class_Ref__c = strategicDep[0].Id;
        rep1.Hospital_Department__c  = dep.Id;
        rep1.Date__c                 = Date.today().addDays(-1);
        rep1.Loaner_request_no1__c = raObj.Id;
        rep1.Status__c = '申请中';
        insert rep1;
        raObj.Status__c = '已批准';
        update raObj;
        
        List<String> rA_statusList = new List<String>();
        rA_statusList.add('已出库');rA_statusList.add('医院已装机确认');rA_statusList.add('已回寄');
        rA_statusList.add('欠品中');rA_statusList.add('申请者已收货');rA_statusList.add('已回收');
        rA_statusList.add('完了');
        List<Rental_Apply__c> raList =[Select Id,Follow_UP_Opp__c,RA_Status__c,ShelfUp_ng_num__c,Follow_UP_Opp__r.Spare_Application_Singular__c 
            from Rental_Apply__c where Follow_UP_Opp__c = :opp.Id ];
        System.assertEquals('完了', raList[0].RA_Status__c);
        System.assertEquals(1, raList.size());
        
        Id execBTId = null;
        execBTId = Database.executeBatch(new SpareIsLoanBatch());
        Rental_Apply__c rac = [select Id,OPDAmount__c from Rental_Apply__c where Id = :raObj.Id ];
        System.assertEquals(1, rac.OPDAmount__c);
        System.Test.StopTest();
        List<String> oppIdList = new List<String>();
        rep1.Status__c = '申请中';
        update rep1;
        raObj.OPDAmount__c = 5;
        oppIdList.add(raObj.Follow_UP_Opp__c);
        execBTId = Database.executeBatch(new SpareIsLoanBatch(oppIdList));
        rac = [select Id,OPDAmount__c from Rental_Apply__c where Id = :raObj.Id];
        System.assertEquals(0, rac.OPDAmount__c);
        
    }
    
    
}