001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time.timesheet.web;
017    
018    import java.math.BigDecimal;
019    import java.sql.Date;
020    import java.util.ArrayList;
021    import java.util.HashMap;
022    import java.util.HashSet;
023    import java.util.List;
024    import java.util.Map;
025    import java.util.Map.Entry;
026    import java.util.Set;
027    
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.http.HttpServletResponse;
030    
031    import org.apache.commons.lang.StringUtils;
032    import org.apache.commons.lang.time.DateUtils;
033    import org.apache.log4j.Logger;
034    import org.apache.struts.action.ActionForm;
035    import org.apache.struts.action.ActionForward;
036    import org.apache.struts.action.ActionMapping;
037    import org.apache.struts.action.ActionRedirect;
038    import org.kuali.hr.lm.LMConstants;
039    import org.kuali.hr.lm.accrual.AccrualCategory;
040    import org.kuali.hr.lm.accrual.AccrualCategoryRule;
041    import org.kuali.hr.lm.balancetransfer.BalanceTransfer;
042    import org.kuali.hr.lm.leaveSummary.LeaveSummary;
043    import org.kuali.hr.lm.leaveSummary.LeaveSummaryRow;
044    import org.kuali.hr.lm.leavecalendar.validation.LeaveCalendarValidationUtil;
045    import org.kuali.hr.lm.leavepayout.LeavePayout;
046    import org.kuali.hr.time.base.web.TkAction;
047    import org.kuali.hr.time.calendar.Calendar;
048    import org.kuali.hr.time.calendar.CalendarEntries;
049    import org.kuali.hr.time.detail.web.ActionFormUtils;
050    import org.kuali.hr.time.principal.PrincipalHRAttributes;
051    import org.kuali.hr.time.roles.TkUserRoles;
052    import org.kuali.hr.time.roles.UserRoles;
053    import org.kuali.hr.time.service.base.TkServiceLocator;
054    import org.kuali.hr.time.timesheet.TimesheetDocument;
055    import org.kuali.hr.time.timesummary.EarnCodeSection;
056    import org.kuali.hr.time.timesummary.EarnGroupSection;
057    import org.kuali.hr.time.util.TKContext;
058    import org.kuali.hr.time.util.TKUser;
059    import org.kuali.hr.time.util.TKUtils;
060    import org.kuali.hr.time.util.TkConstants;
061    import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
062    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
063    import org.kuali.rice.krad.exception.AuthorizationException;
064    import org.kuali.rice.krad.util.GlobalVariables;
065    import org.kuali.rice.krad.util.ObjectUtils;
066    
067    public class TimesheetAction extends TkAction {
068    
069            private static final Logger LOG = Logger.getLogger(TimesheetAction.class);
070    
071        @Override
072        protected void checkTKAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
073            UserRoles roles = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId());
074            TimesheetDocument doc = TKContext.getCurrentTimesheetDocument();
075    
076            if (!roles.isDocumentReadable(doc)) {
077                throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalId(), "TimesheetAction: docid: " + (doc == null ? "" : doc.getDocumentId()), "");
078            }
079        }
080    
081        @Override
082            public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
083                    TimesheetActionForm taForm = (TimesheetActionForm) form;
084                    String documentId = taForm.getDocumentId();
085    
086            if (StringUtils.equals(request.getParameter("command"), "displayDocSearchView")
087                            || StringUtils.equals(request.getParameter("command"), "displayActionListView") ) {
088                    documentId = (String) request.getParameter("docId");
089            }
090    
091            LOG.debug("DOCID: " + documentId);
092    
093            // Here - viewPrincipal will be the principal of the user we intend to
094            // view, be it target user, backdoor or otherwise.
095            String viewPrincipal = TKUser.getCurrentTargetPerson().getPrincipalId();
096            Date currentDate = TKUtils.getTimelessDate(null);
097                    CalendarEntries payCalendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(viewPrincipal, currentDate);
098    
099            // 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    }