001    /*
002     * Copyright 2011 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.kfs.module.ar.web.struts;
017    
018    import java.util.Collection;
019    
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpServletResponse;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.apache.struts.action.ActionForm;
025    import org.apache.struts.action.ActionForward;
026    import org.apache.struts.action.ActionMapping;
027    import org.kuali.kfs.module.ar.ArKeyConstants;
028    import org.kuali.kfs.module.ar.ArPropertyConstants;
029    import org.kuali.kfs.module.ar.batch.service.CustomerInvoiceWriteoffBatchService;
030    import org.kuali.kfs.module.ar.businessobject.CustomerInvoiceWriteoffLookupResult;
031    import org.kuali.kfs.module.ar.businessobject.lookup.CustomerInvoiceWriteoffLookupUtil;
032    import org.kuali.kfs.module.ar.document.CustomerInvoiceDocument;
033    import org.kuali.kfs.module.ar.document.service.CustomerInvoiceWriteoffDocumentService;
034    import org.kuali.kfs.sys.KFSConstants;
035    import org.kuali.kfs.sys.context.SpringContext;
036    import org.kuali.rice.kim.bo.Person;
037    import org.kuali.rice.kns.util.GlobalVariables;
038    import org.kuali.rice.kns.web.struts.action.KualiAction;
039    
040    public class CustomerInvoiceWriteoffLookupSummaryAction extends KualiAction {
041    
042        public ActionForward viewSummary(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
043    
044            CustomerInvoiceWriteoffLookupSummaryForm customerInvoiceWriteoffLookupSummaryForm = (CustomerInvoiceWriteoffLookupSummaryForm) form;
045            String lookupResultsSequenceNumber = customerInvoiceWriteoffLookupSummaryForm.getLookupResultsSequenceNumber();
046            if (StringUtils.isNotBlank(lookupResultsSequenceNumber)) {
047                String personId = GlobalVariables.getUserSession().getPerson().getPrincipalId();
048                Collection<CustomerInvoiceWriteoffLookupResult> customerInvoiceWriteoffLookupResults = CustomerInvoiceWriteoffLookupUtil.getCustomerInvoiceWriteoffResutlsFromLookupResultsSequenceNumber(lookupResultsSequenceNumber,personId);
049                customerInvoiceWriteoffLookupSummaryForm.setCustomerInvoiceWriteoffLookupResults(customerInvoiceWriteoffLookupResults);
050            }
051            return mapping.findForward(KFSConstants.MAPPING_BASIC);
052        }
053    
054        public ActionForward createCustomerInvoiceWriteoffs(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
055            
056            CustomerInvoiceWriteoffLookupSummaryForm customerInvoiceWriteoffLookupSummaryForm = (CustomerInvoiceWriteoffLookupSummaryForm) form;        
057    
058            Person person = GlobalVariables.getUserSession().getPerson();
059    
060            CustomerInvoiceWriteoffDocumentService service = SpringContext.getBean(CustomerInvoiceWriteoffDocumentService.class);
061            Collection<CustomerInvoiceWriteoffLookupResult> lookupResults = customerInvoiceWriteoffLookupSummaryForm.getCustomerInvoiceWriteoffLookupResults();
062    
063            //TODO Need to check every invoiceNumber submitted and make sure that:
064            //      1. Invoice exists in the system.
065            //      2. Invoice doesnt already have a writeoff in progress, either in route or final.
066            
067            //  make sure no null/blank invoiceNumbers get sent
068            boolean anyFound = false;
069            boolean customerNoteMissingOrInvalid = false;
070            int ind = 0;
071            String customerNote;
072            for( CustomerInvoiceWriteoffLookupResult customerInvoiceWriteoffLookupResult : lookupResults ){
073                customerNote = customerInvoiceWriteoffLookupResult.getCustomerNote();
074                if (StringUtils.isEmpty(customerNote)) {
075                    GlobalVariables.getMessageMap().putError(KFSConstants.CUSTOMER_INVOICE_WRITEOFF_LOOKUP_RESULT_ERRORS + "[" + ind +"]." + ArPropertyConstants.CustomerInvoiceWriteoffLookupResultFields.CUSTOMER_NOTE, ArKeyConstants.ERROR_CUSTOMER_INVOICE_WRITEOFF_CUSTOMER_NOTE_REQUIRED);
076                    customerNoteMissingOrInvalid = true;
077                } else if (customerNote.trim().length() < 5) {
078                    GlobalVariables.getMessageMap().putError(KFSConstants.CUSTOMER_INVOICE_WRITEOFF_LOOKUP_RESULT_ERRORS + "[" + ind +"]." + ArPropertyConstants.CustomerInvoiceWriteoffLookupResultFields.CUSTOMER_NOTE, ArKeyConstants.ERROR_CUSTOMER_INVOICE_WRITEOFF_CUSTOMER_NOTE_INVALID);
079                    customerNoteMissingOrInvalid = true;
080                }
081    
082                for (CustomerInvoiceDocument invoiceDocument : customerInvoiceWriteoffLookupResult.getCustomerInvoiceDocuments()) {
083                    if (StringUtils.isNotBlank(invoiceDocument.getDocumentNumber())) {
084                        anyFound = true;
085                    }
086                }
087                ind++;
088            }
089            
090            if (customerNoteMissingOrInvalid || !anyFound) {
091                // only submit this if there's at least one invoiceNumber in the stack
092                if (!anyFound)
093                    GlobalVariables.getMessageMap().putError(KFSConstants.GLOBAL_ERRORS, ArKeyConstants.ERROR_CUSTOMER_INVOICE_WRITEOFF_NO_INVOICES_SELECTED);
094                return mapping.findForward(KFSConstants.MAPPING_BASIC);
095            }
096            
097            //  send the batch file off
098            String filename = service.sendCustomerInvoiceWriteoffDocumentsToBatch(person, lookupResults);
099            
100            //  manually fire off the batch job
101            SpringContext.getBean(CustomerInvoiceWriteoffBatchService.class).loadFiles(); 
102            
103            customerInvoiceWriteoffLookupSummaryForm.setSentToBatch(true);
104            
105            GlobalVariables.getMessageList().add(ArKeyConstants.ERROR_CUSTOMER_INVOICE_WRITEOFF_BATCH_SENT);
106            return mapping.findForward(KFSConstants.MAPPING_BASIC);
107        }
108        
109        public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
110            return mapping.findForward(KFSConstants.MAPPING_CANCEL);
111        }        
112    }
113