@isTest
private class AmountCalculationFMTest {
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;

    static testMethod void testMethod1() {
            // 病院を作る
        Account hospital = new Account();
        // RecordTypeId = '01210000000QemG'
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        // hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and Id='01210000000QemG'].id;
        hospital.Name = 'hospitalabc';
        hospital.Is_Active__c = '有効';
        hospital.Attribute_Type__c = '卫生部';
        hospital.Speciality_Type__c = '综合医院';
        hospital.Grade__c = '一级';
        hospital.IF_Coverage_Target_HP__c = '1';
        hospital.OCM_Category__c = 'SLTV';
        hospital.Is_Medical__c = '医疗机构';
        hospital.Town__c = '东京';
        Oly_TriggerHandler.bypass('NFM701ControllerHandler');
        Oly_TriggerHandler.bypass('UpdateContractAimAmountHandler');
        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;
            
            // 製品を作る
            Product2 productA = new Product2( Name='テスト商品', Maintenance_Price_Year__c = 12000, Manual_Entry__c = false,Intra_Trade_Gurantee_RMB_2__c = 6500.00 ,
                            Intra_Trade_Gurantee_RMB_Date2__c = Date.toDay().addMonths(-1),
                            Intra_Trade_Gurantee_RMB_End_Date2__c = Date.toDay().addMonths(24));
            insert productA;

            
            // 価格表エントリを作成する        
            PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=productA.Id);
            entry.UnitPrice = 0;
            entry.IsActive = true;
            entry.UseStandardPrice = false;
            entry.CurrencyIsoCode = 'CNY';
            entry.Product2Id = productA.Id;
            insert entry;
            
            // 納入機器を作成する
            // Asset asset01 = createAsset('asset01', dep.Id, strategicDep[0].Id, hospital.Id, '00001', productA.Id,'主机');
            Asset asset01 = new Asset(Asset_Owner__c ='病院資産');
        		asset01.RecordTypeId = System.Label.Asset_RecordType;
        		asset01.SerialNumber = 'ass01';
        		asset01.Name = 'ass01';
        		asset01.AccountId = dep.Id;
        		asset01.Guarantee_period_for_products__c = Date.today();
        		asset01.Department_Class__c = strategicDep[0].Id;
        		asset01.Hospital__c = hospital.Id;
        		asset01.Product2Id = productA.Id;
        		asset01.Quantity = 1;
        // asset01.Asset_Year__c = Date.today().addYears(-1);
        
        		asset01.Status = '使用中';
        		asset01.Manage_type__c = '个体管理';
        		asset01.Loaner_accsessary__c = false;
        		asset01.Product_category__c = 'GI';
        		asset01.Delete_Flag__c = false;
        		asset01.Freeze_sign__c = false;
        		asset01.Out_of_wh__c = 0;
        		asset01.Salesdepartment__c = '1.华北营业本部';
        		asset01.Internal_asset_location__c = '北京 备品中心';
        		asset01.Product_category__c = 'GI';
        		asset01.Equipment_Type__c = '产品试用';
        		asset01.SalesProvince__c = '北京';
        		asset01.WH_location__c = '货架号1';
        		asset01.Asset_loaner_category__c = '固定资产';
        		asset01.CompanyOfEquipment__c = '123';
     		    asset01.Internal_Asset_number__c = '123';
        		asset01.InstallDate =Date.newInstance(2021, 5, 1);
        		asset01.Maintenance_Price_Year__c = 999;
        	insert asset01;
            // 维修合同を作成する
            Maintenance_Contract__c contract = new Maintenance_Contract__c();
            contract.Name = 'tect contract';
            contract.Hospital__c = hospital.Id;
            contract.Department_Class__c = strategicDep[0].Id;
            contract.Department__c = dep.Id;
            insert contract;
           
            
            // 维修合同报价を作成する
            Maintenance_Contract_Estimate__c contactEsti1 = new Maintenance_Contract_Estimate__c(
                Name = 'contract estimate 1',
                Contract_Esti_Start_Date__c = Date.today(),
                Contract_Range__c = 2,
                Maintenance_Contract__c = contract.Id
            );
            Maintenance_Contract_Estimate__c contactEsti2 = new Maintenance_Contract_Estimate__c(
                Name = 'contract estimate 2',
                Contract_Esti_Start_Date__c = Date.today().addMonths(6),
                Contract_Range__c = 3,
                Maintenance_Contract__c = contract.Id
            );
            
            insert new Maintenance_Contract_Estimate__c[] {contactEsti1, contactEsti2};

            Maintenance_Contract_Asset__c mca1 = new Maintenance_Contract_Asset__c();
            mca1.Asset__c = asset01.Id;
            mca1.Estimate_List_Price__c = 100;
            mca1.Maintenance_Contract__c = contract.Id;
            
            Maintenance_Contract_Asset__c mca2 = new Maintenance_Contract_Asset__c();
            mca2.Asset__c = asset01.Id;
            mca2.Estimate_List_Price__c = 100;
            mca2.Maintenance_Contract__c = contract.Id;
            
            insert new Maintenance_Contract_Asset__c[] {mca1, mca2};


            productA.Intra_Trade_Gurantee_RMB_Date2__c = Date.toDay().addMonths(-2);
            update productA;
                    
    }

}