@isTest private class SetPersonalProductTargetControllerTest { static private String currentPeriod(Integer i) { Date dateNow = Date.today(); Integer year = dateNow.year(); Integer month = dateNow.month(); if (month < 4) { year -= 1; } return String.valueOf(year + i - 1867 + 'P'); } static private Date oppCloseDate() { Date nowDate = Date.today(); Integer year = nowDate.year(); Integer month = nowDate.month(); if (month < 4) { nowDate = nowDate.addYears(-1); } return nowDate; } static private void makeIPC() { ImportantProductCategory__c ipc1 = new ImportantProductCategory__c( Name = 'test01', depName__c = 'GI', importantProductGoaIName__c = '重点产品01', commodityCode__c = 'test01', commodityName__c = 'test01', if_Quantity__c = true, sort__c = 1 ); insert ipc1; ImportantProductCategory__c ipc2 = new ImportantProductCategory__c( Name = 'test02', depName__c = 'GI', importantProductGoaIName__c = '重点产品02', commodityCode__c = 'test02', commodityName__c = 'test02', if_Quantity__c = true, sort__c = 2 ); insert ipc2; ImportantProductCategory__c ipc3 = new ImportantProductCategory__c( Name = 'test03', depName__c = 'GI', importantProductGoaIName__c = '重点产品03', commodityCode__c = 'test03', commodityName__c = 'test03', if_Quantity__c = false, sort__c = 3 ); insert ipc3; } // 主流程 @isTest static void myUnitTest() { // システム管理者 User u3 = new User(Test_staff__c = true); u3.LastName = '_サンブリッジ'; u3.FirstName = 'う'; u3.Alias = 'う'; u3.Email = 'olympusTest03@sunbridge.com'; u3.Username = 'olympusTest03@sunbridge.com'; u3.CommunityNickname = 'う'; u3.IsActive = true; u3.EmailEncodingKey = 'ISO-2022-JP'; u3.TimeZoneSidKey = 'Asia/Tokyo'; u3.LocaleSidKey = 'ja_JP'; u3.LanguageLocaleKey = 'ja'; u3.ProfileId = System.Label.ProfileId_SystemAdmin; u3.Job_Category__c = '销售推广'; u3.Province__c = '上海市'; u3.Use_Start_Date__c = Date.today().addMonths(-6); insert u3; System.runAs(u3) { Oly_TriggerHandler.bypass('PowerBIBaseHandler'); makeIPC(); Product2 product = new Product2( Name = 'テスト商品', Important_Rroduct_1GI__c = true, Important_Rroduct_2GI__c = true, Important_Rroduct_3GI__c = true, Important_Rroduct_4GI__c = true, Important_Rroduct_5GI__c = true ); insert product; Pricebook2 pricebook = ControllerUtil.getStandardPricebook(); PricebookEntry entry = new PricebookEntry( Pricebook2Id = pricebook.Id, Product2Id = product.Id); entry.UnitPrice = 0; entry.IsActive = true; entry.UseStandardPrice = false; entry.CurrencyIsoCode = 'CNY'; insert entry; RecordType[] rt = [select Id from RecordType where SobjectType = 'Opportunity' and IsActive = true and DeveloperName = 'Target']; Profile p = [select id from Profile where id = :System.Label.ProfileId_SystemAdmin]; User u1 = new User(Test_staff__c = false); u1.LastName = '_サンブリッジ7'; u1.FirstName = 'あ'; u1.Alias = 'あ9'; u1.Email = 'olympusTest01@sunbridge.com'; u1.Username = 'olympusTest91@sunbridge.com'; u1.CommunityNickname = 'あ9'; u1.IsActive = true; u1.EmailEncodingKey = 'ISO-2022-JP'; u1.TimeZoneSidKey = 'Asia/Tokyo'; u1.LocaleSidKey = 'ja_JP'; u1.LanguageLocaleKey = 'ja'; u1.ProfileId = System.Label.ProfileId_SystemAdmin; u1.Job_Category__c = '销售推广'; u1.Province__c = '上海市'; u1.Post__c = '一般'; u1.Sales_Speciality__c = '产品担当'; u1.Use_Start_Date__c = Date.today().addMonths(-6); insert u1; User u2 = new User(Test_staff__c = false); u2.LastName = '_サンブリッジ6'; u2.FirstName = 'い'; u2.Alias = 'い'; u2.Email = 'olympusTest02@sunbridge.com'; u2.Username = 'olympusTest02@sunbridge.com'; u2.CommunityNickname = 'い'; u2.IsActive = true; u2.EmailEncodingKey = 'ISO-2022-JP'; u2.TimeZoneSidKey = 'Asia/Tokyo'; u2.LocaleSidKey = 'ja_JP'; u2.LanguageLocaleKey = 'ja'; u2.ProfileId = System.Label.ProfileId_SystemAdmin; u2.Job_Category__c = '销售推广'; u2.Province__c = '上海市'; u2.Post__c = '一般'; u2.Sales_Speciality__c = '产品担当'; u2.Use_Start_Date__c = Date.today().addMonths(-6); insert u2; SetPersonalProductTargetController sptc = new SetPersonalProductTargetController(); // 初期表示 sptc.init(); //System.assertEquals(2, sptc.dataBeans.size()); //System.assertEquals(3, sptc.dataBeans.size()); System.assertEquals(false, sptc.saveFlg); if (Date.today().month() != 3) { System.assertEquals(false, sptc.isPast); } else { //System.assertEquals(true, sptc.isPast); System.assertEquals(false, sptc.isPast); } //System.assertEquals(currentPeriod(0), sptc.currentPeriod); System.assertEquals(true, sptc.previousRendered); System.assertEquals(true, sptc.nextRendered); // 插入数据 sptc.dataBeans[0].oppInput[0].Num_Of_OPD__c = 100; sptc.dataBeans[0].isChanged = '1'; sptc.dataBeans[1].oppInput[0].Num_Of_OPD__c = 200; sptc.dataBeans[1].isChanged = '1'; sptc.saveBtn(); System.assertEquals('保存成功', ApexPages.getMessages()[0].getDetail()); //20210207 ljh add start //导出 sptc.exportBycsv(); System.assertEquals('test01(GI)', sptc.opportunity_category[0]);//opportunity_category System.assertEquals('test02(GI)', sptc.opportunity_category[1]); System.assertEquals('test03(GI)金额', sptc.opportunity_category[2]); System.assertEquals(3, sptc.opportunity_category.size()); sptc.csvAsString = '本部,省,角色,担当,职位,负责产品分类(主),负责产品分类(兼),test01(GI),test02(GI),test03(GI)金额'; sptc.csvAsString +='\n5.华东,上海市,上海GI组,あ9,一般,GI,,1,2,3'; //test read csv file sptc.importCSVFile(); //20210207 ljh add end //2018年7月9日 SWAG-AZHBH7 对中间表最终结果进行比对 by 张玉山 list Existed_Num_Major_Products = [select key__c, Num_Of_OPD__c, user_Alias__c, SAP_Province__c, Is_Processing__c, iYear__c from Num_Major_Product__c]; System.assertEquals(4, Existed_Num_Major_Products.size()); system.assertEquals(1, Existed_Num_Major_Products[0].Num_Of_OPD__c); system.assertEquals(200, Existed_Num_Major_Products[1].Num_Of_OPD__c); //sptc.UpdateBtn(); /* 2018年7月9日 SWAG-AZHBH7 移除原有对业务机会直接操作版本的test function by 张玉山 List opps = [select Id,Num_Of_OPD__c,Owner_System__c,Important_Key_product_category__c,Target_category__c from Opportunity where RecordTypeId = :rt[0].Id and Important_Key_product_category__c = '重点产品01' order by ownerId]; System.assertEquals(24, opps.size()); System.assertEquals('重点产品01', opps[0].Important_Key_product_category__c); System.assertEquals('担当重点产品目标', opps[0].Target_category__c); System.assertEquals('重点产品01', opps[12].Important_Key_product_category__c); System.assertEquals('担当重点产品目标', opps[12].Target_category__c); List olis1 = [select Id, Quantity,UnitPrice,OpportunityId from OpportunityLineItem where OpportunityId = :opps[0].Id]; System.assertEquals(100, olis1[0].Quantity); List olis2 = [select Id, Quantity,UnitPrice,OpportunityId from OpportunityLineItem where OpportunityId = :opps[12].Id]; System.assertEquals(200, olis2[0].Quantity); */ // 上年度 // 2018年7月9日 SWAG-AZHBH7 对saveFlg赋值true by 张玉山 sptc.saveFlg = true; sptc.previous(); //System.assertEquals(true, sptc.previousRendered); // 下年度 // 2018年7月9日 SWAG-AZHBH7 对saveFlg赋值true by 张玉山 sptc.saveFlg = true; sptc.next(); // 上年度 sptc.previous(); // 更新,删除 sptc.dataBeans[0].oppInput[0].Num_Of_OPD__c = 1000; sptc.dataBeans[0].isChanged = '1'; sptc.dataBeans[1].oppInput[0].Num_Of_OPD__c = null; sptc.dataBeans[1].isChanged = '1'; sptc.saveBtn(); //2018年7月9日 SWAG-AZHBH7 对中间表最终结果进行比对 Existed_Num_Major_Products = [select key__c, Num_Of_OPD__c, user_Alias__c, SAP_Province__c, Is_Processing__c, iYear__c from Num_Major_Product__c]; //System.assertEquals(2, Existed_Num_Major_Products.size()); //system.assertEquals(1000, Existed_Num_Major_Products[0].Num_Of_OPD__c); //system.assertEquals(null, Existed_Num_Major_Products[1].Num_Of_OPD__c); /* 2018年7月9日 SWAG-AZHBH7 移除原有对业务机会直接操作版本的test function List opps2 = [select Id,Num_Of_OPD__c,Owner_System__c,Important_Key_product_category__c,Target_category__c from Opportunity where RecordTypeId = :rt[0].Id and Important_Key_product_category__c = '重点产品01' order by ownerId]; System.assertEquals(12, opps2.size()); System.assertEquals('重点产品01', opps2[0].Important_Key_product_category__c); System.assertEquals('担当重点产品目标', opps2[0].Target_category__c); List olis3 = [select Id, Quantity,UnitPrice,OpportunityId from OpportunityLineItem where OpportunityId = :opps2[0].Id]; System.assertEquals(1000, olis3[0].Quantity); */ // 删除担当者 sptc.dataBeans[0].oppInput[0].Num_Of_OPD__c = null; sptc.dataBeans[0].isChanged = '1'; sptc.saveBtn(); /*2018年7月9日 SWAG-AZHBH7 移除原有对业务机会直接操作版本的test function List opps3 = [select Id,Num_Of_OPD__c,Owner_System__c,Important_Key_product_category__c,Target_category__c from Opportunity where RecordTypeId = :rt[0].Id and Important_Key_product_category__c = '重点产品01' order by ownerId]; System.assertEquals(0, opps3.size()); */ //2018年7月9日 SWAG-AZHBH7 对中间表最终结果进行比对 Existed_Num_Major_Products = [select key__c, Num_Of_OPD__c, user_Alias__c, SAP_Province__c, Is_Processing__c, iYear__c from Num_Major_Product__c]; //System.assertEquals(2, Existed_Num_Major_Products.size()); //system.assertEquals(null, Existed_Num_Major_Products[0].Num_Of_OPD__c); // 返回 sptc.backBtn(); } } // 数量,金额切换 @isTest static void myUnitTest2() { // システム管理者 User u3 = new User(Test_staff__c = true); u3.LastName = '_サンブリッジ5'; u3.FirstName = 'う'; u3.Alias = 'う'; u3.Email = 'olympusTest03@sunbridge.com'; u3.Username = 'olympusTest03@sunbridge.com'; u3.CommunityNickname = 'う'; u3.IsActive = true; u3.EmailEncodingKey = 'ISO-2022-JP'; u3.TimeZoneSidKey = 'Asia/Tokyo'; u3.LocaleSidKey = 'ja_JP'; u3.LanguageLocaleKey = 'ja'; u3.ProfileId = System.Label.ProfileId_SystemAdmin; u3.Job_Category__c = '销售推广'; u3.Province__c = '上海市'; u3.Use_Start_Date__c = Date.today().addMonths(-6); insert u3; System.runAs(u3) { Oly_TriggerHandler.bypass('PowerBIBaseHandler'); makeIPC(); Product2 product = new Product2( Name = 'テスト商品', Important_Rroduct_1GI__c = true, Important_Rroduct_2GI__c = true, Important_Rroduct_3GI__c = true, Important_Rroduct_4GI__c = true, Important_Rroduct_5GI__c = true ); insert product; Pricebook2 pricebook = ControllerUtil.getStandardPricebook(); PricebookEntry entry = new PricebookEntry( Pricebook2Id = pricebook.Id, Product2Id = product.Id); entry.UnitPrice = 0; entry.IsActive = true; entry.UseStandardPrice = false; entry.CurrencyIsoCode = 'CNY'; insert entry; RecordType[] rt = [select Id from RecordType where SobjectType = 'Opportunity' and IsActive = true and DeveloperName = 'Target']; User u1 = new User(Test_staff__c = false); u1.LastName = '_サンブリッジ4'; u1.FirstName = 'あ'; u1.Alias = 'あ9'; u1.Email = 'olympusTest01@sunbridge.com'; u1.Username = 'olympusTest91@sunbridge.com'; u1.CommunityNickname = 'あ9'; u1.IsActive = true; u1.EmailEncodingKey = 'ISO-2022-JP'; u1.TimeZoneSidKey = 'Asia/Tokyo'; u1.LocaleSidKey = 'ja_JP'; u1.LanguageLocaleKey = 'ja'; u1.ProfileId = System.Label.ProfileId_SystemAdmin; u1.Job_Category__c = '销售推广'; u1.Province__c = '上海市'; u1.Post__c = '一般'; u1.Sales_Speciality__c = '产品担当'; u1.Use_Start_Date__c = Date.today().addMonths(-6); insert u1; User u2 = new User(Test_staff__c = false); u2.LastName = '_サンブリッジ3'; u2.FirstName = 'い'; u2.Alias = 'い'; u2.Email = 'olympusTest02@sunbridge.com'; u2.Username = 'olympusTest02@sunbridge.com'; u2.CommunityNickname = 'い'; u2.IsActive = true; u2.EmailEncodingKey = 'ISO-2022-JP'; u2.TimeZoneSidKey = 'Asia/Tokyo'; u2.LocaleSidKey = 'ja_JP'; u2.LanguageLocaleKey = 'ja'; u2.ProfileId = System.Label.ProfileId_SystemAdmin; u2.Job_Category__c = '销售推广'; u2.Province__c = '上海市'; u2.Post__c = '一般'; u2.Sales_Speciality__c = '产品担当'; u2.Use_Start_Date__c = Date.today().addMonths(-6); insert u2; SetPersonalProductTargetController sptc = new SetPersonalProductTargetController(); // 初期表示 sptc.init(); // 插入数据 sptc.dataBeans[0].oppInput[0].Num_Of_OPD__c = 100; sptc.dataBeans[0].isChanged = '1'; sptc.dataBeans[1].oppInput[0].Num_Of_OPD__c = 200; sptc.dataBeans[1].isChanged = '1'; sptc.saveBtn(); list Existed_Num_Major_Products = [select key__c, Num_Of_OPD__c, user_Alias__c, SAP_Province__c, Is_Processing__c, iYear__c from Num_Major_Product__c]; System.assertEquals(2, Existed_Num_Major_Products.size()); //System.assertEquals(100, sptc.dataBeans[0].oppInput[0].Num_Of_OPD__c); System.assertEquals(200, sptc.dataBeans[1].oppInput[0].Num_Of_OPD__c); // 切换金额 sptc.opp.Important_Key_product_category__c = '重点产品02'; sptc.searchByImpKey(); //System.assertEquals(null, sptc.dataBeans[0].oppInput[0].Num_Of_OPD__c); //System.assertEquals(null, sptc.dataBeans[1].oppInput[0].Num_Of_OPD__c); // //// 插入数据 //sptc.dataBeans[0].oppInput[0].Num_Of_OPD__c = 300; //sptc.dataBeans[0].isChanged = '1'; //sptc.dataBeans[1].oppInput[0].Num_Of_OPD__c = 400; //sptc.dataBeans[1].isChanged = '1'; //sptc.saveBtn(); // //List opps2 = [select Id,Num_Of_OPD__c,Owner_System__c,Important_Key_product_category__c,Target_category__c from Opportunity where RecordTypeId = :rt[0].Id and Important_Key_product_category__c = '重点产品02' order by Id]; //System.assertEquals(24, opps2.size()); // //System.assertEquals(300, sptc.dataBeans[0].oppInput[0].Num_Of_OPD__c); //System.assertEquals(400, sptc.dataBeans[1].oppInput[0].Num_Of_OPD__c); } } // 上年度,下年度 @isTest static void myUnitTest3() { // システム管理者 User u3 = new User(Test_staff__c = true); u3.LastName = '_サンブリッジ1'; u3.FirstName = 'う'; u3.Alias = 'う'; u3.Email = 'olympusTest03@sunbridge.com'; u3.Username = 'olympusTest03@sunbridge.com'; u3.CommunityNickname = 'う'; u3.IsActive = true; u3.EmailEncodingKey = 'ISO-2022-JP'; u3.TimeZoneSidKey = 'Asia/Tokyo'; u3.LocaleSidKey = 'ja_JP'; u3.LanguageLocaleKey = 'ja'; u3.ProfileId = System.Label.ProfileId_SystemAdmin; u3.Job_Category__c = '销售推广'; u3.Province__c = '上海市'; u3.Use_Start_Date__c = Date.today().addMonths(-6); insert u3; System.runAs(u3) { Oly_TriggerHandler.bypass('PowerBIBaseHandler'); makeIPC(); Product2 product = new Product2( Name = 'テスト商品', Important_Rroduct_1GI__c = true, Important_Rroduct_2GI__c = true, Important_Rroduct_3GI__c = true, Important_Rroduct_4GI__c = true, Important_Rroduct_5GI__c = true ); insert product; Pricebook2 pricebook = ControllerUtil.getStandardPricebook(); PricebookEntry entry = new PricebookEntry( Pricebook2Id = pricebook.Id, Product2Id = product.Id); entry.UnitPrice = 0; entry.IsActive = true; entry.UseStandardPrice = false; entry.CurrencyIsoCode = 'CNY'; insert entry; RecordType[] rt = [select Id from RecordType where SobjectType = 'Opportunity' and IsActive = true and DeveloperName = 'Target']; // 102_销售产品推广 User u1 = new User(Test_staff__c = false); u1.LastName = '_サンブリッジ2'; u1.FirstName = 'あ'; u1.Alias = 'あ9'; u1.Email = 'olympusTest01@sunbridge.com'; u1.Username = 'olympusTest91@sunbridge.com'; u1.CommunityNickname = 'あ9'; u1.IsActive = true; u1.EmailEncodingKey = 'ISO-2022-JP'; u1.TimeZoneSidKey = 'Asia/Tokyo'; u1.LocaleSidKey = 'ja_JP'; u1.LanguageLocaleKey = 'ja'; u1.ProfileId = System.Label.ProfileId_SystemAdmin; u1.Job_Category__c = '销售推广'; u1.Province__c = '上海市'; u1.Post__c = '一般'; u1.Sales_Speciality__c = '产品担当'; u1.Use_Start_Date__c = Date.today().addMonths(-6); insert u1; Opportunity opp1 = new Opportunity(); opp1.Name = u1.Alias + ' 目标'; opp1.StageName = '目標'; opp1.OwnerId = u1.Id; opp1.Owner_System__c = u1.Id; opp1.Opportunity_Category__c = 'GI'; opp1.CloseDate = Date.valueOf(oppCloseDate().year() - 1 + '-04-01'); opp1.Num_Of_OPD__c = 150; opp1.Proportion__c = 100; opp1.SAP_Province__c = '上海市'; opp1.Target_category__c = '担当重点产品目标'; opp1.Important_Key_product_category__c = '重点产品01'; opp1.RecordTypeId = rt[0].Id; opp1.OCM_Target_period__c = currentPeriod(-1); insert opp1; List pbes = [select Id from PricebookEntry where IsActive = true limit 1]; OpportunityLineItem oppli1 = new OpportunityLineItem(); oppli1.OpportunityId = opp1.Id; oppli1.UnitPrice = 1; oppli1.Quantity = 150; oppli1.PricebookEntryId = pbes[0].Id; insert oppli1; Opportunity opp2 = new Opportunity(); opp2.Name = u1.Alias + ' 目标'; opp2.StageName = '目標'; opp2.OwnerId = u1.Id; opp2.Owner_System__c = u1.Id; opp2.Opportunity_Category__c = 'GI'; opp2.CloseDate = Date.valueOf(oppCloseDate().year() - 2 + '-04-01'); opp2.Num_Of_OPD__c = 250; opp2.Proportion__c = 100; opp2.SAP_Province__c = '上海市'; opp2.Target_category__c = '担当重点产品目标'; opp2.Important_Key_product_category__c = '重点产品01'; opp2.RecordTypeId = rt[0].Id; opp2.OCM_Target_period__c = currentPeriod(-2); insert opp2; OpportunityLineItem oppli2 = new OpportunityLineItem(); oppli2.OpportunityId = opp2.Id; oppli2.UnitPrice = 0; oppli2.Quantity = 250; oppli2.PricebookEntryId = pbes[0].Id; insert oppli2; SetPersonalProductTargetController sptc = new SetPersonalProductTargetController(); // 初期表示 sptc.init(); // 2018年7月9日 SWAG-AZHBH7 获取用户数量 by 张玉山 sptc.getUserSize(); //System.assertEquals(currentPeriod(0), sptc.currentPeriod); System.assertEquals(true, sptc.previousRendered); System.assertEquals(true, sptc.nextRendered); if (Date.today().month() != 3) { System.assertEquals(false, sptc.isPast); } else { //System.assertEquals(true, sptc.isPast); System.assertEquals(false, sptc.isPast); } System.assertEquals(null, sptc.dataBeans[0].oppInput[0].Num_Of_OPD__c); sptc.salesDpt = '1.华北'; sptc.searchByDpt(); // 2018年7月9日 SWAG-AZHBH7 设置saveflag 为true by张玉山 sptc.saveFlg = true; sptc.opp.SAP_Province__c = '北京市'; sptc.searchByProvince(); // 2018年7月9日 SWAG-AZHBH7 设置saveflag 为true by张玉山 sptc.opp.SAP_Province__c = null; sptc.searchByProvince(); // 2018年7月9日 SWAG-AZHBH7 设置saveflag 为true by张玉山 sptc.saveFlg = true; sptc.opp.Important_Key_product_category__c = '重点产品02'; sptc.searchByImpKey(); // 2018年7月9日 SWAG-AZHBH7 设置saveflag 为true by张玉山 sptc.saveFlg = true; sptc.loginUser.Job_Category__c = '销售推广'; sptc.searchByFilter(); sptc.loginUser.Sales_Speciality__c = 'FSE'; sptc.searchByFilter(); sptc.checkAll = false; sptc.searchByFilter(); } } }