/** 
 | 
 * 注残出納表をupsert 
 | 
 */ 
 | 
trigger NFM010UpsertStatuAchievements on Statu_Achievements__c (after insert, after update) { 
 | 
    // CHAN-AZM8SE 2018/8/27 start 
 | 
    if (Label.update_Statu_Achievements_Journal.equals('true')) { 
 | 
        return; 
 | 
    } 
 | 
    // CHAN-AZM8SE 2018/8/27 end 
 | 
  
 | 
    Map<String, Statu_Achievements_Journal__c> stajUpsertMap = new Map<String, Statu_Achievements_Journal__c>(); 
 | 
    // CHAN-BCPCA3 需要更新或创建的虚拟DN start 
 | 
    Map<String, Statu_Achievements_DN__c> staDNUpsertMap 
 | 
        = new Map<String, Statu_Achievements_DN__c>(); 
 | 
  
 | 
    // CHAN-BCPCA3 需要更新或创建的虚拟DN end 
 | 
  
 | 
    List<String> stajOldKeys = new List<String>();        // 发货日変更なし、金額変更がある場合 
 | 
    Map<String, Statu_Achievements_Journal__c> stajOldKeyNewMap = new Map<String, Statu_Achievements_Journal__c>(); 
 | 
    List<String> stajOld2Keys = new List<String>();       // 金額変更なし、发货日変更がある場合 
 | 
    Map<String, Statu_Achievements_Journal__c> stajOld2KeyNewMap = new Map<String, Statu_Achievements_Journal__c>(); 
 | 
    List<String> stajOldKeyDelKeyList = new List<String>();       // 发货日がありからなしに変更した場合、oldが削除対象 
 | 
  
 | 
    Datetime pNow = Datetime.now(); 
 | 
    //--------------------CHAN-AZM8SE--START-------------------------------------------------------------------- 
 | 
    // 累计询价中所有注残的已发货金额 
 | 
    //Set<String> oppNo = new Set<String>(); 
 | 
    //Map<String,Decimal> oppShippedAmount = new Map<String,Decimal>(); 
 | 
    //Map<String,Decimal> staUnshippedAmount = new Map<String,Decimal>(); 
 | 
    //Map<String,Decimal> changeAmount = new Map<String,Decimal>(); 
 | 
    //for(Statu_Achievements__c oppSta : Trigger.new) { 
 | 
    //    oppNo.add(oppSta.Opp_Number__c); 
 | 
    //    oppShippedAmount.put(oppSta.Opp_Number__c, 0); 
 | 
    //    Decimal am = changeAmount.get(oppSta.Opp_Number__c) == null ? 0 : changeAmount.get(oppSta.Opp_Number__c); 
 | 
    //    Decimal newAm = 0; 
 | 
    //    if (oppSta.ReturnFlag__c) { 
 | 
    //        newAm = -(oppSta.ContractAmount__c - (oppSta.UnshippedAmount__c == null ? 0 : oppSta.UnshippedAmount__c)); 
 | 
    //    } else { 
 | 
    //        newAm = oppSta.ContractAmount__c - (oppSta.UnshippedAmount__c == null ? 0 : oppSta.UnshippedAmount__c); 
 | 
    //    } 
 | 
    //    changeAmount.put(oppSta.Opp_Number__c, am + newAm); 
 | 
    //} 
 | 
    //List<Statu_Achievements__c> oppStaList = [Select Id, Name, Opp_Number__c, ShippedAmount_F__c, ReturnFlag__c 
 | 
    //                                        from Statu_Achievements__c where Opp_Number__c In:oppNo]; 
 | 
    //if (oppStaList != null && oppStaList.size() > 0) { 
 | 
    //    for (Statu_Achievements__c amountSum : oppStaList) { 
 | 
    //        oppShippedAmount.put(amountSum.Opp_Number__c, oppShippedAmount.get(amountSum.Opp_Number__c) + amountSum.ShippedAmount_F__c); 
 | 
    //        if (amountSum.ReturnFlag__c) { 
 | 
    //            staUnshippedAmount.put(amountSum.Name, (staUnshippedAmount.get(amountSum.Name) ==null ? 0 : staUnshippedAmount.get(amountSum.Name)) + amountSum.ShippedAmount_F__c); 
 | 
    //        } 
 | 
    //    } 
 | 
    //} 
 | 
  
 | 
    if (Trigger.isInsert) { 
 | 
        // insertの場合、そのまま1レコードStatu_Achievements_Journal__cにinsert 
 | 
        for (Statu_Achievements__c sta : Trigger.new) { 
 | 
            Statu_Achievements_Journal__c staj = new Statu_Achievements_Journal__c(); 
 | 
            staj.IF_LastModifiedDateTime__c = pNow; 
 | 
            staj.Statu_Achievements__c = sta.id; 
 | 
            staj.CurrencyIsoCode = sta.CurrencyIsoCode; 
 | 
            if (sta.UnshippedAmount__c == null) { 
 | 
                staj.ShippedAmount__c = -sta.ContractAmount__c; 
 | 
            } else { 
 | 
                staj.ShippedAmount__c = sta.ContractAmount__c - sta.UnshippedAmount__c; 
 | 
            } 
 | 
            if (sta.ReturnFlag__c) { 
 | 
                staj.DeliveryDate__c = sta.LastApproveDate__c; 
 | 
                staj.ShippedAmount__c = -staj.ShippedAmount__c; 
 | 
            } else { 
 | 
                staj.DeliveryDate__c = sta.DeliveryDate__c; 
 | 
            } 
 | 
             
 | 
            // CHAN-BCPCA3 判断是否ZM订单 如果是则生成虚拟DN start 
 | 
            //只有ZM17、ZM18订单生成虚拟DN  thh 2021-12-02 start 
 | 
            System.debug('sta.orderType__c1:' + sta.orderType__c); 
 | 
            if ('ZM17'.equals(sta.orderType__c) || 'ZM18'.equals(sta.orderType__c)) { 
 | 
                System.debug('sta.orderType__c2:' + sta.orderType__c); 
 | 
                Statu_Achievements_DN__c staDN  = new Statu_Achievements_DN__c(); 
 | 
                staDN.orderType__c              = sta.orderType__c; 
 | 
                staDN.DeliveryDate__c           =  sta.LastApproveDate__c; 
 | 
                if (sta.orderType__c.equals('ZM17')) { 
 | 
                    staDN.ShippedAmount_ZM__c       =  sta.UnshippedAmount__c - sta.ContractAmount__c  ; 
 | 
                } else { 
 | 
                    staDN.ShippedAmount_ZM__c       =  sta.ContractAmount__c  - sta.UnshippedAmount__c ; 
 | 
                } 
 | 
                staDN.Statu_Achievements__c     =   sta.id; 
 | 
                if (staDN.DeliveryDate__c == null) continue; 
 | 
                staDN.DNKey__c                  =  sta.Name + ':' + 
 | 
                                                   datetime.newInstance( 
 | 
                                                       staDN.DeliveryDate__c.year(), 
 | 
                                                       staDN.DeliveryDate__c.month(), 
 | 
                                                       staDN.DeliveryDate__c.day() 
 | 
                                                   ).format('yyyyMMdd'); 
 | 
                 
 | 
                staDNUpsertMap.put(staDN.DNKey__c, staDN); 
 | 
                continue; 
 | 
            } 
 | 
            //只有ZM17、ZM18订单生成虚拟DN  thh 2021-12-02 end 
 | 
            // CHAN-BCPCA3 判断是否ZM订单 如果是则生成虚拟DN end 
 | 
  
 | 
            if (staj.DeliveryDate__c == null) continue; 
 | 
            staj.SoNo_DeliveryDate_Text__c = sta.Name + ':' + 
 | 
                                             datetime.newInstance( 
 | 
                                                 staj.DeliveryDate__c.year(), staj.DeliveryDate__c.month(), staj.DeliveryDate__c.day() 
 | 
                                             ).format('yyyyMMdd'); 
 | 
  
 | 
            stajUpsertMap.put(staj.SoNo_DeliveryDate_Text__c, staj); 
 | 
        } 
 | 
    } else { 
 | 
        // updateの場合、发货日、发货金额 有变化才是对象 
 | 
        Map<Id, Decimal> staAmountMap = new Map<Id, Decimal>(); 
 | 
        List<Id> staIds = new List<String>(); 
 | 
  
 | 
        // CHAN-BCPCA3 同一条ZM17,ZM18 注残所有调整金额 start 
 | 
        Map<Id, Decimal> staDNALAmountMap = new Map<Id, Decimal>(); 
 | 
        // CHAN-BCPCA3 同一条ZM17, ZM18 注残所有调整金额 end 
 | 
  
 | 
        //HWAG-B4PCZ5 start 2018-09-19 更新对应询价的共享规则 
 | 
        List<OpportunityShare> osList = new List<OpportunityShare>(); 
 | 
        list<Statu_Achievements__c> tmpSAlist = 
 | 
            [select id, DeliveryDate__c, FSE_GI_LeaderID__c, FSE_SP_Leader_id__c 
 | 
             , Opportunity__c 
 | 
             , Opportunity__r.Hospital__r.FSE_GI_Main_Leader__r.IsActive 
 | 
             , Opportunity__r.Hospital__r.FSE_SP_Main_Leader__r.IsActive 
 | 
             , ForecastAccuracyObject__c , Opportunity__r.ownerId 
 | 
             , Opportunity__r.OCM_man_province__c // CHAN-B6H4NT 2018/11/14 
 | 
             // SWAG-B7LADC 2018/12/21 start 
 | 
             , Opportunity__r.Hospital__r.FSE_ENG_Main_Leader__r.Id 
 | 
             , Opportunity__r.Hospital__r.FSE_ENG_Main_Leader__r.IsActive 
 | 
             , Opportunity__r.Hospital__r.FSE_ENG_Vice_Leader__c 
 | 
             , Opportunity__r.Hospital__r.FSE_GI_Vice_Leader__c 
 | 
             , Opportunity__r.Hospital__r.FSE_SP_Vice_Leader__c 
 | 
             // SWAG-B7LADC 2018/12/21 end 
 | 
             from Statu_Achievements__c where id in: Trigger.new]; 
 | 
  
 | 
        // SWAG-B7LADC 2018/12/21 获取出对应医院所有的副担当,并检索出其ID start 
 | 
        list<ID> FSE_SPVicelist = new list<ID>(); 
 | 
        list<ID> FSE_GIVicelist = new list<ID>(); 
 | 
        list<ID> FSE_ENGVicelist = new list<ID>(); 
 | 
        map<id, string> FSE_SPViceIDtoNameMap = new  map<id, string>(); 
 | 
        map<id, string> FSE_GIViceIDtoNameMap = new  map<id, string>(); 
 | 
        map<id, string> FSE_ENGViceIDtoNameMap = new  map<id, string>(); 
 | 
        list<string> FSE_SP_ViceNameList = new list<string>(); 
 | 
        list<string> FSE_GI_ViceNameList = new list<string>(); 
 | 
        list<string> FSE_ENG_ViceNameList = new list<string>(); 
 | 
        Map<id, Map<string, list<String>>> sAIDtoVICENameMap = new Map<id, Map<string, list<String>>>(); 
 | 
        for (Statu_Achievements__c temSta : tmpSAlist) { 
 | 
            Statu_Achievements__c OldSta = Trigger.oldMap.get(temSta.id); 
 | 
            if ((temSta.DeliveryDate__c != null && temSta.ForecastAccuracyObject__c) 
 | 
                    && (StaticParameter.EscapeNFM010UpsertStatuAchievementsTrigger  || 
 | 
                        OldSta.DeliveryDate__c == null || label.Statu_Achievements_share.equals('true'))) { 
 | 
                Map<String, List<String>>  FSETypetoNameMap = new Map<String, List<String>> (); 
 | 
                if (temSta.Opportunity__r.Hospital__r.FSE_ENG_Vice_Leader__c != null) { 
 | 
  
 | 
                    list<string> currentFSE_ENGUserNameList = new list<string>(); 
 | 
                    for (String UsrName : temSta.Opportunity__r.Hospital__r.FSE_ENG_Vice_Leader__c.split('[,\\,]')) { 
 | 
                        FSE_ENG_ViceNameList.add(UsrName); 
 | 
                        currentFSE_ENGUserNameList.add(UsrName); 
 | 
                    } 
 | 
                    FSETypetoNameMap.put('FSE_ENG', currentFSE_ENGUserNameList); 
 | 
  
 | 
                } 
 | 
  
 | 
                if (temSta.Opportunity__r.Hospital__r.FSE_GI_Vice_Leader__c != null) { 
 | 
                    list<string> currentFSE_GIUserNameList = new list<string>(); 
 | 
                    for (String UsrName : temSta.Opportunity__r.Hospital__r.FSE_GI_Vice_Leader__c.split('[,\\,]')) { 
 | 
                        FSE_GI_ViceNameList.add(UsrName); 
 | 
                        currentFSE_GIUserNameList.add(UsrName); 
 | 
                    } 
 | 
                    FSETypetoNameMap.put('FSE_GI', currentFSE_GIUserNameList); 
 | 
                } 
 | 
                if (temSta.Opportunity__r.Hospital__r.FSE_SP_Vice_Leader__c != null) { 
 | 
                    list<string> currentFSE_SPUserNameList = new list<string>(); 
 | 
                    for (String UsrName : temSta.Opportunity__r.Hospital__r.FSE_SP_Vice_Leader__c.split('[,\\,]')) { 
 | 
                        FSE_SP_ViceNameList.add(UsrName); 
 | 
                        currentFSE_SPUserNameList.add(UsrName); 
 | 
                    } 
 | 
                    FSETypetoNameMap.put('FSE_SP', currentFSE_SPUserNameList); 
 | 
  
 | 
                } 
 | 
                sAIDtoVICENameMap.put(temSta.id, FSETypetoNameMap); 
 | 
            } 
 | 
        } 
 | 
        //throw new ControllerUtil.myException( 'a' + FSE_ENG_ViceNameList + FSE_GI_ViceNameList +FSE_SP_ViceNameList); 
 | 
  
 | 
        list<user> allViceUserList = 
 | 
            [select id, Alias__c 
 | 
             from user 
 | 
             where IsActive = true 
 | 
                              and (Alias__c in: FSE_ENG_ViceNameList 
 | 
                                   or Alias__c in: FSE_GI_ViceNameList 
 | 
                                   or Alias__c in: FSE_SP_ViceNameList )]; 
 | 
        system.debug('allViceUserList------:' + allViceUserList); 
 | 
        for (User tempUser : allViceUserList) { 
 | 
            if (FSE_ENG_ViceNameList.contains(tempUser.Alias__c)) { 
 | 
                FSE_ENGVicelist.add(tempUser.id); 
 | 
                FSE_ENGViceIDtoNameMap.put(tempUser.id, tempUser.Alias__c); 
 | 
            } 
 | 
            if (FSE_SP_ViceNameList.contains(tempUser.Alias__c)) { 
 | 
                FSE_SPVicelist.add(tempUser.id); 
 | 
                FSE_SPViceIDtoNameMap.put(tempUser.id, tempUser.Alias__c); 
 | 
            } 
 | 
            if (FSE_GI_ViceNameList.contains(tempUser.Alias__c)) { 
 | 
                FSE_GIVicelist.add(tempUser.id); 
 | 
                FSE_GIViceIDtoNameMap.put(tempUser.id, tempUser.Alias__c); 
 | 
            } 
 | 
        } 
 | 
        system.debug('FSE_GIVicelist------:' + FSE_GIVicelist); 
 | 
        system.debug('FSE_SPVicelist------:' + FSE_SPVicelist); 
 | 
        system.debug('FSE_ENGVicelist------:' + FSE_ENGVicelist); 
 | 
  
 | 
  
 | 
        // SWAG-B7LADC 2018/12/21 获取出对应医院所有的副担当,并检索出其ID end 
 | 
  
 | 
        for (Statu_Achievements__c sta : tmpSAlist) { 
 | 
            Statu_Achievements__c OldSta = Trigger.oldMap.get(sta.id); 
 | 
            //system.debug('OldSta:'+OldSta.DeliveryDate__c); 
 | 
            //throw new ControllerUtil.myException('sta.Opportunity__r.OCM_man_province__c:' + sta.Opportunity__r.OCM_man_province__c); 
 | 
            if (  sta.Opportunity__r.OCM_man_province__c != null && 
 | 
                    !sta.Opportunity__r.OCM_man_province__c.equals('能量') && // CHAN-B6H4NT 2018/11/14 
 | 
                    (sta.DeliveryDate__c != null && sta.ForecastAccuracyObject__c) 
 | 
                    && (StaticParameter.EscapeNFM010UpsertStatuAchievementsTrigger  || 
 | 
                        OldSta.DeliveryDate__c == null || label.Statu_Achievements_share.equals('true'))) { 
 | 
                if (sta.FSE_GI_LeaderID__c != null 
 | 
                        && sta.FSE_GI_LeaderID__c != sta.Opportunity__r.ownerId && 
 | 
                        sta.Opportunity__r.Hospital__r.FSE_GI_Main_Leader__r.IsActive) { 
 | 
                    osList.add(new OpportunityShare( 
 | 
                                   UserOrGroupId = sta.FSE_GI_LeaderID__c, 
 | 
                                   OpportunityId = sta.Opportunity__c, OpportunityAccessLevel = 'Edit' 
 | 
                               )); 
 | 
                } 
 | 
                if (sta.FSE_SP_Leader_id__c != null 
 | 
                        && sta.FSE_SP_Leader_id__c != sta.Opportunity__r.ownerId && 
 | 
                        sta.Opportunity__r.Hospital__r.FSE_SP_Main_Leader__r.IsActive) { 
 | 
                    osList.add(new OpportunityShare( 
 | 
                                   UserOrGroupId = sta.FSE_SP_Leader_id__c, 
 | 
                                   OpportunityId = sta.Opportunity__c, OpportunityAccessLevel = 'Edit' 
 | 
                               )); 
 | 
                } 
 | 
  
 | 
  
 | 
                for (ID FSEGIID : FSE_GIVicelist) { 
 | 
  
 | 
                    if (FSEGIID != sta.Opportunity__r.ownerId 
 | 
                            && FSE_GIViceIDtoNameMap.get(FSEGIID) != null 
 | 
                            && sta.Opportunity__r.Hospital__r.FSE_GI_Vice_Leader__c != null 
 | 
                            && sAIDtoVICENameMap.get(sta.id) != null 
 | 
                            && sAIDtoVICENameMap.get(sta.id).get('FSE_GI') != null 
 | 
                            && sAIDtoVICENameMap.get(sta.id).get('FSE_GI').contains(FSE_GIViceIDtoNameMap.get(FSEGIID)) 
 | 
                       ) { 
 | 
                        osList.add(new OpportunityShare( 
 | 
                                       UserOrGroupId = FSEGIID, 
 | 
                                       OpportunityId = sta.Opportunity__c, OpportunityAccessLevel = 'Edit' 
 | 
                                   )); 
 | 
  
 | 
                    } 
 | 
                } 
 | 
                for (ID FSESPID : FSE_SPVicelist) { 
 | 
                    if (FSESPID != sta.Opportunity__r.ownerId 
 | 
                            && FSE_SPViceIDtoNameMap.get(FSESPID) != null 
 | 
                            && sta.Opportunity__r.Hospital__r.FSE_SP_Vice_Leader__c != null 
 | 
                            && sAIDtoVICENameMap.get(sta.id) != null 
 | 
                            && sAIDtoVICENameMap.get(sta.id).get('FSE_SP') != null 
 | 
                            && sAIDtoVICENameMap.get(sta.id).get('FSE_SP').contains(FSE_SPViceIDtoNameMap.get(FSESPID)) 
 | 
                       ) { 
 | 
                        osList.add(new OpportunityShare( 
 | 
                                       UserOrGroupId = FSESPID, 
 | 
                                       OpportunityId = sta.Opportunity__c, OpportunityAccessLevel = 'Edit' 
 | 
                                   )); 
 | 
  
 | 
                    } 
 | 
                } 
 | 
                osList.add(new OpportunityShare( 
 | 
                               UserOrGroupId = Label.Group_ServiceManagementID , 
 | 
                               OpportunityId = sta.Opportunity__c, OpportunityAccessLevel = 'Read' 
 | 
                           )); 
 | 
            } 
 | 
            if (sta.Opportunity__r.OCM_man_province__c != null && 
 | 
                    sta.Opportunity__r.OCM_man_province__c.equals('能量') && // CHAN-B6H4NT 2018/11/14 
 | 
                    (sta.DeliveryDate__c != null && sta.ForecastAccuracyObject__c) 
 | 
                    && (StaticParameter.EscapeNFM010UpsertStatuAchievementsTrigger  || 
 | 
                        OldSta.DeliveryDate__c == null || label.Statu_Achievements_share.equals('true'))) { 
 | 
                if (sta.Opportunity__r.Hospital__r.FSE_ENG_Main_Leader__r.Id != null 
 | 
                        && sta.Opportunity__r.Hospital__r.FSE_ENG_Main_Leader__r.Id != sta.Opportunity__r.ownerId && 
 | 
                        sta.Opportunity__r.Hospital__r.FSE_ENG_Main_Leader__r.IsActive) { 
 | 
                    osList.add(new OpportunityShare( 
 | 
                                   UserOrGroupId = sta.Opportunity__r.Hospital__r.FSE_ENG_Main_Leader__r.Id, 
 | 
                                   OpportunityId = sta.Opportunity__c, OpportunityAccessLevel = 'Edit' 
 | 
                               )); 
 | 
                } 
 | 
                for (ID FSEENGID : FSE_ENGVicelist) { 
 | 
                    if (FSEENGID != sta.Opportunity__r.ownerId 
 | 
                            && FSE_ENGViceIDtoNameMap.get(FSEENGID) != null 
 | 
                            && sta.Opportunity__r.Hospital__r.FSE_ENG_Vice_Leader__c != null 
 | 
                            && sAIDtoVICENameMap.get(sta.id) != null 
 | 
                            && sAIDtoVICENameMap.get(sta.id).get('FSE_ENG') != null 
 | 
                            && sAIDtoVICENameMap.get(sta.id).get('FSE_ENG').contains(FSE_ENGViceIDtoNameMap.get(FSEENGID)) 
 | 
  
 | 
                       ) { 
 | 
                        osList.add(new OpportunityShare( 
 | 
                                       UserOrGroupId = FSEENGID, 
 | 
                                       OpportunityId = sta.Opportunity__c, OpportunityAccessLevel = 'Edit' 
 | 
                                   )); 
 | 
  
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        if (osList.size() > 0) { 
 | 
            upsert osList; 
 | 
        } 
 | 
        //HWAG-B4PCZ5 end  2018-09-19 更新对应询价的共享规则 
 | 
  
 | 
        for (Statu_Achievements__c sta : Trigger.new) { 
 | 
            //Statu_Achievements__c oldTemp = Trigger.oldMap.get(sta.Id); 
 | 
            //if (oldTemp.UnshippedAmount__c == sta.UnshippedAmount__c) { 
 | 
            //    continue; 
 | 
            //} 
 | 
            Statu_Achievements_Journal__c staj = new Statu_Achievements_Journal__c(); 
 | 
            staj.IF_LastModifiedDateTime__c = pNow; 
 | 
            staj.Statu_Achievements__c = sta.id; 
 | 
            staj.CurrencyIsoCode = sta.CurrencyIsoCode; 
 | 
            if (sta.UnshippedAmount__c == null) { 
 | 
                staj.ShippedAmount__c = sta.ContractAmount__c; 
 | 
            } else { 
 | 
                staj.ShippedAmount__c = sta.ContractAmount__c - sta.UnshippedAmount__c; 
 | 
            } 
 | 
            //CHAN-BAN9DG 2019/03/27 start 注释为原有逻辑 退货注残出纳表的发货日从注残最终审判日改为发货日 
 | 
            //if (sta.ReturnFlag__c) { 
 | 
            //    staj.ShippedAmount__c = -staj.ShippedAmount__c; 
 | 
            //    staj.DeliveryDate__c = sta.LastApproveDate__c; 
 | 
            //} else { 
 | 
            //    staj.DeliveryDate__c = sta.DeliveryDate__c; 
 | 
            //} 
 | 
            if (sta.ReturnFlag__c) { 
 | 
                staj.ShippedAmount__c = -staj.ShippedAmount__c; 
 | 
  
 | 
            } 
 | 
            staj.DeliveryDate__c = sta.DeliveryDate__c; 
 | 
  
 | 
            // CHAN-BAN9DG 2019/03/27 end  注释为原有逻辑 
 | 
  
 | 
            // CHAN-BCPCA3 判断是否ZM订单 如果是则生成虚拟DN start 
 | 
            //只有ZM17、ZM18订单生成虚拟DN  thh 2021-12-02 start 
 | 
            if ('ZM17'.equals(sta.orderType__c) || 'ZM18'.equals(sta.orderType__c)) { 
 | 
                Statu_Achievements_DN__c staDN  = new Statu_Achievements_DN__c(); 
 | 
                staDN.orderType__c              = sta.orderType__c; 
 | 
                staDN.DeliveryDate__c           =  sta.LastApproveDate__c; 
 | 
                if (sta.orderType__c.equals('ZM17')) { 
 | 
                    staDN.ShippedAmount_ZM__c       =  sta.UnshippedAmount__c - sta.ContractAmount__c  ; 
 | 
                } else { 
 | 
                    staDN.ShippedAmount_ZM__c       =  sta.ContractAmount__c  - sta.UnshippedAmount__c ; 
 | 
                } 
 | 
                staDN.Statu_Achievements__c     =   sta.id; 
 | 
                if (staDN.DeliveryDate__c == null) continue; 
 | 
                staDN.DNKey__c                  =  sta.Name + ':' + 
 | 
                                                   datetime.newInstance( 
 | 
                                                       staDN.DeliveryDate__c.year(), 
 | 
                                                       staDN.DeliveryDate__c.month(), 
 | 
                                                       staDN.DeliveryDate__c.day() 
 | 
                                                   ).format('yyyyMMdd'); 
 | 
                 
 | 
                staIds.add(sta.Id); 
 | 
                staDNUpsertMap.put(staDN.DNKey__c, staDN); 
 | 
                staDNALAmountMap.put(sta.Id, 0); 
 | 
                continue; 
 | 
            } 
 | 
            //只有ZM17、ZM18订单生成虚拟DN  thh 2021-12-02 end 
 | 
            // CHAN-BCPCA3 判断是否ZM订单 如果是则生成虚拟DN end 
 | 
            if (staj.DeliveryDate__c == null) continue; 
 | 
            if (staj.DeliveryDate__c != null) { 
 | 
                staj.SoNo_DeliveryDate_Text__c = sta.Name + ':' + 
 | 
                                                 datetime.newInstance( 
 | 
                                                     staj.DeliveryDate__c.year(), 
 | 
                                                     staj.DeliveryDate__c.month(), 
 | 
                                                     staj.DeliveryDate__c.day() 
 | 
                                                 ).format('yyyyMMdd'); 
 | 
            } 
 | 
            staIds.add(sta.Id); 
 | 
            stajUpsertMap.put(staj.SoNo_DeliveryDate_Text__c, staj); 
 | 
            staAmountMap.put(sta.Id, 0); 
 | 
        } 
 | 
  
 | 
        //  CHAN-BCPCA3 检索该注残下现有的虚拟DN 并计算 虚拟DN值 start 
 | 
        /* 
 | 
         * 
 | 
         */ 
 | 
        List<Statu_Achievements_DN__c> staDNList = 
 | 
            [Select Id, Statu_Achievements__c, DeliveryDate__c, ShippedAmount_ZM__c, DNKey__c,orderType__c  
 | 
             from Statu_Achievements_DN__c 
 | 
             where Statu_Achievements__r.Id in : staIds]; 
 | 
  
 | 
        if (staDNList != null && staDNList.size() > 0) { 
 | 
            for (Statu_Achievements_DN__c staDN : staDNList) { 
 | 
                if (staDN.orderType__c == null) 
 | 
                    continue; 
 | 
                staDNALAmountMap.put(staDN.Statu_Achievements__c, staDN.ShippedAmount_ZM__c 
 | 
                                     + staDNALAmountMap.get(staDN.Statu_Achievements__c)); 
 | 
                if (staDNUpsertMap.containsKey(staDN.DNKey__c)) { 
 | 
                    Statu_Achievements_DN__c tempStaDN = staDNUpsertMap.get(staDN.DNKey__c); 
 | 
                    tempStaDN.ShippedAmount_ZM__c += staDN.ShippedAmount_ZM__c; 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        for (String staDNKey : staDNUpsertMap.keySet()) { 
 | 
            Statu_Achievements_DN__c tempStaDN = staDNUpsertMap.get(staDNKey); 
 | 
            tempStaDN.ShippedAmount_ZM__c = tempStaDN.ShippedAmount_ZM__c 
 | 
                                            - staDNALAmountMap.get(tempStaDN.Statu_Achievements__c); 
 | 
        } 
 | 
        //  CHAN-BCPCA3 检索该注残下现有的虚拟DN 并计算 虚拟DN值 end 
 | 
        List<Statu_Achievements_Journal__c> stajList = [Select Id, Statu_Achievements__c, DeliveryDate__c, ShippedAmount__c, SoNo_DeliveryDate_Text__c 
 | 
                from Statu_Achievements_Journal__c where Statu_Achievements__r.Id in : staIds]; 
 | 
                if (stajList != null && stajList.size() > 0) { 
 | 
                    for (Statu_Achievements_Journal__c sajc : stajList) { 
 | 
                        staAmountMap.put(sajc.Statu_Achievements__c, sajc.ShippedAmount__c 
 | 
                                 + staAmountMap.get(sajc.Statu_Achievements__c)); 
 | 
                if (stajUpsertMap.containsKey(sajc.SoNo_DeliveryDate_Text__c)) { 
 | 
                    Statu_Achievements_Journal__c tempStaj = stajUpsertMap.get(sajc.SoNo_DeliveryDate_Text__c); 
 | 
                    tempStaj.ShippedAmount__c += sajc.ShippedAmount__c; 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        for (String stajKey : stajUpsertMap.keySet()) { 
 | 
            Statu_Achievements_Journal__c tempStaj = stajUpsertMap.get(stajKey); 
 | 
            tempStaj.ShippedAmount__c = tempStaj.ShippedAmount__c - staAmountMap.get(tempStaj.Statu_Achievements__c); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    // SoNo_DeliveryDate_Text__c にて upsert 
 | 
    if (stajUpsertMap.size() > 0) { 
 | 
        ControllerUtil.upsertStatuAchievementsJournalByKey(stajUpsertMap.values()); 
 | 
    } 
 | 
  
 | 
    // CHAN-BCPCA3 插入并更新虚拟DN数据 start 
 | 
    if (staDNUpsertMap.size() > 0) { 
 | 
        ControllerUtil.upsertStatuAchievementsDNByKey(staDNUpsertMap.values()); 
 | 
    } 
 | 
  
 | 
    // CHAN-BCPCA3 插入并更新虚拟DN数据 end 
 | 
    // 削除 
 | 
    //ControllerUtil.deleteStatuAchievementsJournalByKey(stajOldKeyDelKeyList); 
 | 
    //--------------------CHAN-AZM8SE--END-------------------------------------------------------------------- 
 | 
} 
 |