trigger TrackConsumableSalesForecastTrigger on TrackConsumableSalesForecast__c (before insert, before update) {
|
|
//deloitte-zhj 20231124 本地化导入 start
|
if((!Test.isRunningTest())&&System.Label.ByPassTrigger.contains(UserInfo.getUserId())){
|
return;
|
}
|
//deloitte-zhj 20231124 本地化导入 end
|
|
String recordIdET = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ET').getRecordTypeId();
|
String recordIdENG = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ENG').getRecordTypeId();
|
if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){
|
List<TrackConsumableSalesForecast__c> newList = Trigger.new;
|
for(TrackConsumableSalesForecast__c tcsf : newList){
|
//根据产品分类分配记录类型
|
if(tcsf.Type__c == 'ENG'){
|
tcsf.RecordTypeId = recordIdENG;
|
}else {
|
tcsf.RecordTypeId = recordIdET;
|
}
|
//根据快照月取到半年前
|
Date sixMonthAgo;
|
Date lastMonth;
|
Integer year = tcsf.SnapshotMonth__c.year();
|
Integer month = tcsf.SnapshotMonth__c.month();
|
//失单预警赋值,不为1就为0
|
if(tcsf.BillLostWarning__c != 1){
|
tcsf.BillLostWarning__c = 0;
|
}
|
//医院/分类2赋值
|
if(tcsf.HospitalAndCategory2__c == null || tcsf.HospitalAndCategory2__c == ''){
|
tcsf.HospitalAndCategory2__c = tcsf.Management_Code__c + '-' + tcsf.Consumable_Category2__c;
|
}
|
if(month <= 6){
|
sixMonthAgo = Date.newInstance(year-1, 6+month, 1);
|
}else {
|
sixMonthAgo = Date.newInstance(year, month-6, 1);
|
}
|
if(month <= 1){
|
lastMonth = Date.newInstance(year-1, 11+month, 1);
|
}else {
|
lastMonth = Date.newInstance(year, month-1, 1);
|
}
|
if(tcsf.LastMonthDeliveryNumber__c == null){
|
tcsf.LastMonthDeliveryNumber__c = 0;
|
}
|
if(tcsf.LastSixMonthsAverage__c == null){
|
tcsf.LastSixMonthsAverage__c = 0;
|
}
|
if(tcsf.LastShipmentDate__c != null && tcsf.LastShipmentDate__c >= sixMonthAgo){
|
if(tcsf.LastShipmentDate__c >= lastMonth && tcsf.LastShipmentDate__c < tcsf.SnapshotMonth__c && tcsf.LastSixMonthsAverage__c == 0){
|
tcsf.SaleStatus1__c = '新开';
|
tcsf.SaleStatus2__c = '新开';
|
tcsf.BillLostWarning__c = 0;
|
}else{
|
tcsf.SaleStatus1__c = '存量';
|
if(tcsf.Type__c == 'ENG'){
|
if(tcsf.ThisMonthDeliveryNumber__c == 0 && tcsf.LastMonthDeliveryNumber__c == 0){
|
tcsf.SaleStatus2__c = '存量-';
|
}else if(tcsf.ThisMonthDeliveryNumber__c <= tcsf.LastMonthDeliveryNumber__c){
|
tcsf.SaleStatus2__c = '存量-';
|
}else if(tcsf.ThisMonthDeliveryNumber__c > tcsf.LastMonthDeliveryNumber__c){
|
tcsf.SaleStatus2__c = '存量+';
|
}
|
}else{
|
if(tcsf.ThisMonthDeliveryNumber__c <= tcsf.LastSixMonthsAverage__c){
|
tcsf.SaleStatus2__c = '存量-';
|
}else if(tcsf.ThisMonthDeliveryNumber__c > tcsf.LastSixMonthsAverage__c){
|
tcsf.SaleStatus2__c = '存量+';
|
}
|
}
|
}
|
}else if(tcsf.ThisMonthDeliveryNumber__c == null || tcsf.ThisMonthDeliveryNumber__c == 0){
|
tcsf.SaleStatus1__c = '丢单';
|
tcsf.SaleStatus2__c = '丢单';
|
}
|
}
|
}
|
}
|