/**
 * @description       : 
 * @author            : ChangeMeIn@UserSettingsUnder.SFDoc
 * @group             : 
 * @last modified on  : 04-21-2022
 * @last modified by  : ChangeMeIn@UserSettingsUnder.SFDoc
**/
@isTest
private class OpportunityTriggerTest {
    
    // 关闭询价插入和更新
    static testMethod void myTest1() {
        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 IE',
            FacilityName__c='abc',
            PostCode__c = '123456'
        );
        insert accIE; 
        
        PriceBook2 pricebook =new PriceBook2(
            Name = 'IE',
            ProductSegment__c = 'IE',
            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 = 'IE',
            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;

        OpportunityWebService.UpdateStageName(opp.Id, 'Closed Cancel');
        update opp;
        
        List<Opportunity> oppList = [Select Id, CloseDate, ProductSegmentCompetitor__c, Pricebook2Id from Opportunity where Id = :opp.Id];
        
        //System.assertEquals(Date.today().addDays(2), oppList[0].CloseDate);
        //System.assertEquals('IE', oppList[0].ProductSegmentCompetitor__c);
        //System.assertEquals(pricebook.Id, oppList[0].Pricebook2Id);
        
    }
    
    // 未关闭询价插入和更新
    static testMethod void myTest2() {
        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 IE',
            FacilityName__c='abc',
            PostCode__c = '123456'
        );
        insert accIE; 
        
        PriceBook2 pricebook =new PriceBook2(
            Name = 'IE',
            ProductSegment__c = 'IE',
            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',
            CurrencyIsoCode = 'CNY',
            ProductSegment__c = 'IE',
            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;
        
        List<Opportunity> oppList = [Select Id, CloseDate, ProductSegmentCompetitor__c, Pricebook2Id from Opportunity where Id = :opp.Id];
        
        //System.assertEquals(Date.today().addDays(2), oppList[0].CloseDate);
        //System.assertEquals('IE', oppList[0].ProductSegmentCompetitor__c);
        //System.assertEquals(pricebook.Id, oppList[0].Pricebook2Id);
        
        opp.ExpectedOrderDate__c = Date.today().addDays(3);
        opp.ProductSegment__c = 'BS';
        update opp;
        
        List<Opportunity> oppList2 = [Select Id, CloseDate, ProductSegmentCompetitor__c, Pricebook2Id from Opportunity where Id = :opp.Id];
        
        //System.assertEquals(Date.today().addDays(3), oppList2[0].CloseDate);
        //System.assertEquals('BS', oppList2[0].ProductSegmentCompetitor__c);
        
        // 无预定下单日
        Opportunity opp2 = new Opportunity(
            Name = 'test opp',
            AccountId = accIE.Id,
            RecordTypeId = rectOpp[0].Id,
            OwnerId = UserInfo.getUserId(),
            StageName = 'Prospect Created',
            CurrencyIsoCode = 'CNY',
            ProductSegment__c = 'IE',
            CloseDate = Date.today(),
            NewInquiryDate__c = Date.today().addDays(-2),
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            Machine_Parts__c = 'Machine'
        );
        insert opp2;
        
        List<Opportunity> oppList3 = [Select Id, CloseDate, ProductSegmentCompetitor__c, Pricebook2Id from Opportunity where Id = :opp2.Id];
        
        //System.assertEquals(Date.today().addDays(-2).addMonths(6), oppList3[0].CloseDate);
        //System.assertEquals('IE', oppList3[0].ProductSegmentCompetitor__c);
        //System.assertEquals(pricebook.Id, oppList3[0].Pricebook2Id);
        
        opp2.NewInquiryDate__c = Date.today().addDays(-3);
        opp2.ProductSegment__c = 'BS';
        update opp2;
        
        List<Opportunity> oppList4 = [Select Id, CloseDate, ProductSegmentCompetitor__c, Pricebook2Id from Opportunity where Id = :opp2.Id];
        
        //System.assertEquals(Date.today().addDays(-3).addMonths(6), oppList4[0].CloseDate);
        //System.assertEquals('BS', oppList4[0].ProductSegmentCompetitor__c);
    }
    //@isTest(SeeAllData=true)
    static testMethod void myTest3() {
        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        List<RecordType> rectForeignCompany = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'ForeignCompany'];
        Id pricebookId = Test.getStandardPricebookId();
        
        /*PriceBook2 pricebook1 = ControllerUtil.getStandardPricebook();
        Id pricebookId = pricebook1.id;*/
        Pricebook2 pricebook = new Pricebook2(
            Name = 'IE',
            ProductSegment__c = 'IE',
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            MachineParts__c = 'Machine',
            isActive = true
        );
        insert pricebook;
        
        /*Pricebook2 pricebook =  [select Id from PriceBook2 where ProductSegment__c = 'IE' and
         TradeType__c = 'Taxation' and SalesChannel__c = 'direct' and MachineParts__c = 'Machine' and  isActive = true limit 1];
*/
        Product2 product1 = new Product2();
        product1.Name = 'product1';
        product1.IsActive = true;
        
        Product2 product2 = new Product2();
        product2.Name = 'product2';
        product2.IsActive = true;
        
        Product2 product3 = new Product2();
        product3.Name = 'product3';
        product3.IsActive = true;
        
        insert new Product2[] {product1,product2,product3};
        
        PricebookEntry standardPrice1 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product1.Id,
            UnitPrice = 0,
            CurrencyIsoCode = 'CNY',
            IsActive = true

        );
        
        PricebookEntry standardPrice2 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product2.Id,
            UnitPrice = 0,
            CurrencyIsoCode = 'CNY',
            IsActive = true
        );
        
        PricebookEntry standardPrice3 = new PricebookEntry(
            Pricebook2Id = pricebookId,
            Product2Id = product3.Id,
            UnitPrice = 0,
            CurrencyIsoCode = 'CNY',
            IsActive = true
        );
        
        insert new PricebookEntry[] {standardPrice1,standardPrice2,standardPrice3};
        
        PricebookEntry entry1 = new PricebookEntry( Pricebook2Id=pricebook.Id, Product2Id=product1.Id);
        entry1.UnitPrice = 0;
        entry1.IsActive = true;
        entry1.UseStandardPrice = false;
        entry1.CurrencyIsoCode = 'CNY';
        
        PricebookEntry entry2 = new PricebookEntry( Pricebook2Id=pricebook.Id, Product2Id=product2.Id);
        entry2.UnitPrice = 0;
        entry2.IsActive = true;
        entry2.UseStandardPrice = false;
        entry2.CurrencyIsoCode = 'CNY';
        
        PricebookEntry entry3 = new PricebookEntry( Pricebook2Id=pricebook.Id, Product2Id=product3.Id);
        entry3.UnitPrice = 0;
        entry3.IsActive = true;
        entry3.UseStandardPrice = false;
        entry3.CurrencyIsoCode = 'CNY';

        
        insert new PricebookEntry[] {entry1,entry2,entry3};
        
        Account accIE = new Account(
            Name = '*',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c='Customer IE',
            FacilityName__c='abc',
            PostCode__c = '123456'
        );
        insert accIE; 
        
        Account specialDeliveryAddress1 = new Account(Name = '*',FacilityName__c='specialDeliveryAddress1',PostCode__c = '123456');
        Account specialDeliveryAddress2 = new Account(Name = '*',FacilityName__c='specialDeliveryAddress2',PostCode__c = '123456');
        Account specialDeliveryAddress3 = new Account(Name = '*',FacilityName__c='specialDeliveryAddress3',PostCode__c = '123456');
        Account foreignTradeCompany1 = new Account(Name = '*',FacilityName__c='foreignTradeCompany1',PostCode__c = '123456',RecordTypeId = rectForeignCompany[0].Id);
        Account foreignTradeCompany2 = new Account(Name = '*',FacilityName__c='foreignTradeCompany2',PostCode__c = '123456',RecordTypeId = rectForeignCompany[0].Id);
        Account foreignTradeCompany3 = new Account(Name = '*',FacilityName__c='foreignTradeCompany3',PostCode__c = '123456',RecordTypeId = rectForeignCompany[0].Id);
        insert new Account[] {specialDeliveryAddress1,specialDeliveryAddress2,specialDeliveryAddress3,foreignTradeCompany1,foreignTradeCompany2,foreignTradeCompany3};
        
        User user = new User();
        user.LastName = 'test';
        user.FirstName = 'test';
        user.Alias = 'test';
        user.Email = 'User20210519@test.com';
        user.Username = 'User20210519@test222.com';
        user.CommunityNickname = 'test01';
        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;
        
        Date day1 = Date.today();
        Date day2 = day1.addDays(-1);
        List<RecordType> rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity'];
        // 有预定下单日
        Opportunity opp = new Opportunity();
        opp.Name = 'test opp';
        opp.AccountId = accIE.Id;
        opp.RecordTypeId = rectOpp[0].Id;
        opp.OwnerId = UserInfo.getUserId();
        opp.StageName = 'Prospect Created';
        opp.CurrencyIsoCode = 'CNY';
        opp.ProductSegment__c = 'IE';
        opp.CloseDate = Date.today();
        opp.NewInquiryDate__c = Date.today().addDays(-2);
        opp.ExpectedOrderDate__c = Date.today().addDays(2);
        opp.ApprovalStatus_D__c = 'Pass';
        
        opp.LeadSource = 'LeadSource_D__c';
        opp.BudgetAmount__c = 111;
        opp.CancelReasonText__c = 'Cancel_Reason_Text_D__c';
        opp.CancelReason__c = 'Cancel_reason_D__c';
        opp.CompetitorCompany__c = 'Competitor_Company_D__c';
        opp.Competitor_Product__c = 'Competitor_Product_D__c';
        opp.Competitor_Product2__c = 'Competitor_Product2_D__c';
        opp.Competitor_Product3__c = 'Competitor_Product3_D__c';
        opp.Competitor_Product4__c = 'Competitor_Product4_D__c';
        opp.Competitor_Product5__c = 'Competitor_Product5_D__c';
        opp.Competitor_Product6__c = 'Competitor_Product6_D__c';
        opp.Competitor_Product7__c = 'Competitor_Product7_D__c';
        opp.Competitor_Product8__c = 'Competitor_Product8_D__c';
        opp.CompetitorProductCode__c = 'Competitor_Product_Code_D__c';
        opp.CompetitorProductCode2__c = 'Competitor_Product_Code2_D__c';
        opp.CompetitorProductCode3__c = 'Competitor_Product_Code3_D__c';
        opp.CompetitorProductCode4__c = 'Competitor_Product_Code4_D__c';
        opp.CompetitorProductCode5__c = 'Competitor_Product_Code5_D__c';
        opp.CompetitorProductCode6__c = 'Competitor_Product_Code6_D__c';
        opp.CompetitorProductCode7__c = 'Competitor_Product_Code7_D__c';
        opp.CompetitorProductCode8__c = 'Competitor_Product_Code8_D__c';
        opp.DealerSalesStaffName__c = 'Dealer_Sales_Staff_Name_D__c';
        opp.DealerService__c = 'Dealer_Service_D__c';
        opp.ExpectedDeliveryDate__c = day1;
        opp.InquiryResult__c = 'InquiryResultD__c';
        //opp.InquiryResultCancel__c = day1;
        //opp.InquiryResultLost__c = day1;
        opp.InquiryResultOrder__c = day1;
        opp.LostAmount__c = 111;
        opp.LostCompetitorProduct__c = 'Lost_competitor_product_D__c';
        opp.LostReasonText__c = 'LostReasonText_D__c';
        opp.LostReason__c = 'Lostreason_D__c';
        opp.Phase1Date__c = day1;
        opp.Phase2Date__c = day1;
        opp.Phase3Date__c = day1;
        opp.SalesChannel__c = 'direct';
        opp.SubDealer__c = 'Sub_Dealer_D__c';
        opp.TradeType__c = 'Taxation';
        opp.Machine_Parts__c = 'Machine';
        opp.SpecialDeliveryAddress__c = specialDeliveryAddress1.Id;
        opp.ForeignTradeCompany__c = foreignTradeCompany1.Id;
  
        insert opp;
        
        OpportunityLineItem oli = new OpportunityLineItem();
        oli.OpportunityId = opp.Id;
        oli.Quantity = 2;
        oli.UnitPrice = 111;
        oli.Description = 'test';
        oli.PricebookEntryId = entry1.Id;
        
        insert oli;
        
        opp.LeadSource = 'LeadSource';
        opp.BudgetAmount__c = 222;
        opp.CancelReasonText__c = 'CancelReasonText__c';
        opp.CancelReason__c = 'CancelReason__c';
        opp.CompetitorCompany__c = 'CompetitorCompany__c';
        opp.Competitor_Product__c = 'Competitor_Product__c';
        opp.Competitor_Product2__c = 'Competitor_Product2__c';
        opp.Competitor_Product3__c = 'Competitor_Product3__c';
        opp.Competitor_Product4__c = 'Competitor_Product4__c';
        opp.Competitor_Product5__c = 'Competitor_Product5__c';
        opp.Competitor_Product6__c = 'Competitor_Product6__c';
        opp.Competitor_Product7__c = 'Competitor_Product7__c';
        opp.Competitor_Product8__c = 'Competitor_Product8__c';
        opp.CompetitorProductCode__c = 'CompetitorProductCode__c';
        opp.CompetitorProductCode2__c = 'CompetitorProductCode2__c';
        opp.CompetitorProductCode3__c = 'CompetitorProductCode3__c';
        opp.CompetitorProductCode4__c = 'CompetitorProductCode4__c';
        opp.CompetitorProductCode5__c = 'CompetitorProductCode5__c';
        opp.CompetitorProductCode6__c = 'CompetitorProductCode6__c';
        opp.CompetitorProductCode7__c = 'CompetitorProductCode7__c';
        opp.CompetitorProductCode8__c = 'CompetitorProductCode8__c';
        opp.DealerSalesStaffName__c = 'DealerSalesStaffName__c';
        opp.DealerService__c = 'DealerService__c';
        opp.ExpectedDeliveryDate__c = day2;
        opp.ExpectedOrderDate__c = day2;
        opp.InquiryResult__c = 'InquiryResult__c';
        opp.InquiryResultCancel__c = null;
        opp.InquiryResultLost__c = null;
        opp.InquiryResultOrder__c = null;
        opp.LostAmount__c = 222;
        opp.LostCompetitorProduct__c = 'LostCompetitorProduct__c';
        opp.LostReasonText__c = 'LostReasonText__c';
        opp.LostReason__c = 'LostReason__c';
        opp.NewInquiryDate__c = day2;
        opp.Phase1Date__c = day2;
        opp.Phase2Date__c = day2;
        opp.Phase3Date__c = day2;
        opp.SalesChannel__c = 'direct';
        opp.SubDealer__c = 'SubDealer__c';
        opp.TradeType__c = 'Taxation';
        opp.Machine_Parts__c = 'Machine';
        opp.SpecialDeliveryAddress__c = specialDeliveryAddress2.Id;
        opp.ForeignTradeCompany__c = foreignTradeCompany2.Id;
        
        
        opp.OwnerId = user.Id;
        
        update opp;
        
        oli.Quantity = 3;
        oli.UnitPrice = 222;
        oli.Description = 'test1';
        
        update oli;
        
        opp.LeadSource_D__c = 'newOpp.LeadSource';
        opp.Budget_Amount_D__c = 111;
        opp.Cancel_Reason_Text_D__c = 'newOpp.CancelReasonText__c';
        opp.Cancel_Reason_D__c = 'newOpp.CancelReason__c';
        opp.Competitor_Company_D__c = 'newOpp.CompetitorCompany__c';
        opp.Competitor_Product_D__c = 'newOpp.Competitor_Product__c';
        opp.Competitor_Product2_D__c = 'newOpp.Competitor_Product2__c';
        opp.Competitor_Product3_D__c = 'newOpp.Competitor_Product3__c';
        opp.Competitor_Product4_D__c = 'newOpp.Competitor_Product4__c';
        opp.Competitor_Product5_D__c = 'newOpp.Competitor_Product5__c';
        opp.Competitor_Product6_D__c = 'newOpp.Competitor_Product6__c';
        opp.Competitor_Product7_D__c = 'newOpp.Competitor_Product7__c';
        opp.Competitor_Product8_D__c = 'newOpp.Competitor_Product8__c';
        opp.Competitor_Product_Code_D__c = 'newOpp.CompetitorProductCode__c';
        opp.Competitor_Product_Code2_D__c = 'newOpp.CompetitorProductCode2__c';
        opp.Competitor_Product_Code3_D__c = 'newOpp.CompetitorProductCode3__c';
        opp.Competitor_Product_Code4_D__c = 'newOpp.CompetitorProductCode4__c';
        opp.Competitor_Product_Code5_D__c = 'newOpp.CompetitorProductCode5__c';
        opp.Competitor_Product_Code6_D__c = 'newOpp.CompetitorProductCode6__c';
        opp.Competitor_Product_Code7_D__c = 'newOpp.CompetitorProductCode7__c';
        opp.Competitor_Product_Code8_D__c = 'newOpp.CompetitorProductCode8__c';
        opp.Dealer_Sales_Staff_Name_D__c = 'newOpp.DealerSalesStaffName__c';
        opp.Dealer_Service_D__c = 'newOpp.DealerService__c';
        opp.Expected_Delivery_Date_D__c = day1;
        opp.Expected_Order_Date_D__c = day1;
        opp.Inquiry_Result_D__c = 'newOpp.InquiryResult__c';
        opp.Inquiry_Result_Cancel_D__c = null;
        opp.Inquiry_Result_Lost_D__c = null;
        opp.Inquiry_Result_Order_D__c = null;
        opp.Lost_Amount_D__c = 111;
        opp.Lost_Competitor_Product_D__c = 'newOpp.LostCompetitorProduct__c';
        opp.LostReasonText_D__c = 'newOpp.LostReasonText__c';
        opp.LostReason_D__c = 'newOpp.Lostreason__c';
        opp.New_Inquiry_Date_D__c = day1;
        opp.Phase1Date_D__c = day1;
        opp.Phase2Date_D__c = day1;
        opp.Phase3Date_D__c = day1;
        opp.Sales_Channel_D__c = 'direct';
        opp.Sub_Dealer_D__c = 'newOpp.SubDealer__c';
        opp.Trade_Type_D__c = 'Taxation';
        opp.Machine_Parts_D__c = 'Machine';
        opp.SpecialDeliveryAddress_D__c= specialDeliveryAddress3.Id;
        opp.ForeignTradeCompany_D__c = foreignTradeCompany3.Id;
        
        opp.DealerSelectOwner__c = UserInfo.getUserId();
        
        update opp;
        
        oli.QuantityD__c = 2;
        oli.UnitPriceD__c = 111;
        oli.DescriptionD__c = 'test';
        oli.DealerSelectProduct__c = product1.Id;
        
        update oli;
        
        opp.ApprovalStatus_D__c = 'Completed';
        update opp;
        opp.ApprovalStatus_D__c = 'Submit';
        update opp;
        opp.ApprovalStatus_D__c = 'Reject';
        update opp;

        opp.ApprovalStatus_D__c = 'Completed';
        update opp;
        opp.ApprovalStatus_D__c = 'Submit';
        update opp;
        opp.ApprovalStatus_D__c = 'Pass';
        update opp;
    }

    static testMethod void myTest4() {

        StaticParameter.OpportunityTriggerIsUpdate = true;
        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        Account accIE = new Account(
            Name = 'test',
            RecordTypeId = rectIE[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c='Customer IE',
            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;
        PriceBook2 pricebook =new PriceBook2(
            Name = 'IE',
            ProductSegment__c = 'IE',
            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',
            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 = 'IE',
            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;
        
        opp.Phase1Date_D__c = Date.today();
        update opp;

        opp.Phase2Date_D__c = Date.today();
        update opp;

        opp.StageName = 'Phase3';
        update opp;

        OpportunityContactRole role = new OpportunityContactRole(
            Role  = 'End user',
            IsPrimary = true,
            OpportunityId = opp.Id,
            ContactId = con.Id
        );
        insert role;

        Order odr = new Order(
            Name = 'test',
            Status = 'Draft',
            Status__c = 'Active',
            AccountId = accIE.Id,
            ApproveStatus__c = 'Draft',
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            IE_SP5_D__c = true,
            IE_SP8__c = true,
            SpecialDeliveryAccount_D__c = accIE.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            EndUser__c = con.Id,
            EndUserD__c = con.Id
        );
        insert odr;
        opp.Name = 'test';
        update opp;
    }

    static testMethod void myTest5() {
        
        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 IE',
            FacilityName__c='abc',
            PostCode__c = '123456'
        );
        insert accIE; 
        
        PriceBook2 pricebook =new PriceBook2(
            Name = 'IE',
            ProductSegment__c = 'IE',
            TradeType__c = 'Taxation',
            SalesChannel__c = 'direct',
            MachineParts__c = 'Machine'
        );
        insert pricebook;

        User user = new User();
        user.LastName = 'test';
        user.FirstName = 'test';
        user.Alias = 'test';
        user.Email = 'User202105191300@test.com';
        user.Username = 'User202105191300@test222.com';
        user.CommunityNickname = 'test01';
        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;
           
        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 = 'IE',
            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;
        Opportunity oppNew = new Opportunity();
        oppNew.Name = 'test opp';
        oppNew.AccountId = accIE.Id;
        oppNew.RecordTypeId = rectOpp[0].Id;
        oppNew.OwnerId = user.Id;
        oppNew.StageName = 'Prospect Created';
        oppNew.CancelReason__c = 'Inquiry repeat';
        oppNew.Cancel_reason_D__c = 'Inquiry repeat';
        oppNew.CurrencyIsoCode = 'CNY';
        oppNew.ProductSegment__c = 'IE';
        oppNew.CloseDate = Date.today();
        oppNew.NewInquiryDate__c = Date.today().addDays(-2);
        oppNew.ExpectedOrderDate__c = Date.today().addDays(2);
        oppNew.TradeType__c = 'Taxation';
        oppNew.SalesChannel__c = 'direct';
        oppNew.Machine_Parts__c = 'Machine';
        insert oppNew;
        oppNew.Manager__c  = user.Id;
        oppNew.Have_Computer__c = true;
        oppNew.CanChangeOpp__c = true;
        oppNew.Association_Opportunity__c = opp.Id;
        update oppNew;
        /*oppNew.Association_Opportunity__c = null;
        update oppNew;*/
    }
}