GWY
2022-05-21 a3460549533111815e7f73d6cef601a58031525d
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
public with sharing class SearchProductController {
 
    public Integer quoteEntryMaxSet {get; private set;}
 
    public List<ProductLine> cl {get;set;}
    public Boolean getHascl() {
        Boolean rtn = true;
        if (cl != null && cl.size() > 0) {
            rtn = false;
        }
        return rtn;
    }
 
    public String searchMode;                         // 検索モード
    public String openFlag {get;set;}                 // 親画面Flag
    public String lineNo {get;set;}                   // 親画面の選択した行
    public String val {get;set;}
    public String setStr {get;set;}
    public String oppId {get;set;}                    // 親画面のからの検索文字列
    public String SearchName {get;set;}               // 検索文字列
    public String baseUrl { get; set; }
    public String productSetVal {get;set;}
    public boolean oppflg {get;set;}
 
    public String productSegment {get;set;}
 
    public String openType {get;set;}
 
    public List<SelectOption> productSet { get; set; }
 
    public boolean is_hidden_user { get; set; }
    public boolean is_dealer_user { get; set; }
 
    public Boolean is_hidden_all { get; set; }
 
    public Boolean specialDealer { get; set; }
 
    public SearchProductController(ApexPages.StandardController controller){
        this();
    }
 
    public SearchProductController() {
        this.quoteEntryMaxSet = Integer.valueOf(System.Label.QuoteEntryMaxSet);
        //XHL-20190428-UpdateStart
        //this.productSetVal = Apexpages.currentPage().getParameters().get('set');
        //if (productSetVal!=null || productSetVal!=''){
        //} else {
        //   this.productSetVal = '单品';
        //}
        //XHL-20190428-UpdateEnd
        is_hidden_user = false;
        // String profileid = UserInfo.getProfileId().subString(0,15);
 
        //新的获取简档ID  calendarUtil.getMemberProfileID  这里用到的人员ID 和获取到的简档ID都是15位的
        String new_profileId = calendarUtil.getMemberProfileID(UserInfo.getUserId().subString(0,15));
 
        if (new_profileId.substring(0, 15) == System.Label.RT_BS_No_Price) {
            is_hidden_user = true;
        }
        is_dealer_user = false;
        if (UserInfo.getUserType() == 'PowerPartner') {
            is_dealer_user = true;
        }
 
        //XHL-20190428-UpdateStart
        //this.productSet = new List<SelectOption>();
        //this.productSet.add(new SelectOption('单品','单品'));
        //this.openType = Apexpages.currentPage().getParameters().get('openType');
        //if (openType == 'service') {
        //    this.productSet.add(new SelectOption('set01','零件费用'));
        //    this.productSet.add(new SelectOption('set02','维修费'));
        //    this.productSet.add(new SelectOption('set03','其他费用'));
        //} else {
        //    for (Integer i = 0; i < this.quoteEntryMaxSet; i++) {
        //        if (i < 9) {
        //            this.productSet.add(new SelectOption('set0'+ String.valueOf(i+1),'套装0'+ String.valueOf(i+1)));
        //        } else {
        //            this.productSet.add(new SelectOption('set'+ String.valueOf(i+1),'套装'+ String.valueOf(i+1)));
        //        }
        //    }
        //}
        //XHL-20190428-UpdateEnd
        this.openFlag = Apexpages.currentPage().getParameters().get('openFlag');
        this.lineNo = Apexpages.currentPage().getParameters().get('lineno');
        this.val = Apexpages.currentPage().getParameters().get('val');
        this.oppId = Apexpages.currentPage().getParameters().get('oppId');
 
        if (openFlag == 'opp') {
            oppflg = false;
        } else {
            oppflg = true;
        }
 
        if (val!=null && val!=''){
            SearchName = val;
            val = null;
        }
 
        cl = new List<ProductLine>();
    }
 
    public void init () {
        system.debug('init==========> start');
        system.debug('SearchName==========>' + SearchName);
        if (SearchName != null && SearchName != '') {
            serContact();
        }
 
        system.debug('init==========> end');
 
    }
 
    public PageReference serContact(){
        //is_hidden_all = false;
        is_hidden_all = true;
        List<Opportunity> oppList = [select Id,DealerId__c,RecordTypeId,CurrencyIsoCode,Pricebook2Id,ProductSegment__c,Machine_Parts__c,SalesChannel__c From Opportunity where Id = :oppid];
        //List<Opportunity> oppList = [select Id,DealerId__c,RecordTypeId,CurrencyIsoCode,Pricebook2Id,ProductSegment__c,Machine_Parts__c,SalesChannel__c,DealerId__c From Opportunity where Id = :oppid];
        //is_hidden_all = StaticParameter.specialDealerMap.containsKey(oppList[0].DealerId__c);
        //is_hidden_all = !StaticParameter.specialDealerMap.containsKey(oppList[0].DealerId__c);
        is_hidden_all = !IfspecialDealer(oppList[0].DealerId__c);
        system.debug('is_hidden_all---->'+is_hidden_all);
        String sCurrencyIsoCode = oppList[0].CurrencyIsoCode;
        String sPricebook2Id = oppList[0].Pricebook2Id;
        String sProductSegment = oppList[0].ProductSegment__c;
        productSegment =  oppList[0].ProductSegment__c;   
        boolean is_Parts_direct = false;
        boolean is_Replacement = false;
        if(oppList[0].RecordTypeId == System.Label.SSBD_Replacement){
            is_Replacement = true;
        }
        if (oppList[0].Machine_Parts__c == 'Parts' && oppList[0].SalesChannel__c == 'direct') {
            is_Parts_direct = true;
        }
        //if(StaticParameter.specialDealerMap.containsKey(oppList[0].DealerId__c)){
        if(IfspecialDealer(oppList[0].DealerId__c)){
            is_hidden_user = true;
            specialDealer = true;
        }
        system.debug('sCurrencyIsoCode==========>' + sCurrencyIsoCode);
        system.debug('sPricebook2Id==========>' + sPricebook2Id);
 
        String searchSql = 'select Id,Product2Id,SalesPrice__c,SalesPriceA__c,SalesPriceB__c,SalesPriceC__c,CostPrice__c,Pricebook2Id,Product2.ProductModels__c,Hidden_flag__c from PricebookEntry';
        //20191113-XHL---Start-将产品状态(ProductStatus__c)用产品状态(公式)(ProductStatusFormula__c)替换
        //String whereStr = '  Where (Product2.ProductModels__c = true or (SalesPrice__c > 0 and (CostPrice__c > 0 or Product2.Dummy__c = true))) and Product2.ProductStatus__c = \'1\' and Pricebook2Id = :sPricebook2Id and CurrencyIsoCode = :sCurrencyIsoCode';
        String whereStr = '';
        if(is_Replacement){
            boolean temp = !is_Replacement;
            whereStr = '  Where (Product2.ProductModels__c = true or (SalesPrice__c > 0 and (CostPrice__c > 0 or Product2.Dummy__c = true))) and Product2.ProductStatusFormula__c = \'1\' and Pricebook2Id = :sPricebook2Id and CurrencyIsoCode = :sCurrencyIsoCode and Product2.isMain__c =:temp';
            //20191113-XHL---End-将产品状态(ProductStatus__c)用产品状态(公式)(ProductStatusFormula__c)替换
            //String whereStr = '  Where (Product2.ProductModels__c = true or (SalesPrice__c > 0)) and Product2.ProductStatus__c = \'1\' and Pricebook2Id = :sPricebook2Id and CurrencyIsoCode = :sCurrencyIsoCode';
        
        }else{
            whereStr = '  Where (Product2.ProductModels__c = true or (SalesPrice__c > 0 and (CostPrice__c > 0 or Product2.Dummy__c = true))) and Product2.ProductStatusFormula__c = \'1\' and Pricebook2Id = :sPricebook2Id and CurrencyIsoCode = :sCurrencyIsoCode ';
            //20191113-XHL---End-将产品状态(ProductStatus__c)用产品状态(公式)(ProductStatusFormula__c)替换
            //String whereStr = '  Where (Product2.ProductModels__c = true or (SalesPrice__c > 0)) and Product2.ProductStatus__c = \'1\' and Pricebook2Id = :sPricebook2Id and CurrencyIsoCode = :sCurrencyIsoCode';
        
        }
        /*String whereStr = '  Where (Product2.ProductModels__c = true or (SalesPrice__c > 0 and (CostPrice__c > 0 or Product2.Dummy__c = true))) and Product2.ProductStatusFormula__c = \'1\' and Pricebook2Id = :sPricebook2Id and CurrencyIsoCode = :sCurrencyIsoCode and Product2.isReplacement__c =:is_Replacement';
        //20191113-XHL---End-将产品状态(ProductStatus__c)用产品状态(公式)(ProductStatusFormula__c)替换
        //String whereStr = '  Where (Product2.ProductModels__c = true or (SalesPrice__c > 0)) and Product2.ProductStatus__c = \'1\' and Pricebook2Id = :sPricebook2Id and CurrencyIsoCode = :sCurrencyIsoCode';*/
        if (is_Parts_direct) {
            //20191113-XHL---Start-将产品状态(ProductStatus__c)用产品状态(公式)(ProductStatusFormula__c)替换
            //whereStr = '  Where Product2.ProductStatus__c = \'1\' and Pricebook2Id = :sPricebook2Id and CurrencyIsoCode = :sCurrencyIsoCode';
            whereStr = '  Where Product2.ProductStatusFormula__c = \'1\' and Pricebook2Id = :sPricebook2Id and CurrencyIsoCode = :sCurrencyIsoCode';
            //20191113-XHL---End-将产品状态(ProductStatus__c)用产品状态(公式)(ProductStatusFormula__c)替换
        }
        String whereSql = ' and Product2.ManualHidden__c = false ' ;
        if (SearchName != null && SearchName != '') {
            String likename = String.escapeSingleQuotes(SearchName);
            String prdcd = String.escapeSingleQuotes(SearchName);
            String prdec = String.escapeSingleQuotes(SearchName);
            if (likename.indexOf('*') >= 0) {
                likename = likename.replace('*', '%');
            } else {
                likename = '%' + likename + '%';
            }
            if (prdcd.indexOf('*') >= 0) {
                prdcd = prdcd.replace('*', '%');
            } else {
                prdcd = '%' + prdcd + '%';
            }
            if (prdec.indexOf('*') >= 0) {
                prdec = prdcd.replace('*', '%');
            } else {
                prdec = '%' + prdec + '%';
            }
            whereSql += ' and (Product2.Name Like ' + '\'' + likename + '\' or Product2.ProductCode Like ' + '\'' + prdcd + '\' or Product2.Product_ECCode__c like ' + '\'' + prdec + '\')';
        }
 
        searchSql = searchSql + whereStr + whereSql;
        searchSql += ' order by Product2.ProductModels__c desc, Product2.ProductCode limit 200';
        System.debug('searchSql::::::' + searchSql);
 
        List<PricebookEntry> pbes = Database.query(searchSql);
 
        System.debug('pbes::::::' + pbes);
 
        if (pbes.size() > 0) {
 
            List<String> forSelect = new List<String>();
 
            for (PricebookEntry pbe : pbes) {
                forSelect.add(pbe.Product2Id);
            }
 
            System.debug('forSelect::::::' + forSelect);
 
            List<Product_Search__c>  productSearchList = [select Id,Name,ProductCode__c,Product_ECCode__c,ProductModels1__c, ProductDescription__c,ProductStatus__c,Product__c
                                                            from Product_Search__c
                                                           where Product__c in : forSelect];
 
            System.debug('productSearchList::::::' + productSearchList);
 
            Map<String, Product_Search__c> tempMap = new Map<String, Product_Search__c>();
 
            for (Product_Search__c productSearch : productSearchList) {
                tempMap.put(productSearch.Product__c, productSearch);
            }
 
            System.debug('tempMap::::::' + tempMap);
 
            Integer lineNo = 0;
            cl = new List<ProductLine>();
            for (Integer i = 0; i < pbes.size(); i++) {
                System.debug('tempMap.get(pbes[i].Product2Id)::::::' + tempMap.get(pbes[i].Product2Id));
                Product_Search__c ps = tempMap.get(pbes[i].Product2Id);
                if (ps != null) {
                    if (pbes[i].SalesPrice__c == null) {
                        ProductLine pl = new ProductLine(lineNo, ps, pbes[i].Id,'0.00','0.00','0.00','0.00',pbes[i].Pricebook2Id,pbes[i].Hidden_flag__c);
                        cl.add(pl);
                    } else {
                        ProductLine pl = new ProductLine(lineNo, ps,pbes[i].Id,
                                                         String.valueOf(pbes[i].SalesPrice__c),
                                                         String.valueOf(pbes[i].SalesPriceA__c),
                                                         String.valueOf(pbes[i].SalesPriceB__c),
                                                         String.valueOf(pbes[i].SalesPriceC__c),
                                                         pbes[i].Pricebook2Id,
                                                         pbes[i].Hidden_flag__c);
                        if (is_hidden_user == true || is_dealer_user == true && pl.hiddenflg == true) {
                            pl.salespriceR = '0.00';
                        }
                        cl.add(pl);
                    }
 
                    lineNo++;
                    // 最多显示100条
                    if (lineNo >= 100) {
                        break;
                    }
                }
            }
        }   
 
        return null;
    }
 
    public static Boolean IfspecialDealer(String oppDealerId){
        if (String.isBlank(oppDealerId)) {
            return false;
        }
        //return StaticParameter.specialDealerMap.containsKey(oppDealerId.substring(0,15));
        return StaticParameter.specialDealerMap.containsKey(oppDealerId.substring(0,15)) && UserInfo.getUserType() == 'PowerPartner';
    }
 
    public class ProductLine {
        public Integer idx {get; set;}
        public Product_Search__c pbe {get; set;}
        public String pbeId {get; set;}
        public String pbId {get; set;}
        public String salesprice {get; set;}
        public String salespriceA {get; set;}
        public String salespriceB {get; set;}
        public String salespriceC {get; set;}
        public boolean hiddenflg {get; set;}
        public String salespriceR {get; set;}
        
        public ProductLine(Integer i, Product_Search__c pbe, String pbeId, String salespriceStr, String salespriceAStr, String salespriceBStr, String salespriceCStr, String pbId, boolean flag) {
            this.idx = i;
            this.pbe = pbe;
            this.pbeId = pbeId;
            this.pbId = pbId;
            System.debug('pbeId=======>' +i+'==' + pbeId);
            this.salesprice = salespriceStr;
            this.salespriceA = salespriceAStr;
            this.salespriceB = salespriceBStr;
            this.salespriceC = salespriceCStr;
            this.hiddenflg = flag;
            this.salespriceR = salespriceStr;
        }
    }
}