@isTest
private class OpportunityLineItemTriggerTest {

	private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
	
	static testMethod void testAfterChange() {
		List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }

        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM007TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM007TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;
        List<Opportunity> oppList = new List<Opportunity>();
        Opportunity opp = new Opportunity();
        opp.AccountId           = depart.Id;
        opp.Department_Class__c = section.Id;
        opp.Hospital__c         = company.Id;
        opp.SAP_Send_OK__c      = false;
        opp.Name                = 'GZ-SP-NFM007_1';
        opp.Trade__c            = '内貿';
        opp.StageName           = '引合';
        opp.CloseDate           = date.newinstance(2022, 11, 30);
        opp.STMS_Package_Sale__c = true;
        opp.EnergyHost__c = true;
        //insert opp;
        oppList.add(opp);

        Opportunity opp2 = new Opportunity();
        opp2.AccountId           = depart.Id;
        opp2.Department_Class__c = section.Id;
        opp2.Hospital__c         = company.Id;
        opp2.SAP_Send_OK__c      = false;
        opp2.Name                = 'GZ-SP-NFM007_2';
        opp2.Trade__c            = '内貿';
        opp2.StageName           = '引合';
        opp2.STMS_Package_Sale__c = true;
        opp2.EnergyHost__c = true;
        opp2.CloseDate           = date.newinstance(2022, 11, 30);
        //insert opp2;
        oppList.add(opp2);
        insert oppList;
        List<Product2> proList = new List<Product2>();
        Product2 product = new Product2( Name='テスト商品');
        product.SFDA_Status__c = '有効';
        product.Intra_Trade_List_RMB_1__c = 100;
        product.Intra_Trade_List_RMB_Date1__c = date.today();
        product.Intra_Trade_Cost_RMB_1__c = 200;
        product.Intra_Trade_Cost_RMB_Date1__c = date.today();
        product.Manual_Entry__c = false;
        product.Asset_Model_No__c = '11111';
        product.ProductCode = 'OTV-SP1H-NA-12E';
        product.Category3__c = '主机';
        product.Family = 'ENG';
        product.MDM_Model_No__c = 'dsfdf_USG-400_jfoewf';
        product.ProductCode_Ext__c = 'dsfad';
        //insert product;
        proList.add(product);

        Product2 product2 = new Product2( Name='テスト商品22');
        product2.SFDA_Status__c = '有効';
        product2.Intra_Trade_List_RMB_1__c = 110;
        product2.Intra_Trade_List_RMB_Date1__c = date.today();
        product2.Intra_Trade_Cost_RMB_1__c = 220;
        product2.Intra_Trade_Cost_RMB_Date1__c = date.today();
        product2.Manual_Entry__c = false;
        product2.Asset_Model_No__c = '882345';
        product2.ProductCode = 'OTV-SP1H-NA-12E9';
        product2.Category3__c = '能量主机';
        product.Family = 'ENG';
        product2.ProductCode_Ext__c = 'dsfadweffd';
        //insert product2;
        proList.add(product2);

        Product2 product3 = new Product2( Name='テスト商品3');
        product3.SFDA_Status__c = '有効';
        product3.Intra_Trade_List_RMB_1__c = 120;
        product3.Intra_Trade_List_RMB_Date1__c = date.today();
        product3.Intra_Trade_Cost_RMB_1__c = 230;
        product3.Intra_Trade_Cost_RMB_Date1__c = date.today();
        product3.Manual_Entry__c = false;
        product3.Asset_Model_No__c = '88990';
        product3.ProductCode = 'OTV-SP1H-NA-12E0';
        product3.Category3__c = '其他';
        product3.ProductCode_Ext__c = 'AAAAAAAAAA';
        product3.MDM_Model_No__c = 'test_USG-400';
        //insert product3;
        proList.add(product3);
        insert proList;
        ////insert new Product2[] {product,product2,product3};
        List<PricebookEntry> pBList = new List<PricebookEntry>();
        PricebookEntry entry3 = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=product3.Id);
        entry3.UnitPrice = 0;
        entry3.IsActive = true;
        entry3.UseStandardPrice = false;
        entry3.CurrencyIsoCode = 'CNY';
        //insert entry3;
        pBList.add(entry3);

        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=product.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        //insert entry;
        pBList.add(entry);

        PricebookEntry entry2 = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=product2.Id);
        entry2.UnitPrice = 0;
        entry2.IsActive = true;
        entry2.UseStandardPrice = false;
        entry2.CurrencyIsoCode = 'CNY';
        //insert entry2;
        pBList.add(entry2);
        insert pBList;
        ////insert new PricebookEntry[] {entry,entry2,entry3};

        List<OpportunityLineItem> oppliList = new List<OpportunityLineItem>();

        OpportunityLineItem oppli = new OpportunityLineItem();
        oppli.OpportunityId = opp.Id;
        oppli.Id__c = '110';
        oppli.SFDA_Status__c = '有効';
        oppli.Name__c = '一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十';
        oppli.ListPrice__c = 112;
        oppli.Quantity = 113;
        oppli.UnitPrice = 114;
        oppli.UnitPrice__c = 115;
        oppli.Qty_Unit__c = '116';
        oppli.Cost__c = 117;
        oppli.BSS_Category__c = 'G&R';
        oppli.Item_Order__c = 1;
        oppli.PricebookEntryId = entry.Id;
        oppliList.add(oppli);
        
        OpportunityLineItem oppli2 = new OpportunityLineItem();
        oppli2.OpportunityId = opp.Id;
        oppli2.Id__c = '110';
        oppli2.SFDA_Status__c = '有効';
        oppli2.Name__c = '一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五';
        oppli2.ListPrice__c = 112;
        oppli2.Quantity = 113;
        oppli2.UnitPrice = 114;
        oppli2.UnitPrice__c = 115;
        oppli2.Qty_Unit__c = '116';
        oppli2.Cost__c = 117;
        oppli2.BSS_Category__c = 'G&R';
        oppli2.Item_Order__c = 1;
        oppli2.PricebookEntryId = entry2.Id;
        oppliList.add(oppli2);

        OpportunityLineItem oppli3 = new OpportunityLineItem();
        oppli3.OpportunityId = opp2.Id;
        //oppli3.OpportunityId = opp.Id;
        oppli3.Id__c = '110';
        oppli3.SFDA_Status__c = '有効';
        oppli3.Name__c = '一二三四五六七八九十一二三四五六七八九九十一二三四五';
        oppli3.ListPrice__c = 112;
        oppli3.Quantity = 113;
        oppli3.UnitPrice = 114;
        oppli3.UnitPrice__c = 115;
        oppli3.Qty_Unit__c = '116';
        oppli3.Cost__c = 117;
        oppli3.BSS_Category__c = 'G&R';
        oppli3.Item_Order__c = 1;
        oppli3.PricebookEntryId = entry2.Id;
        oppliList.add(oppli3);
        //insert oppli3;
        System.Test.startTest();
        insert oppliList;

        //opp.STMS_Package_Sale__c = false;
        //update opp;

        oppliList = new List<OpportunityLineItem>();
        oppli.Name__c = '一二三四五六七八九十一二三四五';
        oppliList.add(oppli);
        //oppli2.Name__c = '一二三四五六七八九十一二三四五六七八九十一二三四五六';
        //oppliList.add(oppli2);
        ////oppli3.Name__c = '一二三四五六七八九八九十一二三四五六';
        ////oppliList.add(oppli3);
        update oppliList;


        //OpportunityLineItem oppli4 = new OpportunityLineItem();
        ////oppli4.OpportunityId = opp2.Id;
        //oppli4.OpportunityId = opp.Id;
        //oppli4.Id__c = '110';
        //oppli4.SFDA_Status__c = '有効';
        //oppli4.Name__c = '一二三四五六七八九十一二三四五六七八九九十一二三四五';
        //oppli4.ListPrice__c = 112;
        //oppli4.Quantity = 113;
        //oppli4.UnitPrice = 114;
        //oppli4.UnitPrice__c = 115;
        //oppli4.Qty_Unit__c = '116';
        //oppli4.Cost__c = 117;
        //oppli4.BSS_Category__c = 'G&R';
        //oppli4.Item_Order__c = 1;
        //oppli4.PricebookEntryId = entry3.Id;
        //insert oppli4;

        //delete oppli2;

        oppliList = new List<OpportunityLineItem>();
        oppliList.add(oppli);
        //oppliList.add(oppli3);
        //oppliList.add(oppli4);
        //delete oppliList;
        System.Test.stopTest();
	}

    static testMethod void testAfterChange2() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }

        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM007TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM007TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;
        List<Opportunity> oppList = new List<Opportunity>();
        Opportunity opp = new Opportunity();
        opp.AccountId           = depart.Id;
        opp.Department_Class__c = section.Id;
        opp.Hospital__c         = company.Id;
        opp.SAP_Send_OK__c      = false;
        opp.Name                = 'GZ-SP-NFM007_1';
        opp.Trade__c            = '内貿';
        opp.StageName           = '引合';
        opp.CloseDate           = date.newinstance(2022, 11, 30);
        opp.STMS_Package_Sale__c = true;
        opp.EnergyHost__c = false;
        //insert opp;
        oppList.add(opp);

        Opportunity opp2 = new Opportunity();
        opp2.AccountId           = depart.Id;
        opp2.Department_Class__c = section.Id;
        opp2.Hospital__c         = company.Id;
        opp2.SAP_Send_OK__c      = false;
        opp2.Name                = 'GZ-SP-NFM007_2';
        opp2.Trade__c            = '内貿';
        opp2.StageName           = '引合';
        opp2.STMS_Package_Sale__c = false;
        opp2.EnergyHost__c = true;
        opp2.CloseDate           = date.newinstance(2022, 11, 30);
        //insert opp2;
        oppList.add(opp2);
        insert oppList;
        List<Product2> proList = new List<Product2>();
        Product2 product = new Product2( Name='テスト商品');
        product.SFDA_Status__c = '有効';
        product.Intra_Trade_List_RMB_1__c = 100;
        product.Intra_Trade_List_RMB_Date1__c = date.today();
        product.Intra_Trade_Cost_RMB_1__c = 200;
        product.Intra_Trade_Cost_RMB_Date1__c = date.today();
        product.Manual_Entry__c = false;
        product.Asset_Model_No__c = '11111';
        product.ProductCode = 'OTV-SP1H-NA-12E';
        product.Category3__c = '主机';
        product.Family = 'ENG';
        product.MDM_Model_No__c = 'dsfdf_USG-400_jfoewf';
        product.ProductCode_Ext__c = 'dsfad';
        //insert product;
        proList.add(product);

        Product2 product2 = new Product2( Name='テスト商品22');
        product2.SFDA_Status__c = '有効';
        product2.Intra_Trade_List_RMB_1__c = 110;
        product2.Intra_Trade_List_RMB_Date1__c = date.today();
        product2.Intra_Trade_Cost_RMB_1__c = 220;
        product2.Intra_Trade_Cost_RMB_Date1__c = date.today();
        product2.Manual_Entry__c = false;
        product2.Asset_Model_No__c = '882345';
        product2.ProductCode = 'OTV-SP1H-NA-12E9';
        product2.Category3__c = '其他';
        product.Family = 'ENG';
        product2.ProductCode_Ext__c = 'dsfadweffd';
        //insert product2;
        proList.add(product2);

        Product2 product3 = new Product2( Name='テスト商品3');
        product3.SFDA_Status__c = '有効';
        product3.Intra_Trade_List_RMB_1__c = 120;
        product3.Intra_Trade_List_RMB_Date1__c = date.today();
        product3.Intra_Trade_Cost_RMB_1__c = 230;
        product3.Intra_Trade_Cost_RMB_Date1__c = date.today();
        product3.Manual_Entry__c = false;
        product3.Asset_Model_No__c = '88990';
        product3.ProductCode = 'OTV-SP1H-NA-12E0';
        product3.Category3__c = '其他';
        product3.ProductCode_Ext__c = 'AAAAAAAAAA';
        product3.MDM_Model_No__c = 'test_USG-400';
        //insert product3;
        proList.add(product3);
        insert proList;
        ////insert new Product2[] {product,product2,product3};
        List<PricebookEntry> pBList = new List<PricebookEntry>();
        PricebookEntry entry3 = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=product3.Id);
        entry3.UnitPrice = 0;
        entry3.IsActive = true;
        entry3.UseStandardPrice = false;
        entry3.CurrencyIsoCode = 'CNY';
        //insert entry3;
        pBList.add(entry3);

        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=product.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        //insert entry;
        pBList.add(entry);

        PricebookEntry entry2 = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=product2.Id);
        entry2.UnitPrice = 0;
        entry2.IsActive = true;
        entry2.UseStandardPrice = false;
        entry2.CurrencyIsoCode = 'CNY';
        //insert entry2;
        pBList.add(entry2);
        insert pBList;
        ////insert new PricebookEntry[] {entry,entry2,entry3};

        List<OpportunityLineItem> oppliList = new List<OpportunityLineItem>();

        OpportunityLineItem oppli = new OpportunityLineItem();
        oppli.OpportunityId = opp.Id;
        oppli.Id__c = '110';
        oppli.SFDA_Status__c = '有効';
        oppli.Name__c = '一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十';
        oppli.ListPrice__c = 112;
        oppli.Quantity = 113;
        oppli.UnitPrice = 114;
        oppli.UnitPrice__c = 115;
        oppli.Qty_Unit__c = '116';
        oppli.Cost__c = 117;
        oppli.BSS_Category__c = 'G&R';
        oppli.Item_Order__c = 1;
        oppli.PricebookEntryId = entry.Id;
        oppliList.add(oppli);
        
        OpportunityLineItem oppli2 = new OpportunityLineItem();
        oppli2.OpportunityId = opp.Id;
        oppli2.Id__c = '110';
        oppli2.SFDA_Status__c = '有効';
        oppli2.Name__c = '一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十一二三四五';
        oppli2.ListPrice__c = 112;
        oppli2.Quantity = 113;
        oppli2.UnitPrice = 114;
        oppli2.UnitPrice__c = 115;
        oppli2.Qty_Unit__c = '116';
        oppli2.Cost__c = 117;
        oppli2.BSS_Category__c = 'G&R';
        oppli2.Item_Order__c = 1;
        oppli2.PricebookEntryId = entry2.Id;
        oppliList.add(oppli2);

        OpportunityLineItem oppli3 = new OpportunityLineItem();
        oppli3.OpportunityId = opp2.Id;
        //oppli3.OpportunityId = opp.Id;
        oppli3.Id__c = '110';
        oppli3.SFDA_Status__c = '有効';
        oppli3.Name__c = '一二三四五六七八九十一二三四五六七八九九十一二三四五';
        oppli3.ListPrice__c = 112;
        oppli3.Quantity = 113;
        oppli3.UnitPrice = 114;
        oppli3.UnitPrice__c = 115;
        oppli3.Qty_Unit__c = '116';
        oppli3.Cost__c = 117;
        oppli3.BSS_Category__c = 'G&R';
        oppli3.Item_Order__c = 1;
        oppli3.PricebookEntryId = entry2.Id;
        oppliList.add(oppli3);
        //insert oppli3;
        System.Test.startTest();
        insert oppliList;

        //opp.STMS_Package_Sale__c = false;
        //update opp;

        oppliList = new List<OpportunityLineItem>();
        oppli.Name__c = '一二三四五六七八九十一二三四五';
        oppliList.add(oppli);
        //oppli2.Name__c = '一二三四五六七八九十一二三四五六七八九十一二三四五六';
        //oppliList.add(oppli2);
        ////oppli3.Name__c = '一二三四五六七八九八九十一二三四五六';
        ////oppliList.add(oppli3);
        update oppliList;


        //OpportunityLineItem oppli4 = new OpportunityLineItem();
        ////oppli4.OpportunityId = opp2.Id;
        //oppli4.OpportunityId = opp.Id;
        //oppli4.Id__c = '110';
        //oppli4.SFDA_Status__c = '有効';
        //oppli4.Name__c = '一二三四五六七八九十一二三四五六七八九九十一二三四五';
        //oppli4.ListPrice__c = 112;
        //oppli4.Quantity = 113;
        //oppli4.UnitPrice = 114;
        //oppli4.UnitPrice__c = 115;
        //oppli4.Qty_Unit__c = '116';
        //oppli4.Cost__c = 117;
        //oppli4.BSS_Category__c = 'G&R';
        //oppli4.Item_Order__c = 1;
        //oppli4.PricebookEntryId = entry3.Id;
        //insert oppli4;

        //delete oppli2;

        oppliList = new List<OpportunityLineItem>();
        oppliList.add(oppli);
        //oppliList.add(oppli3);
        //oppliList.add(oppli4);
        //delete oppliList;
        System.Test.stopTest();
    }
	
	
}