@isTest
private class StatuAchievementsPCLControllerTest {

	static testMethod void myUnitTest() {


		RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Hp'];
		List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName IN ('Department_GI', 'Department_BF') order by DeveloperName desc];
		Profile p = [select Id from Profile where id =:System.Label.ProfileId_2S6];
		// ユーザー作成
		User hpOwner = new User(SAP_Send_OFF__c = true,Batch_User__c = true,Quote_Correct__c=true,Quote_Special_Operation__c=true, Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
		insert hpOwner;
		system.runAs(hpOwner){

			Account acc = new Account();
			acc.RecordTypeId = rectCo.Id;
			acc.Name = 'HP test1';
			acc.OCM_Category__c = 'L';
			insert acc;

			List<Account> dept = [select Id, Name from Account where ParentId = :acc.Id and Department_Class_Label__c IN ('消化科', '呼吸科') order by Department_Class_Label__c];

			Account depart1 = new Account();
			depart1.RecordTypeId = rectDpt[0].Id;
			depart1.Name         = '*';
			depart1.Department_Name__c  = 'Gastoro Intestin Test';
			depart1.ParentId            = dept[0].Id;
			depart1.Department_Class__c = dept[0].Id;
			depart1.Hospital__c         = acc.Id;

			Account depart2 = new Account();
			depart2.RecordTypeId = rectDpt[1].Id;
			depart2.Name         = '*';
			depart2.Department_Name__c  = '診療科2';
			depart2.ParentId            = dept[1].Id;
			depart2.Department_Class__c = dept[1].Id;
			depart2.Hospital__c         = acc.Id;
			insert new Account[] {depart1, depart2};

			RecordType oppVND = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity'];

			Opportunity opp1 = new Opportunity(
				Name = 'test opp1',
				StageName = '引合',
				CurrencyIsoCode = 'USD',
				CloseDate = Date.today(),
				AccountId = depart1.Id,
				RecordTypeId = oppVND.Id,
				Closing_Bid_Date__c = Date.today().addDays(-5),
				Hospital__c = acc.Id,
				Tax_Intra_F_Copy__c  = 1,
				Sales_Root__c = 'OCM直接販売',
				Competitor__c = 'A'
				);
			Opportunity opp2 = new Opportunity(
				Name = 'test opp2',
				StageName = '引合',
				CurrencyIsoCode = 'USD',
				CloseDate = Date.today(),
				AccountId = depart2.Id,
				RecordTypeId = oppVND.Id,
				Closing_Bid_Date__c = Date.today().addDays(-5),
				Hospital__c = acc.Id,
				Sales_Root__c = 'OCM直接販売',
				Tax_Intra_F_Copy__c  = 1,
				Competitor__c = 'B'
				);
			insert new Opportunity[] {opp1, opp2};
			// 見積りを追加
			opp1.Estimation_Decision__c = true;
			opp1.Whether_FALSE_order__c = true;
			opp1.Estimation_Id__c    = 'Estimation_Id';
			opp1.Purchase_Type__c    = '一般引合';
			opp1.Promise_Class__c    = '内貿';
			opp1.Fund_Basis__c       = '政府資金';
			opp1.SAP_Province__c     = '北京市';
			opp1.SAP_Send_OK__c      = true;
			opp2.Estimation_Decision__c = true;
			opp2.Whether_FALSE_order__c = true;
			opp2.Estimation_Id__c    = 'Estimation_Id';
			opp2.Purchase_Type__c    = '一般引合';
			opp2.Promise_Class__c    = '内貿';
			opp2.Fund_Basis__c       = '政府資金';
			opp2.SAP_Province__c     = '北京市';
			opp2.SAP_Send_OK__c      = true;
			// System.Test.startTest();
			StaticParameter.EscapeOpportunityBefUpdTrigger = true;
			update new Opportunity[] {opp1, opp2};
			//System.Test.stopTest();
			Oly_TriggerHandler.bypass('PowerBIBaseHandler');
			Statu_Achievements__c sta1 = new Statu_Achievements__c();
			sta1.Statu_Achievements_Ext__c = 'opp1:SoNo1';
			sta1.Name                      = 'SoNo1';
			sta1.Opportunity__c            = opp1.Id;
			sta1.ReturnFlag__c             = false;
			sta1.OverviewStatus__c         = 'OverviewStatus__c';
			sta1.ContractNO__c             = 'ContractNO1';
			sta1.ContractReceivedDate__c   = null;
			sta1.FirstApproveDate__c       = null;
			sta1.LastApproveDate__c        = null;
			sta1.SoLatestDeliveryDate__c   = null;
			sta1.DeliveryDate__c           = null;
			sta1.InstallDate__c            = null;
			sta1.Collection_Day_Invoice__c = System.today();
			sta1.CurrencyIsoCode           = opp1.CurrencyIsoCode;
			sta1.ContractAmount__c         = 1000;
			sta1.PaymentAmount__c          = 0;
			sta1.PaymentRate__c            = '0';
			sta1.DeliveryStatus__c         = 'DeliveryStatus__c';
			sta1.AssignmentStatus__c       = 'AssignmentStatus__c';
			sta1.PaymentStatus__c          = 'PaymentStatus__c';
			sta1.InstallStatus__c          = 'InstallStatus__c';
			sta1.AuthorizationNo__c        = 'AuthorizationNo__c';
			sta1.ContractApplicationNo__c  = 'ContractApplicationNo__c';
			sta1.PaymentTerms__c           = 'PaymentTerms__c';
			sta1.UnshippedAmount__c        = 520.02;
			sta1.SalesChannel__c           = 'SalesChannel__c';
			sta1.SoLatestDeliveryDate__c   = Date.today().addDays(5);
			Statu_Achievements__c sta2 = new Statu_Achievements__c();
			sta2.Statu_Achievements_Ext__c = 'opp2:SoNo2';
			sta2.Name                      = 'SoNo2';
			sta2.Opportunity__c            = opp2.Id;
			sta2.ReturnFlag__c             = false;
			sta2.OverviewStatus__c         = 'OverviewStatus__c';
			sta2.ContractNO__c             = 'ContractNO2';
			sta2.ContractReceivedDate__c   = null;
			sta2.FirstApproveDate__c       = null;
			sta2.LastApproveDate__c        = null;
			sta2.SoLatestDeliveryDate__c   = null;
			sta2.DeliveryDate__c           = null;
			sta2.InstallDate__c            = null;
			sta2.Collection_Day_Invoice__c = System.today();
			sta2.CurrencyIsoCode           = opp2.CurrencyIsoCode;
			sta2.ContractAmount__c         = 1000;
			sta2.PaymentAmount__c          = 0;
			sta2.PaymentRate__c            = '0';
			sta2.DeliveryStatus__c         = 'DeliveryStatus__c';
			sta2.AssignmentStatus__c       = 'AssignmentStatus__c';
			sta2.PaymentStatus__c          = 'PaymentStatus__c';
			sta2.InstallStatus__c          = 'InstallStatus__c';
			sta2.AuthorizationNo__c        = 'AuthorizationNo__c';
			sta2.ContractApplicationNo__c  = 'ContractApplicationNo__c';
			sta2.PaymentTerms__c           = 'PaymentTerms__c';
			sta2.UnshippedAmount__c        = 857.00;
			sta2.SalesChannel__c           = 'SalesChannel__c';
			sta2.SoLatestDeliveryDate__c   = Date.today().addDays(5);
			insert new Statu_Achievements__c[] {sta1, sta2};
			// Test.startTest();
			StatuAchievementsPCLController pcl = new StatuAchievementsPCLController();
			pcl.text = 'Test';
			// vivek start
			// pcl.value = 'Test';
			// pcl.value2 = 'Test';
			// pcl.value3 = 'Test';
			// pcl.value = 'a0N1000000gwjG7';
			// pcl.condition='notequals';
			// pcl.text='S:ReturnFlag__c';
			// pcl.condition='equals';
			// pcl.value = 'false,false';

			// pcl.value2 = '无效';
			// vivek end
			pcl.init();
			// Test.stopTest();

		}
	}
	static testMethod void myUnitTest_Two() {
		RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Hp'];
		List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName IN ('Department_GI', 'Department_BF') order by DeveloperName desc];
		Profile p = [select Id from Profile where id =:System.Label.ProfileId_2S6];
		// ユーザー作成
		User hpOwner = new User(SAP_Send_OFF__c = true,Batch_User__c = true,Quote_Correct__c=true,Quote_Special_Operation__c=true, Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
		insert hpOwner;
		system.runAs(hpOwner){
			Account acc = new Account();
			acc.RecordTypeId = rectCo.Id;
			acc.Name = 'HP test1';
			acc.OCM_Category__c = 'L';
			insert acc;

			List<Account> dept = [select Id, Name from Account where ParentId = :acc.Id and Department_Class_Label__c IN ('消化科', '呼吸科') order by Department_Class_Label__c];

			Account depart1 = new Account();
			depart1.RecordTypeId = rectDpt[0].Id;
			depart1.Name         = '*';
			depart1.Department_Name__c  = 'Gastoro Intestin Test';
			depart1.ParentId            = dept[0].Id;
			depart1.Department_Class__c = dept[0].Id;
			depart1.Hospital__c         = acc.Id;

			Account depart2 = new Account();
			depart2.RecordTypeId = rectDpt[1].Id;
			depart2.Name         = '*';
			depart2.Department_Name__c  = '診療科2';
			depart2.ParentId            = dept[1].Id;
			depart2.Department_Class__c = dept[1].Id;
			depart2.Hospital__c         = acc.Id;
			insert new Account[] {depart1, depart2};

			RecordType oppVND = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity'];

			Opportunity opp1 = new Opportunity(
				Name = 'test opp1',
				StageName = '引合',
				CurrencyIsoCode = 'USD',
				CloseDate = Date.today(),
				AccountId = depart1.Id,
				RecordTypeId = oppVND.Id,
				Closing_Bid_Date__c = Date.today().addDays(-5),
				Hospital__c = acc.Id,
				Sales_Root__c = 'OCM直接販売',
				Competitor__c = 'A'
				);
			Opportunity opp2 = new Opportunity(
				Name = 'test opp2',
				StageName = '引合',
				CurrencyIsoCode = 'USD',
				CloseDate = Date.today(),
				AccountId = depart2.Id,
				RecordTypeId = oppVND.Id,
				Closing_Bid_Date__c = Date.today().addDays(-5),
				Hospital__c = acc.Id,
				Sales_Root__c = 'OCM直接販売',
				Competitor__c = 'B'
				);
			insert new Opportunity[] {opp1, opp2};
			// 見積りを追加
			//opp1.Estimation_Decision__c = true;
			//opp1.Whether_FALSE_order__c = true;
			//opp1.Estimation_Id__c    = 'Estimation_Id';
			//opp1.Purchase_Type__c    = '一般引合';
			//opp1.Promise_Class__c    = '内貿';
			//opp1.Fund_Basis__c       = '政府資金';
			//opp1.SAP_Province__c     = '北京市';
			//opp1.SAP_Send_OK__c      = true;
			//opp2.Estimation_Decision__c = true;
			//opp2.Whether_FALSE_order__c = true;
			//opp2.Estimation_Id__c    = 'Estimation_Id';
			//opp2.Purchase_Type__c    = '一般引合';
			//opp2.Promise_Class__c    = '内貿';
			//opp2.Fund_Basis__c       = '政府資金';
			//opp2.SAP_Province__c     = '北京市';
			//opp2.SAP_Send_OK__c      = true;
			//update new Opportunity[] {opp1, opp2};

			Statu_Achievements__c sta1 = new Statu_Achievements__c();
			sta1.Statu_Achievements_Ext__c = 'opp1:SoNo1';
			sta1.Name                      = 'SoNo1';
			sta1.Opportunity__c            = opp1.Id;
			sta1.ReturnFlag__c             = false;
			sta1.OverviewStatus__c         = 'OverviewStatus__c';
			sta1.ContractNO__c             = 'ContractNO1';
			sta1.ContractReceivedDate__c   = null;
			sta1.FirstApproveDate__c       = null;
			sta1.LastApproveDate__c        = null;
			sta1.SoLatestDeliveryDate__c   = null;
			sta1.DeliveryDate__c           = null;
			sta1.InstallDate__c            = null;
			sta1.Collection_Day_Invoice__c = System.today();
			sta1.CurrencyIsoCode           = opp1.CurrencyIsoCode;
			sta1.ContractAmount__c         = 0;
			sta1.PaymentAmount__c          = 0;
			sta1.PaymentRate__c            = '0';
			sta1.DeliveryStatus__c         = 'DeliveryStatus__c';
			sta1.AssignmentStatus__c       = 'AssignmentStatus__c';
			sta1.PaymentStatus__c          = 'PaymentStatus__c';
			sta1.InstallStatus__c          = 'InstallStatus__c';
			sta1.AuthorizationNo__c        = 'AuthorizationNo__c';
			sta1.ContractApplicationNo__c  = 'ContractApplicationNo__c';
			sta1.PaymentTerms__c           = 'PaymentTerms__c';
			sta1.UnshippedAmount__c        = 582.01;
			sta1.SalesChannel__c           = 'SalesChannel__c';
			sta1.SoLatestDeliveryDate__c   = Date.today().addDays(5);
			Statu_Achievements__c sta2 = new Statu_Achievements__c();
			sta2.Statu_Achievements_Ext__c = 'opp2:SoNo2';
			sta2.Name                      = 'SoNo2';
			sta2.Opportunity__c            = opp2.Id;
			sta2.ReturnFlag__c             = false;
			sta2.OverviewStatus__c         = 'OverviewStatus__c';
			sta2.ContractNO__c             = 'ContractNO2';
			sta2.ContractReceivedDate__c   = null;
			sta2.FirstApproveDate__c       = null;
			sta2.LastApproveDate__c        = null;
			sta2.SoLatestDeliveryDate__c   = null;
			sta2.DeliveryDate__c           = null;
			sta2.InstallDate__c            = null;
			sta2.Collection_Day_Invoice__c = System.today();
			sta2.CurrencyIsoCode           = opp2.CurrencyIsoCode;
			sta2.ContractAmount__c         = 0;
			sta2.PaymentAmount__c          = 0;
			sta2.PaymentRate__c            = '0';
			sta2.DeliveryStatus__c         = 'DeliveryStatus__c';
			sta2.AssignmentStatus__c       = 'AssignmentStatus__c';
			sta2.PaymentStatus__c          = 'PaymentStatus__c';
			sta2.InstallStatus__c          = 'InstallStatus__c';
			sta2.AuthorizationNo__c        = 'AuthorizationNo__c';
			sta2.ContractApplicationNo__c  = 'ContractApplicationNo__c';
			sta2.PaymentTerms__c           = 'PaymentTerms__c';
			sta2.UnshippedAmount__c        = 0;
			sta2.SalesChannel__c           = 'SalesChannel__c';
			sta2.SoLatestDeliveryDate__c   = Date.today().addDays(5);
			insert new Statu_Achievements__c[] {sta1, sta2};

			PageReference page = new PageReference('/apex/AssetModifyBelongs?uid=' +hpOwner.Id+'&hpid='+acc.Id);
			page.setRedirect(true);
			System.Test.setCurrentPage(page);

			StatuAchievementsPCLController pcl = new StatuAchievementsPCLController();
			// vivek start
			pcl.text='S:Status_2_Formula__c';
			pcl.condition='equals';
			pcl.value = '已完全交付,无效';
			// vivek end
			pcl.init();
			// vivek start
			pcl.text='S:Status_2_Formula__c';
			pcl.condition='notequals';
			pcl.value = '已完全交付,无效';
			// vivek end
			pcl.searchOpp();
			//System.assertEquals(1, pcl.pclInfos.size());
			//System.assertEquals(sta1.Id, pcl.pclInfos[0].rec.Id);

			pcl.text3 = '';
			// vivek start
			pcl.text='S:Status_2_Formula__c';
			pcl.condition='contains';
			pcl.value = '已完全交付,无效';
			// vivek end
			pcl.searchOpp();
			pcl.preSortKey = '0';
			pcl.sortKey = '0';    // ContractNO__c
			pcl.sortTable();      // preSortKey と sortKey が同じのため、desc になります
			//System.assertEquals(2, pcl.pclInfos.size());
			//System.assertEquals(sta2.Id, pcl.pclInfos[0].rec.Id);
			//System.assertEquals(sta1.Id, pcl.pclInfos[1].rec.Id);

			// 保存後、ソートの確認
			//pcl.pclInfos[0].rec.ContractNO__c = 'ContractNO__B';
			//pcl.pclInfos[1].rec.ContractNO__c = 'ContractNO__A';
			pcl.saveType = '2';
			pcl.onlyOpp = true;
			pcl.save();
			//System.assertEquals(sta1.Id, pcl.pclInfos[0].rec.Id);
			//System.assertEquals(sta2.Id, pcl.pclInfos[1].rec.Id);
		}
	}
}