高章伟
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
@isTest
private class AssetUpdateLastContractbatchTest {
 
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    static Asset createAsset(String input, String accountid, String dcId, String hpId, String serialNo, String prId) {
        Asset asset = new Asset();
        asset.Name = input;
        asset.AccountId = accountid;
        asset.Department_Class__c = dcId;
        asset.Hospital__c = hpId;
        asset.SerialNumber = serialNo;
        asset.Product2Id = prId;
        asset.InstallDate = Date.today();
        insert asset;
        return asset;
    }
 
 
    @isTest
    static void testMethod1() {
        
        ControllerUtil.EscapeNFM001Trigger = true;
        StaticParameter.EscapeSyncProduct2Trigger = true;
        //StaticParameter.EscapeAccountTrigger = true;
        //StaticParameter.EscapeMaintenanceContractAfterUpdateTrigger = true;
 
        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 = '有效';
        insert hospital;
        
        // 戦略科室を得る
        List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
        // 診療科を作る
        Account 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;
        
        // 製品を作る
        Product2 productA = new Product2( Name='テスト商品', Maintenance_Price_Year__c = 12000, Manual_Entry__c = false);
        insert productA;
        
        // 価格表エントリを作成する        
        PricebookEntry 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;
        
        // 納入機器を作成する
        Asset asset01 = createAsset('asset01', dep.Id, strategicDep[0].Id, hospital.Id, '00001', productA.Id);
 
        // 第一期合同
        Maintenance_Contract__c contract1 = new Maintenance_Contract__c();
        contract1.Name = 'tect contract1';
        contract1.Not_Upper_limit_reason__c = 'tect contract1';
        contract1.RecordtypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId();
        contract1.Hospital__c = hospital.Id;
        contract1.Switch_TimeBase_WF__c = true;
        contract1.Department_Class__c = strategicDep[0].Id;
        contract1.Department__c = dep.Id;
        contract1.Service_Contract_Staff__c = UserInfo.getUserId();
        contract1.Maintenance_Contract_No__c = 'tect contract1';
        contract1.Status__c = '契約満了';
        contract1.Contract_Start_Date__c = Date.today().addMonths(-14);
        contract1.Contract_End_Date__c = Date.today().addMonths(-2);
 
        // 第二期合同
        Maintenance_Contract__c contract2 = new Maintenance_Contract__c();
        contract2.Name = 'tect contract2';
        contract2.Not_Upper_limit_reason__c = 'tect contract2';
        contract2.RecordtypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('NewMaintenance_Contract').getRecordTypeId();
        contract2.Hospital__c = hospital.Id;
        contract1.Switch_TimeBase_WF__c = true;
        contract2.Department_Class__c = strategicDep[0].Id;
        contract2.Department__c = dep.Id;
        contract2.Service_Contract_Staff__c = UserInfo.getUserId();
        contract2.Maintenance_Contract_No__c = 'tect contract2';
        contract1.Status__c = '契約満了';
        contract2.Contract_Start_Date__c = Date.today().addMonths(-13);
        contract2.Contract_End_Date__c = Date.today().addMonths(-1);
        insert new Maintenance_Contract__c[] {contract1,contract2};
        contract1.Status__c = '契約満了';
        update contract1;
 
        contract2.Status__c = '契約満了';
        update contract2;
 
        Maintenance_Contract_Asset__c mca1 = new Maintenance_Contract_Asset__c();
        mca1.Asset__c = asset01.Id;
        mca1.Estimate_List_Price__c = 100;
        mca1.Maintenance_Contract__c = contract1.Id;
 
        Maintenance_Contract_Asset__c mca2 = new Maintenance_Contract_Asset__c();
        mca2.Asset__c = asset01.Id;
        mca2.Estimate_List_Price__c = 100;
        mca2.Maintenance_Contract__c = contract2.Id;
        
        insert new Maintenance_Contract_Asset__c[] {mca1,mca2};
        System.Test.startTest();
        List<String> ll = new List<String>();
        List<asset> ass = [select id from asset WHERE id = : asset01.Id];
        System.assertEquals(1, ass.size());
        ll.add(ass[0].Id);
        Id execBTId = Database.executeBatch(new AssetUpdateLastContractbatch(ll), 100);
 
        // Maintenance_Contract_Asset__c mca =  [select Id,LastMContract__c
        //                             from Maintenance_Contract_Asset__c WHERE Id = : mca2.Id];
        // System.assertEquals(contract1.Id, mca.LastMContract__c);
        System.Test.stopTest();
    }
}