buli
2023-04-20 f0bccccbb88d93ac05010c17d4b2e0cb22a2ce9a
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
import getStaticResource from "@salesforce/apex/ButtonJsUtility.getStaticResource";
import init from "@salesforce/apex/otherButtonRepairController.init";
import queryForEquipments from "@salesforce/apex/otherButtonRepairController.queryForEquipments";
import queryForFindInvalidLicense from "@salesforce/apex/otherButtonRepairController.queryForFindInvalidLicense";
import queryForRds from "@salesforce/apex/otherButtonRepairController.queryForRds";
import queryForReocrds from "@salesforce/apex/otherButtonRepairController.queryForReocrds";
import queryForRepairCount from "@salesforce/apex/otherButtonRepairController.queryForRepairCount";
import queryForUrfAsset from "@salesforce/apex/otherButtonRepairController.queryForUrfAsset";
import queryForUrfSeriesInfo from "@salesforce/apex/otherButtonRepairController.queryForUrfSeriesInfo";
import queryForrecords3 from "@salesforce/apex/otherButtonRepairController.queryForrecords3";
import updateForRepair1 from "@salesforce/apex/otherButtonRepairController.updateForRepair1";
import updateRepair2 from "@salesforce/apex/otherButtonRepairController.updateRepair2";
import AWSService from "@salesforce/resourceUrl/AWSService";
import common from "@salesforce/resourceUrl/CommonUtilJs";
import apex from "@salesforce/resourceUrl/apex20";
import con from "@salesforce/resourceUrl/connection20";
import jqueryurl from "@salesforce/resourceUrl/jquery183minjs";
import jq from "@salesforce/resourceUrl/jquery_jqplot";
import {
  CloseActionScreenEvent
} from "lightning/actions";
import {
  CurrentPageReference
} from "lightning/navigation";
import {
  loadScript
} from "lightning/platformResourceLoader";
import {
  ShowToastEvent
} from 'lightning/platformShowToastEvent';
import {
  updateRecord
} from "lightning/uiRecordApi";
import {
  LightningElement,
  api,
  wire
} from "lwc";
 
export default class LexSendNFM103 extends LightningElement {
  @api recordId;
  detailedAddress;
  dealer;
  numberofEffectiveContract;
  newProductGuaranteTxt;
  onSiteRepair;
  maintenanceContract;
  VMMaintenanceContract;
  FSEState;
  dateReceiptQuestions;
  delayReportReason;
  repairSource;
  quickRepairSign;
  RCCD;
  returnsProductway;
  salesDeptHP;
  isAohuiProduct;
  stateHospital;
  assetOwner;
  deliveredProductId;
  FSEApplyForRepairTime;
  maintenanceContractId;
  URFLimitSerial;
  status;
  dealerId;
  ifDeadHurt;
  dayLater30;
  recallCorrespondence;
  URFReapir;
  inchargeStaffId;
  DWSign;
  DOJStatus;
  SAPcondition;
  salesOfficeCodeSelection;
  profileId;
  userId;
  AWSDataId;
 
  @wire(CurrentPageReference)
  getStateParameters(currentPageReference) {
    if (currentPageReference) {
      const urlValue = currentPageReference.state.recordId;
      if (urlValue) {
        let str = `${urlValue}`;
        this.recordId = str;
      }
    }
  }
 
  connectedCallback() {
    init({
      recordId: this.recordId
    }).then(result => {
      console.log(result);
 
      var btns = document.getElementsByName("send_nfm103");
      for (var i = 0; i < btns.length; i++) {
        btns[i].disabled = true;
        btns[i].className = 'btnDisabled';
      }
 
      this.detailedAddress = result.detailedAddress;
      this.dealer = result.dealer;
      this.numberofEffectiveContract = result.numberofEffectiveContract;
      this.newProductGuaranteTxt = result.newProductGuaranteTxt;
      this.onSiteRepair = result.onSiteRepair;
      this.maintenanceContract = result.maintenanceContract;
      this.VMMaintenanceContract = result.VMMaintenanceContract;
      this.FSEState = result.FSEState;
      this.dateReceiptQuestions = result.dateReceiptQuestions;
      this.delayReportReason = result.delayReportReason;
      this.repairSource = result.repairSource;
      this.quickRepairSign = result.quickRepairSign;
      this.RCCD = result.RCCD;
      this.returnsProductway = result.returnsProductway;
      this.salesDeptHP = result.salesDeptHP;
      this.isAohuiProduct = result.isAohuiProduct;
      this.stateHospital = result.stateHospital;
      this.assetOwner = result.assetOwner;
      this.deliveredProductId = result.deliveredProductId;
      this.FSEApplyForRepairTime = result.FSEApplyForRepairTime;
      this.maintenanceContractId = result.maintenanceContractId;
      this.URFLimitSerial = result.URFLimitSerial;
      this.status = result.status;
      this.dealerId = result.dealerId;
      this.ifDeadHurt = result.ifDeadHurt;
      this.dayLater30 = result.dayLater30;
      this.recallCorrespondence = result.recallCorrespondence;
      this.URFReapir = result.URFReapir;
      this.inchargeStaffId = result.inchargeStaffId;
      this.DWSign = result.DWSign;
      this.DOJStatus = result.DOJStatus;
      this.SAPcondition = result.SAPcondition;
      this.salesOfficeCodeSelection = result.salesOfficeCodeSelection;
      this.profileId = result.profileId;
      this.userId = result.userId;
      this.AWSDataId = result.AWSDataId;
      
      this.logicalJudgmentA();
    }).catch(error => {
      console.log("error");
      console.log(error);
    });
  }
 
 
  showToast(msg, type) {
    const event = new ShowToastEvent({
      title: '',
      message: msg,
      variant: type
    });
    this.dispatchEvent(event);
  }
 
  updateRecordView(recordId) {
    updateRecord({
      fields: {
        Id: recordId
      }
    });
  }
 
  logicalJudgmentA() {
    try {
      if (
        this.numberofEffectiveContract == "无" &&
        this.newProductGuaranteTxt != "2: 多年保修" &&
        this.newProductGuaranteTxt != "2: 服务多年保修" &&
        this.newProductGuaranteTxt != "8: 市场多年保修" &&
        this.onSiteRepair == "直送SORC修理"
      ) {
        if (
          !confirm("设备为非维修合同且非多年保修对象品,请确认是否直送SORC修理?")
        ) {
          this.updateRecordView(this.recordId);
          this.dispatchEvent(new CloseActionScreenEvent());
          return;
        }
      }
      console.log("11");
      if (
        (this.maintenanceContract != undefined ||
          this.VMMaintenanceContract != undefined) &&
        this.onSiteRepair == "RC修理" &&
        this.FSEState == "山东省"
      ) {
        if (
          !confirm("该设备为维修合同或多年保修对象品,请确认是否送至RC修理?")
        ) {
          this.updateRecordView(this.recordId);
          this.dispatchEvent(new CloseActionScreenEvent());
          return;
        }
      }
      var today = Date.now();
      var dateReceiptQuestions = Date.parse(this.dateReceiptQuestions);
      var dates = (today - dateReceiptQuestions) / (24 * 60 * 60 * 1000);
      if (
        this.profileId != "00e10000000Y3o5" &&
        this.userId != "0051000000AKgzv" &&
        this.userId != "0051000000AKh05" &&
        this.userId != "00510000004q1ed" &&
        this.delayReportReason == "" &&
        dates > "1" &&
        this.repairSource != "QIS转修理"
      ) {
        this.showToast("申请修理时SAP修理申请日超过问题联络收到日1天,必须填写问超时报告的理由", "error");
        this.updateRecordView(this.recordId);
        this.dispatchEvent(new CloseActionScreenEvent());
        return;
      }
      if (
        this.quickRepairSign == true &&
        (this.numberofEffectiveContract == "有" ||
          this.newProductGuaranteTxt == "2: 多年保修" ||
          this.newProductGuaranteTxt == "2: 服务多年保修" ||
          this.newProductGuaranteTxt == "8: 市场多年保修") &&
        (this.onSiteRepair == "RC修理" ||
          this.onSiteRepair == "办事处修理" ||
          this.onSiteRepair == "现场修理")
      ) {
        if (this.RCCD == "2.东北" || this.RCCD == "3.西北") {
          if (
            !confirm(
              "该型号对应为合同或多年保对象品。如确认为大修理故障,需要选择直送SORC修理,请确认是否继续。"
            )
          ) {
            this.updateRecordView(this.recordId);
            this.dispatchEvent(new CloseActionScreenEvent());
            return;
          }
        } else if (
          this.RCCD == "1.华北" ||
          this.RCCD == "6.华南" ||
          this.RCCD == "4.西南"
        ) {
          if (
            !confirm(
              "该型号对应为合同或多年保对象品。如确认为大修理故障,需要选择直送OGZ修理,请确认是否继续。"
            )
          ) {
            this.updateRecordView(this.recordId);
            this.dispatchEvent(new CloseActionScreenEvent());
            return;
          }
        }
      }
      var returnsProductWay = this.returnsProductway;
      var falg = true;
      if (returnsProductWay == undefined) {
        if (!confirm("返品方式为空,是否继续申请修理!")) {
          this.updateRecordView(this.recordId);
          this.dispatchEvent(new CloseActionScreenEvent());
          return;
        }
      }
      if (this.assetOwner == "Olympus") {} else {
        console.log(this.detailedAddress);
        if (this.detailedAddress != undefined) {
          var prompt =
            "当前直返收货地址为:" + this.detailedAddress + ",您确认不修改了吗?";
          if (!confirm(prompt)) {
            this.updateRecordView(this.recordId);
            this.dispatchEvent(new CloseActionScreenEvent());
            return;
          }
        } else {
          this.showToast("当前修理没有录入直返收货的详细地址,不能申请修理!", "error");
          this.updateRecordView(this.recordId);
          this.dispatchEvent(new CloseActionScreenEvent());
          return;
        }
      }
 
      queryForEquipments({
        deliveredProductId: this.deliveredProductId,
      }).then((result) => {
        var equipments = result;
        if (equipments != null && equipments.length > 0) {
          var equipment = equipments[0];
          if (equipment.Status == "廃棄") {
            this.showToast("设备废弃,不能提交申请!", "error");
            this.updateRecordView(this.recordId);
            this.dispatchEvent(new CloseActionScreenEvent());
            return;
          }
        }
      });
      if (this.FSEApplyForRepairTime != undefined) {
        this.showToast("已修理申请,不再修理申请了", "error");
        this.updateRecordView(this.recordId);
        this.dispatchEvent(new CloseActionScreenEvent());
        return;
      }
      var surplusCount = 0;
      if (this.URFReapir == true) {
        var urfAsset = null;
 
        queryForUrfAsset({
          maintenanceContractId: this.maintenanceContractId,
          deliveredProductId: this.deliveredProductId,
        }).then((result) => {
          urfAsset = result;
          var seriesMaxRepairCount = 0;
          var seriesRepairCount = 0;
          if (urfAsset != null && urfAsset.length > 0) {
            seriesMaxRepairCount = urfAsset[0].Series_MaxRepairCount_F__c;
            seriesRepairCount = urfAsset[0].Series_RepairCount_F__c;
            surplusCount = seriesMaxRepairCount - seriesRepairCount;
          }
        });
        var urfSeriesInfo = null;
 
        queryForUrfSeriesInfo({
          URFLimitSerial: this.URFLimitSerial,
        }).then((result) => {
          urfSeriesInfo = result;
          var urfSeries = "";
          if (urfSeriesInfo != null && urfSeriesInfo.length > 0) {
            urfSeries = urfSeriesInfo[0].URFLimitSerial__c;
          }
          var repairCount = null;
 
          queryForRepairCount({
            maintenanceContractId: this.maintenanceContractId,
            status1: "1.受理完毕",
            URFLimitSerial: urfSeries,
          }).then((result) => {
            repairCount = result;
          });
          if (repairCount.length > 0 && surplusCount == 1) {
            this.showToast("该设备关联限次合同,目前剩余次数小于送修设备。请等待进行中修理完成后再进行申请", "error");
            this.updateRecordView(this.recordId);
            this.dispatchEvent(new CloseActionScreenEvent());
            return;
          }
        });
      }
      if (
        this.status != "草案中" &&
        this.status != "1.受理完毕(SAP待发送)" &&
        this.status != "4.修理品返送阶段"
      ) {
        this.showToast("不是草案中不能申请", "error");
        this.updateRecordView(this.recordId);
        this.dispatchEvent(new CloseActionScreenEvent());
      } else {
        if (this.dealer != undefined) {
          var findInvalidLicense = null;
 
          queryForFindInvalidLicense({
            isValidity: false,
            dealerId: this.dealerId,
          }).then((result) => {
            findInvalidLicense = result;
            if (findInvalidLicense.size > 0) {
              this.showToast("经销商三证不在有效期,无法送修", "error");
              this.updateRecordView(this.recordId);
              this.dispatchEvent(new CloseActionScreenEvent());
              return;
            }
          });
        }
        if (this.ifDeadHurt == "有") {
          if (!confirm("是否确认AE要提交?")) {
            this.updateRecordView(this.recordId);
            this.dispatchEvent(new CloseActionScreenEvent());
            return;
          }
        }
        if (this.dayLater30 == "是") {
          this.showToast("创建后超过30天,不能申请了", "error");
          this.updateRecordView(this.recordId);
          this.dispatchEvent(new CloseActionScreenEvent());
          return;
        }
        var prompt11 = "合同内大修次数已用完,当前修理为合同外的修理,是否继续?";
        if (this.URFReapir == true && surplusCount == 0) {
          var resultTmp = null;
          if (!confirm(prompt11)) {
            this.updateRecordView(this.recordId);
            this.dispatchEvent(new CloseActionScreenEvent());
            return;
          }
 
          updateForRepair1({
            recordId: this.recordId,
          }).then((result) => {
            resultTmp = result;
            console.log("1111");
            var messagesTmp = "";
            if (resultTmp) {
              messagesTmp = this.getConnectDMLErrorMessages(resultTmp);
            }
            if (messagesTmp.length > 0) {
              this.showToast(messagesTmp.join("\n"), "error");
              this.updateRecordView(this.recordId);
              this.dispatchEvent(new CloseActionScreenEvent());
              return;
            }
          });
        }
        var records = null;
 
        queryForReocrds({
          inchargeStaffId: this.inchargeStaffId,
        }).then((result) => {
          records = result;
          console.log(result);
          this.logicalJudgmentB(records);
        });
      }
    } catch (error) {
      console(error);
    }
    var staticResource = null;
 
    getStaticResource({
      sobj: "Repair__c"
    }).then(result => {
      console.log(result);
      staticResource = JSON.parse(result);
      Promise.all([
        loadScript(this, AWSService + '/AWSService.js'),
        loadScript(this, jqueryurl),
        loadScript(this, jq + "/dist/jquery.js"),
        loadScript(this, con),
        loadScript(this, apex),
        loadScript(this, common)
      ]).then(() => {
        window.AWSService.query(staticResource.queryUrl,
          this.AWSDataId,
          function (data) {
            console.log(data);
            if (data && data.object) {
              var DetailedAddress = data.object.detailedAddress;
            }
            this.foo();
          },
          staticResource.token).then(result => {
          this.updateRecordView(this.recordId);
          this.dispatchEvent(new CloseActionScreenEvent());
        });
      }).catch(error => {
        console.log('Failed to load the JQuery : ' + error);
      });
    }).catch(error => {
      console.log('Failed to load the JQuery : ' + error);
    });
    console.log('load correctly');
  }
 
  foo() {
    console.log("end");
    var records3 = null;
 
    queryForrecords3({
      recordId: this.recordId,
    }).then((result) => {
      records3 = result;
    });
    var FirstParagraph__c = "";
    if (records3.length >= 1) {
      FirstParagraph__c = records3[0].FirstParagraph__c;
    }
    if (FirstParagraph__c == "true") {
      if (!confirm("该经销商为先款对象,是否继续申请修理?")) {
        this.updateRecordView(this.recordId);
        this.dispatchEvent(new CloseActionScreenEvent());
        return;
      }
    }
    console.log("endend");
  }
 
  logicalJudgmentB(records) {
    if (records != null && records.length > 0) {
      var staff = records[0];
      var res = null;
      updateRepair2({
        recordId: this.recordId,
        staff: staff,
        DWSign: this.DWSign,
        statusc: "1.受理完毕",
        DOJStatus: this.DOJStatus,
        SAPcondition: this.SAPcondition,
      }).then((result) => {
        console.log(result + "result");
        res = result;
        console.log("start3");
        var messages = "";
        if (res) {
          messages = this.getConnectDMLErrorMessages(res);
        }
        if (messages.length > 0) {
          this.showToast(messages.join("\n"), "error");
          this.updateRecordView(this.recordId);
          this.dispatchEvent(new CloseActionScreenEvent());
        } else {
          if (this.salesOfficeCodeSelection == "成都RC") {
 
            this.showToast("现阶段,成都RC仅受理四川(西藏)内镜、西南全域CV/CLV,其余实物请发至广州RC。", "error");
            this.updateRecordView(this.recordId);
            this.dispatchEvent(new CloseActionScreenEvent());
          }
        }
        var rds = null;
        
        queryForRds({
          recordId: this.recordId,
        }).then((result) => {
          rds = result;
          if (rds != null && rds.length > 0) {
            var repairAft = rds[0];
            if (
              repairAft.NewProductGuarante_Formula__c !=
              repairAft.ProductGuarante_Create__c
            ) {
              this.showToast("该设备保修信息已发生变化,请了解", "error");
            }
          }
        });
      });
 
    } else {
      this.showToast("没有找到 修理委托者", "error");
      this.updateRecordView(this.recordId);
      this.dispatchEvent(new CloseActionScreenEvent());
    }
  }
 
  getConnectDMLErrorMessages(results) {
    console.log(results);
    var messages = [],
      i = 0,
      len = results.length,
      r;
    console.log("sos");
    for (; i < len; i++) {
      r = results[i];
      if (!r.getBoolean("success")) {
        messages = messages.concat(getConnectDMLMessagesOfAResult(r));
      }
    }
    return messages;
  }
 
  getConnectDMLMessagesOfAResult(res) {
    var messages = [],
      errors = res.getArray("errors"),
      i = 0,
      len = errors.length,
      e;
    for (; i < len; i++) {
      e = errors[i];
      messages.push(e.message + " " + getConnectDMLErrorFields(e));
    }
    return messages;
  }
 
  getConnectDMLErrorFields(error) {
    var fields = error.getArray("fields");
    if (fields.length > 0) {
      return "[" + fields.join(",") + "]";
    } else {
      return "";
    }
  }
}