@isTest
private class OrderDivisionControllerTest {
    // Update PIPL 20220516 By Chen Yanan Start
    @TestSetup
    static void makeData(){
        TestDataUtility.CreatePIPolicyConfiguration();
    }
    // Update PIPL 20220516 By Chen Yanan End

    @isTest static void test_init() {
        Test.setMock(HttpCalloutMock.class, new TestDataUtility.CreateMetaDataUtilityHttpMock());   // Update PIPL 20220516 By Chen Yanan
        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,
            CurrencyIsoCode = 'CNY',
            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;
        entry1.CurrencyIsoCode = 'CNY';
        insert entry1;

        List<RecordType> rectBS = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer BS'];
        Account user = new Account(
            Name = '*',
            FacilityName__c = 'user',
            OwnerId = UserInfo.getUserId(),
            PostCode__c = '123456',
            RecordTypeId = rectBS[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',
            OwnerId = UserInfo.getUserId(),
            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 = contact.Id
            );
        insert role;

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

        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'
        );
        insert qli1;

        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;

        
        PageReference page = new PageReference('/apex/OrderDivision?odrId=' + order.Id + '&retURL=' + opp.Id);
        System.Test.setCurrentPage(page);
        OrderDivisionController controller = new OrderDivisionController();

        controller.init();

        //System.assertEquals(1, controller.lineinfo.size());
        //System.assertEquals(10, controller.lineinfo[0].oi.Quantity);
        //System.assertEquals(20, controller.lineinfo[1].oi.Quantity);

        controller.lineinfo[0].divideCnt = 10;
        //controller.lineinfo[1].divideCnt = 11;
        
        System.Test.StartTest();
        controller.saveBtn();
        //system.assertEquals(null,ApexPages.getMessages()[0].getDetail());

        List<OrderItem> oiList1 = [select id, Quantity from OrderItem where OrderId = :order.Id];
        //System.assertEquals(0, oiList1.size());
        //System.assertEquals(9, oiList1[0].Quantity);

        List<OrderItem> oiList2 = [select id, Quantity from OrderItem where OrderId <> :order.Id];
       // System.assertEquals(1, oiList2.size());
        //System.assertEquals(10, oiList2[0].Quantity);
        //System.assertEquals(11, oiList2[1].Quantity);

        controller.backBtn();
        System.Test.StopTest();
    }

/*    @isTest static void test_init_2() {
        Id pricebookId = Test.getStandardPricebookId();

        Pricebook2 pricebook = new Pricebook2(
            Name = 'IE',
            ProductSegment__c = 'IE',
            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> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        Account user = new Account(
            Name = '*',
            FacilityName__c = 'user',
            PostCode__c = '123456',
            RecordTypeId = rectIE[0].Id
        );
        insert user;

        Opportunity opp = new Opportunity(
            Name = 'test opp',
            AccountId = user.Id,
            StageName = 'Prospect Created',
            CurrencyIsoCode = 'CNY',
            ProductSegment__c = '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',
            Pricebook2Id = pricebook.Id
        );
        insert opp;

        OpportunityLineItem oli2 = new OpportunityLineItem(
            OpportunityId = opp.Id,
            PricebookEntryId = entry1.Id,
            Quantity = 20,
            UnitPrice = 20
        );
        insert oli2;

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

        QuoteLineItem qli2 = new QuoteLineItem(
            QuoteId = quo.Id,
            PricebookEntryId = entry1.Id,
            Quantity = 20,
            UnitPrice = 20,
            Custom_Price__c = 25,
            Set__c = 'set01'
        );
        insert 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
        );
        insert order;

        PageReference page = new PageReference('/apex/OrderDivision?odrId=' + order.Id + '&retURL=' + opp.Id);
        System.Test.setCurrentPage(page);
        OrderDivisionController controller = new OrderDivisionController();

        controller.init();

        System.assertEquals(1, controller.lineinfo.size());
        //System.assertEquals(10, controller.lineinfo[0].oi.Quantity);
        System.assertEquals(20, controller.lineinfo[0].oi.Quantity);

        //controller.lineinfo[0].divideCnt = 10;
        controller.lineinfo[0].divideCnt = 11;

        controller.saveBtn();

        //system.assertEquals(null,ApexPages.getMessages()[0].getDetail());

        List<OrderItem> oiList1 = [select id, Quantity from OrderItem where OrderId = :order.Id];
        System.assertEquals(1, oiList1.size());
        System.assertEquals(9, oiList1[0].Quantity);

        List<OrderItem> oiList2 = [select id, Quantity from OrderItem where OrderId <> :order.Id];
        System.assertEquals(1, oiList2.size());
        //System.assertEquals(10, oiList2[0].Quantity);
        System.assertEquals(11, oiList2[0].Quantity);
    }*/

    @isTest static void test_init2() {
        Test.setMock(HttpCalloutMock.class, new TestDataUtility.CreateMetaDataUtilityHttpMock());   // Update PIPL 20220516 By Chen Yanan
        Id pricebookId = Test.getStandardPricebookId();

        Pricebook2 pricebook = new Pricebook2(
            Name = 'IE',
            ProductSegment__c = 'IE',
            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;

        List<RecordType> rectIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
        Account user = new Account(
            Name = '*',
            FacilityName__c = 'user',
            PostCode__c = '123456',
            RecordTypeId = rectIE[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 = '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',
            Pricebook2Id = pricebook.Id,
            IE_Custom_Price__c = 2000,
            IE_local_cost__c = 200,
            IE_ShippingHandling__c = 100
        );
        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
        );
        OpportunityLineItem oli3 = new OpportunityLineItem(
            OpportunityId = opp.Id,
            PricebookEntryId = entry1.Id,
            Quantity = 30,
            UnitPrice = 30
        );
        insert new OpportunityLineItem[] {oli1, oli2, oli3};

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

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

        
        PageReference page = new PageReference('/apex/OrderDivision?odrId=' + order.Id + '&retURL=' + opp.Id);
        System.Test.setCurrentPage(page);
        OrderDivisionController controller = new OrderDivisionController();

        controller.init();

        System.assertEquals(3, controller.lineinfo.size());
        System.assertEquals(10, controller.lineinfo[0].oi.Quantity);
        System.assertEquals(20, controller.lineinfo[1].oi.Quantity);
        System.assertEquals(30, controller.lineinfo[2].oi.Quantity);

        controller.lineinfo[0].divideCnt = 10;
        controller.lineinfo[1].divideCnt = 11;
System.Test.StartTest();
        controller.saveBtn();
System.Test.StopTest();
        List<OrderItem> oiList1 = [select id, Quantity from OrderItem where OrderId = :order.Id];
        System.assertEquals(2, oiList1.size());
        System.assertEquals(9, oiList1[0].Quantity);
        System.assertEquals(30, oiList1[1].Quantity);

        List<OrderItem> oiList2 = [select id, Quantity from OrderItem where OrderId <> :order.Id];
        System.assertEquals(2, oiList2.size());
        System.assertEquals(10, oiList2[0].Quantity);
        System.assertEquals(11, oiList2[1].Quantity);
        
    }

}