高章伟
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
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
@isTest
private class OpportunityHpDeptUpdTriggerTest {
 
    static testMethod void testUpdate() {
        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        if (rectDpt.size() == 0) {
            return;
        }
 
        // insert
        Account company1 = new Account();
        Account company2 = new Account();
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name = '病院テスト1';
        company2.RecordTypeId = rectCo[0].Id;
        company2.Name = '病院テスト2';
        List<Account> hps = new Account[] {company1, company2};
        insert hps;
  
        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c];
        List<Account> dc2s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c];
 
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = company1.Id;
        
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[1].Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dc2s[1].Id;
        depart2.Department_Class__c = dc2s[1].Id;
        depart2.Hospital__c         = company2.Id;
 
        insert new Account[] {depart1, depart2};
 
        Opportunity opp1 = new Opportunity(Name='testOwner', StageName='contact', CurrencyIsoCode='CNY', CloseDate=Date.today(),AccountId=depart1.Id);
 
        Opportunity opp2 = new Opportunity(Name='testOwner2', StageName='contact', CurrencyIsoCode='CNY', CloseDate=Date.today(),AccountId=depart2.Id);
        
        insert new Opportunity[] {opp1, opp2};
        
        //まずひとつ追加
        //まずひとつ
        Opportunity oppChanged = [Select Id, AccountId, Department_Class__c, Hospital__c from Opportunity where Id = :opp1.Id ];
        System.assertEquals(oppChanged.Department_Class__c, dc1s[0].Id);
        System.assertEquals(oppChanged.Hospital__c , company1.Id);
 
        Opportunity oppChange2 = [Select Id, AccountId, Department_Class__c, Hospital__c from Opportunity where Id = :opp2.Id];        
        System.assertEquals(oppChange2.Department_Class__c, dc2s[1].Id);
        System.assertEquals(oppChange2.Hospital__c , company2.Id);
        
        opp1.AccountId = depart2.Id;
        opp2.AccountId = depart1.Id;
        update new Opportunity[] {opp1, opp2};
        
        //まずひとつ
        oppChanged = [Select Id, AccountId, Department_Class__c, Hospital__c from Opportunity where Id = :opp1.Id ];
        System.assertEquals(oppChanged.Department_Class__c, dc2s[1].Id);
        System.assertEquals(oppChanged.Hospital__c , company2.Id);
 
        oppChange2 = [Select Id, AccountId, Department_Class__c, Hospital__c from Opportunity where Id = :opp2.Id];        
        System.assertEquals(oppChange2.Department_Class__c, dc1s[0].Id);
        System.assertEquals(oppChange2.Hospital__c , company1.Id);
 
    }
 
    @isTest
    static void testLookupIds() {
        // OlympusCalendar にデータを追加
        OlympusCalendar__c cal11 = new OlympusCalendar__c(
            Date__c = Date.today()
        );
        OlympusCalendar__c cal12 = new OlympusCalendar__c(
            Date__c = Date.today().addDays(1)
        );
        OlympusCalendar__c cal13 = new OlympusCalendar__c(
            Date__c = Date.today().addDays(2)
        );
        OlympusCalendar__c cal14 = new OlympusCalendar__c(
            Date__c = Date.today().addDays(3)
        );
        insert new OlympusCalendar__c[] {cal11, cal12, cal13, cal14};
 
        // recode type を取得
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw(new ControllerUtil.myException('Not Exist RecordType 病院'));
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc];
        if (rectDpt.size() == 0) {
            throw(new ControllerUtil.myException('Not Exist RecordType 診療科 消化科, 診療科 呼吸科'));
        }
 
        // insert
        Account company1 = new Account();
        Account company2 = new Account();
        company1.RecordTypeId = rectCo[0].Id;
        company1.Name = 'HPテスト1';
        company2.RecordTypeId = rectCo[0].Id;
        company2.Name = 'HPテスト2';
        List<Account> hps = new Account[] {company1, company2};
        insert hps;
  
        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id and Department_Class_Label__c = '消化科'];
        List<Account> dc2s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company2.Id and Department_Class_Label__c = '呼吸科'];
        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[0].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = company1.Id;
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[1].Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dc2s[0].Id;
        depart2.Department_Class__c = dc2s[0].Id;
        depart2.Hospital__c         = company2.Id;
        insert new Account[] {depart1, depart2};
 
        Opportunity opp1 = new Opportunity(Name='testOwner', Opportunity_Category__c='GI', StageName='contact', CurrencyIsoCode='USD',
            Close_Forecasted_Date__c = Date.today().addDays(-28), Bid_Planned_Date__c = Date.today().addDays(1), CloseDate=Date.today(), AccountId=depart1.Id);
        Opportunity opp2 = new Opportunity(Name='testOwner2', Opportunity_Category__c='GS', StageName='contact', CurrencyIsoCode='USD',
            Close_Forecasted_Date__c = Date.today().addDays(-26), Bid_Planned_Date__c = Date.today().addDays(10), CloseDate=Date.today(), AccountId=depart2.Id);
 
        System.Test.startTest();
        insert new Opportunity[] {opp1, opp2};
        System.Test.stopTest();
 
        opp1 = [Select Calendar_Created_Date_ID__c, CloseDate, Calendar_Forecast_Shipping_Date_ID__c, Calendar_Forecast_Date_ID__c, Calendar_Forecast_Tender_Date_ID__c from Opportunity where Id =: opp1.Id];
        System.assertEquals(cal11.Id, opp1.Calendar_Created_Date_ID__c);
        System.assertEquals(cal13.Date__c, opp1.CloseDate);                     // Close_Forecasted_Date__c + 30
        System.assertEquals(cal13.Id, opp1.Calendar_Forecast_Shipping_Date_ID__c);
        System.assertEquals(null, opp1.Calendar_Forecast_Date_ID__c);
        System.assertEquals(cal12.Id, opp1.Calendar_Forecast_Tender_Date_ID__c);
        opp2 = [Select Calendar_Created_Date_ID__c, Calendar_Forecast_Shipping_Date_ID__c, Calendar_Forecast_Date_ID__c, Calendar_Forecast_Tender_Date_ID__c from Opportunity where Id =: opp2.Id];
        System.assertEquals(cal11.Id, opp2.Calendar_Created_Date_ID__c);
        System.assertEquals(null, opp2.Calendar_Forecast_Shipping_Date_ID__c);  // Close_Forecasted_Date__c + 30
        System.assertEquals(null, opp2.Calendar_Forecast_Date_ID__c);
        System.assertEquals(null, opp2.Calendar_Forecast_Tender_Date_ID__c);
        
        // 更新テスト
        opp1.Opportunity_Category__c = 'GS';
        opp1.Calendar_Created_Date_ID__c = cal12.Id;
        opp1.Close_Forecasted_Date__c = Date.today().addDays(2);
        opp2.Opportunity_Category__c = 'ET';
        opp2.CloseDate = Date.today();
        opp2.Bid_Planned_Date__c = Date.today().addDays(2);
        update new Opportunity[] {opp1, opp2};
 
        opp1 = [Select Calendar_Created_Date_ID__c, Calendar_Forecast_Shipping_Date_ID__c, Calendar_Forecast_Date_ID__c, Calendar_Forecast_Tender_Date_ID__c from Opportunity where Id =: opp1.Id];
        System.assertEquals(cal12.Id, opp1.Calendar_Created_Date_ID__c);
        System.assertEquals(cal13.Id, opp1.Calendar_Forecast_Shipping_Date_ID__c);
        System.assertEquals(cal13.Id, opp1.Calendar_Forecast_Date_ID__c);
        System.assertEquals(cal12.Id, opp1.Calendar_Forecast_Tender_Date_ID__c);
        opp2 = [Select Calendar_Created_Date_ID__c, Calendar_Forecast_Shipping_Date_ID__c, Calendar_Forecast_Date_ID__c, Calendar_Forecast_Tender_Date_ID__c from Opportunity where Id =: opp2.Id];
        System.assertEquals(cal11.Id, opp2.Calendar_Created_Date_ID__c);
        System.assertEquals(cal11.Id, opp2.Calendar_Forecast_Shipping_Date_ID__c);
        System.assertEquals(null, opp2.Calendar_Forecast_Date_ID__c);
        System.assertEquals(cal13.Id, opp2.Calendar_Forecast_Tender_Date_ID__c);
    }
}