liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
public without sharing class otherButtonMaintenanceContractCtl {
    @AuraEnabled
    public static InitData init(String recordId){
        InitData res = new initData();
        String reportId = '';
        List<Report> reportTmpt = [select Id,DeveloperName from Report where DeveloperName = 'SLAReportDetail'];
        if(reportTmpt.size()>0) {
            reportId = reportTmpt[0].Id;
        }   
        try{
             RecordType re = [select id  from RecordType where name = '服务合同'];
             res.newMcRecordType = re.Id;
            Maintenance_Contract__c report =  [SELECT 
            Hospital__c,Department_Class__c,Department__c,Service_Contract_Staff__c,
 
            Service_contract_target_number__c,
            PB_Contrant_Cnt__c,
            PA_Contrant_Cnt__c,
            Contract_Conclusion_Date__c,
            Contract_Start_Date__c,
            Estimate_Trial_Money__c,
            Contract_End_Date__c,
            Receipt_Date__c,
            Recycling_Predetermined_Day__c,
            Contract_Amount__c,
            Collect_Date__c,
            Recoverable_Amount__c,
            Not_collect_money__c,
            Sum_repair_price__c,
            invoice_amount__c,
            Total_not_collect_amount__c,
            Description__c,
            OCM_Print_request__c,
            HP_Dealer_print__c,
            // Contract_print_completed__c,
            Submit_contract_to_Financial_dept__c,
            Received_Date__c,
            Repair_Cost_Sum__c,
            // Maintenance_Contract_No__c,
            Last_year_service_contract__c,
            UserType__c,
             MaintanceContractPack__c,Decided_Estimation__c,Estimation_Id__c,URF_Contract__c,RecordTypeId,Is_RecognitionModel__c,Status__c,MC_approval_status__c,Payment_Plan_Sum_First__c,
            Contract_quotation_or_not__c,Name,notRenew__c,Contract_print_completed__c,Maintenance_Contract_No__c,
            upload_to_sap_time__c,old_Is_RecognitionModel__c,upload_to_RM_time__c,Is_Recognition_Model_True__c,
            Id,URF_Contract_F__c,RecordType_DeveloperName__c,Estimate_Target__c,AccDealerBlacklist__c FROM Maintenance_Contract__c WHERE Id =: recordId LIMIT 1];
            System.debug(LoggingLevel.INFO, '*** opp: ' + report);
            res.EstimationIdIdC = report.Estimation_Id__c;
            res.DecidedEstimationC = report.Decided_Estimation__c;
            res.StatusC = report.Status__c;
            res.IsRecognitionModelC = report.Is_RecognitionModel__c;
            res.RecordTypeId = report.RecordTypeId;
            res.URFContractC = report.URF_Contract__c;
            res.MCApprovalStatusC = report.MC_approval_status__c;
            res.MaintenanceContractNoC = report.Maintenance_Contract_No__c;
            res.uploadToSapTimeC = report.upload_to_sap_time__c;
            res.oldIsRecognitionModelC = report.old_Is_RecognitionModel__c;
            res.uploadToRMTimeC = report.upload_to_RM_time__c;
            res.IsRecognitionModelTrueC = report.Is_Recognition_Model_True__c;
            res.Id = report.Id;
            res.URFContractFC = report.URF_Contract_F__c;
            res.RecordTypeDeveloperNameC = report.RecordType_DeveloperName__c;
            res.EstimateTargetC = report.Estimate_Target__c;
            res.ContractprintCompletedC = report.Contract_print_completed__c;
            res.notRenewC = report.notRenew__c;
            res.Name = report.Name;
            res.ContractQuotationOrNotC = report.Contract_quotation_or_not__c;
            res.PaymentPlanSumFirstC = report.Payment_Plan_Sum_First__c;
            res.reportId = reportId;
            //zzm 230816 报价合并 start --打包合同下的小合同只能去打包合同上进行合同复核
            res.MaintanceContractPackC = report.MaintanceContractPack__c;
            //zzm 230816 报价合并 end --打包合同下的小合同只能去打包合同上进行合同复核
            //贸易合规 zzm 230829 start
            res.accDealerBlacklist = report.AccDealerBlacklist__c;
            //贸易合规 zzm 230829 end
 
            res.hospital  = report.Hospital__c;
            res.department_Class  = report.Department_Class__c;
            res.department  = report.Department__c;
            res.service_Contract_Staff  = report.Service_Contract_Staff__c;
 
            res.Service_contract_target_number = report.Service_contract_target_number__c;
            res.PB_Contrant_Cnt = report.PB_Contrant_Cnt__c;
            res.PA_Contrant_Cnt = report.PA_Contrant_Cnt__c;
            res.Contract_Conclusion_Date = report.Contract_Conclusion_Date__c;
            res.Contract_Start_Date = report.Contract_Start_Date__c;
            res.Estimate_Trial_Money = report.Estimate_Trial_Money__c;
            res.Contract_End_Date = report.Contract_End_Date__c;
            res.Receipt_Date = report.Receipt_Date__c;
            res.Recycling_Predetermined_Day = report.Recycling_Predetermined_Day__c;
            res.Contract_Amount = report.Contract_Amount__c;
            res.Collect_Date = report.Collect_Date__c;
            res.Recoverable_Amount = report.Recoverable_Amount__c;
            res.Not_collect_money = report.Not_collect_money__c;
            res.Sum_repair_price = report.Sum_repair_price__c;
            res.invoice_amount = report.invoice_amount__c;
            res.Total_not_collect_amount = report.Total_not_collect_amount__c;
            res.Description = report.Description__c;
            res.OCM_Print_request = report.OCM_Print_request__c;
            res.HP_Dealer_print = report.HP_Dealer_print__c;
            res.Contract_print_completed = report.Contract_print_completed__c;
            res.Submit_contract_to_Financial_dept = report.Submit_contract_to_Financial_dept__c;
            res.Received_Date = report.Received_Date__c;
            res.Repair_Cost_Sum = report.Repair_Cost_Sum__c;
            res.Maintenance_Contract_No = report.Maintenance_Contract_No__c;
            res.Last_year_service_contract = report.Last_year_service_contract__c;
            res.UserType = report.UserType__c;
 
            //20231120 sx add  贸易合规二期 start
            // res.hosTradeComplianceStatus = report.Hospital__r.TradeComplianceStatus__c;
            // res.dealTradeComplianceStatus = report.Dealer__r.TradeComplianceStatus__c;
            List<Maintenance_Contract_Asset__c> mcaListtemp = [SELECT Id, Asset__r.product2.CountryOfOrigin__c, Asset__r.product2.USRatio_US__c, Asset__r.product2.ProTradeComplianceStatus__c,Asset__r.product2.Asset_Model_No__c FROM Maintenance_Contract_Asset__c WHERE Maintenance_Contract__c = :recordId];
            String violationName = '';
            if(mcaListtemp.size() > 0){
                for(Maintenance_Contract_Asset__c mac : mcaListtemp){
                    if(mac.Asset__r.product2.ProTradeComplianceStatus__c == '0'){
                        res.proTradeComplianceStatus = '0';
                        violationName += mac.Asset__r.product2.Asset_Model_No__c+',';
                    }
                }
            }else{
                res.proTradeComplianceStatus = '1';
            }
            res.violationName = violationName;
            //20231120 sx add  贸易合规二期 end
            System.debug(LoggingLevel.INFO, '*** res: ' + res);
        }catch(Exception e){
            System.debug(LoggingLevel.INFO, '*** e: ' + e);
        }
        return res;
    } 
    @AuraEnabled    
    public static String updateFirstContract(String mcid ){
         List<Maintenance_Contract__c> mcList = [select id, Effective_Status__c,Status__c,upload_to_sap_time__c from Maintenance_Contract__c where id = :mcid ];
         if (mcList.size() == 0) {
            return '维修合同不存在,请确认维修合同ID。';
        }
       
        try{
         Maintenance_Contract__c mc = mcList[0];
         List<Maintenance_Contract_Asset__c> mcaList = [select id,Maintenance_Contract__r.Contract_Start_Date__c,Maintenance_Contract__r.Contract_End_Date__c,Maintenance_Contract__r.Maintenance_Contract_No__c,Asset__r.name,Asset__r.id,asset__r.First_Service_number__c,asset__r.First_Service_Start_Day__c,asset__r.First_Service_End_Day__c 
                                                          from Maintenance_Contract_Asset__c where Maintenance_Contract__c = :mc.id];
         List<Asset> assList = new List<Asset>();
         if (mc.Effective_Status__c =='有效' && mc.Status__c =='契約'&&mc.upload_to_sap_time__c==null && mcaList!= null) {
              for (Maintenance_Contract_Asset__c mca:mcaList) {
                   Asset ass = new Asset();
                   ass.id = mca.asset__r.id;
                 if (mca.asset__r.First_Service_number__c == null) {
                    ass.First_Service_number__c = mca.Maintenance_Contract__r.Maintenance_Contract_No__c;
                 }
                 if (mca.asset__r.First_Service_End_Day__c  == null) {
                    ass.First_Service_End_Day__c = mca.Maintenance_Contract__r.Contract_End_Date__c;
                 }
                 if (mca.asset__r.First_Service_Start_Day__c == null) {
                    ass.First_Service_Start_Day__c = mca.Maintenance_Contract__r.Contract_Start_Date__c;
                 }
                 assList.add(ass);
              }
         }
              if (assList.size()>0 && !Test.isRunningTest()) {
                update assList;
             }
        }catch(Exception e){
         return e.getMessage();
            // return e.getDmlMessage(0);
 
        }
        return '1';
     }
 
     // 维修合同失单报告
    @AuraEnabled
    public static List<Lost_Report__c> selectRecords(String mcId){
        List<Lost_Report__c> res = new List<Lost_Report__c>();
        try{
            res = [SELECT Id,Status__c,Other_Reasons__c,Other__c,Third_Party_Company__c,Third_Party_Contract_Price__c,To_Where__c,Specific_Reasons__c,Maintenance_Contract__c from Lost_Report__c where Maintenance_Contract__c =: mcId ];
        }catch(Exception e){
            System.debug(LoggingLevel.INFO, '*** e: ' + e);
        }
        return res;
    }
 
    // 查找维修合同报价
    @AuraEnabled
    public static List<Maintenance_Contract_Estimate__c> selectMaintenanceContractEstimate(String recordId){
        List<Maintenance_Contract_Estimate__c> res = new List<Maintenance_Contract_Estimate__c>();
        try{
            res = [SELECT Id  from Maintenance_Contract_Estimate__c where Maintenance_Contract__c=:recordId];
        }catch(Exception e){
            System.debug(LoggingLevel.INFO, '*** e: ' + e);
        }
        return res;
    }
 
    // 工作流状态
    @AuraEnabled
    public static String processResults(String recordId){
        try{
            Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
            psr.setobjectid(recordId);
            Approval.ProcessResult submitResult = Approval.process(psr);
            return '1';
 
        } catch(Exception e) {
            return e.getMessage();
            // return e.getDmlMessage(0);
        }
        
    }
    //1.要求: 将当前合同信息更新到当前合同下的`上一期维修合同`中的`来年合同`中
 
    ///实现
    ///1.获取当前合同信息 1).记录类型 2)保有设备
    ///2.获取当前合同下的保有设备下的`上一期维修合同`信息   条件1`空白期小于等于365天,条件2`上一期维修合同不为空
    ///3.设置该`上一期维修合同`的`来年合同`为当前合同  条件1` 如果当前合同的记录类型为`服务合同`,则更新`上一期维修合同`中的来年服务合同字段,(维修合同同上
 
 
    //用户点击上传SAP按钮,更新'来年服务合同'/'来年维修合同'
    
    @AuraEnabled
    public static String updateColunm(String mcid ){
        //当前维修合同
        Maintenance_Contract__c newMcc = [select id,name,RecordTypeId,Contract_Start_Date__c from Maintenance_Contract__c where Id = :mcid];
 
        //1. 获取当前合同的记录类型
        RecordType rt = [select Name from RecordType where id = :newMcc.RecordTypeId];
 
        //1. 获取当前合同下的所有符合更改条件的保有设备
        List<Maintenance_Contract_Asset__c> temp = [
        select Id,LastMContract__c,LastMContract_Gap__c from Maintenance_Contract_Asset__c 
        where Maintenance_Contract__c = :mcid
        ];
 
        if (temp.size() == 0) {
            return '1';
        }
 
        //1. 获取保有设备下的合同信息
        List<String> lastMcIds = new List<String>();
        for(Maintenance_Contract_Asset__c mcac : temp){
            if (mcac.LastMContract__c != null) {
                if (mcac.LastMContract_Gap__c <= 365) {
                    lastMcIds.add(mcac.LastMContract__c);   
                }   
            }
        }
 
        List<Maintenance_Contract__c> lastMcc = 
        [select id,name,RecordTypeId,Contract_Start_Date__c,serviceContractNextYear_new__c,contractNextYear_new__c from Maintenance_Contract__c where Id in :lastMcIds];
        
        Map<Id,Maintenance_Contract__c> mmap = new Map<Id,Maintenance_Contract__c>();
 
        if (rt.Name.equals('服务合同')) {
            for(Maintenance_Contract__c mcc : lastMcc){
                Maintenance_Contract__c mc = new Maintenance_Contract__c();
                mc.Id = mcc.Id;
                mc.serviceContractNextYear_new__c = mcid;
                mmap.put(mcc.Id,mc);
            }
        }else if (rt.Name.equals('维修合同')) {
            for(Maintenance_Contract__c mcc : lastMcc){
                Maintenance_Contract__c mc = new Maintenance_Contract__c();
                mc.Id = mcc.Id;
                mc.serviceContractNextYear_new__c = mcid;
                mc.contractNextYear_new__c = mcid;
                mmap.put(mcc.Id,mc);
            }
        }
 
        try {
            if (!Test.isRunningTest()) {
                update mmap.values();
            }
            
        }
        catch (Exception e) {
            return E.getMessage();
            // return e.getDmlMessage(0);
        }
 
        return '1';
    }
   /* @AuraEnabled
    public static String initCustomNewCopy2(String recordId){
        String s='';
        try {
            String objectName = 'Maintenance_Contract__c'; // 要获取字段的对象名
            Map<String, Schema.SObjectType> globalDescribe = Schema.getGlobalDescribe();
            Schema.SObjectType objType = globalDescribe.get(objectName);
            if (objType != null) {
                Schema.DescribeSObjectResult objDescribe = objType.getDescribe();
                Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap();
                s+='SELECT ';
                // 现在,fieldMap中包含了对象的所有字段信息
                for (String fieldName : fieldMap.keySet()) {
                    if(!fieldName.equals('id')
                        &&!fieldName.equals('Id'))
                        s+=fieldName+',';
                }
 
                s=s.removeEnd(',');
                s+=' FROM Maintenance_Contract__c where id=\''+recordId+'\'';
                system.debug('SQL:'+s);
                List<Maintenance_Contract__c> opportunitys = Database.query(s);
                s='';
                if(opportunitys.size()>0){
                    system.debug('in!');
                    for (String fieldName : fieldMap.keySet()) {
                        String formaF=fieldMap.get(fieldName).getDescribe().getName();
                        if(opportunitys.get(0).get(fieldName)!=null&&!opportunitys.get(0).get(fieldName).equals('null')){
                            if(formaF.equals('Id')
                                ||formaF.equals('OwnerId')
                                ||formaF.equals('CreatedDate')
                                ||formaF.equals('CreatedById')
                                )
                            {
                                continue;
                            }
                            Object val=opportunitys.get(0).get(fieldName);
                            if(val instanceof Date ){
                                String str=String.valueOf(val);
                                str=str.replace(' ','T');
                                str+='.000Z';
                                s+=formaF+'='+str+',';
                            }else if (val instanceof DateTime){
                                String str=String.valueOf(val);
                                str=str.replace(' ','T');
                                str+='.000Z';
                                s+=formaF+'='+str+',';
                            }else{
                                s+=formaF+'='+opportunitys.get(0).get(fieldName)+',';
                            }
                            
                        }
                    }
                    s=s.removeEnd(',');
                    return s;
                }
            }
            return s;
        } catch (Exception e) {
            System.debug('lexCopyToBaseController init error:'+e.getMessage());
        }
        return s;
    }*/
 
    public class InitData{
        @AuraEnabled
       public String DecidedEstimationC;
       @AuraEnabled
       public String EstimationIdIdC;
       @AuraEnabled
       public String MCApprovalStatusC;
       @AuraEnabled
       public String MaintenanceContractNoC;
       @AuraEnabled
       public Datetime uploadToSapTimeC;
       @AuraEnabled
       public Boolean oldIsRecognitionModelC;
       @AuraEnabled
       public Datetime uploadToRMTimeC;
       @AuraEnabled
       public Boolean IsRecognitionModelTrueC;
       @AuraEnabled
       public String Id;
       @AuraEnabled
       public String URFContractFC;
       @AuraEnabled
       public String RecordTypeDeveloperNameC;
       @AuraEnabled
       public String EstimateTargetC;
       @AuraEnabled
       public Datetime ContractprintCompletedC;  
       @AuraEnabled
       public Boolean notRenewC;   
       @AuraEnabled
       public String Name;  
       @AuraEnabled
       public String ContractQuotationOrNotC;   
       @AuraEnabled
       public Double PaymentPlanSumFirstC;   
       @AuraEnabled
       public String StatusC;   
       @AuraEnabled
       public Boolean IsRecognitionModelC;   
       @AuraEnabled
       public String RecordTypeId;   
       @AuraEnabled
       public Boolean URFContractC;  
       @AuraEnabled
       public ID RecordTypeId1; 
       @AuraEnabled
       public String reportId;
       @AuraEnabled
       public String MaintanceContractPackC; 
       @AuraEnabled
       public String accDealerBlacklist;
       @AuraEnabled
        public Id hospital; 
        @AuraEnabled
        public Id department_Class; 
        @AuraEnabled
        public Id department; 
        @AuraEnabled
        public Id service_Contract_Staff; 
        @AuraEnabled
        public String newMcRecordType;
 
 
        @AuraEnabled public Decimal Service_contract_target_number;
        @AuraEnabled public Decimal  PB_Contrant_Cnt;
        @AuraEnabled public Decimal  PA_Contrant_Cnt;
        @AuraEnabled public Date  Contract_Conclusion_Date;
        @AuraEnabled public Date  Contract_Start_Date;
        @AuraEnabled public Decimal  Estimate_Trial_Money;
        @AuraEnabled public Date  Contract_End_Date;
        @AuraEnabled public Date  Receipt_Date;
        @AuraEnabled public Date  Recycling_Predetermined_Day;
        @AuraEnabled public Decimal  Contract_Amount;
        @AuraEnabled public Date  Collect_Date;
        @AuraEnabled public Decimal  Recoverable_Amount;
        @AuraEnabled public Decimal  Not_collect_money;
        @AuraEnabled public Decimal  Sum_repair_price;
        @AuraEnabled public Decimal  invoice_amount;
        @AuraEnabled public Decimal  Total_not_collect_amount;
        @AuraEnabled public String  Description;
        @AuraEnabled public Date  OCM_Print_request;
        @AuraEnabled public Date  HP_Dealer_print;
        @AuraEnabled public Date  Contract_print_completed;
        @AuraEnabled public Date  Submit_contract_to_Financial_dept;
        @AuraEnabled public Date  Received_Date;
        @AuraEnabled public Decimal  Repair_Cost_Sum;
        @AuraEnabled public String  Maintenance_Contract_No;
        @AuraEnabled public String  Last_year_service_contract;
        @AuraEnabled public String  UserType;
 
        @AuraEnabled public Boolean  agree_Upper_limit;
        @AuraEnabled public Boolean  URF_Contract;
 
        //20231211 sx 贸易合规二期add start
        @AuraEnabled public String proTradeComplianceStatus;
                @AuraEnabled public String hosTradeComplianceStatus;
        @AuraEnabled public String dealTradeComplianceStatus;
 
        @AuraEnabled public String violationName;
    } 
    
   
}