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
public without sharing class AccountToTMSInfoTrigger {
    public static void createTms(List<Account> newList, Map<Id, Account> newMap, List<Account> oldList, Map<Id, Account> oldMap) {
        if (Trigger.isInsert || Trigger.isUpdate) {
            Map<String,Dealer_ConsumableManage__c> dcList = new Map<String,Dealer_ConsumableManage__c>();
            Map<String,Dealer_ConsumableManage__c> dcList2 = new Map<String,Dealer_ConsumableManage__c>();
            List<String> keys = new List<String>();
            Integer year = Date.today().year();
            for (Account acc : newList) {
                keys.add(acc.Agent_Ref__c+'-'+acc.Contact_Type__c+'-'+'FY'+year+'-'+acc.OCM_man_province_HP__c);
            }
            
            List<Dealer_ConsumableManage__c> dcHas = [select id,TMS_Key__c from Dealer_ConsumableManage__c where TMS_Key__c in :keys];
            System.debug('dcHas='+dcHas);
            String myUserID = UserInfo.getUserId();
            User userinfo = [SELECT id,profileId FROM User WHERE id = :myUserID LIMIT 1];
            String userpid = userinfo.profileId;
            String OLYid = System.Label.OlympusSystem_ID;
            String ProfileId = System.Label.ProfileId_SystemAdmin;
            if ((myUserID.substring(0,15) == OLYid.substring(0,15) || (userpid == ProfileId))) {
                if (dcHas.size()==0) {
                    for (Account acc : newList) {
                        if (Trigger.isInsert || Trigger.isUpdate) {
                            if (acc.Acc_Record_Type__c == '契約' && acc.Is_Active_Formula__c == '有效' &&(acc.Contact_Type__c == 'ET' || acc.Contact_Type__c == 'ENG')) {
                                Dealer_ConsumableManage__c dc =  new Dealer_ConsumableManage__c();
                                Decimal amount = 0;
                                if (acc.Aim_Price__c != null) {
                                    amount = (acc.Aim_Price__c/12).setScale(0);
                                }
                                System.debug('acc.ET_SP_Dealer__c='+acc.ET_SP_Dealer__c);
                                if (acc.ET_SP_Dealer__c == true){
                                    dc.Category2__c = 'ET耗材';
                                    dc.ET_ENG__c = 'ET';
                                    dc.Dealer_Name__c = acc.Agent_Ref__c;
                                    dc.Dealer_Name_Account__c = acc.id;
                                    dc.FY__c = 'FY'+year;
                                    dc.FY_P_1__c = amount;
                                    dc.FY_P_10__c = amount;
                                    dc.FY_P_11__c = amount;
                                    dc.FY_P_12__c = amount;
                                    dc.FY_P_2__c = amount;
                                    dc.FY_P_3__c = amount;
                                    dc.FY_P_4__c = amount;
                                    dc.FY_P_5__c = amount;
                                    dc.FY_P_6__c = amount;
                                    dc.FY_P_7__c = amount;
                                    dc.FY_P_8__c = amount;
                                    dc.FY_P_9__c = amount;
                                    dc.TMS_Key__c = acc.Agent_Ref__c+'-'+'ET'+'-'+'FY'+year+'-'+acc.OCM_man_province_HP__c;
                                }else if (acc.ENG_Dealer__c == true){
                                    dc.Category2__c = '外科耗材';
                                    dc.ET_ENG__c = 'ENG';
                                    dc.Dealer_Name__c = acc.Agent_Ref__c;
                                    dc.Dealer_Name_Account__c = acc.id;
                                    dc.FY__c = 'FY'+year;
                                    dc.FY_P_1__c = amount;
                                    dc.FY_P_10__c = amount;
                                    dc.FY_P_11__c = amount;
                                    dc.FY_P_12__c = amount;
                                    dc.FY_P_2__c = amount;
                                    dc.FY_P_3__c = amount;
                                    dc.FY_P_4__c = amount;
                                    dc.FY_P_5__c = amount;
                                    dc.FY_P_6__c = amount;
                                    dc.FY_P_7__c = amount;
                                    dc.FY_P_8__c = amount;
                                    dc.FY_P_9__c = amount;
                                    dc.TMS_Key__c = acc.Agent_Ref__c+'-'+'ENG'+'-'+'FY'+year+'-'+acc.OCM_man_province_HP__c;
                                }
                                if (dc.TMS_Key__c !=null && !dcList.containskey(dc.id)) {
                                    dcList.put(dc.id,dc); 
                                }
                            } 
                        } 
                    }    
                }
                if (dcHas.size()!=0) {
                    for (Account acc : newList) {
                        if (acc.Acc_Record_Type__c == '契約' && acc.Is_Active_Formula__c == '有效'  &&(acc.Contact_Type__c == 'ET' || acc.Contact_Type__c == 'ENG')) {
                            String tmskey = acc.Agent_Ref__c+'-'+acc.Contact_Type__c+'-'+'FY'+year+'-'+acc.OCM_man_province_HP__c;
                            Decimal amount = 0;
                            if (acc.Aim_Price__c != null) {
                                amount = (acc.Aim_Price__c/12).setScale(0);
                            }
                            Dealer_ConsumableManage__c dc =  new Dealer_ConsumableManage__c();
                            if (Trigger.isInsert || Trigger.isUpdate) {
                                for (Dealer_ConsumableManage__c dcm : dcHas) {
                                    if (tmskey == dcm.TMS_Key__c) {
                                        dc.id = dcm.id;
                                        dc.FY_P_1__c = amount;
                                        dc.FY_P_10__c = amount;
                                        dc.FY_P_11__c = amount;
                                        dc.FY_P_12__c = amount;
                                        dc.FY_P_2__c = amount;
                                        dc.FY_P_3__c = amount;
                                        dc.FY_P_4__c = amount;
                                        dc.FY_P_5__c = amount;
                                        dc.FY_P_6__c = amount;
                                        dc.FY_P_7__c = amount;
                                        dc.FY_P_8__c = amount;
                                        dc.FY_P_9__c = amount;
                                        if (!dcList2.containsKey(dc.id)) {
                                            dcList2.put(dc.id,dc); 
                                        }
                                    }  
                                }
                            }
                        }
                    }
                }
            }
            
            system.debug('dcList=='+dcList.values());
            if (dcList.size()>0) {
                insert dcList.values();
            }
            if (dcList2.size()>0) {
                update dcList2.values();
            }
        }
    }
}