Thhto
2022-11-17 ea6a4eec81a6a74e214aefc5971d701242c3e7d3
force-app/main/default/classes/NewQuoteEntryController.cls
@@ -220,6 +220,12 @@
               //【是否需要价格申请】 thh start
               If_Need_PriceApply__c
               //【是否需要价格申请】 thh end
               // Celon询价修改 2022-04-24 ssm start
               ,CelonOpportunity__c
               // Celon询价修改 2022-04-24 ssm end
               // lastbuy判断变更用户询价 start
               , If_Account_Change__c
               // lastbuy判断变更用户询价 end
               FROM Opportunity Where Id = :oppId];
    // 多年保修 start
    trade = '外貿';
@@ -362,8 +368,8 @@
      }
    } else {
      //添加行
      List<Quote> quoList =
        [ SELECT Id, Name, Cancel_Decide__c, CreatedDate, PriceRefreshDate__c, Quote_Print_Date__c,
      List<Quote> quoList =//SWAG-CKDATG 【委托】【OBSAP-报价委托】报价委托项目改善1 fy start QuotationChange__c,MainEngineWithoutMonitor__c,Interdepartmental__c,
        [ SELECT Id, Name, Cancel_Decide__c, CreatedDate, PriceRefreshDate__c, Quote_Print_Date__c,QuotationChange__c,MainEngineWithoutMonitor__c,Interdepartmental__c,
          Dealer_Final_Price__c, TotalPrice__c, Estimation_List_Price__c, QuoteNumber,
          QuoteToName, Quote_Expiration_Date__c, Quote_Comment__c, Stocking_Price__c, Unit_Price__c,
          Offer_Amount__c, TOTAL__c, Discount__c, Pricing__c, Preferential_Trading_Price__c, Contract__c,
@@ -442,6 +448,7 @@
        }
        if (copyQuoId == null) {
          quo = quoList[0];
          quo.QuoteName__c = quo.Name;
          // 多年保修 start
          quoteGurantee_Period = quo.Gurantee_Period__c;
@@ -813,10 +820,13 @@
    // CHAN-AVG3PW 询价报价画面规则变更
    Datetime cDate = quo.CreatedDate;
    over3month = cDate != null && cDate.date().addMonths(3) < Date.today();
//SWAG-CKDATG 【委托】【OBSAP-报价委托】报价委托项目改善1 fy start
quo.Offer_Amount__c = true;
quo.TOTAL__c = true;
quo.Preferential_Gurantee_Period__c = true;
//SWAG-CKDATG 【委托】【OBSAP-报价委托】报价委托项目改善1 fy end
    return null;
  }
  //Search Events============================================================
  // TODO ManualEntryと同様、jsにて解決できる、ここでwebserviceだけを実装、今後 by katsu
@@ -1254,6 +1264,8 @@
    for (QELine t : tmpactivities) {
      if (i == j) {
        Map<String, Product2> mpProduct2 = new Map<String, Product2>();                     // keyがProductCodeです。
        // Celon询价修改 20220513 ssm start
        String celon_type = String.isNotBlank(oppInfo.CelonOpportunity) ? oppInfo.CelonOpportunity : null;
        List<Product2> items = [select Id, Name, ProductCode,
                                Foreign_Trade_Cost_US__c, Foreign_Trade_List_US__c, Intra_Trade_Cost_RMB__c, Intra_Trade_List_RMB__c,
                                //CHAN-BKU3XH 检查是否存在不是同一个供销商名称 精琢技术 2020/02/17 Start 增加字段
@@ -1286,6 +1298,9 @@
                              //SFDC停止预警 lt 20211009 end
                                FROM Product2 Where ProductCode In :codelist
                                // Celon询价
                                And Celon_Product__c = :celon_type
                                // Celon询价
                                And Manual_Entry__c = false];
        for (Product2 prd : items) {
          system.debug('prd.ProductCode:::::' + prd.ProductCode);
@@ -1542,6 +1557,22 @@
      errorMessagechack = null;
      Savepoint sp = Database.setSavepoint();
      try {
        // 2022-04-19 营业部门需求 没有行项目不能保存 ssm start
        if (activities != null && activities.size() > 0) {
          QELine line1 = activities[0];
          // System.debug('Save() line1: ' + line1);
          // System.debug('line1 != null: ' + (line1 != null));
          // System.debug('line1.pageObject.PricebookEntryId != null: ' + (line1.pageObject.PricebookEntryId != null));
          // System.debug('isNotBlank(line1.pageObject.PricebookEntryId): ' + (String.isNotBlank(line1.pageObject.PricebookEntryId)));
          // System.debug('Save() line1 flag: ' + (!(line1 != null && String.isNotBlank(line1.pageObject.PricebookEntryId))));
          if (!(line1 != null && String.isNotBlank(line1.pageObject.PricebookEntryId))) {
            errorflg = true;
            errorMessage = '不允许保存空的报价单,请选择产品';
            return null;
          }
        }
        // 2022-04-19 ssm end
        // CHAN-AVG3PW 询价报价画面规则变更
        // 报价创建日超过三个月时,点保存时强制更新
        // CHAN-AZG864 不管在不在报价有效期内,CFDA不可销售的时候,都是报错的,其中不可销售产品显示红字,不应该保存。
@@ -1555,7 +1586,6 @@
          errormessage = '请更新不可销售的产品。';
          return null;
        }
        System.debug('checkSFDAStatus1 finished at: ' + System.currentTimeMillis());
        //データチェック
@@ -1737,6 +1767,22 @@
      // 报价创建日超过三个月时,点保存时强制更新
      // CHAN-AZG864 不管在不在报价有效期内,CFDA不可销售的时候,都是报错的,其中不可销售产品显示红字,不应该保存。
      // 2022-04-19 XYOU-CDLCMM 没有行项目不能保存 ssm start
      if (activities != null && activities.size() > 0) {
        QELine line1 = activities[0];
        // System.debug('Save() line1: ' + line1);
        // System.debug('line1 != null: ' + (line1 != null));
        // System.debug('line1.pageObject.PricebookEntryId != null: ' + (line1.pageObject.PricebookEntryId != null));
        // System.debug('isNotBlank(line1.pageObject.PricebookEntryId): ' + (String.isNotBlank(line1.pageObject.PricebookEntryId)));
        // System.debug('Save() line1 flag: ' + (!(line1 != null && String.isNotBlank(line1.pageObject.PricebookEntryId))));
        if (!(line1 != null && String.isNotBlank(line1.pageObject.PricebookEntryId))) {
          errorflg = true;
          errorMessage = '不允许保存空的报价单,请选择产品';
          return null;
        }
      }
      // 2022-04-19 ssm end
      if (WinOrDecideAlert && (!productStatusUpdated)) {
        errorflg = true;
        errorMessage = '产品状态发生变化,请更新';
@@ -1794,20 +1840,22 @@
      errorflg = false;
      errormessage = null;
      //20220214 fy lastbuy start 
      if(!ReservedProductVerification()){
        system.debug('flglastbuy++++'+flglastbuy);
        if(flglastbuy==1){
          errorflg = true;
          errormessage =  '预留产品表中没有该询价,请通过本部窗口联系营业管理课' ;
          return null;
        }else if(flglastbuy==2){
          errorflg = true;
          errormessage =  errorProductmodel+'产品数量不可超过产品预留数量' ;
          return null;
        }else if(flglastbuy==3){
          errorflg = true;
          errormessage =  '预留产品'+errorProductmodel+'未录入预留产品表';
          return null;
      if (!oppInfo.is_changed_opp) {
        if(!ReservedProductVerification()){
          system.debug('flglastbuy++++'+flglastbuy);
          if(flglastbuy==1){
            errorflg = true;
            errormessage =  '预留产品表中没有该询价,请通过本部窗口联系营业管理课' ;
            return null;
          }else if(flglastbuy==2){
            errorflg = true;
            errormessage =  errorProductmodel+'产品数量不可超过产品预留数量' ;
            return null;
          }else if(flglastbuy==3){
            errorflg = true;
            errormessage =  '预留产品'+errorProductmodel+'未录入预留产品表';
            return null;
          }
        }
      }
      //20220214 fy lastbuy end
@@ -1843,12 +1891,14 @@
      if (dataEntry() == false) {
        return null;
      }
      //SWAG-CC6AFQ【委托】又有询价跳过了报价计算 fy start
      if(SkipQuotefilg){
        errorflg = true;
        errormessage =  '画面配置变更,已生成新的报价单,请重新做报价计算';
        errormessage =  '产品配置或者经销商信息发生变化,已经生成新的报价单,请重新操作报价计算';
        quo.IsQuoteTrial__c = false;
        return null;
      }
      //SWAG-CC6AFQ【委托】又有询价跳过了报价计算 fy end
      //引合に見積決定をオン
      if (oppId == null) {
        system.debug('Error is Opportunityid null!!!');
@@ -1859,7 +1909,12 @@
          /*↓↓↓見積同期↓↓↓2012/11/28 未使用
          opps[0].SyncedQuoteId = quoId;
          ↑↑↑    ↑↑↑*/
          ControllerUtil.updOpp(opps[0]);
          // SWAG-CER5R4 优化保存速度 start
          // ControllerUtil.updOpp(opps[0]);
          StaticParameter.EscapeOppandStaTrigger = true;
          update opps[0];
          StaticParameter.EscapeOppandStaTrigger = false;
          // SWAG-CER5R4 优化保存速度 end
        }
        errorflg = true;
@@ -1994,7 +2049,12 @@
              if (quos[0].Quote_Date__c == null) {
                quos[0].Quote_Date__c = date.Today();
                opps[0].Estimation_Proposal_Date__c = date.Today();
                ControllerUtil.updOpp(opps[0]);
                // SWAG-CER5R4 优化保存速度 start
                // ControllerUtil.updOpp(opps[0]);
                StaticParameter.EscapeOppandStaTrigger = true;
                update opps[0];
                StaticParameter.EscapeOppandStaTrigger = false;
                // SWAG-CER5R4 优化保存速度 end
              }
              quos[0].Quote_Print_Date__c = date.Today();
              ControllerUtil.updQuote(quos[0]);
@@ -2143,7 +2203,11 @@
      }
    }
    //SWAG-CER5R4 【委托】报价合同金额增加限制 fy start
    if(ContractAmountLimitJudge()){
      return false;
    }
    //SWAG-CER5R4 【委托】报价合同金额增加限制 fy end
    if (error == true && WinOrDecideAlert == false) {
      PageArrange();
      errorflg = true;
@@ -2211,7 +2275,13 @@
    Boolean error = false;
    errorflg = false;
    errormessage = null;
    //obsap 最终用户合同金额验证 fy start
    if (quo.Dealer_Final_Price_Page__c == null) {
      quo.Dealer_Final_Price__c.addError(System.Label.Error_Message3);
      error = true;
      errormessage = System.Label.Error_Message3;
    }
    //obsap 最终用户合同金额验证 fy end
    if (checkAgentsDeleteFlag() == false) {
      return false;
    }
@@ -2574,7 +2644,55 @@
    return false;
  }
  //SWAG-C9JCS8 【委托】【紧急】询价GZ-SP-GD0757135报价单问题 fy end
  //SWAG-CER5R4 【委托】报价合同金额增加限制 fy start
  private boolean ContractAmountLimitJudge(){
    system.debug('oppInfo.Sales_Root+++='+oppInfo.Sales_Root);
    if(oppInfo.Sales_Root =='OCM直接销售'){
      return false;
    }
    Boolean error = false;
    // List<QuoteLineItem> act = new List<QuoteLineItem>();
    // List<QuoteLineItem> act2 = new List<QuoteLineItem>();
    Double AccrualTotal=0;
    Double sumContractAmount =0;
    for(QELine aaa :activities){
      // 2022-06-14 紧急修复 ssm
      if (String.isBlank(aaa.Asset_Model)) {
        continue;
      }
      if(aaa.pageObject.PricebookEntry.Product2Id!=null){
        if(aaa.GuranteePrice==null){
          aaa.GuranteePrice=0;
        }
        AccrualTotal += aaa.pageObject.Quantity__c*aaa.GuranteePrice;
        // act.add(aaa.pageObject);
      }
    }
    // act2=act.deepClone();
    // Double AccrualTotal=0;
    // Double sumContractAmount =0;
    // for(QuoteLineItem pspsc :act2){
    //   AccrualTotal += pspsc.Quantity__c*pspsc.GuranteePrice__c;
    // }
    if(quo.MultiYearWarrantyTotalPrice__c==null){
      quo.MultiYearWarrantyTotalPrice__c=0;
    }
    sumContractAmount = AccrualTotal+quo.MultiYearWarrantyTotalPrice__c;
    if(quo.OCM_Agent1_Price_Page__c<=sumContractAmount){
      error = true;
      errorflg = true;
      errormessage = '预测金额为负数,请正确填写合同金额。';
    }
    if(quo.multiYearWarranty__c){
      if(quo.OCM_Agent1_Price_Page__c<=0){
        error = true;
        errorflg = true;
        errormessage = '预测金额为负数,请正确填写合同金额。';
      }
    }
    return error;
  }
  //SWAG-CER5R4 【委托】报价合同金额增加限制 fy end
  public boolean dataEntry() {
    system.debug('activities++++----****3'+activities);
    //SWAG-C9JCS8 【委托】【紧急】询价GZ-SP-GD0757135报价单问题 fy start
@@ -2600,11 +2718,11 @@
        error = true;
        errormessage = System.Label.Error_Message3;
      }
      if (quo.Dealer_Final_Price_Page__c == null) {
        quo.Dealer_Final_Price__c.addError(System.Label.Error_Message3);
        error = true;
        errormessage = System.Label.Error_Message3;
      }
      // if (quo.Dealer_Final_Price_Page__c == null) {
      //   quo.Dealer_Final_Price__c.addError(System.Label.Error_Message3);
      //   error = true;
      //   errormessage = System.Label.Error_Message3;
      // }
      if (qb.Quote_Adjust_Calculate == null) {
        quo.Quote_Adjust_Calculate__c.addError(System.Label.Error_Message3);
        error = true;
@@ -2687,10 +2805,10 @@
      }
      if (DisCalculation >= 1000 || DisCalculation <= -1000) {
        error = true;
        errormessage = System.Label.Error_Message38;
      }
      // if (DisCalculation >= 1000 || DisCalculation <= -1000) {
      //   error = true;
      //   errormessage = System.Label.Error_Message38;
      // }
      if (qb.SalesCalculation1 >= 1000 || qb.SalesCalculation1 <= -1000) {
        error = true;
        errormessage = System.Label.Error_Message38;
@@ -2807,8 +2925,8 @@
        }
      } else {
        List<Quote> qs = New List<Quote>();
        qs = [select Id, OpportunityId, CreatedDate, Pricebook2Id, Name, Estimation_List_Price__c, Dealer_Final_Price__c,
        List<Quote> qs = New List<Quote>();//SWAG-CKDATG 【委托】【OBSAP-报价委托】报价委托项目改善1 fy start QuotationChange__c,MainEngineWithoutMonitor__c,Interdepartmental__c,
        qs = [select Id, OpportunityId, CreatedDate, Pricebook2Id, Name, Estimation_List_Price__c, Dealer_Final_Price__c,QuotationChange__c,MainEngineWithoutMonitor__c,Interdepartmental__c,
              Stocking_Price__c, Discount_Amount__c, Discount_Amount_Calculate__c, Quote_Adjust_Amount__c, Quote_Adjust_Calculate__c,
              Agency1__c, OCM_Agent1_Price__c, Agency1_Profit__c, Agency1_Profit_Rate__c, Quote_No__c,
              Agency2__c, Agent1_Agent2_Price__c, Agency2_Profit__c, Agency2_Profit_Rate__c,
@@ -2894,18 +3012,29 @@
      q.Quote_Comment__c = quo.Quote_Comment__c;
      q.Installation_location__c = quo.Installation_location__c;
      //SWAG-CKDATG 【委托】【OBSAP-报价委托】报价委托项目改善1 fy start
      q.QuotationChange__c = quo.QuotationChange__c;
      q.MainEngineWithoutMonitor__c = quo.MainEngineWithoutMonitor__c;
      q.Interdepartmental__c = quo.Interdepartmental__c;
      //SWAG-CKDATG 【委托】【OBSAP-报价委托】报价委托项目改善1 fy end
      /*
      if (hasType3Machine) {
          q.Installation_location__c = '';
      }
      */
      System.debug('start save quote at: ' + System.currentTimeMillis());
      System.debug('q.QuotationChange__c+++'+q.QuotationChange__c);
      System.debug('quo.QuotationChange__c++' + quo.QuotationChange__c);
      System.debug('quo.MainEngineWithoutMonitor__c++' + quo.MainEngineWithoutMonitor__c);
      if (quoId == null) {
        insert q;
        quo.Quote_No__c = q.Quote_No__c;
      } else {
        update q;
      }
      System.debug('q.QuotationChange__c2+++'+q.QuotationChange__c);
      System.debug('quo.QuotationChange__c2++' + quo.QuotationChange__c);
      System.debug('quo.MainEngineWithoutMonitor__c2++' + quo.MainEngineWithoutMonitor__c);
      System.debug('finished save quote at: ' + System.currentTimeMillis());
      system.debug('○○○○○Save1○○○○○');
@@ -2989,6 +3118,9 @@
      os = [select Id, Estimation_List_Price__c, Dealer_Final_Price__c, Estimation_List_Price_Without_Tax__c,
            Agency1__c, OCM_Agent1_Price__c, Agency1_Profit__c, Agency1_Profit_Rate__c, Stocking_Price__c,
            Agency2__c, Agent1_Agent2_Price__c, Agency2_Profit__c, Agency2_Profit_Rate__c, Quote_Update_Sum__c , Hospital__c,Is_Corrosion__c 
            // lt 20221008 SWAG-CHL5XA【FY23询价改善】-统计主机台数 start
            ,StageName ,OlyNumberHosts__c
            // lt 20221008 SWAG-CHL5XA【FY23询价改善】-统计主机台数 end
            From Opportunity Where Id = :oppid];
      if (os.size() > 0) {
        o = os[0];
@@ -3008,7 +3140,7 @@
        o.Stocking_Price__c = quo.Stocking_Price__c;
        o.Estimation_No__c = quo.Quote_No__c;
        o.Estimation_No__c = q.Quote_No__c; // quo.Quote_No__c;
        //o.Estimation_Name__c = quo.Name;
        o.Estimation_Name__c = q.Name;
        o.Estimation_Id__c = q.Id;
@@ -3024,6 +3156,40 @@
        o.quoteSavedDate__c = Date.today();
        // 多年保修 end
        // lt 20221008 SWAG-CHL5XA【FY23询价改善】-统计主机台数 start
        System.debug('lt123询价状态(阶段): ' + o.StageName);
        System.debug('lt123activities: ' + activities);
        o.OlyNumberHosts__c = 0;
        System.debug('lt123主机数: ' + o.OlyNumberHosts__c);
        List<String> oppIds = New List<String>();
        List<String> product2Ids = New List<String>();
        Map<String,Decimal> product2IdsMap = new Map<String,Decimal>();
        if (activities.size() > 0) {
          for (QELine a : activities) {
            if (String.isBlank(a.PageObject.Id__c) == false) {
              product2Ids.add(a.PageObject.Id__c);
              product2IdsMap.put(a.PageObject.Id__c,a.PageObject.Quantity__c);
            }
          }
          for (Product2 prod : [Select Id,ProductClass__c
                                From Product2 Where Id IN :product2Ids]) {
            System.debug('lt123产品市场类别: ' + prod.ProductClass__c);
                if(o.StageName == '引合' || o.StageName == '注残' || o.StageName == '出荷' || o.StageName == '完了'){
                  if(prod.ProductClass__c == '主机'){
                    if(product2IdsMap.containsKey(prod.Id)){
                      o.OlyNumberHosts__c += product2IdsMap.get(prod.Id);
                      oppIds.add(o.Id);
                      ControllerUtil.UpdateBiddingFlag(oppIds);
                    }
                  }else{
                    o.OlyNumberHosts__c += 0;
                  }
                }
          }
        }
        // lt 20221008 SWAG-CHL5XA【FY23询价改善】-统计主机台数 end
        if (o.Quote_Update_Sum__c == null) {
          o.Quote_Update_Sum__c = 1;
@@ -3031,7 +3197,11 @@
          o.Quote_Update_Sum__c = o.Quote_Update_Sum__c + 1;
        }
        System.debug('start 1st save Opportunity at: ' + System.currentTimeMillis());
        ControllerUtil.updOpp(o);
        // SWAG-CER5R4 优化保存速度 start
        // ControllerUtil.updOpp(o);
        StaticParameter.EscapeOppandStaTrigger = true;
        update o;
        // SWAG-CER5R4 优化保存速度 end
        System.debug('finished 1st save Opportunity at: ' + System.currentTimeMillis());
        system.debug('○○○○○Save3○○○○○');
@@ -3041,7 +3211,10 @@
        //価格、単位、小計、OCM売上予測金額(税抜)、価格表
        List<OpportunityLineItem> ols = New List<OpportunityLineItem>();
        OpportunityLineItem ol = New OpportunityLineItem();
        ols = [select Id from OpportunityLineItem Where OpportunityId = :oppid];
        // lt 20221008 SWAG-CHL5XA【FY23询价改善】-统计主机台数 end
        ols = [select Id, Quantity from OpportunityLineItem Where OpportunityId = :oppid];  //update
        System.debug('lt123询价产品: ' + ols);
        // lt 20221008 SWAG-CHL5XA【FY23询价改善】-统计主机台数 end
        if (ols.size() > 0) {
          //delete
          ControllerUtil.delOppLine(ols);
@@ -3055,6 +3228,7 @@
            if (s.Asset_Model != null && s.Asset_Model != '') {
              if (s.PageObject.PricebookEntryId != null) {
                ol = New OpportunityLineItem();
                System.debug('lt123询价产品数量: ' + ol.Quantity);   //lt CHL5XA【FY23询价改善】
                ol.OpportunityId = oppid;
                ol.Id__c = s.PageObject.Id__c;
                // CHAN-AVG3PW 询价报价画面规则变更
@@ -3287,6 +3461,13 @@
    public String Authorized_DB_No { get; set; }
    // LHJ End
    // Celon询价修改 2022-04-24 ssm start
    public String CelonOpportunity { get; set; }
    // Celon询价修改 2022-04-24 ssm end
    // lastbuy判断变更用户询价 start
    public Boolean is_changed_opp { get; set; }
    // lastbuy判断变更用户询价 end
    public OppInfo(Opportunity opp) {
      Account_RecordType_DeveloperName = opp.Account.RecordType.DeveloperName;
      Direct_Separate = opp.Direct_Separate__c;
@@ -3300,6 +3481,12 @@
      If_Need_Authorize = opp.If_Need_Authorize__c;
      Authorized_DB_No = opp.Authorized_DB_No__c;
      // LHJ End
      // Celon询价修改 2022-04-24 ssm start
      CelonOpportunity = opp.CelonOpportunity__c;
      // Celon询价修改 2022-04-24 ssm end
      // lastbuy判断变更用户询价 start
      is_changed_opp = opp.If_Account_Change__c != null ? opp.If_Account_Change__c : false;
      // lastbuy判断变更用户询价 end
    }
  }
  //TODO 1.Quoteオブジェクト追加