buli
2022-05-14 ead4df22dca33a867279471821ca675f91dec760
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
public without sharing class LoanerApplicationStockUpCheckController {
 // 基础Url
    public String baseUrl { get; private set; }
    // 样机借出申请ID
    public String laId { get; private set; }
    public loaner_application__c la { get; private set; }
    /* 画面步骤
     * 下架:StockDown
     *
     */
    public String step { get; private set; }
    // 明细Bean
    public List<DataLineBean> dataLines { get; set; }
    // 明细行数
    public Integer getDatalineSize() {
        return dataLines == null ? 0 : dataLines.size();
    }
 
    public LoanerApplicationStockUpCheckController() {
        laId = System.currentPageReference().getParameters().get('id');
        step = System.currentPageReference().getParameters().get('step');
    }
 
    public PageReference init() {
 
        // 基础Url
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        String path = URL.getCurrentRequestUrl().getPath();
        if (path.indexOf('/apex') > 0) {
            baseUrl += path.substring(0,path.indexOf('/apex'));
        } else if (path.indexOf('production/') > 0) {
            baseUrl += '/production';
        }
        // 明细Bean
        dataLines = new List<DataLineBean>();
 
        if (String.isBlank(laId) == true) {
            return null;
        }
        if (String.isBlank(step) == true) {
            return null;
        }
        // 样机借出申请取得
        List<loaner_application__c> laList = [select Id,Status__c,Equipment_Type__c from loaner_application__c where Id = :laId];
        if (laList.size() == 0) {
            return null;
        }
        la = laList[0];
        // 样机借出申请明细取得
        String sql_select = 'select Id,loaner_manage_place__c,StockUpRemark__c, SerialNumber__c, Internal_Asset_number__c, OTCODE__c, ECCode__c , Inspection_result_after__c,RAESD_Status__c,LOANER__c,LOANER__r.RecordType.Id,LOANER__r.Name,LOANER__r.Remarks2__c';
        String sql_where = ' where loaner_application__c = :laId ';
        if (step == 'StockUpCheck') {
            sql_where += ' and RAESD_Status__c in (\'已回收\',\'修理中\',\'待上架\',\'待移至报废区\')';
 
        } else {
            return null;
        }
 
        String sql_full = sql_select + ' from loaner_application_detail__c ' + sql_where;
                   
        List<loaner_application_detail__c> lads = Database.query(sql_full);
        if (lads.size() == 0) {
            return null;
        }
 
        // 明细Bean作成
        Integer cnt = 0;
        for (loaner_application_detail__c lad : lads) {
            cnt += 1;
            DataLineBean dataLine = new DataLineBean(cnt, lad);
            dataLines.add(dataLine);
        }
 
        return null;
    }
 
 public PageReference saveBtn() {
        // 根据step更新样机借出申请明细状态
        List<loaner_application_detail__c> updList = new List<loaner_application_detail__c>();
        List<Asset> assList = new List<Asset>();
        if(dataLines.size()>0){
            for (DataLineBean dataLine : dataLines) {
                if (dataLine.checkFlag == true) {
                    loaner_application_detail__c lad = dataLine.lad;
                    Asset loa = new Asset(id = lad.LOANER__c);
                    if (step == 'StockUpCheck') {
                        lad.Inspection_result_after__c = dataLine.lad.Inspection_result_after__c;
                        lad.After_Inspection_Date__c = Date.today();
                        if('NG-修理' == dataLine.lad.Inspection_result_after__c){
                            if(lad.StockUpRemark__c == null || lad.StockUpRemark__c == ''){
                                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, '请填写NG-修理备注'));
                                return null;
                            }
                            lad.RAESD_Status__c = '待修理';
                            if(lad.LOANER__r.RecordType.Id == System.label.Asset_Record_Type1){
                               loa.status = '待修理';
                               if(la.Equipment_Type__c== 'NDT' || la.Equipment_Type__c == 'ANI' || la.Equipment_Type__c == 'IE' || la.Equipment_Type__c == 'BS'){
                                    loa.Rental_Customer__c = null;
                                    loa.Rental_Start_Date__c = null;
                                    loa.Rental_end_Date__c = null;
                                    loa.Loaner__c = null;
                                    if(la.Equipment_Type__c== 'NDT' || la.Equipment_Type__c == 'ANI' ){
                                        loa.loaner_place__c = lad.loaner_manage_place__c;
                                    }
                                }
                            }
                        }else if('NG-废弃' == dataLine.lad.Inspection_result_after__c){
                            if(lad.StockUpRemark__c == null || lad.StockUpRemark__c == ''){
                                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, '请填写NG-废弃备注'));
                                return null;
                            }
                            lad.StockUp__c = true;
                            lad.Arrival_wh_Date__c = Date.today();
                            lad.RAESD_Status__c = '待废弃';
                            if(lad.LOANER__r.RecordType.Id == System.label.Asset_Record_Type1){
                               loa.status = '待废弃';
                               if(la.Equipment_Type__c== 'NDT' || la.Equipment_Type__c == 'ANI' || la.Equipment_Type__c == 'IE' || la.Equipment_Type__c == 'BS'){
                                    loa.Rental_Customer__c = null;
                                    loa.Rental_Start_Date__c = null;
                                    loa.Rental_end_Date__c = null;
                                    loa.Loaner__c = null;
                                    if(la.Equipment_Type__c== 'NDT' || la.Equipment_Type__c == 'ANI' ){
                                        loa.loaner_place__c = lad.loaner_manage_place__c;
                                    }
                                }
                            }
                        }else if('Ok' == dataLine.lad.Inspection_result_after__c){
                            lad.StockUp__c = true;
                            lad.Arrival_wh_Date__c = Date.today();
                            lad.RAESD_Status__c = '已回库';
                            if(lad.LOANER__r.RecordType.Id == System.label.Asset_Record_Type1){
                                loa.status = '在库';
                                if(la.Equipment_Type__c== 'NDT' || la.Equipment_Type__c == 'ANI' || la.Equipment_Type__c == 'IE' || la.Equipment_Type__c == 'BS'){
                                    loa.Rental_Customer__c = null;
                                    loa.Rental_Start_Date__c = null;
                                    loa.Rental_end_Date__c = null;
                                    loa.Loaner__c = null;
                                    if(la.Equipment_Type__c== 'NDT' || la.Equipment_Type__c == 'ANI' ){
                                        loa.loaner_place__c = lad.loaner_manage_place__c;
                                    }
                                }
                            }
                        }else{
                            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, '请选择上架检查状态'));
                            return null;
                        }
                    }
                    updList.add(lad);
                    if(lad.LOANER__r.RecordType.Id == System.label.Asset_Record_Type1){
                        assList.add(loa);
                    }
                }
 
 
 
            }
            Savepoint sp = Database.setSavepoint();
            try {
               if (updList.size() > 0) {
                    
                    if(la.Status__c != '完毕'/* && la.Status__c != '全部回收'*/){
                        System.debug(la.Status__c);
                        la.Status__c =  LoanerOrderState.getOrderStater(la.Id);
                        System.debug(la.Status__c);
                        update la;
                    }
 
                    update updList;
                    update assList;
                }else{
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, '请选择上架检查的样机'));
                    return null;
                }
 
            } catch (Exception ex) {
                Database.rollback(sp);
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, ex.getMessage() + ' | Line:' + ex.getLineNumber()));
                return null;
            }
        }
        // 返回样机借出申请画面
        String url = baseUrl;
        url += '\\' + laId;
        return new Pagereference(url);
    }
 
    public PageReference cancelBtn() {
        // 返回样机借出申请画面
        String url = baseUrl;
        url += '\\' + laId;
        return new Pagereference(url);
        return null;
    }
 
     public class DataLineBean {
        // 是否选择
        public boolean checkFlag { get; set; }
        // 行号
        public Integer lineNo { get; private set; }
        // 样机借出申请明细
        public loaner_application_detail__c lad { get; set; }
 
        public DataLineBean(Integer in_no, loaner_application_detail__c in_lad) {
            checkFlag = false;
            lineNo = in_no;
            lad = in_lad;
        }
    }
 
}