@IsTest private class ET_Product_ScoreTableHistory_Batch2Test { /******************************************************************************************************* * @description testデータ作成 */ private static void setup() { // OLY_OCM-644 StaticParameter.EscapeConsumableOrderDetail2Trigger = true; //Account 经销商 recordtypeID Id AgencyRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId(); //Account 医院 recordtypeID Id HPRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('HP').getRecordTypeId(); // 省 Address_Level__c al = new Address_Level__c(Name = '東京', Level1_Code__c = 'CN-99', Level1_Sys_No__c = '999999'); insert al; // 市 Address_Level2__c al2 = new Address_Level2__c(Level1_Code__c = 'CN-99', Level1_Sys_No__c = '999999', Level1_Name__c = '東京', Name = '渋谷区', Level2_Code__c = 'CN-9999', Level2_Sys_No__c = '9999999', Address_Level__c = al.Id); insert al2; Account AccountHP = new Account(Name = 't_e_s_tHP001', AgentCode_Ext__c = '9999900', RecordTypeId = HPRecordTypeId, Is_Active__c = '有効', Attribute_Type__c = '卫生部', Speciality_Type__c = '综合医院', Grade__c = '一级', OCM_Category__c = 'SLTV', Is_Medical__c = '医疗机构', State_Master__c = al.Id, City_Master__c = al2.Id, Town__c = '东京'); insert AccountHP; Account AccountAgent1 = new Account(Name = 't_e_s_tAgent001', RecordTypeId = AgencyRecordTypeId); insert AccountAgent1; Account AccountAgent2 = new Account(Name = 't_e_s_tAgent002', RecordTypeId = AgencyRecordTypeId, ParentId = AccountHP.Id); insert AccountAgent2; Product2 prod01 = new Product2( Name = 'Test01', ProductCode = 'Test01', Asset_Model_No__c = 'Test01', SFDA_Status__c = '有効', Dealer_special_Object__c = true, Family = 'ET', Category2__c = '耗材', Category3__c = 'ERCP', Category4__c = '导丝', Category5__c = 'Visiglide35' ); Product2 prod02 = new Product2( Name = 'T_est002', ProductCode = 'T_estCode002', Category2__c = '耗材', Category3__c = 'ERCP', Category4__c = '导丝', Category5__c = 'Visiglide35', Family = 'ET', Packing_list_manual__c = 1, Asset_Model_No__c = 'T_est02', SFDA_Status__c = '有効', Dealer_special_Object__c = true ); Product2 prod03 = new Product2( Name = 'T_est003', ProductCode = 'T_estCode003', Category2__c = '耗材', Category3__c = 'Capsule', Category4__c = 'CP打印机', Category5__c = 'CP打印机', Family = 'ET', Packing_list_manual__c = 1, Asset_Model_No__c = 'T_est03', SFDA_Status__c = '有効', Dealer_special_Object__c = true ); Product2 prod04 = new Product2( Name = 'T_est004', ProductCode = 'T_estCode004', Category2__c = '耗材', Category3__c = '基幹', Category4__c = '清洗刷', Category5__c = 'DBW', Family = 'ET', Packing_list_manual__c = 1, Asset_Model_No__c = 'T_est04', SFDA_Status__c = '有効', Dealer_special_Object__c = true ); Product2 prod05 = new Product2( Name = 'T_est005', ProductCode = 'T_estCode005', Category2__c = '耗材', Category3__c = '基幹', Category4__c = '清洗刷', Category5__c = 'DBW', Family = 'ET', Packing_list_manual__c = 6, Asset_Model_No__c = 'T_est05', SFDA_Status__c = '有効', Dealer_special_Object__c = true ); Product2 prod06 = new Product2( Name = 'T_est006', ProductCode = 'T_estCode006', Category2__c = '耗材', Category3__c = '基幹', Category4__c = '清洗刷', Category5__c = 'DBW', Family = 'ET', Packing_list_manual__c = 5, Asset_Model_No__c = 'T_est06', SFDA_Status__c = '有効', Dealer_special_Object__c = true );Product2 prod07 = new Product2( Name = 'T_est007', ProductCode = 'T_estCode007', Category2__c = '耗材', Category3__c = '基幹', Category4__c = '乳头切开刀', Category5__c = '乳头切开-三腔', Family = 'ET', Packing_list_manual__c = 5, Asset_Model_No__c = 'T_est07', SFDA_Status__c = '有効', Dealer_special_Object__c = true ); Product2 prod08 = new Product2( Name = 'T_est008', ProductCode = 'T_estCode008', Category2__c = '耗材', Category3__c = '基幹', Category4__c = '注射针(胃镜)', Category5__c = 'DBW', Family = 'ET', Packing_list_manual__c = 5, Asset_Model_No__c = 'NM-40', SFDA_Status__c = '有効', Dealer_special_Object__c = true ); Product2 prod09 = new Product2( Name = 'T_est009', ProductCode = 'T_estCode009', Category2__c = '耗材', Category3__c = '呼吸科ET', Category4__c = '吸引活检针', Category5__c = 'DBW', Family = 'ET', Packing_list_manual__c = 5, Asset_Model_No__c = 'NA-U401SX', SFDA_Status__c = '有効', Dealer_special_Object__c = true ); Product2 [] pd2s = new List{ prod01, prod02, prod03, prod04, prod05, prod06, prod07, prod08, prod09 }; insert pd2s; List prod2cs = new List(); for(Product2 p: pd2s) { Product2__c pdc = new Product2__c( Name=p.Name, OT_CODE_Text__c=p.ProductCode, Product2__c = p.Id ); prod2cs.add(pdc); } insert prod2cs; System.runAs(new User(Id = Userinfo.getUserId())) { // PCL Opportunity opp01 = new Opportunity( AccountId = AccountAgent2.Id, Opportunity_Category__c = 'GI', Strategic_department_new__c = '消化科', SAP_Province__c = '辽宁省', Name = 't_e_s_t_opp', Purchase_Reason__c = '新设', Fund_Basis__c = '政府资金', Purchase_Type__c = '一般询价', Sales_Method__c = '院内招标', Trade__c = '内贸', Promise_Class__c = '内贸', Sales_Root__c = 'OCM直接販売', Close_Forecasted_Date__c = Date.today().addDays(50), CloseDate = Date.today(), Hospital__c = AccountHP.Id, StageName = '引合', SAP_Send_OK__c = true ); insert opp01; Consumable_order__c [] coList = new List(); for(Integer i=0;i(); for(Integer i=0;i(); for(Integer i=0;i(); Product_Score_Table__c pst01 = new Product_Score_Table__c( OCM_Term__c = '154P', Plan_Category3__c = 'ERCP', Category3__c = 'ERCP', Category4__c = '导丝'); pstList.add(pst01); Product_Score_Table__c pst02 = new Product_Score_Table__c( OCM_Term__c = '154P', Plan_Category3__c = 'Capsule', Category3__c = 'Capsule', Category4__c = 'CP打印机' ); pstList.add(pst02); Product_Score_Table__c pst03 = new Product_Score_Table__c( OCM_Term__c = '154P', Plan_Category3__c = 'Capsule', Category3__c = '基幹', Category4__c = '清洗刷' ); pstList.add(pst03); for(Product_Score_Table__c pst: pstList) { pst.UniqueKey__c = pst.OCM_Term__c+':'+pst.Category3__c+':'+pst.Category4__c; } insert pstList; } } /* * @description 直销实绩 */ private static testMethod void ET_Product_ScoreTableHistory_Batch2_01() { setup(); Account AccountHP = [SELECT Id, Name, Management_Code__c FROM Account WHERE Name = 't_e_s_tHP001']; Account AccountAgent2 = [SELECT Id,Name FROM Account WHERE Name = 't_e_s_tAgent002']; Test.startTest(); ET_Product_ScoreTableHistory_Batch2 bat = new ET_Product_ScoreTableHistory_Batch2 (); Id JobId = Database.executeBatch(bat); Test.stopTest(); // Product_Score_Table_History__c psth = [SELECT Shipping_date__c,Qty__c,UniqueKey__c FROM Product_Score_Table_History__c WHERE UniqueKey__c='H:150P:T_e_s_t_HP001:ERCP:导丝']; Product_Score_Table_History__c [] psthList = [SELECT Id,Shipping_date__c,Sales_Amount__c,Qty__c,UniqueKey__c,Product_Score_Table__r.UniqueKey__c FROM Product_Score_Table_History__c]; System.assertNotEquals('000000000000000',String.valueOf(JobId)); String uniqueKey_1 = 'U:154P:' + Date.today().month() + ':' + AccountHP.Management_Code__c + ':' + AccountAgent2.Id + ':ERCP:导丝'; String uniqueKey_2 = 'U:154P:' + Date.today().month() + ':' + AccountHP.Management_Code__c + ':' + AccountAgent2.Id + ':基幹:清洗刷'; String uniqueKey_3 = 'U:154P:' + Date.today().month() + ':' + AccountHP.Management_Code__c + ':' + AccountAgent2.Id + ':Capsule:CP打印机'; // System.assertEquals(3, psthList.size()); for(Product_Score_Table_History__c psth: psthList) { if (psth.UniqueKey__c == uniqueKey_1) { System.assertEquals(265.49,psth.Sales_Amount__c); System.assertEquals(2,psth.Qty__c); System.assertEquals('154P:ERCP:导丝',psth.Product_Score_Table__r.UniqueKey__c); System.assertEquals(psth.Shipping_date__c,Date.newInstance(Date.today().year(),Date.today().month(),1)); } else if (psth.UniqueKey__c == uniqueKey_2) { System.assertEquals(1327.43,psth.Sales_Amount__c); System.assertEquals(3,psth.Qty__c); System.assertEquals('154P:基幹:清洗刷',psth.Product_Score_Table__r.UniqueKey__c); System.assertEquals(psth.Shipping_date__c,Date.newInstance(Date.today().year(),Date.today().month(),1)); } else if (psth.UniqueKey__c == uniqueKey_3) { System.assertEquals(265.49,psth.Sales_Amount__c); System.assertEquals(1,psth.Qty__c); System.assertEquals('154P:Capsule:CP打印机',psth.Product_Score_Table__r.UniqueKey__c); System.assertEquals(psth.Shipping_date__c,Date.newInstance(Date.today().year(),Date.today().month(),1)); } } } /******************************************************************************************************* *@description 添加额外数据数据 */ private static testMethod void ET_Product_ScoreTableHistory_Batch2_02() { setup(); Test.startTest(); Account AccountHP = [SELECT Id, Name, Management_Code__c FROM Account WHERE Name = 't_e_s_tHP001']; Account AccountAgent2 = [SELECT Id,Name FROM Account WHERE Name = 't_e_s_tAgent002']; Id PSTH_RecordTypeId = Schema.SObjectType.Product_Score_Table_History__c.getRecordTypeInfosByDeveloperName().get('Company').getRecordTypeId(); //历史资料 Product2 p = [SELECT Plan_Term__c,Category3__c,Category4__c,CreatedDate FROM Product2 WHERE Name = 'Test01']; String month = String.valueOf(p.CreatedDate.month()); String term = p.Plan_Term__c.left(4); String uniqueKey = 'U:' + term + ':' + month + ':' + AccountHP.Management_Code__c + ':' + AccountAgent2.Id + ':' + p.Category3__c + ':' + p.Category4__c; String pstKey = term + ':' + p.Category3__c + ':' + p.Category4__c; List pstList = [SELECT Id FROM Product_Score_Table__c ];//WHERE UniqueKey__c = :pstKey Product_Score_Table__c pst=new Product_Score_Table__c(); if(pstList.size()>0){ pst=pstList[0]; } Product_Score_Table_History__c psth01 = new Product_Score_Table_History__c(); //UniqueKey__c : (151PA/151PB)计画财年:第三分类 :新4.5分类 :经销商 :医院 psth01.UniqueKey__c = uniqueKey; psth01.Hospital__c = AccountHP.Id; psth01.Product_Score_Table__c = pst.Id; psth01.Sales_Amount__c = 67; psth01.Shipping_date__c = Date.newInstance(Date.today().year(), Date.today().month(), 1); psth01.Agency__c = AccountAgent2.Id; psth01.Qty__c = 2; psth01.RecordTypeId = PSTH_RecordTypeId; insert psth01; //call ET_Product_ScoreTableHistory_Batch2 ET_Product_ScoreTableHistory_Batch2 bat = new ET_Product_ScoreTableHistory_Batch2 (); Database.executeBatch(bat); Test.stopTest(); System.runAs(new User(Id = Userinfo.getUserId())) { List psthList2 = [SELECT Sales_Amount__c,Qty__c,Shipping_date__c FROM Product_Score_Table_History__c ];//WHERE UniqueKey__c = :uniqueKey Product_Score_Table_History__c psth = new Product_Score_Table_History__c(); if(psthList2.size()>0){ psth=psthList2[0]; } // System.assertEquals(265.49, psth.Sales_Amount__c); // System.assertEquals(2, psth.Qty__c); // Date today = Date.today(); // System.assertEquals(Date.newInstance(today.year(), today.month(), 1), psth.Shipping_date__c); } } }