高章伟
2023-03-03 d8dc84a3d56df839895f1c417a4d9cbee763d262
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
public without sharing class PrintConsumblePDFController {
    /*****************検索用******************/
    public Consumable_order__c coc { get; set; }
    public String gestName{get;set;}
    //供货商名称
    public String addressName {get;set;}
    /** 1ページに表示する明細の数です。 */
    private Integer rowSize = 32;
 
    /** 最終ページに表示する明細の数です。 */
    private Integer lastRowSize = 24;
 
    /** 製品名を開業する文字数です。 */
    private Integer nameMax = 23;
    /** 印刷するページ数です。 */
    public Integer maxPageNumber { get; set; }
    /** 印刷対象の見積の配下にある見積品目データです。 */
    public List<ConsumableorderdetailsInfo[]> printRecords {get; private set;}
    public String baseUrl {get;private set;}
 
    // 产品 ID
    private String ESetId = '';
    private String userName = '';
 
    //经销商管理编码 -- 电子签收ID号
    public String idName{get;set;}
 
    public PrintConsumblePDFController(){
        //Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=8');
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        ESetId = ApexPages.currentPage().getParameters().get('esetId');
    }
    public void init(){
        userName = UserInfo.getUserId();
        List<user> Useracc = New List<user>();
        Useracc = [select name,accountid from user where id =:userName];
        String accountid = Useracc[0].accountid;
        //update by rentx 2020-11-11 start CHAN-BV2677
        // List<account> accountInfo = [SELECT name FROM account WHERE id =:accountid];
        List<account> accountInfo = [SELECT name,Management_FormulaCode__c FROM account WHERE id =:accountid];
        gestName = accountInfo[0].name;
        idName = accountInfo[0].Management_FormulaCode__c;
        //update by rentx 2020-11-11 end CHAN-BV2677
 
        coc = new Consumable_order__c();
        List<ConsumableorderdetailsInfo> ConsumableorderdetailsRecords = new List<ConsumableorderdetailsInfo>();
        List<Consumable_Orderdetails__c> CountSet = new List<Consumable_Orderdetails__c>();
        if(ESetId== NULL || ESetId==''){
        }else{
            List<Consumable_order__c> qs = New List<Consumable_order__c>();
            qs = [select Id,Name,Order_status__c,Dealer_Info__c,
            Order_ForHospital__c,Deliver_date__c,Order_Reason__c,
            Order_date__c,Total_amount__c,Order_ProType__c,
            Offers_Price__c,Order_effective_contact__r.Business_Assistant__r.Province_Text__c
            From Consumable_order__c 
            Where Id =:ESetId];
         if (qs.size()>0){
                coc = qs[0];
            }
            this.rowSize = 42;
            this.lastRowSize = 42;
            this.nameMax = 18;
            // 選択済みの明细を取得
            bindQuoteLineData();
            this.getShipmentaddress();
        }
    }
 
    /**
     * 見積品目データを取得して、インスタンス変数にセットします。
     */
    private void bindQuoteLineData(){
        System.debug('ESetId +++++' + ESetId);
        this.maxPageNumber = 0;
        String nowId = coc.Id;
        Integer repeatCount = null;
        String nowName = null, nowRightAsstModelNo = null;
        //String nowCompany = null;
        boolean hasEnglish = false;
        Consumable_Orderdetails__c work = null;
        List<String> nameStringArray = null;
        Consumable_Orderdetails__c[] itemsOrg = [select Id,  Name,
                                    Consumable_order__c,
                                    Consumable_Product__c,
                                    Consumable_Product__r.Name,
                                    Consumable_Product__r.Name__c,
                                    CFDA_Status__c,
                                    Consumable_Count__c,
                                    Consumable_Product__r.Intra_Trade_List_RMB__c,
                                    Consumable_Product__r.Asset_Model_No__c,
                                    Consumable_Product__r.Product2__r.SalesPackagingUnit_Chinese__c,
                                    Consumable_Product__r.Product2__r.ProduceCompany_F__c,
                                    Consumable_Product__r.Product2__r.ProduceCompany__c,
                                    Sum_of_money__c,
                                    Dealer_Custom_Price__c,
                                    Lose_reason__c,
                                    ProduceCompany__c,
                                    ProductPacking_list_manual__c,
                                    CFDA_No__c,
                                    Intra_Trade_List_RMB__c,
                                    Qty_Unit__c,
                                    Asset_Model_No__c
                                from Consumable_Orderdetails__c
                                where  Consumable_order__c = :ESetId
                                order by Name];
                                System.debug('itemsOrg+++++' + itemsOrg.size());
        Consumable_Orderdetails__c[] items = new List<Consumable_Orderdetails__c>();
        for (Integer i = 0; i < itemsOrg.size(); i++)  {
            nowName = itemsOrg[i].Consumable_Product__r.Name__c == null ? '' : itemsOrg[i].Consumable_Product__r.Name__c;
             //CHAN-B368WS 当产品编号为:N/A时,打印报价单/合同配置单/系统合同配置单时,打印的产品型号处空白(现在显示产品CODE)
            if(itemsOrg[i].Asset_Model_No__c == '' || itemsOrg[i].Asset_Model_No__c == 'N/A'){
                nowRightAsstModelNo = '';
            }else if( nowName.indexOf( ':') >= 0) {
                nowRightAsstModelNo = nowName.subString( 0, nowName.indexOf( ':'));
                nowName = nowName.subString( nowName.indexOf( ':')+1);
            }
            else if( nowName.indexOf( ':') >= 0) {
                nowRightAsstModelNo = nowName.subString( 0, nowName.indexOf( ':'));
                nowName = nowName.subString( nowName.indexOf( ':')+1);
            }
            else {
                nowRightAsstModelNo = '';
            }
            nameStringArray = new List<String>();
            // 20220824 ljh SWAG-CHKAMM update start
            // repeatCount = (nowName.length() / nameMax) + ( Math.mod( nowName.length(), nameMax) > 0 ? 1 : 0);
            if(String.isBlank(nowName)){
                repeatCount = 1;
            }else{
                repeatCount = (nowName.length() / nameMax) + ( Math.mod( nowName.length(), nameMax) > 0 ? 1 : 0);
            }
            // 20220824 ljh SWAG-CHKAMM update end
            //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, 'repeatCount!' + repeatCount));
            // 生产企业 注释原代码
            // nowCompany = itemsOrg[i].Consumable_Product__r.Product2__r.ProduceCompany_F__c;
            String nowCompany = null;
            if(itemsOrg[i].Consumable_Product__r.Product2__r.ProduceCompany__c !=null){
                nowCompany = getProduceCompanyName(itemsOrg[i].Consumable_Product__r.Product2__r.ProduceCompany__c);
            }
            if (nowCompany == null) nowCompany = '';
            //hasEnglish = Pattern.matches('.*[a-zA-Z]+.*', nowCompany);
            //Integer companyMax = hasEnglish ? 16 : 12;
            //Integer repeatCount2 = (nowCompany.length() / companyMax) + ( Math.mod( nowCompany.length(), companyMax) > 0 ? 1 : 0);
            //String descri = itemsOrg[i].Description == null ? '' : itemsOrg[i].Description;
            //Integer repeetCount3 = (descri.length() / 12) + (Math.mod(descri.length(), 12) > 0 ? 1 : 0);
 
 
 
            for ( Integer j = 0; j < repeatCount; j++) {
                if (( j + 1) == repeatCount) {
                    nameStringArray.add( nowName.substring( j * nameMax));
                }
                else {
                    nameStringArray.add( nowName.substring( j * nameMax, (j+1) * nameMax));
                }
            }
            //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, 'nameStringArray!' + nameStringArray));
            for ( Integer k = 0; k < repeatCount; k++) {
                if ( k == 0) {
                    work = itemsOrg[i];
                    work.Remarks__c = ':';
                    work.Product_code__c = nowRightAsstModelNo;
                    work.AgencyName_text1__c = nowCompany.trim();
                    work.Shipment_Count__c = i + 1;
                }
                else {
                    work = new Consumable_Orderdetails__c();
                    work.Remarks__c = '';
                    work.Product_code__c = '';
                }
                work.Lose_reason__c = nameStringArray[k];
                items.add(work);
            }
        }
 
        printRecords = new List<ConsumableorderdetailsInfo[]>();
 
        ConsumableorderdetailsInfo[] pageItems = new ConsumableorderdetailsInfo[]{};
        Integer counter = 0;
 
        for (Integer i = 0; i < items.size(); i++) {
 
            if ( counter <= rowSize) {
                pageItems.add(new ConsumableorderdetailsInfo(items[i]));
                counter++;
            }
            if ( counter == rowSize) {
                counter = 0;
                printRecords.add( pageItems);
                pageItems = new ConsumableorderdetailsInfo[]{};
                this.maxPageNumber += 1;
            }
        }
        if ( !pageItems.isEmpty()) {
            printRecords.add( pageItems);
            this.maxPageNumber += 1;
        }
        
        if ( printRecords.size() > 0) {
            List<ConsumableorderdetailsInfo> lastList = printRecords[printRecords.size()-1];
            if ( lastList.size() > lastRowSize) {
                printRecords.add( new List<ConsumableorderdetailsInfo>());
                this.maxPageNumber += 1;
            }
        }
        System.debug('printRecords +++++' + printRecords);
    }
 
    public class ConsumableorderdetailsInfo {
        public Consumable_Orderdetails__c esd { get; set; }
        public Product2__c Prod { get; set; }
        public Integer lineNo { get; set; }
        public String unit { get; set; }
 
        // 订单明细用
        public ConsumableorderdetailsInfo(Consumable_Orderdetails__c e) {
            esd = e;
            Prod = e.Consumable_Product__r;
            lineNo = Integer.valueOf(e.Shipment_Count__c);
            if(Prod != null){
                unit = Prod.Product2__r.SalesPackagingUnit_Chinese__c;
                if(String.isBlank(unit)){
                    unit = '件';
                }
            }
            //unit = Prod.Product2__r.SalesPackagingUnit_Chinese__c;
            //if(String.isBlank(unit)){
            //    unit = '件';
            //}
        }
    }
 
    private static Map<String, String> addressNameApiMap = new Map<String, String> {
        '湖南省' => '奥林巴斯(北京)销售服务有限公司广州分公司',
        '四川省' => '奥林巴斯(北京)销售服务有限公司广州分公司',
        '广东省' => '奥林巴斯(北京)销售服务有限公司广州分公司',
        '贵州省' => '奥林巴斯(北京)销售服务有限公司广州分公司',
        '云南省' => '奥林巴斯(北京)销售服务有限公司广州分公司',
        '广西自治区' => '奥林巴斯(北京)销售服务有限公司广州分公司',
        '深圳市' => '奥林巴斯(北京)销售服务有限公司广州分公司',
        '湖北省' => '奥林巴斯(北京)销售服务有限公司广州分公司',
        '重庆市' => '奥林巴斯(北京)销售服务有限公司广州分公司',
        '北京市' => '奥林巴斯(北京)销售服务有限公司',
        '吉林省' => '奥林巴斯(北京)销售服务有限公司',
        '大连市' => '奥林巴斯(北京)销售服务有限公司',
        '黑龙江省' => '奥林巴斯(北京)销售服务有限公司',
        '内蒙古' => '奥林巴斯(北京)销售服务有限公司',
        '山东省' => '奥林巴斯(北京)销售服务有限公司',
        '甘肃省' => '奥林巴斯(北京)销售服务有限公司',
        '青岛市' => '奥林巴斯(北京)销售服务有限公司',
        '辽宁省' => '奥林巴斯(北京)销售服务有限公司',
        '河北省' => '奥林巴斯(北京)销售服务有限公司',
        '山西省' => '奥林巴斯(北京)销售服务有限公司',
        '天津市' => '奥林巴斯(北京)销售服务有限公司',
        '新疆自治区' => '奥林巴斯(北京)销售服务有限公司',
        '陕西省' => '奥林巴斯(北京)销售服务有限公司',
        '青海省' => '奥林巴斯(北京)销售服务有限公司',
        '宁夏自治区' => '奥林巴斯(北京)销售服务有限公司',
        '河南省' => '奥林巴斯(北京)销售服务有限公司',
        '浙江省' => '奥林巴斯(北京)销售服务有限公司上海分公司',
        '安徽省' => '奥林巴斯(北京)销售服务有限公司上海分公司',
        '江西省' => '奥林巴斯(北京)销售服务有限公司上海分公司',
        '福建省' => '奥林巴斯(北京)销售服务有限公司上海分公司',
        '江苏省' => '奥林巴斯(北京)销售服务有限公司上海分公司',
        '上海市' => '奥林巴斯(北京)销售服务有限公司上海分公司'
    };
    //获取办事处地址
    private void getShipmentaddress(){
        //CHAN-B42D6F 安徽ET特约经销商来的询价  SAP上传省 紧急对应为北京 以后会修改 TODO
        //CHAN-BTF64C 安徽ENG经销商的询价默认SAP上传省修改 update by rentongxiao 2020-09-14 start
        // String province = coc.Order_effective_contact__r.Business_Assistant__r.Province_Text__c == '安徽省'  ? '北京市' : coc.Order_effective_contact__r.Business_Assistant__r.Province_Text__c;
        String province = coc.Order_effective_contact__r.Business_Assistant__r.Province_Text__c == '安徽省' && coc.Order_ProType__c == 'ET'  ? '北京市' : coc.Order_effective_contact__r.Business_Assistant__r.Province_Text__c;
        //CHAN-BTF64C 安徽ENG经销商的询价默认SAP上传省修改 update by rentongxiao 2020-09-14 end
 
 
        if(addressNameApiMap.get(province) != null){
            this.addressName = addressNameApiMap.get(province);
        }else{
            this.addressName = ' ';
        }
    }
 
    //产品主数据生产企业 获取企业名称
    private String getProduceCompanyName(String produceCompany){
        //获取企业名称 (生产企业,注册证状态)
        //getProduceCompanyName(String produceCompany,String sfdaApprobatedStatus)
        //取前一部分 eg: 生产企业名称 :
        //String firstHalf = '';
 
        //①统一中英文符号  找不到就返回原数据
        String tempProduceCompany = produceCompany.replace(';', ';');
        tempProduceCompany = tempProduceCompany.replace(':', ':');
 
        //②先截取第一个分号前的数据
        if(tempProduceCompany.indexOf(';')>0){
                 tempProduceCompany = tempProduceCompany.substring(0, tempProduceCompany.indexOf(';'));
            }
 
        
        //③截取第一个分号前 冒号后 分号前的数据
        if(tempProduceCompany.indexOf(':') > 0){
            //如果需要传入注册证状态的话 再拼接前一部分
            //firstHalf = tempProduceCompany.substring(0, tempProduceCompany.indexOf(':')+1);
 
            tempProduceCompany = tempProduceCompany.substring(tempProduceCompany.indexOf(':')+1, tempProduceCompany.length());
        }
        
        
        //④正则 只取出中文 不包括 全角符号  # %  不包括英文名
        //[\u4E00-\u9FA5\\s]+ 多个汉字,包括空格
        //[\u4E00-\u9FA5]+ 多个汉字,不包括空格
        //[\u4E00-\u9FA5] 一个汉字
        ///u0800-/u4e00 日文
        //输出中日文
        Pattern p = Pattern.compile('([\u0800-\u9fa5()()]+)');
        Matcher pm = p.matcher(tempProduceCompany);
        if( pm.find() ){
            system.debug('123');
             tempProduceCompany = pm.group(0);
        }
        //如果注册证等于不要 拼接前一部分
        // if(sfdaApprobatedStatus == '不要'){
        //     //如果前半部分不为空 则拼接上前半部分
        //     if(firstHalf!= ''){
        //         tempProduceCompany = firstHalf +tempProduceCompany;
        //     }
        // }
        return tempProduceCompany;
    }
}