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
//TestClass: RentalFixtureManage5Test, RentalFixtureManage2Test
public with sharing class EquipmentRentalCancelController {
    // 备品申请书
    public Rental_Apply__c rentalApply { get; set; }
    // 明细行项目
    public List<LineInfo> lineInfoList { get; set; }
    //备品申请书ID
    public String rentalApplyId { get; private set; }
    public Boolean hasError { get; private set; }
    public Boolean saveBtn { get; private set; }
    //现有备品数量(基于备品一览数量)
    public Integer EquipmentSetCnt{get;set;}
    public EquipmentRentalCancelController() {
        //Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=8');
        rentalApplyId = ApexPages.currentPage().getParameters().get('raid');
    }
 
    // 画面初始化
    public PageReference init() {
        saveBtn = false;
        hasError = false;
        rentalApply = new Rental_Apply__c();
        lineInfoList = new List<LineInfo>();
 
        if (rentalApplyId != null && rentalApplyId.length() > 0) {
            // 备品借出申请取得
            List<Rental_Apply__c> raList = [
                select Id, Status__c, RA_Status__c
                  from Rental_Apply__c
                 where Id = :rentalApplyId];
 
            if (raList.size() > 0) {
                rentalApply = raList[0];
            } else {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '备品申请书不存在,请确认。'));
                saveBtn = true;
                return null;
            }
        } else {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '备品申请书不存在,请确认。'));
            saveBtn = true;
            return null;
        }
 
        // 备品借出备品set一览取得 
        List<Rental_Apply_Equipment_Set__c> raesList = [
            select id, Name, Fixture_Set__c, Fixture_Set__r.Name, Fixture_Set__r.Loaner_code__c, RAES_Status__c, Rental_Start_Date__c, Final_reply_day__c, Yi_StockDown__c,
                   StockDown_time__c, Shippment_loaner_time2__c, Cancel_Select__c, Cancel_Reason__c, Cancel_Mem__c, Loaner_cancel_Remarks__c, Fixture_Set__r.Loaner_name__c,
                   First_RAESD__r.Asset__c,Loaner_cancel_reason__c //20210708 SFDC-C448KZ you start
              from Rental_Apply_Equipment_Set__c
             where Rental_Apply__c = :rentalApply.Id
               and Cancel_Select__c = false
               and Yi_loaner_arranged__c = 0];
 
        if (raesList.size() == 0) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '备品申请书未分配明细,不能取消分配。'));
            saveBtn = true;
            return null;
        }else{
            EquipmentSetCnt = raesList.size();
        }
 
        // 明细行做成
        for (Rental_Apply_Equipment_Set__c raes : raesList) {
            LineInfo lineInfo = new LineInfo(raes);
            lineInfoList.add(lineInfo);
        }
 
        return null;
    }
 
    // 保存按钮
    // https://sohobb.backlog.jp/view/OLY_OCM-152#comment-20041467
    // TODO OLY_OCM-206 select from 一対一Link, 把 分配数 清 0
    public PageReference saveBtn() {
        List<Rental_Apply_Equipment_Set__c> delList = new List<Rental_Apply_Equipment_Set__c>();
        List<Rental_Apply_Equipment_Set__c> updList = new List<Rental_Apply_Equipment_Set__c>();
        Set<Id> esIdSet = new Set<Id>();
 
        // 明细行check
        Integer cntSelect = 0;
        hasError = false;
        for (LineInfo line : lineInfoList) {
            if (line.isSelect == true) {
                // 入力規則を利用した
                //String reason = line.raes.Cancel_Reason__c;
                //if (line.status == 'canCancel' && (reason == null || reason.length() == 0)) {
                //    line.raes.Cancel_Reason__c.addError('必须输入取消理由。');
                //    hasError = true;
                //}
                //20210708 SFDC-C448KZ you start
                if (String.isBlank(line.raes.Cancel_Reason__c)) {
                      line.raes.Cancel_Reason__c.addError('请输入取消理由');
                      hasError = true;
                  }
                  if (String.isBlank(line.raes.Loaner_cancel_reason__c)) {
                      line.raes.Loaner_cancel_reason__c.addError('请输入一览取消理由');
                      hasError = true;
                  }
 
                  if (String.isNotBlank(line.raes.Loaner_cancel_reason__c) && line.raes.Loaner_cancel_reason__c=='其他' && String.isBlank(line.raes.Loaner_cancel_Remarks__c)) {
                     line.raes.Loaner_cancel_Remarks__c.addError('请输入取消理由备注');
                      hasError = true;
                  }
                //20210708 SFDC-C448KZ you end
                cntSelect += 1;
            }
        }
        if (cntSelect == 0) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '未选择取消分配的明细。'));
            hasError = true;
        }
        if (hasError == true) {
            system.debug('=====hasError');
            return null;
        }
 
        Set<Id> raesdIdSet = new Set<Id>();
 
        // 保存逻辑
        for (LineInfo line : lineInfoList) {
            //if (line.isSelect == true && line.status == 'canCancel') {
            //    Rental_Apply_Equipment_Set__c upd = new Rental_Apply_Equipment_Set__c(Id = line.raes.Id);
            //    // delList.add(del);
            //    // esIdSet.add(line.raes.Fixture_Set__c);
            //    upd.Cancel_Select__c = true;
            //    upd.Cancel_Date__c = Date.today();
            //    upd.Cancel_Reason__c = line.raes.Cancel_Reason__c;
            //    upd.Loaner_cancel_Remarks__c = line.raes.Loaner_cancel_Remarks__c;
            //    updList.add(upd);
            //}
            //if (line.isSelect == true && line.status == 'OnStock_Pre_Inspection') {
            //    Rental_Apply_Equipment_Set__c upd = new Rental_Apply_Equipment_Set__c(Id = line.raes.Id);
            //    //upd.OnStock_Pre_Inspection__c = true;
            //    upd.Cancel_Select__c = true;
            //    upd.Cancel_Date__c = Date.today();
            //    upd.Cancel_Reason__c = line.raes.Cancel_Reason__c;
            //    upd.Loaner_cancel_Remarks__c = line.raes.Loaner_cancel_Remarks__c;
            //    updList.add(upd);
            //    //esIdSet.add(line.raes.Fixture_Set__c);
            //}
            if (line.isSelect == true) {
                Rental_Apply_Equipment_Set__c upd = new Rental_Apply_Equipment_Set__c();
                // 20230706 ljh DB202306220097 start 
                // upd = line.raes;
                upd.Id = line.raes.Id;
                // 20230706 ljh DB202306220097 end 
                upd.Cancel_Select__c = true;
                upd.Cancel_Date__c = Date.today();
                upd.Cancel_Time__c = MainFixtureSelectController.getCurrentTime();
                upd.Cancel_Reason__c = line.raes.Cancel_Reason__c;
                //20210708 SFDC-C448KZ you start
                upd.Loaner_cancel_reason__c = line.raes.Loaner_cancel_reason__c;
                upd.Loaner_cancel_Remarks__c = line.raes.Loaner_cancel_Remarks__c==null ? line.raes.Loaner_cancel_reason__c : line.raes.Loaner_cancel_Remarks__c;
                //20210708 SFDC-C448KZ you end
                if (String.isNotBlank(upd.First_RAESD__r.Asset__c)) {
                    raesdIdSet.add(upd.First_RAESD__r.Asset__c);
                }
                updList.add(upd);
            }
        }
 
        // 保存
        Savepoint sp = Database.setSavepoint();
        try {
            // if  (   delList.size()  >   0   )   delete  delList;
            if (!raesdIdSet.isEmpty()) {
                List<Fixture_OneToOne_Link__c> fotos = [Select Id From Fixture_OneToOne_Link__c
                        Where Main_Asset__c =: raesdIdSet
                        for update];
                if (!fotos.isEmpty()) {
                    for (Fixture_OneToOne_Link__c foto : fotos) {
                        foto.Select_Accessory_Asset_Cnt__c = 0;
                    }
                    FixtureUtil.withoutUpdate(fotos);
                }
            }
            if  (   updList.size()  >   0   ) {
                Set<Id> assetIdSet = new Set<Id>();
                for (Rental_Apply_Equipment_Set_Detail__c raesd : [SELECT Id, Asset__c
                                                                     FROM Rental_Apply_Equipment_Set_Detail__c
                                                                    WHERE Rental_Apply_Equipment_Set__c = :updList FOR UPDATE]
                ) {
                    if (String.isNotBlank(raesd.Asset__c)) {
                        assetIdSet.add(raesd.Asset__c);
                    }
                }
                if (assetIdSet.size() > 0) {
                    List<Asset> assetList = [SELECT Id
                             FROM Asset
                            WHERE Id = :assetIdSet FOR UPDATE];
                }
                FixtureUtil.withoutUpdate(updList);
            }
            // system.debug(EquipmentSetCnt+'2223333'+updList.size()   );
            // if  ((  updList.size()  ==  EquipmentSetCnt ||  
            //         delList.size()  ==  EquipmentSetCnt ||
            //         delList.size()  +   updList.size()  ==  EquipmentSetCnt )
            //         &&  EquipmentSetCnt !=  0
            //         &&  (   rentalApply.Status__c =='引当完了'  
            //             ||  rentalApply.Status__c =='出库指示完了'      )){
            //     Rental_Apply__c RaUpdate    =   new Rental_Apply__c();
            //     RaUpdate.id =   rentalApplyId;
            //     RaUpdate.Status__c  =   '已批准';
            //     update  RaUpdate;
            // }
//bp2            ControllerUtil.setEquipmentSetProvisionFlg(esIdSet);
        }   catch (Exception ex)    {
            system.debug('=====' + ex.getMessage());
            hasError = true;
            ApexPages.addMessages(ex);
            Database.rollback(sp);
            return null;
        }
        return null;
    }
 
    @TestVisible
    class LineInfo {
        // 选择
        public boolean isSelect { get; set; }
        // 借出备品set一览
        public Rental_Apply_Equipment_Set__c raes { get; set; }
        // 状态:可以删除、可以取消、不能取消
        //public String status { get; private set; }
        public String esName { get; private set; }
        public String loanerCode { get; private set; }
        public LineInfo(Rental_Apply_Equipment_Set__c r) {
            isSelect = false;
            raes = r;
            loanerCode = r.Fixture_Set__r.Loaner_name__c;
            ////cancel
            //if (r.Yi_StockDown__c > 0) {
            //    status = 'canCancel';
            ////下架以后改成待上架的checkbox
            ////cancel
            //} else {
            //    status = 'OnStock_Pre_Inspection';
            //}
            esName = r.Fixture_Set__r.Name;
        }
    }
 
}