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.PurapPropertyConstants;
21 import org.kuali.ole.module.purap.document.PaymentRequestDocument;
22 import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
23 import org.kuali.ole.module.purap.document.service.AccountsPayableService;
24 import org.kuali.ole.module.purap.document.service.PurapService;
25 import org.kuali.ole.module.purap.exception.PurError;
26 import org.kuali.ole.module.purap.util.ExpiredOrClosedAccountEntry;
27 import org.kuali.ole.module.purap.util.PurApItemUtils;
28 import org.kuali.ole.module.purap.util.PurApObjectUtils;
29 import org.kuali.ole.select.businessobject.OleInvoiceItem;
30 import org.kuali.ole.select.document.OlePaymentRequestDocument;
31 import org.kuali.ole.select.document.service.OlePaymentRequestService;
32 import org.kuali.ole.sys.businessobject.SourceAccountingLine;
33 import org.kuali.ole.sys.context.SpringContext;
34 import org.kuali.rice.core.api.util.type.KualiDecimal;
35 import org.kuali.rice.krad.util.ObjectUtils;
36
37 import java.math.BigDecimal;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
41
42
43
44
45 public class PaymentRequestItem extends AccountsPayableItemBase {
46 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PaymentRequestItem.class);
47
48 protected BigDecimal purchaseOrderItemUnitPrice;
49 private KualiDecimal itemOutstandingInvoiceQuantity;
50 private KualiDecimal itemOutstandingInvoiceAmount;
51
52
53
54
55 public PaymentRequestItem() {
56
57 }
58
59
60
61
62
63
64
65 public PaymentRequestItem(PurchaseOrderItem poi, PaymentRequestDocument preq) {
66 this(poi, preq, new HashMap<String, ExpiredOrClosedAccountEntry>());
67 }
68
69
70
71
72
73
74
75
76 public PaymentRequestItem(PurchaseOrderItem poi, PaymentRequestDocument preq, HashMap<String, ExpiredOrClosedAccountEntry> expiredOrClosedAccountList) {
77
78
79 PurApObjectUtils.populateFromBaseClass(PurApItemBase.class, poi, this, PurapConstants.PREQ_ITEM_UNCOPYABLE_FIELDS);
80
81 setItemDescription(poi.getItemDescription());
82
83
84 resetAccount();
85
86
87 List accounts = new ArrayList();
88 for (PurApAccountingLine account : poi.getSourceAccountingLines()) {
89 PurchaseOrderAccount poa = (PurchaseOrderAccount) account;
90
91
92 SpringContext.getBean(AccountsPayableService.class).processExpiredOrClosedAccount(poa, expiredOrClosedAccountList);
93
94
95 if (poa.getAmount().isZero()) {
96 if (SpringContext.getBean(AccountsPayableService.class).canCopyAccountingLinesWithZeroAmount()) {
97 accounts.add(new PaymentRequestAccount(this, poa));
98 }
99 } else {
100 accounts.add(new PaymentRequestAccount(this, poa));
101 }
102 }
103
104 this.setSourceAccountingLines(accounts);
105 this.getUseTaxItems().clear();
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 if ((ObjectUtils.isNotNull(this.getItemType()) && this.getItemType().isAmountBasedGeneralLedgerIndicator())) {
122
123 this.setItemUnitPrice(null);
124 }
125
126
127 this.purchaseOrderItemUnitPrice = poi.getItemUnitPrice();
128
129
130
131 this.setPurapDocumentIdentifier(preq.getPurapDocumentIdentifier());
132 this.setPurapDocument(preq);
133 }
134
135
136
137
138
139
140
141 @Override
142 public PurchaseOrderItem getPurchaseOrderItem() {
143 if (ObjectUtils.isNotNull(this.getPurapDocumentIdentifier())) {
144 if (ObjectUtils.isNull(this.getPaymentRequest())) {
145 this.refreshReferenceObject(PurapPropertyConstants.PURAP_DOC);
146 }
147 }
148
149
150 if (getPaymentRequest() != null) {
151 PurchaseOrderDocument po = getPaymentRequest().getPurchaseOrderDocument();
152 PurchaseOrderItem poi = null;
153 if (this.getItemType().isLineItemIndicator()) {
154 List<PurchaseOrderItem> items = po.getItems();
155 poi = items.get(this.getItemLineNumber().intValue() - 1);
156
157
158
159
160
161
162
163 } else {
164 poi = (PurchaseOrderItem) SpringContext.getBean(PurapService.class).getBelowTheLineByType(po, this.getItemType());
165 }
166 if (poi != null) {
167 return poi;
168 } else {
169 if (LOG.isDebugEnabled()) {
170 LOG.debug("getPurchaseOrderItem() Returning null because PurchaseOrderItem object for line number" + getItemLineNumber() + "or itemType " + getItemTypeCode() + " is null");
171 }
172 return null;
173 }
174 } else {
175
176 LOG.error("getPurchaseOrderItem() Returning null because paymentRequest object is null");
177 throw new PurError("Payment Request Object in Purchase Order item line number " + getItemLineNumber() + "or itemType " + getItemTypeCode() + " is null");
178 }
179 }
180
181 public KualiDecimal getPoOutstandingAmount() {
182 PurchaseOrderItem poi = getPurchaseOrderItem();
183 if (ObjectUtils.isNull(this.getPurchaseOrderItemUnitPrice()) || KualiDecimal.ZERO.equals(this.getPurchaseOrderItemUnitPrice())) {
184 return null;
185 } else {
186 return this.getPoOutstandingAmount(poi);
187 }
188 }
189
190 private KualiDecimal getPoOutstandingAmount(PurchaseOrderItem poi) {
191 if (poi == null) {
192 return KualiDecimal.ZERO;
193 } else {
194 return poi.getItemOutstandingEncumberedAmount();
195 }
196 }
197
198 public KualiDecimal getPoOriginalAmount() {
199 PurchaseOrderItem poi = getPurchaseOrderItem();
200 if (poi == null) {
201 return null;
202 } else {
203 return poi.getExtendedPrice();
204 }
205 }
206
207
208
209
210
211
212
213 @Deprecated
214 public void setPoOutstandingAmount(KualiDecimal amount) {
215
216 }
217
218
219 public KualiDecimal getPoOutstandingQuantity() {
220 PurchaseOrderItem poi = getPurchaseOrderItem();
221 if (poi == null) {
222 return null;
223 } else {
224 if (PurapConstants.ItemTypeCodes.ITEM_TYPE_SERVICE_CODE.equals(this.getItemTypeCode())) {
225 return null;
226 } else {
227 return poi.getOutstandingQuantity();
228 }
229 }
230 }
231
232
233
234
235
236
237
238 @Deprecated
239 public void setPoOutstandingQuantity(KualiDecimal qty) {
240
241 }
242
243 public BigDecimal getPurchaseOrderItemUnitPrice() {
244 return purchaseOrderItemUnitPrice;
245 }
246
247 public BigDecimal getOriginalAmountfromPO() {
248 return purchaseOrderItemUnitPrice;
249 }
250
251 public void setOriginalAmountfromPO(BigDecimal purchaseOrderItemUnitPrice) {
252
253 }
254
255 public void setPurchaseOrderItemUnitPrice(BigDecimal purchaseOrderItemUnitPrice) {
256 this.purchaseOrderItemUnitPrice = purchaseOrderItemUnitPrice;
257 }
258
259 public KualiDecimal getItemOutstandingInvoiceAmount() {
260 return itemOutstandingInvoiceAmount;
261 }
262
263 public void setItemOutstandingInvoiceAmount(KualiDecimal itemOutstandingInvoiceAmount) {
264 this.itemOutstandingInvoiceAmount = itemOutstandingInvoiceAmount;
265 }
266
267 public KualiDecimal getItemOutstandingInvoiceQuantity() {
268 return itemOutstandingInvoiceQuantity;
269 }
270
271 public void setItemOutstandingInvoiceQuantity(KualiDecimal itemOutstandingInvoiceQuantity) {
272 this.itemOutstandingInvoiceQuantity = itemOutstandingInvoiceQuantity;
273 }
274
275 public PaymentRequestDocument getPaymentRequest() {
276 if (ObjectUtils.isNotNull(getPurapDocumentIdentifier())) {
277 if (ObjectUtils.isNull(getPurapDocument())) {
278 this.refreshReferenceObject(PurapPropertyConstants.PURAP_DOC);
279 }
280 }
281 return super.getPurapDocument();
282 }
283
284 public void setPaymentRequest(PaymentRequestDocument paymentRequest) {
285 this.setPurapDocument(paymentRequest);
286 }
287
288 public void generateAccountListFromPoItemAccounts(List<PurApAccountingLine> accounts) {
289 for (PurApAccountingLine line : accounts) {
290 PurchaseOrderAccount poa = (PurchaseOrderAccount) line;
291 if (!line.isEmpty()) {
292 getSourceAccountingLines().add(new PaymentRequestAccount(this, poa));
293 }
294 }
295 }
296
297
298
299
300 @Override
301 public Class getAccountingLineClass() {
302 return PaymentRequestAccount.class;
303 }
304
305 public boolean isDisplayOnPreq() {
306 PurchaseOrderItem poi = getPurchaseOrderItem();
307 if (ObjectUtils.isNull(poi)) {
308 LOG.debug("poi was null");
309 return false;
310 }
311
312
313 if (!poi.isItemActiveIndicator()) {
314 if (LOG.isDebugEnabled()) {
315 LOG.debug("poi was not active: " + poi.toString());
316 }
317 return false;
318 }
319
320 ItemType poiType = poi.getItemType();
321
322 if (poiType.isQuantityBasedGeneralLedgerIndicator()) {
323 if (poi.getItemQuantity().isGreaterThan(poi.getItemInvoicedTotalQuantity())) {
324 return true;
325 } else {
326 if (ObjectUtils.isNotNull(this.getItemQuantity()) && this.getItemQuantity().isGreaterThan(KualiDecimal.ZERO)) {
327 return true;
328 }
329 }
330
331 return false;
332 } else {
333 if (poi.getItemOutstandingEncumberedAmount().isGreaterThan(KualiDecimal.ZERO)) {
334 return true;
335 } else {
336 if (PurApItemUtils.isNonZeroExtended(this)) {
337 return true;
338 }
339 return false;
340 }
341
342 }
343 }
344
345
346
347
348
349
350 @Override
351 public void resetAccount() {
352 super.resetAccount();
353 this.getNewSourceLine().setAmount(null);
354 this.getNewSourceLine().setAccountLinePercent(new BigDecimal(0));
355 }
356
357
358
359
360 public void addToUnitPrice(BigDecimal addThisValue) {
361 if (getItemUnitPrice() == null) {
362 setItemUnitPrice(BigDecimal.ZERO);
363 }
364 BigDecimal addedPrice = getItemUnitPrice().add(addThisValue);
365 setItemUnitPrice(addedPrice);
366 }
367
368 public void addToExtendedPrice(KualiDecimal addThisValue) {
369 if (getExtendedPrice() == null) {
370 setExtendedPrice(KualiDecimal.ZERO);
371 }
372 KualiDecimal addedPrice = getExtendedPrice().add(addThisValue);
373 setExtendedPrice(addedPrice);
374 }
375
376 @Override
377 public Class getUseTaxClass() {
378 return PaymentRequestItemUseTax.class;
379 }
380
381
382
383
384
385
386
387 public PaymentRequestItem(OleInvoiceItem poi, OlePaymentRequestDocument preq) {
388 this(poi, preq, new HashMap<String, ExpiredOrClosedAccountEntry>());
389 }
390
391
392
393
394
395
396
397
398
399 public PaymentRequestItem(OleInvoiceItem poi, OlePaymentRequestDocument preq, HashMap<String, ExpiredOrClosedAccountEntry> expiredOrClosedAccountList) {
400
401
402 PurApObjectUtils.populateFromBaseClass(PurApItemBase.class, poi, this, PurapConstants.PREQ_ITEM_UNCOPYABLE_FIELDS);
403
404 setItemDescription(poi.getItemDescription());
405
406
407 resetAccount();
408
409
410 List accounts = new ArrayList();
411
412 for (PurApAccountingLine account : poi.getSourceAccountingLines()) {
413 InvoiceAccount poa = (InvoiceAccount) account;
414
415
416 SpringContext.getBean(AccountsPayableService.class).processExpiredOrClosedAccount(poa, expiredOrClosedAccountList);
417
418
419 if (poa.getAmount().isZero()) {
420 if (SpringContext.getBean(AccountsPayableService.class).canCopyAccountingLinesWithZeroAmount()) {
421 accounts.add(new PaymentRequestAccount(this, poa));
422 }
423 } else {
424 accounts.add(new PaymentRequestAccount(this, poa));
425 }
426 }
427
428 this.setSourceAccountingLines(accounts);
429 this.getUseTaxItems().clear();
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445 if ((ObjectUtils.isNotNull(this.getItemType()) && this.getItemType().isAmountBasedGeneralLedgerIndicator())) {
446
447
448 }
449
450
451
452 this.purchaseOrderItemUnitPrice = poi.getPurchaseOrderItem()!=null ? poi.getPurchaseOrderItem().getItemUnitPrice() : null;
453
454
455
456 this.setPurapDocumentIdentifier(preq.getPurapDocumentIdentifier());
457 this.setPurapDocument(preq);
458 }
459
460 }