@isTest
private class OpdPlan_CreatePlanRentalEquipmentTest {
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    static testMethod void init() {
        RecordType rectCam = [SELECT Id FROM RecordType
                        WHERE IsActive = true AND SobjectType = 'Campaign'
                        AND DeveloperName = 'Training_event'];
        
        Campaign cam = new Campaign();
        cam.Name = 'cam';
        cam.Name2__c = '1234';
        cam.RecordTypeId = rectCam.Id;
        cam.StartDate = Date.today().addDays(-15);
        cam.EndDate = Date.today().addDays(18);
        cam.Mailflg_after45__c = true;
        cam.Mailflg_cancel__c = true;
        cam.Mailflg_before15__c = true;
        cam.Mailflg_before7__c = true;
        cam.Mailflg_after3__c = true;
        cam.Is_LendProduct__c = '是';
        insert cam;

        Opportunity opp = new Opportunity(
            Name='Opp',
            StageName='引合',
            CloseDate=Date.today().addDays(10),
            Close_Forecasted_Date__c=Date.today(),

            CurrencyIsoCode = 'CNY'
        );
        insert opp;
        
        Quote q = new Quote(
            Name = 'quote',
            OpportunityId = opp.Id,
            Pricebook2Id = pricebookId
        );
        insert q;

        opp.Estimation_Id__c = q.Id;
        update opp;

        Product2 prd1 = new Product2();
            prd1.Name     = 'Prd1';
            prd1.ProductCode_Ext__c     = 'Prd1';
            prd1.ProductCode            = 'Prd1';
            prd1.Asset_Model_No__c = '1001';
            prd1.Important_product__c = true;
            prd1.Important_Rroduct_1GI__c = true;
            prd1.RentalSubject__c = true;
            insert prd1;  

        PricebookEntry entry = new PricebookEntry( Pricebook2Id = pricebookId, Product2Id = prd1.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        insert entry;

        QuoteLineItem quoteItem = new QuoteLineItem();
        quoteItem.Name__c = 'テスト商品';
        quoteItem.QuoteId = q.id;
        quoteItem.Quantity = 1;
        quoteItem.Cost_Subtotal__c = 0;
        quoteItem.Cost__c = 200;
        quoteItem.Item_Order__c  = 3;
        quoteItem.UnitPrice = 10;
        quoteItem.Product2Id = prd1.id;
        quoteItem.PricebookEntryId = entry.Id;
        quoteItem.OppIsLendMark__c = true;
        insert quoteItem;


        Id planRERecordTypeCampaign = Schema.SObjectType.Plan_Rental_Equipment__c.getRecordTypeInfosByDeveloperName().get('campaign').getRecordTypeId();
        Plan_Rental_Equipment__c planRE2 = new Plan_Rental_Equipment__c();
        planRE2.Campaign__c = cam.Id;
        planRE2.Rental_Equipment__c = 'CF-H170I';
        planRE2.Name = 'CF-H170I';
        planRE2.Rental_Quantity__c = '10';
        planRE2.ProductCode__c = 'N4480080';
        planRE2.MDM_Model_No__c = 'CF-H170I';
        planRE2.RecordTypeId = planRERecordTypeCampaign;
        insert planRE2;

        //OPD计划
        OPDPlan__c oPDPlan = new OPDPlan__c();
        oPDPlan.Status__c = '草案中';
        oPDPlan.OPDPlan_ImplementDate__c =  Date.today().addMonths(1);
        oPDPlan.NoOpp_Reason__c = 'HCP对应';     //无询价理由
        oPDPlan.OPDLendSortDraft__c = 1;       //备品借出优先度
        oPDPlan.OPDType__c = '询价';
        oPDPlan.Related_Opportunity1_ID__c = opp.Id;
        oPDPlan.NoOpp_Reason__c = null;
        insert oPDPlan;

        oPDPlan.Status__c = '计划中';
        update oPDPlan;

        OPDPlan__c oPDPlan1 = new OPDPlan__c();
        oPDPlan1.Status__c = '取消';
        oPDPlan1.OPDPlan_ImplementDate__c =  Date.today().addMonths(1);
        oPDPlan1.NoOpp_Reason__c = 'HCP对应';     //无询价理由
        oPDPlan1.OPDLendSortDraft__c = 1;       //备品借出优先度
        oPDPlan1.OPDType__c = '学会';
        oPDPlan1.Campaign__c = cam.Id;
        // oPDPlan1.NoOpp_Reason__c = 'HCP对应';
        insert oPDPlan1;

        Plan_Rental_Equipment__c planRE1 = new Plan_Rental_Equipment__c();
        planRE1.OPD_Plan__c = oPDPlan.Id;
        planRE1.Rental_Equipment__c = 'CF-H170I';
        planRE1.Name = 'CF-H170I';
        planRE1.Rental_Quantity__c = '10';
        planRE1.ProductCode__c = 'N4480080';
        planRE1.MDM_Model_No__c = 'CF-H170I';
        insert planRE1;
        
        Test.startTest();

        cam.Status = '公开中';
        update cam;
        // oPDPlan.Status__c = '提交';
        // update oPDPlan;


         Test.stopTest();


    }   
}