@isTest
private class OrderWebServiceTest {

	@isTest static void test_updItems1() {
        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,
            CurrencyIsoCode = 'CNY',
            IsActive = true
        );
        insert standardPrice1;

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

        Account user = new Account(
            Name = '*',
            FacilityName__c = 'user',
            PostCode__c = '123456'
        );
        insert user;

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

        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = user.Id,
            StageName = 'Phase3',
            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;
        OpportunityContactRole role = new OpportunityContactRole(
            Role  = 'End user',
            IsPrimary = true,
            OpportunityId = opp.Id,
            ContactId = con.Id
            );
        insert role;

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

        QuoteLineItem qli = new QuoteLineItem(
            QuoteId = quo.Id,
            PricebookEntryId = entry1.Id,
            Quantity = 1,
            UnitPrice = 10,
            Set__c = 'set01'
        );
        insert qli;

        opp.SyncedQuoteId = quo.Id;
        update opp;

        Order order = new Order(
            Name = 'order',
            CurrencyIsoCode = 'CNY',
            AccountId = user.Id,
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            Status = 'Draft',
            Pricebook2Id = pricebook.Id,
            SpecialDeliveryAccount__c = user.Id,
            SpecialDeliveryAccount_D__c = user.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            EndUser__c = con.Id
        );
        insert order;

        List<OrderItem> oiList = new List<OrderItem>();
        for (Integer i = 0; i < 1; i++) {
            OrderItem oi = new OrderItem(
                OrderId = order.Id,
                PriceBookEntryId = entry1.Id,
                Quantity = 1,
                UnitPrice = 10
            );
            oiList.add(oi);
        }

        insert oiList;
        Test.startTest();
        OrderWebService.updItems(opp.Id, order.Id);
         Test.stopTest();
	}

    @isTest static void test_updItems2() {
        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,
            CurrencyIsoCode = 'CNY',
            IsActive = true
        );
        insert standardPrice1;

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

        Account user = new Account(
            Name = '*',
            FacilityName__c = 'user',
            PostCode__c = '123456'
        );
        insert user;

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

        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = user.Id,
            StageName = 'Phase3',
            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;
         OpportunityContactRole role = new OpportunityContactRole(
            Role  = 'End user',
            IsPrimary = true,
            OpportunityId = opp.Id,
            ContactId = con.Id
            );
        insert role;

        OpportunityLineItem oli = new OpportunityLineItem(
            OpportunityId = opp.Id,
            PricebookEntryId = entry1.Id,
            Quantity = 1,
            UnitPrice = 10
        );
        insert oli;

        Order order = new Order(
            Name = 'order',
            CurrencyIsoCode = 'CNY',
            AccountId = user.Id,
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            Status = 'Draft',
            Pricebook2Id = pricebook.Id,
            SpecialDeliveryAccount__c = user.Id,
            SpecialDeliveryAccount_D__c = user.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            EndUser__c = con.Id
        );
        insert order;

        List<OrderItem> oiList = new List<OrderItem>();
        for (Integer i = 0; i < 1; i++) {
            OrderItem oi = new OrderItem(
                OrderId = order.Id,
                PriceBookEntryId = entry1.Id,
                Quantity = 1,
                UnitPrice = 10
            );
            oiList.add(oi);
        }

        insert oiList;
        test.startTest();
        OrderWebService.updItems(opp.Id, order.Id);
        test.stopTest();
    }

    @isTest static void test_copyOrder() {
        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,
            CurrencyIsoCode = 'CNY',
            IsActive = true
        );
        insert standardPrice1;

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

        Account user = new Account(
            Name = '*',
            FacilityName__c = 'user',
            PostCode__c = '123456'
        );
        insert user;

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

        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 = '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;

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

        // OpportunityContactRole ocr = new OpportunityContactRole(
        //     OpportunityId = opp.Id,
        //     ContactId = contact.Id,
        //     IsPrimary = true
        // );
        // insert ocr;
       
        OpportunityLineItem oli = new OpportunityLineItem(
            OpportunityId = opp.Id,
            PricebookEntryId = entry1.Id,
            Quantity = 1,
            UnitPrice = 10
        );
        insert oli;

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

        QuoteLineItem qli = new QuoteLineItem(
            QuoteId = quo.Id,
            PricebookEntryId = entry1.Id,
            Quantity = 1,
            UnitPrice = 10,
            Set__c = 'set01'
        );
        insert qli;

        opp.SyncedQuoteId = quo.Id;
        update opp;

        Order order = new Order(
            Name = 'order',
            CurrencyIsoCode = 'CNY',
            AccountId = user.Id,
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            Status = 'Draft',
            Pricebook2Id = pricebook.Id,
            SpecialDeliveryAccount__c = user.Id,
            SpecialDeliveryAccount_D__c = user.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            EndUser__c = con.Id
        );
        insert order;

        List<OrderItem> oiList = new List<OrderItem>();
        for (Integer i = 0; i < 1; i++) {
            OrderItem oi = new OrderItem(
                OrderId = order.Id,
                PriceBookEntryId = entry1.Id,
                Quantity = 1,
                UnitPrice = 10
            );
            oiList.add(oi);
        }

        insert oiList;
        test.startTest();
        OrderWebService.copyOrder(order.Id);
        test.stopTest();
    }

    @isTest static void test_selectOrder1() {
        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,
            CurrencyIsoCode = 'CNY',
            IsActive = true
        );
        insert standardPrice1;

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

        Account user = new Account(
            Name = '*',
            FacilityName__c = 'user',
            PostCode__c = '123456'
        );
        insert user;

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

        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = user.Id,
            StageName = 'Phase3',
            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
        );
        test.startTest();
        insert opp;

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

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

        QuoteLineItem qli = new QuoteLineItem(
            QuoteId = quo.Id,
            PricebookEntryId = entry1.Id,
            Quantity = 1,
            UnitPrice = 10,
            Set__c = 'set01'
        );
        insert qli;

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

        List<OrderItem> oiList = new List<OrderItem>();
        for (Integer i = 0; i < 1; i++) {
            OrderItem oi = new OrderItem(
                OrderId = order.Id,
                PriceBookEntryId = entry1.Id,
                Quantity = 1,
                UnitPrice = 10
            );
            oiList.add(oi);
        }

        insert oiList;

        Order order2 = new Order(
            Name = 'order2',
            CurrencyIsoCode = 'CNY',
            AccountId = user.Id,
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            Status = 'Draft',
            Pricebook2Id = pricebook.Id,
            SpecialDeliveryAccount__c = user.Id,
            SpecialDeliveryAccount_D__c = user.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            EndUser__c = con.Id
        );
        insert order2;

        OrderWebService.selectOrder(order.Id);
        test.stopTest();
    }

    @isTest static void test_selectOrder2() {
        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,
            CurrencyIsoCode = 'CNY',
            IsActive = true
        );
        insert standardPrice1;

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

        Account user = new Account(
            Name = '*',
            FacilityName__c = 'user',
            PostCode__c = '123456'
        );
        insert user;

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


        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = user.Id,
            StageName = 'Phase3',
            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
        );
        test.startTest();
        insert opp;

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

        Order order = new Order(
            Name = 'order',
            CurrencyIsoCode = 'CNY',
            AccountId = user.Id,
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            Status = 'Draft',
            Pricebook2Id = pricebook.Id,
            SpecialDeliveryAccount__c = user.Id,
            SpecialDeliveryAccount_D__c = user.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            EndUser__c = con.Id
        );
        insert order;

        List<OrderItem> oiList = new List<OrderItem>();
        for (Integer i = 0; i < 1; i++) {
            OrderItem oi = new OrderItem(
                OrderId = order.Id,
                PriceBookEntryId = entry1.Id,
                Quantity = 1,
                UnitPrice = 10
            );
            oiList.add(oi);
        }

        insert oiList;

        Order order2 = new Order(
            Name = 'order2',
            CurrencyIsoCode = 'CNY',
            AccountId = user.Id,
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            Status = 'Draft',
            Pricebook2Id = pricebook.Id,
            SpecialDeliveryAccount__c = user.Id,
            SpecialDeliveryAccount_D__c = user.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            EndUser__c = con.Id
        );
        insert order2;

        OrderWebService.selectOrder(order.Id);
        test.stopTest();
    }

     @isTest static void test_order1() {
        Id pricebookId = Test.getStandardPricebookId();
        ////记录类型-奥林巴斯分公司
        //List<RecordType> olympusRectdealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'OlympusCompany'];
        ////记录类型-代理商
        //List<RecordType> rectdealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer'];
       
         List<RecordType> rectDealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer'];
        if (rectDealer.size() == 0) {
            return;
        }
        List<RecordType> olyCompany = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'OlympusCompany'];
        if (olyCompany.size() == 0) {
            return;
        }

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

       //Account user = new Account(
       //     Name = '*',
       //     FacilityName__c = 'user',
       //     PostCode__c = '123456',
       //     RecordTypeId = rectDealer[0].Id
       // );
       // insert user;
        //1.创建两个客户①.ANI代理商②奥林巴斯代理商
        //
        test.startTest();
        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;
        product1.NMPAStatus_one__c = 'Z2';
        insert product1;

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

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

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

        //Account user = new Account(
        //    Name = '*',
        //    FacilityName__c = 'user',
        //    PostCode__c = '123456'
        //);
        //insert user;

        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = dealer1.Id,
            StageName = 'Phase3',
            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;

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

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

        Order order = new Order(
            Name = 'order',
            CurrencyIsoCode = 'CNY',
            AccountId = dealer1.Id,
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            Status = 'Draft',
            Pricebook2Id = pricebook.Id,
            SpecialDeliveryAccount__c = dealer1.Id,
            SpecialDeliveryAccount_D__c = dealer1.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            EndUser__c = con.Id
        );
        insert order;

        //List<OrderItem> oiList = new List<OrderItem>();
        //for (Integer i = 0; i < 1; i++) {
        //    OrderItem oi = new OrderItem(
        //        OrderId = order.Id,
        //        PriceBookEntryId = entry1.Id,
        //        Quantity = 1,
        //        UnitPrice = 10
        //    );
        //    oiList.add(oi);
        //}

        //insert oiList;

        //Order order2 = new Order(
        //    Name = 'order2',
        //    CurrencyIsoCode = 'CNY',
        //    AccountId = user.Id,
        //    OpportunityId = opp.Id,
        //    EffectiveDate = Date.today(),
        //    Status = 'Draft',
        //    Pricebook2Id = pricebook.Id
        //);
        //insert order2;

        OrderWebService.checkUpperLimitOfSales(order.Id,dealer1.Id);
        OrderWebService.checkUpperLimitOfSalesOly(order.Id,dealer1.Id);
        OrderWebService.checkLicense1(opp.Id);
        OrderWebService.checkLicense(opp.Id,order.Id);
        OrderWebService.checkproduct(opp.Id);
        OrderWebService.checkaddress(opp.Id,'Test0522',order.Id);
        test.stopTest();
    }

    @isTest static void test_checkAccountContact() {

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

        Account acc = new Account(
            Name = '*',
            FacilityName__c = 'user',
            PostCode__c = '123456',
            stautesD__c = 'Submit'
        );
        insert acc;

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

        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = acc.Id,
            StageName = 'Phase3',
            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;

        OpportunityContactRole role = new OpportunityContactRole(
            Role  = 'End user',
            IsPrimary = true,
            OpportunityId = opp.Id,
            ContactId = con.Id
            );
        insert role;
        
        Order order = new Order(
            Name = 'order',
            CurrencyIsoCode = 'CNY',
            AccountId = acc.Id,
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            Status = 'Draft',
            Pricebook2Id = pricebook.Id,
            SpecialDeliveryAccount__c = acc.Id,
            SpecialDeliveryAccount_D__c = acc.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            EndUser__c = con.Id
        );
        insert order;
        test.startTest();
        OrderWebService.checkAccountContact(order.Id);
        test.stopTest();
    }

    @isTest static void test_Inventory() {

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

        Account acc = new Account(
            Name = '*',
            FacilityName__c = 'user',
            PostCode__c = '123456',
            stautesD__c = 'Submit'
        );
        insert acc;

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

        Id pricebookId = Test.getStandardPricebookId();

        // Pricebook2 pricebook = new Pricebook2(
        //     Name = 'IE',
        //     ProductSegment__c = 'IE',
        //     TradeType__c = 'Tax Exemption',
        //     SalesChannel__c = 'direct',
        //     MachineParts__c = 'Machine',
        //     isActive = true,
        //     CurrencyIsoCode = 'USD'
        // );
        // insert pricebook;

        Product2 product1 = new Product2();
        product1.Name = 'product1';
        product1.ProductCode = 'product1';
        product1.Product_ECCode__c = 'product1';
        product1.NMPAStatus_one__c = 'Z5';
        product1.MaterialStatus_one_Start__c = Date.today().addDays(-22);
        product1.MaterialStatus_one_End__c = Date.today().addDays(22);
        product1.IsActive = true;
        insert product1;

        Product2 product2 = new Product2();
        product2.Name = 'product2';
        product2.ProductCode = 'product2';
        product2.Product_ECCode__c = 'product2';
        product2.NMPAStatus_one__c = 'Z5';
        product2.MaterialStatus_one_Start__c = Date.today().addDays(-22);
        product2.MaterialStatus_one_End__c = Date.today().addDays(22);
        product2.IsActive = true;
        insert product2;

        Product2 product3 = new Product2();
        product3.Name = 'product3';
        product3.ProductCode = 'product3';
        product3.Product_ECCode__c = 'product3';
        product3.NMPAStatus_one__c = 'Z5';
        product3.MaterialStatus_one_Start__c = Date.today().addDays(-22);
        product3.MaterialStatus_one_End__c = Date.today().addDays(22);
        product3.IsActive = true;
        insert product3;

        ProductInventory__c productInventory = new ProductInventory__c();
        productInventory.TransforDate__c = Date.today() ;
        productInventory.ProductCode__c = product1.Id;
        productInventory.Plant__c = 'CT22';
        productInventory.ProductSegment__c = 'LS' ;
        productInventory.ProductType__c = 'FG' ;
        productInventory.InventoryQuantity__c = 2;
        productInventory.ManagementCode__c = product1.ProductCode +'_LS'+'_FG';
        productInventory.TradeType__c = 'Taxation';
        insert productInventory;

        ProductInventory__c productInventory2 = new ProductInventory__c();
        productInventory2.TransforDate__c = Date.today() ;
        productInventory2.ProductCode__c = product3.Id;
        productInventory2.Plant__c = 'CT22';
        productInventory2.ProductSegment__c = 'LS' ;
        productInventory2.ProductType__c = 'FG' ;
        productInventory2.InventoryQuantity__c = 2;
        productInventory2.ManagementCode__c = product1.ProductCode +'_LS'+'_FG';
        productInventory2.TradeType__c = 'Taxation';
        insert productInventory2;

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

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

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

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

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

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

        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = acc.Id,
            StageName = 'Phase3',
            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 oli = new OpportunityLineItem(
            OpportunityId = opp.Id,
            PricebookEntryId = entry1.Id,
            Quantity = 1,
            UnitPrice = 10
        );
        insert oli;

        OpportunityLineItem oli2 = new OpportunityLineItem(
            OpportunityId = opp.Id,
            PricebookEntryId = entry2.Id,
            Quantity = 1,
            UnitPrice = 10
        );
        insert oli2;

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

        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 = 1,
            UnitPrice = 10,
            Custom_Price__c = 15,
            Set__c = 'set01'
        );
        insert qli1;
        OpportunityContactRole role = new OpportunityContactRole(
            Role  = 'End user',
            IsPrimary = true,
            OpportunityId = opp.Id,
            ContactId = con.Id
            );
        insert role;
        
        Order order = new Order(
            Name = 'order',
            CurrencyIsoCode = 'CNY',
            AccountId = acc.Id,
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            Status = 'Draft',
            Pricebook2Id = pricebook.Id,
            SpecialDeliveryAccount__c = acc.Id,
            SpecialDeliveryAccount_D__c = acc.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            EndUser__c = con.Id
        );
        insert order;
        test.startTest();
        OrderWebService.testI();
        OrderWebService.OrderInventoryCheck(order.Id);
        OrderWebService.OpportunityInventoryCheck(opp.Id);
        OrderWebService.OrderUpdateInventory(order.Id);
        OrderWebService.QuoteInventoryCheck(quo.Id);
        test.stopTest();
    }
}