高章伟
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
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
public with sharing class ReceivingNoteListFirstController  extends CreateRelationListPagingCtrlBase {
    public override Integer getSearchNumMax() {
        //各ページに制御あれば、最大件数を指定する
        // searchNumMax = Integer.valueOf(Label.Product_Select_Limit);
        // searchNumMax = 20;
        pagesize = '20';
        return searchNumMax;
    }
 
    /* 選択されたデータ取得用Soql Fromから*/
    public override String getSelectedDataSql() {
        // オブジェクトAPI名
        selectedDataSql = ' From ReceivingNote__c';
        selectedDataSql += ' where id = null ';
        selectedDataSql += ' order by Id';
        if (myComponentController.columnLeftRW != null) {
            for (String key : myComponentController.columnLeftRW.keySet()) {
                myComponentController.columnLeftRW.put(key, 'r');
            }
        }
        if (myComponentController.columnRightRW != null) {
            for (String key : myComponentController.columnRightRW.keySet()) {
                myComponentController.columnRightRW.put(key, 'r');
            }
        }
        return selectedDataSql;
    }
 
    public override String getOriginObjName() {
        // オブジェクトAPI名
        originObjName = 'ReceivingNote__c';
        return originObjName;
    }
    public override String getOriginObjColumns() {
        // 項目セット
        // 20210312 you WLIG-BV8CHF start
        //originObjColumns = 'Id , RN_Status__c , Internal_asset_location__c , Sales_order_number__c , OrderCode__c , ApprovalNumber__c , Asset_loaner_category__c , DateOfDelivery__c , ReceivingNoteSummary_Cnt__c , Data_Confirmation_Again_Link__c, Data_Confirmation_Link__c, ReceivingNoteNo__c,ReceivingNotePDF_LINK__c';
        // 20210630 ljh 增加 ContactPerson__c
        originObjColumns = 'Id ,ContactPerson__c, RN_Status__c , Internal_asset_location__c , Sales_order_number__c , OrderCode__c , ApprovalNumber__c , Asset_loaner_category__c , DateOfDelivery__c , ReceivingNoteSummary_Cnt__c , Data_Confirmation_Again_Link__c, Data_Confirmation_Link__c, ReceivingNoteNo_New__c,ReceivingNotePDF_LINK__c';
        // 20210312 you WLIG-BV8CHF end
        return originObjColumns;
    }
 
    public override String getObjName() {
        // オブジェクトAPI名
        objName = 'ReceivingNote__c';
        return objName;
    }
    public override String getColumnLeftFieldSetName() {
        // 左の項目セット
        columnLeftFieldSetName = 'ReceivingNoteListFirst_L';
        return columnLeftFieldSetName;
    }
    public override String getColumnRightFieldSetName() {
        // 右の項目セット
        columnRightFieldSetName = 'ReceivingNoteListFirst_R';
        return columnRightFieldSetName;
    }
 
    public override List<String> getColumnFieldList() {
        // strColumus 里加 field
        // FixtureUtil#raesdGroupByAssetId()の項目も必要
        return new List<String>{'Id'
        };
    }
 
    public override String getFKColumnField() {
        // getObjName 连 getOriginObjName 的 FK
        return '';
    }
 
    public override String getRecordTypeId() {
        //ページレイアウトを収得するのレコードタイプ
        recordTypeId = '';
        return recordTypeId;
    }
 
    // ページコントローラに検索処理は、WhereSoql作成のみ、パラメータとして、コンポーネントに渡される
    public override String getSqlWhereStr() {
        sqlWhereStr = '';
 
        if (getIsNeedRunSearch()) {
            sqlWhereStr = this.makeSoql();
        }
 
        return sqlWhereStr;
 
    }
 
    public override String getOrderbyStr() {
        return 'order by Id';
    }
 
    public override Boolean getIsNeedRunSearch() {
        return true;
    }
 
    public ReceivingNote__c rn { get; set; }     //别存放地
 
    public String bieCunFangDi { get; set; }     //别存放地
    public String orderCode { get; set; }         //订单编码
    public String salesOrderNumber {get;set;}      // 合同编码
    public String approvalNumber { get; set; }         //采购申请裁决号
    //wangweipeng 20210616   新增 联系人 查询条件
    public String contactPerson { get; set; }         //联系人
    public String asset_loaner_category { get; set; }         //备品类别
    public String dateOfDelivery { get; set; }         //发货日
    public String status { get; set; }         //收货状态
    public String beiShangGuang {get; set;} // 北上广备品中心
 
    public ReceivingNoteListFirstController() {
        parentId = '00000';
        status = '待数据确认';
        String userId = Userinfo.getUserId();
        User  u = [SELECT UserRole.name  from User where Id =: userId];
        switch on u.UserRole.name  {
            when '备品中心北方管理成员' {
                beiShangGuang = '北京 备品中心';
            }
            when '备品中心华东管理成员' {
                beiShangGuang = '上海 备品中心';
            }
            when '备品中心南方管理成员' {
                beiShangGuang = '广州 备品中心';
            }
            when else{
                beiShangGuang = 'ALL';
            }
        }
    }
 
    public void init() {
        rn = new ReceivingNote__c();
        isNeedSearchFirst = true;
        isNeedCheckEvent = true;
        getSqlWhereStr();
    }
 
    public PageReference searchOpp() {
 
        if (!getIsNeedRunSearch()) {
            return null;
        }
 
        // 選択済みの製品を取得
        myComponentController.getSelectedDataInfo();
 
        getSqlWhereStr();
        // コンポーネントにSoqlを発行して、ページングする
        myComponentController.searchAndPaging();
        return null;
    }
 
    public override void setViewList(List<sObject> queryList) {
        viewList = new List<WrapperInfo>();
        for (Integer i = 0; i < queryList.size(); i++) {
            // 501を超えた場合前500のみを出す
            if (i == getSearchNumMax()) { break; }
            ReceivingNote__c mf = (ReceivingNote__c)queryList[i];
            viewList.add(new WrapperInfo(mf, myComponentController));
            viewList[viewList.size() - 1].lineNo = viewList.size() - 1;
        }
    }
 
    //别存放地 SelectOption
    public List<SelectOption> getbieCunFangDiOps() {
        //备品存放地(现在)
        // return FixtureUtil.bieCunFangDiOpsMap.get(sonObj.Internal_asset_location__c);
        // return new List<SelectOption>{
        //     new SelectOption(sonObj.Rental_Apply__r.Internal_asset_location_F__c, sonObj.Rental_Apply__r.Internal_asset_location_F__c)
        // };
        // cunfangdiSet = new Set<String>();
        List<SelectOption> opList;
        opList = FixtureUtil.bieCunFangDiOpsMap.get('备品管理中心');
 
        // for (SelectOption op : opList) {
        //     if (String.isNotBlank(op.getValue())) {
        //         cunfangdiSet.add(op.getValue());
        //     }
        // }
        return opList;
    }
 
    //别本部 SelectOption
    public List<SelectOption> getbieBenBuOps() {
        //所在地区(本部) 现在
        // return FixtureUtil.bieBenBuOpsMap.get(sonObj.Salesdepartment__c);
        // benbuSet = new Set<String>();
        List<SelectOption> opList = FixtureUtil.bieBenBuOpsMap.get('All');
        // for (SelectOption op : opList) {
        //     if (String.isNotBlank(op.getValue())) {
        //         benbuSet.add(op.getValue());
        //     }
        // }
        return opList;
    }
 
    //产品分类 SelectOption
    public List<SelectOption> getbieChanPinFenLeiOps() {
        //产品分类(GI/SP) F Product_category_F__c
        // return FixtureUtil.bieChanPinFenLeiOpsMap.get(sonObj.Product_category_F__c);
        return FixtureUtil.bieChanPinFenLeiOpsMap.get('GISP');
    }
 
    //别备品分类 SelectOption
    public List<SelectOption> getbieBeiPinFenLeiOps() {
        //备品分类(现在) Equipment_Type__c
        // return FixtureUtil.bieBeiPinFenLeiOpsMap.get(sonObj.Equipment_Type__c);
        return FixtureUtil.bieBeiPinFenLeiOpsMap.get('备品分类');
    }
 
    // 备品类别
    public List<SelectOption> getAsset_loaner_categoryOps() {
        return FixtureUtil.getPlickList('ReceivingNote__c', 'Asset_loaner_category__c');
    }
 
    public List<SelectOption> getRNStatusOpss() {
        return ReceivingNoteListFirstController.getRNStatusOps();
    }
 
    // 备品类别
    public static List<SelectOption> getRNStatusOps() {
        List<FixtureUtil.RNStatus> rnsList = new List<FixtureUtil.RNStatus>();
        rnsList.add(FixtureUtil.RNStatus.Dai_Shu_Ju_Que_Ren);
        rnsList.add(FixtureUtil.RNStatus.Yi_Shu_Ju_Que_Ren);
        rnsList.add(FixtureUtil.RNStatus.Yi_Shou_Huo_Que_Ren);
        rnsList.add(FixtureUtil.RNStatus.Yi_Qing_Dian);
        rnsList.add(FixtureUtil.RNStatus.Yi_Yan_Shou);
        //rnsList.add(FixtureUtil.RNStatus.Yi_Jian_Ce);
        rnsList.add(FixtureUtil.RNStatus.Dai_Ru_Ku_Zhi_Shi);
        rnsList.add(FixtureUtil.RNStatus.Yi_Ru_Ku_Zhi_Shi);
        rnsList.add(FixtureUtil.RNStatus.Yi_Ru_Ku);
 
        List<SelectOption> opList = new List<SelectOption>();
        opList.add(new SelectOption('', '--全部--'));
        for (FixtureUtil.RNStatus sNum : rnsList) {
            String value = FixtureUtil.RNStatusMap.get(sNum.ordinal());
            opList.add(new SelectOption(value, value));
        }
 
        return opList;
    }
 
    private String makeSoql() {
        String soql = ' Where Id != null';
        if ('ALL' == beiShangGuang) {
            if(String.isNotBlank(bieCunFangDi) && bieCunFangDi != 'All') {
                soql += ' AND Internal_asset_location__c = \'' + String.escapeSingleQuotes(bieCunFangDi) + '\'';
            }else{
                //wangweipeng  20210615
                soql += ' AND (Internal_asset_location__c =\'北京 备品中心\' or Internal_asset_location__c = \'上海 备品中心\' or Internal_asset_location__c = \'广州 备品中心\')';
            }
        }
        else{
            // 只有查看自己备品中心
            soql += ' AND Internal_asset_location__c = \'' + beiShangGuang + '\'';
        }
        if (String.isNotBlank(orderCode)) {
            soql += ' AND OrderCode__c LIKE \'%' + String.escapeSingleQuotes(orderCode) + '%\'';
        }
 
        if (String.isNotBlank(salesOrderNumber)) {
            soql += ' AND Sales_order_number__c LIKE \'%' + String.escapeSingleQuotes(salesOrderNumber) + '%\'';
        }
 
        if (String.isNotBlank(approvalNumber)) {
            soql += ' AND ApprovalNumber__c LIKE \'%' + String.escapeSingleQuotes(approvalNumber) + '%\'';
        }
 
        //wangweipeng 20210616   新增 联系人 查询条件
        if(String.isNotBlank(contactPerson)){
            soql += ' AND ContactPerson__c LIKE \'%' + String.escapeSingleQuotes(contactPerson) + '%\'';
        }
 
        if (String.isNotBlank(asset_loaner_category)) {
            soql += ' AND Asset_loaner_category__c = \'' + String.escapeSingleQuotes(asset_loaner_category) + '\'';
        }
 
        if (String.isNotBlank(dateOfDelivery)) {
            String dateStr = dateOfDelivery.replace('/', '-');
            soql += ' AND DateOfDelivery__c = ' + String.escapeSingleQuotes(dateStr);
        }
 
        if (String.isNotBlank(status)) {
            soql += ' AND RN_Status__c = \'' + String.escapeSingleQuotes(status) + '\'';
        }
        System.debug('zheli:'+soql);
        return soql;
    }
    /**
    @description 以清单为单位给明细做入库指示
    */
    public PageReference inStockRequest() {
        try{
            // 获取打勾清单
            Map<Id,Integer> rnRndMap = new Map<Id, Integer>();
            for (WrapperInfo info:viewList){
                if(info.check){
                    rnRndMap.put(info.sobj.Id, 0);
                }
            }
 
            // 获取明细
            List<ReceivingNoteDetail__c> rndList = [
                    SELECT DataConfirmationAgain__c
                         , Arrival_wh_Request_time__c
                         , ReceivingNote__r.ReceivingNoteNo__c
                         , ReceivingNote__r.Name
                      FROM ReceivingNoteDetail__c
                     WHERE Delete_Flag__c = false 
                       AND ReceivingNote__c IN :rnRndMap.keySet()
                       FOR UPDATE
            ];
            rndList = [
                    SELECT DataConfirmationAgain__c
                         , Arrival_wh_Request_time__c
                         , ReceivingNote__r.ReceivingNoteNo__c
                         , ReceivingNote__r.Name
                      FROM ReceivingNoteDetail__c
                     WHERE Delete_Flag__c = false
                       AND ReceivingNote__c IN :rnRndMap.keySet()
                     ORDER BY ReceivingNote__c
            ];
 
            List<ReceivingNoteDetail__c> stockInRequestAvailableRndList = new List<ReceivingNoteDetail__c>(); // 可做入库指示的明细列表
 
            Id lastRnId = null;
            Boolean hasStockInAvailable = false;
            Boolean hasStockInUnavailable = false;
            ReceivingNoteDetail__c lastRnd;
 
            Datetime now = Datetime.now();
            for(ReceivingNoteDetail__c rnd:rndList){
                if (String.isBlank(lastRnId)){
                    lastRnId = rnd.ReceivingNote__c;
                }
                else if (lastRnId != rnd.ReceivingNote__c){
                    if(hasStockInAvailable == false){
                        Apexpages.addMessage(new ApexPages.message(Apexpages.Severity.ERROR,  lastRnd.ReceivingNote__r.Name + '无法入库指示'));
                        return null;
                    }
                    else if (hasStockInUnavailable){
                        Apexpages.addMessage(new ApexPages.message(Apexpages.Severity.INFO, lastRnd.ReceivingNote__r.Name + '部分入库指示'));
                    }
                    lastRnId = rnd.ReceivingNote__c;
                    hasStockInAvailable = false;
                    hasStockInUnavailable = false;
                }
                if(rnd.DataConfirmationAgain__c && rnd.Arrival_wh_Request_time__c == null){
                    rnd.Arrival_wh_Request_time__c = now;
                    hasStockInAvailable = true;
                    stockInRequestAvailableRndList.add(rnd);
                }
                else{
                    hasStockInUnavailable = true;
                }
                lastRnd = rnd;
            }
 
            if (String.isNotBlank(lastRnId)) {
                if(hasStockInAvailable == false){
                    Apexpages.addMessage(new ApexPages.message(Apexpages.Severity.ERROR,  lastRnd.ReceivingNote__r.Name + '无法入库指示'));
                    return null;
                }
                else if (hasStockInUnavailable){
                    Apexpages.addMessage(new ApexPages.message(Apexpages.Severity.INFO, lastRnd.ReceivingNote__r.Name + '部分入库指示'));
                }
            }
            // 写入数据库
            if (stockInRequestAvailableRndList.size()>0){
                update stockInRequestAvailableRndList;
                PageReference pageRef = ApexPages.CurrentPage();
                return pageRef;
            }
        }
        catch (Exception e) {
            ApexPages.addMessages(e);
            return null;
        }
        return null;
    }
}