@isTest private class InventoryReportControllerTest { // 创建 Asset & 盘点表头 & 盘点明细 数据 static void setupTestData() { 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}; // 病院を作る 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; // 戦略科室を得る 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 dep'; dep.AgentCode_Ext__c = '9999998'; dep.ParentId = strategicDep[0].Id; dep.Department_Class__c = strategicDep[0].Id; dep.Hospital__c = hospital.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', 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', 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', ProductCode_Ext__c='pc03',Manual_Entry__c=false); insert new Product2[] {pro1, pro2, pro3}; //---------------------------主体 两个附属品(个体管理 数量管理) // 保有设备A (主体) Asset assetA1 = new Asset(Asset_Owner__c = 'Olympus'); assetA1.RecordTypeId = System.Label.Asset_RecordType; assetA1.SerialNumber = 'ass01'; assetA1.Name = 'ass01'; assetA1.AccountId = dep.Id; assetA1.Department_Class__c = strategicDep[0].Id; assetA1.Hospital__c = hospital.Id; assetA1.Product2Id = pro1.Id; assetA1.Quantity = 1; assetA1.Status = '有库存'; assetA1.Manage_type__c = '个体管理'; assetA1.Loaner_accsessary__c = false; assetA1.Product_category__c = 'GI'; assetA1.Delete_Flag__c = false; assetA1.Freeze_sign__c = false; assetA1.Out_of_wh__c = 0; assetA1.Salesdepartment__c = '1.华北营业本部'; assetA1.Internal_asset_location__c = '北京 备品中心'; assetA1.Product_category__c = 'GI'; assetA1.Equipment_Type__c = '产品试用'; assetA1.SalesProvince__c = '北京'; assetA1.WH_location__c = '货架号1'; // 保有设备A (附属品 数量管理) Asset assetA2 = new Asset(Asset_Owner__c = 'Olympus'); assetA2.RecordTypeId = System.Label.Asset_RecordType; assetA2.SerialNumber = 'ass02'; assetA2.Name = 'ass02'; assetA2.AccountId = dep.Id; assetA2.Department_Class__c = strategicDep[0].Id; assetA2.Hospital__c = hospital.Id; assetA2.Product2Id = pro2.Id; assetA2.Quantity = 10; assetA2.Status = '有库存'; assetA2.Manage_type__c = '数量管理'; assetA2.Loaner_accsessary__c = true; assetA2.Product_category__c = 'GI'; assetA2.Delete_Flag__c = false; assetA2.Freeze_sign__c = false; assetA2.Out_of_wh__c = 3; assetA2.Frozen_Quantity__c = 2; assetA2.Salesdepartment__c = '1.华北营业本部'; assetA2.Internal_asset_location__c = '北京 备品中心'; assetA2.Product_category__c = 'GI'; assetA2.Equipment_Type__c = '产品试用'; assetA2.SalesProvince__c = '北京'; assetA2.WH_location__c = '货架号2'; // 保有设备A (附属品 个体管理) Asset assetA3 = new Asset(Asset_Owner__c = 'Olympus'); assetA3.RecordTypeId = System.Label.Asset_RecordType; assetA3.SerialNumber = 'ass03'; assetA3.Name = 'ass03'; assetA3.AccountId = dep.Id; assetA3.Department_Class__c = strategicDep[0].Id; assetA3.Hospital__c = hospital.Id; assetA3.Product2Id = pro3.Id; assetA3.Quantity = 10; assetA3.Status = '有库存'; assetA3.Manage_type__c = '数量管理'; assetA3.Loaner_accsessary__c = true; assetA3.Product_category__c = 'GI'; assetA3.Delete_Flag__c = false; assetA3.Freeze_sign__c = false; assetA3.Out_of_wh__c = 3; assetA3.Salesdepartment__c = '1.华北营业本部'; assetA3.Internal_asset_location__c = '北京 备品中心'; assetA3.Product_category__c = 'GI'; assetA3.Equipment_Type__c = '产品试用'; assetA3.SalesProvince__c = '北京'; assetA3.WH_location__c = '货架号3'; assetA3.Abandoned_Inventory__c = 1; insert new Asset[] {assetA1, assetA2, assetA3}; // 盘点表头 List invHeadTestList = new List(); Inventory_Header__c invHead1 = new Inventory_Header__c(); invHead1.Internal_asset_location__c = '北京 备品中心'; // invHead1.InventoryPIC__c = ; invHead1.Inventory_Start_Date__c = Date.today(); invHead1.Inventory_Status__c = '盘点中'; invHead1.UniqueKey__c = '备品中心:' + String.valueOf(Date.today()); invHeadTestList.add(invHead1); insert invHeadTestList; List invHeadList = [SELECT Id FROM Inventory_Header__c]; System.assertEquals(1, invHeadList.size()); // 盘点表明细 List invDetailTestList = new List(); Inventory_Detail__c invDetail1 = new Inventory_Detail__c(); invDetail1.Asset__c = assetA1.Id; invDetail1.Inventory_Header__c = invHead1.Id; invDetail1.toAbandon_amount__c = 1; // 待废弃数 invDetail1.Inventory_Count__c = 15; // 实盘数 invDetail1.Amount__c = 15; // 应盘数 invDetail1.Internal_Asset_number__c = 'abc'; // 固定资产 invDetail1.Internal_asset_location__c = '北京 备品中心'; invDetail1.Inventory_Time__c = Date.today(); invDetail1.Asset_Status__c = '在库'; invDetail1.UniqueKey__c = '备品中心:' + String.valueOf(Date.today()) + '1'; invDetailTestList.add(invDetail1); Inventory_Detail__c invDetail2 = new Inventory_Detail__c(); invDetail2.Asset__c = assetA2.Id; invDetail2.Inventory_Header__c = invHead1.Id; invDetail2.toAbandon_amount__c = 1; // 待废弃数 invDetail2.Inventory_Count__c = 15; // 实盘数 invDetail2.Amount__c = 15; // 应盘数 invDetail2.Internal_Asset_number__c = 'abc'; // 固定资产 invDetail2.Internal_asset_location__c = '北京 备品中心'; invDetail2.Asset_Status__c = '维修中'; invDetail2.Inventory_Time__c = Date.today(); invDetail2.UniqueKey__c = '备品中心:' + String.valueOf(Date.today()) + '2'; invDetailTestList.add(invDetail2); Inventory_Detail__c invDetail3 = new Inventory_Detail__c(); invDetail3.Asset__c = assetA3.Id; invDetail3.Inventory_Header__c = invHead1.Id; invDetail3.toAbandon_amount__c = 1; // 待废弃数 invDetail3.Inventory_Count__c = 15; // 实盘数 invDetail3.Amount__c = 15; // 应盘数 invDetail3.Internal_Asset_number__c = 'abc'; // 固定资产 invDetail3.Internal_asset_location__c = '北京 备品中心'; invDetail3.Asset_Status__c = '出借中'; invDetail3.Inventory_Time__c = Date.today(); invDetail3.UniqueKey__c = '备品中心:' + String.valueOf(Date.today()) + '3'; invDetailTestList.add(invDetail3); Inventory_Detail__c invDetail4 = new Inventory_Detail__c(); invDetail4.Asset__c = assetA1.Id; invDetail4.Inventory_Header__c = invHead1.Id; invDetail4.toAbandon_amount__c = 1; // 待废弃数 invDetail4.Inventory_Count__c = 15; // 实盘数 invDetail4.Amount__c = 15; // 应盘数 invDetail4.Internal_Asset_number__c = 'abc'; // 固定资产 invDetail4.Internal_asset_location__c = '北京 备品中心'; invDetail4.Asset_Status__c = '丢失'; invDetail4.UniqueKey__c = '备品中心:' + String.valueOf(Date.today()) + '4'; invDetailTestList.add(invDetail4); Inventory_Detail__c invDetail5 = new Inventory_Detail__c(); invDetail5.Asset__c = assetA2.Id; invDetail5.Inventory_Header__c = invHead1.Id; invDetail5.toAbandon_amount__c = 1; // 待废弃数 invDetail5.Inventory_Count__c = 15; // 实盘数 invDetail5.Amount__c = 15; // 应盘数 invDetail5.Internal_asset_location__c = '北京 备品中心'; invDetail5.Asset_Status__c = '在库'; invDetail5.UniqueKey__c = '备品中心:' + String.valueOf(Date.today()) + '5'; invDetailTestList.add(invDetail5); Inventory_Detail__c invDetail6 = new Inventory_Detail__c(); invDetail6.Asset__c = assetA3.Id; invDetail6.Inventory_Header__c = invHead1.Id; invDetail6.toAbandon_amount__c = 1; // 待废弃数 invDetail6.Inventory_Count__c = 15; // 实盘数 invDetail6.Amount__c = 15; // 应盘数 invDetail6.Internal_asset_location__c = '北京 备品中心'; invDetail6.Asset_Status__c = '在库'; invDetail6.UniqueKey__c = '备品中心:' + String.valueOf(Date.today()) + '6'; invDetailTestList.add(invDetail6); insert invDetailTestList; List invDetailList = [SELECT Id FROM Inventory_Detail__c]; System.assertEquals(6, invDetailList.size()); } // 获取存在经理(备品)、部长(备品)、总监的用户[用于保存为盘点报告提交人] static Map usersMap { get { List submitUser = [ SELECT Id, Name, JingliEquipmentManager__c, Buzhang_Equipment_Manager__c , ZongjianApprovalManager__c, SalesManager__c, BuchangApprovalManagerSales__c FROM User WHERE ZongjianApprovalManager__c <> null AND BuZhangEquipmentId__c <> null AND JingliEquipmentManager__c <> null AND Buzhang_Equipment_Manager__c <> null AND JingliEquipmentId__c <> null LIMIT 1 ]; System.assertEquals(submitUser.size(), 1); // todo jwt 如果是新刷的sandbox 可能会报错 Map userIdMap = new Map(); userIdMap.put('submiter', submitUser[0].Id); // 经理/部长(备品)没有的话, 取经理/部长(销售) if (submitUser[0].JingliEquipmentManager__c == null) { userIdMap.put('submiterJingli', submitUser[0].SalesManager__c); } else { userIdMap.put('submiterJingli', submitUser[0].JingliEquipmentManager__c); } if (submitUser[0].Buzhang_Equipment_Manager__c == null) { userIdMap.put('submiterBuzhang', submitUser[0].BuchangApprovalManagerSales__c); } else { userIdMap.put('submiterBuzhang', submitUser[0].Buzhang_Equipment_Manager__c); } userIdMap.put('submiterZongjian', submitUser[0].ZongjianApprovalManager__c); return userIdMap; }} static Inventory_Header__c invHeadObj { get { List invObjs = [ SELECT Id, Inventory_Status__c, Name, InventoryPIC__r.Name , Inventory_Submit_Date__c, Internal_asset_location__c, Inventory_Start_Date__c , Inventory_Date_From__c, Inventory_Date_To__c, Inventory_Persons__c , InventorySubmit_PIC__c, InventorySubmit_PIC__r.Post__c, InventoryCheck_PIC__c , InventoryCheck_PIC__r.Post__c, Checktime__c , Jingli_Equipment__r.Name, Buzhang_Equipment__r.Name, Zongjian_Equipment__r.Name , IvtDifference_Detail__c, IvtDifference_Comment__c , Buzhang_Equipment_Id__c, Zongjian_Equipment_Id__c, Jingli_Equipment_Id__c FROM Inventory_Header__c ]; System.assertEquals(invObjs.size(), 1); Inventory_Header__c invHeadObj = invObjs[0]; return invHeadObj; }} static String invHeadId { get { return invHeadObj.Id; }} // 初始化画面 static testMethod void testInitPage() { // システム管理者 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.Job_Category__c = '销售推广'; user.Province__c = '北京市'; user.Use_Start_Date__c = Date.today().addMonths(-6); user.SalesManager__c = UserInfo.getUserId(); user.BuchangApprovalManagerSales__c = UserInfo.getUserId(); user.JingliApprovalManager__c = UserInfo.getUserId(); user.BuchangApprovalManager__c = UserInfo.getUserId(); user.ZongjianApprovalManager__c = UserInfo.getUserId(); user.Buzhang_Equipment_Manager__c = UserInfo.getUserId(); user.JingliEquipmentManager__c = UserInfo.getUserId(); System.runAs(new User(Id = Userinfo.getUserId())) { setupTestData(); insert user; User currUser = [ SELECT Id, Name, Default_Referable_Apply_Equipment_Center__c FROM User WHERE Id = :UserInfo.getUserId() ]; System.assertEquals('北京 备品中心', currUser.Default_Referable_Apply_Equipment_Center__c); PageReference page = new PageReference('/apex/InventoryReport'); // 不带id的url System.Test.setCurrentPage(page); InventoryReportController controller = new InventoryReportController(); controller.init(); page = new PageReference('/apex/InventoryReport?Id=' + invHeadId); // 带id的url System.Test.setCurrentPage(page); controller.init(); System.Test.StartTest(); Inventory_Header__c invHead = [ SELECT Id, Inventory_Date_From__c, Inventory_Date_To__c, Inventory_Persons__c , IvtDifference_Detail__c, IvtDifference_Comment__c, Internal_asset_location__c , InventorySubmit_PIC__c, Jingli_Equipment__c, Buzhang_Equipment__c, Zongjian_Equipment__c FROM Inventory_Header__c WHERE Id = :invHeadId ]; System.assertEquals(invHead.Internal_asset_location__c, '北京 备品中心'); if (String.isNotBlank(invHead.InventorySubmit_PIC__c)) { User submitUser = [ SELECT Id, Name, JingliEquipmentManager__c, Buzhang_Equipment_Manager__c , ZongjianApprovalManager__c FROM User WHERE Id = :invHead.InventorySubmit_PIC__c ]; System.assertEquals(invHead.Jingli_Equipment__c, submitUser.JingliEquipmentManager__c); System.assertEquals(invHead.Buzhang_Equipment__c, submitUser.Buzhang_Equipment_Manager__c); System.assertEquals(invHead.Zongjian_Equipment__c, submitUser.ZongjianApprovalManager__c); } System.Test.StopTest(); } } // 保存按钮--盘点报告提交人 & 盘点报告确认人 & 盘点人员 & 盘点时间From & 盘点时间To & 盘点差异具体情况说明 & 差异资产部门处理意见 static testMethod void testSaveData() { setupTestData(); PageReference page = new PageReference('/apex/InventoryReport?Id=' + invHeadId); System.Test.setCurrentPage(page); InventoryReportController controller = new InventoryReportController(); controller.init(); controller.ihObj.Inventory_Date_From__c = Date.today(); controller.ihObj.Inventory_Date_To__c = Date.today(); controller.ihObj.Inventory_Persons__c = 'inventoryPerson1,inventoryPerson2'; controller.ihObj.InventorySubmit_PIC__c = usersMap.get('submiter'); controller.ihObj.InventoryCheck_PIC__c = usersMap.get('submiter'); // 随意指定,不能为空 // controller.ihObj.Jingli_Equipment__c = hpOwner.JingliEquipmentManager__c; // controller.ihObj.Buzhang_Equipment__c = hpOwner.Buzhang_Equipment_Manager__c; // controller.ihObj.Zongjian_Equipment__c = hpOwner.ZongjianApprovalManager__c; controller.ihObj.IvtDifference_Detail__c = '盘点差异具体情况说明'; controller.ihObj.IvtDifference_Comment__c = '差异资产部门处理意见'; System.Test.StartTest(); controller.init(); page = controller.saveSubmit(); Inventory_Header__c invHead = [ SELECT Id, Inventory_Date_From__c, Inventory_Date_To__c, Inventory_Persons__c , IvtDifference_Detail__c, IvtDifference_Comment__c , InventorySubmit_PIC__c, InventoryCheck_PIC__c, Jingli_Equipment__c, Buzhang_Equipment__c, Zongjian_Equipment__c FROM Inventory_Header__c WHERE Id = :invHeadId ]; // System.assertEquals(invHead.Inventory_Date_From__c, Date.today()); // System.assertEquals(invHead.Inventory_Date_To__c, Date.today()); // System.assertEquals(invHead.Inventory_Persons__c, 'inventoryPerson1,inventoryPerson2'); // System.assertEquals(invHead.IvtDifference_Detail__c, '盘点差异具体情况说明'); // System.assertEquals(invHead.IvtDifference_Comment__c, '差异资产部门处理意见'); // 自动获取盘点报告提交人的经理 部长 总监 //System.assertEquals(invHead.InventorySubmit_PIC__c, usersMap.get('submiter')); //System.assertEquals(invHead.Jingli_Equipment__c, usersMap.get('submiterJingli')); //System.assertEquals(invHead.Buzhang_Equipment__c, usersMap.get('submiterBuzhang')); //System.assertEquals(invHead.Zongjian_Equipment__c, usersMap.get('submiterZongjian')); System.Test.StopTest(); } // 提交盘点报告按钮 static testMethod void testSubmitData() { setupTestData(); PageReference page = new PageReference('/apex/InventoryReport?Id=' + invHeadId + '&saveType=1'); System.Test.setCurrentPage(page); InventoryReportController controller = new InventoryReportController(); controller.init(); System.assertEquals(String.isNotBlank(usersMap.get('submiter')), true); controller.ihObj.Inventory_Date_From__c = Date.today(); controller.ihObj.Inventory_Date_To__c = Date.today(); controller.ihObj.Inventory_Persons__c = 'inventoryPerson1,inventoryPerson2'; controller.ihObj.InventorySubmit_PIC__c = usersMap.get('submiter'); controller.ihObj.InventoryCheck_PIC__c = usersMap.get('submiter'); // 随意指定,不能为空 controller.ihObj.IvtDifference_Detail__c = '盘点差异具体情况说明'; controller.ihObj.IvtDifference_Comment__c = '差异资产部门处理意见'; page = controller.saveSubmit(); System.Test.StartTest(); //System.assertEquals(controller.ihObj.Inventory_Status__c, '填写完毕'); //System.assertEquals(controller.ihObj.Inventory_Submit_Date__c, Date.today()); // 自动获取盘点报告提交人的经理 部长 总监 System.Test.StopTest(); } // 提交盘点报告按钮--审批流 static testMethod void testApprovalProcess() { setupTestData(); PageReference page = new PageReference('/apex/InventoryReport?Id=' + invHeadId + '&saveType=1'); System.Test.setCurrentPage(page); InventoryReportController controller = new InventoryReportController(); controller.init(); System.assertEquals(String.isNotBlank(usersMap.get('submiter')), true); controller.ihObj.Inventory_Date_From__c = Date.today(); controller.ihObj.Inventory_Date_To__c = Date.today(); controller.ihObj.Inventory_Persons__c = 'inventoryPerson1,inventoryPerson2'; controller.ihObj.InventorySubmit_PIC__c = usersMap.get('submiter'); controller.ihObj.InventoryCheck_PIC__c = usersMap.get('submiter'); // 随意指定,不能为空 controller.ihObj.IvtDifference_Detail__c = '盘点差异具体情况说明'; controller.ihObj.IvtDifference_Comment__c = '差异资产部门处理意见'; page = controller.saveSubmit(); System.Test.StartTest(); // List piList = [ // SELECT Id, TargetObjectId, Status, CreatedDate, CompletedDate // ,( // SELECT Id // ,ActorId // ,Comments // ,IsPending // ,OriginalActorId // ,ProcessInstanceId // ,StepStatus // ,TargetObjectId // ,CreatedDate // FROM StepsAndWorkitems // ORDER BY CreatedDate // ) // FROM ProcessInstance // WHERE TargetObjectId = :invHeadObj.Id // ORDER BY CreatedDate DESC // ]; // List piHistory = piList[0].StepsAndWorkitems; // // Create an approval request for the Opportunity // Approval.ProcessWorkitemRequest req1 = new Approval.ProcessWorkitemRequest(); // req1.setComments('Remark'); // req1.setWorkitemId(piHistory[0].Id); // req1.setAction('Approve'); // // Submit the approval request for the Opportunity // Approval.ProcessResult result1 = Approval.process(req1); controller.init(); System.Test.StopTest(); } // for coverage static testMethod void testException() { setupTestData(); Inventory_Header__c invHeadObj = new Inventory_Header__c(); invHeadObj.Internal_asset_location__c = '北京 备品中心'; invHeadObj.Inventory_Start_Date__c = Date.today(); invHeadObj.Inventory_Status__c = '处理中'; invHeadObj.UniqueKey__c = '备品中心Obj:' + String.valueOf(Date.today()); insert invHeadObj; InventoryReportController controller = new InventoryReportController(); // 数据错误, 或者盘点报告状态为处理中 PageReference page1 = new PageReference('/apex/InventoryReport?Id=' + invHeadObj.Id); System.Test.setCurrentPage(page1); controller.init(); // 当前用户所在地区没有盘点报告, 或者盘点报告状态为处理中 PageReference page2 = new PageReference('/apex/InventoryReport'); System.Test.setCurrentPage(page2); controller.init(); delete invHeadObj; // 盘点时间From不能大于盘点时间To。 PageReference page3 = new PageReference('/apex/InventoryReport?Id=' + invHeadId); System.Test.setCurrentPage(page3); controller.init(); controller.ihObj.Inventory_Date_From__c = Date.today().addDays(60); controller.ihObj.Inventory_Date_To__c = Date.today(); page3 = controller.saveSubmit(); // 盘点报告提交人或确认人不能为空, 请先确认。 PageReference page4 = new PageReference('/apex/InventoryReport?Id=' + invHeadId); System.Test.setCurrentPage(page4); controller.init(); controller.ihObj.Inventory_Date_From__c = Date.today(); controller.ihObj.Inventory_Date_To__c = Date.today(); controller.ihObj.InventorySubmit_PIC__c = null; page4 = controller.saveSubmit(); } static testMethod void updaue() { InventoryReportController.test();//先调用这个,然后继续补充测试类 } }