涂煌豪
2022-05-06 98e508d2852896dbde98edcc9ed6d87645a6a230
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
public with sharing class MceConfigPDFController {
    /** 処理対象の見積オブジェクトです。 */
    public EstimateInfo targetObj { get; private set; }
    /** 見積オブジェクトのリスト(改ページ用) */
    public List<EstimateInfo> targetObjList { get; private set; }
    /** 1ページに表示する明細の数です。 */
    //public Boolean HideOrNot{ get; private set; }
    private Integer rowSize = 30;   // TODO カスタムラベルに定義
    
    private Integer nameMax = 20;   //名称最大长度 全是中文的话 最多18 但因为不会全是中文 所以设为20
    private Integer commentMax = 5; //备注最大长度
    //private Integer modelNoMax = 9; //型号最大长度
 
    private Integer lineNo = 0;
    
    /** 最終ページに表示する明細の数です。 */
    //private Integer lastRowSize = 24;
    /** 印刷するページ数です。 */
    //public Integer maxPageNumber { get; private set; }
 
    public String  RecordTypeName { get; private set; }
    public Integer year { get; private set; }
    public String flag { get; private set; }
    public Integer getTargetObjSize() {
        return this.targetObjList.size();
    }
    
    /**
     * Visualforceページがアクセスするコンストラクタです。
     * 印刷するデータを収集し、インスタンス変数にセットします。
     * @param controller スタンダードコントローラーのインスタンス
     */
    public MceConfigPDFController(ApexPages.StandardController controller) {
    }
    public void init() {
        String targetEstimateId = ApexPages.currentPage().getParameters().get('id');
        flag = ApexPages.currentPage().getParameters().get('flag');
        // 見積情報を取得、必ずあるはず
        List<Maintenance_Contract_Estimate__c> mceList = [SELECT Id, Name, Maintenance_Contract__c, Estimate_Target__c, Dealer__r.Name,
                                                                 Maintenance_Contract__r.Management_Code__c, CreatedBy.fen_corp_name__c, 
                                                                 Maintenance_Contract__r.Hospital__r.Name, Department__c,
                                                                 Contract_Range__c, Contract_Esti_Start_Date__c, Contract_Esti_End_Date__c,
                                                                 Contract_Start_Date__c, Contract_End_Date__c,Maintenance_Contract__r.SalesOfficeCode_selection__c,
                                                                 Estimate_Trial_Money__c, Maintenance_Price__c, NotUse_Oxygenated_Water__c,
                                                                 Examination_Price__c, Service_contract_target_number__c, Append_Condition_Price__c,
                                                                 Discount_Price__c, Discount_Percentage__c, Last_Discount__c, Asset_Sum_Price__c,
                                                                 Print_Contract__c, Print_RepairPrice__c, Print_DiscountPercentage__c,
                                                                 Print_DiscountPrice__c, Print_ListPrice__c, Print_MaintePrice__c, Print_SumPrice__c,
                                                                 Is_New_Contract__c, // HWAG-B5C88S 2018/11/06
                                                                 RecordType.DeveloperName
                                                            FROM Maintenance_Contract_Estimate__c WHERE Id = :targetEstimateId];
        // 見積を取得できない場合
        if (mceList.size() == 0) {
            throw new ControllerUtil.myException('报价不存在');
        }
        
        this.targetObj = new EstimateInfo(mceList);
        targetObjList = new List<EstimateInfo>();
        EstimateInfo tmpTargetObj = new EstimateInfo(mceList);
 
        RecordTypeName = mceList[0].RecordType.DeveloperName;
        // 明細の設定
        Integer cnt = 0;
        List<Maintenance_Contract_Asset_Estimate__c> MaintenanceCAE = [SELECT Id, Name, Asset__c, Maintenance_Contract_Estimate__r.Department__c,
                        Asset__r.Name, Asset__r.SerialNumber, Asset__r.InstallDate,
                        Asset__r.Product2.Name,
                        Product_Manual__c, Product_Manual__r.Name, IsNew__c,
                        Maintenance_Price_Month__c, Estimate_List_Price__c,
                        Check_Result__c, Repair_Price__c, Comment__c,Characteristic__c,
                        Check_Object__c //wangweipeng 20210610
                   FROM Maintenance_Contract_Asset_Estimate__c
                  WHERE Maintenance_Contract_Estimate__c = :this.targetObj.estimate.Id
                  ORDER BY Product_Manual__c, Asset__r.SerialNumber, Asset__r.Name, Asset__r.Department_Name__c, Asset__r.InstallDate];
                  system.debug('检查::::::'+MaintenanceCAE);
        for(Maintenance_Contract_Asset_Estimate__c mcae :MaintenanceCAE) {
            //for(Integer a=0;a<50;a++){
            //tmpTargetObj.addAssetInfo(mcae);
            //HideOrNot = false;
            system.debug(mcae.Product_Manual__r.Name+':::::::::::'+mcae.Product_Manual__c);
            String sname = '';
            if (mcae.Asset__c !=  null) {
                sname = mcae.Asset__r.Product2.Name == null ? '' : mcae.Asset__r.Product2.Name;
            } else {
                sname = mcae.Product_Manual__r.Name == null ? '' : mcae.Product_Manual__r.Name;
            }
            String smodelNo = '';
            String scomment = mcae.Comment__c == null ? '' : mcae.Comment__c;
            if (sname.indexOf(':') >= 0) {
                smodelNo = sname.subString(0, sname.indexOf(':')+1);
                sname = sname.subString(sname.indexOf(':')+1);
            } else if (sname.indexOf(':') >= 0) {
                smodelNo = sname.subString(0, sname.indexOf(':')+1);
                sname = sname.subString(sname.indexOf(':')+1);
            } else {
                smodelNo = '';
            }
 
            //型号行数
            /*Integer imodelNo =(smodelNo.length() / modelNoMax) + (Math.mod(smodelNo.length(), modelNoMax) > 0 ? 1 : 0);
            if(imodelNo == 0){
                imodelNo = 1;
            }*/
 
            //机器名行数
            
            Integer iname = (sname.length() / nameMax) + (Math.mod(sname.length(), nameMax) > 0 ? 1 : 0);
            if (iname == 0) {
                iname = 1;
            }
            //备注行数
            Integer icomment = (scomment.length() / commentMax) + (Math.mod(scomment.length(), commentMax) > 0 ? 1 : 0);
            if (icomment == 0) {
                icomment = 1;
            }
            //没个td中的实际行数
            Integer icnt = iname;
            if (iname < icomment) {
                /*if(icomment < imodelNo){
                    icnt = imodelNo;
                }else{
                    icnt = icomment;
                }*/
                icnt = icomment;
            }/*else{
                if(icomment < imodelNo){
                    if(iname < imodelNo){
                        icnt = imodelNo;
                    }
                }
            }*/
 
            
            if (mcae.Asset__c !=  null) {
                mcae.Asset__r.Name = smodelNo + sname;
            } else {
                mcae.Product_Manual__r.Name = smodelNo + sname;
                system.debug('测试一下这个位置sname'+sname);
            }
            
            cnt+=icnt;
            //
            if (cnt > this.rowSize) {
                cnt = icnt;
                //HideOrNot = false;
                targetObjList.add(tmpTargetObj);
                tmpTargetObj = new EstimateInfo(mceList);
                
            }
 
 
            // 改ページ判断
            lineNo += 1;
            tmpTargetObj.addAssetInfo(lineNo, mcae, nameMax,commentMax);
            
            /*if (icnt > 1) {
                for (Integer i = 0; i < icnt; i++) {
                    if  (i == 0) {//第一行
                        if (mcae.Asset__c !=  null) {
                            if (i + 1 == iname) {
                                mcae.Asset__r.Name = smodelNo + sname;
                            } else {
                                mcae.Asset__r.Name = smodelNo + sname.substring(0, nameMax);
                            }
                        } else {
                            if (i + 1 == iname) {
                                mcae.Product_Manual__r.Name = smodelNo + sname;
                                system.debug('测试一下这个位置sname'+sname);
                            } else {
                                mcae.Product_Manual__r.Name = smodelNo + sname.substring(0, nameMax);
                                //mcae.Product_Manual__r.Name = '哈哈哈哈';
                            }
                        }
                        if (i + 1 == icomment) {
                            mcae.Comment__c = scomment;
                        } else {
                            mcae.Comment__c = scomment.substring(0, commentMax);
                        }
                        lineNo += 1;
                        tmpTargetObj.addAssetInfo(lineNo, mcae,HideOrNot);
                    } else {
                        system.debug('Before clone::::'+mcae.Product_Manual__r.Name);
                        Maintenance_Contract_Asset_Estimate__c newmcae = mcae.clone();
                        if (mcae.Asset__c !=  null) {
                            if (i < iname) {
                                if (i + 1 == iname) {
                                    newmcae.Asset__r.Name = sname.substring(i*nameMax);
                                } else {
                                    newmcae.Asset__r.Name = sname.substring(i*nameMax, (i+1)*nameMax);
                                }
                            } else {
                                newmcae.Asset__r.Name = '';
                            }
                        } else {
                            if (i < iname) {
                                if (i + 1 == iname) {
                                    newmcae.Product_Manual__r.Name = sname.substring(i*nameMax);
                                } else {
                                    newmcae.Product_Manual__r.Name = sname.substring(i*nameMax, (i+1)*nameMax);
                                }
                            } else {
                                system.debug('Before Clear::::::'+newmcae.Product_Manual__r.Name);
                                //newmcae.Product_Manual__r.Name = '';
                                HideOrNot = true;
                            }
                        }
                        if (i < icomment) {
                            if (i + 1 == icomment) {
                                newmcae.Comment__c = scomment.substring(i*commentMax);
                            } else {
                                newmcae.Comment__c = scomment.substring(i*commentMax, (i+1)*commentMax);
                            }
                        } else {
                            newmcae.Comment__c = '';
                        }
                        tmpTargetObj.addAssetInfo(lineNo, newmcae,HideOrNot);
                        
                        AssetInfo ainfo = tmpTargetObj.assetInfoList.get(tmpTargetObj.assetInfoList.size()-1);
                        ainfo.lineNo = null;
                        ainfo.assetModelNo = null;
                        ainfo.assetSerialNumber = null;
                    }
                    // 改ページ判断
                    if (cnt == this.rowSize) {
                        cnt = 0;
                        HideOrNot = false;
                        targetObjList.add(tmpTargetObj);
                        tmpTargetObj = new EstimateInfo(mceList);
                    }
                    cnt++;
                }
            } else {
                lineNo += 1;
                if (mcae.Asset__c != null) {
                    mcae.Asset__r.Name = smodelNo + sname;
                } else {
                    mcae.Product_Manual__r.Name = smodelNo + sname;
                    system.debug('::::测试极端'+smodelNo + sname);
                }
                tmpTargetObj.addAssetInfo(lineNo, mcae,HideOrNot);
                // 改ページ判断
                if (cnt == this.rowSize) {
                    cnt = 0;
                    HideOrNot = false;
                    targetObjList.add(tmpTargetObj);
                    tmpTargetObj = new EstimateInfo(mceList);
                }
                cnt++;
            }*/
        }
        
        if (tmpTargetObj.assetInfoList.size() > 0) {
            targetObjList.add(tmpTargetObj);
        }
        
        // 会計年度計算、ただしここは 日本の年度ではない
//        Integer y = this.targetObj.estimate.Contract_Esti_Start_Date__c.year();
//        Integer m = this.targetObj.estimate.Contract_Esti_Start_Date__c.month();
        Integer y = this.targetObj.estimate.Contract_Start_Date__c.year();
        Integer m = this.targetObj.estimate.Contract_Start_Date__c.month();
        if (m >= 4) {
            this.year = y;
        } else {
            this.year = y;  // y - 1;
        }
    }
    
    private static Map<String, String> companyApiMap = new Map<String, String> {
        '北京RC' => '奥林巴斯(北京)销售服务有限公司',
        '上海RC' => '奥林巴斯(北京)销售服务有限公司上海分公司',
        '广州RC' => '奥林巴斯(北京)销售服务有限公司广州分公司',
        '沈阳RC' => '奥林巴斯(北京)销售服务有限公司',
        //wangweipeng  2021/07/30   start
        '成都RC' => '奥林巴斯(北京)销售服务有限公司成都维修分公司',
        '西安RC' => '奥林巴斯(北京)销售服务有限公司西安维修分公司',
        '杭州RC' => '奥林巴斯(北京)销售服务有限公司杭州维修分公司'
        //wangweipeng  2021/07/30   start
    };
 
    /**
     * 印刷するデータを格納するオブジェクを定義する、親
     */
    public class EstimateInfo {
        public Maintenance_Contract_Estimate__c estimate {get; private set;}
        public List<AssetInfo> assetInfoList {get; private set;}
        public String BCompany {get; private set;}
        public String HospitalName {get; private set;}
        
        private String returnNewStr(String hospitalName){
            String hospitalNames = '';
            if(hospitalName.contains('/')){
                Integer index = hospitalName.lastIndexOf('/');
                hospitalNames = hospitalName.substring(0,index)+'<br/>'+hospitalName.substring(index+1);
            }else{
                if(hospitalName.length()>22){
                    hospitalNames = hospitalName.substring(0,22)+'<br/>'+hospitalName.substring(22);
                }else{
                    hospitalNames = hospitalName;
                }
                
            }
            return hospitalNames;
        }
        
        public EstimateInfo(List<Maintenance_Contract_Estimate__c> estimateList) {
            if (estimateList.size() > 0) {
                this.estimate = estimateList[0];
                this.assetInfoList = new List<AssetInfo>();
                this.BCompany = companyApiMap.get(estimate.Maintenance_Contract__r.SalesOfficeCode_selection__c);
                String Hospital = estimate.Maintenance_Contract__r.Hospital__r.Name;
                
                this.HospitalName = returnNewStr(Hospital);
            }
        }
        public void addAssetInfo(Integer lno, Maintenance_Contract_Asset_Estimate__c mcae, Integer nameMax, Integer commentMax) {
            this.assetInfoList.add(new AssetInfo(lno, mcae, estimate, nameMax, commentMax));
        }
    }
                                   
    /**
     * 印刷するデータを格納するオブジェクを定義する、子供
     */
    public class AssetInfo {
        public Integer lineNo {get; private set;}
        public Maintenance_Contract_Asset_Estimate__c mcae {get; private set;}
        public String assetModelNo {get; private set;}
        public String assetName {get; private set;}
        public String assetSerialNumber {get; private set;}
        public String assetComment {get; private set;}
        //public boolean NeedHide {get;private set;}
        public AssetInfo() {
            this.mcae = null;
        }
        
 
        private String Substr(Integer length, String parameter){
            String params = '';
            Integer size = (parameter.length() / length) + (Math.mod(parameter.length(), length) > 0 ? 1 :0);
            if(size == 0){
                size = 1;
            }
            for(Integer I =0; I<size;I++){
                String s;
                if(I == (size-1)){
                    s = parameter.substring(I*length,parameter.length());
                }else{
                    s = parameter.substring(I*length,(I+1)*length)+'<br/>';
                }
                
                params += s;
            }
            return params;
        }
 
 
        public AssetInfo(Integer lineNo, Maintenance_Contract_Asset_Estimate__c mcae, Maintenance_Contract_Estimate__c estimate, Integer nameMax, Integer commentMax) {
            this.lineNo = lineNo;
            this.mcae = mcae;
 
            //this.NeedHide = HideOrNot;
            if (mcae.Asset__c != null) {
                // Assetある
                this.assetName = mcae.Asset__r.Name+'';
                this.assetSerialNumber = mcae.Asset__r.SerialNumber;
            } else {
                // 手入力
                this.assetName = mcae.Product_Manual__r.Name+'';
                this.assetSerialNumber = '新购入';
            }
            // 名前 ':' の対応
            this.assetName = assetName == null ? '' : assetName;
            String assetModelNotemp;
            String assetNametemp;
            if (this.assetName.indexOf(':') >= 0) {
                assetModelNotemp = this.assetName.subString(0, this.assetName.indexOf(':'));
                assetNametemp = this.assetName.subString(this.assetName.indexOf(':')+1);
            }
            else if (this.assetName.indexOf(':') >= 0) {
                assetModelNotemp = this.assetName.subString(0, this.assetName.indexOf( ':'));
                assetNametemp = this.assetName.subString(this.assetName.indexOf(':')+1);
            }
            else {
                this.assetModelNo = '';
            }
            this.assetModelNo  = assetModelNotemp == null ? '' : assetModelNotemp;
            //this.assetModelNo = Substr(modelNoMax,assetModelNotemp);
            assetNametemp = assetNametemp == null ? '' : assetNametemp;
            this.assetName = Substr(nameMax,assetNametemp);
            String assetCommenttemp = mcae.Comment__c == null ? '' : mcae.Comment__c;
            this.assetComment = Substr(commentMax,assetCommenttemp);
 
 
 
        }
    }
}