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
//仿EquipmentSetSRList2Controller.cls
/*
    TestClass
    RentalFixtureManage1Test
    RentalFixtureManage3Test
    RentalFixtureManage9Test
    RentalFixtureManage10Test
 */
public with sharing class ReceivingNoteListController {
    public List<ReceivingNoteSummary__c> rnsList { get; set; }
    public List<SummaryInfo> summaryInfoList{get;set;}
    public String sr_open { get; set; }
    public Boolean doneFlg {get; set; }
    public String receiving_note_no { get; set; }
    public String sr_id { get; set; }
    public String sr_type { get; set; }
    public List<SelectOption> typeList {get; set;}
    public String selectedType {get; set;}
    public String rndIdsStr {get; set;}
    public String rnId {get; set;}
    public String userId;
    public ReceivingNote__c relatedNote { get; set; }
    public String cdlStr {get; private set;} // 20231013 ljh add
    
 
    public Integer getRnsListSize() {
        return rnsList.size();
    }
 
    public ReceivingNoteListController() {
        receiving_note_no = ApexPages.currentPage().getParameters().get('id');
        sr_type = ApexPages.currentPage().getParameters().get('type');
        sr_open = ApexPages.currentPage().getParameters().get('open');
        summaryInfoList = new List<SummaryInfo>();
        userId = Userinfo.getUserId();
    }
 
    public void init() {
        rnsList = new List<ReceivingNoteSummary__c>();
        doneFlg = false;
        
        initTypeList();
        Set<Id> summaryIdSet = new Set<Id>();
        Map<Id, Integer> summaryFileCnt = new Map<Id, Integer>();
 
        if (receiving_note_no != null) {
            rnsList = [
                    SELECT Id
                         , ReceivingNoteSummaryNo__c
                         , Fixture_Model_No_F__c
                         , First_RND__r.SerialNumber_F__c
                         , First_RND__r.Fixture_QRCode_F__c
                         , RNS_Status__c
                         , ReceivingNote__c
                         , Wei_DataConfirmation_Cnt__c
                         , Wei_Acceptance_Cnt__c
                         , ReceivingNoteDetail_Cnt__c
                         , ReceivingNote__r.Asset_loaner_category__c
                         , ReceivingNote__r.Status__c
                         , Package_Status__c
                         , Package_Status_Memo__c
                      FROM ReceivingNoteSummary__c
                     //WLIG-BV8CHF 20210331 you  start
                     //WHERE ReceivingNote__r.ReceivingNoteNo__c = :receiving_note_no
                     WHERE ReceivingNote__r.ReceivingNoteNo_New__c = :receiving_note_no
                     //WLIG-BV8CHF 20210331 you  end
                  ORDER BY ReceivingNoteSummaryNo__c
                    ];
            System.debug('receiving_note_no = ' + receiving_note_no + ': ' + rnsList.size());
 
            for(ReceivingNoteSummary__c rns:rnsList){
                SummaryInfo summaryInfo = new SummaryInfo(rns);
                summaryInfoList.add(summaryInfo);
                rnId = rns.ReceivingNote__c;
                summaryIdSet.add(rns.Id);
            }
 
 
            if(summaryInfoList.size() == 0){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '收货清单没有收货清单明细'));
            } else {
                // 20231013 ljh add start
                cdlStr = '';
                for (ContentDocumentLink link : [SELECT ContentDocumentId,ContentDocument.LatestPublishedVersionId FROM ContentDocumentLink WHERE LinkedEntityId = :rnId]) {
                    String idS = link.ContentDocument.LatestPublishedVersionId;
                    cdlStr += idS.substring(0,15)+'_'+link.ContentDocumentId+';';
                }
                // 20231013 ljh add end  
                        relatedNote = [select Id, Name from ReceivingNote__c where id = :rnId];
                AggregateResult[] fileCnt = [select count(id) cnt, LinkedEntityId
                                                from ContentDocumentLink
                                                where LinkedEntityId in :summaryIdSet
                                                group by LinkedEntityId];
                for (AggregateResult cnt : fileCnt) {
                    summaryFileCnt.put((Id)cnt.get('LinkedEntityId'), Integer.valueOf(cnt.get('cnt')));
                }
 
                for (SummaryInfo si : summaryInfoList) {
                    if (summaryFileCnt.containsKey(si.rns.Id)) {
                        si.fileCnt = summaryFileCnt.get(si.rns.Id);
                    }
                }
               
 
            }
        }
        return;
    }
 
    private void initTypeList(){
        typeList = new List<SelectOption>();
        typeList.add(new SelectOption('无','无'));
        typeList.add(new SelectOption('收货确认','收货确认'));
        typeList.add(new SelectOption('取消收货确认','取消收货确认'));
        typeList.add(new SelectOption('清点','清点'));
        typeList.add(new SelectOption('验收','验收'));
        typeList.add(new SelectOption('入库', '入库'));
 
        System_UserSetting__c config = System_UserSetting__c.getInstance(userId);
        selectedType = config.ReceivingNote_Action_Type__c;
        if (String.isBlank(selectedType)){
            selectedType = '无';
        }
    }
    public void setType(){
 
        if (selectedType != '无' && selectedType != null ){
            System_UserSetting__c config = System_UserSetting__c.getInstance(userId);
            config.ReceivingNote_Action_Type__c = selectedType;
            FixtureUtil.withoutUpsertObjects(new System_UserSetting__c[]{config});
        }
 
    }
    /**
    @description 变更收货确认状态
    */
    public void receivedConfirm(){
        List<ReceivingNoteSummary__c>  updateRNSList = new List<ReceivingNoteSummary__c> ();
        List<Id> rnsIdList = new List<Id>();
        List<Id> delRnsList = new List<Id>();
        Map<Id, ReceivingNoteSummary__c> updateMap = new Map<Id, ReceivingNoteSummary__c>([
                        SELECT Id
                             , Wei_DataConfirmation_Cnt__c
                             , Wei_Acceptance_Cnt__c
                             , ReceivingNoteDetail_Cnt__c
                          FROM ReceivingNoteSummary__c
                          //WLIG-BV8CHF 20210331 you  start
                         //WHERE ReceivingNote__r.ReceivingNoteNo__c = :receiving_note_no
                         WHERE ReceivingNote__r.ReceivingNoteNo_New__c = :receiving_note_no
                         //WLIG-BV8CHF 20210331 you  end
                        ]);
 
        if (selectedType == '收货确认') {
            for (SummaryInfo sinfo : summaryInfoList){
                if(sinfo.isChecked) {
                    if(updateMap.containsKey(sinfo.rns.Id)) {
                        ReceivingNoteSummary__c originRns = updateMap.get(sinfo.rns.Id);
                        if (sinfo.rns.Wei_DataConfirmation_Cnt__c > 0 || originRns.Wei_DataConfirmation_Cnt__c > 0) {
                            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '【'+sinfo.rns.ReceivingNoteSummaryNo__c+'】有未做数据确认的明细存在,不能确认收货,请刷新画面重试。'));
                        }
                        if (originRns.Wei_Acceptance_Cnt__c < originRns.ReceivingNoteDetail_Cnt__c) {
                            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '【'+sinfo.rns.ReceivingNoteSummaryNo__c+'】清点已开始,不可以修改收货确认状态,请刷新画面重试。'));
                        }
                    }
                    else {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '【'+sinfo.rns.ReceivingNoteSummaryNo__c+'】不存在,请刷新画面重试。'));
                    }
                    if (sinfo.rns.Package_Status__c == null) {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '【'+sinfo.rns.ReceivingNoteSummaryNo__c+'】未填写外包装是否完整,不能确认收货。'));
                    }
                    // 对被选中的一览 的 确认时间 和 确认人 赋值
                    sinfo.rns.Received_Confirm_Time__c = System.now();
                    sinfo.rns.Received_Confirm_Staff__c = userId;
                    updateRNSList.add(sinfo.rns);
                    rnsIdList.add(sinfo.rns.Id);
                }
            }
            if (ApexPages.hasMessages()) return;
        }
        else if (selectedType == '取消收货确认'){
            for (SummaryInfo sinfo : summaryInfoList){
                if(sinfo.isChecked){
                    if(updateMap.containsKey(sinfo.rns.Id)) {
                        ReceivingNoteSummary__c originRns = updateMap.get(sinfo.rns.Id);
                        if (originRns.Wei_Acceptance_Cnt__c < originRns.ReceivingNoteDetail_Cnt__c) {
                            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '【'+sinfo.rns.ReceivingNoteSummaryNo__c+'】清点已开始,不可以修改收货确认状态,请刷新画面重试。'));
                            return;
                        }
                    }
                    else {
                        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '【'+sinfo.rns.ReceivingNoteSummaryNo__c+'】不存在,请刷新画面重试。'));
                        return;
                    }
                    // 将被选中的一览的确认时间和确认人清空
                    sinfo.rns.Received_Confirm_Time__c = null;
                    sinfo.rns.Received_Confirm_Staff__c = null;
                    sinfo.rns.Package_Status__c = null;
                    sinfo.rns.Package_Status_Memo__c = null;
                    updateRNSList.add(sinfo.rns);
                    rnsIdList.add(sinfo.rns.Id);
                    delRnsList.add(sinfo.rns.Id);
                }
            }
        }
 
        if (updateRNSList.size() > 0){
            Savepoint sp = Database.setSavepoint();
            List<ReceivingNoteSummary__c> rnsList = [select ReceivingNoteSummaryNo__c,
                                                            Id,
                                                            Fixture_Model_No__c,
                                                            First_RND__r.SerialNumber_F__c,
                                                            First_RND__r.Fixture_QRCode_F__c,
                                                            RNS_Status__c,
                                                            ReceivingNote__r.Status__c,
                                                            Received_Confirm_Time__c,
                                                            Received_Confirm_Staff__c
 
                                                       from ReceivingNoteSummary__c
                                                      where Id in :rnsIdList
                                                      for update
                                                    ];
            try {
                FixtureUtil.withoutUpsertObjects(updateRNSList);
                if (delRnsList.size() > 0) {
                    List<ContentDocumentLink> delList =  [select id from ContentDocumentLink where LinkedEntityId in :delRnsList];
                    FixtureUtil.withoutDelete(delList);
                }
            } catch (Exception ex) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, ex.getMessage()));
                Database.rollback(sp);
                return;
            }
        }
        doneFlg = true;
        return;
 
    }
 
    public void checkckb() {
        if (selectedType == '收货确认' || selectedType == '取消收货确认') {
            for (SummaryInfo sinfo : summaryInfoList){
                if (sinfo.canChange == false) sinfo.isChecked = false;
            }
        } else {
            for (SummaryInfo sinfo : summaryInfoList){
                sinfo.isChecked = sinfo.isChecked;
            }
        }
    }
 
    public class SummaryInfo{
        public ReceivingNoteSummary__c rns {get;set;}
        public boolean isChecked{get;set;}
        public Integer fileCnt{get;set;}
        public Boolean canChange {get;set;}
        public Boolean disableFalg {get; set; } // 20231019 ymh add sitbug 
        public SummaryInfo(ReceivingNoteSummary__c rns){
            this.isChecked = false;
            this.rns = rns;
            this.fileCnt = 0;
            this.canChange = false;
            disableFalg = true;// 20231019 ymh add sitbug 
            // 全部明细数据确认后,且还没开始清点才可以编辑
            if (rns.Wei_DataConfirmation_Cnt__c == 0 && rns.Wei_Acceptance_Cnt__c == rns.ReceivingNoteDetail_Cnt__c) {
                //this.isChecked = true;
                this.canChange = true;
            }
            // 20231019 ymh add sitbug start
            if(rns.RNS_Status__c == '已入库'){
                disableFalg = false;
            }
            // 20231019 ymh add sitbug start
        }
 
    }
}