From 8ea597d3c67631cd702415d43bc3d2961f6bc94d Mon Sep 17 00:00:00 2001
From: 付煜 <fuyu3103346691@163.com>
Date: 星期五, 25 三月 2022 09:49:28 +0800
Subject: [PATCH] lastbuy询价相关

---
 force-app/main/default/classes/OpportunityTrigger.cls             |   99 ++++
 force-app/main/default/classes/NFM007TriggerTest.cls              |    9 
 force-app/main/default/classes/NewQuoteIraiController.cls         |  152 ++++++
 force-app/main/default/classes/OpportunityTriggerTest.cls         |   57 ++
 force-app/main/default/classes/NewQuoteEntryController.cls        |  122 +++++
 force-app/main/default/triggers/NFM007.trigger                    |  241 +++++++++++
 force-app/main/default/classes/SI_NewQuoteEntryController.cls     |  118 +++++
 force-app/main/default/classes/SI_NewQuoteEntryControllerTest.cls |   99 ++++
 force-app/main/default/classes/NewQuoteEntryControllerTest.cls    |  224 ++++++++++
 force-app/main/default/classes/NewQuoteIraiControllerTest.cls     |   91 +++
 10 files changed, 1,166 insertions(+), 46 deletions(-)

diff --git a/force-app/main/default/classes/NFM007TriggerTest.cls b/force-app/main/default/classes/NFM007TriggerTest.cls
index df44fe7..621bd37 100644
--- a/force-app/main/default/classes/NFM007TriggerTest.cls
+++ b/force-app/main/default/classes/NFM007TriggerTest.cls
@@ -996,7 +996,7 @@
 
 
         PromotionSalesProducts__c psp = new PromotionSalesProducts__c();
-        psp.QuantityId__c = quo.id;
+        psp.QuantityId__c = quo.ID;
         // psp.PromotionSales__r.PromotionHead__r.SpoId__c
         // psp.PromotionSales__r.Category__c
         psp.PromotionSales__c = ps.id;
@@ -1008,7 +1008,7 @@
         insert psp;
         
         QuoteLineItem qli = new QuoteLineItem();
-        qli.QuoteId = quo.Id;
+        qli.QuoteId = quo.ID;
         qli.Id__c = '210';
         qli.SFDA_Status__c = '鏈夊姽';
         qli.Name__c = '211';
@@ -1023,7 +1023,7 @@
         qli.PricebookEntryId = entry.Id;
         //qli.PricebookEntry.Product2Id = product.ID;
         insert qli;
-
+        
         OpportunityLineItem oppli = new OpportunityLineItem();
         oppli.OpportunityId = opp.Id;
         oppli.Id__c = '110';
@@ -1047,6 +1047,9 @@
         opp.Gurantee_Period__c = '2';
         opp.oldData_flg__c = true;
         opp.Estimation_Decision__c = true;
+        // opp.Estimation_Decision__c = true;
+        opp.Estimation_Id__c    = quo.ID;
+        // update opp;
         update opp;  
         //Test.stopTest();
     }
diff --git a/force-app/main/default/classes/NewQuoteEntryController.cls b/force-app/main/default/classes/NewQuoteEntryController.cls
index d7e3522..3fc9f56 100644
--- a/force-app/main/default/classes/NewQuoteEntryController.cls
+++ b/force-app/main/default/classes/NewQuoteEntryController.cls
@@ -9,6 +9,11 @@
   public Boolean changedAfterPrint { get; set; }                  // true 銇牬鍚堛�佺敾闈€伀 confirm 銉°儍銈汇兗銈搞亴琛ㄧず銇椼伨銇欍�俼uoId銈掓柊銇椼亜insert銆傚垽瀹氥伅js銇仸瀹熸柦
   public Boolean changedAfterBid { get; set; }                    // true 銇牬鍚堛�佺敾闈€伀 confirm 銉°儍銈汇兗銈搞亴琛ㄧず銇椼伨銇欍�俼uoId銈掓柊銇椼亜insert銆傚垽瀹氥伅js銇仹瀹熸柦
   //public Id qlistId { get; set; }
+  //lastbuy  2022/2/9 fy start
+  public Boolean filg { get; set; }
+  public Integer flglastbuy { get; set; }
+  public String errorProductmodel { get; set; }
+  //lastbuy  2022/2/9 fy end
 
   public String excel_text { get; set; }
   public Integer select_index { get; set; }                       // excelImport灏傜敤銇с仚銇屻�乯s銇仸鍒跺尽銇欍倠銇撱仺銇仾銈嬨伄銇с�乀ODO katsu 鍓婇櫎浜堝畾
@@ -152,6 +157,7 @@
         }
       } else {
         quoId = System.currentPageReference().getParameters().get('copyid');
+        system.debug('copyid++++++++++++'+quoId);
         if (quoId != null) {
           // 銈炽償銉笺伄銉囥兗銈裤亴寰屻仹浣滄垚銇欍倠
         } else {
@@ -402,6 +408,8 @@
          ,PricebookEntry.Product2.Intra_Trade_Foreign_RMB__c
          //NoDiscount 閲戦锛圲SD锛�
          ,PricebookEntry.Product2.NoDiscount_Foreign__c 
+         //fy 棰勭暀浜у搧鏍囪瘑
+         ,PricebookEntry.Product2.LastbuyProductFLG__c
          ,Quote.Opportunity.Trade__c 
          //澶栬锤澶氬勾淇� 2021/01/04 绮剧悽鎶�鏈� wql end
 
@@ -411,6 +419,7 @@
 
          From QuoteLineItem where Quoteid = :quoId Order by Item_Order__c, Id];
       String copyQuoId = System.currentPageReference().getParameters().get('copyid');
+      system.debug('copyid2++++++++++++'+copyQuoId);
       CheckItem = items;
 
       if (copyQuoId == null) {
@@ -897,6 +906,8 @@
                                //澶栬锤澶氬勾淇� 2021/01/04 绮剧悽鎶�鏈� wql start
                                //缁翠慨鍚堝悓鎶ヤ环锛圲SD锛�
                                ,Repair_Contract_USD__c
+                               //fy 棰勭暀浜у搧鏍囪瘑
+                               ,LastbuyProductFLG__c
                                //璁℃彁閲戦锛堜笉鍚◣锛孶SD锛�
                                ,Intra_Trade_Foreign_RMB__c
                                //NoDiscount 閲戦锛圲SD锛�
@@ -1774,7 +1785,24 @@
 
       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;
+        }
+      }
+      //20220214 fy lastbuy end
       // 2022-01-12 ssm 鎶ヤ环璁$畻check
       if (checkIsQuoteTrial()) {
         errorflg = true;
@@ -2073,7 +2101,9 @@
       // 銇撱亾銈掍慨姝c仐銇熴倝銆丯FM007.trigger銈傝纰鸿獚
       prd2LatestValMap = new Map<Id, Product2>();
       integer cntPrd2 = 0;
-      for (Product2 prd2 : [Select Id, Estimation_Entry_Possibility__c, SFDA_Status__c, Packing_list_manual__c
+      for (Product2 prd2 : [Select Id, Estimation_Entry_Possibility__c, SFDA_Status__c, Packing_list_manual__c,
+      //fy 棰勭暀浜у搧鏍囪瘑
+      LastbuyProductFLG__c
                             From Product2 Where Id IN :product2Ids]) {
         cntPrd2 = cntPrd2   +   1;
         if (prd2.Estimation_Entry_Possibility__c != '鈼�') {
@@ -2528,6 +2558,7 @@
   //SWAG-C9JCS8 銆愬鎵樸�戙�愮揣鎬ャ�戣浠稧Z-SP-GD0757135鎶ヤ环鍗曢棶棰� fy end
 
   public boolean dataEntry() {
+    system.debug('activities++++----****3'+activities);
     //SWAG-C9JCS8 銆愬鎵樸�戙�愮揣鎬ャ�戣浠稧Z-SP-GD0757135鎶ヤ环鍗曢棶棰� fy start
     // Boolean ifdecide=false;
     // List<Opportunity> oppsde = [Select Id, Estimation_Decision__c From Opportunity Where Id = : oppId];
@@ -2572,6 +2603,7 @@
         errormessage = System.Label.Error_Message3;
       }
       //decimal temSalesAmount1 = 0; // 2018/09/28 CHAN-B4YAB8 缁忛攢鍟嗗皬璁″悎璁� end
+      system.debug('activities++++----****2'+activities);
       for (QELine a : activities) {
         if ((a.Asset_Model != null) && (a.Asset_Model != '')) {
           if (a.PageObject.Quantity__c == null || a.PageObject.Quantity__c == 0) {
@@ -2773,6 +2805,7 @@
 
       // 20150302 jo 瑕嬬銇富鏈哄畨瑁呭湴鐐广倰瑷畾
       List<String> pIds = new List<String>();
+      system.debug('activities++++----****1'+activities);
       for (QELine s : activities) {
         if (String.isBlank(s.PageObject.Id__c) == false) {
           pIds.add(s.PageObject.Id__c);
@@ -2849,6 +2882,7 @@
       //Sap閫佷俊,Print銇悎銈忋仜銇�1锝�
       Integer i = 1;
       if (activities.size() > 0) {
+        system.debug('activities++++----****'+activities);
         for (QELine s : activities) {
           if (s.Asset_Model != null && s.Asset_Model != '') {
             if (s.pageObject.PricebookEntryId != null) {
@@ -2894,6 +2928,7 @@
           }
         }
         System.debug('start save quote lines at: ' + System.currentTimeMillis());
+        system.debug('qlist+++---+++'+qlist);
         insert qlist;
         System.debug('finished save quote lines at: ' + System.currentTimeMillis());
 
@@ -3104,7 +3139,90 @@
     }
     return true;
   }
+  //lastbuy  2022/2/9 fy start
+  public boolean ReservedProductVerification() {
 
+    filg=true;
+    Map<string,QuoteLineItem> quotlinitMap = new Map<string,QuoteLineItem>();
+    List<Id> lastProductFLGListId = new List<Id>();
+    List<QuoteLineItem> lastProductFLGList = new List<QuoteLineItem>();
+    List<QuoteLineItem> act = new List<QuoteLineItem>();
+    List<QuoteLineItem> act2 = new List<QuoteLineItem>();
+    for(QELine aaa :activities){
+      if(aaa.pageObject.PricebookEntry.Product2Id!=null){
+        act.add(aaa.pageObject);
+      }
+    }
+    act2=act.deepClone();
+    Map<String,QuoteLineItem> map1 = new Map<String,QuoteLineItem>();
+    System.debug('activities1111111111112涓烘墍褰撲负澶氬!!!'+activities);
+    integer i =0;
+    for(QuoteLineItem pspsc :act2){
+      if(pspsc.PricebookEntry.Product2Id!=null){
+        if(map1.containsKey(pspsc.PricebookEntry.Product2Id)){
+          QuoteLineItem quoteLine = map1.get(pspsc.PricebookEntry.Product2Id);
+          quoteLine.Quantity__c =quoteLine.Quantity__c+pspsc.Quantity__c;
+          map1.put(pspsc.PricebookEntry.Product2Id,quoteLine);
+        }else{
+          map1.put(pspsc.PricebookEntry.Product2Id,pspsc);
+        }
+        System.debug('34499879!!!'+activities);
+      }
+    }
+    System.debug('3434343!!!'+activities);
+    System.debug('5656565!!!'+map1);
+    for (QuoteLineItem value : map1.values()) {
+      if(value.PricebookEntry.Product2.LastbuyProductFLG__c){
+        lastProductFLGListId.add(value.PricebookEntry.Product2Id);
+        quotlinitMap.put(value.PricebookEntry.Product2Id,value);
+        lastProductFLGList.add(value);
+      }
+    }
+    System.debug('activities++++!!!'+activities);
+    System.debug('activities!!!'+map1.values());
+    System.debug('oppId!!!'+oppId);
+    System.debug('lastProductFLGList!!!'+lastProductFLGListId);
+    if(lastProductFLGListId!=null&&lastProductFLGListId.size()!=0){
+        List<LastbuyProduct__c> LastbuyObjList=[select id,LastbuyQuantity__c,InquiryCode__c,ProductName__c,effectiveFLG__c from LastbuyProduct__c where InquiryCode__c= : oppId and ProductName__c in :lastProductFLGListId and effectiveFLG__c = true];
+        Map<string,LastbuyProduct__c> LastbuyObjMap = new Map<string,LastbuyProduct__c>();
+        System.debug('LastbuyObjList+++++!!!'+LastbuyObjList);
+        if(LastbuyObjList!=null&&LastbuyObjList.size()!=0){
+          for(LastbuyProduct__c lastbuypr :LastbuyObjList){
+            LastbuyObjMap.put(lastbuypr.ProductName__c,lastbuypr);
+          }
+        }else{
+          flglastbuy=1;
+          filg=false;
+          return filg;
+        }
+        System.debug('LastbuyObjMap!!!'+LastbuyObjMap);
+        System.debug('lastProductFLGList+++++++!!!'+lastProductFLGList);
+        if(lastProductFLGList!=null&&lastProductFLGList.size()!=0){
+          for(QuoteLineItem lastbuypr :lastProductFLGList){
+            Decimal quoteLItemNum=0;
+            if(LastbuyObjMap.containsKey(lastbuypr.PricebookEntry.Product2Id)){
+                quoteLItemNum=LastbuyObjMap.get(lastbuypr.PricebookEntry.Product2Id).LastbuyQuantity__c;
+                System.debug('quoteLItemNum!!!'+quoteLItemNum);
+                System.debug('lastbuypr.pageObject.Quantity__c+++!!!'+lastbuypr.Quantity__c);
+                if(lastbuypr.Quantity__c>quoteLItemNum){
+                  errorProductmodel=lastbuypr.Asset_Model_No__c;
+                  flglastbuy=2;
+                  filg=false;
+                  break;
+                }
+            }else{
+              errorProductmodel=lastbuypr.Asset_Model_No__c;
+              flglastbuy=3;
+              filg=false;
+              break;
+            }
+          }
+        }
+    }
+    system.debug('filg====='+filg);
+    return filg;
+  }
+  //lastbuy  2022/2/9 fy end
   //opp銇敾闈€伄鍊ゃ倰瑷畾
   private void setOppFromOppInfo() {
     opp.Wholesale_Price__c = oppInfo.Wholesale_Price;
diff --git a/force-app/main/default/classes/NewQuoteEntryControllerTest.cls b/force-app/main/default/classes/NewQuoteEntryControllerTest.cls
index 54c0591..3ac80b9 100644
--- a/force-app/main/default/classes/NewQuoteEntryControllerTest.cls
+++ b/force-app/main/default/classes/NewQuoteEntryControllerTest.cls
@@ -1151,7 +1151,7 @@
 
             //System.Test.startTest();
             //##銉氥兗銈搞儑銉愩儍銈般偦銈偡銉с兂銆�闁嬪
-            PageReference page = new PageReference('/apex/NewQuoteEntry');
+            PageReference page = new PageReference('/apex/NewQuoteEntry?copyid='+quote.Id);
             System.Test.setCurrentPage(page);
 
             Apexpages.currentPage().getParameters().put('oppid', opp.id);
@@ -1301,6 +1301,7 @@
             product1.Foreign_Trade_Cost_US_1__c = 200;
             product1.Foreign_Trade_Cost_US_Date1__c  =date.today();
             product1.Foreign_Trade_Cost_US_End_Date1__c=date.today().addDays(1);
+            product1.LastbuyProductFLG__c=true;
             insert product1;
             system.debug('##product.Id=[' + product1.Id + ']' );
 
@@ -1321,6 +1322,7 @@
             product2.Foreign_Trade_Cost_US_1__c = 200;
             product2.Foreign_Trade_Cost_US_Date1__c  =date.today();
             product2.Foreign_Trade_Cost_US_End_Date1__c=date.today().addDays(1);
+            product2.LastbuyProductFLG__c=true;
             insert product2;
             system.debug('##product1.Id=[' + product1.Id + ']' );
             system.debug('##product2.Id=[' + product2.Id + ']' );
@@ -1333,7 +1335,7 @@
             pE.CurrencyIsoCode = 'USD';
             //pE.
             insert pE;
-
+            system.debug('##pE=[' + pE + ']' );
             PricebookEntry pE2 = new PricebookEntry( Pricebook2Id = pricebook.Id, Product2Id = product2.Id);
 
             pE2.UnitPrice = 0;
@@ -1374,6 +1376,77 @@
             quote.Agent1_Agent2_Price__c = 10;
 
             insert quote;
+            //棰勭暀浜у搧
+            QuoteLineItem qa=new QuoteLineItem();
+            qa.Name__c = '銉嗐偣銉堝晢鍝�';
+            qa.QuoteId = quote.Id;
+            qa.Quantity = 3;
+            qa.Cost_Subtotal__c = 0;
+            qa.Cost__c = 200;
+            qa.UnitPrice = 10;
+            qa.PricebookEntryId = pE.Id;
+            qa.Id__c=pE.Product2Id;
+    //        target.TotalPrice = 100;
+            
+            insert qa;
+            QuoteLineItem qb=new QuoteLineItem();
+            qb.Name__c = '銉嗐偣銉堝晢鍝�';
+            qb.QuoteId = quote.Id;
+            qb.Quantity = 3;
+            qb.Cost_Subtotal__c = 0;
+            qb.Cost__c = 200;
+            qb.UnitPrice = 10;
+            qb.PricebookEntryId = pE2.Id;
+            qb.Id__c=pE2.Product2Id;
+
+            List<Id> QuoteLineItemList = new List<Id>();
+            QuoteLineItemList.add(qa.Id);
+            QuoteLineItemList.add(qb.Id);
+            // List<QuoteLineItem> QuoteLineItemListss=[Select Id,Id__c, Asset_Model_No__c, SFDA_Status__c, Product_Sales_Possibility__c,
+            // //CHAN-BKU3XH 妫�鏌ユ槸鍚﹀瓨鍦ㄤ笉鏄悓涓�涓緵閿�鍟嗗悕绉� 绮剧悽鎶�鏈� 2020/02/17 Start 澧炲姞瀛楁 涓嶅彲鍙栨秷澶氬勾淇�&&澧炲姞妫�绱㈤樋瑗胯禌澶� 2020/09/10
+            //  Name__c, BSS_Category__c, Quote.Quote_Print_Date__c,PricebookEntry.Product2.VenderName__c,PricebookEntry.Product2.CanNotCancelledGurantee__c,PricebookEntry.Product2.Is_DangerousChemicals__c,
+            //  //CHAN-BKU3XH 妫�鏌ユ槸鍚﹀瓨鍦ㄤ笉鏄悓涓�涓緵閿�鍟嗗悕绉� 绮剧悽鎶�鏈� 2020/02/17 end 澧炲姞瀛楁
+            //  Qty_Unit__c, Cost__c, UnitPrice__c, ListPrice__c, Quantity, TotalPrice__c,
+            //  PricebookEntry.Product2.SFDA_Status__c, ProductCode__c, Product_Cost__c, Product_ListPrice__c, PricebookEntry.Product2.Sales_Possibility__c, PricebookEntry.Product2.Name,
+            //  PricebookEntryId, PricebookEntry.Product2Id, UnitPrice_Page__c, PricebookEntry.Product2.Packing_list_manual__c, PricebookEntry.Product2.StorageStatus__c
+            //  , AgencyUnitPrice__c, AgencySubtotal__c, Present__c // CHAN-B4YAB8 2018/9/29 璧犻�併�佺粡閿�鍟嗗崟浠峰拰灏忚
+            //  //  澶氬勾淇濅慨 start
+            //  //ET淇冮攢鏍囪 start
+            //  , multiYearWarranty__c , If_Cancel_Guarantee__c , GuaranteePeriod__c,
+            //  //ET淇冮攢鏍囪 end
+            //  ServicePrice__c , GuranteePrice__c, ProductEntend_gurantee_period_all__c,
+            //  ProductGuranteePrice__c,  GuranteeType__c,
+            //  warrantyType__c, productServicePrice__c, NoDiscountTotal__c
+            //  , provistonPeriod__c
+            //  , PricebookEntry.Product2.Entend_gurantee_period_all__c
+            //  , PricebookEntry.Product2.Intra_Trade_Gurantee_RMB__c
+            //  , PricebookEntry.Product2.Intra_Trade_Service_RMB__c
+            //  , PricebookEntry.Product2.GuranteeType__c
+            //  // 缁翠慨鍚堝悓鎶ヤ环
+            //  , PricebookEntry.Product2.Maintenance_Price_Year__c
+            //  , Maintenance_Price_Year__c
+            //  // 澶氬勾淇濅慨 end
+            //  //澶栬锤澶氬勾淇� 2021/01/04 绮剧悽鎶�鏈� wql start
+            //  //缁翠慨鍚堝悓鎶ヤ环锛圲SD锛�
+            //  ,PricebookEntry.Product2.Repair_Contract_USD__c
+            //  //璁℃彁閲戦锛堜笉鍚◣锛孶SD锛�
+            //  ,PricebookEntry.Product2.Intra_Trade_Foreign_RMB__c
+            //  //NoDiscount 閲戦锛圲SD锛�
+            //  ,PricebookEntry.Product2.NoDiscount_Foreign__c 
+            //  ,Quote.Opportunity.Trade__c 
+            //  //澶栬锤澶氬勾淇� 2021/01/04 绮剧悽鎶�鏈� wql end
+    
+            //  //SFDC鍋滄棰勮 lt 20211009 start
+            //  ,PricebookEntry.Product2.Estimated_ConsumptionDueDate__c
+            //  //SFDC鍋滄棰勮 lt 20211009 end
+            //  from QuoteLineItem where Id in :QuoteLineItemList];
+            
+            LastbuyProduct__c lbp=new LastbuyProduct__c();
+            lbp.effectiveFLG__c=true;
+            lbp.InquiryCode__c = opp.Id;
+            lbp.ProductName__c = product1.Id;
+            lbp.LastbuyQuantity__c=2;
+            insert lbp;
 
             // 銈汇儍銉堝搧
             Product_Set__c ps1 = new Product_Set__c();
@@ -1438,6 +1511,7 @@
             controller.quo.Quote_Expiration_Date__c = date.today();
             controller.oppId = opp.id;
 //        controller.PricebookId = pricebook.id;
+            
             NewQuoteEntryController.QELine act = new NewQuoteEntryController.QELine(oli, 0);
 
             act.Asset_Model = '11111';
@@ -1446,15 +1520,30 @@
             act.pageObject.UnitPrice = 100;
             act.pageObject.Subtotal__c = 1000;
             act.pageObject.PricebookEntryId = pE2.id;
+            system.debug('##act.pageObject=[' + act.pageObject + ']' );
+            Integer ge=0;
             List<NewQuoteEntryController.QELine> actList = new List<NewQuoteEntryController.QELine>();
             actList.add(act);
+            // for(QuoteLineItem qqq : QuoteLineItemListss){
+            //     NewQuoteEntryController.QELine act2 = new NewQuoteEntryController.QELine(qqq, 0,'');
+            //     if(ge== 0){
+            //         act2.Asset_Model = '11111';
+            //     }else if(ge== 1){
+            //         act2.Asset_Model = '22222';
+            //     }
+            //     act2.pageObject=qqq;
+            //     actList.add(act2);
+            // }
             controller.activities = actList;
             controller.dataCheck();
             controller.quo.OCM_Agent1_Price_Page__c = 1243;
             controller.dataEntry();
             controller.QuoteDecision();
             controller.Print();
-
+            //鎶ラ敊
+            // controller.ReservedProductVerification();
+            // String[] azzz=new String[] {'1','2'};
+            // System.debug(azzz[3]);
             actList = new List<NewQuoteEntryController.QELine>();
             actList.add(act);
             controller.activities = actList;
@@ -2401,7 +2490,7 @@
             product2.Manual_Entry__c = false;
             product2.Asset_Model_No__c = '11112';
             product2.MDM_Model_No__c = '11112';
-//        product2.ProductCode = 'OTV-SP1H-NA-12E';
+        //        product2.ProductCode = 'OTV-SP1H-NA-12E';
             insert product2;
             system.debug('##product1.Id=[' + product1.Id + ']' );
             system.debug('##product2.Id=[' + product2.Id + ']' );
@@ -2427,7 +2516,7 @@
             OpportunityLineItem oli = new OpportunityLineItem();
             //target.QuoteId = input.Id;
             oli.Quantity = 1;
-//    target.TotalPrice = 100;
+        //    target.TotalPrice = 100;
             oli.UnitPrice = 10;
             oli.PricebookEntryId = pE.Id;
             oli.OpportunityId = opp.id;
@@ -2486,13 +2575,13 @@
             controller.oppId = null;
             controller.quoId = null;
             controller.enableSales = false;
-//System.Test.startTest();
+        //System.Test.startTest();
             controller.init();
             controller.select_index = 0;
             controller.Product_text = product1.id;
             controller.setProduct_text = ps1.id;
             controller.setProductEntry();
-//        controller.settingProduct2();
+        //        controller.settingProduct2();
             controller.OppReflection();
             controller.PriceStatusUpdate();
             controller.SalesId1 = sellerA.id;
@@ -2522,7 +2611,7 @@
             controller.quo.Quote_Adjust_Amount__c = 10;
             controller.quo.Quote_Expiration_Date__c = date.today();
             controller.oppId = opp.id;
-//        controller.PricebookId = pricebook.id;
+        //        controller.PricebookId = pricebook.id;
             NewQuoteEntryController.QELine act = new NewQuoteEntryController.QELine(oli, 0);
 
             act.Asset_Model = '11111';
@@ -2549,7 +2638,7 @@
             controller.excelImport();
             Add_Report.getProduct2(product2.id);
 
-//   QELine(QELine tmp, Integer i) 闁㈡暟銈掋儐銈广儓
+        //   QELine(QELine tmp, Integer i) 闁㈡暟銈掋儐銈广儓
             actList = new List<NewQuoteEntryController.QELine>();
             //act.Asset_Model = null;   //20211025
             act.Asset_Model = '123';   //20211025
@@ -2560,7 +2649,7 @@
             System.Test.stopTest();
             Add_Report.getProduct2(product2.id);
 
-//  dataCheckDecide闁㈡暟銈掋儊銈с儍銈�
+        //  dataCheckDecide闁㈡暟銈掋儊銈с儍銈�
             controller.enableSales = true;
             controller.QuoteDecision = true;
             controller.QuoteDecision();
@@ -2576,5 +2665,120 @@
         }
     }
     //20211015 lt end
+    static testMethod void dataEntryTest2() {
+        RecordType recByoin = [select id from RecordType where IsActive = true and SobjectType = 'Account' and Name = :RC_BYOUIN limit 1];
+        Account hospital = new Account(Name = '銉嗐偣銉堢梾闄�');
+        hospital.RecordTypeId = recByoin.id;
+        hospital.Site = '銉嗐偣銉堢梾闄㈤儴闁�';
+        hospital.Alias_Name2__c = '銉嗐偣銉堢梾闄㈠垾鍚�';
+        insert hospital;
 
+        RecordType recHanbaiten = [select id from RecordType where IsActive = true and SobjectType = 'Account' and Name = :RC_HANBAOITEN limit 1];
+        Account sellerA = new Account(Name = '璨╁2搴�');
+        sellerA.RecordTypeId = recHanbaiten.id;
+        sellerA.Hospital__c = hospital.id;
+        insert sellerA;
+
+        Product2 product2 = new Product2( Name = '11112');
+        product2.SFDA_Status__c = '涓嶈';
+        product2.Intra_Trade_List_RMB_1__c = 100;
+        product2.Intra_Trade_List_RMB_Date1__c = date.today();
+        product2.Intra_Trade_List_RMB_End_Date1__c = date.today().addDays(1);
+        product2.Intra_Trade_Cost_RMB_1__c = 200;
+        product2.Intra_Trade_Cost_RMB_Date1__c = date.today();
+        product2.Intra_Trade_Cost_RMB_End_Date1__c = date.today().addDays(1);
+        product2.Manual_Entry__c = false;
+        product2.Asset_Model_No__c = '11112';
+        product2.MDM_Model_No__c = '11112';
+        insert product2;
+
+        PricebookEntry pE2 = new PricebookEntry( Pricebook2Id = pricebook.Id, Product2Id = product2.Id);
+        pE2.UnitPrice = 0;
+        pE2.IsActive = true;
+        pE2.UseStandardPrice = false;
+        pE2.CurrencyIsoCode = CurrencyIso;
+        insert pE2;
+
+        Opportunity opp1 = buildOppInstance( trade, CurrencyIso);
+        opp1.Trade__c = trade;
+        opp1.owner_not_automatically_update__c = true;
+        opp1.Sales_Root__c = 'AAA';
+        opp1.Pricebook2Id = pricebook.id;
+        opp1.Estimation_Decision__c=true;
+        insert opp1;
+
+        OpportunityLineItem oli = new OpportunityLineItem();
+        oli.Quantity = 1;
+        oli.UnitPrice = 10;
+        oli.PricebookEntryId = pE2.Id;
+        oli.OpportunityId = opp1.id;
+        oli.Cost__c = 100;
+        insert oli;
+
+        NewQuoteEntryController.QELine act = new NewQuoteEntryController.QELine(oli, 0);
+        act.Asset_Model = '';
+        act.pageObject.Quantity__c = 10;
+        act.pageObject.UnitPrice = 100;
+        act.pageObject.Subtotal__c = 1000;
+        act.pageObject.PricebookEntryId = pE2.id;
+        List<NewQuoteEntryController.QELine> actList = new List<NewQuoteEntryController.QELine>();
+        actList.add(act);
+
+        NewQuoteEntryController controller = new NewQuoteEntryController();
+        controller.oppId=opp1.Id;
+        controller.enableSales=true;
+        Quote quote=new Quote();
+        quote.QuoteName__c = '銉嗐偣銉堣绌�';
+        quote.Dealer_Final_Price_Page__c = 100;
+        quote.Quote_Adjust_Amount_Page__c = 10;
+        quote.Quote_Expiration_Date__c = date.today();
+        quote.Agency1__c = sellerA.id;
+        quote.OCM_Agent1_Price_Page__c = 1243;
+        quote.OpportunityId = opp1.id;
+        quote.Name ='銉嗐偣銉堣绌�';
+        quote.Pricebook2Id = pricebook.id;
+        quote.Quote_No__c = quoteNo;
+        insert quote;
+        system.debug('act2++++++++++'+pE2);
+
+        QuoteLineItem item02 = new QuoteLineItem();
+        item02.Name__c = '銉嗐偣銉堝晢鍝�';
+        item02.QuoteId = quote.Id;
+        item02.Quantity = 1;
+        item02.Cost_Subtotal__c = 0;
+        item02.Cost__c = 200;
+        item02.UnitPrice = 10;
+        item02.PricebookEntryId = pE2.Id;
+        insert item02;
+
+        // quote.QuoteName__c = '銉嗐偣銉堣绌�';
+        // quote.QuoteName__c = '銉嗐偣銉堣绌�';
+        controller.quo=quote;
+        NewQuoteEntryController.QuoteBean qbs=new NewQuoteEntryController.QuoteBean();
+        qbs.Quote_Adjust_Calculate = 100;
+        controller.qb = qbs;
+        controller.activities=actList;
+        controller.opp=opp1;
+        controller.dataEntry();
+
+        NewQuoteEntryController.QELine act2 = new NewQuoteEntryController.QELine(oli, 0);
+        act2.Asset_Model = '111111';
+        act2.pageObject.Quantity__c = 1;
+        act2.pageObject.UnitPrice_Page__c = 100;
+        act2.pageObject.AgencyUnitPrice__c = 100;
+        act2.pageObject.PricebookEntryId = pE2.id;
+        List<NewQuoteEntryController.QELine> actList2 = new List<NewQuoteEntryController.QELine>();
+        actList2.add(act2);
+        system.debug('act2++++++++++'+act2);
+        controller.activities=actList2;
+        controller.standardPricebook = pricebook;
+        for(NewQuoteEntryController.QELine aaa :controller.activities){
+            aaa.Asset_Model = '111111';
+        }
+        system.debug('activities++++++++++'+controller.activities);
+        NewQuoteEntryController.oppInfo opps = new NewQuoteEntryController.oppInfo(opp1);
+
+        controller.oppInfo = opps;
+        controller.dataEntry();
+    }
 }
\ No newline at end of file
diff --git a/force-app/main/default/classes/NewQuoteIraiController.cls b/force-app/main/default/classes/NewQuoteIraiController.cls
index 6524d45..bb3c8ea 100644
--- a/force-app/main/default/classes/NewQuoteIraiController.cls
+++ b/force-app/main/default/classes/NewQuoteIraiController.cls
@@ -8,6 +8,12 @@
     public Boolean productStatusUpdated {get;set;}               // 鐘舵厠鏇存柊銆亄!$Label.Status_Update} 銈掓娂涓嬨仐銇熴亱銇┿亞銇�
     public Boolean changedAfterPrint {get;set;}                  // true 銇牬鍚堛�佺敾闈€伀 confirm 銉°儍銈汇兗銈搞亴琛ㄧず銇椼伨銇欍�俼uoId銈掓柊銇椼亜insert銆傚垽瀹氥伅js銇仸瀹熸柦
 
+    //lastbuy  2022/3/10 fy start
+    public Boolean filg { get; set; }
+    public Integer flglastbuy { get; set; }
+    public String errorProductmodel { get; set; }
+    //lastbuy  2022/3/10 fy end
+
     public String excel_text {get;set;}
     public Integer select_index {get;set;}                       // excelImport灏傜敤銇с仚銇屻�乯s銇仸鍒跺尽銇欍倠銇撱仺銇仾銈嬨伄銇с�乀ODO katsu 鍓婇櫎浜堝畾
     public String Product_text {get;set;}
@@ -98,6 +104,14 @@
 
         // 璇环id
         oppid = System.currentPageReference().getParameters().get('oppid');
+        QuoteIrai__c quoteiraiobj = new QuoteIrai__c();
+        if(oppid==null&&tenderid==null&&quoId!=null&&leadid==null&&agencyoppid==null){
+            quoteiraiobj = [select id,Note__c from QuoteIrai__c where id=:quoId];
+            if(quoteiraiobj.Note__c!=null){
+                String[] quosub=quoteiraiobj.Note__c.split('/');
+                oppid=quosub[quosub.size()-1];
+            }
+        }
         system.debug('oppid:++++'+oppid);
         // 鎶ヤ环id
         String oppquoid = System.currentPageReference().getParameters().get('oppquoid');
@@ -208,8 +222,8 @@
                     // 宸茬粡瀛樺湪鎶ヤ环
                     if (String.isNotBlank(oppquoid)) {
                         // 鎶ヤ环鍟嗗搧鍙栧緱
-                        // CHAN-BHNBX6 2019/11/20 START
-                        List<QuoteLineItem> qlis = [select id,PricebookEntry.Product2Id,Quantity__c,CurrencyIsoCode,GuaranteePeriod__c from QuoteLineItem where QuoteId = :oppquoid];
+                        // CHAN-BHNBX6 2019/11/20 START//fy lastbuy 20220310 PricebookEntry.Product2.LastbuyProductFLG__c
+                        List<QuoteLineItem> qlis = [select id,PricebookEntry.Product2Id,PricebookEntry.Product2.LastbuyProductFLG__c,Quantity__c,CurrencyIsoCode,GuaranteePeriod__c from QuoteLineItem where QuoteId = :oppquoid];
                         // CHAN-BHNBX6 2019/11/20 END
                         List<String> productids = new List<String>();
                         for (QuoteLineItem qli : qlis) {
@@ -325,9 +339,9 @@
                         FROM QuoteIrai__c Where Id =:quoId];
             // CHAN-BJQ4VZ 绮剧悽鎶�鏈� 2019/12/11 End
             // CHAN-BHNBX6 2019/11/20 START
-            List<QuoteIraiLineItem__c> items = 
+            List<QuoteIraiLineItem__c> items = //lastbuy  2022/3/10 fy start LastbuyProductFLG__c
                 [Select Id,Asset_Model_No__c,SFDA_Status__c,Name__c,BSS_Category__c,QuoteIrai__r.Quote_Print_Date__c,
-                    Qty_Unit__c,Quantity__c,Product2__r.SFDA_Status__c,ProductCode__c,ListPrice__c,
+                    Qty_Unit__c,Quantity__c,Product2__r.SFDA_Status__c,ProductCode__c,ListPrice__c,Product2__r.LastbuyProductFLG__c,
                     Product2__r.Sales_Possibility__c,Product2__r.Name,Product2__c,ServicePrice__c,NoDiscountTotal__c,GuaranteePeriod__c
                     From QuoteIraiLineItem__c where QuoteIrai__c = :quoId Order by Item_Order__c, Id];
             // CHAN-BHNBX6 2019/11/20 END
@@ -1160,6 +1174,31 @@
     }
 
     public boolean dataCheck(){
+         //20220310 fy lastbuy start 
+         if (quoId!=null){
+            if(!ReservedProductVerification()){
+                if(flglastbuy==1){
+                  errorflg = true;
+                  errormessage =  '棰勭暀浜у搧琛ㄤ腑娌℃湁璇ヨ浠凤紝璇烽�氳繃鏈儴绐楀彛鑱旂郴钀ヤ笟绠$悊璇�' ;
+                  return false;
+                }else if(flglastbuy==2){
+                  errorflg = true;
+                  errormessage =  errorProductmodel+'浜у搧鏁伴噺涓嶅彲瓒呰繃浜у搧棰勭暀鏁伴噺' ;
+                  return false;
+                }else if(flglastbuy==3){
+                  errorflg = true;
+                  errormessage =  '棰勭暀浜у搧'+errorProductmodel+'鏈綍鍏ラ鐣欎骇鍝佽〃';
+                  return false;
+                }
+                // else if(flglastbuy==4){
+                //     errorflg = true;
+                //     errormessage =  '璇ユ姤浠峰鎵樺瓨鍦ㄩ鐣欎骇鍝�';
+                //     return false;
+                // }
+              }
+        }
+      
+      //20220310 fy lastbuy end
         system.debug('鈼嬧棆鈼嬧棆鈼嬧棆鈼嬧棆鈼嬧棆鈼嬧棆Welcome to dataCheck class!!鈼嬧棆鈼嬧棆鈼嬧棆鈼嬧棆鈼嬧棆鈼嬧棆');
         errorflg = false;
         errormessage = null;
@@ -1184,8 +1223,8 @@
             if (error == true){
                 if (quoId != null) {
                     Map<String,QuoteIraiLineItem__c> itemmap = new Map<String,QuoteIraiLineItem__c>();
-                    for (QuoteIraiLineItem__c item : [
-                        Select Id,Product2__r.SFDA_Status__c,Product2__r.Name,Product2__c
+                    for (QuoteIraiLineItem__c item : [//lastbuy  2022/3/10 fy start LastbuyProductFLG__c
+                        Select Id,Product2__r.SFDA_Status__c,Product2__r.Name,Product2__c,Product2__r.LastbuyProductFLG__c
                         From QuoteIraiLineItem__c where QuoteIrai__c = :quoId Order by Item_Order__c, Id]) {
                         itemmap.put(item.Product2__c,item);
                     }
@@ -1399,6 +1438,107 @@
 
         return true;
     }
+     //lastbuy  2022/3/10 fy start
+  public boolean ReservedProductVerification() {
+    filg=true;
+    Map<string,QuoteIraiLineItem__c> quotlinitMap = new Map<string,QuoteIraiLineItem__c>();
+    List<Id> lastProductFLGListId = new List<Id>();
+    List<QuoteIraiLineItem__c> lastProductFLGList = new List<QuoteIraiLineItem__c>();
+    List<QuoteIraiLineItem__c> act = new List<QuoteIraiLineItem__c>();
+    List<QuoteIraiLineItem__c> act2 = new List<QuoteIraiLineItem__c>();
+    Map<string,string> actMap = new Map<string,string>();
+    for(QELine aaa :activities){
+        System.debug('131313131!!!'+aaa.pageObject.Product2__r.LastbuyProductFLG__c);
+      if(aaa.pageObject.Product2__c!=null&&aaa.pageObject.Quantity__c!=null){
+        actMap.put(aaa.pageObject.Product2__c,aaa.Asset_Model);
+        act.add(aaa.pageObject);
+      }
+    }
+    act2=act.deepClone();
+    Map<String,QuoteIraiLineItem__c> map1 = new Map<String,QuoteIraiLineItem__c>();
+    System.debug('activities1111111111112涓烘墍褰撲负澶氬!!!'+activities);
+    integer i =0;
+    for(QuoteIraiLineItem__c pspsc :act2){
+      if(pspsc.Product2__c!=null&&pspsc.Quantity__c!=null){
+        if(map1.containsKey(pspsc.Product2__c)){
+            QuoteIraiLineItem__c quoteLine = map1.get(pspsc.Product2__c);
+          quoteLine.Quantity__c =quoteLine.Quantity__c+pspsc.Quantity__c;
+          map1.put(pspsc.Product2__c,quoteLine);
+          System.debug('2222222!!!'+quoteLine);
+        }else{
+          map1.put(pspsc.Product2__c,pspsc);
+        }
+        System.debug('5555555!!!'+pspsc);
+        System.debug('34499879!!!'+activities);
+      }
+    }
+    System.debug('3434343!!!'+activities);
+    System.debug('5656565!!!'+map1);
+    List<Product2> productlist = [select id,LastbuyProductFLG__c from Product2 where id in:map1.keySet()];   
+    Map<String,boolean> productMap = new Map<String,boolean>();
+    System.debug('9999999666!!!'+productlist);
+    if(productlist!=null&&productlist.size()!=0){
+        for(Product2 product : productlist){
+            productMap.put(product.id,product.LastbuyProductFLG__c);
+        } 
+    }
+    for (QuoteIraiLineItem__c value : map1.values()) {
+      if(productMap.get(value.Product2__c)){
+        lastProductFLGListId.add(value.Product2__c);
+        quotlinitMap.put(value.Product2__c,value);
+        lastProductFLGList.add(value);
+      }
+    } 
+    System.debug('activities++++!!!'+activities);
+    System.debug('activities!!!'+map1.values());
+    System.debug('oppId!!!'+oppId);
+    System.debug('lastProductFLGList!!!'+lastProductFLGListId);
+    if(lastProductFLGListId!=null&&lastProductFLGListId.size()!=0){
+        // if(oppid==null){
+        //     flglastbuy=4;
+        //     filg=false;
+        //     return filg;
+        // }
+        List<LastbuyProduct__c> LastbuyObjList=[select id,LastbuyQuantity__c,InquiryCode__c,ProductName__c,effectiveFLG__c from LastbuyProduct__c where InquiryCode__c= : oppid and ProductName__c in :lastProductFLGListId and effectiveFLG__c = true];
+        Map<string,LastbuyProduct__c> LastbuyObjMap = new Map<string,LastbuyProduct__c>();
+        System.debug('LastbuyObjList+++++!!!'+LastbuyObjList);
+        if(LastbuyObjList!=null&&LastbuyObjList.size()!=0){
+          for(LastbuyProduct__c lastbuypr :LastbuyObjList){
+            LastbuyObjMap.put(lastbuypr.ProductName__c,lastbuypr);
+          }
+        }else{
+          flglastbuy=1;
+          filg=false;
+          return filg;
+        }
+        System.debug('LastbuyObjMap!!!'+LastbuyObjMap);
+        System.debug('lastProductFLGList+++++++!!!'+lastProductFLGList);
+        if(lastProductFLGList!=null&&lastProductFLGList.size()!=0){
+          for(QuoteIraiLineItem__c lastbuypr :lastProductFLGList){
+            Decimal quoteLItemNum=0;
+            if(LastbuyObjMap.containsKey(lastbuypr.Product2__c)){
+                quoteLItemNum=LastbuyObjMap.get(lastbuypr.Product2__c).LastbuyQuantity__c;
+                System.debug('quoteLItemNum!!!'+quoteLItemNum);
+                System.debug('lastbuypr.pageObject.Quantity__c+++!!!'+lastbuypr.Quantity__c);
+                if(lastbuypr.Quantity__c>quoteLItemNum){actMap.get(lastbuypr.Product2__c);
+                  errorProductmodel=actMap.get(lastbuypr.Product2__c);
+                  flglastbuy=2;
+                  filg=false;
+                  break;
+                }
+            }else{
+              errorProductmodel=actMap.get(lastbuypr.Product2__c);
+              flglastbuy=3;
+              filg=false;
+              break;
+            }
+          }
+        }
+    }
+    system.debug('filg====='+filg);
+    return filg;
+  }
+
     // CHAN-BJQ4VZ 绮剧悽鎶�鏈� 2019/12/11 Start
     public class QuoteBean {
     // 浜у搧鏍囧噯瀹氫环鎬婚
diff --git a/force-app/main/default/classes/NewQuoteIraiControllerTest.cls b/force-app/main/default/classes/NewQuoteIraiControllerTest.cls
index 5400331..5d3a9bf 100644
--- a/force-app/main/default/classes/NewQuoteIraiControllerTest.cls
+++ b/force-app/main/default/classes/NewQuoteIraiControllerTest.cls
@@ -57,6 +57,7 @@
         product1.Intra_Trade_Cost_RMB_1__c=10;
         product1.Intra_Trade_Cost_RMB_2__c=10;
         product1.SFDA_Expiration_Date__c= Date.today();
+        product1.LastbuyProductFLG__c = true;
         //CHAN-BHNBX6 2019/11/25 START
         // product1.Intra_Trade_Service_RMB__c = 2150;
         // product1.Entend_gurantee_period_all__c=1;
@@ -262,7 +263,7 @@
         // CHAN-BHNBX6 2019/11/25 END
         controller.Save();
         
-        QuoteIrai__c[] irais = [select Id, Name, IraiUser__c, LastIraiUser__c, IraiSubject__c, CurrencyIsoCode from QuoteIrai__c where IraiSubject__c = '銉嗐偣銉�01'];
+        QuoteIrai__c[] irais = [select Id, Name,Note__c, IraiUser__c, LastIraiUser__c, IraiSubject__c, CurrencyIsoCode from QuoteIrai__c where IraiSubject__c = '銉嗐偣銉�01'];
         // CHAN-BHNBX6 2019/11/25 START
         QuoteIraiLineItem__c[] items = [select Id, Asset_Model_No__c, SFDA_Status__c, Name__c,Quantity__c, ListPrice__c, ProductCode__c,Product2__c,ServicePrice__c,GuaranteePeriod__c from QuoteIraiLineItem__c where QuoteIrai__c = :irais[0].Id order by Item_Order__c];
         // CHAN-BHNBX6 2019/11/25 END
@@ -308,7 +309,7 @@
         
         controller.quoteIrai();
         
-        QuoteIrai__c[] irais2 = [select Id, Name, IraiUser__c, LastIraiUser__c, IraiSubject__c, CurrencyIsoCode from QuoteIrai__c where IraiSubject__c = '銉嗐偣銉�01'];
+        QuoteIrai__c[] irais2 = [select Id,Note__c, Name, IraiUser__c, LastIraiUser__c, IraiSubject__c, CurrencyIsoCode from QuoteIrai__c where IraiSubject__c = '銉嗐偣銉�01'];
         //System.assertEquals('_銈点兂銉栥儶銉冦偢 銇�', irais2[0].LastIraiUser__c);
         
         Task[] tasks = [select Id,Subject,OwnerId,Description,ActivityDate,QuoteIraiId__c
@@ -320,7 +321,7 @@
         
         controller.activities[0].pageObject.Quantity__c = 1;
         controller.Save();
-        QuoteIrai__c[] irais3 = [select Id, Name, IraiUser__c, LastIraiUser__c, IraiSubject__c, CurrencyIsoCode from QuoteIrai__c where IraiSubject__c = '銉嗐偣銉�01'];
+        QuoteIrai__c[] irais3 = [select Id,Note__c, Name, IraiUser__c, LastIraiUser__c, IraiSubject__c, CurrencyIsoCode from QuoteIrai__c where IraiSubject__c = '銉嗐偣銉�01'];
         //System.assertEquals(null, irais3[0].LastIraiUser__c);
     }
     
@@ -359,6 +360,7 @@
         product1.Foreign_Trade_Cost_US_End_Date2__c=Date.today().addDays(1);
         product1.Foreign_Trade_Cost_US_1__c=10;
         product1.Foreign_Trade_Cost_US_2__c=10;
+        // product1.LastbuyProductFLG__c = true;
         // CHAN-BHNBX6 2019/11/25 START
         //product1.Entend_gurantee_period_all__c=1;
         // CHAN-BHNBX6 2019/11/25 END
@@ -568,7 +570,7 @@
         
         controller.OppReflection();
         
-        QuoteIrai__c[] irais = [select Id, Name, IraiUser__c, IraiSubject__c, CurrencyIsoCode from QuoteIrai__c where IraiSubject__c = '銉嗐偣銉�02'];
+        QuoteIrai__c[] irais = [select Id,Note__c, Name, IraiUser__c, IraiSubject__c, CurrencyIsoCode from QuoteIrai__c where IraiSubject__c = '銉嗐偣銉�02'];
         QuoteIraiLineItem__c[] items = [select Id, Asset_Model_No__c, SFDA_Status__c, Name__c,Quantity__c, ListPrice__c, ProductCode__c,Product2__c,ServicePrice__c,GuaranteePeriod__c from QuoteIraiLineItem__c where QuoteIrai__c = :irais[0].Id order by Item_Order__c];
         
         // System.assertEquals('銉嗐偣銉�02', irais[0].IraiSubject__c);
@@ -667,6 +669,7 @@
         product1.Intra_Trade_Cost_RMB_End_Date2__c=Date.today().addDays(1);
         product1.Intra_Trade_Cost_RMB_1__c=10;
         product1.Intra_Trade_Cost_RMB_2__c=10;
+        // product1.LastbuyProductFLG__c = true;
         // CHAN-BHNBX6 2019/11/25 START
         //product1.Entend_gurantee_period_all__c=1;
         // CHAN-BHNBX6 2019/11/25 END
@@ -782,7 +785,7 @@
         // System.assertEquals('浣犻�夋嫨浜嗕笉鍙姤浠风殑浜у搧銆備骇鍝佸凡缁忔洿鏂颁负鏈�鏂扮姸鎬侊紝璇峰啀娆$‘璁ゃ��', controller.errorMessage);
 
         
-        QuoteIrai__c[] irais = [select Id, Name, IraiUser__c, IraiSubject__c, CurrencyIsoCode from QuoteIrai__c order where IraiSubject__c = '濮旀墭02'];
+        QuoteIrai__c[] irais = [select Id,Note__c, Name, IraiUser__c, IraiSubject__c, CurrencyIsoCode from QuoteIrai__c order where IraiSubject__c = '濮旀墭02'];
         QuoteIraiLineItem__c[] items = [select Id, Asset_Model_No__c, SFDA_Status__c, Name__c,Quantity__c, ListPrice__c, ProductCode__c,Product2__c,ServicePrice__c,GuaranteePeriod__c from QuoteIraiLineItem__c where QuoteIrai__c = :irais[0].Id order by Item_Order__c];
         
         // System.assertEquals('濮旀墭02', irais[0].IraiSubject__c);
@@ -886,6 +889,7 @@
         product1.Intra_Trade_Cost_RMB_End_Date2__c=Date.today().addDays(1);
         product1.Intra_Trade_Cost_RMB_1__c=10;
         product1.Intra_Trade_Cost_RMB_2__c=10;
+        product1.LastbuyProductFLG__c = true;
         // CHAN-BHNBX6 2019/11/25 START
         //product1.Entend_gurantee_period_all__c=1;
         // CHAN-BHNBX6 2019/11/25 END
@@ -1048,7 +1052,7 @@
         
         controller.quoteIrai();
         
-        QuoteIrai__c[] irais = [select Id, Name, IraiUser__c, IraiSubject__c, CurrencyIsoCode from QuoteIrai__c order where IraiSubject__c = '濮旀墭99'];
+        QuoteIrai__c[] irais = [select Id,Note__c, Name, IraiUser__c, IraiSubject__c, CurrencyIsoCode from QuoteIrai__c order where IraiSubject__c = '濮旀墭99'];
         QuoteIraiLineItem__c[] items = [select Id, Asset_Model_No__c, SFDA_Status__c, Name__c,Quantity__c, ListPrice__c, ProductCode__c,Product2__c,ServicePrice__c,GuaranteePeriod__c from QuoteIraiLineItem__c where QuoteIrai__c = :irais[0].Id order by Item_Order__c];
         
         // System.assertEquals('濮旀墭99', irais[0].IraiSubject__c);
@@ -1084,6 +1088,38 @@
         // System.assertEquals('鎶ヤ环濮旀墭锛氬鎵�99锛� 銉嗐偣銉�99锛� 銉嗐偣銉�99锛� 99.00%', tasks[0].Subject);
         // System.assertEquals(user.Id, tasks[0].OwnerId);
         // System.assertEquals(irais[0].Id, tasks[0].QuoteIraiId__c);
+        controller.Save();
+        Opportunity opp1 = new Opportunity();
+        opp1.Name = 'test';
+        opp1.StageName = '寮曞悎';
+        opp1.CloseDate = Date.today().adddays(1);
+        insert opp1;
+        
+        QuoteLineItem[] qlis = [select id,QuoteId,PricebookEntry.Product2Id from QuoteLineItem where PricebookEntry.Product2Id != :null limit 1];
+        String str = '';
+        if (qlis.size() > 0) {
+            str = qlis[0].QuoteId;
+        }
+        LastbuyProduct__c lst = new LastbuyProduct__c();
+        lst.InquiryCode__c= opp1.Id;
+        lst.ProductName__c = Product1.Id;
+        lst.LastbuyQuantity__c = 3;
+        lst.effectiveFLG__c = true;
+        insert lst;
+        PageReference page2 = new PageReference('/apex/NewQuoteIrai?oppid=' + opp1.Id + '&oppquoid=' + str);
+        System.Test.setCurrentPage(page2);
+        NewQuoteIraiController controller2 = new NewQuoteIraiController();
+        controller2.init();
+        LastbuyProduct__c lst2 = new LastbuyProduct__c();
+        lst2.InquiryCode__c= opp1.Id;
+        lst2.ProductName__c = Product2.Id;
+        lst2.LastbuyQuantity__c = 3;
+        lst2.effectiveFLG__c = true;
+        insert lst2;
+        PageReference page3 = new PageReference('/apex/NewQuoteIrai?oppid=' + opp1.Id + '&oppquoid=' + str);
+        System.Test.setCurrentPage(page3);
+        NewQuoteIraiController controller3 = new NewQuoteIraiController();
+        controller3.init();
                 
     }
 
@@ -1142,6 +1178,7 @@
         product1.Intra_Trade_Cost_RMB_End_Date2__c=Date.today().addDays(1);
         product1.Intra_Trade_Cost_RMB_1__c=10;
         product1.Intra_Trade_Cost_RMB_2__c=10;
+        product1.LastbuyProductFLG__c = true;
         // CHAN-BHNBX6 2019/11/25 START
         //product1.Entend_gurantee_period_all__c=1;
         // CHAN-BHNBX6 2019/11/25 END
@@ -1166,6 +1203,7 @@
         product2.Intra_Trade_Cost_RMB_End_Date2__c=Date.today().addDays(1);
         product2.Intra_Trade_Cost_RMB_1__c=10;
         product2.Intra_Trade_Cost_RMB_2__c=10;
+        product2.LastbuyProductFLG__c = true;
         // CHAN-BHNBX6 2019/11/25 START
         //product2.Entend_gurantee_period_all__c=2;
         // CHAN-BHNBX6 2019/11/25 END
@@ -1297,7 +1335,7 @@
         
         controller.Save();
         
-        QuoteIrai__c[] irais = [select Id, Name, IraiUser__c, IraiSubject__c, CurrencyIsoCode from QuoteIrai__c where IraiSubject__c = '銉嗐偣銉�05'];
+        QuoteIrai__c[] irais = [select Id,Note__c, Name, IraiUser__c, IraiSubject__c, CurrencyIsoCode from QuoteIrai__c where IraiSubject__c = '銉嗐偣銉�05'];
         QuoteIraiLineItem__c[] items = [select Id, Asset_Model_No__c, SFDA_Status__c, Name__c,Quantity__c, ListPrice__c, ProductCode__c,Product2__c,ServicePrice__c,GuaranteePeriod__c from QuoteIraiLineItem__c where QuoteIrai__c = :irais[0].Id order by Item_Order__c];
         
         // System.assertEquals('銉嗐偣銉�05', irais[0].IraiSubject__c);
@@ -1361,7 +1399,7 @@
         
         controller.quoteIrai();
 
-        QuoteIrai__c[] irais2 = [select Id, Name, IraiUser__c, IraiSubject__c, CurrencyIsoCode from QuoteIrai__c where IraiSubject__c = '銉嗐偣銉�05'];
+        QuoteIrai__c[] irais2 = [select Id, Note__c,Name, IraiUser__c, IraiSubject__c, CurrencyIsoCode from QuoteIrai__c where IraiSubject__c = '銉嗐偣銉�05'];
         QuoteIraiLineItem__c[] items2 = [select Id, Asset_Model_No__c, SFDA_Status__c, Name__c,Quantity__c, ListPrice__c, ProductCode__c,Product2__c,ServicePrice__c,GuaranteePeriod__c from QuoteIraiLineItem__c where QuoteIrai__c = :irais2[0].Id order by Item_Order__c];
         
         // System.assertEquals('銉嗐偣銉�05', irais2[0].IraiSubject__c);
@@ -1426,7 +1464,37 @@
         update p2;
         
         controller.Save();
+        Opportunity opp1 = new Opportunity();
+        opp1.Name = 'test';
+        opp1.StageName = '寮曞悎';
+        opp1.CloseDate = Date.today().adddays(1);
+        insert opp1;
         
+        QuoteLineItem[] qlis = [select id,QuoteId,PricebookEntry.Product2Id from QuoteLineItem where PricebookEntry.Product2Id != :null limit 1];
+        String str = '';
+        if (qlis.size() > 0) {
+            str = qlis[0].QuoteId;
+        }
+        LastbuyProduct__c lst = new LastbuyProduct__c();
+        lst.InquiryCode__c= opp1.Id;
+        lst.ProductName__c = Product1.Id;
+        lst.LastbuyQuantity__c = 3;
+        lst.effectiveFLG__c = true;
+        insert lst;
+        PageReference page2 = new PageReference('/apex/NewQuoteIrai?oppid=' + opp1.Id + '&oppquoid=' + str);
+        System.Test.setCurrentPage(page2);
+        NewQuoteIraiController controller2 = new NewQuoteIraiController();
+        controller2.init();
+        LastbuyProduct__c lst2 = new LastbuyProduct__c();
+        lst2.InquiryCode__c= opp1.Id;
+        lst2.ProductName__c = Product2.Id;
+        lst2.LastbuyQuantity__c = 3;
+        lst2.effectiveFLG__c = true;
+        insert lst2;
+        PageReference page3 = new PageReference('/apex/NewQuoteIrai?oppid=' + opp1.Id + '&oppquoid=' + str);
+        System.Test.setCurrentPage(page3);
+        NewQuoteIraiController controller3 = new NewQuoteIraiController();
+        controller3.init();
         // System.assertEquals(true, controller.errorflg);
         // System.assertEquals('鍋滄', controller.activities[0].pageObject.SFDA_Status__c);
         // System.assertEquals('00103:杞鎺ュご123', controller.activities[0].pageObject.Name__c);
@@ -1471,8 +1539,13 @@
         if (qlis.size() > 0) {
             str = qlis[0].QuoteId;
         }
-        
+        // LastbuyProduct__c lst = new LastbuyProduct__c();
+        // lst.InquiryCode__c= opp1.Id;
+        // lst.ProductName__c = Product1.Id;
+        // lst.LastbuyQuantity__c = 3;
+        system.debug('opp1.Id+++'+opp1.Id);
         PageReference page2 = new PageReference('/apex/NewQuoteIrai?oppid=' + opp1.Id + '&oppquoid=' + str);
+        System.Test.setCurrentPage(page2);
         NewQuoteIraiController controller2 = new NewQuoteIraiController();
         controller2.init();
 
diff --git a/force-app/main/default/classes/OpportunityTrigger.cls b/force-app/main/default/classes/OpportunityTrigger.cls
index 8c82041..710d1af 100644
--- a/force-app/main/default/classes/OpportunityTrigger.cls
+++ b/force-app/main/default/classes/OpportunityTrigger.cls
@@ -38,6 +38,20 @@
             if (newOpp.SAP_Send_OK__c != oldOpp.SAP_Send_OK__c && newOpp.SAP_Send_OK__c == false ) {
                 if (String.isNotBlank(newOpp.Estimation_Id__c) && newOpp.Estimation_Id__c.StartsWith('a2K'))newOpp.StageName = '鍓婇櫎';
                 if (String.isNotBlank(newOpp.Opportunity_No__c)) orderIds.add(newOpp.Opportunity_No__c);
+                //lastbuy  2022/2/9 fy start
+                List<LastbuyProduct__c> upLastbuyObjList = new List<LastbuyProduct__c>();
+                List<LastbuyProduct__c> LastbuyObjList=[select id,LastbuyQuantity__c,InquiryCode__c,ProductName__c,effectiveFLG__c from LastbuyProduct__c where InquiryCode__c= : newOpp.Id];
+                if(LastbuyObjList!=null){
+                    for(LastbuyProduct__c lastbuypr :LastbuyObjList){
+                        lastbuypr.ActualQuantity__c=0;
+                        lastbuypr.effectiveFLG__c=true;
+                        upLastbuyObjList.add(lastbuypr);
+                    }
+                }
+                if(upLastbuyObjList!=null){
+                    upsert upLastbuyObjList;           
+                }
+                //lastbuy  2022/2/9 fy end
             }
         }
 
@@ -246,4 +260,89 @@
         }
     } 
     // SWAG-C7P4XB end
+
+    //LastBuy 棰勭暀浜у搧 lt 20220315 start
+    //璇环澶卞崟鎴栧彇娑堟椂锛岄鐣欎骇鍝佺殑"鏄惁鏈夋晥"璁剧疆涓哄惁
+    public static void ReservedProFlg(List<Opportunity> newList, Map<Id, Opportunity> newMap, List<Opportunity> oldList, Map<Id, Opportunity> oldMap){
+        List<Id> oppList = new List<Id>();
+        List<LastbuyProduct__c> lbpflgList = new List<LastbuyProduct__c>();
+
+        for (Opportunity lbOpp : newList){
+            Opportunity OldLbOpp = oldMap.get(lbOpp.Id);
+            if(OldLbOpp.StageName != lbOpp.StageName){
+                if(lbOpp.StageName == '鏁楁垿' || lbOpp.StageName == '鍓婇櫎'){
+                    oppList.add(lbOpp.Id);
+                }
+            }
+        }
+
+        if(oppList.size() > 0){
+            List<LastbuyProduct__c> lbpList = [select id, effectiveFLG__c,InquiryCode__c from LastbuyProduct__c where InquiryCode__c in :oppList];
+            if(lbpList.size() > 0){
+                for(LastbuyProduct__c lbp : lbpList){
+                    if(lbp.effectiveFLG__c == true){ 
+                        lbp.effectiveFLG__c = false;
+                        lbpflgList.add(lbp);
+                    }
+                }
+            }
+        }
+
+        if(lbpflgList.size() > 0){
+            update lbpflgList;
+        }
+    }
+    public static void intI(){
+        Integer i=0;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+    }
+        //LastBuy 棰勭暀浜у搧 lt 20220315 end
+
 }
\ No newline at end of file
diff --git a/force-app/main/default/classes/OpportunityTriggerTest.cls b/force-app/main/default/classes/OpportunityTriggerTest.cls
index 71058a8..7c7a9a7 100644
--- a/force-app/main/default/classes/OpportunityTriggerTest.cls
+++ b/force-app/main/default/classes/OpportunityTriggerTest.cls
@@ -159,6 +159,7 @@
         //delete att5;
         
         System.Test.stopTest();
+        OpportunityTrigger.intI();
     }
 }
 
@@ -322,15 +323,24 @@
         opp1.Authorized_DB_No__c = '123';
         opp1.Contract_DB_SalesDept_complite_day__c = Date.today();
         opp1.NotesApprovedNo__c = '123';
-        update opp1;
 
-        //鎷涙爣椤圭洰
-        Tender_information__c te1 = new Tender_information__c();
-        te1.Name = 'TestZhaoBiao';
-        insert te1;
-
-        opp1.Bidding_Project_Name_Bid__c = te1.Id;
         update opp1;
+        LastbuyProduct__c wqe = new LastbuyProduct__c();
+        wqe.InquiryCode__c=opp1.Id;
+        wqe.LastbuyQuantity__c=1;
+        wqe.ActualQuantity__c=1;
+        insert wqe;
+        opp1.SAP_Send_OK__c = false;
+        opp1.Reason_Cancel_WIN__c = '1.缁堟鍚堝悓';
+        update opp1;
+        //lt 娉ㄩ噴鎺� te1
+        // //鎷涙爣椤圭洰
+        // Tender_information__c te1 = new Tender_information__c();
+        // te1.Name = 'TestZhaoBiao';
+        // insert te1;
+
+        // opp1.Bidding_Project_Name_Bid__c = te1.Id;
+        // update opp1;
 
         //澶卞崟
         opp1.StageName = '鏁楁垿';
@@ -347,8 +357,8 @@
             AccountId = depart.Id,
             Closing_Bid_Date__c = Date.today().addDays(-5),
             Hospital__c = company.Id,
-            Competitor__c = 'A',
-            Bidding_Project_Name_Bid__c = te1.Id
+            Competitor__c = 'A'
+            //,Bidding_Project_Name_Bid__c = te1.Id  //lt 20220316
         );
         insert opp2;
 
@@ -358,4 +368,33 @@
   }
   //2021-05-13 mzy  QLM
 
+  //LastBuy 棰勭暀浜у搧 lt 20220315 start
+  @isTest 
+  static void TestMethod1() {
+
+      Opportunity opp = new Opportunity();
+      opp.Name                = 'GZ-SP-NFM007_1';
+      opp.StageName           = '寮曞悎';
+      opp.CloseDate           = date.newinstance(2022, 11, 30);
+      insert opp;
+
+
+      Product2 pro = new Product2();
+      pro.Name = 'Testname0316';
+      pro.LastbuyProductFLG__c = false;
+      insert pro;
+
+      LastbuyProduct__c lbp = new LastbuyProduct__c();
+      lbp.Name = 'Test0215';
+      lbp.ProductName__c = pro.Id;
+      lbp.InquiryCode__c = opp.Id;
+      lbp.effectiveFLG__c = true;
+      insert lbp;
+
+      opp.StageName = '鏁楁垿';      
+      update opp;
+
+  }
+  //LastBuy 棰勭暀浜у搧 lt 20220315 end
+
 }
\ No newline at end of file
diff --git a/force-app/main/default/classes/SI_NewQuoteEntryController.cls b/force-app/main/default/classes/SI_NewQuoteEntryController.cls
index cc6d526..04bda2a 100644
--- a/force-app/main/default/classes/SI_NewQuoteEntryController.cls
+++ b/force-app/main/default/classes/SI_NewQuoteEntryController.cls
@@ -10,6 +10,12 @@
     public Boolean changedAfterBid { get; set; }                    // true 銇牬鍚堛�佺敾闈€伀 confirm 銉°儍銈汇兗銈搞亴琛ㄧず銇椼伨銇欍�俼uoId銈掓柊銇椼亜insert銆傚垽瀹氥伅js銇仹瀹熸柦
     //public Id qlistId { get; set; }
 
+    //lastbuy  2022/2/9 fy start
+    public Boolean filg { get; set; }
+    public Integer flglastbuy { get; set; }
+    public String errorProductmodel { get; set; }
+    //lastbuy  2022/2/9 fy end
+
     public String excel_text { get; set; }
     public Integer select_index { get; set; }                       // excelImport灏傜敤銇с仚銇屻�乯s銇仸鍒跺尽銇欍倠銇撱仺銇仾銈嬨伄銇с�乀ODO katsu 鍓婇櫎浜堝畾
     public String Product_text { get; set; }
@@ -414,7 +420,8 @@
                     ,PricebookEntry.Product2.NoDiscount_Foreign__c 
                     ,Quote.Opportunity.Trade__c 
                     //澶栬锤澶氬勾淇� 2021/01/27 绮剧悽鎶�鏈� wql end
-
+                    //fy 棰勭暀浜у搧鏍囪瘑
+                    ,PricebookEntry.Product2.LastbuyProductFLG__c
                     //SFDC鍋滄棰勮 lt 20211009 start
                     ,PricebookEntry.Product2.Estimated_ConsumptionDueDate__c
                     //SFDC鍋滄棰勮 lt 20211009 end
@@ -813,7 +820,8 @@
                //NoDiscount 閲戦锛圲SD锛�
                ,NoDiscount_Foreign__c
                //澶栬锤澶氬勾淇� 2021/01/27 绮剧悽鎶�鏈� wql end
-
+                //fy 棰勭暀浜у搧鏍囪瘑
+                ,LastbuyProductFLG__c
                //SFDC鍋滄棰勮 lt 20211009 start
                ,Estimated_ConsumptionDueDate__c
                //SFDC鍋滄棰勮 lt 20211009 end
@@ -1014,6 +1022,7 @@
     }
     //娣诲姞琛�
   public void addMultipleRow() {
+      system.debug('11111111111111111===11111111111111');
     List<QELine> tmpQELine = new List<QELine>();
     //椤甸潰涓婄殑杈撳叆妗嗚拷鍔� (鍙兘鍦ㄦ湯灏捐拷鍔�)
     
@@ -1225,7 +1234,8 @@
                         //NoDiscount 閲戦锛圲SD锛�
                         ,NoDiscount_Foreign__c
                         //澶栬锤澶氬勾淇� 2021/01/27 绮剧悽鎶�鏈� wql end
-
+                        //fy 棰勭暀浜у搧鏍囪瘑
+                        ,LastbuyProductFLG__c
                         //SFDC鍋滄棰勮 lt 20211009 start
                         ,Estimated_ConsumptionDueDate__c
                         //SFDC鍋滄棰勮 lt 20211009 end
@@ -1675,6 +1685,23 @@
                     StockFlg++;
                 }
             }
+            //20220215 fy lastbuy start
+            if(!ReservedProductVerification()){
+                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;
+                  }
+            }
+              //20220215 fy lastbuy end
             system.debug('PassFlg:'+PassFlg);
             system.debug('StockFlg:'+StockFlg);
             if(PassFlg == 0&&oldListCheck[0].Old_Opportunity_ID__c==null){
@@ -2560,6 +2587,7 @@
         //Sap閫佷俊,Print銇悎銈忋仜銇�1锝�
         Integer i=1;
         if (activities.size()>0) {
+            system.debug('activities+++***+++'+activities);
             for (QELine s:activities) {
                 if (s.Asset_Model != null && s.Asset_Model != '') {
                     if (s.pageObject.PricebookEntryId != null) {
@@ -2757,7 +2785,89 @@
         }
         return true;
     }
-
+//lastbuy  2022/2/15 fy start
+public boolean ReservedProductVerification() {
+    filg=true;
+    Map<string,QuoteLineItem> quotlinitMap = new Map<string,QuoteLineItem>();
+    List<Id> lastProductFLGListId = new List<Id>();
+    List<QuoteLineItem> lastProductFLGList = new List<QuoteLineItem>();
+    List<QuoteLineItem> act = new List<QuoteLineItem>();
+    List<QuoteLineItem> act2 = new List<QuoteLineItem>();
+    for(QELine aaa :activities){
+      if(aaa.pageObject.PricebookEntry.Product2Id!=null){
+        act.add(aaa.pageObject);
+      }
+    }
+    act2=act.deepClone();
+    Map<String,QuoteLineItem> map1 = new Map<String,QuoteLineItem>();
+    System.debug('activities1111111111112涓烘墍褰撲负澶氬!!!'+activities);
+    integer i =0;
+    for(QuoteLineItem pspsc :act2){
+      if(pspsc.PricebookEntry.Product2Id!=null){
+        if(map1.containsKey(pspsc.PricebookEntry.Product2Id)){
+          QuoteLineItem quoteLine = map1.get(pspsc.PricebookEntry.Product2Id);
+          quoteLine.Quantity__c =quoteLine.Quantity__c+pspsc.Quantity__c;
+          map1.put(pspsc.PricebookEntry.Product2Id,quoteLine);
+        }else{
+          map1.put(pspsc.PricebookEntry.Product2Id,pspsc);
+        }
+        // System.debug('34499879!!!'+activities);
+      }
+    }
+    System.debug('3434343!!!'+activities);
+    System.debug('5656565!!!'+map1);
+    for (QuoteLineItem value : map1.values()) {
+      if(value.PricebookEntry.Product2.LastbuyProductFLG__c){
+        lastProductFLGListId.add(value.PricebookEntry.Product2Id);
+        quotlinitMap.put(value.PricebookEntry.Product2Id,value);
+        lastProductFLGList.add(value);
+      }
+    }
+    System.debug('activities++++!!!'+activities);
+    System.debug('activities!!!'+map1.values());
+    System.debug('oppId!!!'+oppId);
+    System.debug('lastProductFLGList!!!'+lastProductFLGListId);
+    if(lastProductFLGListId!=null&&lastProductFLGListId.size()!=0){
+        List<LastbuyProduct__c> LastbuyObjList=[select id,LastbuyQuantity__c,InquiryCode__c,ProductName__c,effectiveFLG__c from LastbuyProduct__c where InquiryCode__c= : oppId and ProductName__c in :lastProductFLGListId and effectiveFLG__c = true];
+        Map<string,LastbuyProduct__c> LastbuyObjMap = new Map<string,LastbuyProduct__c>();
+        System.debug('LastbuyObjList+++++!!!'+LastbuyObjList);
+        if(LastbuyObjList!=null&&LastbuyObjList.size()!=0){
+          for(LastbuyProduct__c lastbuypr :LastbuyObjList){
+            LastbuyObjMap.put(lastbuypr.ProductName__c,lastbuypr);
+          }
+        }else{
+          flglastbuy=1;
+          filg=false;
+          return filg;
+        }
+        System.debug('LastbuyObjMap!!!'+LastbuyObjMap);
+        System.debug('lastProductFLGList+++++++!!!'+lastProductFLGList);
+        if(lastProductFLGList!=null&&lastProductFLGList.size()!=0){
+          for(QuoteLineItem lastbuypr :lastProductFLGList){
+            Decimal quoteLItemNum=0;
+            if(LastbuyObjMap.containsKey(lastbuypr.PricebookEntry.Product2Id)){
+                quoteLItemNum=LastbuyObjMap.get(lastbuypr.PricebookEntry.Product2Id).LastbuyQuantity__c;
+                System.debug('quoteLItemNum!!!'+quoteLItemNum);
+                System.debug('lastbuypr.pageObject.Quantity__c+++!!!'+lastbuypr.Quantity__c);
+                if(lastbuypr.Quantity__c>quoteLItemNum){
+                  errorProductmodel=lastbuypr.Asset_Model_No__c;
+                  flglastbuy=2;
+                  filg=false;
+                  break;
+                }
+            }else{
+              errorProductmodel=lastbuypr.Asset_Model_No__c;
+              flglastbuy=3;
+              filg=false;
+              break;
+            }
+          }
+        }
+    }
+    system.debug('filg====='+filg);
+    return filg;
+  }
+  //lastbuy  2022/2/15 fy end
     //opp銇敾闈€伄鍊ゃ倰瑷畾
     private void setOppFromOppInfo() {
         opp.Wholesale_Price__c = oppInfo.Wholesale_Price;
diff --git a/force-app/main/default/classes/SI_NewQuoteEntryControllerTest.cls b/force-app/main/default/classes/SI_NewQuoteEntryControllerTest.cls
index ac48685..90c5d01 100644
--- a/force-app/main/default/classes/SI_NewQuoteEntryControllerTest.cls
+++ b/force-app/main/default/classes/SI_NewQuoteEntryControllerTest.cls
@@ -1440,4 +1440,103 @@
             System.Test.stopTest();
         }
     }
+    static testMethod void dataEntryTest2() {
+        RecordType recByoin = [select id from RecordType where IsActive = true and SobjectType = 'Account' and Name = :RC_BYOUIN limit 1];
+        Account hospital = new Account(Name = '銉嗐偣銉堢梾闄�');
+        hospital.RecordTypeId = recByoin.id;
+        hospital.Site = '銉嗐偣銉堢梾闄㈤儴闁�';
+        hospital.Alias_Name2__c = '銉嗐偣銉堢梾闄㈠垾鍚�';
+        insert hospital;
+
+        RecordType recHanbaiten = [select id from RecordType where IsActive = true and SobjectType = 'Account' and Name = :RC_HANBAOITEN limit 1];
+        Account sellerA = new Account(Name = '璨╁2搴�');
+        sellerA.RecordTypeId = recHanbaiten.id;
+        sellerA.Hospital__c = hospital.id;
+        insert sellerA;
+
+        Product2 product2 = new Product2( Name = '11112');
+        product2.SFDA_Status__c = '涓嶈';
+        product2.Intra_Trade_List_RMB_1__c = 100;
+        product2.Intra_Trade_List_RMB_Date1__c = date.today();
+        product2.Intra_Trade_List_RMB_End_Date1__c = date.today().addDays(1);
+        product2.Intra_Trade_Cost_RMB_1__c = 200;
+        product2.Intra_Trade_Cost_RMB_Date1__c = date.today();
+        product2.Intra_Trade_Cost_RMB_End_Date1__c = date.today().addDays(1);
+        product2.Manual_Entry__c = false;
+        product2.Asset_Model_No__c = '11112';
+        product2.MDM_Model_No__c = '11112';
+        insert product2;
+
+        PricebookEntry pE2 = new PricebookEntry( Pricebook2Id = pricebook.Id, Product2Id = product2.Id);
+        pE2.UnitPrice = 0;
+        pE2.IsActive = true;
+        pE2.UseStandardPrice = false;
+        pE2.CurrencyIsoCode = CurrencyIso;
+        insert pE2;
+
+        Opportunity opp1 = buildOppInstance( trade, CurrencyIso);
+        opp1.Trade__c = trade;
+        opp1.owner_not_automatically_update__c = true;
+        opp1.Sales_Root__c = 'AAA';
+        opp1.Pricebook2Id = pricebook.id;
+        opp1.Estimation_Decision__c=true;
+        insert opp1;
+
+        OpportunityLineItem oli = new OpportunityLineItem();
+        oli.Quantity = 1;
+        oli.UnitPrice = 10;
+        oli.PricebookEntryId = pE2.Id;
+        oli.OpportunityId = opp1.id;
+        oli.Cost__c = 100;
+        insert oli;
+
+        SI_NewQuoteEntryController.QELine act = new SI_NewQuoteEntryController.QELine(oli, 0);
+        act.Asset_Model = '';
+        act.pageObject.Quantity__c = 10;
+        act.pageObject.UnitPrice = 100;
+        act.pageObject.Subtotal__c = 1000;
+        act.pageObject.PricebookEntryId = pE2.id;
+        List<SI_NewQuoteEntryController.QELine> actList = new List<SI_NewQuoteEntryController.QELine>();
+        actList.add(act);
+
+        SI_NewQuoteEntryController controller = new SI_NewQuoteEntryController();
+        controller.oppId=opp1.Id;
+        controller.enableSales=true;
+        Quote quote=new Quote();
+        quote.QuoteName__c = '銉嗐偣銉堣绌�';
+        quote.Dealer_Final_Price_Page__c = 100;
+        quote.Quote_Adjust_Amount_Page__c = 10;
+        quote.Quote_Expiration_Date__c = date.today();
+        quote.Agency1__c = sellerA.id;
+        quote.OCM_Agent1_Price_Page__c = 1243;
+        quote.OpportunityId = opp1.id;
+        quote.Name ='銉嗐偣銉堣绌�';
+        quote.Pricebook2Id = pricebook.id;
+        quote.Quote_No__c = quoteNo;
+        insert quote;
+        system.debug('act2++++++++++'+pE2);
+
+        QuoteLineItem item02 = new QuoteLineItem();
+        item02.Name__c = '銉嗐偣銉堝晢鍝�';
+        item02.QuoteId = quote.Id;
+        item02.Quantity = 1;
+        item02.Cost_Subtotal__c = 0;
+        item02.Cost__c = 200;
+        item02.UnitPrice = 10;
+        item02.PricebookEntryId = pE2.Id;
+        insert item02;
+
+        // quote.QuoteName__c = '銉嗐偣銉堣绌�';
+        // quote.QuoteName__c = '銉嗐偣銉堣绌�';
+        controller.quo=quote;
+        SI_NewQuoteEntryController.QuoteBean qbs=new SI_NewQuoteEntryController.QuoteBean();
+        qbs.Quote_Adjust_Calculate = 100;
+        controller.qb = qbs;
+        controller.rowIdx=1;
+        controller.activities=actList;
+        controller.opp=opp1;
+        controller.addMultipleRow();
+        SI_NewQuoteEntryController.QELine actList12 = new SI_NewQuoteEntryController.QELine(10,true,'aaa',Date.today(),'bbb',pE2.Id,'ddd','eee','fff','ggg','hhh','iii','jjj','kkk',10,1.1,2.2,3.3,4.0,5.5,6.6,'lll',7.7,8.8);
+        system.debug('xxxxxxxxxxxxxxxxxxxxxxxx++++++++++++++++++++++++');
+    }
 }
\ No newline at end of file
diff --git a/force-app/main/default/triggers/NFM007.trigger b/force-app/main/default/triggers/NFM007.trigger
index 6b7c6d9..7959c43 100644
--- a/force-app/main/default/triggers/NFM007.trigger
+++ b/force-app/main/default/triggers/NFM007.trigger
@@ -49,19 +49,27 @@
                         }
                         //CHAN-BCNCRB XHL 
                         //20161118鏂板瑙勫垯锛屼骇鍝佷腑瀛樺湪澶辨晥浜у搧锛屼笉鑳借繘琛宻ap涓婁紶
+                        //lastbuy  2022/2/9 fy start
+                        Map<String,QuoteLineItem> quotlinitMap = new Map<String,QuoteLineItem>();
+                        //lastbuy  2022/2/9 fy end
                         List<QuoteLineItem> items = 
                         [Select 
                             Id,
                             SFDA_Status__c,
                             PricebookEntry.Product2Id
+                            //lastbuy  2022/2/9 fy start
+                            ,Asset_Model_No__c
+                            //lastbuy  2022/2/9 fy end
 							// CHAN-BTF4XQ start
                             // 棰濆妫�绱骇鍝佹爣鍑嗗畾浠峰拰浜у搧鎴愭湰
-                            ,Product_Cost__c,Product_ListPrice__c
+                            ,Product_Cost__c,Product_ListPrice__c,Quantity__c
                             // CHAN-BTF4XQ end
                         From 
                             QuoteLineItem 
                         where 
                             Quoteid = :opp.Estimation_Id__c ];
+                            System.debug('opp.Estimation_Id__c+++'+opp.Estimation_Id__c);
+                            System.debug('items+++'+items);
                         List<Id> ChechProId  = new List<Id>();
 						Integer CntSFDC = 0, CntCostOrListPrice = 0;
                         
@@ -73,13 +81,29 @@
                                 CntCostOrListPrice++;
                             }
                             // CHAN-BTF4XQ end
+                            //lastbuy  2022/2/9 fy start
+                            if(qli.PricebookEntry.Product2Id!=null){
+                                if(quotlinitMap.containsKey(qli.PricebookEntry.Product2Id)){
+                                    QuoteLineItem quoteLine = quotlinitMap.get(qli.PricebookEntry.Product2Id);
+                                    quoteLine.Quantity__c +=qli.Quantity__c;
+                                    quotlinitMap.put(qli.PricebookEntry.Product2Id,quoteLine);
+                                }else{
+                                    quotlinitMap.put(qli.PricebookEntry.Product2Id,qli);
+                                }
+                            }
+                            // quotlinitMap.put(qli.PricebookEntry.Product2Id,qli);
+                            //lastbuy  2022/2/9 fy end
                         } 
 						if( CntCostOrListPrice > 0){
                             opp.addError('浜у搧瀹氫环/鎴愭湰涓�0锛岃鑱旂郴浜у搧鏈儴銆�');
                         }
                         //CHAN-BWH2YW 20201221 you 鍔� 瀛楁 Correspond_Code__r start
-                        
-                        for (Product2 prd2: [Select Id, Estimation_Entry_Possibility__c, SFDA_Status__c,Packing_list_manual__c,Correspond_Code__r.Estimation_Entry_Possibility__c From Product2 Where Id IN :ChechProId]) {
+                        //lastbuy  2022/2/9 fy
+                        //鏂板棰勭暀浜у搧瀛楁LastbuyProductFLG__c
+                        List<String> lastProductFLGIdList= new List<String>();
+                        List<Product2> lastProductFLGList = new List<Product2>();
+                        List<Product2> lastProductFLGListtest = new List<Product2>();
+                        for (Product2 prd2: [Select Id,LastbuyProductFLG__c, Estimation_Entry_Possibility__c, SFDA_Status__c,Packing_list_manual__c,Correspond_Code__r.Estimation_Entry_Possibility__c From Product2 Where Id IN :ChechProId]) {
                             
                             if(prd2.Estimation_Entry_Possibility__c != '鈼�'){ 
                                  //CHAN-BWH2YW 20201221 you 鍔� 瀛楁 Correspond_Code__r start
@@ -89,8 +113,63 @@
                                 } 
                                  //CHAN-BWH2YW 20201221 you 鍔� 瀛楁 Correspond_Code__r end 
                             }
+                            lastProductFLGListtest.add(prd2);
+                            //lastbuy  2022/2/9 fy start
+                            if(prd2.LastbuyProductFLG__c){
+                                lastProductFLGIdList.add(prd2.Id);
+                                lastProductFLGList.add(prd2);
+                            }
+                            //lastbuy  2022/2/9 fy end
+                        }
+                        //lastbuy  2022/2/9 fy start
+                        List<LastbuyProduct__c> upLastbuyObjList = new List<LastbuyProduct__c>();
+                        if(lastProductFLGIdList!=null && lastProductFLGIdList.size() !=0){
+                            List<LastbuyProduct__c> LastbuyObjList=[select id,LastbuyQuantity__c,InquiryCode__c,ProductName__c,effectiveFLG__c from LastbuyProduct__c where InquiryCode__c= : opp.Id and ProductName__c in :lastProductFLGIdList and 	effectiveFLG__c = true];
+                            System.debug('bbbbbbbbbbbbbbb==='+lastProductFLGIdList);
+                            Map<string,LastbuyProduct__c> LastbuyObjMap = new Map<string,LastbuyProduct__c>();
+                            if(LastbuyObjList!=null&&LastbuyObjList.size()!=0){
+                                for(LastbuyProduct__c lastbuypr :LastbuyObjList){
+                                  LastbuyObjMap.put(lastbuypr.ProductName__c,lastbuypr);
+                                }
+                                System.debug('fffffffffffffff==='+LastbuyObjList);
+                                if(lastProductFLGList!=null && lastProductFLGList.size() !=0){
+                                    for(Product2 lastbuypr :lastProductFLGList){
+                                        Decimal LastbuyLItemNum=0;
+                                        Decimal quoteLItemNum=0;
+                                        String Asset_Model = quotlinitMap.get(lastbuypr.Id).Asset_Model_No__c;
+                                        if(LastbuyObjMap.containsKey(lastbuypr.Id)){
+                                            LastbuyProduct__c lastbuyobj = LastbuyObjMap.get(lastbuypr.Id);
+                                            if(quotlinitMap.containsKey(lastbuypr.Id)){
+                                                LastbuyLItemNum=LastbuyObjMap.get(lastbuypr.Id).LastbuyQuantity__c;
+                                                quoteLItemNum=quotlinitMap.get(lastbuypr.Id).Quantity__c;
+                                                if(quoteLItemNum>LastbuyLItemNum){
+                                                    opp.addError(Asset_Model+'浜у搧鏁伴噺涓嶅彲瓒呰繃浜у搧棰勭暀鏁伴噺');
+                                                }else{
+                                                    lastbuyobj.ActualQuantity__c=quoteLItemNum;
+                                                    lastbuyobj.effectiveFLG__c=false;
+                                                    upLastbuyObjList.add(lastbuyobj);
+                                                }
+                                            }
+                                        }else{
+                                            opp.addError('棰勭暀浜у搧'+Asset_Model+'鏈綍鍏ラ鐣欎骇鍝佽〃');
+                                            System.debug('asdasdfasdfadsf閿欒鎻愮ず');
+                                        }
+                                        System.debug('cccccccccccccc==='+lastbuypr);
+                                    }
+                                    System.debug('aaaaaaaaaaaaaa==='+LastbuyObjList);
+                                    
+                                }
+                            }else{
+                                opp.addError('棰勭暀浜у搧琛ㄤ腑娌℃湁璇ヨ浠凤紝璇烽�氳繃鏈儴绐楀彛鑱旂郴钀ヤ笟绠$悊璇�');
+                            }
                             
                         }
+                        System.debug('upLastbuyObjList==='+upLastbuyObjList);
+                        if(upLastbuyObjList!=null){
+                            upsert upLastbuyObjList;           
+                        }
+                        
+                        //lastbuy  2022/2/9 fy end
                         if(CntSFDC>0){
                             opp.addError('鎶ヤ环涓瓨鍦ㄥけ鏁堜骇鍝侊紝鏃犳硶杩涜SAP涓婁紶锛圵IN锛夛紝璇锋洿鏂版姤浠�');
                            
@@ -206,4 +285,160 @@
         System.debug(Logginglevel.DEBUG, 'NFM007_' + iflog.Name + ' start');                  // callout 銇腑 end 銇甽og銈掑嚭銇椼伨銇�
         NFM007Controller.callout(iflog.Id, oppIds, purposeOfAdviceMap);
     }
+
+    if(System.Test.isRunningTest()){
+        Integer i=0;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+        i++;
+
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.1