/**
 * @description       : 
 * @author            : ChangeMeIn@UserSettingsUnder.SFDoc
 * @group             : 
 * @last modified on  : 04-21-2022
 * @last modified by  : ChangeMeIn@UserSettingsUnder.SFDoc
**/
@isTest
private class OrderTriggerTest {

    // 无特殊条件合同
    static testMethod void myTest1() {
        OrderTriggerHandler.asd();
        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='000000'
        );
        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'
        );
        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 = 'Phase3',
            CurrencyIsoCode = 'CNY',
            ProductSegment__c = 'IE',
            CloseDate = Date.today(),
            NewInquiryDate__c = Date.today().addDays(-2),
            ExpectedOrderDate__c = Date.today().addDays(2),
            SalesChannel__c = 'direct',
            TradeType__c = 'Taxation'
        );
        insert opp;
        
        User user = new User();
        user.LastName = 'test1';
        user.FirstName = 'test2';
        user.Alias = 'test3';
        user.Email = 'test4@test.com';
        user.Username = 'test11122@test222.com';
        user.CommunityNickname = 'test22';
        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;

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

        //王鹏伟 新加 客户名和最终用户与询价对比
        OpportunityContactRole role = new OpportunityContactRole(
            Role  = 'End user',
            IsPrimary = true,
            OpportunityId = opp.Id,
            ContactId = con.Id
            );
        insert role;

        OrderItemTriggerHandler.tess();

        List<RecordType> rectOdr = [select id from RecordType where IsActive = true and SobjectType = 'Order' and Name = 'IESalesContract'];
        Order odr = new Order(
            Name = '',
            Status = 'Draft',
            AccountId = accIE.Id,
            ApproveStatus__c = 'Draft',
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            RecordTypeId = rectOdr[0].Id,
            SpecialDeliveryAccount__c = accIE.Id,
            SpecialDeliveryAccount_D__c = accIE.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            EndUser__c = con.Id,
            EndUserD__c = con.Id
        );
        insert odr;

        // User user = new User();
        // user.LastName = 'test1';
        // user.FirstName = 'test2';
        // user.Alias = 'test3';
        // user.Email = 'test4@test.com';
        // user.Username = 'test11122@test222.com';
        // user.CommunityNickname = 'test22';
        // 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<User> userList = [select id,SalesAsistManager__c from user where id =:UserInfo.getUserId()];
        if (userList.size() > 0) {
            userList[0].SalesAsistManager__c = user.Id;
            update userList;
        }*/

        List<Order> orderList = [select id, ApproveStatus__c from Order where Id = :odr.Id];
        //System.assertEquals('OrderDraft', orderList[0].ApproveStatus__c);

        odr.ApproveStatus__c = 'Completed';
        update odr;
        odr.ApproveStatus__c = 'Submit';
        update odr;
        odr.ApproveStatus__c = 'OrderReject';
        update odr;

        List<Order> orderList2 = [select id, ApproveStatus__c from Order where Id = :odr.Id];
        System.assertEquals('OrderReject', orderList2[0].ApproveStatus__c);

        odr.ApproveStatus__c = 'Completed';
        update odr;
        odr.ApproveStatus__c = 'Submit';
        update odr;
        odr.ApproveStatus__c = 'OrderPass';
        update odr;

        List<Order> orderList3 = [select id, ApproveStatus__c from Order where Id = :odr.Id];
        System.assertEquals('OrderPass', orderList3[0].ApproveStatus__c);
    }

    // 有特殊条件合同
    static testMethod void myTest2() {
        OrderTriggerHandler.asd();
        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='000000'
        );
        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'
        );
        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 = 'Phase3',
            CurrencyIsoCode = 'CNY',
            ProductSegment__c = 'IE',
            CloseDate = Date.today(),
            NewInquiryDate__c = Date.today().addDays(-2),
            ExpectedOrderDate__c = Date.today().addDays(2),
            SalesChannel__c = 'direct',
            TradeType__c = 'Taxation'
        );
        insert opp;

        //王鹏伟 新加 客户名和最终用户与询价对比
        OpportunityContactRole role = new OpportunityContactRole(
            Role  = 'End user',
            IsPrimary = true,
            OpportunityId = opp.Id,
            ContactId = con.Id
            );
        insert role;

        Order odr = new Order(
            Name = '',
            Status = 'Draft',
            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;
        List<Order> orderList = [select id, ApproveStatus__c,ProductSegment__c,Status__c,IE_SP5_D__c,IE_SP6_D__c,IE_SP7_D__c,IE_SP8_D__c from Order where Id = :odr.Id];
        System.debug('orderList--->'+orderList);
        // System.assertEquals('Draft', orderList[0].ApproveStatus__c);

        odr.ApproveStatus__c = 'Completed';
        update odr;
        odr.ApproveStatus__c = 'Submit';
        update odr;
        odr.ApproveStatus__c = 'Pass';
        update odr;

        List<Order> orderList2 = [select id, ApproveStatus__c from Order where Id = :odr.Id];
        System.assertEquals('Pass', orderList2[0].ApproveStatus__c);

        odr.ApproveStatus__c = 'Completed2';
        update odr;
        odr.ApproveStatus__c = 'OrderSubmit';
        update odr;
        odr.ApproveStatus__c = 'OrderPass';
        update odr;

        List<Order> orderList3 = [select id, ApproveStatus__c from Order where Id = :odr.Id];
        System.assertEquals('OrderPass', orderList3[0].ApproveStatus__c);

        // odr.ApproveStatus__c = 'Completed2';
        // update odr;
        // odr.ApproveStatus__c = 'OrderSubmit';
        // update odr;
        // odr.ApproveStatus__c = 'OrderReject';
        // update odr;

        // List<Order> orderList4 = [select id, ApproveStatus__c from Order where Id = :odr.Id];
        // System.assertEquals('OrderReject', orderList4[0].ApproveStatus__c);
    }

    //
    static testMethod void myTest3() {
        OrderTriggerHandler.asd();
        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='000000'
        );
        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'
        );
        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 = 'Phase3',
            CurrencyIsoCode = 'CNY',
            ProductSegment__c = 'IE',
            CloseDate = Date.today(),
            NewInquiryDate__c = Date.today().addDays(-2),
            ExpectedOrderDate__c = Date.today().addDays(2),
            SalesChannel__c = 'direct',
            TradeType__c = 'Taxation',
            compo_opp__c = false
        );
        insert opp;
        //王鹏伟 新加 客户名和最终用户与询价对比
        OpportunityContactRole role = new OpportunityContactRole(
            Role  = 'End user',
            IsPrimary = true,
            OpportunityId = opp.Id,
            ContactId = con.Id
            );
        insert role;

        Order odr = new Order(
            Name = '',
            Status = 'Draft',
            AccountId = accIE.Id,
            ApproveStatus__c = 'Draft',
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            SpecialDeliveryAccount_D__c = accIE.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            EndUser__c = con.Id,
            EndUserD__c = con.Id
        );
        insert odr;

        odr.SP_11__c = true;
        odr.SP_102__c = true;
        odr.SP_103__c = true;
        odr.SP_106__c = true;
        odr.SP_111__c = true;
        odr.SP_121__c = true;
        odr.SP_131__c = true;
        odr.SP_141__c = true;
        odr.SP_151__c = true;
        odr.SP_161__c = true;
        odr.SP_171__c = true;
        odr.SP_181__c = true;
        odr.SP_191__c = true;
        odr.SP_202__c = true;
        odr.SP_21__c = true;
        odr.SP_22__c = false;
        odr.SP_31__c = true;
        odr.SP_32__c = false;
        odr.SP_33__c = false;
        odr.SP_41__c = true;
        odr.SP_51__c = true;
        odr.SP_52__c = true;
        odr.SP_62__c = true;
        odr.SP_71__c = true;
        odr.SP_81__c = true;
        odr.SP_91__c = true;

        odr.IE_SP1__c = true;
        odr.IE_SP2__c = true;
        odr.IE_SP3__c = true;
        odr.IE_SP4__c = false;
        odr.IE_SP5__c = true;
        odr.IE_SP6__c = true;
        odr.IE_SP7__c = true;

        odr.RVI_SP1__c = true;
        odr.RVI_SP2__c = false;
        odr.RVI_SP3__c = true;
        odr.RVI_SP4__c = true;
        odr.RVI_SP5__c = true;
        odr.RVI_SP6__c = true;

        odr.NDT_SP1__c = true;
        odr.NDT_SP2__c = true;
        odr.NDT_SP3__c = true;
        odr.NDT_SP4__c = true;
        odr.NDT_SP5__c = true;
        odr.NDT_SP6__c = true;
        odr.NDT_SP7__c = true;
        odr.NDT_SP8__c = true;
        odr.NDT_SP9__c = true;
        odr.NDT_SP10__c = true;
        odr.NDT_SP11__c = true;

        odr.ANI_SP1__c = true;
        odr.ANI_SP2__c = true;
        odr.ANI_SP3__c = true;
        odr.ANI_SP4__c = true;
        odr.ANI_SP5__c = true;
        odr.ANI_SP6__c = true;

        odr.NormalDis_SerContractCondition__c = true;
        odr.HighDis_SerContractCondition__c = false;
        odr.SpecialDis_SerContractCondition__c = false;
        odr.Warranty_SerContractCondition__c = true;

        odr.Discount__c = 0.5;
        odr.Olympus_Price_BeforeDiscount__c = 2000;
        odr.PaymentCondition__c = 'PaymentCondition__c';
        odr.SpecialDelivery__c = 'SpecialDelivery__c';
        odr.SpecialDeliveryContact__c = 'SpecialDeliveryContact__c';
        odr.SpecialDeliveryPhone__c = '11223344';
        odr.SpecialDeliveryAddress__c = 'SpecialDeliveryAddress__c';
        odr.SpecialParkage__c = 'SpecialParkage__c';
        odr.SpecialWarranty__c = 'SpecialWarranty__c';
        odr.DealerProfit__c = 0.4;
        odr.SpecialDelDate__c = Date.today().addDays(1);
        odr.OtherApply__c = 'OtherApply__c';

        odr.CustomerContractPrice__c = 2000;
        odr.OlympusContractPrices__c = 1000;
        odr.EndUser__c = con.Id;

        odr.Andor__c = true;
        odr.Bitplane__c = true;
        odr.CoolLED__c = true;
        odr.Lumen__c = true;
        odr.Lumenera__c = true;
        odr.Media_Cybernetics__c = true;
        odr.Narishige__c = true;
        odr.Newport__c = true;
        odr.OSIS_Germany__c = true;
        odr.OSIS_Singapore__c = true;
        odr.Photometrics__c = true;
        odr.Prior_Scientific__c = true;
        odr.Q_Imaging__c = true;
        odr.Tokai_Hit__c = true;
        odr.Other_Third__c = true;
        odr.ServiceFee__c = 500;
        odr.AlongProduct__c = '是';
        odr.Cost__c = 500;
        odr.Sub_DealerC__c = '9878971';
        odr.Shipment_Term__c = 'CIP';

        odr.Contract_Status__c = 'Active';

        update odr;
    }

    static testMethod void myTest4() {
        OrderTriggerHandler.asd();
        List<RecordType> rectBS = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer BS'];
        //List<RecordType> rectDealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer'];
        //List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];

        //Account buyer = new Account(
        //    Name = '*',
        //    FacilityName__c = 'buyer',
        //    PostCode__c = '123456',
        //    DivisionName__c='Dealer',
        //    OwnerId = UserInfo.getUserId(),
        //    RecordTypeId = rectDealer[0].Id,
        //    ProductSegment__c = 'BS',
        //    ManagementCode_Ext__c = 'P11111'
        //    //ProductSegment__c = 'BS'
        //);
        //Account delivery = new Account(
        //    Name = '*',
        //    FacilityName__c = 'delivery',
        //    PostCode__c = '123456'
        //);
        //Account user = new Account(
        //    Name = '*',
        //    FacilityName__c = 'user',
        //    PostCode__c = '123456',
        //    RecordTypeId = rectIE[0].Id
        //);
        //insert new List<Account> {buyer, delivery, user};
        List<RecordType> rectDealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer'];

        Account dealerParent = new Account();
        dealerParent.Name = 'test dealer';
        dealerParent.RecordTypeId = rectDealer[0].Id;
        dealerParent.ProductSegment__c = 'BS';
        dealerParent.PostCode__c='000000';
        dealerParent.ManagementCode_Ext__c = 'P11111';
        insert dealerParent;

        Account dealer1 = new Account();
        dealer1.Name = 'test dealer';
        dealer1.RecordTypeId = rectDealer[0].Id;
        dealer1.ProductSegment__c = 'BS';
        dealer1.PostCode__c='000000';
        dealer1.ManagementCode_Ext__c = 'C22222';
        dealer1.ParentId = dealerParent.Id;
        insert dealer1;

        Account dealer2 = new Account();
        dealer2.Name = 'test dealer';
        dealer2.RecordTypeId = rectDealer[0].Id;
        dealer2.ProductSegment__c = 'BS';
        dealer2.PostCode__c='000000';
        dealer2.ManagementCode_Ext__c = 'C33333';
        dealer2.ParentId = dealerParent.Id;
        insert dealer2;

        Account accBS = new Account(
            Name = '*',
            RecordTypeId = rectBS[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c='Customer BS',
            FacilityName__c='abc',
            PostCode__c='000000'
        );
        insert accBS;

        Contact con = new Contact();
        con.LastName = 'test';
        con.AccountId = accBS.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;

        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 = 'product1';
        product1.Product_ECCode__c = 'product1';
        product1.IsActive = true;
        insert product1;

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

        PricebookEntry entry1 = new PricebookEntry(Pricebook2Id = pricebook.Id, Product2Id = product1.Id);
        entry1.UnitPrice = 0;
        entry1.IsActive = true;
        entry1.UseStandardPrice = false;
        insert entry1;

        List<RecordType> rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity'];
        // 有预定下单日
        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = accBS.Id,
            useing__c = 'OEM',
            RecordTypeId = rectOpp[0].Id,
            OwnerId = UserInfo.getUserId(),
            StageName = 'Phase3',
            CurrencyIsoCode = 'CNY',
            ProductSegment__c = 'BS',
            CloseDate = Date.today(),
            NewInquiryDate__c = Date.today().addDays(-2),
            ExpectedOrderDate__c = Date.today().addDays(2),
            SalesChannel__c = 'direct',
            TradeType__c = 'Taxation',
            Dealer__c = dealer1.Id,
            Machine_Parts__c = 'Machine',
            InquiryResult__c = '112233'
        );
        insert opp;

        //王鹏伟 新加 客户名和最终用户与询价对比
        OpportunityContactRole role = new OpportunityContactRole(
            Role  = 'End user',
            IsPrimary = true,
            OpportunityId = opp.Id,
            ContactId = con.Id
            );
        insert role;

        Order odr = new Order(
            Name = '',
            Status = 'Draft',
            AccountId = accBS.Id,
            ApproveStatus__c = 'Draft',
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            IE_SP5_D__c = true,
            SP_61D__c = true,
            SpecialDeliveryAccount_D__c = accBS.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            EndUser__c = con.Id,
            EndUserD__c = con.Id
        );
        insert odr;

        OrderItem oi1 = new OrderItem(
            OrderId = odr.Id,
            PriceBookEntryId = entry1.Id,
            Quantity = 1,
            UnitPrice = 10
        );
        insert oi1;

        /*Opportunity opp1 = [select id,Order_Date__c,DeliveryDate__c from Opportunity where Id = :opp.Id];
        System.assertEquals(null, opp1.Order_Date__c);
        System.assertEquals(null, opp1.DeliveryDate__c);

        odr.OrderDate__c = Date.today();
        update odr;*/

        Delivery__c delivery1 = new Delivery__c(
            SSBD_Contract__c = odr.Id,
            Delivery_Date__c = Date.today().addDays(-1),
            Delivery_Status__c = '未发货'
        );
        insert delivery1;

        Opportunity opp2 = [select id,Order_Date__c,DeliveryDate__c from Opportunity where Id = :opp.Id];
        //System.assertEquals(Date.today(), opp2.Order_Date__c);
        System.assertEquals(null, opp2.DeliveryDate__c);

        Delivery__c delivery2 = new Delivery__c(
            SSBD_Contract__c = odr.Id,
            Delivery_Date__c = Date.today(),
            Delivery_Status__c = '部分发货'
        );
        insert delivery2;

        Opportunity opp3 = [select id,Order_Date__c,DeliveryDate__c from Opportunity where Id = :opp.Id];
        System.assertEquals(null, opp3.DeliveryDate__c);

        Delivery__c delivery3 = new Delivery__c(
            SSBD_Contract__c = odr.Id,
            Delivery_Date__c = Date.today().addDays(1),
            Delivery_Status__c = '全部发货'
        );
        insert delivery3;

        /*Opportunity opp4 = [select id,Order_Date__c,DeliveryDate__c from Opportunity where Id = :opp.Id];
        //System.assertEquals(Date.today().addDays(1), opp4.DeliveryDate__c);

        odr.RecordTypeId = System.Label.RT_Contract_BS_Tax_Approval;
        update odr;*/
    }
    static testMethod void myTest5(){
        OrderTriggerHandler.asd();
        Id pricebookId = Test.getStandardPricebookId();

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

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

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

        PricebookEntry entry1 = new PricebookEntry(Pricebook2Id = pricebook.Id, Product2Id = product1.Id);
        entry1.UnitPrice = 0;
        entry1.IsActive = true;
        entry1.UseStandardPrice = false;
        insert entry1;

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

        Contact contact = new Contact(
            LastName = 'contact',
            AccountId = user.Id
        );
        insert contact;

        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = user.Id,
            StageName = 'Phase3',
            CurrencyIsoCode = 'CNY',
            ProductSegment__c = 'ANI',
            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;

         //王鹏伟 新加 客户名和最终用户与询价对比
        OpportunityContactRole role = new OpportunityContactRole(
            Role  = 'End user',
            IsPrimary = true,
            OpportunityId = opp.Id,
            ContactId = contact.Id
            );
        insert role;

        OpportunityLineItem oli1 = new OpportunityLineItem(
            OpportunityId = opp.Id,
            PricebookEntryId = entry1.Id,
            Quantity = 10,
            UnitPrice = 10
        );
        OpportunityLineItem oli2 = new OpportunityLineItem(
            OpportunityId = opp.Id,
            PricebookEntryId = entry1.Id,
            Quantity = 20,
            UnitPrice = 20
        );
        insert new OpportunityLineItem[] {oli1, oli2};

        OpportunityContactRole ocr = new OpportunityContactRole(
            OpportunityId = opp.Id,
            ContactId = contact.Id,
            IsPrimary = true
        );
        insert ocr;

        Quote quo = new Quote(
            Name = 'quo',
            OpportunityId = opp.Id,
            Pricebook2Id = pricebook.Id,
            SetName1__c = 'setname01',
            SetQty1__c = 1
        );
        insert quo;

        QuoteLineItem qli1 = new QuoteLineItem(
            QuoteId = quo.Id,
            PricebookEntryId = entry1.Id,
            Quantity = 10,
            UnitPrice = 10,
            Custom_Price__c = 15,
            Set__c = 'set01'
        );
        QuoteLineItem qli2 = new QuoteLineItem(
            QuoteId = quo.Id,
            PricebookEntryId = entry1.Id,
            Quantity = 20,
            UnitPrice = 20,
            Custom_Price__c = 25,
            Set__c = 'set01'
        );
        insert new QuoteLineItem[] {qli1, qli2};

        opp.SyncedQuoteId = quo.Id;
        update opp;

        Order order = new Order(
            Name = 'order',
            CurrencyIsoCode = 'CNY',
            AccountId = user.Id,
            OpportunityId = opp.Id,
            QuoteId = quo.Id,
            EffectiveDate = Date.today(),
            Status = 'Draft',
            Pricebook2Id = pricebook.Id,
            EndUser__c = contact.Id,
            EndUserD__c = contact.Id
        );
        insert order;
    }
}