高章伟
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
@isTest
private class MaintenanceContractAssetTriggerTester {
    
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    private static Account hospital = null;
    private static List<Account> strategicDep = null;
    private static Product2 productA = null;
    private static Account dep = null;
    private static PricebookEntry entry = null;
    private static Asset asset01 = null;
    private static Asset asset02 = null;
    private static Asset asset03 = null;
    private static Maintenance_Contract__c contract = null;
    static {
        // 病院を作る
        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;
        
        // 戦略科室を得る
        strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
        
        // 診療科を作る
        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;
        
        // 製品を作る
        productA = new Product2( Name='テスト商品');
        insert productA;
        
        // 価格表エントリを作成する     
        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;
        
        // 納入機器を作成する
        asset01 = new Asset();
        asset01.Name = 'テスト機器';
        asset01.AccountId = dep.Id;
        asset01.Department_Class__c = strategicDep[0].Id;
        asset01.Hospital__c = hospital.Id;
        asset01.SerialNumber = 'testserial';
        insert asset01;
        asset02 = new Asset();
        asset02.Name = 'テスト機器';
        asset02.AccountId = dep.Id;
        asset02.Department_Class__c = strategicDep[0].Id;
        asset02.Hospital__c = hospital.Id;
        asset02.SerialNumber = 'testserial';
        insert asset02;
        asset03 = new Asset();
        asset03.Name = 'テスト機器';
        asset03.AccountId = dep.Id;
        asset03.Department_Class__c = strategicDep[0].Id;
        asset03.Hospital__c = hospital.Id;
        asset03.SerialNumber = 'testserial';
        insert asset03;
        
        // 维修合同を作成する
        contract = new Maintenance_Contract__c();
        contract.Name = 'tect contract';
        contract.Hospital__c = hospital.Id;
        contract.Department_Class__c = strategicDep[0].Id;
        contract.Department__c = dep.Id;
        contract.Contract_Start_Date__c = Date.today() - 10;
        contract.Contract_End_Date__c = Date.today() + 10;
        insert contract;
        
    }
 
    static testMethod void myTest01() {
        // 修理を作成する
        Repair__c repair = new Repair__c();
        repair.Account__c = dep.Id;
        repair.Department_Class__c = strategicDep[0].Id;
        repair.Hospital__c = hospital.Id;
        repair.Delivered_Product__c = asset01.Id;
        insert repair;
        
        // 维修合同に納入商品を関連付ける
        List<Maintenance_Contract_Asset__c> insertTarget = new List<Maintenance_Contract_Asset__c>();
        
        Maintenance_Contract_Asset__c target01 = new Maintenance_Contract_Asset__c();
        target01.Maintenance_Contract__c = contract.Id;
        target01.Asset__c = asset01.Id;
        insertTarget.add( target01);
        
        Maintenance_Contract_Asset__c target02 = new Maintenance_Contract_Asset__c();
        target02.Maintenance_Contract__c = contract.Id;
        target02.Asset__c = asset02.Id;
        insertTarget.add( target02);
        
        Maintenance_Contract_Asset__c target03 = new Maintenance_Contract_Asset__c();
        target03.Maintenance_Contract__c = contract.Id;
        target03.Asset__c = asset03.Id;
        insertTarget.add( target03);
        
        insert insertTarget;
        
    }
}