View Javadoc
1   /*
2    * Copyright 2006 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  
17  package org.kuali.ole.module.purap.businessobject;
18  
19  import org.apache.commons.lang.StringUtils;
20  import org.kuali.ole.module.purap.PurapConstants;
21  import org.kuali.ole.module.purap.PurapPropertyConstants;
22  import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
23  import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
24  import org.kuali.ole.select.businessobject.OleNotes;
25  import org.kuali.ole.select.businessobject.OlePurchaseOrderNotes;
26  import org.kuali.ole.sys.context.SpringContext;
27  import org.kuali.rice.core.api.util.type.KualiDecimal;
28  import org.kuali.rice.krad.service.SequenceAccessorService;
29  import org.kuali.rice.krad.util.ObjectUtils;
30  
31  import java.math.BigDecimal;
32  import java.util.ArrayList;
33  import java.util.LinkedHashMap;
34  import java.util.List;
35  
36  import static org.kuali.rice.core.api.util.type.KualiDecimal.ZERO;
37  
38  /**
39   * Purchase Order Item Business Object.
40   */
41  public class PurchaseOrderItem extends PurchasingItemBase {
42      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PurchaseOrderItem.class);
43  
44      private String documentNumber;
45      private KualiDecimal itemInvoicedTotalQuantity;
46      private KualiDecimal itemInvoicedTotalAmount;
47      private KualiDecimal itemReceivedTotalQuantity;
48      private KualiDecimal itemOutstandingEncumberedQuantity;
49      private KualiDecimal itemOutstandingEncumberedAmount;
50      private boolean itemActiveIndicator = true;
51      private KualiDecimal itemDamagedTotalQuantity;
52      private List<OleNotes> notes;
53      private Integer noteTypeId;
54      private String note;
55  
56      private PurchaseOrderDocument purchaseOrder;
57  
58      // Not persisted to DB
59      private boolean itemSelectedForRetransmitIndicator;
60      private boolean movingToSplit;
61  
62  
63      /**
64       * Default constructor.
65       */
66      public PurchaseOrderItem() {
67          notes = new ArrayList<OleNotes>();
68      }
69  
70      /**
71       * Constructor.
72       *
73       * @param ri - Requisition Item
74       * @param po - Purchase Order Document
75       */
76      public PurchaseOrderItem(RequisitionItem ri, PurchaseOrderDocument po, RequisitionCapitalAssetItem reqCamsItem) {
77          super();
78          this.setPurchaseOrder(po);
79          SequenceAccessorService sas = SpringContext.getBean(SequenceAccessorService.class);
80          Integer itemIdentifier = sas.getNextAvailableSequenceNumber("PO_ITM_ID", PurchaseOrderDocument.class).intValue();
81          this.setItemIdentifier(itemIdentifier);
82          this.setItemLineNumber(ri.getItemLineNumber());
83          this.setItemUnitOfMeasureCode(ri.getItemUnitOfMeasureCode());
84          this.setItemQuantity(ri.getItemQuantity());
85          this.setItemCatalogNumber(ri.getItemCatalogNumber());
86          this.setItemDescription(ri.getItemDescription());
87          this.setItemUnitPrice(ri.getItemUnitPrice());
88          this.setItemAuxiliaryPartIdentifier(ri.getItemAuxiliaryPartIdentifier());
89          this.setItemAssignedToTradeInIndicator(ri.getItemAssignedToTradeInIndicator());
90          this.setItemTaxAmount(ri.getItemTaxAmount());
91          //copy use tax items over, and blank out keys (useTaxId and itemIdentifier)
92          for (PurApItemUseTax useTaxItem : ri.getUseTaxItems()) {
93              PurchaseOrderItemUseTax newItemUseTax = new PurchaseOrderItemUseTax(useTaxItem);
94              newItemUseTax.setItemIdentifier(itemIdentifier);
95              this.getUseTaxItems().add(newItemUseTax);
96  
97          }
98  
99          this.setExternalOrganizationB2bProductReferenceNumber(ri.getExternalOrganizationB2bProductReferenceNumber());
100         this.setExternalOrganizationB2bProductTypeName(ri.getExternalOrganizationB2bProductTypeName());
101 
102         this.setItemReceivedTotalQuantity(ZERO);
103         this.setItemDamagedTotalQuantity(ZERO);
104 
105         this.setItemTypeCode(ri.getItemTypeCode());
106 
107         if (ri.getSourceAccountingLines() != null && ri.getSourceAccountingLines().size() > 0 &&
108                 !StringUtils.equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_ORDER_DISCOUNT_CODE, ri.getItemType().getItemTypeCode())) {
109             List accounts = new ArrayList();
110             for (PurApAccountingLine account : ri.getSourceAccountingLines()) {
111                 PurchaseOrderAccount poAccount = new PurchaseOrderAccount(account);
112                 poAccount.setPurchaseOrderItem(this);
113                 accounts.add(poAccount);
114             }
115             this.setSourceAccountingLines(accounts);
116         }
117         // By default, set the item active indicator to true.
118         // In amendment, the user can set it to false when they click on
119         // the inactivate button.
120         this.setItemActiveIndicator(true);
121 
122         this.setPurchasingCommodityCode(ri.getPurchasingCommodityCode());
123         this.setCommodityCode(getCommodityCode());
124 
125         // If the RequisitionItem has a CapitalAssetItem, create a new PurchasingCapitalAssetItem and add it to the PO.
126         if (ObjectUtils.isNotNull(reqCamsItem)) {
127             PurchaseOrderCapitalAssetItem newPOCapitalAssetItem = new PurchaseOrderCapitalAssetItem(reqCamsItem, itemIdentifier);
128             po.getPurchasingCapitalAssetItems().add(newPOCapitalAssetItem);
129         }
130     }
131 
132     public boolean isItemActiveIndicator() {
133         return itemActiveIndicator;
134     }
135 
136 //    public String getItemActiveIndicator() {
137 //        return (new Boolean(itemActiveIndicator)).toString();
138 //    }
139 
140 
141     public List<OleNotes> getNotes() {
142         return notes;
143     }
144 
145     public void setNotes(List<OleNotes> notes) {
146         this.notes = notes;
147     }
148 
149     public String getNote() {
150         return note;
151     }
152 
153     public void setNote(String note) {
154         this.note = note;
155     }
156 
157     public Integer getNoteTypeId() {
158         return noteTypeId;
159     }
160 
161     public void setNoteTypeId(Integer noteTypeId) {
162         this.noteTypeId = noteTypeId;
163     }
164 
165     public void setItemActiveIndicator(boolean itemActiveIndicator) {
166         this.itemActiveIndicator = itemActiveIndicator;
167     }
168 
169     public KualiDecimal getItemInvoicedTotalAmount() {
170         return itemInvoicedTotalAmount;
171     }
172 
173     public void setItemInvoicedTotalAmount(KualiDecimal itemInvoicedTotalAmount) {
174         this.itemInvoicedTotalAmount = itemInvoicedTotalAmount;
175     }
176 
177     public KualiDecimal getItemInvoicedTotalQuantity() {
178         return itemInvoicedTotalQuantity;
179     }
180 
181     public void setItemInvoicedTotalQuantity(KualiDecimal itemInvoicedTotalQuantity) {
182         this.itemInvoicedTotalQuantity = itemInvoicedTotalQuantity;
183     }
184 
185     public KualiDecimal getItemOutstandingEncumberedQuantity() {
186         return itemOutstandingEncumberedQuantity;
187     }
188 
189     public void setItemOutstandingEncumberedQuantity(KualiDecimal itemOutstandingEncumberedQuantity) {
190         this.itemOutstandingEncumberedQuantity = itemOutstandingEncumberedQuantity;
191     }
192 
193     public KualiDecimal getItemOutstandingEncumberedAmount() {
194         return itemOutstandingEncumberedAmount;
195     }
196 
197     public void setItemOutstandingEncumberedAmount(KualiDecimal itemOutstandingEncumbranceAmount) {
198         this.itemOutstandingEncumberedAmount = itemOutstandingEncumbranceAmount;
199     }
200 
201     public KualiDecimal getItemReceivedTotalQuantity() {
202         return itemReceivedTotalQuantity;
203     }
204 
205     public void setItemReceivedTotalQuantity(KualiDecimal itemReceivedTotalQuantity) {
206         this.itemReceivedTotalQuantity = itemReceivedTotalQuantity;
207     }
208 
209     /**
210      * Gets the itemDamagedTotalQuantity attribute.
211      *
212      * @return Returns the itemDamagedTotalQuantity.
213      */
214     public KualiDecimal getItemDamagedTotalQuantity() {
215         return itemDamagedTotalQuantity;
216     }
217 
218     /**
219      * Sets the itemDamagedTotalQuantity attribute value.
220      *
221      * @param itemDamagedTotalQuantity The itemDamagedTotalQuantity to set.
222      */
223     public void setItemDamagedTotalQuantity(KualiDecimal itemDamagedTotalQuantity) {
224         this.itemDamagedTotalQuantity = itemDamagedTotalQuantity;
225     }
226 
227     public PurchaseOrderDocument getPurchaseOrder() {
228         if (ObjectUtils.isNull(purchaseOrder)) {
229             refreshReferenceObject(PurapPropertyConstants.PURAP_DOC);
230         }
231         return super.getPurapDocument();
232     }
233 
234     public void setPurchaseOrder(PurchaseOrderDocument purchaseOrder) {
235         setPurapDocument(purchaseOrder);
236     }
237 
238     public String getDocumentNumber() {
239         return documentNumber;
240     }
241 
242     public void setDocumentNumber(String documentNumber) {
243         this.documentNumber = documentNumber;
244     }
245 
246     public boolean isItemSelectedForRetransmitIndicator() {
247         return itemSelectedForRetransmitIndicator;
248     }
249 
250     public void setItemSelectedForRetransmitIndicator(boolean itemSelectedForRetransmitIndicator) {
251         this.itemSelectedForRetransmitIndicator = itemSelectedForRetransmitIndicator;
252     }
253 
254     public boolean isMovingToSplit() {
255         return movingToSplit;
256     }
257 
258     public void setMovingToSplit(boolean movingToSplit) {
259         this.movingToSplit = movingToSplit;
260     }
261 
262     /**
263      * @see org.kuali.rice.krad.bo.BusinessObjectBase#toStringMapper()
264      */
265 
266     protected LinkedHashMap toStringMapper_RICE20_REFACTORME() {
267         LinkedHashMap m = new LinkedHashMap();
268         m.put("documentNumber", this.documentNumber);
269         if (this.getItemIdentifier() != null) {
270             m.put("itemIdentifier", this.getItemIdentifier().toString());
271         }
272         return m;
273     }
274 
275     /**
276      * @see org.kuali.ole.module.purap.businessobject.PurApItem#getAccountingLineClass()
277      */
278     public Class getAccountingLineClass() {
279         return PurchaseOrderAccount.class;
280     }
281 
282     /**
283      * This method returns the total item paid amount
284      *
285      * @return
286      */
287     public KualiDecimal getItemPaidAmount() {
288         if (!(this.isItemActiveIndicator())) {
289             return KualiDecimal.ZERO;
290         }
291         return this.getItemInvoicedTotalAmount();
292     }
293 
294     public KualiDecimal getItemEncumbranceRelievedAmount() {
295         // check that it is active else return zero
296         if (this == null || !this.isItemActiveIndicator()) {
297             return KualiDecimal.ZERO;
298         }
299         // setup outstanding amount and get totalEncumberance from this.getExtendedCost()
300         KualiDecimal outstandingAmount = KualiDecimal.ZERO;
301         KualiDecimal totalEncumberance = this.getTotalAmount();
302 
303         ItemType iT = this.getItemType();
304         // if service add the po outstanding amount to outstanding amount
305         if (iT.isAmountBasedGeneralLedgerIndicator()) {
306             outstandingAmount = outstandingAmount.add(this.getItemOutstandingEncumberedAmount());
307         } else {
308             // else add outstanding quantity * unit price
309             BigDecimal qty = new BigDecimal(this.getOutstandingQuantity().toString());
310             outstandingAmount = outstandingAmount.add(new KualiDecimal(this.getItemUnitPrice().multiply(qty).setScale(KualiDecimal.SCALE, KualiDecimal.ROUND_BEHAVIOR)));
311 
312             KualiDecimal itemTaxAmount = this.getItemTaxAmount() == null ? ZERO : this.getItemTaxAmount();
313             KualiDecimal outstandingTaxAmount = new KualiDecimal(qty).divide(this.getItemQuantity()).multiply(itemTaxAmount);
314             outstandingAmount = outstandingAmount.add(outstandingTaxAmount);
315         }
316 
317         // return the total encumbrance subtracted by the outstanding amount from above
318         return totalEncumberance.subtract(outstandingAmount);
319     }
320 
321     /**
322      * Exists due to a setter requirement by the htmlControlAttribute
323      *
324      * @param amount - outstanding quantity
325      * @deprecated
326      */
327     public void setOutstandingQuantity() {
328         // do nothing
329     }
330 
331     public KualiDecimal getOutstandingQuantity() {
332         KualiDecimal outstandingQuantity = (this.getItemQuantity() != null) ? this.getItemQuantity() : KualiDecimal.ZERO;
333         KualiDecimal invoicedQuantity = (this.getItemInvoicedTotalQuantity() != null) ? this.getItemInvoicedTotalQuantity() : KualiDecimal.ZERO;
334         return outstandingQuantity.subtract(invoicedQuantity);
335     }
336 
337     public boolean isCanInactivateItem() {
338         if (versionNumber == null) {
339             // don't allow newly added item to be inactivatable.
340             return false;
341         } else if (versionNumber != null && itemActiveIndicator && !getPurchaseOrder().getContainsUnpaidPaymentRequestsOrCreditMemos()) {
342             return true;
343         }
344         return false;
345     }
346 
347     /**
348      * Override the method in PurApItemBase so that if the item is
349      * not eligible to be displayed in the account summary tab,
350      * which is if the item is inactive, we'll return null and
351      * the item won't be added to the list of account summary.
352      *
353      * @see org.kuali.ole.module.purap.businessobject.PurApItemBase#getSummaryItem()
354      */
355     @Override
356     public PurApSummaryItem getSummaryItem() {
357         if (!this.itemActiveIndicator) {
358             return null;
359         } else {
360             return super.getSummaryItem();
361         }
362     }
363 
364     public boolean isNewUnorderedItem() {
365         return SpringContext.getBean(PurchaseOrderService.class).isNewUnorderedItem(this);
366     }
367 
368     @Override
369     public boolean isNewItemForAmendment() {
370         return SpringContext.getBean(PurchaseOrderService.class).isNewItemForAmendment(this);
371     }
372 
373     @Override
374     public Class getUseTaxClass() {
375         return PurchaseOrderItemUseTax.class;
376     }
377 
378 
379 }