高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
public with sharing class ConsumTrialConfirmController {
    public Consum_Apply__c ca {get;set;}
    public Boolean applyerCanEdit {get;set;}
    public Boolean centerCanEdit {get;set;}
    private String caid;
    public ConsumTrialConfirmController() {
        caid = System.currentPageReference().getParameters().get('id');
    }
    public void init(){
        if (String.isBlank(caid)) {
            ApexPages.addmessage(new ApexPages.Message(ApexPages.severity.ERROR, '无效的耗材申请Id参数') );
            return ;
        }
        List<Consum_Apply__c> caList = [SELECT Id
                                             , HP_received_sign_rich__c
                                             , HP_received_sign_NG__c
                                             , HP_received_sign_NG_Reason__c
                                             , AssetManageConfirm__c
                                             , RA_Status__c
                                          FROM Consum_Apply__c
                                         WHERE Id =: caid
                                       ];
        if (caList.size() > 0) {
            ca = caList[0];
        }
        else {
            ApexPages.addmessage(new ApexPages.Message(ApexPages.severity.ERROR, '无效的耗材申请Id参数') );
            return;
        }
 
        setEditable(ca);
        String savetype = System.currentPageReference().getParameters().get('savetype');
        if(savetype == '1'){
            ApexPages.addmessage(new ApexPages.Message(ApexPages.severity.INFO, '操作完成') );
        }
 
    }
    /**
    @param ca Consum_Apply__c 耗材申请
    @description 根据用户身份和耗材申请中字段状态,控制画面的可编辑
    */
    public void setEditable(Consum_Apply__c ca){
        Id profileId = userinfo.getProfileId();
        Profile pf = [Select Id, Name from Profile where Id=:profileId];
        applyerCanEdit = false;
        centerCanEdit = false;
        if(pf.Id == '00e10000000Y3o5AAC'){ // 系统管理员
            applyerCanEdit = true;
            centerCanEdit = true;
        }
        else {
            String pfNo = pf.Name.split('_')[0];
            if (pfNo.contains('2B')){
                // 20211217 ljh SFDC-C9RCEX update start
                User currentUser = [SELECT UserRole.Name, ProfileId,OCM_man_province_Rental__c,Default_Referable_Apply_Equipment_Center__c,Dept__c  FROM User WHERE Id=: UserInfo.getUserId()];
                // User currentUser = [SELECT UserRole.Name, ProfileId,OCM_man_province_Rental__c,Default_Referable_Apply_Equipment_Center__c   FROM User WHERE Id= '00510000001OOJx'];
                //用于判断角色
                String roleName = String.valueOf(currentUser.UserRole.Name);
                if( (pf.Id == System.Label.ProfileId_EquipmentCenter && roleName == '备品中心华东管理成员')
                    || pf.Id == System.Label.ProfileId_EquCenAdminPic
                ){ 
                    applyerCanEdit = ca.RA_Status__c == '申请者已收货';
                }else{
                    applyerCanEdit  = false;
                }
                // applyerCanEdit  = false;
                // 20211217 ljh SFDC-C9RCEX update end
                // 已上传试用表 且 没有驳回 且 没有最终确认时,可以编辑
                centerCanEdit = String.isNotBlank(ca.HP_received_sign_rich__c)
                              && !ca.HP_received_sign_NG__c
                              && !ca.AssetManageConfirm__c;               
            }
            else{
                // 申请者已收货状态下,可编辑
                applyerCanEdit = ca.RA_Status__c == '申请者已收货';
                centerCanEdit = false;
            }
        }
    }
    /**
    @description 全部消耗按钮
    */
    public PageReference allConsumed() {
        Savepoint sp = Database.setSavepoint();
        try{
            if(String.isBlank(ca.HP_received_sign_rich__c)){
                throw new ControllerUtil.myException('试用表不可为空!');
            }
            ca.HP_received_sign_NG__c = false;
            ca.HP_received_sign_NG_Reason__c = null;
            List<Consum_Apply_Equipment_Set_Detail__c> caesdList = [
                    SELECT Id
                         , Received_Confirm__c
                         , Check_lost_Item__c
                         , Asset_Center_Confirm_F__c
                         , RAESD_Status__c
                      FROM Consum_Apply_Equipment_Set_Detail__c
                     WHERE Consum_Apply__c =: caid
                       AND Cancel_Select__c = false
                       FOR UPDATE
                ];
            // 有未做到货确认的明细,不能操作全部消耗,回寄
            for(Consum_Apply_Equipment_Set_Detail__c caesd : caesdList){
                if( caesd.Asset_Center_Confirm_F__c != 'OK') {
                    throw new ControllerUtil.myException('未完成到货确认或备品中心未确认不能上传试用表');
                }
            }
 
            Datetime now = System.now();
            for(Consum_Apply_Equipment_Set_Detail__c caesd : caesdList){
                if(caesd.RAESD_Status__c == '申请者已收货') {
                    caesd.Check_lost_Item__c = '消耗';
                }
                // trigger 里修改 操作消耗人员 和 时间
            }
            FixtureUtil.withoutUpdate(caesdList);
            FixtureUtil.withoutUpdate(new List<Consum_Apply__c> {ca});
            // 刷新画面
            PageReference page = new PageReference('/apex/ConsumTrialConfirm');
            page.getParameters().put('id', caid);
            page.getParameters().put('savetype', '1');
            page.setRedirect(true);
            return page;
        }
        catch(Exception ex){
            ApexPages.addMessages(ex);
            Database.rollback(sp);
        }
        return null;
    }
 
    /**
    @ description 跳到部分寄回画面
    */
    public PageReference partSendback() {
        Savepoint sp = Database.setSavepoint();
        try{
            if(String.isBlank(ca.HP_received_sign_rich__c)){
                throw new ControllerUtil.myException('试用表不可为空!');
            }
            List<Consum_Apply_Equipment_Set_Detail__c> caesdList = [
                    SELECT Id
                         , Received_Confirm__c
                         , Asset_Center_Confirm__c
                      FROM Consum_Apply_Equipment_Set_Detail__c
                     WHERE Consum_Apply__c =: caid
                       AND Cancel_Select__c = false
                       FOR UPDATE
                ];
            // 有未做到货确认的明细,不能操作全部消耗,回寄
            for(Consum_Apply_Equipment_Set_Detail__c caesd : caesdList){
                if( caesd.Received_Confirm__c == 'OK' || caesd.Received_Confirm__c == '默认签收-OK' || caesd.Asset_Center_Confirm__c == 'OK') {
                    continue;
                }
                else{
                    throw new ControllerUtil.myException('未完成到货确认或备品中心未确认不能上传试用表');
                }
            }
            ca.HP_received_sign_NG__c = false;
            ca.HP_received_sign_NG_Reason__c = null;
            FixtureUtil.withoutUpdate(new List<Consum_Apply__c> {ca});
 
            PageReference sendbackPage = new PageReference('/apex/ConsumEquipmentSetReturn');
            sendbackPage.getParameters().put('id', caid);
            sendbackPage.setRedirect(true);
            return sendbackPage;
        }
        catch(Exception ex){
            ApexPages.addMessages(ex);
            Database.rollback(sp);
            return null;
        }
    }
    /**
    @description 备品中心确认后保存
    */
    public PageReference save(){
        Savepoint sp = Database.setSavepoint();
        try{
            List<Consum_Apply_Equipment_Set_Detail__c> caesdList = [
                SELECT Id
                     , Received_Confirm__c
                     , Check_lost_Item_Final__c
                     , Return_DeliverySlip__c
                     , Asset__c
                  FROM Consum_Apply_Equipment_Set_Detail__c
                 WHERE Consum_Apply__c =: caid
                   AND Cancel_Select__c = false
                   FOR UPDATE
            ];
 
            // 试用表不合格
            if(ca.HP_received_sign_NG__c){
                if(String.isBlank(ca.HP_received_sign_NG_Reason__c)) {
                    throw new ControllerUtil.myException('试用表不合格理由不可为空!');
                }
                if(ca.AssetManageConfirm__c){
                    throw new ControllerUtil.myException('试用表不合格时,备品中心不能确认!');
                }
                for(Consum_Apply_Equipment_Set_Detail__c caesd : caesdList){
                    if(caesd.Check_lost_Item_Final__c != '消耗') {
                       caesd.Return_DeliverySlip__c = null;
                       caesd.Check_lost_Item__c = null;
                       caesd.Asset_return_time__c = null;
                    }
                }
                FixtureUtil.withoutUpdate(new List<Consum_Apply__c> {ca});
                FixtureUtil.withoutUpdate(caesdList);
                // 刷新画面
                PageReference page = new PageReference('/apex/ConsumTrialConfirm');
                page.getParameters().put('id', caid);
                page.getParameters().put('savetype', '1');
                page.setRedirect(true);
                return page;
 
            // 备品中心确认OK
            }else if(ca.AssetManageConfirm__c) {
                if(String.isBlank(ca.HP_received_sign_rich__c)){
                    throw new ControllerUtil.myException('试用表不可为空!');
                }
                FixtureUtil.withoutUpdate(new List<Consum_Apply__c> {ca});
 
                Set<Id> assetIdSet = new Set<Id>();
                for(Consum_Apply_Equipment_Set_Detail__c caesd : caesdList) {
                    caesd.Check_lost_Item__c = null;
                    // 没寄回的全部当成消耗
                    if(caesd.Return_DeliverySlip__c == null) {
                        caesd.Check_lost_Item_Final__c = '消耗';
                        assetIdSet.add(caesd.Asset__c);
                    }
                    // trigger 里操作保有设备的已消耗数量
                }
 
                List<Asset> assetList = [
                    SELECT Id
                      FROM Asset
                     WHERE Id IN:assetIdSet
                     FOR UPDATE
                ];
 
                FixtureUtil.withoutUpdate(caesdList);
                // 刷新画面
                PageReference page = new PageReference('/apex/ConsumTrialConfirm');
                page.getParameters().put('id', caid);
                page.getParameters().put('savetype', '1');
                page.setRedirect(true);
                return page;
            }
        }
        catch(Exception ex){
            ApexPages.addMessages(ex);
            Database.rollback(sp);
        }
        return null;
    }
 
    @TestVisible private static void test() {
        if (false == Test.isRunningTest()) return;
        Integer i = 0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        //20211220 add
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
    }
}