binxie
2024-01-20 e0de9222da210f9c8eb1a9f5400f936a14923e11
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
/*
 * @Date: 2023-05-16 13:32:13
 * @Author: @zyh
 * @LastEditTime: 2024-01-07 12:55:23
 * @Descripttion: 
 */
import { LightningElement, wire, track, api } from 'lwc';
import { CurrentPageReference } from "lightning/navigation";
import { CloseActionScreenEvent } from 'lightning/actions';
import { NavigationMixin } from 'lightning/navigation';
import init from '@salesforce/apex/lexRentalApply_FromRepCtl.init';
import userInfo from '@salesforce/apex/lexRentalApply_FromRepCtl.UserInfo_Owner';
import rentalApp from '@salesforce/apex/lexRentalApply_FromRepCtl.rentalApp';
import qureySoql from '@salesforce/apex/ControllerUtil.qureySoql';
import { encodeDefaultFieldValues } from 'lightning/pageReferenceUtils';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import lwcCSS from '@salesforce/resourceUrl/lwcCSS';
import { loadStyle } from 'lightning/platformResourceLoader'
 
export default class LexEquipmentRentalApply_FromRepair2 extends NavigationMixin(LightningElement) {
    @api recordId; //OpportunityId
    stauts1;
    repairId;
    repairName;
    hospital;
    repairDepartment;
    repairAccount;
    inspectionDate;
    shippedDate;
    assetModelNo;
    qisRepairId;
    qisId;
    qisRepairName;
    repairSource;
    maintenanceContract;
    purpose2;
    newProductGuaranteeObject;
    ifCheckFixture;
    IsLoading = true;
    isFormalStuff;
    userId;
    firstName;
    lastName;
    rtnJSON;
    records;
    dataFields;
    maintial; // 借用机会可视化-维保预判
    estimateDate; // 借用机会可视化-报价日
    agreeDate; // 借用机会可视化-同意报价日
    dateCheck = false; // 借用机会可视化-判断是否新建
    plannedRepairCompletedDate; // 修理预计完成日
    mafinal;
 
    @wire(CurrentPageReference)
    getStateParameters(currentPageReference) {
        console.log(111);
        console.log(currentPageReference);
 
        if (currentPageReference) {
            const urlValue = currentPageReference.state.recordId;
            if (urlValue) {
                let str = `${urlValue}`;
                console.log("str");
                console.log(str);
                this.recordId = str;
            }
        }
    }
 
    connectedCallback() {
        console.log(this.recordId);
 
        this.IsLoading = false;
        Promise.all([
            loadStyle(this, lwcCSS)
        ]);
        this.initRepairToApply();
    }
    async initRepairToApply(){
        await rentalApp({
            recordId: this.recordId
        }).then(result => {
            console.log('rentalApp============', result);
            if (result.length > 0) {
                const evt = new ShowToastEvent({
                    // title : 'Error',
                    message: '同一个型号的修理记录不能重复申请备品',
                    variant: 'error',
                    mode: 'sticky'
                });
                this.dispatchEvent(evt);
                this.dispatchEvent(new CloseActionScreenEvent());
                return;
            } else {
                init({
                    recordId: this.recordId
                }).then(initRes => {
                    console.log('init============' + initRes);
                    console.log(initRes);
                    if (initRes != null) {
                        userInfo({}).then(userRes => {
                            this.userId = userRes.id;
                            this.isFormalStuff = userRes.isFormalStuff;
                            // this.firstName = result.firstName;
                            this.lastName = userRes.lastName;
                        }).catch(error => {
                            console.log("error");
                            console.log(error);
                        }).finally(() => {
    
                        });
                        this.stauts1 = initRes.stauts1;
                        this.repairId = initRes.repairId;
                        this.repairName = initRes.repairName;
                        this.hospital = initRes.hospital;
                        this.inspectionDate = initRes.inspectionDate;
                        this.shippedDate = initRes.shippedDate;
                        this.qisId = initRes.qisId;
                        this.newProductGuaranteeObject = initRes.newProductGuaranteeObject;
                        this.maintenanceContract = initRes.maintenanceContract;
                        this.repairSource = initRes.repairSource;
                        this.ifCheckFixture = initRes.ifCheckFixture;
                        this.repairDepartment = initRes.repairDepartment;
                        this.repairAccount = initRes.repairAccount;
                        this.maintial = initRes.maintial; // 借用机会可视化-维保预判
                        this.estimateDate = initRes.estimateDate; // 借用机会可视化-报价日
                        this.agreeDate = initRes.agreeDate; // 借用机会可视化-同意日
                        this.plannedRepairCompletedDate = initRes.plannedRepairCompletedDate; // 借用机会可视化-修理预计完成日
                        this.mafinal = initRes.mafinal; // 借用机会可视化-维保项目
                        
                        if (this.inspectionDate != null) {
                            this.showToast('从修理做备品借出申请时,修理的[最终检测日]必须为空','error');
                            console.log('从修理做备品借出申请时,修理的[最终检测日]必须为空' + this.inspectionDate);
                            return;
                        }
                        if (this.shippedDate != null) {
                            this.showToast('从修理做备品借出申请时,修理的[RC修理品返送日]必须为空','error');
                            console.log('从修理做备品借出申请时,修理的[RC修理品返送日]必须为空' + this.shippedDate);
                            return;
                        }
                        if (this.isFormalStuff == true) {
                            this.showToast('试用期内,不能申请','error');
                            console.log('试用期内,不能申请' + this.isFormalStuff);
                            return;
                        } else if (this.stauts1 == '0.删除') {
                            this.showToast('修理的状态为删除,不可借用备品','error');
                            console.log('修理的状态为删除,不可借用备品' + this.stauts1);
                            return;
                        } else if (this.stauts1 == '0.取消') {
                            this.showToast('修理的状态为取消,不可借用备品','error');
                            console.log('修理的状态为取消,不可借用备品' + this.stauts1);
                            return;
                        } else if (this.stauts1 == '5.完毕') {
                            this.showToast('修理的状态为完毕,不可借用备品','error');
                            console.log('修理的状态为完毕,不可借用备品' + this.stauts1);
                            return;
                            // 借用机会可视化 zyh start
                        } else if (this.maintial == true && ((this.estimateDate == null && this.agreeDate == null) || (this.estimateDate == undefined && this.agreeDate == undefined) || this.mafinal != undefined) && this.maintenanceContract != undefined){
                            this.showToast('维保预判时,报价日/同意日为空,不可借用备品','error');
                            console.log('借用机会可视化============验证',this.maintial == true,this.estimateDate == null,this.agreeDate == null,this.mafinal != '',this.mafinal);
                            return;
                            // 借用机会可视化 zyh end
                        }
                        console.log('======='+this.maintial,this.estimateDate,this.maintenanceContract)
                        if (this.qisId != '' && this.qisId != null) {
                            console.log('qureySoql1===row');
                            var sql = "select Delivered_Product__r.Product2.Asset_Model_No__c,QIS_ID__r.Repair__c,QIS_ID__r.Repair__r.Name from Repair__c where id ='" + this.recordId + "'";
                            this.selectSoql(sql);
                        } else {
                            console.log('qureySoql2===row');
                            console.log("select Delivered_Product__r.Product2.Asset_Model_No__c from Repair__c where id ='" + this.recordId + "'");
                            var sql = "select Delivered_Product__r.Product2.Asset_Model_No__c from Repair__c where id ='" + this.recordId + "'";
                            this.selectSoql(sql);
                            
                        }
                        setTimeout(() => {
                            this.dispatchEvent(new CloseActionScreenEvent());
                        },10000);
                    }
                }).catch(error => {
                    console.log("error");
                    console.log(error);
                }).finally(() => {
 
                });
            }
        }).catch(error => {
            console.log("error11");
            console.log(error);
        }).finally(() => {
 
        });
    }
    selectSoql(sql){
        qureySoql({
            soql: sql
        }).then(result => {
            console.log('qureySoql============' + result);
            this.rtnJSON = result;
            console.log('rtnJSON111111===row' + this.rtnJSON);
            this.records = JSON.parse(this.rtnJSON);
            this.repairToApply();
 
        }).catch(error => {
            console.log("error");
            console.log(error);
        }).finally(() => {
 
        });
    }
    repairToApply() {
        console.log('repairToApply_Status============' + this.stauts1);
        if (this.stauts1 != '草案中') {
            var AssetModelNo = '';
            if (this.records.length > 0) {
                console.log('records.lenth===row');
                AssetModelNo = this.records[0].Delivered_Product__r.Product2.Asset_Model_No__c;
                console.log('records.lenth===qisId' + this.qisId);
                console.log('records.lenth===qisIdtest' + this.qisId != '' && this.qisId != null);
                if (this.qisId != '' && this.qisId != null) {
                    this.qisRepairId = this.records[0].QIS_ID__r.Repair__c;
                    this.qisRepairName = this.records[0].QIS_ID__r.Repair__r.Name;
                }
            }
            var isKeepRepair = false;
            if (this.newProductGuaranteeObject == '2: 服务多年保修' && (AssetModelNo == 'LTF-190-10-3D' || AssetModelNo == 'LTF-S190-5' || AssetModelNo == 'CYF-VHA' || AssetModelNo == 'CYF-VA2' || AssetModelNo == 'CYF-5A' || AssetModelNo == 'LTF-S190-10' || AssetModelNo == 'OER-AW' || AssetModelNo == 'URF-V' || AssetModelNo == 'URF-V2' || AssetModelNo == 'URF-P6')) {
                isKeepRepair = true;
            }
            var purpose2 = '';
            if (this.repairSource != 'QIS转修理') {
                if (this.newProductGuaranteeObject == '8: 市场多年保修') {
                    purpose2 = '市场多年保修';
                } else if (this.maintenanceContract != null && this.maintenanceContract != '') {
                    purpose2 = "保修用户";
                } else if (this.ifCheckFixture) {
                    purpose2 = "故障排查";
                } else {
                    if (isKeepRepair) {
                        purpose2 = '保修用户';
                    } else {
                        purpose2 = '一般用户';
                    }
                }
            } else {
                purpose2 = '索赔QIS';
            }
            let defaultValues = {
                // RecordTypeId : '',
                Name: '*',
                //2023 7 11 update by 张赫阳 PIPL Start
                RepairName: this.repairName,
                //2023 7 11 update by 张赫阳 PIPL End
                Hospital__c: this.hospital,
                Strategic_dept__c: this.repairDepartment,
                Account__c: this.repairAccount,
                Demo_purpose1__c: '维修代用',
                demo_purpose2__c: purpose2,
                Person_In_Charge__c: this.userId, //备品出街担当
                applyUser__c: this.userId, //操作者
                Repair__c: this.repairId,
                Loaner_received_staff__c: this.lastName // 收件者姓名 (销售或FSE)
            }
 
            this.dataFields = defaultValues;
            if (purpose2 == '索赔QIS' && this.qisId != null && this.qisId != '') {
                let defaultValues = {
                    // RecordTypeId : '',
                    Repair__c: this.repairId,
                    //2023 7 11 update by 张赫阳 PIPL Start
                    RepairName: this.repairName,
                    //2023 7 11 update by 张赫阳 PIPL End
                    Name: '*',
                    Hospital__c: this.hospital,
                    Strategic_dept__c: this.repairDepartment,
                    Account__c: this.repairAccount,
                    Demo_purpose1__c: '维修代用',
                    demo_purpose2__c: purpose2,
                    Person_In_Charge__c: this.userId, //备品出街担当
                    applyUser__c: this.userId, //操作者
                    Loaner_received_staff__c: this.lastName, // 收件者姓名 (销售或FSE)
                    QISRepair__c: this.qisRepairId,
                    QIS_number__c: this.qisId
                }
 
                this.dataFields = defaultValues;
                console.log('testzyh==='+this.qisRepairName);
            }
            if(this.plannedRepairCompletedDate != null){
                if(confirm('修理预计完成日为'+this.plannedRepairCompletedDate+',是否确认新建申请单?')){
                    this.rentalApply();
                }else {
                    return;
                }
            }else {
                this.rentalApply();
            }
 
        } else {
            this.showToast('申请修理后,才可以新建备品借出申请','error');
            return;
        }
    }
    rentalApply(){
        console.log('236===row12313' , this.dataFields);
        this[NavigationMixin.Navigate]({
            type: 'standard__objectPage',
            attributes: {
                objectApiName: 'Rental_Apply__c',
                actionName: 'new'
            },
            state: {
                //nooverride: '1',
                defaultFieldValues: encodeDefaultFieldValues(this.dataFields) }
        });
        console.log('236===row11111' + this.dataFields);
    }
    // 提醒、报错
    showToast(msg,type) {
        if(type == "success"){
          const event = new ShowToastEvent({
              title: '',
              message: msg,
              variant: type
          });
          this.dispatchEvent(event);
        }else{
          const event = new ShowToastEvent({
              title: '',
              message: msg,
              variant: type,
              mode:'sticky'
          });
          this.dispatchEvent(event);
        }
        this.dispatchEvent(new CloseActionScreenEvent());
    }
}