@isTest
private class ConsumableOrdertosapTest
{
	static testMethod void ConsumableTest_updOrder(){
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectCotac = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '契約'];
        if (rectCotac.size() == 0) {
            return;
        }
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        // 产品
        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;
        // 価格表エントリを作成する
        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};
        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = product.Id);
        insert new Product2__c[] {pro1};
        Account myAccount2 = new Account(Name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id,Product_Limit_Date__c = 'Test01|2|4,Test02|3|5');
        insert myAccount2;


        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount2.id);
        insert core;

        //医疗器械经营许可证
        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 = myAccount2.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_Test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com');
        insert myUser_Test;
        //system.runAs(myUser_Test){
        	 Account contact = new Account(name='Testaccount002',
                                    RecordTypeId = rectCotac[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 =myAccount2.Id,
                                    ET_SP_Dealer__c = true,
                                    ParentId =myAccount2.Id,Business_Assistant__c = UserInfo.getUserId());
        	insert contact;

            Consumable_order__c Order1 = new Consumable_order__c();
            Order1.Name = 'OCM_01_001';
            Order1.Order_status__c = '附件上传完成';
            Order1.Order_type__c = '订单';
            Order1.Dealer_Info__c = myAccount2.Id;
            Order1.Order_effective_contact__c = contact.Id;
            Order1.Consumable_pdf_insert_day__c = Date.today();
            Order1.RecordTypeid = System.Label.RT_ConOrder_Order;
            insert  Order1;

            List<Consumable_order__c> abc = [select Order_effective_contact__c, SalesManager__c from Consumable_order__c where Id = :Order1.Id];
            System.assertEquals(1, abc.size());
            System.assertEquals(contact.Id, abc[0].Order_effective_contact__c);

            //其他订单明细
            //fahuo
            Consumable_orderdetails__c Orderdet = new Consumable_orderdetails__c();
            Orderdet.Name = 'OCM_01_00101';
            Orderdet.Consumable_order__c = Order1.Id;
            Orderdet.RecordTypeId = System.Label.RT_ConOrderDetail1_Order;
            Orderdet.Consumable_Product__c = pro1.Id;
            Orderdet.Consumable_count__c = 1;
            Orderdet.Intra_Trade_List_RMB__c = 10;
            insert Orderdet;
            //Order1.Order_status__c = '已提交';
            System.Test.startTest();
            Order1.Order_status__c = '批准';
            update Order1;
            System.Test.stopTest();
        //}
    }
    static testMethod void ConsumableTest_cancelOrder(){
         StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectCotac = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '契約'];
        if (rectCotac.size() == 0) {
            return;
        }
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        // 产品
        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;
        // 価格表エントリを作成する
        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};
        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = product.Id);
        insert new Product2__c[] {pro1};
        Account myAccount2 = new Account(Name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id,Product_Limit_Date__c = 'Test01|2|4,Test02|3|5');
        insert myAccount2;


        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount2.id);
        insert core;

        //医疗器械经营许可证
        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 = myAccount2.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_Test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com');
        insert myUser_Test;
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        Account contact = new Account(name='Testaccount002',
                                    RecordTypeId = rectCotac[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 =myAccount2.Id,
                                    ET_SP_Dealer__c = true,
                                    ParentId =myAccount2.Id,Business_Assistant__c = UserInfo.getUserId());
            insert contact;

            Consumable_order__c Order1 = new Consumable_order__c();
            Order1.Name = 'OCM_01_001';
            Order1.Order_status__c = '附件上传完成';
            Order1.Order_type__c = '订单';
            Order1.Dealer_Info__c = myAccount2.Id;
            Order1.Order_effective_contact__c = contact.Id;
            Order1.Consumable_pdf_insert_day__c = Date.today();
            Order1.RecordTypeid = System.Label.RT_ConOrder_Order;
            insert  Order1;

            List<Consumable_order__c> abc = [select Order_effective_contact__c, SalesManager__c from Consumable_order__c where Id = :Order1.Id];
            System.assertEquals(1, abc.size());
            System.assertEquals(contact.Id, abc[0].Order_effective_contact__c);

            //其他订单明细
            //fahuo
            Consumable_orderdetails__c Orderdet = new Consumable_orderdetails__c();
            Orderdet.Name = 'OCM_01_00101';
            Orderdet.Consumable_order__c = Order1.Id;
            Orderdet.RecordTypeId = System.Label.RT_ConOrderDetail1_Order;
            Orderdet.Consumable_Product__c = pro1.Id;
            Orderdet.Consumable_count__c = 1;
            Orderdet.Intra_Trade_List_RMB__c = 10;
            insert Orderdet;
            //system.runAs(hpOwner){
            //Order1.Order_status__c = '已提交';
            System.Test.startTest();
            Order1.Order_status__c = '批准';
            update Order1;
            Opportunity[] coc1 = [select id,Name,SAP_Send_OK__c from Opportunity];
            if(coc1.size() > 0){
            	System.assertEquals(1, coc1.size());
	            System.assertEquals(true, coc1[0].SAP_Send_OK__c);
	            //Opportunity opp = new Opportunity();
	            //opp.Id = coc1[0].Id;
	            //opp.SAP_Send_OK__c = false;
	            //update opp;
	            //Order1.Order_status__c = '已提交';
	            //update Order1;
            }
            System.Test.stopTest();
        //}
    }
}