@isTest
private class CurrentPeriodPaymentAmountBatchTest {
	
	@isTest static void test_method_one() {
		ControllerUtil.EscapeNFM001Trigger = true;


		Profile p = [select Id from Profile where id =: System.Label.ProfileId_SystemAdmin];
        User u1 = new User(Test_staff__c = true);
        u1.LastName = '_サンブリッジ';
        u1.FirstName = 'あ';
        u1.Alias = 'あ';
        u1.Email = 'olympusTest01@sunbridge.com';
        u1.Username = 'olympusTest01@sunbridge.com';
        u1.CommunityNickname = 'あ';
        u1.IsActive = true;
        u1.EmailEncodingKey = 'ISO-2022-JP';
        u1.TimeZoneSidKey = 'Asia/Tokyo';
        u1.LocaleSidKey = 'ja_JP';
        u1.LanguageLocaleKey = 'ja';
        u1.ProfileId = p.Id;
        u1.Job_Category__c = '销售服务';
        u1.Province__c = '東京';
        insert u1;

        User u2 = new User(Test_staff__c = true);
        u2.LastName = '_サンブリッジ';
        u2.FirstName = 'い';
        u2.Alias = 'い';
        u2.Email = 'olympusTest02@sunbridge.com';
        u2.Username = 'olympusTest02@sunbridge.com';
        u2.CommunityNickname = 'い';
        u2.IsActive = true;
        u2.EmailEncodingKey = 'ISO-2022-JP';
        u2.TimeZoneSidKey = 'Asia/Tokyo';
        u2.LocaleSidKey = 'ja_JP';
        u2.LanguageLocaleKey = 'ja';
        u2.ProfileId = p.Id;
        u2.Job_Category__c = '销售推广';
        u2.Province__c = '東京';
        insert u2;

		system.runAs(u1) {
			List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
	        if (rectCo.size() == 0) {
	            throw new ControllerUtil.myException('not found 病院 recodetype');
	        }
	        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 消化科'];
	        if (rectSct.size() == 0) {
	            throw new ControllerUtil.myException('not found 戦略科室分類 消化科 recodetype');
	        }
	        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
	        if (rectDpt.size() == 0) {
	            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
	        }
	        // テストデータ
	        Account company = new Account();
	        company.RecordTypeId = rectCo[0].Id;
	        company.Name         = 'NFM108TestCompany';
	        company.FSE_SP_Main_Leader__c = u1.Id;
	        company.FSE_GI_Main_Leader__c = u1.Id;
	        upsert company;
	        Account section = [Select Id, Name, Department_Class_Label__c, ParentId from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];

	        Account depart = new Account();
	        depart.RecordTypeId        = rectDpt[0].Id;
	        depart.Name                = '*';
	        depart.Department_Name__c  = 'NFM108TestDepart';
	        depart.ParentId            = section.Id;
	        depart.Department_Class__c = section.Id;
	        depart.Hospital__c         = company.Id;
	        upsert depart;

	        // 再取得
	        List<Account> accList = new List<Account>();
	        // company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id];
	        // accList.add(company);
	        // section = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :section.Id];
	        // accList.add(section);
	        // depart = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart.Id];
	        // accList.add(depart);
			company = [select Management_Code__c, Name, Id from Account where Id = :company.Id];
	        accList.add(company);
	        section = [select Management_Code__c, Name, Id from Account where Id = :section.Id];
	        accList.add(section);
	        depart = [select Management_Code__c, Name, Id from Account where Id = :depart.Id];
	        accList.add(depart);

	        List<Product2> prdList = new List<Product2>();
	        Product2 prd1 = new Product2();
	        prd1.ProductCode_Ext__c     = 'NFM108Prd1';
	        prd1.ProductCode            = 'NFM108Prd1';
	        prd1.Repair_Product_Code__c = 'NFM108Prd1_RP';
	        prd1.Name                   = 'NFM108Prd1';
	        prd1.Manual_Entry__c        = false;
	        prdList.add(prd1);
	        Product2 prd2 = new Product2();
	        prd2.ProductCode_Ext__c     = 'NFM108Prd2';
	        prd2.ProductCode            = 'NFM108Prd2';
	        prd2.Repair_Product_Code__c = 'NFM108Prd2_RP';
	        prd2.Name                   = 'NFM108Prd2';
	        prd2.Manual_Entry__c        = false;
	        prdList.add(prd2);
	        insert prdList;

	        Asset ast = new Asset();
	        ast.Name                             = 'NFM108Ast1';
	        ast.AccountId                        = depart.Id;
	        ast.Department_Class__c              = section.Id;
	        ast.Hospital__c                      = company.Id;
	        ast.Product2Id                       = prd1.Id;
	        ast.SerialNumber                     = 'NFM108SerialNumber';
	        ast.Guarantee_period_for_products__c = Date.today();
	        ast.InstallDate                      = Date.today();
	        insert ast;
	        // ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c, Department_Class__r.Management_Code_Auto__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber
	        //                          from Asset
	        //                         where Id = :ast.Id];
			ast = [select Id, Name, Product_Serial_No__c, AccountId, Department_Class__c, Hospital__c, Product2Id, Product2.ProductCode, Product2.Repair_Product_Code__c, SerialNumber
	                                 from Asset
	                                where Id = :ast.Id];

	        Repair__c rpr = new Repair__c();
	        rpr.SAPRepairNo__c   = 'NFM108SAP';
	        rpr.Account__c             = depart.Id;
	        rpr.Department_Class__c    = section.Id;
	        rpr.Hospital__c            = company.Id;
	        rpr.Delivered_Product__c   = ast.Id;
	        rpr.SalesOfficeCode_selection__c = '北京';
	        rpr.LastIssueCumulativePaybackAmount__c = 0;
	        insert rpr;
	        rpr = [select Id, Name, Delivered_Product__c, SAPRepairNo__c from Repair__c where Id = :rpr.Id];
	        String strSFDCRepairNo = rpr.Name + ':123';

	        Maintenance_Contract__c mc1 = new Maintenance_Contract__c();
	        mc1.Name = 'mc1';
	        mc1.Past_update_contract__c = true;
	        mc1.RecordTypeId = '01210000000QjeFAAS';
	        mc1.CurrencyIsoCode = 'CNY';
	        mc1.Status__c = '契約';
	        mc1.Hospital__c = company.Id;
	        mc1.Department_Class__c = section.Id;
	        mc1.Department__c = depart.Id;
	        mc1.Maintenance_Contract_No__c = 'mc1';
	        mc1.Contract_Conclusion_Date__c = Date.today();
	        mc1.Contract_End_Date__c = Date.today().addDays(1);
	        mc1.SalesOfficeCode_selection__c = '北京RC';
	        mc1.LastIssueCumulativePaybackAmount__c = 0;
	        mc1.Service_Contract_Staff__c = u1.Id;
	        insert mc1;


	        Date toDate = Date.today();
	        Date expirationDate = Date.newInstance(toDate.year(), 3, 31);
	        Repair_receipt__c rr = new Repair_receipt__c();
	        rr.Name = '123456';
	        rr.UniqueKey__c = '1234567';
	        rr.Repair__c = rpr.Id;
	        rr.Maintenance_Contract__c =  mc1.Id;
	        rr.PaymentAmount__c = Decimal.valueOf('1235');
	        rr.PaymentDate__c = expirationDate;
	        rr.DeadlineLastMonthPeriodPaymentAmountFigu__c = 2;
	        insert rr;

	        System.Test.StartTest();
	        Id execBTId = Database.executeBatch(new CurrentPeriodPaymentAmountBatch(),100);
	        List<String> idList = new List<String>();
	        idList.add(rr.id);
	        execBTId = Database.executeBatch(new CurrentPeriodPaymentAmountBatch(idList),100);
	        System.Test.StopTest();
	        List<Repair__c> repairList = [select Id,Name,
		                            LastIssueCumulativePaybackAmount__c,
		                            LastIssueCumulativePayback__c
		                            		from Repair__c];
		    //若此处通不过，请修改修理字段"上期止累计回款(后台用)"的过滤条件，改为当前财年的4月1日
		    System.assertEquals(1235,repairList[0].LastIssueCumulativePayback__c);
		    System.assertEquals(1235,repairList[0].LastIssueCumulativePaybackAmount__c); 
		    List<Maintenance_Contract__c> mcList = [select Id,Name,
		                            LastIssueCumulativePayback__c,
		                            LastIssueCumulativePaybackAmount__c
		                            		from Maintenance_Contract__c]; 
		    //若此处通不过，请修改维修合同字段"上期止累计回款(后台用)"的过滤条件，改为当前财年的4月1日                        		
		    System.assertEquals(1235,mcList[0].LastIssueCumulativePaybackAmount__c);                        		                      		
	        

		}

		
	}
	
}