@isTest
private class RollupToMaintenanceContractBatchTest {
    
        static testMethod void testExecuteSAP() {
                ControllerUtil.EscapeNFM001Trigger = true;
                // 病院を作る
                Account hospital = new Account();
                hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
                hospital.Name = 'test hospital';
                hospital.FSE_SP_Main_Leader__c = UserInfo.getUserId();
                hospital.FSE_GI_Main_Leader__c = UserInfo.getUserId();
                insert hospital;
                
                // 戦略科室を得る
                List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
                
                // 診療科を作る
                Account dep = new Account();
                dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'].id;
                dep.Name = 'test dep';
                dep.ParentId = strategicDep[0].Id;
                dep.Department_Class__c = strategicDep[0].Id;
                dep.Hospital__c = hospital.Id;
                insert dep;
                
                // 维修合同を作成する
                Maintenance_Contract__c contract = new Maintenance_Contract__c();
                contract.Name = 'tect contract';
                contract.status__c ='契約';
                contract.Maintenance_Contract_No__c = 'Kami_Contract_No';
                contract.Contract_Conclusion_Date__c = Date.today();
                contract.Hospital__c = hospital.Id;
                contract.Department_Class__c = strategicDep[0].Id;
                contract.Department__c = dep.Id;
                contract.Contract_Start_Date__c = Date.today().addDays(-3);
                contract.Contract_End_Date__c = Date.today();
                contract.SalesOfficeCode_selection__c = '北京RC';
                
                Maintenance_Contract__c contract2 = new Maintenance_Contract__c();
                contract2.Name = 'tect contract2';
                contract2.status__c ='契約';
                contract2.Maintenance_Contract_No__c = 'Kami_Contract_No_2';
                contract2.Contract_Conclusion_Date__c = Date.today();
                contract2.Hospital__c = hospital.Id;
                contract2.Department_Class__c = strategicDep[0].Id;
                contract2.Department__c = dep.Id;
                contract2.Contract_Start_Date__c = Date.today().addDays(-3);
                contract2.Contract_End_Date__c = Date.today();
                contract2.SalesOfficeCode_selection__c = '北京RC';
                insert new Maintenance_Contract__c[] {contract,contract2};
                contract = [Select Id, Status__c from Maintenance_Contract__c where Id = :contract.Id];
                System.assertEquals('契約', contract.Status__c);
                
                Product2 prd1 = new Product2();
                prd1.ProductCode_Ext__c     = 'Prd1';
                prd1.ProductCode            = 'Prd1';
                prd1.Repair_Product_Code__c = 'Prd1_RP';
                prd1.Name                   = 'Prd1';
                prd1.Manual_Entry__c        = false;
                        insert prd1;
                
                Asset ast1 = new Asset();
                ast1.Name                   = '保有設備1';
                ast1.Hospital__c            = hospital.Id;
                ast1.Department_Class__c    = strategicDep[0].Id;
                ast1.AccountId              = dep.Id;
                ast1.Product2Id             = prd1.Id;
                ast1.SerialNumber           = 'SerialNumber1';
                ast1.Guarantee_period_for_products__c = Date.today();
                ast1.InstallDate                      = Date.today();
                
                Asset ast2 = new Asset();
                ast2.Name                   = '保有設備2';
                ast2.Hospital__c            = hospital.Id;
                ast2.Department_Class__c    = strategicDep[0].Id;
                ast2.AccountId              = dep.Id;
                ast2.Product2Id             = prd1.Id;
                ast2.SerialNumber           = 'SerialNumber2';
                ast2.Guarantee_period_for_products__c = Date.today();
                ast2.InstallDate                      = Date.today();
                
                insert new Asset[] {ast1,ast2};
                
                Maintenance_Contract_Asset__c mca = new Maintenance_Contract_Asset__c();
                mca.Asset__c = ast1.Id;
                mca.Maintenance_Contract__c = contract.id;
                
                Maintenance_Contract_Asset__c mca2 = new Maintenance_Contract_Asset__c();
                mca2.Asset__c = ast2.Id;
                mca2.Maintenance_Contract__c = contract2.id;
                
                insert new Maintenance_Contract_Asset__c[] {mca, mca2};
                System.Test.StartTest();
                Repair__c repair1 = new Repair__c();
                repair1.Service_Repair_No__c = 'repair1';
                repair1.Hospital__c            = hospital.Id;
                repair1.Department_Class__c    = strategicDep[0].Id;
                repair1.Account__c             = dep.Id;
                repair1.Delivered_Product__c = ast1.Id;
                repair1.Repair_List_Price__c = 100;
                repair1.Failure_Occurrence_Date__c = Date.today().addDays(-2);
                repair1.Maintenance_Contract__c = contract.Id;
                repair1.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair1.Final_complete_day__c = Date.today().addDays(-1);
                
                Repair__c repair2 = new Repair__c();
                repair2.Service_Repair_No__c = 'repair2';
                repair2.Hospital__c            = hospital.Id;
                repair2.Department_Class__c    = strategicDep[0].Id;
                repair2.Account__c             = dep.Id;
                repair2.Delivered_Product__c = ast1.Id;
                repair2.Repair_List_Price__c = 200;
                repair2.Failure_Occurrence_Date__c = Date.today().addDays(-3);
                repair2.Maintenance_Contract__c = contract.Id;
                repair2.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair2.Final_complete_day__c = Date.today().addDays(-1);
                
                Repair__c repair3 = new Repair__c();
                repair3.Service_Repair_No__c = 'repair3';
                repair3.Hospital__c            = hospital.Id;
                repair3.Department_Class__c    = strategicDep[0].Id;
                repair3.Account__c             = dep.Id;
                repair3.Delivered_Product__c = ast1.Id;
                repair3.Repair_List_Price__c = 300;
                repair3.Failure_Occurrence_Date__c = Date.today().addDays(-1);
                repair3.Maintenance_Contract__c = contract.Id;
                repair3.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair3.Final_complete_day__c = Date.today().addDays(-1);
                
                Repair__c repair4 = new Repair__c();
                repair4.Service_Repair_No__c = 'repair4';
                repair4.Hospital__c            = hospital.Id;
                repair4.Department_Class__c    = strategicDep[0].Id;
                repair4.Account__c             = dep.Id;
                repair4.Delivered_Product__c = ast1.Id;
                repair4.Repair_List_Price__c = 400;
                repair4.Failure_Occurrence_Date__c = Date.today().addDays( 1);
                repair4.Maintenance_Contract__c = contract.Id;
                repair4.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair4.Final_complete_day__c = Date.today().addDays(-1);
                
                Repair__c repair5 = new Repair__c();
                repair5.Service_Repair_No__c = 'repair5';
                repair5.Hospital__c            = hospital.Id;
                repair5.Department_Class__c    = strategicDep[0].Id;
                repair5.Account__c             = dep.Id;
                repair5.Delivered_Product__c = ast1.Id;
                repair5.Repair_List_Price__c = 500;
                repair5.Failure_Occurrence_Date__c = Date.today().addDays(-4);
                repair5.Maintenance_Contract__c = contract.Id;
                repair5.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair5.Final_complete_day__c = Date.today().addDays(-1);
                
                Repair__c repair6 = new Repair__c();
                repair6.Service_Repair_No__c = 'repair6';
                repair6.Hospital__c            = hospital.Id;
                repair6.Department_Class__c    = strategicDep[0].Id;
                repair6.Account__c             = dep.Id;
                repair6.Delivered_Product__c = ast2.Id;
                repair6.Repair_List_Price__c = 111;
                repair6.Failure_Occurrence_Date__c = Date.today().addDays(-2);
                repair6.Maintenance_Contract__c = contract2.Id;
                repair6.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair6.Final_complete_day__c = Date.today().addDays(-1);
                
                insert new Repair__c[] {repair1,repair2,repair3,repair4,repair5, repair6};


                Repair_Quotation__c rq1 = new Repair_Quotation__c();
                rq1.Name  = 'testBatch1';
                rq1.Repair__c = repair1.Id;
                rq1.Servince_contract_discount_amount__c = 12345;
                insert rq1;

                Repair_Quotation__c rq2 = new Repair_Quotation__c();
                rq2.Name  = 'testBatch2';
                rq2.Repair__c = repair2.Id;
                rq2.Servince_contract_discount_amount__c = 12345;
                rq2.Set_discount__c = 23456;
                insert rq2;

                Repair_Quotation__c rq3 = new Repair_Quotation__c();
                rq3.Name  = 'testBatch3';
                rq3.Repair__c = repair3.Id;
                rq3.Servince_contract_discount_amount__c = 12345;
                rq3.sales_discount__c = 34567;
                insert rq3;

                Repair_Quotation__c rq4 = new Repair_Quotation__c();
                rq4.Name  = 'testBatch4';
                rq4.Repair__c = repair4.Id;
                rq4.Servince_contract_discount_amount__c = 12345;
                insert rq4;

                Repair_Quotation__c rq5 = new Repair_Quotation__c();
                rq5.Name  = 'testBatch5';
                rq5.Repair__c = repair5.Id;
                rq5.Servince_contract_discount_amount__c = 12345;
                insert rq5;

                Repair_Quotation__c rq6 = new Repair_Quotation__c();
                rq6.Name  = 'testBatch6';
                rq6.Repair__c = repair6.Id;
                rq6.Servince_contract_discount_amount__c = 12345;
                insert rq6;

                repair1.Repair_Quotation_Id__c = rq1.Id;
                repair2.Repair_Quotation_Id__c = rq2.Id;
                repair3.Repair_Quotation_Id__c = rq3.Id;
                repair4.Repair_Quotation_Id__c = rq4.Id;
                repair5.Repair_Quotation_Id__c = rq5.Id;
                repair6.Repair_Quotation_Id__c = rq6.Id;
                update new Repair__c[] {repair1,repair2,repair3,repair4,repair5, repair6};
                
                Maintenance_Contract__c[] mc1 = [select id,Sum_repair_price__c from Maintenance_Contract__c where Id = : contract.Id];
                System.assertEquals(null, mc1[0].Sum_repair_price__c);
                
                Maintenance_Contract__c[] mc2 = [select id,Sum_repair_price__c from Maintenance_Contract__c where Id = : contract2.Id];
                System.assertEquals(null, mc2[0].Sum_repair_price__c);
                
                
                Id execBTId = Database.executeBatch(new RollupToMaintenanceContractBatch(), 5);
                System.Test.StopTest();
                
                Maintenance_Contract__c[] mc3 = [select id,Sum_repair_price__c from Maintenance_Contract__c where Id = : contract.Id];
                //System.assertEquals(119748, mc3[0].Sum_repair_price__c);
                
                Maintenance_Contract__c[] mc4 = [select id,Sum_repair_price__c from Maintenance_Contract__c where Id = : contract2.Id];
                //System.assertEquals(12345, mc4[0].Sum_repair_price__c);
        }

    static testMethod void testExecuteNoSAP() {
        // 病院を作る
                ControllerUtil.EscapeNFM001Trigger = true;
                Account hospital = new Account();
                hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
                hospital.Name = 'test hospital';
                hospital.FSE_SP_Main_Leader__c = UserInfo.getUserId();
                hospital.FSE_GI_Main_Leader__c = UserInfo.getUserId();
                insert hospital;
                
                // 戦略科室を得る
                List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
                
                // 診療科を作る
                Account dep = new Account();
                dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'].id;
                dep.Name = 'test dep';
                dep.ParentId = strategicDep[0].Id;
                dep.Department_Class__c = strategicDep[0].Id;
                dep.Hospital__c = hospital.Id;
                insert dep;
                
                // 维修合同を作成する
                Maintenance_Contract__c contract = new Maintenance_Contract__c();
                contract.Name = 'tect contract';
                contract.status__c ='契約';
                contract.Maintenance_Contract_No__c = 'Kami_Contract_No';
                contract.Contract_Conclusion_Date__c = Date.today();
                contract.Hospital__c = hospital.Id;
                contract.Department_Class__c = strategicDep[0].Id;
                contract.Department__c = dep.Id;
                contract.Contract_Start_Date__c = Date.today().addDays(-3);
                contract.Contract_End_Date__c = Date.today();
                contract.SalesOfficeCode_selection__c = '北京RC';
                
                Maintenance_Contract__c contract2 = new Maintenance_Contract__c();
                contract2.Name = 'tect contract2';
                contract2.status__c ='契約';
                contract2.Maintenance_Contract_No__c = 'Kami_Contract_No_2';
                contract2.Contract_Conclusion_Date__c = Date.today();
                contract2.Hospital__c = hospital.Id;
                contract2.Department_Class__c = strategicDep[0].Id;
                contract2.Department__c = dep.Id;
                contract2.Contract_Start_Date__c = Date.today().addDays(-3);
                contract2.Contract_End_Date__c = Date.today();
                contract2.SalesOfficeCode_selection__c = '北京RC';
                insert new Maintenance_Contract__c[] {contract,contract2};
                contract = [Select Id, Status__c from Maintenance_Contract__c where Id = :contract.Id];
                System.assertEquals('契約', contract.Status__c);
                
                Product2 prd1 = new Product2();
                prd1.ProductCode_Ext__c     = 'Prd1';
                prd1.ProductCode            = 'Prd1';
                prd1.Repair_Product_Code__c = 'Prd1_RP';
                prd1.Name                   = 'Prd1';
                prd1.Manual_Entry__c        = false;
                insert prd1;
                
                Asset ast1 = new Asset();
                ast1.Name                   = '保有設備1';
                ast1.Hospital__c            = hospital.Id;
                ast1.Department_Class__c    = strategicDep[0].Id;
                ast1.AccountId              = dep.Id;
                ast1.Product2Id             = prd1.Id;
                ast1.SerialNumber           = 'SerialNumber1';
                ast1.Guarantee_period_for_products__c = Date.today();
                ast1.InstallDate                      = Date.today();
                
                Asset ast2 = new Asset();
                ast2.Name                   = '保有設備2';
                ast2.Hospital__c            = hospital.Id;
                ast2.Department_Class__c    = strategicDep[0].Id;
                ast2.AccountId              = dep.Id;
                ast2.Product2Id             = prd1.Id;
                ast2.SerialNumber           = 'SerialNumber2';
                ast2.Guarantee_period_for_products__c = Date.today();
                ast2.InstallDate                      = Date.today();
                
                insert new Asset[] {ast1,ast2};
                
                Maintenance_Contract_Asset__c mca = new Maintenance_Contract_Asset__c();
                mca.Asset__c = ast1.Id;
                mca.Maintenance_Contract__c = contract.id;
                
                Maintenance_Contract_Asset__c mca2 = new Maintenance_Contract_Asset__c();
                mca2.Asset__c = ast2.Id;
                mca2.Maintenance_Contract__c = contract2.id;
                
                insert new Maintenance_Contract_Asset__c[] {mca, mca2};
                
                Repair__c repair1 = new Repair__c();
                repair1.Service_Repair_No__c = 'repair1';
                repair1.Hospital__c            = hospital.Id;
                repair1.Department_Class__c    = strategicDep[0].Id;
                repair1.Account__c             = dep.Id;
                repair1.Delivered_Product__c = ast1.Id;
                repair1.Repair_List_Price__c = 100;
                repair1.Failure_Occurrence_Date__c = Date.today().addDays(-2);
                repair1.Maintenance_Contract__c = contract.Id;
                repair1.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair1.Final_complete_day__c = Date.today().addDays(-1);
                
                Repair__c repair2 = new Repair__c();
                repair2.Service_Repair_No__c = 'repair2';
                repair2.Hospital__c            = hospital.Id;
                repair2.Department_Class__c    = strategicDep[0].Id;
                repair2.Account__c             = dep.Id;
                repair2.Delivered_Product__c = ast1.Id;
                repair2.Repair_List_Price__c = 200;
                repair2.Failure_Occurrence_Date__c = Date.today().addDays(-3);
                repair2.Maintenance_Contract__c = contract.Id;
                repair2.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair2.Final_complete_day__c = Date.today().addDays(-1);
                
                Repair__c repair3 = new Repair__c();
                repair3.Service_Repair_No__c = 'repair3';
                repair3.Hospital__c            = hospital.Id;
                repair3.Department_Class__c    = strategicDep[0].Id;
                repair3.Account__c             = dep.Id;
                repair3.Delivered_Product__c = ast1.Id;
                repair3.Repair_List_Price__c = 300;
                repair3.Failure_Occurrence_Date__c = Date.today().addDays(-1);
                repair3.Maintenance_Contract__c = contract.Id;
                repair3.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair3.Final_complete_day__c = Date.today().addDays(-1);
                
                Repair__c repair4 = new Repair__c();
                repair4.Service_Repair_No__c = 'repair4';
                repair4.Hospital__c            = hospital.Id;
                repair4.Department_Class__c    = strategicDep[0].Id;
                repair4.Account__c             = dep.Id;
                repair4.Delivered_Product__c = ast1.Id;
                repair4.Repair_List_Price__c = 400;
                repair4.Failure_Occurrence_Date__c = Date.today().addDays( 1);
                repair4.Maintenance_Contract__c = contract.Id;
                repair4.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair4.Final_complete_day__c = Date.today().addDays(-1);
                
                Repair__c repair5 = new Repair__c();
                repair5.Service_Repair_No__c = 'repair5';
                repair5.Hospital__c            = hospital.Id;
                repair5.Department_Class__c    = strategicDep[0].Id;
                repair5.Account__c             = dep.Id;
                repair5.Delivered_Product__c = ast1.Id;
                repair5.Repair_List_Price__c = 500;
                repair5.Failure_Occurrence_Date__c = Date.today().addDays(-4);
                repair5.Maintenance_Contract__c = contract.Id;
                repair5.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair5.Final_complete_day__c = Date.today().addDays(-1);
                
                Repair__c repair6 = new Repair__c();
                repair6.Service_Repair_No__c = 'repair6';
                repair6.Hospital__c            = hospital.Id;
                repair6.Department_Class__c    = strategicDep[0].Id;
                repair6.Account__c             = dep.Id;
                repair6.Delivered_Product__c = ast2.Id;
                repair6.Repair_List_Price__c = 111;
                repair6.Failure_Occurrence_Date__c = Date.today().addDays(-2);
                repair6.Maintenance_Contract__c = contract2.Id;
                repair6.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair6.Final_complete_day__c = Date.today().addDays(-1);
                
                insert new Repair__c[] {repair1,repair2,repair3,repair4,repair5, repair6};
                
                Maintenance_Contract__c[] mc1 = [select id,Sum_repair_price__c from Maintenance_Contract__c where Id = : contract.Id];
                System.assertEquals(null, mc1[0].Sum_repair_price__c);
                
                Maintenance_Contract__c[] mc2 = [select id,Sum_repair_price__c from Maintenance_Contract__c where Id = : contract2.Id];
                System.assertEquals(null, mc2[0].Sum_repair_price__c);
                
                System.Test.StartTest();
                Id execBTId = Database.executeBatch(new RollupToMaintenanceContractBatch(), 5);
                System.Test.StopTest();
                
                Maintenance_Contract__c[] mc3 = [select id,Sum_repair_price__c from Maintenance_Contract__c where Id = : contract.Id];
                // System.assertEquals(600, mc3[0].Sum_repair_price__c);
                
                Maintenance_Contract__c[] mc4 = [select id,Sum_repair_price__c from Maintenance_Contract__c where Id = : contract2.Id];
                // System.assertEquals(111, mc4[0].Sum_repair_price__c);
    }
    // static testMethod void testbaojiagaishan() {
    //     ControllerUtil.EscapeNFM001Trigger = true;
    //     Oly_TriggerHandler.bypass('SyncRepairFromMCAETrigger');
        
    //             // 病院を作る
    //             Account hospital = new Account();
    //             hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
    //             hospital.Name = 'test hospital';
    //             hospital.FSE_SP_Main_Leader__c = UserInfo.getUserId();
    //             hospital.FSE_GI_Main_Leader__c = UserInfo.getUserId();
    //             insert hospital;
                
    //             // 戦略科室を得る
    //             List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
                
    //             // 診療科を作る
    //             Account dep = new Account();
    //             dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'].id;
    //             dep.Name = 'test dep';
    //             dep.ParentId = strategicDep[0].Id;
    //             dep.Department_Class__c = strategicDep[0].Id;
    //             dep.Hospital__c = hospital.Id;
    //             insert dep;
    //             MaintanceContractPack__c mcp = new MaintanceContractPack__c();
    //             mcp.Limit_Price_Amount__c = 1000000;
    //             insert mcp;
    //             // 维修合同を作成する

    //             Maintenance_Contract__c contract2 = new Maintenance_Contract__c();
    //             contract2.Name = 'tect contract2';
    //             contract2.status__c ='契約';
    //             contract2.Maintenance_Contract_No__c = 'Kami_Contract_No_2123';
    //             contract2.Contract_Conclusion_Date__c = Date.today();
    //             contract2.Hospital__c = hospital.Id;
    //             contract2.Department_Class__c = strategicDep[0].Id;
    //             contract2.Department__c = dep.Id;
    //             contract2.Limit_Price__c = true;
    //             contract2.agree_Upper_limit__c = true;
    //             contract2.MaintanceContractPack__c = mcp.id;
    //             contract2.Contract_Start_Date__c = Date.today();
    //             contract2.Contract_End_Date__c = Date.today().addMonths(12);
    //             contract2.SalesOfficeCode_selection__c = '北京RC';
    //             insert contract2;

    //             Maintenance_Contract__c contract21 = new Maintenance_Contract__c();
    //             contract21.Name = 'tect contract2';
    //             contract21.status__c ='契約';
    //             contract21.Maintenance_Contract_No__c = 'Kami_Contract_No_2123';
    //             contract21.Contract_Conclusion_Date__c = Date.today();
    //             contract21.Hospital__c = hospital.Id;
    //             contract21.Department_Class__c = strategicDep[0].Id;
    //             contract21.Department__c = dep.Id;
    //             contract21.Limit_Price__c = true;
    //             contract21.agree_Upper_limit__c = true;
    //             contract21.MaintanceContractPack__c = mcp.id;
    //             contract21.Contract_Start_Date__c = Date.today();
    //             contract21.RecordTypeId = Schema.SObjectType.Maintenance_Contract__c.getRecordTypeInfosByDeveloperName().get('VM_Contract').getRecordTypeId();
    //             contract21.Contract_End_Date__c = Date.today().addMonths(12);
    //             contract21.SalesOfficeCode_selection__c = '北京RC';
    //             insert contract21;

    //             Product2 prd1 = new Product2();
    //             prd1.ProductCode_Ext__c     = 'Prd1';
    //             prd1.ProductCode            = 'Prd1';
    //             prd1.Repair_Product_Code__c = 'Prd1_RP';
    //             prd1.Name                   = 'Prd1';
    //             prd1.Manual_Entry__c        = false;
    //                     insert prd1;
                
    //             Asset ast1 = new Asset();
    //             ast1.Name                   = '保有設備1';
    //             ast1.Hospital__c            = hospital.Id;
    //             ast1.Department_Class__c    = strategicDep[0].Id;
    //             ast1.AccountId              = dep.Id;
    //             ast1.Product2Id             = prd1.Id;
    //             ast1.SerialNumber           = 'SerialNumber1';
    //             ast1.Guarantee_period_for_products__c = Date.today();
    //             ast1.InstallDate                      = Date.today();
                
    //             insert ast1;
    //             Asset ast2 = new Asset();
    //             ast2.Name                   = '保有設備2';
    //             ast2.Hospital__c            = hospital.Id;
    //             ast2.Department_Class__c    = strategicDep[0].Id;
    //             ast2.AccountId              = dep.Id;
    //             ast2.Product2Id             = prd1.Id;
    //             ast2.SerialNumber           = 'SerialNumber2';
    //             ast2.Guarantee_period_for_products__c = Date.today();
    //             ast2.InstallDate     = Date.today();
    //             insert ast2;
                
    //             Maintenance_Contract_Asset__c mca2 = new Maintenance_Contract_Asset__c();
    //             mca2.Asset__c = ast1.Id;
    //             mca2.Maintenance_Contract__c = contract2.id;
                
    //             insert mca2;
    //             Repair__c repair1 = new Repair__c();
    //             repair1.Service_Repair_No__c = 'repair7';
    //             repair1.Hospital__c            = hospital.Id;
    //             repair1.Department_Class__c    = strategicDep[0].Id;
    //             repair1.Account__c             = dep.Id;
    //             repair1.Delivered_Product__c = ast1.Id;
    //             repair1.Repair_List_Price__c = 500;
    //             repair1.Agreed_Date__c = Date.today();
    //             repair1.Failure_Occurrence_Date__c = Date.today().addMonths(6);
    //             repair1.Maintenance_Contract__c = contract2.Id;
    //             repair1.Service_contract_judege_day__c = Date.today().addDays(-2);
    //             repair1.Final_complete_day__c = Date.today().addDays(-1);
    //             insert repair1;

    //             Repair__c repair2 = new Repair__c();
    //             repair2.Service_Repair_No__c = 'repair1';
    //             repair2.Hospital__c            = hospital.Id;
    //             repair2.Department_Class__c    = strategicDep[0].Id;
    //             repair2.Account__c             = dep.Id;
    //             repair2.Delivered_Product__c = ast2.Id;
    //             repair2.Repair_List_Price__c = 500;
    //             repair2.Agreed_Date__c = Date.today();
    //             repair2.Failure_Occurrence_Date__c = Date.today().addMonths(6);
    //             repair2.VM_Maintenance_Contract__c = contract21.Id;
    //             repair2.Service_contract_judege_day__c = Date.today().addDays(-2);
    //             repair2.Final_complete_day__c = Date.today().addDays(-1);
    //             insert repair2;

    //             Repair_Quotation__c rq1 = new Repair_Quotation__c();
    //             rq1.Name  = 'testBatch1';
    //             rq1.Repair__c = repair1.Id;
    //             rq1.Servince_contract_discount_amount__c = 12345;
    //             insert rq1;

               

    //             repair1.Repair_Quotation_Id__c = rq1.Id;

    //             update repair1;
    //              Maintenance_Contract__c[] mc3 = [select id from Maintenance_Contract__c];
    //              List<ID> ids  = new List<ID>();
    //             for (Maintenance_Contract__c mc : mc3) {
    //                    ids.add(mc.id);
    //             }
                
                
    //             System.Test.StartTest();
    //             Id execBTId = Database.executeBatch(new RollupToMaintenanceContractBatch(ids), 5);
    //             System.Test.StopTest();
    // }


     static testMethod void method5() {
        // 病院を作る
                ControllerUtil.EscapeNFM001Trigger = true;
                Account hospital = new Account();
                hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
                hospital.Name = 'test hospital';
                hospital.FSE_SP_Main_Leader__c = UserInfo.getUserId();
                hospital.FSE_GI_Main_Leader__c = UserInfo.getUserId();
                insert hospital;
                
                // 戦略科室を得る
                List<Account> strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
                
                // 診療科を作る
                Account dep = new Account();
                dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'].id;
                dep.Name = 'test dep';
                dep.ParentId = strategicDep[0].Id;
                dep.Department_Class__c = strategicDep[0].Id;
                dep.Hospital__c = hospital.Id;
                insert dep;
                MaintanceContractPack__c mcp = new MaintanceContractPack__c();
                mcp.Limit_Price_Amount__c = 1000000;
                mcp.Contract_Type__c='上限合同';
                insert mcp;
                // 维修合同を作成する
                Maintenance_Contract__c contract = new Maintenance_Contract__c();
                contract.Name = 'tect contract';
                contract.status__c ='契約';
                contract.Maintenance_Contract_No__c = 'Kami_Contract_No';
                contract.Contract_Conclusion_Date__c = Date.today();
                contract.Hospital__c = hospital.Id;
                contract.Department_Class__c = strategicDep[0].Id;
                contract.Department__c = dep.Id;
                contract.Limit_Price__c = true;
                contract.agree_Upper_limit__c = true;
                contract.MaintanceContractPack__c = mcp.id;
                contract.Contract_Start_Date__c = Date.today().addDays(-3);
                contract.Contract_End_Date__c = Date.today();
                contract.SalesOfficeCode_selection__c = '北京RC';
                
                Maintenance_Contract__c contract2 = new Maintenance_Contract__c();
                contract2.Name = 'tect contract2';
                contract2.status__c ='契約';
                contract2.Maintenance_Contract_No__c = 'Kami_Contract_No_2';
                contract2.Contract_Conclusion_Date__c = Date.today();
                contract2.Hospital__c = hospital.Id;
                contract2.Limit_Price__c = true;
                contract2.agree_Upper_limit__c = true;
                contract2.Department_Class__c = strategicDep[0].Id;
                contract2.Department__c = dep.Id;
                contract2.MaintanceContractPack__c = mcp.id;
                contract2.Contract_Start_Date__c = Date.today().addDays(-3);
                contract2.Contract_End_Date__c = Date.today();
                contract2.SalesOfficeCode_selection__c = '北京RC';
                insert new Maintenance_Contract__c[] {contract,contract2};
                contract = [Select Id, Status__c from Maintenance_Contract__c where Id = :contract.Id];
                System.assertEquals('契約', contract.Status__c);
                
                Product2 prd1 = new Product2();
                prd1.ProductCode_Ext__c     = 'Prd1';
                prd1.ProductCode            = 'Prd1';
                prd1.Repair_Product_Code__c = 'Prd1_RP';
                prd1.Name                   = 'Prd1';
                prd1.Manual_Entry__c        = false;
                insert prd1;
                
                Asset ast1 = new Asset();
                ast1.Name                   = '保有設備1';
                ast1.Hospital__c            = hospital.Id;
                ast1.Department_Class__c    = strategicDep[0].Id;
                ast1.AccountId              = dep.Id;
                ast1.Product2Id             = prd1.Id;
                ast1.SerialNumber           = 'SerialNumber1';
                ast1.Guarantee_period_for_products__c = Date.today();
                ast1.InstallDate                      = Date.today();
                
                Asset ast2 = new Asset();
                ast2.Name                   = '保有設備2';
                ast2.Hospital__c            = hospital.Id;
                ast2.Department_Class__c    = strategicDep[0].Id;
                ast2.AccountId              = dep.Id;
                ast2.Product2Id             = prd1.Id;
                ast2.SerialNumber           = 'SerialNumber2';
                ast2.Guarantee_period_for_products__c = Date.today();
                ast2.InstallDate                      = Date.today();
                
                insert new Asset[] {ast1,ast2};
                
                Maintenance_Contract_Asset__c mca = new Maintenance_Contract_Asset__c();
                mca.Asset__c = ast1.Id;
                mca.Maintenance_Contract__c = contract.id;
                
                Maintenance_Contract_Asset__c mca2 = new Maintenance_Contract_Asset__c();
                mca2.Asset__c = ast2.Id;
                mca2.Maintenance_Contract__c = contract2.id;
                
                insert new Maintenance_Contract_Asset__c[] {mca, mca2};
                
                Repair__c repair1 = new Repair__c();
                repair1.Service_Repair_No__c = 'repair1';
                repair1.Hospital__c            = hospital.Id;
                repair1.Department_Class__c    = strategicDep[0].Id;
                repair1.Account__c             = dep.Id;
                repair1.Delivered_Product__c = ast1.Id;
                repair1.Repair_List_Price__c = 100;
                repair1.Agreed_Date__c = Date.today();
                repair1.Failure_Occurrence_Date__c = Date.today().addDays(-2);
                repair1.Maintenance_Contract__c = contract.Id;
                repair1.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair1.Final_complete_day__c = Date.today().addDays(-1);
                
                Repair__c repair2 = new Repair__c();
                repair2.Service_Repair_No__c = 'repair2';
                repair2.Hospital__c            = hospital.Id;
                repair2.Department_Class__c    = strategicDep[0].Id;
                repair2.Account__c             = dep.Id;
                repair2.Delivered_Product__c = ast1.Id;
                repair2.Repair_List_Price__c = 200;
                repair2.Agreed_Date__c = Date.today();
                repair2.Failure_Occurrence_Date__c = Date.today().addDays(-3);
                repair2.Maintenance_Contract__c = contract.Id;
                repair2.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair2.Final_complete_day__c = Date.today().addDays(-1);
                
                Repair__c repair3 = new Repair__c();
                repair3.Service_Repair_No__c = 'repair3';
                repair3.Hospital__c            = hospital.Id;
                repair3.Department_Class__c    = strategicDep[0].Id;
                repair3.Account__c             = dep.Id;
                repair3.Delivered_Product__c = ast1.Id;
                repair3.Agreed_Date__c = Date.today();
                repair3.Repair_List_Price__c = 300;
                repair3.Failure_Occurrence_Date__c = Date.today().addDays(-1);
                repair3.Maintenance_Contract__c = contract.Id;
                repair3.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair3.Final_complete_day__c = Date.today().addDays(-1);
                
                Repair__c repair4 = new Repair__c();
                repair4.Service_Repair_No__c = 'repair4';
                repair4.Hospital__c            = hospital.Id;
                repair4.Department_Class__c    = strategicDep[0].Id;
                repair4.Account__c             = dep.Id;
                repair4.Delivered_Product__c = ast1.Id;
                repair4.Repair_List_Price__c = 400;
                repair4.Agreed_Date__c = Date.today();
                repair4.Failure_Occurrence_Date__c = Date.today().addDays( 1);
                repair4.Maintenance_Contract__c = contract.Id;
                repair4.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair4.Final_complete_day__c = Date.today().addDays(-1);
                
                Repair__c repair5 = new Repair__c();
                repair5.Service_Repair_No__c = 'repair5';
                repair5.Hospital__c            = hospital.Id;
                repair5.Department_Class__c    = strategicDep[0].Id;
                repair5.Account__c             = dep.Id;
                repair5.Delivered_Product__c = ast1.Id;
                repair5.Repair_List_Price__c = 500;
                repair5.Agreed_Date__c = Date.today();
                repair5.Failure_Occurrence_Date__c = Date.today().addDays(-4);
                repair5.Maintenance_Contract__c = contract.Id;
                repair5.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair5.Final_complete_day__c = Date.today().addDays(-1);
                
                Repair__c repair6 = new Repair__c();
                repair6.Service_Repair_No__c = 'repair6';
                repair6.Hospital__c            = hospital.Id;
                repair6.Department_Class__c    = strategicDep[0].Id;
                repair6.Account__c             = dep.Id;
                repair6.Delivered_Product__c = ast2.Id;
                repair6.Repair_List_Price__c = 111;
                repair6.Agreed_Date__c = Date.today();
                repair6.Failure_Occurrence_Date__c = Date.today().addDays(-2);
                repair6.Maintenance_Contract__c = contract2.Id;
                repair6.Service_contract_judege_day__c = Date.today().addDays(-2);
                repair6.Final_complete_day__c = Date.today().addDays(-1);
                
                insert new Repair__c[] {repair1,repair2,repair3,repair4,repair5, repair6};

                Maintenance_Contract__c[] mc3 = [select id from Maintenance_Contract__c];
                 List<ID> ids  = new List<ID>();
                for (Maintenance_Contract__c mc : mc3) {
                        if (ids.size()<1) {
                                ids.add(mc.id);
                        }  
                }
                
                
                System.Test.StartTest();
                Id execBTId = Database.executeBatch(new RollupToMaintenanceContractBatch(ids), 5);
                System.Test.StopTest();
    }
}