@Istest
public class LexAllReceivedFseControllerTest {
    static void makeCalendar() {
        Date sDate = Date.today().addMonths(-6);
        Date eDate = Date.today().addMonths(6);
        Integer cnt = sDate.daysBetween(eDate);
        List<OlympusCalendar__c> ocList = new List<OlympusCalendar__c>();
        for (Integer i=0; i<cnt; i++) {
            Date d = sDate.addDays(i);
            OlympusCalendar__c oc = new OlympusCalendar__c(Date__c = d);
            ocList.add(oc);
        }
        insert ocList;
    }
    static void setCalendar(Date hDate) {
    	OlympusCalendar__c oc = [select Id, Date__c, IsHoliday__c,Holiday_Days__c
                  from OlympusCalendar__c
                 where Date__c = :hDate];
        oc.IsHoliday__c = true;    
    	oc.Holiday_Days__c = 7;
    }


    static Date getWD_now(Date d) {
        List<OlympusCalendar__c> workday = [
                select Id, Date__c, IsWorkDay__c
                  from OlympusCalendar__c
                 where Date__c >= :d
                   and IsWorkDay__c = 1
                 order by Date__c
                 limit 1];
        Date selectDate = workday[0].Date__c;
        return selectDate;
    }
    @isTest
    static void test_method_twelve() {
        TestDataUtility.CreatePIPolicyConfiguration();
        Oly_TriggerHandler.bypass('RentalApplyTriggerHandler');
        Oly_TriggerHandler.bypass('RentalApplyEquipmentSetHandler');
        Oly_TriggerHandler.bypass('RentalHpDeptUpdHandler');
        Oly_TriggerHandler.bypass('RepairOrderHandler');
        Oly_TriggerHandler.bypass('RepairHandler');
        Oly_TriggerHandler.bypass('RentalApplyShareHandler');
        FixtureUtil.SkipTrigger=true;
        StaticParameter.EscapeNFM001AgencyContractTrigger = true;
        StaticParameter.EscapeNFM001Trigger = true;
        Oly_TriggerHandler.bypass(ContactTriggerHandler.class.getName());
        Oly_TriggerHandler.bypass(AgencyHospitalHandler.class.getName());
        makeCalendar();
        Rental_Apply__c raObj = new Rental_Apply__c();
        // 保有设备
        Fixture_Set_Detail__c fsdObjA1 = new Fixture_Set_Detail__c();
        Rental_Apply_Equipment_Set__c raesObj = new Rental_Apply_Equipment_Set__c();
        Rental_Apply_Equipment_Set__c raesObj1 = new Rental_Apply_Equipment_Set__c();

        Asset asset1 = new Asset(Asset_Owner__c = 'Olympus');
        Fixture_OneToOne_Link__c foLink1 = new Fixture_OneToOne_Link__c();
        Fixture_OneToOne_Link__c foLink2 = new Fixture_OneToOne_Link__c();
        // MIXED_DML_OPERATION, DML operation on setup object is not permitted Error
        System.runAs(new User(Id = System.Label.ByPassTrigger)) {
            StaticParameter.EscapeNFM001AgencyContractTrigger = true;
            StaticParameter.EscapeNFM001Trigger = true;
            Oly_TriggerHandler.bypass(ContactTriggerHandler.class.getName());
            Oly_TriggerHandler.bypass(AgencyHospitalHandler.class.getName());
            // システム管理者
            User user = new User(Test_staff__c = true);
            user.LastName = '_サンブリッジ';
            user.FirstName = 'う';
            user.Alias = 'う';
            user.Email = 'olympusTest03@sunbridge.com';
            user.Username = 'olympusTest03@sunbridge.com';
            user.CommunityNickname = 'う';
            user.IsActive = true;
            user.EmailEncodingKey = 'ISO-2022-JP';
            user.TimeZoneSidKey = 'Asia/Tokyo';
            user.LocaleSidKey = 'ja_JP';
            user.LanguageLocaleKey = 'ja';
            user.ProfileId = System.Label.ProfileId_SystemAdmin;
            user.Province__c = '北京';
            user.Dept__c = '医疗华北营业本部';
            user.Use_Start_Date__c = Date.today().addMonths(-6);
            insert user;

            // 省
            Address_Level__c al = new Address_Level__c();
            al.Name = '東京';
            al.Level1_Code__c = 'CN-99';
            al.Level1_Sys_No__c = '999999';
            insert al;
            // 市
            Address_Level2__c al2 = new Address_Level2__c();
            al2.Level1_Code__c = 'CN-99';
            al2.Level1_Sys_No__c = '999999';
            al2.Level1_Name__c = '東京';
            al2.Name = '渋谷区';
            al2.Level2_Code__c = 'CN-9999';
            al2.Level2_Sys_No__c = '9999999';
            al2.Address_Level__c = al.id;
            insert al2;

            // 病院を作る
            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.Is_Active__c = '有効';
            hospital.Attribute_Type__c = '卫生部';
            hospital.Speciality_Type__c = '综合医院';
            hospital.Grade__c = '一级';
            hospital.OCM_Category__c = 'SLTV';
            hospital.Is_Medical__c = '医疗机构';
            hospital.State_Master__c = al.id;
            hospital.City_Master__c = al2.id;
            hospital.Town__c = '东京';
            insert hospital;

            StaticParameter.EscapeAccountTrigger = true;
            // 戦略科室を得る
            Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_GI'];
            // 診療科を作る
            FixtureUtil.isRepairRenewal=true;
            Account dep = new Account();
            dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_GI'].id;
            dep.Name = 'test dep1';
            dep.AgentCode_Ext__c = '9999999';
            dep.ParentId = strategicDep[0].Id;
            dep.Department_Class__c = strategicDep[0].Id;
            dep.Hospital__c = hospital.Id;
            dep.ParentId = strategicDep[0].Id;
            insert dep;


            Contact contact2 = new Contact();
            contact2.AccountId = dep.Id;
            contact2.FirstName = '責任者';
            contact2.LastName = 'test1经销商';
            insert contact2;

            // 产品
            Product2 pro1 = new Product2(Name='name01',IsActive=true,Family='GI',
                    Fixture_Model_No__c='n01',Serial_Lot_No__c='S/N tracing',
                    Fixture_Model_No_T__c = 'n01', Asset_Model_No__c = 'Pro1',
                    ProductCode_Ext__c='pc01',Manual_Entry__c=false);
            Product2 pro2 = new Product2(Name='name02',IsActive=true,Family='GI',
                    Fixture_Model_No__c='n02',Serial_Lot_No__c='Lot tracing',
                    Fixture_Model_No_T__c = 'n02', Asset_Model_No__c = 'Pro2',
                    ProductCode_Ext__c='pc02',Manual_Entry__c=false);
            Product2 pro3 = new Product2(Name='name03',IsActive=true,Family='GI',
                    Fixture_Model_No__c='n03',Serial_Lot_No__c='Lot tracing',
                    Fixture_Model_No_T__c = 'n03', Asset_Model_No__c = 'Pro3',
                    ProductCode_Ext__c='pc03',Manual_Entry__c=false);
            Product2 pro4 = new Product2(Name='name04',IsActive=true,Family='GI',
                    Fixture_Model_No__c='n04',Serial_Lot_No__c='Lot tracing',
                    Fixture_Model_No_T__c = 'n04', Asset_Model_No__c = 'Pro4',
                    ProductCode_Ext__c='pc04',Manual_Entry__c=false);
            insert new Product2[] {pro1, pro2, pro3, pro4};

            
            RecordType rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity' ];
            Opportunity opp1 = new Opportunity(
            Name='testOpp1',
            StageName='引合',
            CloseDate=Date.today(),
            AccountId=dep.Id,
            Competitor__c ='A',
            Click_Close_Date__c = null,
            RecordType = rectOpp
            );
            insert opp1;

            Statu_Achievements__c Sac = new Statu_Achievements__c(
                name = 'zhucan_one',
                Opportunity__c = opp1.id,
                DeliveryDate__c = Date.today(),
                ContractNO__c = 'ContractNO1',
                ContractAmount__c = 1000
            );
            insert Sac;

            Asset asset = new Asset();
            asset.Name = 'テスト機器';
            asset.AccountId = dep.Id;
            asset.Department_Class__c = strategicDep[0].Id;
            asset.Hospital__c = hospital.Id;
            asset.SerialNumber = 'testserial';
            insert asset;

            Repair__c repair01 = new Repair__c();
            repair01.Account__c = dep.Id;
            repair01.Department_Class__c = strategicDep[0].Id;
            repair01.Hospital__c = hospital.Id;
            repair01.Delivered_Product__c = asset.Id;
            repair01.SERVICE_CONTRACT_JUDEGE_DAY__C = Date.today().addDays(0);  // 维修合同判断日がサービス契約の中間辺りの日付
            repair01.FSE_Work_Location__c = '上海';
            repair01.IfCheckFixture__c = true;
            repair01.Repair_Ordered_Date__c = Date.today();
            repair01.Agreed_Date__c = Date.today().addDays(365);
            repair01.Repair_Estimated_Date__c = Date.today();
            FixtureUtil.SkipTrigger=true;
            insert repair01;
            Repair__c aa = [select id,Repair_Quotation_date__c,Repair_Estimated_date_formula__c from Repair__c where Id =:repair01.Id];
            system.debug('Repair__c+++'+aa);
            Rental_Apply__c raObj1 = new Rental_Apply__c();
            raObj1.Name = 'testra1';
            raObj1.OwnerId = user.Id;
            raObj1.Product_category__c = 'GI';
            raObj1.Demo_purpose1__c = '产品试用';
            raObj1.demo_purpose2__c = '其他';
            raObj1.direct_send__c = '医疗机构';
            raObj1.Loaner_received_staff__c = '王五';
            raObj1.Loaner_received_staff_phone__c = '110';
            raObj1.direct_shippment_address__c = '北京市';
            raObj1.Hospital__c = hospital.Id;
            raObj1.Strategic_dept__c = strategicDep[0].Id;
            raObj1.Account__c = dep.Id;
            raObj1.Request_shipping_day__c = Date.toDay();
            //raObj.Request_return_day__c = Date.toDay();
            raObj1.Phone_number__c = '12345678901';
            raObj1.Loaner_medical_Staff__c = contact2.Id;
            raObj1.DataMigration_Flag__c = false;
            FixtureUtil.isRepairRenewal = true;
            insert raObj1;
            //备品借出申请
            raObj.Name = 'testra';
            raObj.OwnerId = user.Id;
            raObj.Product_category__c = 'GI';
            raObj.Demo_purpose1__c = '产品试用';
            raObj.demo_purpose2__c = '';
            raObj.direct_send__c = '医疗机构';
            raObj.Loaner_received_staff__c = '王五';
            raObj.Loaner_received_staff_phone__c = '110';
            raObj.direct_shippment_address__c = '北京市';
            raObj.Hospital__c = hospital.Id;
            raObj.Strategic_dept__c = strategicDep[0].Id;
            raObj.Account__c = dep.Id;
            raObj.Request_shipping_day__c = Date.toDay();
            raObj.Hope_Lonaer_date_Num__c = 0;
            //raObj.Request_return_day__c = Date.toDay();
            raObj.Phone_number__c = '1234567890';
            raObj.Loaner_medical_Staff__c = contact2.Id;
            raObj.DataMigration_Flag__c = false;
            raObj.Old_Rental_Apply__c = raObj1.Id;
            raObj.Follow_UP_Opp__c = opp1.Id;
            raObj.Statu_Achievements__c = Sac.Id;
            raObj.Status__c = '草案中';
            raObj.Routine_Check_Status__c = 'OK';
            raObj.Repair__c = repair01.Id;
            raObj.RA_Status_Text__c = '已出库';
            raObj.HP_received_sign_text__c= '测试';
            // PI_Policy_Configuration__c config = new PI_Policy_Configuration__c();
            // config.Sobject_Type__c  = 'Rental_Apply__c';
            // config.Sobject_Name__c = 'rental_apply';
            // insert config;
            // PI_Field_Policy_Detail__c   pidetails = new PI_Field_Policy_Detail__c();
            // pidetails.PI_Policy_Configuration_Name__c = 'Rental_Apply__c';

            FixtureUtil.isRepairRenewal = true;
            insert raObj;

            // 保有设备
            asset1.RecordTypeId = System.Label.Asset_RecordType;
            asset1.SerialNumber = 'asset1';
            asset1.Name = 'asset1';
            asset1.AccountId = dep.Id;
            asset1.Department_Class__c = strategicDep[0].Id;
            asset1.Hospital__c = hospital.Id;
            asset1.Product2Id = pro1.Id;
            asset1.Quantity = 1;
            asset1.Status = '不明';
            asset1.Manage_type__c = '个体管理';
            asset1.Loaner_accsessary__c = false;
            asset1.Out_of_wh__c = 0;
            asset1.Salesdepartment__c = '1.华北营业本部';
            asset1.Internal_asset_location__c = '北京 备品中心';
            asset1.Product_category__c = 'GI';
            asset1.Equipment_Type__c = '产品试用';
            asset1.SalesProvince__c = '北京';
            asset1.CompanyOfEquipment__c = '北京';
            asset1.Internal_Asset_number__c = '0001';
            asset1.WH_location__c = '货架号1';
            insert new Asset[] {asset1};


            // 备品配套
            Fixture_Set__c fsObj1 = new Fixture_Set__c();
            fsObj1.Name = 'set1';
            fsObj1.Fixture_Set_Body_Model_No__c = 'modelNo1';
            fsObj1.Loaner_name__c = 'name1';
            insert fsObj1;

            // 备品配套明细
            fsdObjA1.Name = '备品配套明细名1';
            fsdObjA1.Name_CHN_Created__c = '中文名称1';
            fsdObjA1.Product2__c = pro1.Id;
            fsdObjA1.Fixture_Set__c = fsObj1.Id;
            fsdObjA1.Is_Body__c = true;
            fsdObjA1.Is_Optional__c = false;
            fsdObjA1.UniqueKey__c = fsObj1.Id + ':' + pro1.Id;
            fsdObjA1.SortInt__c = 1;
            fsdObjA1.Quantity__c = 1;
           

            insert new Fixture_Set_Detail__c[] {fsdObjA1};

            // 借出备品配套一览
            raesObj.Rental_Apply__c = raObj.Id;
            raesObj.Fixture_Set__c = fsObj1.Id;
            raesObj.Cancel_Select__c = false;
            raesObj.Rental_Start_Date__c = Date.toDay();
            raesObj.Rental_End_Date__c = Date.toDay();
            raesObj.IndexFromUniqueKey__c = 1;
            raesObj.UniqueKey__c = '1:'+ fsObj1.Id + ':1';
            raesObj.Repair_Status_Text__c = '1.受理完毕';
            raesObj.Received_Confirm__c = 'NG';
            raesObj.Received_ng_detail__c = 'test';
            raesObj.Bollow_Date_Add_10_WD__c = Date.toDay().addDays(1);
            raesObj.Asset_return_time__c = Date.toDay().addDays(-30);

            //备品借出历史
            
            // insert new Rental_Apply_Equipment_Set__c[]{raesObj, raesObj1};
            //备品借出历史
           
            // insert raesObj1;
            insert raesObj;

        }
        Rental_Apply_Equipment_Set_Detail__c raesdObj1 = new Rental_Apply_Equipment_Set_Detail__c();
        System.runAs(new User(Id = Userinfo.getUserId())) {
            FixtureUtil.isRepairRenewal = true;
            // 借出备品配套一览明细
            raesdObj1.Rental_Apply__c = raObj.Id;
            raesdObj1.Fixture_Set_Detail__c = fsdObjA1.Id;
            raesdObj1.Queue_Number__c = 1;
            raesdObj1.Is_Body__c = true;
            raesdObj1.Rental_Apply_Equipment_Set__c = raesObj.Id;
            raesdObj1.IndexFromUniqueKey__c = 1;
            raesdObj1.UniqueKey__c = '1:'+ raesObj.Id + ':' + fsdObjA1.Id + ':1';
            raesdObj1.FSD_OneToOneAccessory_Cnt__c = 2;
            raesdObj1.FSD_Is_Optional__c = false;
            raesdObj1.FSD_Is_OneToOne__c = false;
            raesdObj1.ApplyPersonAppended__c = false;
            raesdObj1.FSD_Fixture_Model_No__c = 'n01';
            raesdObj1.Fixture_Model_No_text__c = 'n01';
            raesdObj1.Salesdepartment_before__c = '1.华北营业本部';
            raesdObj1.Internal_asset_location_before__c = '北京 备品中心';
            raesdObj1.Product_category_text__c = 'GI';
            raesdObj1.Equipment_Type_text__c = '产品试用';
            raesdObj1.Cancel_Select__c = false;
            raesdObj1.AgencyTempCancel__c = false;
            

            insert new Rental_Apply_Equipment_Set_Detail__c[] {raesdObj1};
        }
        System.runAs(new User(Id = Userinfo.getUserId())) {
            FixtureUtil.isRepairRenewal = true;
            // 申请单
            raObj.Status__c = '已出库';
            raObj.Status_Text__c = '已出库';
            raObj.NotWatch_RA_Status__c = true;
            raObj.HP_received_sign_day__c = Date.toDay();
            raObj.HP_received_sign_rich__c = 'OK';
            raObj.AssetManageConfirm__c = true;
            raObj.Request_demo_time__c = Date.newInstance(2019, 7, 1);
            update raObj;

            // 申请者收货操作
            // raesObj.Loaner_received_time__c = Datetime.now();
            raesObj.Shippment_loaner_time__c = Datetime.now();
            raesObj.Demo_purpose2_T__c = '故障排查';
            raesObj.First_RAESD__c = raesdObj1.Id;
            raesObj.Cancel_Select__c = false;
            update raesObj;

            FixtureDeliverySlip__c fdsObj = new FixtureDeliverySlip__c();
            fdsObj.Name = '00001';
            fdsObj.DeliveryCompany__c = '利讯';
            fdsObj.Distributor_method__c = '陆运';
            fdsObj.DeliveryType__c = '发货';
            fdsObj.Shippment_loaner_time__c = System.now();
            insert fdsObj;

            FixtureDeliverySlip__c fdsObj2 = new FixtureDeliverySlip__c();
            fdsObj2.Name = '00002';
            fdsObj2.DeliveryCompany__c = '利讯';
            fdsObj2.Distributor_method__c = '陆运';
            fdsObj2.DeliveryType__c = '发货';
            fdsObj2.Shippment_loaner_time__c = System.now();
            insert fdsObj2;

            // 出库前点检操作
            raesdObj1.Asset__c = asset1.Id;
            raesdObj1.Loaner_accsessary__c = false;
            raesdObj1.FSD_Name_CHN__c = 'name01';
            raesdObj1.Shipment_Status_Text__c = FixtureUtil.raesdStatusMap.get(FixtureUtil.HistoryStatus.Yi_Fen_Pei.ordinal());
            raesdObj1.Shipment_request_time2__c = Date.toDay();
            raesdObj1.Shipment_request__c = true;
            raesdObj1.StockDown__c = true;
            raesdObj1.StockDown_time__c = Date.toDay();
            raesdObj1.StockDown_staff__c = Userinfo.getUserId();
            raesdObj1.Shipment_Status_Text__c = FixtureUtil.raesdStatusMap.get(FixtureUtil.HistoryStatus.Yi_Xia_Jia.ordinal());

            raesdObj1.Inspection_result__c = 'OK';
            raesdObj1.Pre_inspection_time__c = System.now();
            raesdObj1.Inspection_staff__c = Userinfo.getUserId();
            // 发货操作
            raesdObj1.DeliverySlip__c = fdsObj.Id;

           
            update new Rental_Apply_Equipment_Set_Detail__c[] {raesdObj1};
        }
        LexAllReceivedFseController.init(raObj.id);
        LexAllReceivedFseController.getRaeSet(raObj.id);
    }

   
}