View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.timesheet.web;
17  
18  import java.math.BigDecimal;
19  import java.sql.Date;
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Map.Entry;
26  import java.util.Set;
27  
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.apache.commons.lang.StringUtils;
32  import org.apache.commons.lang.time.DateUtils;
33  import org.apache.log4j.Logger;
34  import org.apache.struts.action.ActionForm;
35  import org.apache.struts.action.ActionForward;
36  import org.apache.struts.action.ActionMapping;
37  import org.apache.struts.action.ActionRedirect;
38  import org.kuali.hr.lm.LMConstants;
39  import org.kuali.hr.lm.accrual.AccrualCategory;
40  import org.kuali.hr.lm.accrual.AccrualCategoryRule;
41  import org.kuali.hr.lm.balancetransfer.BalanceTransfer;
42  import org.kuali.hr.lm.leaveSummary.LeaveSummary;
43  import org.kuali.hr.lm.leaveSummary.LeaveSummaryRow;
44  import org.kuali.hr.lm.leavecalendar.validation.LeaveCalendarValidationUtil;
45  import org.kuali.hr.lm.leavepayout.LeavePayout;
46  import org.kuali.hr.time.base.web.TkAction;
47  import org.kuali.hr.time.calendar.Calendar;
48  import org.kuali.hr.time.calendar.CalendarEntries;
49  import org.kuali.hr.time.detail.web.ActionFormUtils;
50  import org.kuali.hr.time.principal.PrincipalHRAttributes;
51  import org.kuali.hr.time.roles.TkUserRoles;
52  import org.kuali.hr.time.roles.UserRoles;
53  import org.kuali.hr.time.service.base.TkServiceLocator;
54  import org.kuali.hr.time.timesheet.TimesheetDocument;
55  import org.kuali.hr.time.timesummary.EarnCodeSection;
56  import org.kuali.hr.time.timesummary.EarnGroupSection;
57  import org.kuali.hr.time.util.TKContext;
58  import org.kuali.hr.time.util.TKUser;
59  import org.kuali.hr.time.util.TKUtils;
60  import org.kuali.hr.time.util.TkConstants;
61  import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
62  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
63  import org.kuali.rice.krad.exception.AuthorizationException;
64  import org.kuali.rice.krad.util.GlobalVariables;
65  import org.kuali.rice.krad.util.ObjectUtils;
66  
67  public class TimesheetAction extends TkAction {
68  
69  	private static final Logger LOG = Logger.getLogger(TimesheetAction.class);
70  
71      @Override
72      protected void checkTKAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
73          UserRoles roles = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId());
74          TimesheetDocument doc = TKContext.getCurrentTimesheetDocument();
75  
76          if (!roles.isDocumentReadable(doc)) {
77              throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalId(), "TimesheetAction: docid: " + (doc == null ? "" : doc.getDocumentId()), "");
78          }
79      }
80  
81      @Override
82  	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
83  		TimesheetActionForm taForm = (TimesheetActionForm) form;
84  		String documentId = taForm.getDocumentId();
85  
86          if (StringUtils.equals(request.getParameter("command"), "displayDocSearchView")
87          		|| StringUtils.equals(request.getParameter("command"), "displayActionListView") ) {
88          	documentId = (String) request.getParameter("docId");
89          }
90  
91          LOG.debug("DOCID: " + documentId);
92  
93          // Here - viewPrincipal will be the principal of the user we intend to
94          // view, be it target user, backdoor or otherwise.
95          String viewPrincipal = TKUser.getCurrentTargetPerson().getPrincipalId();
96          Date currentDate = TKUtils.getTimelessDate(null);
97  		CalendarEntries payCalendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(viewPrincipal, currentDate);
98  
99          // By handling the prev/next in the execute method, we are saving one
100         // fetch/construction of a TimesheetDocument. If it were broken out into
101         // methods, we would first fetch the current document, and then fetch
102         // the next one instead of doing it in the single action.
103 		TimesheetDocument td;
104         if (StringUtils.isNotBlank(documentId)) {
105             td = TkServiceLocator.getTimesheetService().getTimesheetDocument(documentId);
106         } else {
107             // Default to whatever is active for "today".
108             if (payCalendarEntry == null) {
109                 throw new RuntimeException("No pay calendar entry for " + viewPrincipal);
110             }
111             td = TkServiceLocator.getTimesheetService().openTimesheetDocument(viewPrincipal, payCalendarEntry);
112         }
113 
114         // Set the TKContext for the current timesheet document id.
115         if (td != null) {
116            setupDocumentOnFormContext(taForm, td);
117         } else {
118             LOG.error("Null timesheet document in TimesheetAction.");
119         }
120         
121         List<String> warnings = new ArrayList<String>();
122         Map<String, Set<String>> allMessages = new HashMap<String,Set<String>>();
123         allMessages.put("actionMessages", new HashSet<String>());
124         allMessages.put("warningMessages", new HashSet<String>());
125         allMessages.put("infoMessages", new HashSet<String>());
126         //placing the following "validation" further down in this method will overwrite messages added prior to this call.
127         //allMessages.putAll(LeaveCalendarValidationUtil.validatePendingTransactions(viewPrincipal, payCalendarEntry.getBeginPeriodDate(), payCalendarEntry.getEndPeriodDate()));
128         
129         // add warning messages based on max carry over balances for each accrual category for non-exempt leave users
130         List<BalanceTransfer> losses = new ArrayList<BalanceTransfer>();
131         if (TkServiceLocator.getLeaveApprovalService().isActiveAssignmentFoundOnJobFlsaStatus(viewPrincipal, TkConstants.FLSA_STATUS_NON_EXEMPT, true)) {
132         	PrincipalHRAttributes principalCalendar = null;
133         	if(ObjectUtils.isNotNull(payCalendarEntry)) {
134 	        	principalCalendar = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(viewPrincipal, payCalendarEntry.getEndPeriodDate());
135 	        	Map<String,ArrayList<String>> transfers = new HashMap<String,ArrayList<String>>();
136 	        	Map<String,ArrayList<String>> payouts = new HashMap<String,ArrayList<String>>();;
137 	        	if(ObjectUtils.isNotNull(principalCalendar)) {
138 	        		transfers = TkServiceLocator.getBalanceTransferService().getEligibleTransfers(td.getCalendarEntry(),td.getPrincipalId());
139 	        		payouts = TkServiceLocator.getLeavePayoutService().getEligiblePayouts(td.getCalendarEntry(),td.getPrincipalId());
140 	        	}
141 	        	
142 	        	for(Entry<String,ArrayList<String>> entry : transfers.entrySet()) {
143 	        		//contains max balance action = lose "transfers".
144 	        		if(!entry.getValue().isEmpty()) {
145 	        			for(String accrualRuleId : entry.getValue()) {
146 	        				AccrualCategoryRule aRule = TkServiceLocator.getAccrualCategoryRuleService().getAccrualCategoryRule(accrualRuleId);
147 	        				AccrualCategory aCat = TkServiceLocator.getAccrualCategoryService().getAccrualCategory(aRule.getLmAccrualCategoryId());
148 	        				allMessages.get("warningMessages").add("You have exceeded the maximum balance limit for '" + aCat.getAccrualCategory() + "'. " +
149 	                    			"Depending upon the accrual category rules, leave over this limit may be forfeited.");
150 	        			}
151 	        		}
152 	        	}
153 	        	for(Entry<String,ArrayList<String>> entry : payouts.entrySet()) {
154 	        		//contains only payouts.
155 	        		if(!entry.getValue().isEmpty()) {
156 	        			for(String accrualRuleId : entry.getValue()) {
157 	        				AccrualCategoryRule aRule = TkServiceLocator.getAccrualCategoryRuleService().getAccrualCategoryRule(accrualRuleId);
158 	        				AccrualCategory aCat = TkServiceLocator.getAccrualCategoryService().getAccrualCategory(aRule.getLmAccrualCategoryId());
159 	        				allMessages.get("warningMessages").add("You have exceeded the maximum balance limit for '" + aCat.getAccrualCategory() + "'. " +
160 	                    			"Depending upon the accrual category rules, leave over this limit may be forfeited.");
161 	        			}
162 	        		}
163 	        	}
164 	            LeaveSummary leaveSummary = TkServiceLocator.getLeaveSummaryService().getLeaveSummary(viewPrincipal, payCalendarEntry);
165 	            for(String accrualRuleId : transfers.get(LMConstants.MAX_BAL_ACTION_FREQ.LEAVE_APPROVE)) {
166 	            	AccrualCategoryRule aRule = TkServiceLocator.getAccrualCategoryRuleService().getAccrualCategoryRule(accrualRuleId);
167 	            	if(StringUtils.equals(aRule.getActionAtMaxBalance(),LMConstants.ACTION_AT_MAX_BAL.LOSE)) {
168 	    	        	BigDecimal accruedBalance = leaveSummary.getLeaveSummaryRowForAccrualCategory(aRule.getLmAccrualCategoryId()).getAccruedBalance();
169 	    	        	Date effectiveDate = TKUtils.getCurrentDate();
170 	    	        	if(TKUtils.getCurrentDate().after(payCalendarEntry.getEndPeriodDate()))
171 	    	        		effectiveDate = new Date(DateUtils.addDays(payCalendarEntry.getEndPeriodDate(),-1).getTime());
172 	    	        	BalanceTransfer loseTransfer = TkServiceLocator.getBalanceTransferService().initializeTransfer(viewPrincipal, accrualRuleId, accruedBalance, effectiveDate);
173 	    	        	losses.add(loseTransfer);
174 	            	}
175 	            }
176 	            for(String accrualRuleId : transfers.get(LMConstants.MAX_BAL_ACTION_FREQ.YEAR_END)) {
177 	            	AccrualCategoryRule aRule = TkServiceLocator.getAccrualCategoryRuleService().getAccrualCategoryRule(accrualRuleId);
178 	            	if(StringUtils.equals(aRule.getActionAtMaxBalance(),LMConstants.ACTION_AT_MAX_BAL.LOSE)) {
179 	    	        	BigDecimal accruedBalance = leaveSummary.getLeaveSummaryRowForAccrualCategory(aRule.getLmAccrualCategoryId()).getAccruedBalance();
180 	    	        	Date effectiveDate = TKUtils.getCurrentDate();
181 	    	        	if(TKUtils.getCurrentDate().after(payCalendarEntry.getEndPeriodDate()))
182 	    	        		effectiveDate = new Date(DateUtils.addDays(payCalendarEntry.getEndPeriodDate(),-1).getTime());
183 	    	        	BalanceTransfer loseTransfer = TkServiceLocator.getBalanceTransferService().initializeTransfer(viewPrincipal, accrualRuleId, accruedBalance, effectiveDate);
184 	    	        	losses.add(loseTransfer);
185 	            	}
186 	            }
187         	}
188             taForm.setForfeitures(losses);
189             
190         	if (principalCalendar != null) {
191 	        	Calendar calendar = TkServiceLocator.getCalendarService().getCalendarByPrincipalIdAndDate(viewPrincipal, taForm.getEndPeriodDateTime(), true);
192 					
193 				if (calendar != null) {
194 					List<CalendarEntries> leaveCalendarEntries = TkServiceLocator.getCalendarEntriesService().getCalendarEntriesEndingBetweenBeginAndEndDate(calendar.getHrCalendarId(), taForm.getBeginPeriodDateTime(), taForm.getEndPeriodDateTime());
195 					
196 					List<AccrualCategory> accrualCategories = TkServiceLocator.getAccrualCategoryService().getActiveLeaveAccrualCategoriesForLeavePlan(principalCalendar.getLeavePlan(), new java.sql.Date(taForm.getEndPeriodDateTime().getTime()));
197 					for (AccrualCategory accrualCategory : accrualCategories) {
198 						if (TkServiceLocator.getAccrualCategoryMaxCarryOverService().exceedsAccrualCategoryMaxCarryOver(accrualCategory.getAccrualCategory(), viewPrincipal, leaveCalendarEntries, taForm.getEndPeriodDateTime())) {
199 							String message = "Your pending leave balance is greater than the annual max carry over for accrual category '" + accrualCategory.getAccrualCategory() + "' and upon approval, the excess balance will be lost.";
200 							if (!warnings.contains(message)) {
201 								warnings.add(message);
202 							}
203 						}
204 					}
205 				}
206 			}
207         }
208 		warnings.addAll(allMessages.get("infoMessages"));
209 		warnings.addAll(allMessages.get("actionMessages"));
210 		warnings.addAll(allMessages.get("warningMessages"));
211 		taForm.setWarningMessages(warnings);
212 
213         // Do this at the end, so we load the document first,
214         // then check security permissions via the superclass execution chain.
215 		return super.execute(mapping, form, request, response);
216 	}
217 
218     public ActionForward docHandler(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
219         ActionForward forward = mapping.findForward("basic");
220     	String command = request.getParameter("command");
221     	
222     	if (StringUtils.equals(command, "displayDocSearchView") || StringUtils.equals(command, "displayActionListView")) {
223         	String docId = (String) request.getParameter("docId");
224         	TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(docId);
225         	String timesheetPrincipalName = KimApiServiceLocator.getPersonService().getPerson(timesheetDocument.getPrincipalId()).getPrincipalName();
226         	
227         	String principalId = TKUser.getCurrentTargetPerson().getPrincipalId();
228         	String principalName = KimApiServiceLocator.getPersonService().getPerson(principalId).getPrincipalName();
229         	
230         	StringBuilder builder = new StringBuilder();
231         	if (!StringUtils.equals(principalName, timesheetPrincipalName)) {
232             	if (StringUtils.equals(command, "displayDocSearchView")) {
233             		builder.append("changeTargetPerson.do?methodToCall=changeTargetPerson");
234             		builder.append("&documentId=");
235             		builder.append(docId);
236             		builder.append("&principalName=");
237             		builder.append(timesheetPrincipalName);
238             		builder.append("&targetUrl=TimeDetail.do");
239             		builder.append("?docmentId=" + docId);
240             		builder.append("&returnUrl=TimeApproval.do");
241             	} else {
242             		builder.append("TimeApproval.do");
243             	}
244         	} else {
245         		builder.append("TimeDetail.do");
246         		builder.append("?docmentId=" + docId);
247         	}
248 
249         	forward = new ActionRedirect(builder.toString());
250         }
251     	
252     	return forward;
253     }
254 
255     protected void setupDocumentOnFormContext(TimesheetActionForm taForm, TimesheetDocument td) throws Exception{
256     	String viewPrincipal = TKUser.getCurrentTargetPerson().getPrincipalId();
257     	TKContext.setCurrentTimesheetDocumentId(td.getDocumentId());
258         TKContext.setCurrentTimesheetDocument(td);
259 	    taForm.setTimesheetDocument(td);
260 	    taForm.setDocumentId(td.getDocumentId());
261         TimesheetDocumentHeader prevTdh = TkServiceLocator.getTimesheetDocumentHeaderService().getPrevOrNextDocumentHeader(TkConstants.PREV_TIMESHEET, viewPrincipal);
262         TimesheetDocumentHeader nextTdh = TkServiceLocator.getTimesheetDocumentHeaderService().getPrevOrNextDocumentHeader(TkConstants.NEXT_TIMESHEET, viewPrincipal);
263        
264         taForm.setPrevDocumentId(prevTdh != null ? prevTdh.getDocumentId() : null);
265         taForm.setNextDocumentId(nextTdh != null ? nextTdh.getDocumentId() : null);
266       
267         taForm.setPayCalendarDates(td.getCalendarEntry());
268         taForm.setOnCurrentPeriod(ActionFormUtils.getOnCurrentPeriodFlag(taForm.getPayCalendarDates()));
269         
270     }
271 
272 }