public without sharing class PreProductStorageAdjustHandler extends Oly_TriggerHandler {
|
private List<PreProduct_StorageAdjust__c> oldList;
|
private List<PreProduct_StorageAdjust__c> newList;
|
private Map<Id,PreProduct_StorageAdjust__c> oldMap;
|
private Map<Id,PreProduct_StorageAdjust__c> newMap;
|
//当前时间
|
private DateTime currentTime = System.now();
|
|
public PreProductStorageAdjustHandler() {
|
this.newList = (List<PreProduct_StorageAdjust__c>) Trigger.new;
|
this.oldMap = (Map<Id,PreProduct_StorageAdjust__c>) Trigger.oldMap;
|
this.newMap = (Map<Id,PreProduct_StorageAdjust__c>) Trigger.newMap;
|
this.oldList = (List<PreProduct_StorageAdjust__c>) Trigger.old;
|
}
|
|
protected override void beforeInsert() {
|
//给送货方所有人赋值 审批流程需要
|
setColumn();
|
}
|
protected override void beforeUpdate() {
|
// check产品预留的本部是否为'其他'||'市场本部',等于时不能更新
|
// checkPreProductDepar();
|
//调整状态为申请中时,增加一条预留产品明细数据,类型为调出,(减少送货方预留产品数量以及check)这个应该在产品明细的触发器里面写
|
checkBeforApproval();
|
}
|
|
protected override void afterUpdate() {
|
//审批批准生成一条预留产品明细,类型为调入.(增加收货方预留产品数量)这个应该在产品明细的触发器里面写
|
adjustPreproductNumber();
|
//如果审批不通过,状态变为驳回时,得在减少的产品预留上增加一条调入的明细,将数量变回
|
//增加一条调入的明细
|
rejectSubmitApproval();
|
//如果审批撤回,同上
|
recallSubmitApproval();
|
}
|
|
|
public void setColumn() {
|
//存放送货方产品预留id
|
List<String> givePreProductIdList = new List<String>();
|
for( PreProduct_StorageAdjust__c adjust : newList){
|
givePreProductIdList.add(adjust.PreProduct_StorageGive__c);
|
System.debug(adjust.PreProduct_StorageGive__c);
|
}
|
//送货方产品预留对象
|
List<PreProduct_Storage__c> givePreProductList = [SELECT OwnerId,CreatedById,ManyImportOwnerId__c
|
FROM PreProduct_Storage__c
|
WHERE ifValid__c = true AND Id IN : givePreProductIdList];
|
//产品预留id,所有人id
|
Map<String,PreProduct_Storage__c> preproductAndOwnMap = new Map<String,PreProduct_Storage__c>();
|
for(PreProduct_Storage__c product : givePreProductList){
|
preproductAndOwnMap.put(product.Id,product);
|
}
|
|
for( PreProduct_StorageAdjust__c adjust : newList){
|
if(preproductAndOwnMap.containskey(adjust.PreProduct_StorageGive__c)){
|
//拿到正确的所有人,产品预留有可能是通过批量导入的,批量导入给ManyImportOwnerId__c赋值原本的所有人
|
if(preproductAndOwnMap.get(adjust.PreProduct_StorageGive__c).ManyImportOwnerId__c == null ){
|
adjust.GivePreProductOwnPerson__c = preproductAndOwnMap.get(adjust.PreProduct_StorageGive__c).OwnerId;
|
}
|
else{
|
adjust.GivePreProductOwnPerson__c = preproductAndOwnMap.get(adjust.PreProduct_StorageGive__c).ManyImportOwnerId__c;
|
}
|
}
|
//给接收方的本部窗口、省份助理 以及 调出方的本部窗口以及省份助理赋值
|
adjust.AcceptDepartWindow__c = adjust.AcceptDepartWindow_F__c;
|
adjust.AcceptProvinceAssistance__c = adjust.AcceptProvinceAssis_F__c;
|
adjust.AcceptProvinceSPAssistance__c = adjust.AcceptProvinceAssis_F__c;
|
adjust.GiveDepartWindow__c = adjust.GiveDepartWindow_F__c;
|
adjust.GiveProvinceAssistance__c = adjust.GiveProvinceAssist_F__c;
|
adjust.GiveProvinceSPAssist__c = adjust.GiveProvinceSPAssist_F__c;
|
}
|
}
|
|
|
public void adjustPreproductNumber(){
|
//调入的预留产品明细的list
|
List<PreProduct_Storage_List__c> storageList = new List<PreProduct_Storage_List__c>();
|
for( PreProduct_StorageAdjust__c adjust : newList){
|
if(adjust.Status__c != null && adjust.Status__c.equals('批准')){
|
PreProduct_Storage_List__c storageDetails = new PreProduct_Storage_List__c();
|
storageDetails.PreProduct_Storage__c = adjust.PreProduct_Storage__c;
|
storageDetails.Modify_Number__c = adjust.Quantity__c;
|
storageDetails.Change_Type__c = '调入';
|
storageDetails.Modified_By__c = adjust.CreatedById;
|
storageDetails.Modified_Time__c = currentTime;
|
storageDetails.AddReduceNumber__c = +adjust.Quantity__c;
|
storageDetails.PreProductAdjust__c = adjust.Id;
|
storageList.add(storageDetails) ;
|
}
|
}
|
if(storageList.size() > 0){
|
INSERT storageList;
|
}
|
}
|
|
public void checkBeforApproval(){
|
//调出的预留产品明细的list
|
List<PreProduct_Storage_List__c> storageList = new List<PreProduct_Storage_List__c>();
|
for( PreProduct_StorageAdjust__c adjust : newList){
|
if(adjust.Status__c != null && adjust.Status__c.equals('申请中')){
|
PreProduct_Storage_List__c storageDetails = new PreProduct_Storage_List__c();
|
storageDetails.PreProduct_Storage__c = adjust.PreProduct_StorageGive__c;
|
storageDetails.Modify_Number__c = adjust.Quantity__c;
|
storageDetails.Change_Type__c = '调出';
|
storageDetails.Modified_By__c = adjust.CreatedById;
|
storageDetails.Modified_Time__c = currentTime;
|
storageDetails.AddReduceNumber__c = -adjust.Quantity__c;
|
storageDetails.PreProductAdjust__c = adjust.Id;
|
storageList.add(storageDetails);
|
}
|
|
}
|
if(storageList.size() > 0){
|
INSERT storageList;
|
}
|
|
}
|
|
// public void checkPreProductDepar(){
|
// //查找设计的预留产品
|
// Set<String> changeNumberProductIdSet = new Set<String>();
|
// for( PreProduct_StorageAdjust__c adjust : newList){
|
// changeNumberProductIdSet.add(adjust.PreProduct_StorageGive__c);
|
// changeNumberProductIdSet.add(adjust.PreProduct_Storage__c);
|
// }
|
// if(changeNumberProductIdSet.size() > 0){
|
// List<PreProduct_Storage__c> changeNumberPreProductList = [SELECT OwnerId,CreatedById,Department__c
|
// FROM PreProduct_Storage__c
|
// WHERE ifValid__c = true AND Id IN : changeNumberProductIdSet];
|
// //是否有涉及的市场本部|其他
|
// for(PreProduct_Storage__c storage : changeNumberPreProductList){
|
// if(storage.Department__c.equals('其他') || storage.Department__c.equals('市场') ){
|
// // storage.Department__c.addError('所属本部为其他或市场本部时,不允许更新调整申请');
|
// String errorMessage = '所属本部为其他或市场时,不允许更新调整申请';
|
// throw new DmlException(errorMessage);
|
// }
|
// }
|
// }
|
|
// }
|
public void rejectSubmitApproval(){
|
//驳回自动生成调入的明细,将调出减少的数据还原
|
//调入的预留产品明细的list
|
List<PreProduct_Storage_List__c> storageList = new List<PreProduct_Storage_List__c>();
|
for( PreProduct_StorageAdjust__c adjust : newList){
|
if(adjust.Status__c != null && adjust.Status__c.equals('驳回') && oldMap.get(adjust.Id).Status__c =='申请中'){
|
PreProduct_Storage_List__c storageDetails = new PreProduct_Storage_List__c();
|
storageDetails.PreProduct_Storage__c = adjust.PreProduct_StorageGive__c;
|
storageDetails.Modify_Number__c = adjust.Quantity__c;
|
storageDetails.Change_Type__c = '调入';
|
storageDetails.Modified_By__c = adjust.CreatedById;
|
storageDetails.Modified_Time__c = currentTime;
|
storageDetails.AddReduceNumber__c = +adjust.Quantity__c;
|
storageDetails.PreProductAdjust__c = adjust.Id;
|
storageList.add(storageDetails) ;
|
}
|
}
|
if(storageList.size() > 0){
|
INSERT storageList;
|
}
|
}
|
public void recallSubmitApproval(){
|
//撤回自动生成调入的明细,将调出减少的数据还原
|
//调入的预留产品明细的list
|
List<PreProduct_Storage_List__c> storageList = new List<PreProduct_Storage_List__c>();
|
for( PreProduct_StorageAdjust__c adjust : newList){
|
if(adjust.Status__c != null && adjust.Status__c.equals('审批撤回') && oldMap.get(adjust.Id).Status__c =='申请中'){
|
PreProduct_Storage_List__c storageDetails = new PreProduct_Storage_List__c();
|
storageDetails.PreProduct_Storage__c = adjust.PreProduct_StorageGive__c;
|
storageDetails.Modify_Number__c = adjust.Quantity__c;
|
storageDetails.Change_Type__c = '调入';
|
storageDetails.Modified_By__c = adjust.CreatedById;
|
storageDetails.Modified_Time__c = currentTime;
|
storageDetails.AddReduceNumber__c = +adjust.Quantity__c;
|
storageDetails.PreProductAdjust__c = adjust.Id;
|
storageList.add(storageDetails) ;
|
}
|
}
|
if(storageList.size() > 0){
|
INSERT storageList;
|
}
|
}
|
}
|