buli
2022-04-15 900c50a247705d6fe8833e84d31b5d51616b7c26
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
public with sharing class NewLoanerApplicationController {
    //客户ID
    public String accountID {get; private set;}
    //联系人id
    public String contactID {get; private set;}
 
    public loaner_application__c la{get; private set;}
 
    // Update 20220318 By Yang Kaiyu Start
    public loaner_user__c lu {get; private set;}
 
    public Contact con {get; private set;}
    // Update 20220318 By Yang Kaiyu End
 
    public String typeName {get; private set;}
 
    public String userType {get; private set;}
 
    public String baseUrl { get; set; }
    public String rtUrl { get; set; }
 
    // Update 20220318 By Yang Kaiyu Start
    public String staticResource {get; set;}
    public String staticResourceContact {get; set;}
    public String staticResourceLoanerUser {get; set;}
 
    public String laid {get; set;}
    // Update 20220318 By Yang Kaiyu End
 
    public NewLoanerApplicationController() {
        baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
        String path = URL.getCurrentRequestUrl().getPath();
        if (path.indexOf('/apex') > 0) {
            baseUrl += path.substring(0,path.indexOf('/apex'));
        } else if (path.indexOf('production/') > 0) {
            baseUrl += '/production';
        }
        rtUrl = System.currentPageReference().getParameters().get('retURL');
        if (rtUrl == null || rtUrl == 'null') {
            rtUrl = '';
        }
    }
 
    public PageReference init() {
        userType = UserInfo.getUserType();
        accountID = System.currentPageReference().getParameters().get('accid');
        contactID = System.currentPageReference().getParameters().get('conId');
        // Update 20220318 By Yang Kaiyu End
        staticResource = JSON.serialize(PIHelper.getPIIntegrationInfo('loaner_application__c')); 
        staticResourceContact = JSON.serialize(PIHelper.getPIIntegrationInfo('Contact')); 
        staticResourceLoanerUser = JSON.serialize(PIHelper.getPIIntegrationInfo('loaner_user__c')); 
        // Update 20220318 By Yang Kaiyu End
 
        Account acc = [select id,ProductSegment__c from Account where id = :accountID];
        typeName = acc.ProductSegment__c;
        Id recordTypeId =Schema.SObjectType.loaner_application__c.getRecordTypeInfosByName()
                  .get(typeName).getRecordTypeId();
        
        la = new loaner_application__c();
        // Update 20220318 By Yang Kaiyu End
        lu = new loaner_user__c();
        con = new Contact();
        // Update 20220318 By Yang Kaiyu End
        la.RecordTypeId= recordTypeId;
        if(typeName == 'BS' && userType != 'Standard'){
            la.RecordTypeId = System.label.bs_D_ID;
        }
        System.debug(la.RecordTypeId);
        //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,accountID));
        // Update 20220318 By Yang Kaiyu End
        con = [select id,AWS_Data_Id__c,Phone,Phone_Encrypted__c,Name,LastName_Encrypted__c,Address1__c from Contact where id = :contactID];
        // Update 20220318 By Yang Kaiyu End
        return null;
    }
 
    public PageReference saveBtn() {
        if(la.Request_shipping_Date__c < Date.today()){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '到货日不得早于今天'));
            return null;
        }
        if(la.Request_shipping_Date__c >= la.Request_return_Date__c){
             ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '归还日不得早于到货日'));
            return null;
        }
        Account acc = [select id,Name from Account where id = :accountID];
        la.Loaner_Account__c = acc.Name;
        la.ApplyPerson__c = UserInfo.getUserId();
         Savepoint sp = Database.setSavepoint();
        try {
            insert la;
            
            // loaner_user__c lu = new loaner_user__c();   // Update 20220412 By Chen Yanan
            lu.loaner_application__c = la.id;
            lu.Customer__c = accountID;
            // Update 20220318 By Yang Kaiyu Start
            // System.debug('lu------------'+lu);
             // if(contactID != null){
             //     lu.Contact__c = contactID;
             //     Contact contact = [select id,AWS_Data_Id__c,Phone,Phone_Encrypted__c,Name,LastName_Encrypted__c,Address1__c from Contact where id = :contactID];
             //     lu.ContactNumber__c = contact.Phone;
                 
             // }
            // Update 20220318 By Yang Kaiyu End
             
            insert lu;
            // Update 20220318 By Yang Kaiyu Start
            laid = la.id;
            // String url = baseUrl + '\\' + la.Id;
            // return new Pagereference(url);
            return null;
            // Update 20220318 By Yang Kaiyu End
        } catch (Exception e) {
            Database.rollback(sp);
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, e.getMessage()));
        }
 
        return null;
    }
 
        public PageReference cancelBtn() {
        // 返回样机借出申请画面
        String url = baseUrl;
        if(contactID == null){
            url += '\\' + accountID;
        }else{
            url += '\\' + contactID;
        }
        
        return new Pagereference(url);
        return null;
    }
}