1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.earncode.service;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.hr.earncodesec.EarnCodeSecurity;
20 import org.kuali.hr.job.Job;
21 import org.kuali.hr.time.assignment.Assignment;
22 import org.kuali.hr.time.earncode.EarnCode;
23 import org.kuali.hr.time.earncode.dao.EarnCodeDao;
24 import org.kuali.hr.time.roles.TkUserRoles;
25 import org.kuali.hr.time.service.base.TkServiceLocator;
26 import org.kuali.hr.time.util.TKContext;
27 import org.kuali.hr.time.util.TKUser;
28 import org.kuali.hr.time.workarea.WorkArea;
29 import org.kuali.rice.krad.util.GlobalVariables;
30
31 import java.sql.Date;
32 import java.util.*;
33
34 public class EarnCodeServiceImpl implements EarnCodeService {
35
36 private EarnCodeDao earnCodeDao;
37
38 public void setEarnCodeDao(EarnCodeDao earnCodeDao) {
39 this.earnCodeDao = earnCodeDao;
40 }
41
42
43 @Override
44 public List<EarnCode> getEarnCodes(Assignment a, Date asOfDate) {
45 return getEarnCodes(a, asOfDate, null);
46 }
47
48 @Override
49 public List<EarnCode> getEarnCodes(Assignment a, Date asOfDate, String earnTypeCode) {
50 List<EarnCode> earnCodes = new LinkedList<EarnCode>();
51
52
53
54
55
56 TKUser user = TKContext.getUser();
57 if (user == null) {
58
59 throw new RuntimeException("No User on context.");
60 }
61
62 if (a == null)
63 throw new RuntimeException("Can not get earn codes for null assignment");
64 Job job = a.getJob();
65 if (job == null || job.getPayTypeObj() == null)
66 throw new RuntimeException("Null job/job paytype on assignment!");
67
68 EarnCode regularEc = getEarnCode(job.getPayTypeObj().getRegEarnCode(), asOfDate);
69 if (regularEc == null)
70 throw new RuntimeException("No regular earn code defined.");
71 earnCodes.add(regularEc);
72 List<EarnCodeSecurity> decs = TkServiceLocator.getEarnCodeSecurityService().getEarnCodeSecurities(job.getDept(), job.getHrSalGroup(), job.getLocation(), asOfDate);
73 for (EarnCodeSecurity dec : decs) {
74 if (StringUtils.isBlank(earnTypeCode)
75 || earnTypeCode.equals(dec.getEarnCodeType())
76 ) {
77
78 boolean addEarnCode = false;
79
80 if (dec.isEmployee() &&
81 (StringUtils.equals(TKUser.getCurrentTargetPerson().getEmployeeId(), GlobalVariables.getUserSession().getPerson().getEmployeeId()))) {
82 addEarnCode = true;
83 }
84
85 if (!addEarnCode && dec.isApprover()) {
86 Set<Long> workAreas = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getApproverWorkAreas();
87 for (Long wa : workAreas) {
88 WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(wa, asOfDate);
89 if (workArea!= null && a.getWorkArea().compareTo(workArea.getWorkArea())==0) {
90 addEarnCode = true;
91 break;
92 }
93 }
94 }
95 if (addEarnCode) {
96 EarnCode ec = getEarnCode(dec.getEarnCode(), asOfDate);
97 if(ec!=null){
98 earnCodes.add(ec);
99 }
100 }
101 }
102 }
103
104 return earnCodes;
105 }
106
107 public EarnCode getEarnCode(String earnCode, Date asOfDate) {
108 EarnCode ec = null;
109
110 ec = earnCodeDao.getEarnCode(earnCode, asOfDate);
111
112 return ec;
113 }
114
115 @Override
116 public String getEarnCodeType(String earnCode, Date asOfDate) {
117 EarnCode earnCodeObj = getEarnCode(earnCode, asOfDate);
118 return earnCodeObj.getEarnCodeType();
119 }
120
121 @Override
122 public EarnCode getEarnCodeById(String earnCodeId) {
123 return earnCodeDao.getEarnCodeById(earnCodeId);
124 }
125
126 public List<EarnCode> getOvertimeEarnCodes(Date asOfDate){
127 return earnCodeDao.getOvertimeEarnCodes(asOfDate);
128 }
129
130 public List<String> getOvertimeEarnCodesStrs(Date asOfDate){
131 List<String> ovtEarnCodeStrs = new ArrayList<String>();
132 List<EarnCode> ovtEarnCodes = getOvertimeEarnCodes(asOfDate);
133 if(ovtEarnCodes != null){
134 for(EarnCode ovtEc : ovtEarnCodes){
135 ovtEarnCodeStrs.add(ovtEc.getEarnCode());
136 }
137 }
138 return ovtEarnCodeStrs;
139 }
140
141 @Override
142 public int getEarnCodeCount(String earnCode) {
143 return earnCodeDao.getEarnCodeCount(earnCode);
144 }
145
146 @Override
147 public int getNewerEarnCodeCount(String earnCode, Date effdt) {
148 return earnCodeDao.getNewerEarnCodeCount(earnCode, effdt);
149 }
150
151
152 public List<EarnCode> getEarnCodesForTime(Assignment a, Date asOfDate) {
153 if (a == null) throw new RuntimeException("No assignment parameter.");
154 Job job = a.getJob();
155 if (job == null || job.getPayTypeObj() == null) throw new RuntimeException("Null job or null job pay type on assignment.");
156
157 List<EarnCode> earnCodes = new LinkedList<EarnCode>();
158
159 EarnCode regularEarnCode = getEarnCode(job.getPayTypeObj().getRegEarnCode(), asOfDate);
160 if (regularEarnCode == null) {
161 throw new RuntimeException("No regular earn code defined for job pay type.");
162 } else {
163 earnCodes.add(regularEarnCode);
164 }
165 List<EarnCodeSecurity> decs = TkServiceLocator.getEarnCodeSecurityService().getEarnCodeSecurities(job.getDept(), job.getHrSalGroup(), job.getLocation(), asOfDate);
166 for (EarnCodeSecurity dec : decs) {
167 boolean addEarnCode = false;
168
169 if (dec.isEmployee() &&
170 (StringUtils.equals(TKUser.getCurrentTargetPerson().getEmployeeId(), GlobalVariables.getUserSession().getPerson().getEmployeeId()))) {
171 addEarnCode = true;
172 }
173
174 if (!addEarnCode && dec.isApprover()) {
175 Set<Long> workAreas = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getApproverWorkAreas();
176 for (Long wa : workAreas) {
177 WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(wa, asOfDate);
178 if (workArea!= null && a.getWorkArea().compareTo(workArea.getWorkArea())==0) {
179 addEarnCode = true;
180 break;
181 }
182 }
183 }
184 if (addEarnCode) {
185 EarnCode ec = getEarnCode(dec.getEarnCode(), asOfDate);
186 if(ec!=null){
187 earnCodes.add(ec);
188 }
189 }
190 }
191
192
193 return earnCodes;
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 }