@isTest
private class SendConsumableordertosapTest {
    static testMethod void send_test(){
        StaticParameter.EscapeNFM007Trigger = true;
        List<RecordType> conOrder_Order = [select Id from RecordType where IsActive = true and SobjectType = 'Consumable_order__c' and Name = '01 订单'];
        if (conOrder_Order.size() == 0) {
            return;
        }

        List<RecordType> conOrderDetail_Order = [select Id from RecordType where IsActive = true and SobjectType = 'Consumable_orderdetails__c' and Name = '订单'];
        if (conOrderDetail_Order.size() == 0) {
            return;
        }

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }

        List<RecordType> rectContract = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '契約'];
        if (rectCo.size() == 0) {
            return;
        }

        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET)'];
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        // 产品
        Product2 product = new Product2( Name='テスト商品');
        product.SFDA_Status__c = '有効';
        product.Intra_Trade_List_RMB_1__c = 100;
        product.Intra_Trade_List_RMB_Date1__c = date.today();
        product.Intra_Trade_Cost_RMB_1__c = 200;
        product.Intra_Trade_Cost_RMB_Date1__c = date.today();
        product.Manual_Entry__c = false;
        product.Asset_Model_No__c = '11111';
        product.ProductCode = 'OTV-SP1H-NA-12E';
        product.Product_Status__c = '正常销售';
        product.SFDA_Approbated_Status__c = '有效';
        insert product;
        Product2 pro = [select id,SFDA_Status__c,SFDA_Status_New__c,Estimation_Entry_Possibility__c from Product2 where id = :product.Id];
        System.assertEquals('有効', pro.SFDA_Status_New__c);
        System.assertEquals('有効', pro.SFDA_Status__c);
        System.assertEquals('○', pro.Estimation_Entry_Possibility__c);

        // 価格表エントリを作成する
        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=product.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        insert new PricebookEntry[] {entry};
        //经销商、联系人、用户信息
        Account myAccount1 = new Account(name='Testaccount001',
                                        Dealer_discount__c =20,
                                        Ban_On_Use_Date__c = Date.today().addDays(1),
                                        Business_Paper_Expiration_Date__c = Date.today().addDays(1),
                                        Tax_Practice_Expiration_Date__c = Date.today().addDays(1),
                                        Medical_Equipment_Expiration_Date__c = Date.today().addDays(1),
                                        RecordTypeId = rectCo[0].Id);
        insert myAccount1;
        Account acc1 = [select Id,Name,Is_Active_Formula__c from Account where Id = : myAccount1.Id];
        System.assertEquals('有效', acc1.Is_Active_Formula__c);
        Contact core1 = new Contact(email='test@salesforce.com', 
                                    firstname='test',
                                    lastname='001',
                                    accountid=myAccount1.Id);
        insert core1;
        //医疗器械经营许可证
        License_Information__c linc = new License_Information__c( name='Test20181204',
                                                LicenseType__c = '医疗器械经营许可证',
                                                BusinessLicense__c = '20180522',
                                                ValidFrom__c = date.newinstance(2018, 05, 22),
                                                ValidTo__c = date.newinstance(2088, 05, 22),
                                                Scope3__c = '6815;6822;6823;6825',
                                                LicenseAndAccount__c = myAccount1.Id
                                                );
        insert linc;
        //System.assertEquals('123',linc.ScopeKey__c);
        //产品注册证
        Product_Register__c prc = new Product_Register__c(Name = '国械注进20162220210',
                                                MedPrdClass__c = '3',
                                                ValidFrom__c = date.newinstance(2018, 07, 22),
                                                ValidTo__c = date.newinstance(2028, 07, 22),
                                                RegisterNoClass_Old__c = '6815',
                                                RegisterNoClass_New__c = '6815',
                                                RegisterNoStatus__c = '有效'
                                                );
        insert prc;
        //新旧关系对照表
        Product_Register_contrast__c prcc = new Product_Register_contrast__c(
                                                Name = 'Test20181204',
                                                Register_new__c = '314',
                                                Register_old__c = '36815'
                                                );
        insert prcc;
        //产品-产品注册证关系
        Product_Register_Link__c  prlc = new Product_Register_Link__c(
                                                Product2__c = product.Id,
                                                Product_Register__c = prc.Id
                                                ); 
        insert prlc;
         user MyUser_Test1 = New User(ContactId = core1.Id,
                                    Alias = 'newUser1',
                                    Email='newuser1@testorg.com',
                                    EmailEncodingKey='UTF-8', 
                                    LastName='TestUser1', 
                                    LanguageLocaleKey='zh_CN',
                                    LocaleSidKey='zh_CN',
                                    Province__c = '北京市',
                                    ProfileId = prof.Id,
                                    TimeZoneSidKey='Asia/Shanghai', 
                                    UserName='testUser1@testorg.com');
        insert MyUser_Test1;
        //经销商有效合同
        Account myAccount2 = new Account(name='Testaccount002',
                                            RecordTypeId = rectContract[0].Id,
                                            Contract_Decide_Start_Date__c = Date.today().addDays(-1),
                                            Contract_Decide_End_Date__c =Date.today().addDays(1),
                                            Contract_Start_Date__c = Date.today().addDays(-1),
                                            Contract_End_Date__c =Date.today().addDays(1),
                                            Agent_Ref__c =myAccount1.Id,
                                            ET_SP_Dealer__c = true,
                                            ParentId =myAccount1.Id);
        insert myAccount2;
        Account acc2 = [select Id,Name,Is_Active_Formula__c from Account where Id = : myAccount2.Id];
        System.assertEquals('有效', acc2.Is_Active_Formula__c);


        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='NFM110Prd1',Product2__c=product.id);
        //Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='NFM110Prd2',Product2__c=prd2.id);
        insert new Product2__c[] {pro1};
        //消耗品订单
        Consumable_order__c coc = new Consumable_order__c(Name = 'Name001',
                                                            Dealer_Info__c = myAccount1.Id,
                                                            Order_status__c = '批准',
                                                            RecordTypeid = conOrder_Order[0].Id,
                                                            Contract_application_decision__c ='Name001'
                                                            ,SalesManager__c = MyUser_Test1.Id,
                                                            Order_effective_contact__c = myAccount2.Id
                                                            );
        insert coc;
        //消耗品明细1
        Consumable_orderdetails__c codc = new Consumable_orderdetails__c(Name='orderdetails001',                                                                            Consumable_order__c = coc.id,
                                                                            Consumable_Product__c =pro1.id,
                                                                            RecordTypeId =conOrderDetail_Order[0].Id,
                                                                            Consumable_count__c = 2,
                                                                            Intra_Trade_List_RMB__c = 100);
        insert codc;
        System.Test.startTest();
        string aaa =  SendConsumableordertosap.sendSAP(coc.id);
        System.assertEquals('', aaa);
        System.Test.stopTest();
        Opportunity[] coc1 = [select id,Name,SAP_Province__c from Opportunity];
        System.assertEquals(1, coc1.size());
        System.assertEquals('北京市', coc1[0].SAP_Province__c);

    }

}