@isTest
private class Consumable_order_AlertControllerTest {
	
	@isTest static void test_method_one() {
		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重验证'];
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        // 产品
        Product2 prd1 = new Product2(
            Name='name01',
            IsActive=true,
            Asset_Model_No__c='n01',
            ProductCode_Ext__c='pc01',
            Manual_Entry__c=false,
            SFDA_Status__c='有効',
            Intra_Trade_List_RMB_Date1__c=Date.today().addDays(-1),
            Intra_Trade_List_RMB_Date2__c=Date.today().addDays(-1),
            Intra_Trade_List_RMB_End_Date1__c=Date.today().addDays(1),
            Intra_Trade_List_RMB_End_Date2__c=Date.today().addDays(1),
            Intra_Trade_List_RMB_1__c=100,
            Intra_Trade_List_RMB_2__c=200,
            Intra_Trade_Cost_RMB_Date1__c=Date.today().addDays(-1),
            Intra_Trade_Cost_RMB_Date2__c=Date.today().addDays(-1),
            Intra_Trade_Cost_RMB_End_Date1__c=Date.today().addDays(1),
            Intra_Trade_Cost_RMB_End_Date2__c=Date.today().addDays(1),
            Intra_Trade_Cost_RMB_1__c=10,
            Intra_Trade_Cost_RMB_2__c=20
        );
        insert new Product2[] {prd1};

        // 価格表エントリを作成する
        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=prd1.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;
         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;
        //经销商有效合同
        system.runAs(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=prd1.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,
	                                                            Order_type__c = '订单'


	                                                            );
	        insert coc;
	        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;
	        
	        PageReference ref = new PageReference('/apex/Consumable_order_Alert?id=' + coc.id);
	        Test.setCurrentPage(ref);
	       	Consumable_order_AlertController controller = new Consumable_order_AlertController();
	        controller.init();
        }
	}
	

	
}