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
/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an org are executed whenever Apex code is deployed
 * to a production org to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production org. In addition, all triggers must have some code coverage.
 * 
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the org size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class LastRepairUpdateBatchTest {
 
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    
    static Asset createAsset( Account hospital, Account strategicDep, Account dep) {
        // 製品を作る
        Product2 productA = new Product2( Name = 'テスト商品',Asset_Model_No__c = 'BF-1T260');
        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 asset = new Asset();
        asset.Name = 'テスト機器';
        asset.AccountId = dep.Id;
        asset.Department_Class__c = strategicDep.Id;
        asset.Hospital__c = hospital.Id;
        asset.SerialNumber = 'testserial';
        asset.Quantity = 3;
        asset.Extend_Gurantee_DateTo_Text__c =Date.today().addDays(30);
        asset.IS_Extend_Gurantee_Txt__c =true;
        asset.LastSFDCRepairNo__c = 'xxxxxx21';
        asset.Order_No__c = 'BJ_2020';
        insert asset;
        
        return asset;
    }
    @testSetup
    private static void setupTestData(){
        Oly_TriggerHandler.bypass('Repair');
        FixtureUtil.SkipTrigger= true;
        Controllerutil.EscapeNFM001Trigger =true;
        // 病院を作る
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdminGPI];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        // 取引先作成
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectHp.size() == 0) {
            return;
        }
        List<RecordType> rectDp = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科' order by Name desc];
        if (rectDp.size() == 0) {
            return;
        }
 
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科' order by Name desc];
        if (rectDpt.size() == 0) {
            return; 
        }
        Account hp1 = new Account(RecordTypeId = rectHp[0].Id,Is_Active__c = '有効' ,Name = 'testHp1', OCM_Category__c = 'H0', OwnerId = hpOwner.Id);
        insert hp1;
 
        Account dp1 = new Account(RecordTypeId = rectDp[0].Id, Is_Active__c = '有効' , Department_Class_Label__c = '呼吸科',
            ParentId  = hp1.id, Name = 'testdp1', OCM_Category__c = 'H0', OwnerId = hpOwner.Id);
        insert dp1;
 
        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp1.Id and Department_Class_Label__c  = '呼吸科' order by Department_Class_Label__c];
        
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = 'hospitalName';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = hp1.Id;
        depart1.Is_Active__c        = '有効';
        insert depart1;
         // 納入機器を作る
         Asset asset = createAsset( hp1, dc1s[0], depart1);
        String BUid = System.Label.interfaceUserID;
        System.runAs ( new User(Id = Userinfo.getUserId()) ){
        // 修理
        Repair__c re = new Repair__c();
        re.SAPRepairNo__c = '000010168255';
        re.Account__c = depart1.Id;
        re.Department_Class__c = dc1s[0].Id;
        re.Hospital__c = hp1.Id;
        re.Cumulative_Uses__c = 1811;
        re.Cumulative_Times__c = 38011;
        re.AwaitToSendAWS__c = true;
        re.DeliveryLogisticsMode__c = '快递';
        re.engineerSendDate__c = Date.today(); //修理品寄送日
        re.Delivered_Product__c = asset.Id;
        re.Failure_Occurrence_Date__c  = Date.today(); //1.故障发生日
        re.AWS_Data_Id__c = 'sssssswqsss';
        re.Repair_Inspection_Date__c = Date.today().addMonths(-1);
        re.SalesOfficeCode_selection__c = '北京';
        insert re;
        asset.LastSFDCRepairNo__c = re.name;
        update asset;
 
         Repair__c re2 = new Repair__c();
         re2.SAPRepairNo__c = '000010168254';
         re2.Account__c = depart1.Id;
         re2.Department_Class__c = dc1s[0].Id;
         re2.Hospital__c = hp1.Id;
         re2.Cumulative_Uses__c = 0;
         re2.Cumulative_Times__c = 38011;
         re2.AwaitToSendAWS__c = true;
         re2.AWS_Data_Id__c = 'sssssswsss';
         re2.DeliveryLogisticsMode__c = '快递';
         re2.engineerSendDate__c = Date.today(); //修理品寄送日
         re2.Delivered_Product__c = asset.Id;
         re2.Failure_Occurrence_Date__c  = Date.today(); //1.故障发生日
         re2.Cycle_between_failure__c = 12;
         re2.SalesOfficeCode_selection__c = '北京';
         insert re2;
         re2.LastRepairText__c = re.name;
         re2.LastRepair__c = null;
         re2.Repair_Inspection_Date__c = Date.today();
         update re2;
        }
    }
    @isTest
    private static void testMethod1() {
        Oly_TriggerHandler.bypass('RepairTrigger');
        System.runAs ( new User(Id = Userinfo.getUserId()) ){
            Repair__c mc = [select id,name,LastRepairText__c,LastRepair__c,Repair_Inspection_Date__c  from Repair__c where SAPRepairNo__c = '000010168254'];
            List<Repair__c> mcList = [select id,name,LastRepairText__c,LastRepair__c,Repair_Inspection_Date__c,Hospital__c,Product_Unique_Value__c,Difference_in_repair_intervals__c  from Repair__c where LastRepairText__c != null and LastRepair__c = null and Repair_Inspection_Date__c != null];
            System.debug('数据打印:'+mcList);
            List<String> listName = new List<String>();
            listName.add(mc.name);
            LastRepairUpdateBatch.LastRepairText(listName);
            Database.executeBatch(new LastRepairUpdateBatch(mc.Id),1);
            Database.executeBatch(new LastRepairUpdateBatch(),10);
            LastRepairUpdateBatch.calculateDaysBetween( Date.today(),  Date.today().addMonths(2));
        }
    }
}