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
import { LightningElement , wire, api} from 'lwc';
import { CurrentPageReference,NavigationMixin } from 'lightning/navigation';
import { CloseActionScreenEvent } from 'lightning/actions';
import { updateRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
 
import init from '@salesforce/apex/ConsumApplyApprovalProcessController.initSubmitApprovalProcessController';
import selectCampaignById from '@salesforce/apex/ConsumApplyApprovalProcessController.selectCampaignById';
import selectReportById from '@salesforce/apex/ConsumApplyApprovalProcessController.selectReportById';
import selectEquipmentById from '@salesforce/apex/ConsumApplyApprovalProcessController.selectEquipmentById';
import approvalCheck from '@salesforce/apex/ConsumApplyApprovalProcessController.approvalCheck';
import setSObjectShare from '@salesforce/apex/ConsumApplyApprovalProcessController.setSObjectShare';
import updateConsumApply from '@salesforce/apex/ConsumApplyApprovalProcessController.updateConsumApply';
// import setSObjectShare from '@salesforce/apex/ControllerUtil.setSObjectShare';
 
 
export default class LexConsumApplySubmitApprovalProcess extends LightningElement {
 
    @api recordId;
    id;
    shippingDays;//希望到货日前7个工作日
    shippingDay;//希望到货日
    campaign ;//学会
    qISNumber;//QIS
    qISIDLine;//QIS的ID值
    status;//    状态
    minApprovalDate;//最小可以提交申请日期
    applyUser;//操作者
    jingliManager;//CL6 经理级(服务)
    salesManager;//CL6 经理级
    buchangManager;//CL5 总监级(服务)
    buchangManagerSale;//CL5 总监级
    zongjianManager;//CL4 高级总监
    owner;//所有人
    IsLoading = true;
 
    @wire(CurrentPageReference)
     getStateParameters(currentPageReference) {
         if (currentPageReference) {
           const urlValue = currentPageReference.state.recordId;
           if (urlValue) {
             let str = `${urlValue}`;
             console.log("str:"+str);
             this.recordId = str;
            }
         }
     }
 
 
     connectedCallback(){
        init({recordId : this.recordId}).then( result => {
        console.log('result'+result);
        if(result!= null){
          this.IsLoading = false;
          this.id = result.id;
          this.shippingDays = result.shippingDays;
          this.shippingDay = result.shippingDay;
          this.campaign = result.campaign;
          this.qISNumber = result.qISNumber;
          this.qISIDLine = result.qISIDLine;
          this.status = result.status;
          this.minApprovalDate = result.minApprovalDate;
          this.applyUser = result.applyUser;
          this.jingliManager = result.jingliManager;
          this.salesManager = result.salesManager;
          this.buchangManager = result.buchangManager;
          this.buchangManagerSale = result.buchangManagerSale;
          this.zongjianManager = result.zongjianManager;
          this.owner = result.owner;
 
          var date = new Date();
          var day = date.getDate();
 
          console.log('shipingDays'+this.shippingDays);
          if(this.shippingDays < day){
            this.showToast('此申请已不满足(希望到货日-7工作日)前完成审批的情况,请重新修改申请。','error');
          }
 
          if(!confirm('一旦提交此记录以待批准,根据您的设置您可能不再能够编辑此记录或将他从批准过程中调回。是否继续?\n\n※ 需要在'
          +this.shippingDays+'前完成审批,否则申请单自动取消。')){
            console.log('2222222222');
            this.IsLoading = false;
            this.showToast('已取消','error')
          }
        
 
          console.log('3333333333333');
          if(this.shippingDay < day){
            this.showToast('希望到货日不能早于申请提交日','error');
          }
 
          if(this.campaign != null &&  this.campaign != ''){
            console.log('qqqqqqqqqqqqqq');
            var dataList = new Array();
            selectCampaignById({recordId: this.campaign}).then(data =>{
              console.log('iiiiiiiiiiii');
              dataList = data;
              var interval = dataList[0].Status;
              if (interval == '草案中') {
                this.showToast('学会状态为草案中,不能提交','error');
              }
              if (interval == '申请中') {
              this.showToast('学会状态为申请中,不能提','error');
              }
              if (interval == '已结束') {
              this.showToast('学会状态为已结束,不能提交','error');
              }
              if (interval == '已提交报告') {
              this.showToast('学会状态为已提交报告,不能提交','error');
              }
              if (interval == '取消申请中') {
              this.showToast('学会状态为取消申请中,不能提交','error');
              }
              if (interval == '取消') {
              this.showToast('学会状态为取消,不能提交','error');
              }
            })
          }
          console.log('9888888888');
          if(this.qISNumber != null && this.qISNumber != ''){
            var resList = new Array();
            selectReportById({recordId:this.qISIDLine}).then(res=>{
              resList = res;
              var interval = resList[0].next_action__c;
              if (interval == '送回') {
                this.showToast('QIS 已送回,不能再申请备品了','error');
                }
            })
          }
 
          console.log('wwwwwwwwwww');
          var userAccess = new Array();
          userAccess.push(this.applyUser+'_Edit');
          userAccess.push(this.jingliManager+'_Read');
          userAccess.push(this.salesManager+'_Read');
          userAccess.push(this.buchangManager+'_Read');
          userAccess.push(this.buchangManagerSale+'_Read');
          userAccess.push(this.zongjianManager+'_Read');
            
          console.log('yyyyyyyyyy');
          setSObjectShare({applyUser:this.applyUser,
                          parentId:this.Id,
                          userAccess:userAccess,
                          ownerId:this.owner}).then(res=>{
          if(res!='OK'){
            this.showToast(rtn,'error');
          }
          })
          // var rtn = setSObjectShare(Consum_Apply__Share,this.applyUser,this.Id,userAccess,this.owner);
          // if(rtn != 'OK'){
          //   this.showToast(rtn,'error');
          // }
 
            //没有this.status == '引当完了'状态
          if(this.status == '填写完毕' || this.status == '申请中' || this.status == '已批准'
            || this.status == '已出库指示' || this.status == '删除' || this.status == '取消'){
            this.showToast('请备品申请状态确认,已经提交过的申请,不能重复提交','error');
          }
          console.log('eeeeeeeeee');
          if(this.minApprovalDate > day){
            this.showToast('申请提交日到预计使用日必须在22个工作日以内才可以','error');
          }
          var raesList = new Array();
 
          console.log('ttttttttt');
          selectEquipmentById({recordId:this.id}).then(raes=>{
            raesList = raes;
            if(raesList.length > 0){
              this.showToast('有没有明细的耗材备品配套一览,不能提交','error');
            }
          })
 
          console.log('99999999999'+this.id);
          approvalCheck({recordId:this.id}).then(re1=>{
            if(re1 != '1'){
              this.showToast(re1,'error');
            }
          })
 
          updateConsumApply({recordId:this.id}).then(re1=>{
          if(re1 != '1'){
            this.showToast(re1,'error');
          }else{
            this.showToast('已提交','success');
          }
          })
        }else{
          console.log('444444444444');
          this.IsLoading = false;
          this.showToast('result为空','error')
        }
 
      })
        
    }
 
  
 
 
 
updateRecordView() {
        updateRecord({fields: { Id: this.recordId }});
    }
 
    showToast(msg,type) {
        const event = new ShowToastEvent({
            message: msg,
            variant: type
        });
        if(type == 'success'){
            this.updateRecordView();
        }
        this.dispatchEvent(event);
        this.dispatchEvent(new CloseActionScreenEvent());
    }
}