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