buli
2023-05-23 07390e2fcb4adf27c928335bf27ae7939c5a80ad
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
import { LightningElement, track, wire, api } from 'lwc';
import { CloseActionScreenEvent } from 'lightning/actions';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { CurrentPageReference,NavigationMixin } from 'lightning/navigation';
import { updateRecord } from 'lightning/uiRecordApi';
 
import init from '@salesforce/apex/AddSubmitApprovalProcessController.init';
import submitApproval from '@salesforce/apex/AddSubmitApprovalProcessController.submitApproval';
import setSObjectShare from '@salesforce/apex/ControllerUtil.setSObjectShare';
 
export default class lexAddSubmitApprovalProcess extends LightningElement {
    @api recordId;
    IsLoading = true;
    RecordTypeId;
 
    @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=>{
            if(result.addApprovalStatus != '草案中' || result.detailSize == 0){
                this.ToastShow('没有需要审批的追加附属品','warning');
                return;
            }
            if (!confirm("一旦提交此记录以待批准,根据您的设置您可能不再能够编辑此记录或将他从批准过程中调回。是否继续?")) {
                this.dispatchEvent(new CloseActionScreenEvent());
                return;
            }
            // 希望到货日不能早于申请提交日-0418追加
            //if ('{!Rental_Apply__c.Request_shipping_day__c}' < '{!TODAY()}') {
            // alert('希望到货日不能早于申请提交日');
            // return;
            // } 
            console.log(JSON.stringify(result));
            if(result.repairId != null){
                if(result.demoPurpose1 == '维修代用' && result.demoPurpose2 == '一般用户'){
                    if(result.repairEstimatedDateFormula == null){
                        this.ToastShow('一般维修无报价日,不可借用备品','warning');
                        return;
                    }else if(result.repairEstimatedDateFormula <'2019-07-01' && result.agreedDate == null){
                        this.ToastShow('报价日在2019/7/1之前且户同意日为空,不可借用备品','warning');
                        return;
                    }
                }
                //备品申请 过去历史转移
                if(result.recordTypeId != result.demoRequestPastDataId && result.newProductGuaranteeObject=='2: 服务多年保修' && (result.assetModelNo=='CV-V1' ||result.assetModelNo=='CV-V1(A)'||result.assetModelNo=='CV-V1(B)'||result.assetModelNo=='GIF-LV1'||result.assetModelNo=='CF-LV1L'||result.assetModelNo=='CF-LV1I'||result.assetModelNo=='MAJ-1910') &&(result.demoPurpose2=='一般用户'||result.demoPurpose2=='再修理')){
                    this.ToastShow('奥辉设备,保修期内不提供备品.','warning');
                    return;
                }
            }
 
            if(result.campaignId!=null || result.campaignId!=''){
                if(result.campaignStatus == '草案中'){
                    this.ToastShow('学会状态为草案中,不能提交','warning');
                    return;
                }
                if(result.campaignStatus == '申请中'){
                    this.ToastShow('学会状态为申请中,不能提交','warning');
                    return;
                }
                if(result.campaignStatus == '已结束'){
                    this.ToastShow('学会状态为已结束,不能提交','warning');
                    return;
                }
                if(result.campaignStatus == '已提交报告'){
                    this.ToastShow('学会状态为已提交报告,不能提交','warning');
                    return;
                }
                if(result.campaignStatus == '取消申请中'){
                    this.ToastShow('学会状态为取消申请中,不能提交','warning');
                    return;
                }
                if(result.campaignStatus == '取消'){
                    this.ToastShow('学会状态为取消,不能提交','warning');
                    return;
                }
            }
 
            if(result.qISNumber!=null || result.qISNumber!=''){
                if(result.nextAction=='送回'){
                    this.ToastShow('QIS 已送回,不能再申请备品了','warning');
                    return;
                }
            }
 
            // share
            var userAccess = new Array();
            userAccess.push(result.applyUserId + '_Edit'); 
 
            //SWAG-BZMA8W 去掉 服务经理部长共享 start
            //update lxy DLIU-BZNBG9 去掉服务经理部长共享
            //userAccess.push('{!Rental_Apply__c.JingliApprovalManagerId__c}_Read');
            //userAccess.push('{!Rental_Apply__c.SalesManagerId__c}_Read');//20210727 ljh SFDC-C54C33 update 共享触发器完成
            //update lxy DLIU-BZNBG9 去掉服务经理部长共享
            //userAccess.push('{!Rental_Apply__c.BuchangApprovalManagerId__c}_Read');
            //SWAG-BZMA8W 去掉 服务经理部长共享 end
            //userAccess.push('{!Rental_Apply__c.BuchangApprovalManagerSalesId__c}_Read');//20210727 ljh SFDC-C54C33 update 共享触发器完成
            //userAccess.push('{!Rental_Apply__c.ZongjianApprovalManagerId__c}_Read');//20210727 ljh SFDC-C54C33 update 共享触发器完成
            //userAccess.push('{!Rental_Apply__c.Status__c}_Read');
 
            setSObjectShare({
                sobjectName:'Rental_Apply__Share',
                rowCause:'ApplyUserShare__c',
                parentId:result.rentalApplyId,
                userAccess:userAccess,
                ownerId:result.ownerId
            }).then(res=>{
                console.log('setSObjectShareSuccess===='+res);
                if(res!='OK'){            
                    this.ToastShow(res,'warning');
                    return;
                }
            }).catch(err=>{
                console.log('setSObjectShareErr===='+err);
            })
 
            // var request = new sforce.ProcessSubmitRequest();
            // request.objectId = "{!Rental_Apply__c.Id}";
            // var processResults = sforce.connection.process([request]); 
 
 
            // if (processResults[0].errors != null) {
            //     var back = new sforce.SObject("Rental_Apply__c");
            //     back.Id = "{!Rental_Apply__c.Id}";
            //     back.Add_Approval_Status__c = "草案中";
            //     var backResult = sforce.connection.update([back]);
            //     backMessages = this.getConnectDMLErrorMessages(backResult);
            //     if (backMessages.length > 0) {
            //         this.showToast(backMessages.join("\n"),'error');
            //         return;
            //     }
 
            //     var errmsg = processResults[0].errors.message.toString();
            //     this.showToast(errmsg,'error');
            //     return;
            // }
 
            //updateRecord({fields: { Id: this.recordId }});
 
            submitApproval({
                recordId: this.recordId
            }).then(res=>{
                console.log('submitApproval===='+res);
                if(res == 'OK'){
                    this.ToastShow('提交成功','success');
                }else{
                    this.ToastShow(res,'error');
                }
            })
            updateRecord({fields: { Id: this.recordId }});
            
        }).catch(err=>{
            console.log('sErr====',err.message);
        })
    }
 
    ToastShow(msg,type){
        const evt = new ShowToastEvent({
            title : msg,
            message: '',
            variant: type
        });
        this.dispatchEvent(evt);
        this.dispatchEvent(new CloseActionScreenEvent());
    }
 
    getConnectDMLErrorMessages(results){
        var messages = [],
        i = 0,
        len = results.length,
        r;
        for (; i < len; i++) {
            r = results[i];
            if (!r.getBoolean("success")) {
                messages = messages.concat(this.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 + " " + this.getConnectDMLErrorFields(e));
        }
        return messages;
    }
 
    getConnectDMLErrorFields(error){
        var fields = error.getArray('fields');
        if (fields.length > 0) {
        return "[" + fields.join(",") + "]"
        } else {
            return "";
        }
    }
}