高章伟
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
@isTest
private class SyncMBOpportunityLineItemTest {
 
    private static Product2 product = null;
    private static Id pricebookId = null;
 
    private static  Opportunity opprtunity = null;
 
    static final String RC_HOSPITAL = '病院';
    static final String RC_OPP = '引合';
    static final String RC_SENRYAKUKASHITSUBUNRUI = '戦略科室分類 消化科';
    static final String RC_BYOUIN = '病院';
    static final String RC_SHINRYOUKA = '診療科 その他';
    static final String RC_HANBAOITEN = '販売店';
    static final String RC_KEIYAKU = '契約';
 
    static {
        product = new Product2( Name='テスト商品');
        insert product;
        system.debug('##product.Id=[' + product.Id + ']' );
 
        pricebookId = ControllerUtil.getStandardPricebook().Id; 
    }
 
    public static String trade = '内貿';
    public static String CurrencyIso = 'CNY';
 
    private static PricebookEntry buildPB( String input) {
        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=product.Id);
 
        entry.UnitPrice = 10;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = input;
        insert entry;
        return entry;
    }
 
    private static Opportunity buildOppInstance( String inputTrade, String CurrencyIsoCode, Id recordTypeId) {
        Opportunity target = new Opportunity( Name='aiueo', StageName='contact', CloseDate=Date.today());
        target.recordTypeId = recordTypeId;
        target.Trade__c = inputTrade;
        target.CurrencyIsoCode = CurrencyIsoCode;
        target.Wholesale_Price__c = 9000;
        target.SAP_Province__c = '青海省';
        target.Opportunity_Category__c = 'GIGI';
        target.Distributor_InCharge_opp__c = true;
        return target;
    }
 
    // MB_询价产品
    static testMethod void SyncMBOpportunityListTest() {
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', Province__c = '新疆自治区',
                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 = '病院'];
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科'];
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'SoakupTestHp', OwnerId = hpOwner.Id,
                OCM_Category__c = 'LLL'
        );
        insert hp;
        List<Account> dcs = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp.Id and Department_Class_Label__c = '呼吸科'];
 
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = '診療科';
        depart.ParentId            = dcs[0].Id;
        depart.Department_Class__c = dcs[0].Id;
        depart.Hospital__c         = hp.Id;
        insert depart;
 
        // PricebookEntry
        PricebookEntry pbEntry = buildPB(CurrencyIso);
 
        // 引合作成
        Opportunity opp = buildOppInstance(trade, CurrencyIso, '01210000000QekK');
        opp.AccountId = depart.Id;
        opp.Hospital__c = hp.Id;
        opp.Trade__c = trade;
        opp.Sales_Root__c = 'AAA';
        opp.Pricebook2Id = pricebookId;
        insert opp;
 
        List<MB_OpportunityLineItem__c> mbopplis1 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode from MB_OpportunityLineItem__c where Opportunity__c = :opp.Id];
        System.assertEquals(0, mbopplis1.size());
 
        // MB_询价产品
        OpportunityLineItem oppli = New OpportunityLineItem(
            OpportunityId = opp.Id,
            PricebookEntryId = pbEntry.Id,
            Quantity = 1,
            TotalPrice = 10
        );
        insert oppli;
 
        mbopplis1 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode, 
                            State_Text__c, Opp_OCM_text__c, Opportunity_Category_Text__c, Distributor_InCharge_opp__c
                       from MB_OpportunityLineItem__c where Opportunity__c = :opp.Id];
        //System.assertEquals(1, mbopplis1.size());
        //System.assertEquals(oppli.Id, mbopplis1[0].OpportunityLineItemId__c);
        //System.assertEquals(18, mbopplis1[0].OpportunityLineItemId__c.length());
        //System.assertEquals(opp.Id, mbopplis1[0].Opportunity__c);
        //System.assertEquals(product.Id, mbopplis1[0].Product2__c);
        //System.assertEquals(CurrencyIso, mbopplis1[0].CurrencyIsoCode);
        //System.assertEquals('青海省', mbopplis1[0].State_Text__c);
        //System.assertEquals('LLL', mbopplis1[0].Opp_OCM_text__c);
        //System.assertEquals('GIGI', mbopplis1[0].Opportunity_Category_Text__c);
        //System.assertEquals(true, mbopplis1[0].Distributor_InCharge_opp__c);
 
        opp.SAP_Province__c = '北京市';
        update opp;
 
        mbopplis1 = [select Id, Opportunity__c, State_Text__c, Opp_OCM_text__c, Opportunity_Category_Text__c, Distributor_InCharge_opp__c from MB_OpportunityLineItem__c];
        //System.assertEquals(1, mbopplis1.size());
        //System.assertEquals('北京市', mbopplis1[0].State_Text__c);
 
        //delete oppli;
 
        List<MB_OpportunityLineItem__c> mbopplis3 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode from MB_OpportunityLineItem__c where Opportunity__c = :opp.Id];
        //System.assertEquals(0, mbopplis3.size());
    }
 
    // MB_询价产品
    static testMethod void SyncMBTargetListTest() {
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', Province__c = '新疆自治区',
                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
        );
        User oppOwner = new User(Test_staff__c = true, LastName = 'opp', Province__c = '青海省',
                FirstName = 'owner', Alias = 'opp', CommunityNickname = 'oppOwner', Email = 'olympus_oppowner@sunbridge.com', Username = 'olympus_oppowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id
        );
        insert new User[] {hpOwner, oppOwner};
        // 取引先作成
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科'];
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'SoakupTestHp', OwnerId = hpOwner.Id,
                OCM_Category__c = 'LLL'
        );
        insert hp;
        List<Account> dcs = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp.Id and Department_Class_Label__c = '呼吸科'];
 
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = '診療科';
        depart.ParentId            = dcs[0].Id;
        depart.Department_Class__c = dcs[0].Id;
        depart.Hospital__c         = hp.Id;
        insert depart;
 
        // PricebookEntry
        PricebookEntry pbEntry = buildPB(CurrencyIso);
 
        // 引合作成
        Opportunity opp = buildOppInstance(trade, CurrencyIso, '01210000000QekP');      // 目標
        opp.Target_category__c = '担当目标';
        opp.OwnerId = oppOwner.Id;
        opp.AccountId = depart.Id;
        opp.Hospital__c = hp.Id;
        opp.Trade__c = trade;
        opp.Sales_Root__c = 'AAA';
        opp.Pricebook2Id = pricebookId;
        insert opp;
 
        List<MB_TargetLineItem__c> mbopplis1 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode from MB_TargetLineItem__c where Opportunity__c = :opp.Id];
        //System.assertEquals(0, mbopplis1.size());
        List<MB_OpportunityLineItem__c> mbopplis3 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode from MB_OpportunityLineItem__c where Opportunity__c = :opp.Id];
        //System.assertEquals(0, mbopplis3.size());
 
        // MB_询价产品
        OpportunityLineItem oppli = New OpportunityLineItem(
            OpportunityId = opp.Id,
            PricebookEntryId = pbEntry.Id,
            Quantity = 1,
            TotalPrice = 10
        );
        insert oppli;
 
        mbopplis1 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode, 
                            State_Text__c, Opp_OCM_text__c, Opportunity_Category_Text__c, Distributor_InCharge_opp__c
                       from MB_TargetLineItem__c where Opportunity__c = :opp.Id];
        //System.assertEquals(1, mbopplis1.size());
        //System.assertEquals(oppli.Id, mbopplis1[0].OpportunityLineItemId__c);
        //System.assertEquals(18, mbopplis1[0].OpportunityLineItemId__c.length());
        //System.assertEquals(opp.Id, mbopplis1[0].Opportunity__c);
        //System.assertEquals(product.Id, mbopplis1[0].Product2__c);
        //System.assertEquals(CurrencyIso, mbopplis1[0].CurrencyIsoCode);
        //System.assertEquals('青海省', mbopplis1[0].State_Text__c);
        //System.assertEquals('LLL', mbopplis1[0].Opp_OCM_text__c);
        //System.assertEquals('GIGI', mbopplis1[0].Opportunity_Category_Text__c);
        //System.assertEquals(true, mbopplis1[0].Distributor_InCharge_opp__c);
        mbopplis3 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode from MB_OpportunityLineItem__c where Opportunity__c = :opp.Id];
        //System.assertEquals(1, mbopplis3.size());
 
        //delete oppli;
 
        mbopplis1 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode from MB_TargetLineItem__c where Opportunity__c = :opp.Id];
        //System.assertEquals(0, mbopplis1.size());
        mbopplis3 = [select Id, Opportunity__c, Product2__c, OpportunityLineItemId__c, CurrencyIsoCode from MB_OpportunityLineItem__c where Opportunity__c = :opp.Id];
        //System.assertEquals(0, mbopplis3.size());
   }
}