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.document.web.struts;
017    
018    import java.util.List;
019    import java.util.Map;
020    import java.util.Set;
021    
022    import javax.servlet.http.HttpServletRequest;
023    
024    import org.kuali.kfs.module.ar.ArAuthorizationConstants;
025    import org.kuali.kfs.module.ar.ArPropertyConstants;
026    import org.kuali.kfs.module.ar.businessobject.CustomerInvoiceDetail;
027    import org.kuali.kfs.module.ar.businessobject.CustomerInvoiceItemCode;
028    import org.kuali.kfs.module.ar.document.CustomerInvoiceDocument;
029    import org.kuali.kfs.module.ar.document.authorization.CustomerInvoiceDocumentPresentationController;
030    import org.kuali.kfs.module.ar.document.service.CustomerInvoiceDetailService;
031    import org.kuali.kfs.module.ar.document.service.CustomerInvoiceDocumentService;
032    import org.kuali.kfs.sys.KFSConstants;
033    import org.kuali.kfs.sys.KFSPropertyConstants;
034    import org.kuali.kfs.sys.businessobject.SourceAccountingLine;
035    import org.kuali.kfs.sys.businessobject.UnitOfMeasure;
036    import org.kuali.kfs.sys.context.SpringContext;
037    import org.kuali.kfs.sys.document.AccountingDocument;
038    import org.kuali.kfs.sys.web.struts.KualiAccountingDocumentFormBase;
039    import org.kuali.rice.kns.exception.InfrastructureException;
040    import org.kuali.rice.kns.service.DocumentHelperService;
041    import org.kuali.rice.kns.service.KualiConfigurationService;
042    import org.kuali.rice.kns.util.KNSConstants;
043    import org.kuali.rice.kns.web.format.CurrencyFormatter;
044    import org.kuali.rice.kns.web.ui.ExtraButton;
045    import org.kuali.rice.kns.web.ui.HeaderField;
046    import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
047    
048    public class CustomerInvoiceDocumentForm extends KualiAccountingDocumentFormBase {
049    
050        protected transient KualiConfigurationService configService;
051    
052        protected CustomerInvoiceDetail newCustomerInvoiceDetail;
053    
054        /**
055         * Constructs a CustomerInvoiceDocumentForm.java. Also sets new customer invoice document detail to a newly constructed customer
056         * invoice detail.
057         */
058        public CustomerInvoiceDocumentForm() {
059            super();
060        }
061    
062        @Override
063        protected String getDefaultDocumentTypeName() {
064            return "INV";
065        }
066        
067        /**
068         * 
069         * This method...
070         * @return
071         */
072        public CustomerInvoiceDocument getCustomerInvoiceDocument() {
073            return (CustomerInvoiceDocument) getDocument();
074        }
075    
076        /**
077         * @see org.kuali.kfs.sys.web.struts.KualiAccountingDocumentFormBase#populate(javax.servlet.http.HttpServletRequest)
078         */
079        @Override
080        public void populate(HttpServletRequest request) {
081            super.populate(request);
082            SpringContext.getBean(CustomerInvoiceDocumentService.class).loadCustomerAddressesForCustomerInvoiceDocument(getCustomerInvoiceDocument());
083        }
084    
085        /**
086         * Reused to create new source accounting line (i.e customer invoice detail line) with defaulted values.
087         * 
088         * @see org.kuali.kfs.sys.web.struts.KualiAccountingDocumentFormBase#createNewSourceAccountingLine(org.kuali.kfs.sys.document.AccountingDocument)
089         */
090        @Override
091        protected SourceAccountingLine createNewSourceAccountingLine(AccountingDocument financialDocument) {
092            if (financialDocument == null) {
093                throw new IllegalArgumentException("invalid (null) document");
094            }
095            try {
096                return SpringContext.getBean(CustomerInvoiceDetailService.class).getCustomerInvoiceDetailFromOrganizationAccountingDefaultForCurrentYear();
097            }
098            catch (Exception e) {
099                throw new InfrastructureException("Unable to create a new customer invoice document accounting line", e);
100            }
101        }
102    
103        /**
104         * By overriding this method, we can add the invoice total and open amount to the document header.
105         * 
106         * @see org.kuali.rice.kns.web.struts.form.KualiForm#getDocInfo()
107         */
108        @Override
109        public void populateHeaderFields(KualiWorkflowDocument workflowDocument) {
110            super.populateHeaderFields(workflowDocument);
111            getDocInfo().add(new HeaderField("DataDictionary.CustomerInvoiceDocument.attributes.sourceTotal", (String) new CurrencyFormatter().format(getCustomerInvoiceDocument().getSourceTotal())));
112            getDocInfo().add(new HeaderField("DataDictionary.CustomerInvoiceDocument.attributes.openAmount", (String) new CurrencyFormatter().format(getCustomerInvoiceDocument().getOpenAmount())));
113        }
114    
115        /**
116         * Configure lookup for Invoice Item Code source accounting line
117         * 
118         * @see org.kuali.kfs.sys.web.struts.KualiAccountingDocumentFormBase#getForcedLookupOptionalFields()
119         */
120        public Map getForcedLookupOptionalFields() {
121            Map forcedLookupOptionalFields = super.getForcedLookupOptionalFields();
122    
123            forcedLookupOptionalFields.put(ArPropertyConstants.CustomerInvoiceDocumentFields.INVOICE_ITEM_CODE, ArPropertyConstants.CustomerInvoiceDocumentFields.INVOICE_ITEM_CODE + ";" + CustomerInvoiceItemCode.class.getName());
124            forcedLookupOptionalFields.put(ArPropertyConstants.CustomerInvoiceDocumentFields.UNIT_OF_MEASURE_CODE, ArPropertyConstants.CustomerInvoiceDocumentFields.UNIT_OF_MEASURE_CODE + ";" + UnitOfMeasure.class.getName());
125    
126            return forcedLookupOptionalFields;
127        }
128    
129        /**
130         * Make amount and sales tax read only
131         * 
132         * @see org.kuali.rice.kns.web.struts.form.KualiTransactionalDocumentFormBase#getForcedReadOnlyFields()
133         */
134        public Map getForcedReadOnlyFields() {
135            Map map = super.getForcedReadOnlyFields();
136            map.put(KFSPropertyConstants.AMOUNT, Boolean.TRUE);
137            map.put("invoiceItemTaxAmount", Boolean.TRUE);
138            map.put("openAmount", Boolean.TRUE);
139            return map;
140        }
141        
142        /**
143         * Build additional customer invoice specific buttons and set extraButtons list.
144         * 
145         * @return - list of extra buttons to be displayed to the user
146         */
147        @Override
148        public List<ExtraButton> getExtraButtons() {
149            
150            // clear out the extra buttons array
151            extraButtons.clear();
152    
153            //  get the edit modes from the preso controller
154            CustomerInvoiceDocument invoiceDocument = (CustomerInvoiceDocument) getDocument();
155            DocumentHelperService docHelperService = SpringContext.getBean(DocumentHelperService.class);
156            CustomerInvoiceDocumentPresentationController presoController = 
157                    (CustomerInvoiceDocumentPresentationController) docHelperService.getDocumentPresentationController(invoiceDocument);
158            Set<String> editModes = presoController.getEditModes(invoiceDocument);
159            
160            //  draw the Print File button if appropriate 
161            if (editModes.contains(ArAuthorizationConstants.CustomerInvoiceDocumentEditMode.DISPLAY_PRINT_BUTTON)) {
162                String printButtonURL = getConfigService().getPropertyString(KFSConstants.EXTERNALIZABLE_IMAGES_URL_KEY);
163                addExtraButton("methodToCall.print", printButtonURL + "buttonsmall_genprintfile.gif", "Print");
164            }
165    
166            //  draw the Error Correction button if appropriate
167            if (presoController.canErrorCorrect(invoiceDocument)) {
168                String printButtonURL = getConfigService().getPropertyString(KFSConstants.EXTERNALIZABLE_IMAGES_URL_KEY);
169                addExtraButton("methodToCall.correct", printButtonURL + "buttonsmall_correction.gif", "Correct");
170            }
171            
172            return extraButtons;
173        }
174            
175        /**
176         * Adds a new button to the extra buttons collection.
177         * 
178         * @param property - property for button
179         * @param source - location of image
180         * @param altText - alternate text for button if images don't appear
181         */
182        protected void addExtraButton(String property, String source, String altText) {
183    
184            ExtraButton newButton = new ExtraButton();
185    
186            newButton.setExtraButtonProperty(property);
187            newButton.setExtraButtonSource(source);
188            newButton.setExtraButtonAltText(altText);
189    
190            extraButtons.add(newButton);
191        }
192    
193        protected KualiConfigurationService getConfigService() {
194            if (configService == null) {
195                configService = SpringContext.getBean(KualiConfigurationService.class);
196            }
197            return configService;
198        }
199    
200        /**
201         * @see org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase#shouldMethodToCallParameterBeUsed(java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest)
202         */
203        @Override
204        public boolean shouldMethodToCallParameterBeUsed(String methodToCallParameterName, String methodToCallParameterValue, HttpServletRequest request) {
205            if (KNSConstants.DISPATCH_REQUEST_PARAMETER.equals(methodToCallParameterName) && "printInvoicePDF".equals(methodToCallParameterValue)) {
206                return true;
207            }
208            return super.shouldMethodToCallParameterBeUsed(methodToCallParameterName, methodToCallParameterValue, request);
209        }
210    }