@isTest
private class RepairQuoteTriggerTest {

    private static Repair__c rpr { get; set; }
    private static Repair_Quotation__c rq { get; set; }

    private static void init() {
        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         = 'NFM104TestCompany';
        upsert company;
        // Account section = [Select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where ParentId = :company.Id and RecordTypeId = :rectSct[0].Id];
        Account section = [Select Management_Code__c,  Name, Id 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  = 'NFM104TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;

        // company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :company.Id];
        // depart = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id = :depart.Id];
        company = [select Management_Code__c,  Name, Id from Account where Id = :company.Id];
        depart = [select Management_Code__c,  Name, Id from Account where Id = :depart.Id];

        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'NFM104Prd1';
        prd1.ProductCode            = 'NFM104Prd1';
        prd1.Repair_Product_Code__c = 'NFM104Prd1_RP';
        prd1.Name                   = 'NFM104Prd1';
        prd1.Manual_Entry__c        = false;
        insert prd1;

        Asset ast = new Asset();
        ast.Name                             = 'NFM104Ast1';
        ast.AccountId                        = depart.Id;
        ast.Department_Class__c              = section.Id;
        ast.Hospital__c                      = company.Id;
        ast.Product2Id                       = prd1.Id;
        ast.SerialNumber                     = 'NFM104SerialNumber';
        ast.Guarantee_period_for_products__c = Date.today();
        ast.InstallDate                      = Date.today();
        insert ast;

        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];

        User us = new User(Test_staff__c = true, LastName = 'hp', Alias = 'hp', Work_Location__c = '北京', 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);
        us.Job_Category__c = '销售服务';
        insert us;

        rpr = new Repair__c();
        rpr.SAPRepairNo__c   = 'NFM104Rpr1';
        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.Status__c              = '1.受理完毕';
        rpr.Incharge_Staff__c = us.Id;
        rpr.Repair_Detail__c = 'test';
        //rpr.Exc_work_location__c = true;
        //rpr.On_site_repair__c = '直送SORC修理';
        rpr.On_site_repair__c = 'RC修理';
        rpr.work_location_select__c = '杭州办事处';
        rpr.AWS_Data_Id__c='5664120';
        insert rpr;

        rq = new Repair_Quotation__c();
        rq.Name              = 'testNFM104';
        rq.Repair__c       = rpr.Id;
        rq.Repair_quotation_status__c = '减价申请填写完毕';
        rq.Request_amount_after_discount__c = 5000;
        rq.Background_Description__c = '测试';
        rq.Discount_reason__c = '减价申请';

    }
    
    @isTest 
    static void test_isInsert() {
        init();
        insert rq;
    }
    
    @isTest 
    static void test_isUpdate() {
        init();
        rq.Repair_quotation_status__c = '';
        rq.Discount_request_approval_date__c = Date.today() - 100;
        rq.Approvering_Date__c = Date.today() - 100;
        insert rq;

        rq.Repair_quotation_status__c = '减价申请填写完毕';
        rq.Discount_request_approval_date__c = Date.today();
        rq.Approvering_Date__c = Date.today();
        update rq;
    }
    //20200106 add start
    @isTest 
    static void test_isUpdate1() {
        init();
        rq.CutPriceStatus_Service__c='草案中'; 
        rq.CutPrice_Reason_Service__c='99';
        rq.Expect_CutPrice__c = 800;
        rq.Background_Description__c = 'ceshi';
        insert rq;
        rq.CutPriceStatus_Service__c='已提交';
        rq.ServiceCutPriceApplyDate__c =  Date.today();
        //update rq;

        Boolean jl = String.isBlank(rq.ServiceManager__c);
        Boolean bz = String.isBlank(rq.BuchangApprovalServiceManager__c);
        Boolean zj = String.isBlank(rq.ZongjianApprovalServiceManager__c);
        System.assertEquals( true,jl);
        System.assertEquals( true,bz);
        System.assertEquals( true,zj);
    }
    //20200106 add end

    //20220929 add start
    @isTest 
    static void test_isUpdate2() {
        init();


        rq.Rental_Apply_Discount_Status__c='草案中'; 
        rq.CutPrice_Reason_Service__c='99';
        rq.Loaner_repair__c = 8000;
        rq.Loaner_Discount_Price__c = 8000;
        rq.Expect_CutPrice__c = 800;
        rq.Background_Description__c = 'ceshi';
        rq.Rental_Apply_Discount_Reason__c = '备品修理（40）';
        rq.RentalApplyDiscountApplyPerson__c = UserInfo.getUserId();
        insert rq;
        rq.Rental_Apply_Discount_Status__c='批准';
        rq.ServiceCutPriceApplyDate__c =  Date.today();
        
        try {
            update rq;
        } catch (DmlException e) {
            System.debug('The following exception has occurred: ' + e.getMessage());
        }
        
    }

    @isTest 
    static void test_isUpdate3() {
        init();
        rpr.On_site_repair__c = '直送OGZ修理';
        update rpr;

        rq.Rental_Apply_Discount_Status__c='草案中'; 
        rq.CutPrice_Reason_Service__c='99';
        rq.Loaner_repair__c = 8000;
        rq.Loaner_Discount_Price__c = 8000;
        rq.Expect_CutPrice__c = 800;
        rq.Background_Description__c = 'ceshi';
        rq.Rental_Apply_Discount_Reason__c = '备品修理（40）';
        rq.RentalApplyDiscountApplyPerson__c = UserInfo.getUserId();
        insert rq;
        rq.Rental_Apply_Discount_Status__c='批准';
        rq.ServiceCutPriceApplyDate__c =  Date.today();
        
        try {
            update rq;
        } catch (DmlException e) {
            System.debug('The following exception has occurred: ' + e.getMessage());
        }
        
    }

    @isTest 
    static void test_isUpdate4() {
        init();
        rpr.On_site_repair__c = '直送SORC修理';
        update rpr;

        rq.Rental_Apply_Discount_Status__c='草案中'; 
        rq.CutPrice_Reason_Service__c='99';
        rq.Loaner_repair__c = 8000;
        rq.Loaner_Discount_Price__c = 8000;
        rq.Expect_CutPrice__c = 800;
        rq.Background_Description__c = 'ceshi';
        rq.Rental_Apply_Discount_Reason__c = '备品修理（40）';
        rq.RentalApplyDiscountApplyPerson__c = UserInfo.getUserId();
        insert rq;
        rq.Rental_Apply_Discount_Status__c='批准';
        rq.ServiceCutPriceApplyDate__c =  Date.today();
        
        try {
            update rq;
        } catch (DmlException e) {
            System.debug('The following exception has occurred: ' + e.getMessage());
        }
        
    }
    //20220929 add end

    
}