@isTest
private class RadiationUtilTest {
    
    static testMethod void myTest0() {
        List<RecordType> rectBS = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer ANI'];
        List<RecordType> dealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer'];
        Account accBS = new Account(
            Name = '*',
            RecordTypeId = rectBS[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c='Customer ANI',
            FacilityName__c='abc',
            PostCode__c='00000'
        );
        insert accBS;

        Account dealerAccount = new Account(
            Name = '*',
            RecordTypeId = dealer[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c='Dealer',
            FacilityName__c='abc',
            PostCode__c='000001'
        );
        insert dealerAccount;

        PurchaseDetails__c details = new PurchaseDetails__c();
        details.PurchaseAccount__c = dealerAccount.Id;
        details.TakeEffectInYear__c = '2020';
        details.SoldNumber__c = 1;
        details.PurProductModel__c = 'Delta';
        insert details;

        License_Information__c information = new License_Information__c();
        information.ValidFrom__c = Date.today().addDays(-22);
        information.ValidTo__c = Date.today().addDays(22);
        information.LicenseAndAccount__c = dealerAccount.Id;
        information.StorageAddress__c = '北京朝阳亮马桥';
        information.BusinessLicense__c = 'test01';
        information.LicenseType__c = '辐射安全许可证';
        insert information;

        CertificationDetails__c  certification = new CertificationDetails__c(); 
        certification.ActivitieTypes__c = '销售';
        certification.ProdustionType__c = 'Delta和Vanta';
        certification.ProductModelNumber__c = 10;
        certification.LicenseInformation__c = information.Id ;

        insert certification;
        String olympusAccountId = System.label.Olympus_Id;
        PurchaseDetails__c details1 = new PurchaseDetails__c();
        details1.PurchaseAccount__c = olympusAccountId;
        details1.TakeEffectInYear__c = '2020';
        details1.SoldNumber__c = 1;
        details1.PurProductModel__c = 'Delta';
        insert details1;

        License_Information__c olympusinformation = new License_Information__c();

        olympusinformation.ValidFrom__c = Date.today().addDays(-22);
        olympusinformation.ValidTo__c = Date.today().addDays(22);
        olympusinformation.LicenseAndAccount__c = olympusAccountId;
        olympusinformation.StorageAddress__c = '北京朝阳亮马桥';
        olympusinformation.BusinessLicense__c = 'test01';
        olympusinformation.LicenseType__c = '辐射安全许可证';
        insert olympusinformation;

        CertificationDetails__c  olympuscertification = new CertificationDetails__c(); 
        olympuscertification.ActivitieTypes__c = '销售';
        olympuscertification.ProdustionType__c = 'Delta和Vanta';
        olympuscertification.ProductModelNumber__c = 10;
        olympuscertification.LicenseInformation__c = olympusinformation.Id ;

        insert olympuscertification;

        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 = '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;
        product1.If_Radiation_Product__c = true;
        product1.RadiationType__c = 'Vanta';
        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,
            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',
            Machine_Parts__c = 'Machine',
            InquiryResult__c = '112233',
            Dealer__c = dealerAccount.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 odr = new Order(
            Name = '',
            Status = 'Draft',
            AccountId = accBS.Id,
            ApproveStatus__c = 'Draft',
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            IE_SP5_D__c = true,
            SpecialDeliveryAccount_D__c = accBS.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            BusMagDepRev__c = Date.today(),
            EndUser__c = con.Id
        );
        insert odr;

        OrderItem oi1 = new OrderItem(
            OrderId = odr.Id,
            PriceBookEntryId = entry1.Id,
            Quantity = 8,
            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;

        odr.ApproveStatus__c = 'OrderPass';
        update odr;
        odr.ApproveStatus__c = 'Draft' ;
        update odr;
        odr.Status__c = 'Inactive';
        update odr;

    }

    static testMethod void myTest1() {
        List<RecordType> rectBS = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer ANI'];
        List<RecordType> dealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer'];
        Account accBS = new Account(
            Name = '*',
            RecordTypeId = rectBS[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c='Customer ANI',
            FacilityName__c='abc',
            PostCode__c='0000003'
        );
        insert accBS;

        Account dealerAccount = new Account(
            Name = '*',
            RecordTypeId = dealer[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c='Dealer',
            FacilityName__c='abc',
            PostCode__c='000004'
        );
        insert dealerAccount;

        PurchaseDetails__c details = new PurchaseDetails__c();
        details.PurchaseAccount__c = dealerAccount.Id;
        details.TakeEffectInYear__c = String.valueOf(Date.today().year());
        details.SoldNumber__c = 1;
        details.PurProductModel__c = 'Delta';
        insert details;

        License_Information__c information = new License_Information__c();
        information.ValidFrom__c = Date.today().addDays(-22);
        information.ValidTo__c = Date.today().addDays(22);
        information.LicenseAndAccount__c = dealerAccount.Id;
        information.StorageAddress__c = '北京朝阳亮马桥';
        information.BusinessLicense__c = 'test01';
        information.LicenseType__c = '辐射安全许可证';
        insert information;

        CertificationDetails__c  certification = new CertificationDetails__c(); 
        certification.ActivitieTypes__c = '销售';
        certification.ProdustionType__c = 'Delta';
        certification.ProductModelNumber__c = 10;
        certification.LicenseInformation__c = information.Id ;

        insert certification;
        String olympusAccountId = System.label.Olympus_Id;
        PurchaseDetails__c details1 = new PurchaseDetails__c();
        details1.PurchaseAccount__c = olympusAccountId;
        details1.TakeEffectInYear__c = String.valueOf(Date.today().year());
        details1.SoldNumber__c = 1;
        details1.PurProductModel__c = 'Delta';
        insert details1;

        License_Information__c olympusinformation = new License_Information__c();

        olympusinformation.ValidFrom__c = Date.today().addDays(-22);
        olympusinformation.ValidTo__c = Date.today().addDays(22);
        olympusinformation.LicenseAndAccount__c = olympusAccountId;
        olympusinformation.StorageAddress__c = '北京朝阳亮马桥';
        olympusinformation.BusinessLicense__c = 'test01';
        olympusinformation.LicenseType__c = '辐射安全许可证';
        insert olympusinformation;

        CertificationDetails__c  olympuscertification = new CertificationDetails__c(); 
        olympuscertification.ActivitieTypes__c = '销售';
        olympuscertification.ProdustionType__c = 'Delta';
        olympuscertification.ProductModelNumber__c = 10;
        olympuscertification.LicenseInformation__c = olympusinformation.Id ;

        insert olympuscertification;

        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 = '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;
        product1.If_Radiation_Product__c = true;
        product1.RadiationType__c = 'Delta';
        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,
            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',
            Machine_Parts__c = 'Machine',
            InquiryResult__c = '112233',
            Dealer__c = dealerAccount.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 odr = new Order(
            Name = '',
            Status = 'Draft',
            AccountId = accBS.Id,
            ApproveStatus__c = 'Draft',
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            IE_SP5_D__c = true,
            SpecialDeliveryAccount_D__c = accBS.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            BusMagDepRev__c = Date.today(),
            EndUser__c = con.Id
        );
        insert odr;

        OrderItem oi1 = new OrderItem(
            OrderId = odr.Id,
            PriceBookEntryId = entry1.Id,
            Quantity = 7,
            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;

        odr.ApproveStatus__c = 'OrderSubmit';
        update odr;
        
    }

    static testMethod void myTest2() {
        List<RecordType> rectBS = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer ANI'];
        List<RecordType> dealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer'];
        Account accBS = new Account(
            Name = '*',
            RecordTypeId = rectBS[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c='Customer ANI',
            FacilityName__c='abc',
            PostCode__c='0000005'
        );
        insert accBS;

        Account dealerAccount = new Account(
            Name = '*',
            RecordTypeId = dealer[0].Id,
            OwnerId = UserInfo.getUserId(),
            DivisionName__c='Dealer',
            FacilityName__c='abc',
            PostCode__c='000006'
        );
        insert dealerAccount;

        PurchaseDetails__c details = new PurchaseDetails__c();
        details.PurchaseAccount__c = dealerAccount.Id;
        details.TakeEffectInYear__c = String.valueOf(Date.today().year());
        details.SoldNumber__c = 1;
        details.PurProductModel__c = 'Delta';
        insert details;

        License_Information__c information = new License_Information__c();
        information.ValidFrom__c = Date.today().addDays(-22);
        information.ValidTo__c = Date.today().addDays(22);
        information.LicenseAndAccount__c = dealerAccount.Id;
        information.StorageAddress__c = '北京朝阳亮马桥';
        information.BusinessLicense__c = 'test01';
        information.LicenseType__c = '辐射安全许可证';
        insert information;

        CertificationDetails__c  certification = new CertificationDetails__c(); 
        certification.ActivitieTypes__c = '销售';
        certification.ProdustionType__c = 'Delta';
        certification.ProductModelNumber__c = 10;
        certification.LicenseInformation__c = information.Id ;

        insert certification;
        String olympusAccountId = System.label.Olympus_Id;
        PurchaseDetails__c details1 = new PurchaseDetails__c();
        details1.PurchaseAccount__c = olympusAccountId;
        details1.TakeEffectInYear__c = '2020';
        details1.SoldNumber__c = 1;
        details1.PurProductModel__c = 'Delta';
        insert details1;

        License_Information__c olympusinformation = new License_Information__c();

        olympusinformation.ValidFrom__c = Date.today().addDays(-22);
        olympusinformation.ValidTo__c = Date.today().addDays(22);
        olympusinformation.LicenseAndAccount__c = olympusAccountId;
        olympusinformation.StorageAddress__c = '北京朝阳亮马桥';
        olympusinformation.BusinessLicense__c = 'test01';
        olympusinformation.LicenseType__c = '辐射安全许可证';
        insert olympusinformation;

        CertificationDetails__c  olympuscertification = new CertificationDetails__c(); 
        olympuscertification.ActivitieTypes__c = '销售';
        olympuscertification.ProdustionType__c = 'Delta';
        olympuscertification.ProductModelNumber__c = 10;
        olympuscertification.LicenseInformation__c = olympusinformation.Id ;

        insert olympuscertification;

        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 = '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;
        product1.If_Radiation_Product__c = true;
        product1.RadiationType__c = 'Delta';
        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,
            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',
            Machine_Parts__c = 'Machine',
            InquiryResult__c = '112233',
            Dealer__c = dealerAccount.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 odr = new Order(
            Name = '',
            Status = 'Draft',
            AccountId = accBS.Id,
            ApproveStatus__c = 'Draft',
            OpportunityId = opp.Id,
            EffectiveDate = Date.today(),
            IE_SP5_D__c = true,
            SpecialDeliveryAccount_D__c = accBS.Id,
            SpecialDeliveryContact2_D__c = con.Id,
            BusMagDepRev__c = Date.today(),
            EndUser__c = con.Id
        );
        insert odr;

        OrderItem oi1 = new OrderItem(
            OrderId = odr.Id,
            PriceBookEntryId = entry1.Id,
            Quantity = 11,
            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;

        odr.ApproveStatus__c = 'OrderSubmit';
        update odr;

        odr.ApproveStatus__c = 'OrderDraft';
        update odr;
        
    }
    
}