高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
global class UpdateQuoteOtcodeBacth implements Database.Batchable<sObject> {
    //询价id
    String tempOppId ='';
    //参数 报价id
    String tempQuoId ='';
    //参数 otcode
    String otcode ='';
    //参数 产品id
    String proId ='';
    //价格手册id
    String tempPriceBookEntryId;
    public List<QELine> activities;
    public List<QELine> tmpactivities;
    public QELine active_activity;
 
    global UpdateQuoteOtcodeBacth() {
 
    }
    global UpdateQuoteOtcodeBacth(String tempQuoId,String otcode,String proId) {
        this.tempQuoId = tempQuoId;
        this.otcode = otcode;
        this.proId = proId;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        //查询  招投标标识 字段 为 true 的询价
        String query = 'SELECT Id,OpportunityId from quote ';
 
        if(String.isNotBlank(this.tempQuoId)){
            query += ' where Id = :tempQuoId';
        }
        query += ' limit 200 ';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Quote> quoteList) {
        if(quoteList.size()>0){
            tempOppId = quoteList[0].OpportunityId;
        }
        List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name FROM PriceBookEntry WHERE Product2Id=:proId and CurrencyIsoCode ='CNY'];
        if(priceBookList.size()>0){
            tempPriceBookEntryId = priceBookList[0].Id;
        }
        //clone报价行
        List<QuoteLineItem> QuoteLineItemCloneList = new List<QuoteLineItem>();
        //clone询价行
        List<OpportunityLineItem> OpportunityLineItemCloneList = new List<OpportunityLineItem>();
 
        if(otcode!=null &&proId!=null){
            //先检索报价行
            //develop console、batch不能执行
            //List<QuoteLineItem>  QuoteLineItemList= [select fields(all) from QuoteLineItem where Quoteid =:tempQuoId limit 200];
            List<QuoteLineItem> QuoteLineItemList =
            [Select id,Asset_Model_No__c,Product2Id, SFDA_Status__c, Name__c, BSS_Category__c,Qty_Unit__c, Cost__c, UnitPrice__c,UnitPrice, ListPrice__c, Quantity, PricebookEntryId, UnitPrice_Page__c,AgencyUnitPrice__c, AgencySubtotal__c, Present__c, multiYearWarranty__c , If_Cancel_Guarantee__c , GuaranteePeriod__c, ServicePrice__c , GuranteePrice__c, ProductEntend_gurantee_period_all__c,ProductGuranteePrice__c,  GuranteeType__c,warrantyType__c, productServicePrice__c, NoDiscountTotal__c, provistonPeriod__c, Maintenance_Price_Year__c,Quoteid  From QuoteLineItem where Quoteid =:tempQuoId Order by Item_Order__c, Id];
            // //由于ot code是公式文本,所以实际需要的是替换产品id
            // QuoteLineItemCloneList = QuoteLineItemList.clone();
            activities =new List<QELine>();
            if(QuoteLineItemList.size()>0){
                for(QuoteLineItem quote :QuoteLineItemList){
                    QELine c = new QELine(quote,otcode,proId,tempPriceBookEntryId);
                    System.debug('C::'+c);
                    activities.add(c);
                }
            }
            //报价行先删后增 start
            if(QuoteLineItemList.size()>0){
                delete QuoteLineItemList;
            }
            //报价行先删后增 end
            if(activities.size()>0){
                for(QELine  qe:activities){
                    //new 行项目
                    QuoteLineItem quo = new QuoteLineItem();
                    //复制报价行
                    quo = qe.pageObject;
                    //addlist
                    QuoteLineItemCloneList.add(quo);
                }
            }
            
            system.debug('QuoteLineItemCloneList:'+QuoteLineItemCloneList);
            //报价行先删后增 start
            if(QuoteLineItemCloneList.size()>0){
                insert QuoteLineItemCloneList;
            }
            //报价行先删后增 end
 
            //先检索报价行
            //develop console 、batch不能执行
            //List<OpportunityLineItem>  OpportunityLineItemList= [select fields(all) from OpportunityLineItem where Opportunityid =:tempOppId limit 200];
            List<OpportunityLineItem>  OpportunityLineItemList=[Select id,OpportunityId,Product2Id, Asset_Model_No__c, SFDA_Status__c, Name__c, ProductCode__c,Qty_Unit__c, Cost__c, ListPrice__c, Quantity, BSS_Category__c, TotalPrice,Product_Cost__c, Product_ListPrice__c,PricebookEntryId,UnitPrice__c, TotalPrice__c, AgencyUnitPrice__c, AgencySubtotal__c, Present__c, multiYearWarranty__c , If_Cancel_Guarantee__c , GuaranteePeriod__c,ServicePrice__c , GuranteePrice__c,ProductEntend_gurantee_period_all__c,ProductGuranteePrice__c, GuranteeType__c,warrantyType__c, productServicePrice__c,NoDiscountTotal__c, provistonPeriod__c, Maintenance_Price_Year__c From OpportunityLineItem Where OpportunityId =:tempOppId Order by Item_Order__c, Id];
            // //由于ot code是公式文本,所以实际需要的是替换产品id
            // OpportunityLineItemCloneList = OpportunityLineItemList.clone();
            //清空
            // activities.clear();
            activities =new List<QELine>();
            System.debug('最开始的:OpportunityLineItemList:'+OpportunityLineItemList);
            System.debug('最开始的数量:'+OpportunityLineItemList.size());
            if(OpportunityLineItemList.size()>0){
                for(OpportunityLineItem opp :OpportunityLineItemList){
                    QELine c = new QELine(opp,otcode,proId,tempPriceBookEntryId);
                    System.debug('C::'+c);
                    activities.add(c);
                }
            }
            //报价行先删后增 start
            if(OpportunityLineItemList.size()>0){
                delete OpportunityLineItemList;
            }
            system.debug('询价行的行数:'+activities.size());
            //报价行先删后增 end
            if(activities.size()>0){
                for(QELine  ql:activities){
                    //new 行项目
                    OpportunityLineItem oppli = new OpportunityLineItem();
                    //复制报价行
                    oppli = ql.pageOppObject;
                    //addlist
                    OpportunityLineItemCloneList.add(oppli);
                }
            }
            
            system.debug('OpportunityLineItemCloneList:'+OpportunityLineItemCloneList);
            //报价行先删后增 start
            if(OpportunityLineItemCloneList.size()>0){
                insert OpportunityLineItemCloneList;
            }
            
        }
        
    }
    global void finish(Database.BatchableContext BC) {
 
    }
 
    public class QELine {
        public String  Id;
        public String  Asset_Model_No;
        public String  Product2Id;
        public QuoteLineItem pageObject;
        public OpportunityLineItem pageOppObject;
        public String  SFDA_Status;
        public String  Name;
        public String  BSS_Category;
        public String  Qty_Unit;
        public Decimal  Cost;
        public Decimal  UnitPrice;
        public Decimal  ListPrice;
        public Decimal  Quantity;
        public String  PricebookEntryId;
        public Decimal  UnitPrice_Page;
        public Decimal  AgencyUnitPrice;
        public Decimal  AgencySubtotal;
        public Boolean  Present;
        public Boolean  multiYearWarranty;
        public Boolean  If_Cancel_Guarantee;
        public Decimal  GuaranteePeriod;
        public Decimal  ServicePrice;
        public Decimal  GuranteePrice;
        public Decimal  ProductEntend_gurantee_period_all;
        public Decimal  ProductGuranteePrice;
        public String  GuranteeType;
        public String  warrantyType;
        public Decimal  productServicePrice;
        public Decimal  NoDiscountTotal;
        public Decimal  provistonPeriod;
        public Decimal  Maintenance_Price_Year;
        public String  Quoteid;
        //报价行遍历
        public QELine(QuoteLineItem qli,String otcode,String proId,String tempPriceBookEntryId){
            this.pageObject = qli.clone();
            //insert 报价不需要id
            // this.Id      =    qli.Id;
            this.Asset_Model_No        =    qli.Asset_Model_No__c;
            //替换产品和价格手册
            if(qli.Asset_Model_No__c.equals(otcode)){
                this.pageObject.Product2Id=proId  ;
                this.pageObject.PricebookEntryId=tempPriceBookEntryId  ;
            }
            // else{
            //     this.Product2Id=qli.Product2Id  ;
            //     this.PricebookEntryId=qli.PricebookEntryId ;
            // }
            // this.Product2Id      =    qli.Product2Id;
            // this.SFDA_Status        =    qli.SFDA_Status__c;
            // this.Name        =    qli.Name__c;
            // this.BSS_Category        =    qli.BSS_Category__c;
            // this.Qty_Unit        =    qli.Qty_Unit__c;
            // this.Cost        =    qli.Cost__c;
            // this.UnitPrice        =    qli.UnitPrice__c;
            // this.UnitPrice      =    qli.UnitPrice;
            // this.ListPrice        =    qli.ListPrice__c;
            // this.Quantity      =    qli.Quantity;
            // this.PricebookEntryId      =    qli.PricebookEntryId;
            // this.UnitPrice_Page        =    qli.UnitPrice_Page__c;
            // this.AgencyUnitPrice        =    qli.AgencyUnitPrice__c;
            // this.AgencySubtotal        =    qli.AgencySubtotal__c;
            // this.Present        =    qli.Present__c;
            // this.multiYearWarranty        =    qli.multiYearWarranty__c;
            // this.If_Cancel_Guarantee        =    qli.If_Cancel_Guarantee__c;
            // this.GuaranteePeriod        =    qli.GuaranteePeriod__c;
            // this.ServicePrice        =    qli.ServicePrice__c;
            // this.GuranteePrice        =    qli.GuranteePrice__c;
            // this.ProductEntend_gurantee_period_all        =    qli.ProductEntend_gurantee_period_all__c;
            // this.ProductGuranteePrice        =    qli.ProductGuranteePrice__c;
            // this.GuranteeType        =    qli.GuranteeType__c;
            // this.warrantyType        =    qli.warrantyType__c;
            // this.productServicePrice        =    qli.productServicePrice__c;
            // this.NoDiscountTotal        =    qli.NoDiscountTotal__c;
            // this.provistonPeriod        =    qli.provistonPeriod__c;
            // this.Maintenance_Price_Year        =    qli.Maintenance_Price_Year__c;
            // this.Quoteid    =    qli.Quoteid;
        }
        //询价行遍历
        public QELine(OpportunityLineItem oli,String otcode,String proId,String tempPriceBookEntryId){
            this.pageOppObject = oli.clone();
            //insert 报价不需要id
            // this.Id      =    qli.Id;
            this.Asset_Model_No        =    oli.Asset_Model_No__c;
            //替换产品和价格手册
            if(oli.Asset_Model_No__c.equals(otcode)){
                this.pageOppObject.Product2Id=proId ;
                this.pageOppObject.PricebookEntryId=tempPriceBookEntryId  ;
            }
        }
 
    }
 
}