@isTest
private class TransferApplyControllerTest { 
	 @isTest
    public static void testTransferApplyPDF() {
    	ControllerUtil.EscapeNFM001Trigger = true;
        Oly_TriggerHandler.bypass(ContactTriggerHandler.class.getName());
        Oly_TriggerHandler.bypass(RentalApplyTriggerHandler.class.getName());

        SS_Batch_Column_Mapping__c InventoryHeaderName = new SS_Batch_Column_Mapping__c(Name = 'Inventory_Header_Name', SS_TableName__c='Inventory_Header_Name');
        InventoryHeaderName.From_Column_001__c = '北京 备品中心';
        InventoryHeaderName.SS_Column_001__c = 'BJ';
        InventoryHeaderName.From_Column_002__c = '上海 备品中心';
        InventoryHeaderName.SS_Column_002__c = 'SH';
        InventoryHeaderName.From_Column_003__c = '广州 备品中心';
        InventoryHeaderName.SS_Column_003__c = 'GZ';
        insert new SS_Batch_Column_Mapping__c[] {InventoryHeaderName};

         SS_Batch_Column_Mapping__c AgencyInventory = new SS_Batch_Column_Mapping__c(Name = 'Agency_Inventory_Role', SS_TableName__c='Agency_Inventory_Role');
        AgencyInventory.From_Column_001__c = '北京 备品中心';
        AgencyInventory.SS_Column_001__c = '1.华北营业本部';
        AgencyInventory.From_Column_002__c = '济南';
        AgencyInventory.SS_Column_002__c = '1.华北营业本部';
        AgencyInventory.From_Column_003__c = '郑州';
        AgencyInventory.SS_Column_003__c = '3.西北营业本部';
        insert new SS_Batch_Column_Mapping__c[] {AgencyInventory};
        
        User thisUser = [Select Id from User where Id =: UserInfo.getUserId()];
        System.runAs(thisUser) {
            StaticParameter.EscapeNFM001AgencyContractTrigger = true;
            StaticParameter.EscapeNFM001Trigger = true;
            StaticParameter.EscapeAccountTrigger = true;
            ControllerUtil.EscapeNFM001Trigger = true;
            
            // 省
            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_OTH'];
            // 診療科を作る
            Account dep = new Account();
            dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
            dep.Name = 'test dep1';
            dep.AgentCode_Ext__c = System.Label.Account_Asset_FJZ;
            dep.ParentId = strategicDep[0].Id;
            dep.Department_Class__c = strategicDep[0].Id;
            dep.Hospital__c = hospital.Id;

            Account dep1 = new Account();
            dep1.recordtypeId = dep.recordtypeId;
            dep1.Name = 'test dep1';
            dep1.AgentCode_Ext__c = System.Label.Account_Asset;
            dep1.ParentId = strategicDep[0].Id;
            dep1.Department_Class__c = strategicDep[0].Id;
            dep1.Hospital__c = hospital.Id;

            insert new Account[]{dep, dep1};

            // 产品
            Product2 pro1 = new Product2(Name='CLH-250:内窥镜冷光源',IsActive=true,Family='GI',
                    Fixture_Model_No__c='CLH-250',Serial_Lot_No__c='S/N tracing',
                    Fixture_Model_No_T__c = 'CLH-250', Asset_Model_No__c = 'Pro1',
                    ProductCode_Ext__c='4604362',Manual_Entry__c=false);
            Product2 pro2 = new Product2(Name='电源线',IsActive=true,Family='GI',
                    Fixture_Model_No__c='电源线',Serial_Lot_No__c='Lot tracing',
                    Fixture_Model_No_T__c = '电源线', Asset_Model_No__c = 'Pro2',
                    ProductCode_Ext__c='BP900003',Manual_Entry__c=false);
            // Product2 pro3 = new Product2(Name='MAJ-1933:数字调光电缆',IsActive=true,Family='GI',
            //         Fixture_Model_No__c='MAJ-1933',Serial_Lot_No__c='Lot tracing',
            //         Fixture_Model_No_T__c = 'MAJ-1933', Asset_Model_No__c = 'Pro3',
            //         ProductCode_Ext__c='N3647100',Manual_Entry__c=false);
            insert new Product2[] {pro1, pro2};


             // 保有设备  单独主机
            Asset asset1 = new Asset(Asset_Owner__c = 'Olympus');
            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 = 10;
            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.Rental_Count__c = 0;
            asset1.AssetManageConfirm__c = true;
            insert asset1;

             // 保有设备  一对一主机
            Asset asset11 = new Asset(Asset_Owner__c = 'Olympus');
            asset11.RecordTypeId = System.Label.Asset_RecordType;
            asset11.SerialNumber = 'asset11';
            asset11.Name = 'asset11';
            asset11.AccountId = dep.Id;
            asset11.Department_Class__c = strategicDep[0].Id;
            asset11.Hospital__c = hospital.Id;
            asset11.Product2Id = pro1.Id;
            asset11.Quantity = 10;
            asset11.Status = '不明';
            asset11.Manage_type__c = '个体管理';
            asset11.Loaner_accsessary__c = false;
            asset11.Out_of_wh__c = 0;
            asset11.Salesdepartment__c = '1.华北营业本部';
            asset11.Internal_asset_location__c = '北京 备品中心';
            asset11.Product_category__c = 'GI';
            asset11.Equipment_Type__c = '产品试用';
            asset11.SalesProvince__c = '北京';
            asset11.CompanyOfEquipment__c = '北京';
            asset11.Internal_Asset_number__c = '0001';
            asset11.Rental_Count__c = 0;
            asset11.AssetManageConfirm__c = true;
            asset11.AssetManageConfirm__c = true;
            //一对一得附属品    
            Asset asset12 = new Asset(Asset_Owner__c = 'Olympus');
            asset12.RecordTypeId = System.Label.Asset_RecordType;
            asset12.SerialNumber = 'asset12';
            asset12.Name = 'asset12';
            asset12.AccountId = dep.Id;
            asset12.Department_Class__c = strategicDep[0].Id;
            asset12.Hospital__c = hospital.Id;
            asset12.Product2Id = pro2.Id;
            asset12.Quantity = 10;
            asset12.Status = '不明';
            asset12.Manage_type__c = '数量管理';
            asset12.Loaner_accsessary__c = true;   //附属品
            asset12.Out_of_wh__c = 0;
            asset12.Salesdepartment__c = '1.华北营业本部';
            asset12.Internal_asset_location__c = '北京 备品中心';
            asset12.Product_category__c = 'GI';
            asset12.Equipment_Type__c = '产品试用';
            asset12.SalesProvince__c = '北京';
            asset12.CompanyOfEquipment__c = '北京';
            asset12.Internal_Asset_number__c = '0002';
            asset12.Rental_Count__c = 0;
            asset12.AssetManageConfirm__c = true;

            insert new Asset[] {asset11, asset12};

            List<Asset> assetList = [select Name from Asset];
            System.assertEquals(3, assetList.size());

            // 备品一对一Link表
            Fixture_OneToOne_Link__c foLink1 = new Fixture_OneToOne_Link__c();
            foLink1.Main_Asset__c = asset11.Id;
            foLink1.Accessory_Asset__c = asset12.Id; 
            foLink1.Quantity__c = 1;

            insert new Fixture_OneToOne_Link__c[] {foLink1};

            
            FixtureDeliverySlip__c ds = new FixtureDeliverySlip__c();
            ds.Name = 'Test1';
            ds.Wh_Staff__c = UserInfo.getUserId();
            ds.DeliveryCompany__c = '利讯';
            ds.Shippment_loaner_time__c = Datetime.now();
            ds.Distributor_method__c = '陆运';
            ds.DeliveryType__c = '发货';
            ds.DeliveryCompany_SlipNo__c = 'TestNo1';
            insert ds;

            //调拨申请
            TransferApply__c raObj = new TransferApply__c();
            raObj.Name = 'testra';
            raObj.From_Location__c = '北京';
            raObj.Destination_Location__c = '北京 备品中心';
            Map<String,Schema.RecordTypeInfo>  DEVELOPERNAMEMAP  = Schema.SObjectType.TransferApply__c.getRecordTypeInfosByDeveloperName();
            raObj.RecordTypeId = DEVELOPERNAMEMAP.get('AgencyToCenter').getRecordTypeId();            
            insert raObj;

            // 调拨一览
            TransferApplySummary__c raesObj = new TransferApplySummary__c();
            raesObj.TransferApply__c = raObj.Id;
            raesObj.Cancel_Select__c = false;
            raesObj.IndexFromUniqueKey__c = 1;
            insert raesObj;
            //调拨中得数据
            TransferApplyDetail__c raesdObj3 = new TransferApplyDetail__c();
            raesdObj3.TransferApply__c = raObj.Id;
            raesdObj3.TransferApplySummary__c = raesObj.Id;
            raesdObj3.IndexFromUniqueKey__c = 1;
            raesdObj3.ApplyPersonAppended__c = false;
            raesdObj3.FSD_SortInt__c = 01;
            raesdObj3.Asset__c = asset1.Id;
            raesdObj3.StockDown__c = true;
            raesdObj3.StockDown_staff__c = UserInfo.getUserId();
            raesdObj3.StockDown_time__c = Datetime.now();
            raesdObj3.Pre_inspection_time__c = Datetime.now();
            raesdObj3.Inspection_staff__c = UserInfo.getUserId();
            raesdObj3.Inspection_result__c = 'OK';
            raesdObj3.DeliverySlip__c = ds.Id;
            raesdObj3.OneToOneAccessory__c =false;
            raesdObj3.CDS_complete__c =true;
            raesdObj3.CDS_complete_time__c = Datetime.now();
            raesdObj3.Shipment_Status_Text__c = '已出库';
            insert raesdObj3;


            //调拨申请一览明细    一对一走的数据
            //状态 下架   主机
            TransferApplyDetail__c raesdObj1 = new TransferApplyDetail__c();
            raesdObj1.TransferApply__c = raObj.Id;
            raesdObj1.TransferApplySummary__c = raesObj.Id;
            raesdObj1.IndexFromUniqueKey__c = 1;
            raesdObj1.FSD_OneToOneAccessory_Cnt__c = 1;
            raesdObj1.ApplyPersonAppended__c = false;
            raesdObj1.TransferCount__c = 1;
            raesdObj1.FSD_SortInt__c = 01;
            raesdObj1.Asset__c = asset11.Id;
            raesdObj1.Main_OneToOne__c = true;  //主机
            raesdObj1.Salesdepartment_After__c = '1.华北营业本部';
            raesdObj1.OneToOneAccessory__c = true;
            raesdObj1.StockDown__c = true;
            raesdObj1.StockDown_staff__c = UserInfo.getUserId();
            raesdObj1.StockDown_time__c = Datetime.now();
            //附属品  下架
            TransferApplyDetail__c raesdObj2 = new TransferApplyDetail__c();
            raesdObj2.TransferApply__c = raObj.Id;
            raesdObj2.TransferApplySummary__c = raesObj.Id;
            raesdObj2.IndexFromUniqueKey__c = 2;
            raesdObj2.FSD_OneToOneAccessory_Cnt__c = 1;
            raesdObj2.ApplyPersonAppended__c = false;
            raesdObj2.TransferCount__c = 1;
            raesdObj2.FSD_SortInt__c = 02;
            raesdObj2.Asset__c = asset12.Id;
            raesdObj2.OneToOneAccessory__c = true;
            raesdObj2.StockDown__c = true;
            raesdObj2.StockDown_staff__c = UserInfo.getUserId();
            raesdObj2.StockDown_time__c = Datetime.now();
            
            insert new TransferApplyDetail__c[] {raesdObj1,raesdObj2};

            // ////备品借出申请
            // Rental_Apply__c rasObj = new Rental_Apply__c();
            // rasObj.Name = 'testra';
            // rasObj.Product_category__c = 'GI';
            // rasObj.Demo_purpose1__c = '产品试用';
            // rasObj.demo_purpose2__c = FixtureUtil.raDemo_purpose2MAP.get('shiyongwuxunjia');
            // rasObj.direct_send__c = '医疗机构';
            // rasObj.Loaner_received_staff__c = '王五';
            // rasObj.Loaner_received_staff_phone__c = '110';
            // rasObj.direct_shippment_address__c = '北京市';
            // rasObj.Hospital__c = hospital.Id;
            // rasObj.Strategic_dept__c = strategicDep[0].Id;
            // rasObj.Account__c = dep.Id;
            // rasObj.Request_shipping_day__c = Date.toDay();
            // rasObj.Request_return_day__c = Date.toDay();
            // rasObj.Phone_number__c = '1234567890';
            // insert rasObj;
            // Rental_Apply_Equipment_Set__c raessObj = new Rental_Apply_Equipment_Set__c();
            // raessObj.Rental_Apply__c = rasObj.Id;
            // raessObj.Cancel_Select__c = false;
            // raessObj.Shipment_request_time__c = Date.toDay();
            // raessObj.Rental_Start_Date__c = Date.toDay();
            // raessObj.Rental_End_Date__c = Date.toDay();
            // raessObj.UniqueKey__c = rasObj.RequestNoJoinStr2__c + ':'+ null + ':'+ 1;
            // raessObj.IndexFromUniqueKey__c = 1;
            // insert raessObj;

            // // 借出备品配套一览明细
            // Rental_Apply_Equipment_Set_Detail__c raesdsObj1 = new Rental_Apply_Equipment_Set_Detail__c();
            // raesdsObj1.Rental_Apply__c = rasObj.Id;
            // raesdsObj1.Rental_Num__c = 1;
            // raesdsObj1.Queue_Number__c = null;
            // raesdsObj1.Is_Body__c = true;
            // raesdsObj1.Rental_Apply_Equipment_Set__c = raessObj.Id;
            // raesdsObj1.IndexFromUniqueKey__c = 1;
            // raesdsObj1.Salesdepartment_before__c = '1.华北营业本部';
            // raesdsObj1.Internal_asset_location_before__c = '北京 备品中心';
            // raesdsObj1.Product_category_text__c = 'GI';
            // raesdsObj1.Equipment_Type_text__c = '产品试用';
            // raesdsObj1.UniqueKey__c = '1:'+ raessObj.Id + ':' + null + ':1';
            // raesdsObj1.FSD_Fixture_Model_No__c = 'n01';
            // raesdsObj1.Fixture_Model_No_text__c = 'n01';
            // raesdsObj1.Asset__c = asset1.id;
            // raesdsObj1.StockDown__c = true;
            // raesdsObj1.OnStock_By_Cancel__c = true;
            // raesdsObj1.Inspection_result_NG__c = '废弃';
            // raesdsObj1.Inspection_result__c = 'NG';
            // raesdsObj1.Pre_NG_abord_reason__c = '无法维修废弃';
            // raesdsObj1.FSD_Is_OneToOne__c =true;
            // raesdsObj1.Fixture_OneToOne_Link_Id__c ='12435';
            
            // raesdsObj1.Lost_item_giveup__c = true;
            // raesdsObj1.Loaner_Giveup_Time__c = system.now();
            // insert raesdsObj1;
           
             
            // //盘点表头
            // Inventory_Header_Copy__c header = new Inventory_Header_Copy__c();
            // header.Inventory_Start_Date__c = Date.toDay();
            // header.UniqueKey__c = 'IH-0000';
            // header.Inventory_Status__c = '处理中';
            // header.InventoryCheck_PIC__c = [SELECT Id
            //     FROM User WHERE Dept__c = '医疗东北营业本部' and IsActive = true limit 1].id;
            // header.InventorySubmit_PIC__c = [SELECT id
            //     FROM User WHERE Dept__c = '医疗东北营业本部' and IsActive = true limit 1].id;
            // insert header;


            // Inventory_Header_Copy__c header2 = new Inventory_Header_Copy__c();
            // header2.Inventory_Start_Date__c = Date.toDay();
            // header2.UniqueKey__c = 'IH-0000';
            // header2.Inventory_Status__c = null;
            // header2.InventoryCheck_PIC__c = [SELECT Id
            //     FROM User WHERE Dept__c = '医疗东北营业本部' and IsActive = true limit 1].id;
            // header2.InventorySubmit_PIC__c = [SELECT id
            //     FROM User WHERE Dept__c = '医疗东北营业本部' and IsActive = true limit 1].id;
            // insert header2;


            // Inventory_Header__Share xmshare = new Inventory_Header__Share(
            //             RowCause = 'Manual',
            //             ParentId = header.Id,
            //             UserOrGroupId = header.InventoryCheck_PIC__c,
            //             AccessLevel = 'Edit');


            String cunFangDi = '北京 备品中心';

	        System.Test.StartTest();
	       	TransferApplyController.transferApplyPDF(raesObj.Id);
	       	TransferApplyController.applyPermission();
	        Test.stopTest();
    	}
    }
}