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.purap.document.web.struts;
017    
018    import java.util.HashMap;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.kuali.kfs.module.purap.PurapAuthorizationConstants;
023    import org.kuali.kfs.module.purap.PurapConstants;
024    import org.kuali.kfs.module.purap.PurapParameterConstants;
025    import org.kuali.kfs.module.purap.businessobject.LineItemReceivingItem;
026    import org.kuali.kfs.module.purap.document.CorrectionReceivingDocument;
027    import org.kuali.kfs.module.purap.document.LineItemReceivingDocument;
028    import org.kuali.kfs.module.purap.document.service.ReceivingService;
029    import org.kuali.kfs.sys.KFSConstants;
030    import org.kuali.kfs.sys.context.SpringContext;
031    import org.kuali.rice.kim.bo.Person;
032    import org.kuali.rice.kns.document.authorization.DocumentAuthorizer;
033    import org.kuali.rice.kns.service.DataDictionaryService;
034    import org.kuali.rice.kns.service.DocumentHelperService;
035    import org.kuali.rice.kns.service.ParameterService;
036    import org.kuali.rice.kns.util.GlobalVariables;
037    import org.kuali.rice.kns.util.ObjectUtils;
038    import org.kuali.rice.kns.web.ui.ExtraButton;
039    import org.kuali.rice.kns.web.ui.HeaderField;
040    import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
041    
042    public class LineItemReceivingForm extends ReceivingFormBase {
043        
044        protected Integer purchaseOrderId;
045        protected LineItemReceivingItem newLineItemReceivingItemLine;
046        protected boolean fromPurchaseOrder = false;
047        
048        /**
049         * Constructs a LineItemReceivingForm instance and sets up the appropriately casted document.
050         */
051        public LineItemReceivingForm() {
052            super();
053    
054            this.setNewLineItemReceivingItemLine(setupNewLineItemReceivingItemLine());
055            newLineItemReceivingItemLine.setItemTypeCode(PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE);
056        }
057    
058        @Override
059        protected String getDefaultDocumentTypeName() {
060            return "RCVL";
061        }
062        
063        public LineItemReceivingDocument getLineItemReceivingDocument() {
064            return (LineItemReceivingDocument) getDocument();
065        }
066    
067        public void setLineItemReceivingDocument(LineItemReceivingDocument lineItemReceivingDocument) {
068            setDocument(lineItemReceivingDocument);
069        }
070    
071        public Integer getPurchaseOrderId() {
072            return purchaseOrderId;
073        }
074    
075        public void setPurchaseOrderId(Integer purchaseOrderId) {
076            this.purchaseOrderId = purchaseOrderId;
077        }
078    
079        public LineItemReceivingItem setupNewLineItemReceivingItemLine() {
080            return new LineItemReceivingItem();
081        }
082    
083        public LineItemReceivingItem getNewLineItemReceivingItemLine() {
084            return newLineItemReceivingItemLine;
085        }
086    
087        public void setNewLineItemReceivingItemLine(LineItemReceivingItem newLineItemReceivingItemLine) {
088            this.newLineItemReceivingItemLine = newLineItemReceivingItemLine;
089        }
090    
091        @Override
092        public void populateHeaderFields(KualiWorkflowDocument workflowDocument) {
093            super.populateHeaderFields(workflowDocument);
094            //leave the first field blank to match the other PURAP docs
095            getDocInfo().add(new HeaderField());
096            
097            if (ObjectUtils.isNotNull(this.getLineItemReceivingDocument().getLineItemReceivingStatus())) {
098                getDocInfo().add(new HeaderField("DataDictionary.LineItemReceivingDocument.attributes.lineItemReceivingStatusCode", this.getLineItemReceivingDocument().getLineItemReceivingStatus().getLineItemReceivingStatusDescription()));
099            }
100            else {
101                getDocInfo().add(new HeaderField("DataDictionary.LineItemReceivingDocument.attributes.lineItemReceivingStatusCode", "Not Available"));
102            }
103        }
104    
105        /**
106         * Override the superclass method to add appropriate buttons for LineItemReceivingDocument.
107         * 
108         * @see org.kuali.rice.kns.web.struts.form.KualiForm#getExtraButtons()
109         */
110        @Override
111        public List<ExtraButton> getExtraButtons() {
112            extraButtons.clear();
113            Map buttonsMap = createButtonsMap();
114            
115            String displayInitTab = (String) getEditingMode().get(PurapAuthorizationConstants.LineItemReceivingEditMode.DISPLAY_INIT_TAB);
116            if (ObjectUtils.isNotNull(displayInitTab) && displayInitTab.equalsIgnoreCase("true")) {
117                extraButtons.add((ExtraButton) buttonsMap.get("methodToCall.continueReceivingLine"));
118                extraButtons.add((ExtraButton) buttonsMap.get("methodToCall.clearInitFields"));
119            }
120            else {
121                if (canCreateCorrection()) {
122                    extraButtons.add((ExtraButton) buttonsMap.get("methodToCall.createReceivingCorrection"));
123                }
124            }
125            
126            return extraButtons;
127        }        
128    
129        protected boolean canCreateCorrection() {
130            Person user = GlobalVariables.getUserSession().getPerson();
131            DocumentAuthorizer documentAuthorizer = SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(KFSConstants.FinancialDocumentTypeCodes.CORRECTION_RECEIVING);
132            boolean isUserAuthorized = documentAuthorizer.canInitiate(KFSConstants.FinancialDocumentTypeCodes.CORRECTION_RECEIVING, user);
133            return SpringContext.getBean(ReceivingService.class).canCreateCorrectionReceivingDocument(getLineItemReceivingDocument()) && isUserAuthorized;
134        }
135    
136        /**
137         * Creates a MAP for all the buttons to appear on the Receiving Line Form, and sets the attributes of these buttons.
138         * 
139         * @return the button map created.
140         */
141        protected Map<String, ExtraButton> createButtonsMap() {
142            HashMap<String, ExtraButton> result = new HashMap<String, ExtraButton>();
143    
144            // Continue button
145            ExtraButton continueButton = new ExtraButton();
146            continueButton.setExtraButtonProperty("methodToCall.continueReceivingLine");
147            continueButton.setExtraButtonSource("${" + KFSConstants.RICE_EXTERNALIZABLE_IMAGES_URL_KEY + "}buttonsmall_continue.gif");
148            continueButton.setExtraButtonAltText("Continue");
149            result.put(continueButton.getExtraButtonProperty(), continueButton);
150            
151            // Clear button
152            ExtraButton clearButton = new ExtraButton();
153            clearButton.setExtraButtonProperty("methodToCall.clearInitFields");
154            clearButton.setExtraButtonSource("${" + KFSConstants.RICE_EXTERNALIZABLE_IMAGES_URL_KEY + "}buttonsmall_clear.gif");
155            clearButton.setExtraButtonAltText("Clear");
156            result.put(clearButton.getExtraButtonProperty(), clearButton);
157            
158            // Correction button
159            ExtraButton correctionButton = new ExtraButton();
160            correctionButton.setExtraButtonProperty("methodToCall.createReceivingCorrection");
161            correctionButton.setExtraButtonSource("${" + KFSConstants.EXTERNALIZABLE_IMAGES_URL_KEY + "}buttonsmall_correction.gif");
162            correctionButton.setExtraButtonAltText("Correction");                
163            result.put(correctionButton.getExtraButtonProperty(), correctionButton);
164            
165            return result;
166        }
167        
168        /**
169         * Returns the new Receiving Item Line and resets it to null.
170         * 
171         * @return the new Receiving Item Line.
172         */
173        public LineItemReceivingItem getAndResetNewReceivingItemLine() {
174            LineItemReceivingItem receivingItemLine = getNewLineItemReceivingItemLine();
175            setNewLineItemReceivingItemLine(setupNewReceivingItemLine());
176            return receivingItemLine;
177        }
178    
179        /**
180         * This method should be overriden (or see accountingLines for an alternate way of doing this with newInstance)
181         */
182        public LineItemReceivingItem setupNewReceivingItemLine() {
183            LineItemReceivingItem lineItemReceivingItem = new LineItemReceivingItem((LineItemReceivingDocument)getDocument());
184            newLineItemReceivingItemLine.setItemTypeCode(PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE);
185            return lineItemReceivingItem;
186        }
187        
188        /**
189         * Indicates if the clear and load quantity buttons can be shown, according to the
190         * value of a system parameter.
191         *  
192         * @return
193         */
194        public boolean isAbleToShowClearAndLoadQtyButtons(){        
195            return SpringContext.getBean(ParameterService.class).getIndicatorParameter(LineItemReceivingDocument.class, PurapParameterConstants.SHOW_CLEAR_AND_LOAD_QTY_BUTTONS);        
196        }
197    
198        public boolean isFromPurchaseOrder() {
199            return fromPurchaseOrder;
200        }
201    
202        public void setFromPurchaseOrder(boolean fromPurchaseOrder) {
203            this.fromPurchaseOrder = fromPurchaseOrder;
204        }
205    
206    }