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.businessobject;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.kfs.module.purap.document.AccountsPayableDocumentBase;
020    import org.kuali.kfs.module.purap.document.LineItemReceivingDocument;
021    import org.kuali.kfs.module.purap.document.PurchaseOrderDocument;
022    import org.kuali.kfs.module.purap.exception.PurError;
023    import org.kuali.rice.kns.util.KualiDecimal;
024    import org.kuali.rice.kns.util.ObjectUtils;
025    
026    /**
027     * @author Kuali Nervous System Team (kualidev@oncourse.iu.edu)
028     */
029    public class LineItemReceivingItem extends ReceivingItemBase {
030    
031        private KualiDecimal itemOrderedQuantity;
032    
033        // not stored in db
034        private KualiDecimal itemReceivedPriorQuantity;
035        private KualiDecimal itemReceivedToBeQuantity;
036    
037        private LineItemReceivingDocument lineItemReceivingDocument;
038    
039        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountsPayableDocumentBase.class);
040    
041        /**
042         * Default constructor.
043         */
044        public LineItemReceivingItem() {
045        }
046    
047        public LineItemReceivingItem(LineItemReceivingDocument rld) {
048            this.setDocumentNumber(rld.getDocumentNumber());
049            this.setItemReceivedTotalQuantity(KualiDecimal.ZERO);
050            this.setItemReturnedTotalQuantity(KualiDecimal.ZERO);
051            this.setItemDamagedTotalQuantity(KualiDecimal.ZERO);
052            this.setItemOriginalReceivedTotalQuantity(KualiDecimal.ZERO);
053            this.setItemOriginalReturnedTotalQuantity(KualiDecimal.ZERO);
054            this.setItemOriginalDamagedTotalQuantity(KualiDecimal.ZERO);
055        }
056    
057        public LineItemReceivingItem(PurchaseOrderItem poi, LineItemReceivingDocument rld) {
058    
059            this.setDocumentNumber(rld.getDocumentNumber());
060            this.setItemTypeCode(poi.getItemTypeCode());
061    
062            this.setItemLineNumber(poi.getItemLineNumber());
063            this.setItemCatalogNumber(poi.getItemCatalogNumber());
064            this.setItemDescription(poi.getItemDescription());
065    
066            this.setItemOrderedQuantity(poi.getItemQuantity());
067            this.setItemUnitOfMeasureCode(poi.getItemUnitOfMeasureCode());
068    
069            // TODO: Chris - look into this it appears this is null rather than zero on amendment, find out why!
070            if (ObjectUtils.isNull(poi.getItemReceivedTotalQuantity())) {
071                this.setItemReceivedPriorQuantity(KualiDecimal.ZERO);
072            }
073            else {
074                this.setItemReceivedPriorQuantity(poi.getItemReceivedTotalQuantity());
075            }
076    
077            this.setItemReceivedToBeQuantity(this.getItemOrderedQuantity().subtract(this.getItemReceivedPriorQuantity()));
078    
079            // should determine whether this is prefilled be based on the parameter that allows loading from po
080            this.setItemReceivedTotalQuantity(KualiDecimal.ZERO);
081    
082            this.setItemReturnedTotalQuantity(KualiDecimal.ZERO);
083            this.setItemDamagedTotalQuantity(KualiDecimal.ZERO);
084    
085            this.setItemOriginalReceivedTotalQuantity(KualiDecimal.ZERO);
086            this.setItemOriginalReturnedTotalQuantity(KualiDecimal.ZERO);
087            this.setItemOriginalDamagedTotalQuantity(KualiDecimal.ZERO);
088    
089            // not added
090            this.setItemReasonAddedCode(null);
091        }
092    
093        /**
094         * Retreives a purchase order item by inspecting the item type to see if its above the line or below the line and returns the
095         * appropriate type.
096         * 
097         * @return - purchase order item
098         */
099        public PurchaseOrderItem getPurchaseOrderItem() {
100            if (ObjectUtils.isNotNull(this.getLineItemReceivingDocument())) {
101                if (ObjectUtils.isNull(this.getLineItemReceivingDocument())) {
102                    this.refreshReferenceObject("lineItemReceivingDocument");
103                }
104            }
105            // ideally we should do this a different way - maybe move it all into the service or save this info somehow (make sure and
106            // update though)
107            if (getLineItemReceivingDocument() != null) {
108                PurchaseOrderDocument po = getLineItemReceivingDocument().getPurchaseOrderDocument();
109                PurchaseOrderItem poi = null;
110                if (this.getItemType().isLineItemIndicator()) {
111                    poi = (PurchaseOrderItem) po.getItem(this.getItemLineNumber().intValue() - 1);
112                    // throw error if line numbers don't match
113                }
114                if (poi != null) {
115                    return poi;
116                }
117                else {
118                    // LOG.debug("getPurchaseOrderItem() Returning null because PurchaseOrderItem object for line number" +
119                    // getItemLineNumber() + "or itemType " + getItemTypeCode() + " is null");
120                    return null;
121                }
122            }
123            else {
124                LOG.error("getPurchaseOrderItem() Returning null because paymentRequest object is null");
125                throw new PurError("Receiving Line Object in Purchase Order item line number " + getItemLineNumber() + "or itemType " + getItemTypeCode() + " is null");
126            }
127        }
128    
129        /**
130         * Gets the itemOrderedQuantity attribute.
131         * 
132         * @return Returns the itemOrderedQuantity
133         */
134        public KualiDecimal getItemOrderedQuantity() {
135            return itemOrderedQuantity;
136        }
137    
138        /**
139         * Sets the itemOrderedQuantity attribute.
140         * 
141         * @param itemOrderedQuantity The itemOrderedQuantity to set.
142         */
143        public void setItemOrderedQuantity(KualiDecimal itemOrderedQuantity) {
144            this.itemOrderedQuantity = itemOrderedQuantity;
145        }
146    
147        /**
148         * Gets the LineItemReceivingDocument attribute.
149         * 
150         * @return Returns the LineItemReceivingDocument.
151         */
152        public LineItemReceivingDocument getLineItemReceivingDocument() {
153            return lineItemReceivingDocument;
154        }
155    
156        /**
157         * Sets the LineItemReceivingDocument attribute value.
158         * 
159         * @param LineItemReceivingDocument The LineItemReceivingDocument to set.
160         * @deprecated
161         */
162        public void setLineItemReceivingDocument(LineItemReceivingDocument lineItemReceivingDocument) {
163            this.lineItemReceivingDocument = lineItemReceivingDocument;
164        }
165    
166        public KualiDecimal getItemReceivedPriorQuantity() {
167            if (ObjectUtils.isNull(itemReceivedPriorQuantity)) {
168                setItemReceivedPriorQuantity(getPurchaseOrderItem().getItemReceivedTotalQuantity());
169            }
170            return itemReceivedPriorQuantity;
171        }
172    
173        public void setItemReceivedPriorQuantity(KualiDecimal itemReceivedPriorQuantity) {
174    
175            this.itemReceivedPriorQuantity = itemReceivedPriorQuantity;
176        }
177    
178        public KualiDecimal getItemReceivedToBeQuantity() {
179            // lazy loaded
180            KualiDecimal toBeQuantity = this.getItemOrderedQuantity().subtract(getItemReceivedPriorQuantity());
181            if (toBeQuantity.isNegative()) {
182                toBeQuantity = KualiDecimal.ZERO;
183            }
184            setItemReceivedToBeQuantity(toBeQuantity);
185    
186            return itemReceivedToBeQuantity;
187        }
188    
189        public void setItemReceivedToBeQuantity(KualiDecimal itemReceivedToBeQuantity) {
190            this.itemReceivedToBeQuantity = itemReceivedToBeQuantity;
191        }
192    
193        public boolean isOrderedItem() {
194            return StringUtils.isEmpty(getItemReasonAddedCode());
195        }
196    
197    }