@isTest
private class QuoteTriggerTest {

	@isTest static void test_method_one() {
        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        Account accIE = new Account(
            Name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c='Customer BS',
            FacilityName__c='abc',
            PostCode__c = '123456'
        );
        insert accIE;

        Contact con = new Contact();
        con.LastName = 'test';
        con.AccountId = accIE.Id;
        con.MobilePhone = 'MobilePhoneD__c';
        con.OtherPhone = 'OtherPhoneD__c';
        con.Fax = 'FaxD__c';
        con.Email = 'EmailD__c@test.com';
        con.Phone = 'PhoneD__c';
        con.Title = 'TitleD__c';
        con.Address1__c = 'Address1D__c';
        con.Address2__c = 'Address2D__c';
        con.Address3__c = 'Address3D__c';
        con.Postcode__c = '100111';
        con.ContactStatus__c = 'ContactStatusD__c';
        con.CancelReason__c = 'CancelReasonD__c';
        con.StatusD__c = 'Pass';

        insert con;

		User user = new User();
        user.LastName = 'test';
        user.FirstName = 'test';
        user.Alias = 'test';
        user.Email = 'test@test.com';
        user.Username = 'test111@test222.com';
        user.CommunityNickname = 'test20201015';
        user.IsActive = true;
        user.EmailEncodingKey = 'ISO-2022-JP';
        user.TimeZoneSidKey = 'Asia/Tokyo';
        user.LocaleSidKey = 'ja_JP';
        user.LanguageLocaleKey = 'ja';
        user.ProfileId = System.Label.SystemAdmin;
        insert user;

        User user2 = new User();
        user2.LastName = 'test2';
        user2.FirstName = 'test2';
        user2.Alias = 'test2';
        user2.Email = 'test2@test2.com';
        user2.Username = 'test222@test233666.com';
        user2.CommunityNickname = 'test2';
        user2.IsActive = true;
        user2.EmailEncodingKey = 'ISO-2022-JP';
        user2.TimeZoneSidKey = 'Asia/Tokyo';
        user2.LocaleSidKey = 'ja_JP';
        user2.LanguageLocaleKey = 'ja';
        user2.ProfileId = System.Label.RT_BS_No_Price;
        user2.ContactId = con.Id;
        insert user2;

        PriceBook2 pricebook =new PriceBook2(
            Name = 'IE',
            ProductSegment__c = 'BS',
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            MachineParts__c = 'Machine'
        );
        insert pricebook;

        List<RecordType> rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity'];
        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = accIE.Id,
            RecordTypeId = rectOpp[0].Id,
            OwnerId = UserInfo.getUserId(),
            StageName = 'Prospect Created',
            CancelReason__c = 'Inquiry repeat',
            Cancel_reason_D__c = 'Inquiry repeat',
            CurrencyIsoCode = 'CNY',
            ProductSegment__c = 'BS',
            CloseDate = Date.today(),
            NewInquiryDate__c = Date.today().addDays(-2),
            ExpectedOrderDate__c = Date.today().addDays(2),
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            Machine_Parts__c = 'Machine'
        );
        insert opp;

        OpportunityTeamMember otm1 = new OpportunityTeamMember();
        otm1.opportunityId = opp.Id;
        otm1.userId = user.Id;
        otm1.teamMemberRole = 'Sales Manager';
        insert otm1;

        OpportunityTeamMember otm2 = new OpportunityTeamMember();
        otm2.opportunityId = opp.Id;
        otm2.userId = user2.Id;
        otm2.teamMemberRole = 'Sales Manager';
        insert otm2;

        List<OpportunityTeamMember> otmList = [select Id from OpportunityTeamMember where opportunityId = :opp.Id];
        System.assertEquals(2, otmList.size());

        Quote quo = new Quote(
            Name = 'quo',
            OpportunityId = opp.Id,
            Pricebook2Id = pricebook.Id,
            PaymentTerms__c = 'ZZTT'
        );
        insert quo;

        List<OpportunityTeamMember> otmList2 = [select Id from OpportunityTeamMember where opportunityId = :opp.Id];
        //System.assertEquals(1, otmList2.size());

        quo.PaymentTerms__c = 'Z9TT';

        update quo;
	}

   
      @isTest static void test_init1() {
        Id pricebookId = Test.getStandardPricebookId();

        Pricebook2 pricebook = new Pricebook2(
            Name = 'BS',
            ProductSegment__c = 'BS',
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            MachineParts__c = 'Machine',
            isActive = true
        );
        insert pricebook;

        Product2 product1 = new Product2();
        product1.Name = 'product1';
        product1.ProductCode = 'N5679000';
        product1.Product_ECCode__c = 'product1';
        product1.IsActive = true;

        Product2 product2 = new Product2();
        product2.Name = 'product2';
        product2.ProductCode = 'product2';
        product2.Product_ECCode__c = 'product2';
        product2.IsActive = true;

        Product2 product3 = new Product2();
        product3.Name = 'product3';
        product3.ProductCode = 'product3';
        product3.Product_ECCode__c = 'product3';
        product3.IsActive = true;

        insert new Product2[] {product1, product2, product3};

        PricebookEntry standardPrice1 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product1.Id,
            UnitPrice = 0,
            IsActive = true,
            CurrencyIsoCode = 'CNY'
        );

        PricebookEntry standardPrice2 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product2.Id,
            UnitPrice = 0,
            IsActive = true,
            CurrencyIsoCode = 'CNY'
        );

        PricebookEntry standardPrice3 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product3.Id,
            UnitPrice = 0,
            IsActive = true,
            CurrencyIsoCode = 'CNY'
        );

        insert new PricebookEntry[] {standardPrice1, standardPrice2, standardPrice3};

        PricebookEntry entry1 = new PricebookEntry(
            Pricebook2Id = pricebook.Id,
            Product2Id = product1.Id,
            UnitPrice = 30,
            IsActive = true,
            UseStandardPrice = false,
            CurrencyIsoCode = 'CNY'
        );

        PricebookEntry entry2 = new PricebookEntry(
            Pricebook2Id = pricebook.Id,
            Product2Id = product2.Id,
            UnitPrice = 30,
            IsActive = true,
            UseStandardPrice = false,
            CurrencyIsoCode = 'CNY'
        );

        PricebookEntry entry3 = new PricebookEntry(
            Pricebook2Id = pricebook.Id,
            Product2Id = product3.Id,
            UnitPrice = 30,
            IsActive = true,
            UseStandardPrice = false,
            CurrencyIsoCode = 'CNY'
        );

        insert new PricebookEntry[] {entry1, entry2, entry3};

        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer BS'];
        Account user = new Account(
            Name = '*',
            FacilityName__c = 'user',
            PostCode__c = '123456',
            RecordTypeId = rectIE[0].Id
        );
        insert user;

        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = user.Id,
            StageName = 'Prospect Created',
            CurrencyIsoCode = 'CNY',
            ProductSegment__c = 'BS',
            CloseDate = Date.today(),
            NewInquiryDate__c = Date.today().addDays(-2),
            ExpectedOrderDate__c = Date.today().addDays(2),
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            Machine_Parts__c = 'Machine',
            Pricebook2Id = pricebook.Id
        );
        insert opp;

        OpportunityLineItem oli1 = new OpportunityLineItem(
            OpportunityId = opp.Id,
            PricebookEntryId = entry1.Id,
            Quantity = 1,
            UnitPrice = 30
        );

        OpportunityLineItem oli2 = new OpportunityLineItem(
            OpportunityId = opp.Id,
            PricebookEntryId = entry2.Id,
            Quantity = 2,
            UnitPrice = 30
        );

        OpportunityLineItem oli3 = new OpportunityLineItem(
            OpportunityId = opp.Id,
            PricebookEntryId = entry3.Id,
            Quantity = 3,
            UnitPrice = 30
        );

        insert new OpportunityLineItem[] {oli1, oli2, oli3};

        PageReference page = new PageReference('/apex/NewQuoteService?oppid=' + opp.Id + '&openType=service');
        System.Test.setCurrentPage(page);
        NewQuoteEntryController controller = new NewQuoteEntryController();

        controller.init();

        controller.quo.Name = '报价1';
        controller.quo.PaymentTerms__c = 'Z0LC';
        controller.quo.Shipment_Term__c = 'CIP';
        controller.quo.Shipment_Term2__c = 'BEIJING';
        controller.quo.DeliveryLeadTime__c = 90;
        controller.quo.Warranty__c = 1;
        controller.quo.ExpirationDate = Date.today();
        controller.quo.Custom_Price_Total_Text__c = 1000;

/*        controller.activities[0].pageObject.PricebookEntryId = entry1.Id;
        controller.activities[0].pageObject.Set__c = 'set01';
        controller.activities[0].pageObject.UnitPrice = 11;
        controller.activities[0].pageObject.Quantity = 1
        controller.activities[0].pageObject.Discount = 0;
        controller.activities[0].pageObject.Custom_Price__c = 20;
        controller.activities[0].pageObject.Description = 'test1';*/
        
        controller.Save();

        //system.assertEquals('',controller.errormessage);

        controller.excelImport();

        controller.OppReflection();

        controller.BackBtn();
        System.Test.StartTest();
        controller.Decide();
        List<Quote> QuoteList = [select Id from Quote]; 
        //update QuoteList[0];
        if(QuoteList.size() > 0){
            update QuoteList[0];
        }
        System.Test.StopTest();
    }


}