liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
//仿 EquipmentSetShippmentReceived6Controller.cls
public without sharing class ReceivingNoteAcceptanceController {
 
    public ReceivingNoteSummary__c rns { get; set; }
    public List<EsdInfo> esdList { get; set; }
    public Boolean saveBtnDisabled { get; private set; }
    public boolean ReturnRefuse {get;private set;}
    public String ErrorMessage {get; set;}
    //public String CDSFinished {get;set;}
    //public boolean LostFlg {get;set;}
    public boolean done_flg {get;set;}
    public String SR_status { get; private set; }
    public String Step_status { get; private set; }
    //public Boolean needCDS { get; private set; }
    //public String open_type { get; private set; }
    public Boolean forceSave { get; set; }
    public String rnId {get;set;}
    //public String AccessoryId {get;set;}
    //public String AccessorySetId {get;set;}
    //public String UnCheckedId {get;set;}
    private String rnsIdsStr;
    
    public ReceivingNoteAcceptanceController() {
        // Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=8');
        //传入一览的Id或No
        rnsIdsStr = ApexPages.currentPage().getParameters().get('id');
        if (SR_status == null) {
            SR_status = ApexPages.currentPage().getParameters().get('type');
        }
        if (Step_status == null) {
            Step_status = ApexPages.currentPage().getParameters().get('step');
        }
    }
    
    // 画面初始化
    public PageReference init() {
        forceSave = false;
        done_flg = false;
        rns = new ReceivingNoteSummary__c();
        esdList = new List<EsdInfo>();
        Set<Id> rnsIdSet = new Set<Id>();
        saveBtnDisabled = false;
        ReturnRefuse = true;
        ErrorMessage = '';
        List<String> rnsIdList = rnsIdsStr.split(',');
 
        if (rnsIdsStr != null) {
            // 当前User
            String userid = Userinfo.getUserId();
            User user = [select Id,Name from User where Id = :userid];
            // 收货清单一览
            List<ReceivingNoteSummary__c> rnsList = [select Fixture_Model_No__c, Id,ReceivingNote__c
                                                     from ReceivingNoteSummary__c
                                                     where (ReceivingNoteSummaryNo__c in :rnsIdList or Id in :rnsIdList)
                                                    ];
            if (rnsList.size() > 0) {
                for (ReceivingNoteSummary__c rns : rnsList) {
                    rnsIdSet.add(rns.Id);
                    rnId = rns.ReceivingNote__c;
                }
            } else {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '备品不存在'));
                saveBtnDisabled = true;
                return null;
            }
 
        }
 
        // 收货明细
        List<ReceivingNoteDetail__c> rndList = [
                SELECT ReceivingNoteSummary__r.ReceivingNoteSummaryNo__c
                     , Name
                     , ReceivingNoteSummary__c
                     , ReceivingNoteDetailNo__c
                     , AcceptanceResult__c
                     , AcceptanceComments__c
                     , Fixture_QRCode_F__c
                     , Barcode_F__c
                     , Change_Point__c
                     , SerialNumber_F__c
                     , SerialNumber_Origin__c
                     , unknow_serial_NO_product__c
                     , SerialNumber__c
                     , DataConfirmation__c
                     , Inspection_result_after__c
                     , ReceivingNoteSummary__r.Package_Status__c
                     , Product_Name_F__c
                     , After_Inspection_time__c
                     , Packing_list_Fixture_F__c
                     , Fixture_Set_Detail__r.Fixture_Set__c
                     , Fixture_Arrival_Process__c
                     , Fixture_Arrival_Product__r.Name
                     , Fixture_Arrival_Product__r.Fixture_Model_No_T__c
                     , Fixture_Model_No_F__c
                     , PeriodOfUse_Final__c
                     , PeriodOfUse__c
                     , PeriodOfUse_Old_F__c
                     , Asset_loaner_category_F__c
                     // 【P1】UDI编码加入到新品收货功能中与BARCODE的比对 DB202306207904 20230711 by lc Start
                     , RNDAssert__r.Barcode__c
                     , Barcode_Check_Result__c
                     // 【P1】UDI编码加入到新品收货功能中与BARCODE的比对 DB202306207904 20230711 by lc End
                  FROM ReceivingNoteDetail__c
                 WHERE ReceivingNoteSummary__c IN :rnsIdSet AND Delete_Flag__c = false
              ORDER BY ReceivingNoteSummary__r.Name, ReceivingNoteSummary__c asc, Is_Body_F__c desc, passNo2__c, passNo3__c
        ];
        for(ReceivingNoteDetail__c rnd:rndList){
            esdList.add(new EsdInfo(rnd));
        }
 
        if (esdList.size() <= 0) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '没有收货一览明细'));
            saveBtnDisabled = true;
            return null;
        }
        Step_status = '验收';
        SR_status = 'Acceptance';
 
        return null;
    }
    /**
    @param originRnd 数据库中明细
    @param pageRnd 页面上的明细
    @return 值一样时返回true,不一样时返回false
    @description 判断页面上的明细和数据库中的明细是否一致
    */
    @TestVisible private Boolean detailEquals(ReceivingNoteDetail__c originRnd, ReceivingNoteDetail__c pageRnd){
        Boolean isEqual = true;
        //验收结果
        if(!stringEquals(originRnd.AcceptanceResult__c, pageRnd.AcceptanceResult__c)){
            return false;
        }
        //验收备注
        else if (!stringEquals(originRnd.AcceptanceComments__c, pageRnd.AcceptanceComments__c)){
            return false;
        }
        // QRcode
        else if (!stringEquals(originRnd.Fixture_QRCode_F__c, pageRnd.Fixture_QRCode_F__c)){
            return false;
        }
        // Barcode
        else if (!stringEquals(originRnd.Barcode_F__c, pageRnd.Barcode_F__c)){
            return false;
        }
        // 无编号设备
        else if (!stringEquals(originRnd.unknow_serial_NO_product__c, pageRnd.unknow_serial_NO_product__c)){
            return false;
        }
        // 机身编号
        else if (!stringEquals(originRnd.SerialNumber__c, pageRnd.SerialNumber__c)){
            return false;
        }
        // 使用日期
        else if (!stringEquals(String.valueOf(originRnd.PeriodOfUse_Final__c), String.valueOf(pageRnd.PeriodOfUse_Final__c))){
            return false;
        }
        // 【P1】UDI编码加入到新品收货功能中与BARCODE的比对 DB202306207904 20230711 by lc Start
        // barcode编码对比
        else if (!stringEquals(originRnd.Barcode_Check_Result__c, pageRnd.Barcode_Check_Result__c)){
            return false;
        }
        // 【P1】UDI编码加入到新品收货功能中与BARCODE的比对 DB202306207904 20230711 by lc End
        return true;
 
    }
    /**
    @param s1 字符串1
    @param s2 字符串2
    @return 一样则返回true, null和'' 也为true
    @description
    */
    @TestVisible private Boolean stringEquals(String s1, String s2){
        if (String.isBlank(s1) && String.isBlank(s2)){
            return true;
        }
        else if(s1!=null){
            return s1.equals(s2);
        }
        else{
            return s2.equals(s1);
        }
    }
 
    /**
    @description 保存按键
    */
    public PageReference save() {
        // Id-->面面明细
        Map<Id,ReceivingNoteDetail__c> pageRndMap = new Map<Id,ReceivingNoteDetail__c>();
        for(EsdInfo esd : esdList){
            pageRndMap.put(esd.rnd.Id,esd.rnd);
        }
 
        // 数据库读出明细
        List<ReceivingNoteDetail__c> originRndList = [
                SELECT Id
                     , AcceptanceStaff__c
                     , AcceptanceTime__c
                     , AcceptanceResult__c
                     , AcceptanceComments__c
                     , Fixture_QRCode_F__c
                     , Barcode_F__c
                     , unknow_serial_NO_product__c
                     , SerialNumber__c
                     , Name
                     , ReceivingNoteSummary__r.Package_Status__c
                     , DataConfirmation__c
                     , After_Inspection_time__c
                     , Change_Point__c
                     , PeriodOfUse_Final__c
                     , Barcode_Check_Result__c  // 【P1】UDI编码加入到新品收货功能中与BARCODE的比对 DB202306207904
                  FROM ReceivingNoteDetail__c
                 WHERE Id IN :pageRndMap.keySet()
                   FOR UPDATE
                ];
 
        // 存放需要被更新的明细
        List<ReceivingNoteDetail__c> updateRndList = new List<ReceivingNoteDetail__c>();
 
        for(ReceivingNoteDetail__c originRnd : originRndList){
 
            ReceivingNoteDetail__c pageRnd = pageRndMap.get(originRnd.Id);
            // 页面上填写值与数据库不一致时
            if (!detailEquals(originRnd, pageRnd)){
                if(originRnd.Change_Point__c != '删除') {
                    if (originRnd.ReceivingNoteSummary__r.Package_Status__c != 'OK'){
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '【' + originRnd.Name + '】未收货确认,无法保存,请刷新画面重试。'));
                    }
                    if (originRnd.DataConfirmation__c == false){
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '【' + originRnd.Name + '】未数据确认,无法保存,请刷新画面重试。'));
                    }
                    if (originRnd.After_Inspection_time__c != null ){
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '【' + originRnd.Name + '】已验收完成,无法保存,请刷新画面重试。'));
                    }
                    if (ApexPages.hasMessages()) {
                        done_flg = false;
                        return null;
                    }
                }
                // 检查页面值的有效性
                if (pageRnd.unknow_serial_NO_product__c == 'FALSE') {
                    if(String.isBlank(pageRnd.SerialNumber__c) && String.isBlank(pageRnd.SerialNumber_F__c)){
                        //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'origin'+originRnd));
                        //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, 'page'+pageRnd));
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, pageRnd.Name+':请填写【机身编码】或勾选【无机身号码设备】'));
                        done_flg = false;
                        return null;
                    }
                } else {
                    pageRnd.SerialNumber__c = null;
                }
 
                // 【P1】UDI编码加入到新品收货功能中与BARCODE的比对 DB202306207904 20230711 by lc Start
                if (!String.isBlank(pageRnd.RNDAssert__r.Barcode__c)) {
                    if (pageRnd.Barcode_Check_Result__c != 'OK' && pageRnd.Barcode_Check_Result__c != 'NG') {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, pageRnd.Name+':barcode编码对比未完成,无法保存,请确认。'));
                        done_flg = false;
                        return null;
                    }
                }
                // 【P1】UDI编码加入到新品收货功能中与BARCODE的比对 DB202306207904 20230711 by lc End
 
                if (String.isBlank(pageRnd.AcceptanceResult__c)) {
                    pageRnd.AcceptanceStaff__c = null;
                    pageRnd.AcceptanceTime__c = null;
                } else {
                    pageRnd.AcceptanceStaff__c = Userinfo.getUserId();
                    pageRnd.AcceptanceTime__c = System.Now();
                }
                updateRndList.add(pageRnd);
            }
        }
 
        if (updateRndList.size()>0){
            Savepoint sp = Database.setSavepoint();
            try {
                FixtureUtil.withoutUpsertObjects(updateRndList);
                done_flg = true;
                return null;
            } catch (Exception ex) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, ex.getMessage()));
                Database.rollback(sp);
                done_flg = false;
                return null;
            }
        }
        return null;
 
    }
 
    public class EsdInfo {
        public ReceivingNoteDetail__c rnd { get; set; }
        public boolean isChecked { get; set; }
        public Boolean canChange {get;set;}
 
        public EsdInfo(ReceivingNoteDetail__c rnd) {
            this.rnd=rnd;
            this.isChecked=false;
            this.canChange = false;
            // 数据确认完成,包装OK,未验收,未删除才可编辑
            if(this.rnd.ReceivingNoteSummary__r.Package_Status__c == 'OK'
                && this.rnd.DataConfirmation__c == true
                && this.rnd.After_Inspection_time__c == null
                && this.rnd.Change_Point__c != '删除' ) {
                this.canChange = true;
            }
        }
    }
}