1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.ole.module.purap.businessobject;
18
19 import org.kuali.ole.module.purap.PurapConstants;
20 import org.kuali.ole.module.purap.PurapParameterConstants;
21 import org.kuali.ole.module.purap.PurapPropertyConstants;
22 import org.kuali.ole.module.purap.document.InvoiceDocument;
23 import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
24 import org.kuali.ole.module.purap.document.service.AccountsPayableService;
25 import org.kuali.ole.module.purap.document.service.PurapService;
26 import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
27 import org.kuali.ole.module.purap.exception.PurError;
28 import org.kuali.ole.module.purap.util.ExpiredOrClosedAccountEntry;
29 import org.kuali.ole.module.purap.util.PurApItemUtils;
30 import org.kuali.ole.module.purap.util.PurApObjectUtils;
31 import org.kuali.ole.select.document.service.OleInvoiceService;
32 import org.kuali.ole.sys.OLEConstants;
33 import org.kuali.ole.sys.context.SpringContext;
34 import org.kuali.ole.sys.service.impl.OleParameterConstants;
35 import org.kuali.rice.core.api.util.type.KualiDecimal;
36 import org.kuali.rice.krad.util.ObjectUtils;
37
38 import java.math.BigDecimal;
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.List;
42
43
44
45
46 public class InvoiceItem extends AccountsPayableItemBase {
47 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(InvoiceItem.class);
48
49 private BigDecimal purchaseOrderItemUnitPrice;
50 private KualiDecimal itemOutstandingInvoiceQuantity;
51 private KualiDecimal itemOutstandingInvoiceAmount;
52 private Integer invoiceIdentifier;
53 private Integer purchaseOrderIdentifier;
54 protected Integer accountsPayablePurchasingDocumentLinkIdentifier;
55 protected Integer poLineNumber;
56 protected boolean closePurchaseOrderIndicator;
57 protected boolean reopenPurchaseOrderIndicator;
58 protected Integer postingYear;
59 protected String recurringPaymentTypeCode;
60 protected RecurringPaymentType recurringPaymentType;
61 protected boolean receivingDocumentRequiredIndicator;
62 protected PurchaseOrderDocument purchaseOrderDocument;
63
64
65
66
67 public InvoiceItem() {
68
69 }
70
71
72
73
74
75
76
77 public InvoiceItem(PurchaseOrderItem poi, InvoiceDocument inv) {
78 this(poi, inv, new HashMap<String, ExpiredOrClosedAccountEntry>());
79 }
80
81
82
83
84
85
86
87
88 public InvoiceItem(PurchaseOrderItem poi, InvoiceDocument inv, HashMap<String, ExpiredOrClosedAccountEntry> expiredOrClosedAccountList) {
89
90
91 PurApObjectUtils.populateFromBaseClass(PurApItemBase.class, poi, this, PurapConstants.PREQ_ITEM_UNCOPYABLE_FIELDS);
92
93 setItemDescription(poi.getItemDescription());
94
95
96 resetAccount();
97
98
99 List accounts = new ArrayList();
100 for (PurApAccountingLine account : poi.getSourceAccountingLines()) {
101 PurchaseOrderAccount poa = (PurchaseOrderAccount) account;
102
103
104 SpringContext.getBean(AccountsPayableService.class).processExpiredOrClosedAccount(poa, expiredOrClosedAccountList);
105
106
107 if (poa.getAmount()!=null && poa.getAmount().isZero()) {
108 if (SpringContext.getBean(AccountsPayableService.class).canCopyAccountingLinesWithZeroAmount()) {
109 accounts.add(new InvoiceAccount(this, poa));
110 }
111 } else {
112 accounts.add(new InvoiceAccount(this, poa));
113 }
114 }
115
116 this.setSourceAccountingLines(accounts);
117 this.getUseTaxItems().clear();
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 if ((ObjectUtils.isNotNull(this.getItemType()) && this.getItemType().isAmountBasedGeneralLedgerIndicator())) {
134
135 this.setItemUnitPrice(null);
136 }
137
138
139 this.purchaseOrderItemUnitPrice = poi.getItemUnitPrice();
140
141
142
143 this.setPurapDocumentIdentifier(inv.getPurapDocumentIdentifier());
144 this.setPurapDocument(inv);
145 }
146
147
148
149
150
151
152
153 @Override
154 public PurchaseOrderItem getPurchaseOrderItem() {
155 if (ObjectUtils.isNotNull(this.getPurapDocumentIdentifier())) {
156 if (ObjectUtils.isNull(this.getInvoice())) {
157 this.refreshReferenceObject(PurapPropertyConstants.PURAP_DOC);
158 }
159 }
160
161
162 if (getInvoice() != null) {
163 PurchaseOrderDocument po = this.getInvoice().getPurchaseOrderDocument(this.getPurchaseOrderIdentifier());
164 this.setPostingYear(po.getPostingYear());
165 if (po.getRecurringPaymentType() != null) {
166 this.setRecurringPaymentType(po.getRecurringPaymentType());
167 this.setRecurringPaymentTypeCode(po.getRecurringPaymentTypeCode());
168 }
169 PurchaseOrderItem poi = null;
170 if (this.getItemType().isLineItemIndicator()) {
171 List<PurchaseOrderItem> items = po.getItems();
172 poi = items.get(this.getItemLineNumber().intValue() - 1);
173
174
175
176
177
178
179
180 } else {
181 poi = (PurchaseOrderItem) SpringContext.getBean(PurapService.class).getBelowTheLineByType(po, this.getItemType());
182 }
183 if (poi != null) {
184 return poi;
185 } else {
186 if (LOG.isDebugEnabled()) {
187 LOG.debug("getPurchaseOrderItem() Returning null because PurchaseOrderItem object for line number" + getItemLineNumber() + "or itemType " + getItemTypeCode() + " is null");
188 }
189 return null;
190 }
191 } else {
192
193 LOG.error("getPurchaseOrderItem() Returning null because invoice object is null");
194 throw new PurError("Payment Request Object in Purchase Order item line number " + getItemLineNumber() + "or itemType " + getItemTypeCode() + " is null");
195 }
196 }
197
198 public KualiDecimal getPoOutstandingAmount() {
199 PurchaseOrderItem poi = getPurchaseOrderItem();
200 if (ObjectUtils.isNull(this.getPurchaseOrderItemUnitPrice()) || KualiDecimal.ZERO.equals(this.getPurchaseOrderItemUnitPrice())) {
201 return null;
202 } else {
203 return this.getPoOutstandingAmount(poi);
204 }
205 }
206
207 private KualiDecimal getPoOutstandingAmount(PurchaseOrderItem poi) {
208 if (poi == null) {
209 return KualiDecimal.ZERO;
210 } else {
211 return poi.getItemOutstandingEncumberedAmount();
212 }
213 }
214
215 public KualiDecimal getPoOriginalAmount() {
216 PurchaseOrderItem poi = getPurchaseOrderItem();
217 if (poi == null) {
218 return null;
219 } else {
220 return poi.getExtendedPrice();
221 }
222 }
223
224
225
226
227
228
229
230 @Deprecated
231 public void setPoOutstandingAmount(KualiDecimal amount) {
232
233 }
234
235
236 public KualiDecimal getPoOutstandingQuantity() {
237 PurchaseOrderItem poi = getPurchaseOrderItem();
238 if (poi == null) {
239 return null;
240 } else {
241 if (PurapConstants.ItemTypeCodes.ITEM_TYPE_SERVICE_CODE.equals(this.getItemTypeCode())) {
242 return null;
243 } else {
244 return poi.getOutstandingQuantity();
245 }
246 }
247 }
248
249
250
251
252
253
254
255 @Deprecated
256 public void setPoOutstandingQuantity(KualiDecimal qty) {
257
258 }
259
260 public BigDecimal getPurchaseOrderItemUnitPrice() {
261 return purchaseOrderItemUnitPrice;
262 }
263
264 public Integer getAccountsPayablePurchasingDocumentLinkIdentifier() {
265 return accountsPayablePurchasingDocumentLinkIdentifier;
266 }
267
268 public void setAccountsPayablePurchasingDocumentLinkIdentifier(Integer accountsPayablePurchasingDocumentLinkIdentifier) {
269 this.accountsPayablePurchasingDocumentLinkIdentifier = accountsPayablePurchasingDocumentLinkIdentifier;
270 }
271
272 public Integer getPoLineNumber() {
273 return poLineNumber;
274 }
275
276 public void setPoLineNumber(Integer poLineNumber) {
277 this.poLineNumber = poLineNumber;
278 }
279
280
281 public Integer getPurchaseOrderIdentifier() {
282 return purchaseOrderIdentifier;
283 }
284
285 public void setPurchaseOrderIdentifier(Integer purchaseOrderIdentifier) {
286 this.purchaseOrderIdentifier = purchaseOrderIdentifier;
287 }
288
289 public Integer getInvoiceIdentifier() {
290 return invoiceIdentifier;
291 }
292
293 public void setInvoiceIdentifier(Integer invoiceIdentifier) {
294 this.invoiceIdentifier = invoiceIdentifier;
295 }
296
297 public BigDecimal getOriginalAmountfromPO() {
298 return purchaseOrderItemUnitPrice;
299 }
300
301 public void setOriginalAmountfromPO(BigDecimal purchaseOrderItemUnitPrice) {
302
303 }
304
305 public Integer getPostingYear() {
306 return postingYear;
307 }
308
309
310
311
312 public void setPostingYear(Integer postingYear) {
313 this.postingYear = postingYear;
314 }
315
316 public void setPurchaseOrderItemUnitPrice(BigDecimal purchaseOrderItemUnitPrice) {
317 this.purchaseOrderItemUnitPrice = purchaseOrderItemUnitPrice;
318 }
319
320 public KualiDecimal getItemOutstandingInvoiceAmount() {
321 return itemOutstandingInvoiceAmount;
322 }
323
324 public void setItemOutstandingInvoiceAmount(KualiDecimal itemOutstandingInvoiceAmount) {
325 this.itemOutstandingInvoiceAmount = itemOutstandingInvoiceAmount;
326 }
327
328 public KualiDecimal getItemOutstandingInvoiceQuantity() {
329 return itemOutstandingInvoiceQuantity;
330 }
331
332 public void setItemOutstandingInvoiceQuantity(KualiDecimal itemOutstandingInvoiceQuantity) {
333 this.itemOutstandingInvoiceQuantity = itemOutstandingInvoiceQuantity;
334 }
335
336 public InvoiceDocument getInvoice() {
337 if (ObjectUtils.isNotNull(getPurapDocumentIdentifier())) {
338 if (ObjectUtils.isNull(getPurapDocument())) {
339 this.refreshReferenceObject(PurapPropertyConstants.PURAP_DOC);
340 }
341 }
342 return super.getPurapDocument();
343 }
344
345 public void setInvoice(InvoiceDocument invoice) {
346 this.setPurapDocument(invoice);
347 }
348
349 public boolean isClosePurchaseOrderIndicator() {
350 return closePurchaseOrderIndicator;
351 }
352
353 public void setClosePurchaseOrderIndicator(boolean closePurchaseOrderIndicator) {
354 this.closePurchaseOrderIndicator = closePurchaseOrderIndicator;
355 }
356
357 public boolean isReopenPurchaseOrderIndicator() {
358 return reopenPurchaseOrderIndicator;
359 }
360
361 public void setReopenPurchaseOrderIndicator(boolean reopenPurchaseOrderIndicator) {
362 this.reopenPurchaseOrderIndicator = reopenPurchaseOrderIndicator;
363 }
364
365 public String getRecurringPaymentTypeCode() {
366 return recurringPaymentTypeCode;
367 }
368
369 public void setRecurringPaymentTypeCode(String recurringPaymentTypeCode) {
370 this.recurringPaymentTypeCode = recurringPaymentTypeCode;
371 }
372
373 public RecurringPaymentType getRecurringPaymentType() {
374 if (ObjectUtils.isNull(recurringPaymentType)) {
375 refreshReferenceObject(PurapPropertyConstants.RECURRING_PAYMENT_TYPE);
376 }
377 return recurringPaymentType;
378 }
379
380 public void setRecurringPaymentType(RecurringPaymentType recurringPaymentType) {
381 this.recurringPaymentType = recurringPaymentType;
382 }
383
384 public void generateAccountListFromPoItemAccounts(List<PurApAccountingLine> accounts) {
385 for (PurApAccountingLine line : accounts) {
386 PurchaseOrderAccount poa = (PurchaseOrderAccount) line;
387 if (!line.isEmpty()) {
388 getSourceAccountingLines().add(new InvoiceAccount(this, poa));
389 }
390 }
391 }
392
393
394
395
396 @Override
397 public Class getAccountingLineClass() {
398 return InvoiceAccount.class;
399 }
400
401 public boolean isDisplayOnPreq() {
402 PurchaseOrderItem poi = getPurchaseOrderItem();
403 if (ObjectUtils.isNull(poi)) {
404 LOG.debug("poi was null");
405 return false;
406 }
407
408
409 if (!poi.isItemActiveIndicator()) {
410 if (LOG.isDebugEnabled()) {
411 LOG.debug("poi was not active: " + poi.toString());
412 }
413 return false;
414 }
415
416 ItemType poiType = poi.getItemType();
417
418 if (poiType.isQuantityBasedGeneralLedgerIndicator()) {
419 if (poi.getItemQuantity().isGreaterThan(poi.getItemInvoicedTotalQuantity())) {
420 return true;
421 } else {
422 if (ObjectUtils.isNotNull(this.getItemQuantity()) && this.getItemQuantity().isGreaterThan(KualiDecimal.ZERO)) {
423 return true;
424 }
425 }
426
427 return false;
428 } else {
429 if (poi.getItemOutstandingEncumberedAmount().isGreaterThan(KualiDecimal.ZERO)) {
430 return true;
431 } else {
432 if (PurApItemUtils.isNonZeroExtended(this)) {
433 return true;
434 }
435 return false;
436 }
437
438 }
439 }
440
441
442
443
444
445
446 @Override
447 public void resetAccount() {
448 super.resetAccount();
449 this.getNewSourceLine().setAmount(null);
450 this.getNewSourceLine().setAccountLinePercent(null);
451 }
452
453
454
455
456 public void addToUnitPrice(BigDecimal addThisValue) {
457 if (getItemUnitPrice() == null) {
458 setItemUnitPrice(BigDecimal.ZERO);
459 }
460 BigDecimal addedPrice = getItemUnitPrice().add(addThisValue);
461 setItemUnitPrice(addedPrice);
462 }
463
464 public void addToExtendedPrice(KualiDecimal addThisValue) {
465 if (getExtendedPrice() == null) {
466 setExtendedPrice(KualiDecimal.ZERO);
467 }
468 KualiDecimal addedPrice = getExtendedPrice().add(addThisValue);
469 setExtendedPrice(addedPrice);
470 }
471
472 @Override
473 public Class getUseTaxClass() {
474 return InvoiceItemUseTax.class;
475 }
476
477
478
479
480
481
482 public boolean isReceivingDocumentRequiredIndicator() {
483 return receivingDocumentRequiredIndicator;
484 }
485
486
487
488
489
490
491
492 public void setReceivingDocumentRequiredIndicator(boolean receivingDocumentRequiredIndicator) {
493
494 if (!isEnableReceivingDocumentRequiredIndicator()) {
495 this.receivingDocumentRequiredIndicator = false;
496 } else {
497 this.receivingDocumentRequiredIndicator = receivingDocumentRequiredIndicator;
498 }
499 }
500
501
502
503
504 public boolean isEnableReceivingDocumentRequiredIndicator() {
505 return SpringContext.getBean(OleInvoiceService.class).getParameterBoolean(OLEConstants.OptionalModuleNamespaces.PURCHASING_ACCOUNTS_PAYABLE, OleParameterConstants.DOCUMENT_COMPONENT, PurapParameterConstants.RECEIVING_DOCUMENT_REQUIRED_IND);
506
507 }
508
509
510
511
512 public PurchaseOrderDocument getPurchaseOrderDocument() {
513 if ((ObjectUtils.isNull(purchaseOrderDocument) || ObjectUtils.isNull(purchaseOrderDocument.getPurapDocumentIdentifier())) && (ObjectUtils.isNotNull(getPurchaseOrderIdentifier()))) {
514 setPurchaseOrderDocument(SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(this.getPurchaseOrderIdentifier()));
515 }
516 return purchaseOrderDocument;
517 }
518
519
520
521
522 public void setPurchaseOrderDocument(PurchaseOrderDocument purchaseOrderDocument) {
523 if (ObjectUtils.isNull(purchaseOrderDocument)) {
524
525
526 this.purchaseOrderDocument = null;
527 } else {
528 if (ObjectUtils.isNotNull(purchaseOrderDocument.getPurapDocumentIdentifier())) {
529 setPurchaseOrderIdentifier(purchaseOrderDocument.getPurapDocumentIdentifier());
530 }
531 this.purchaseOrderDocument = purchaseOrderDocument;
532 }
533 }
534
535
536 }