View Javadoc
1   /*
2    * Copyright 2008-2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.businessobject;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.module.purap.document.AccountsPayableDocumentBase;
20  import org.kuali.ole.module.purap.document.LineItemReceivingDocument;
21  import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
22  import org.kuali.ole.module.purap.exception.PurError;
23  import org.kuali.rice.core.api.util.type.KualiDecimal;
24  import org.kuali.rice.krad.util.ObjectUtils;
25  
26  import java.util.List;
27  
28  /**
29   * @author Kuali Nervous System Team (kualidev@oncourse.iu.edu)
30   */
31  public class LineItemReceivingItem extends ReceivingItemBase {
32  
33      private KualiDecimal itemOrderedQuantity;
34  
35      // not stored in db
36      private KualiDecimal itemReceivedPriorQuantity;
37      private KualiDecimal itemReceivedToBeQuantity;
38  
39      private LineItemReceivingDocument lineItemReceivingDocument;
40  
41      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountsPayableDocumentBase.class);
42  
43      /**
44       * Default constructor.
45       */
46      public LineItemReceivingItem() {
47      }
48  
49      public LineItemReceivingItem(LineItemReceivingDocument rld) {
50          this.setDocumentNumber(rld.getDocumentNumber());
51          this.setItemReceivedTotalQuantity(KualiDecimal.ZERO);
52          this.setItemReturnedTotalQuantity(KualiDecimal.ZERO);
53          this.setItemDamagedTotalQuantity(KualiDecimal.ZERO);
54          this.setItemOriginalReceivedTotalQuantity(KualiDecimal.ZERO);
55          this.setItemOriginalReturnedTotalQuantity(KualiDecimal.ZERO);
56          this.setItemOriginalDamagedTotalQuantity(KualiDecimal.ZERO);
57      }
58  
59      public LineItemReceivingItem(PurchaseOrderItem poi, LineItemReceivingDocument rld) {
60  
61          this.setDocumentNumber(rld.getDocumentNumber());
62          this.setItemTypeCode(poi.getItemTypeCode());
63          this.setPurchaseOrderIdentifier(rld.getPurchaseOrderIdentifier());
64  
65          this.setItemLineNumber(poi.getItemLineNumber());
66          this.setItemCatalogNumber(poi.getItemCatalogNumber());
67          this.setItemDescription(poi.getItemDescription());
68  
69          this.setItemOrderedQuantity(poi.getItemQuantity());
70          this.setItemUnitOfMeasureCode(poi.getItemUnitOfMeasureCode());
71  
72          // TODO: Chris - look into this it appears this is null rather than zero on amendment, find out why!
73          if (ObjectUtils.isNull(poi.getItemReceivedTotalQuantity())) {
74              this.setItemReceivedPriorQuantity(KualiDecimal.ZERO);
75          } else {
76              this.setItemReceivedPriorQuantity(poi.getItemReceivedTotalQuantity());
77          }
78  
79          this.setItemReceivedToBeQuantity(this.getItemOrderedQuantity().subtract(this.getItemReceivedPriorQuantity()));
80  
81          // should determine whether this is prefilled be based on the parameter that allows loading from po
82          this.setItemReceivedTotalQuantity(KualiDecimal.ZERO);
83  
84          this.setItemReturnedTotalQuantity(KualiDecimal.ZERO);
85          this.setItemDamagedTotalQuantity(KualiDecimal.ZERO);
86  
87          this.setItemOriginalReceivedTotalQuantity(KualiDecimal.ZERO);
88          this.setItemOriginalReturnedTotalQuantity(KualiDecimal.ZERO);
89          this.setItemOriginalDamagedTotalQuantity(KualiDecimal.ZERO);
90  
91          // not added
92          this.setItemReasonAddedCode(null);
93      }
94  
95      /**
96       * Retreives a purchase order item by inspecting the item type to see if its above the line or below the line and returns the
97       * appropriate type.
98       *
99       * @return - purchase order item
100      */
101     public PurchaseOrderItem getPurchaseOrderItem() {
102         if (ObjectUtils.isNotNull(this.getLineItemReceivingDocument())) {
103             if (ObjectUtils.isNull(this.getLineItemReceivingDocument())) {
104                 this.refreshReferenceObject("lineItemReceivingDocument");
105             }
106         }
107         // ideally we should do this a different way - maybe move it all into the service or save this info somehow (make sure and
108         // update though)
109         if (getLineItemReceivingDocument() != null) {
110             PurchaseOrderDocument po = getLineItemReceivingDocument().getPurchaseOrderDocument();
111             PurchaseOrderItem poi = null;
112             if (this.getItemType().isLineItemIndicator()) {
113                 List<PurchaseOrderItem> items = po.getItems();
114                 poi = items.get(this.getItemLineNumber().intValue() - 1);
115 
116                 // throw error if line numbers don't match
117             }
118             if (poi != null) {
119                 return poi;
120             } else {
121                 // LOG.debug("getPurchaseOrderItem() Returning null because PurchaseOrderItem object for line number" +
122                 // getItemLineNumber() + "or itemType " + getItemTypeCode() + " is null");
123                 return null;
124             }
125         } else {
126             LOG.error("getPurchaseOrderItem() Returning null because paymentRequest object is null");
127             throw new PurError("Receiving Line Object in Purchase Order item line number " + getItemLineNumber() + "or itemType " + getItemTypeCode() + " is null");
128         }
129     }
130 
131     /**
132      * Gets the itemOrderedQuantity attribute.
133      *
134      * @return Returns the itemOrderedQuantity
135      */
136     public KualiDecimal getItemOrderedQuantity() {
137         return itemOrderedQuantity;
138     }
139 
140     /**
141      * Sets the itemOrderedQuantity attribute.
142      *
143      * @param itemOrderedQuantity The itemOrderedQuantity to set.
144      */
145     public void setItemOrderedQuantity(KualiDecimal itemOrderedQuantity) {
146         this.itemOrderedQuantity = itemOrderedQuantity;
147     }
148 
149     /**
150      * Gets the LineItemReceivingDocument attribute.
151      *
152      * @return Returns the LineItemReceivingDocument.
153      */
154     public LineItemReceivingDocument getLineItemReceivingDocument() {
155         return lineItemReceivingDocument;
156     }
157 
158     /**
159      * Sets the LineItemReceivingDocument attribute value.
160      *
161      * @param LineItemReceivingDocument The LineItemReceivingDocument to set.
162      * @deprecated
163      */
164     public void setLineItemReceivingDocument(LineItemReceivingDocument lineItemReceivingDocument) {
165         this.lineItemReceivingDocument = lineItemReceivingDocument;
166     }
167 
168     public KualiDecimal getItemReceivedPriorQuantity() {
169         if (ObjectUtils.isNull(itemReceivedPriorQuantity)) {
170             setItemReceivedPriorQuantity(getPurchaseOrderItem().getItemReceivedTotalQuantity());
171         }
172         return itemReceivedPriorQuantity;
173     }
174 
175     public void setItemReceivedPriorQuantity(KualiDecimal itemReceivedPriorQuantity) {
176 
177         this.itemReceivedPriorQuantity = itemReceivedPriorQuantity;
178     }
179 
180     public KualiDecimal getItemReceivedToBeQuantity() {
181         // lazy loaded
182         KualiDecimal toBeQuantity = this.getItemOrderedQuantity().subtract(getItemReceivedPriorQuantity());
183         if (toBeQuantity.isNegative()) {
184             toBeQuantity = KualiDecimal.ZERO;
185         }
186         setItemReceivedToBeQuantity(toBeQuantity);
187 
188         return itemReceivedToBeQuantity;
189     }
190 
191     public void setItemReceivedToBeQuantity(KualiDecimal itemReceivedToBeQuantity) {
192         this.itemReceivedToBeQuantity = itemReceivedToBeQuantity;
193     }
194 
195     public boolean isOrderedItem() {
196         return StringUtils.isEmpty(getItemReasonAddedCode());
197     }
198 
199 }