1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.ole.select.document;
18
19 import org.apache.commons.lang.StringEscapeUtils;
20 import org.apache.commons.lang.StringUtils;
21 import org.kuali.ole.DocumentUniqueIDPrefix;
22 import org.kuali.ole.docstore.common.client.*;
23 import org.kuali.ole.docstore.common.document.Bib;
24 import org.kuali.ole.docstore.common.document.BibMarc;
25 import org.kuali.ole.module.purap.PurapConstants;
26 import org.kuali.ole.module.purap.PurapPropertyConstants;
27 import org.kuali.ole.module.purap.PurapWorkflowConstants;
28 import org.kuali.ole.module.purap.businessobject.CreditMemoItem;
29 import org.kuali.ole.module.purap.businessobject.PurchaseOrderType;
30 import org.kuali.ole.module.purap.document.VendorCreditMemoDocument;
31 import org.kuali.ole.module.purap.document.service.PaymentRequestService;
32 import org.kuali.ole.module.purap.document.validation.event.AttributedContinuePurapEvent;
33 import org.kuali.ole.select.OleSelectConstant;
34 import org.kuali.ole.select.businessobject.OleCreditMemoItem;
35 import org.kuali.ole.select.businessobject.OleInvoiceType;
36 import org.kuali.ole.select.document.service.OleCreditMemoService;
37 import org.kuali.ole.select.document.service.OlePurchaseOrderDocumentHelperService;
38 import org.kuali.ole.select.service.BibInfoService;
39 import org.kuali.ole.select.service.BibInfoWrapperService;
40 import org.kuali.ole.select.service.FileProcessingService;
41 import org.kuali.ole.select.service.impl.BibInfoServiceImpl;
42 import org.kuali.ole.sys.OLEConstants;
43 import org.kuali.ole.sys.context.SpringContext;
44 import org.kuali.ole.vnd.businessobject.OleExchangeRate;
45 import org.kuali.ole.vnd.businessobject.VendorAlias;
46 import org.kuali.rice.core.api.config.property.ConfigurationService;
47 import org.kuali.rice.core.api.util.type.KualiDecimal;
48 import org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent;
49 import org.kuali.rice.krad.service.BusinessObjectService;
50 import org.kuali.rice.krad.util.ObjectUtils;
51
52 import java.math.BigDecimal;
53 import java.math.RoundingMode;
54 import java.util.*;
55
56
57
58
59 public class OleVendorCreditMemoDocument extends VendorCreditMemoDocument {
60 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleVendorCreditMemoDocument.class);
61
62
63 private static transient BusinessObjectService businessObjectService;
64 private static transient ConfigurationService kualiConfigurationService;
65 private static transient FileProcessingService fileProcessingService;
66 private static transient BibInfoWrapperService bibInfoWrapperService;
67 private static transient BibInfoService bibInfoService;
68
69 private boolean prorateQty;
70 private boolean prorateDollar;
71 private boolean prorateManual;
72 private boolean noProrate;
73
74 private String prorateBy;
75 private boolean currencyTypeIndicator = true;
76
77 private String vendorAliasName;
78 public String getVendorAliasName() {
79 return vendorAliasName;
80 }
81
82 public void setVendorAliasName(String vendorAliasName) {
83 this.vendorAliasName = vendorAliasName;
84 }
85
86 private Integer invoiceIdentifier;
87
88 public OleVendorCreditMemoDocument() {
89 super();
90 }
91
92 @Override
93 public Class getItemClass() {
94 return OleCreditMemoItem.class;
95 }
96
97 public static ConfigurationService getConfigurationService() {
98 if (kualiConfigurationService == null) {
99 kualiConfigurationService = SpringContext.getBean(ConfigurationService.class);
100 }
101 return kualiConfigurationService;
102 }
103
104 public static void setConfigurationService(ConfigurationService kualiConfigurationService) {
105 OleVendorCreditMemoDocument.kualiConfigurationService = kualiConfigurationService;
106 }
107
108 public String getBibeditorCreateURL() {
109 String bibeditorCreateURL = getConfigurationService().getPropertyValueAsString(
110 OLEConstants.BIBEDITOR_CREATE_URL_KEY);
111 return bibeditorCreateURL;
112 }
113
114 public String getBibeditorEditURL() {
115 String bibeditorEditURL = getConfigurationService().getPropertyValueAsString(OLEConstants.BIBEDITOR_URL_KEY);
116 return bibeditorEditURL;
117 }
118
119 public String getBibeditorViewURL() {
120 String bibeditorViewURL = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(
121 OLEConstants.DOCSTORE_APP_URL_KEY);
122 return bibeditorViewURL;
123 }
124 public String getDublinEditorEditURL() {
125 return SpringContext.getBean(OlePurchaseOrderDocumentHelperService.class).getDublinEditorEditURL();
126
127 }
128 public String getDublinEditorViewURL() {
129 return SpringContext.getBean(OlePurchaseOrderDocumentHelperService.class).getDublinEditorViewURL();
130 }
131 public static FileProcessingService getFileProcessingService() {
132 if (fileProcessingService == null) {
133 fileProcessingService = SpringContext.getBean(FileProcessingService.class);
134 }
135 return fileProcessingService;
136 }
137
138 public static void setFileProcessingService(FileProcessingService fileProcessingService) {
139 OleVendorCreditMemoDocument.fileProcessingService = fileProcessingService;
140 }
141
142 public static BibInfoService getBibInfoService() {
143 if (bibInfoService == null) {
144 bibInfoService = SpringContext.getBean(BibInfoServiceImpl.class);
145 }
146 return bibInfoService;
147 }
148
149 public String getMarcXMLFileDirLocation() throws Exception {
150 String externaleDirectory = getFileProcessingService().getMarcXMLFileDirLocation();
151 return externaleDirectory;
152 }
153
154 public static BibInfoWrapperService getBibInfoWrapperService() {
155 if (bibInfoWrapperService == null) {
156 bibInfoWrapperService = SpringContext.getBean(BibInfoWrapperService.class);
157 }
158 return bibInfoWrapperService;
159 }
160
161 public static void setBibInfoWrapperService(BibInfoWrapperService bibInfoWrapperService) {
162 OleVendorCreditMemoDocument.bibInfoWrapperService = bibInfoWrapperService;
163 }
164
165 public boolean isProrateQty() {
166 return prorateQty;
167 }
168
169 public void setProrateQty(boolean prorateQty) {
170 this.prorateQty = prorateQty;
171 }
172
173 public boolean isProrateDollar() {
174 return prorateDollar;
175 }
176
177 public void setProrateDollar(boolean prorateDollar) {
178 this.prorateDollar = prorateDollar;
179 }
180
181 public boolean isProrateManual() {
182 return prorateManual;
183 }
184
185 public void setProrateManual(boolean prorateManual) {
186 this.prorateManual = prorateManual;
187 }
188
189 public boolean isNoProrate() {
190 return noProrate;
191 }
192
193 public void setNoProrate(boolean noProrate) {
194 this.noProrate = noProrate;
195 }
196
197 public String getProrateBy() {
198 return prorateBy;
199 }
200
201 public void setProrateBy(String prorateBy) {
202 this.prorateBy = prorateBy;
203 }
204
205
206
207
208
209
210 public boolean getIsSaved() {
211 if (this.getDocumentHeader().getWorkflowDocument().isSaved()
212 || this.getDocumentHeader().getWorkflowDocument().isInitiated()) {
213 return true;
214 }
215 return false;
216 }
217
218 @Override
219 public void prepareForSave(KualiDocumentEvent event) {
220
221 if (event instanceof AttributedContinuePurapEvent) {
222 SpringContext.getBean(OleCreditMemoService.class).populateDocumentAfterInit(this);
223 }
224 customPrepareForSave(event);
225 }
226
227 @Override
228 public void processAfterRetrieve() {
229 super.processAfterRetrieve();
230
231 try {
232 if (this.getVendorAliasName() == null) {
233 populateVendorAliasName();
234 }
235 Map purchaseOrderTypeIdMap = new HashMap();
236 if (this.getPurchaseOrderTypeId() != null) {
237 purchaseOrderTypeIdMap.put("purchaseOrderTypeId", this.getPurchaseOrderTypeId());
238 List<PurchaseOrderType> purchaseOrderTypeDocumentList = (List) getBusinessObjectService().findMatching(PurchaseOrderType.class, purchaseOrderTypeIdMap);
239 if (purchaseOrderTypeDocumentList != null && purchaseOrderTypeDocumentList.size() > 0) {
240 PurchaseOrderType purchaseOrderTypeDoc = purchaseOrderTypeDocumentList.get(0);
241 this.setOrderType(purchaseOrderTypeDoc);
242 }
243 }
244 List<BigDecimal> newUnitPriceList = new ArrayList<BigDecimal>();
245 BigDecimal newUnitPrice = new BigDecimal(0);
246 BigDecimal hundred = new BigDecimal(100);
247 List<OleCreditMemoItem> item = this.getItems();
248
249 for (int i = 0; item.size() > i; i++) {
250 OleCreditMemoItem items = (OleCreditMemoItem) this.getItem(i);
251 if ((items.getItemType().isQuantityBasedGeneralLedgerIndicator())) {
252 newUnitPrice = items.getItemUnitPrice().abs();
253
254 if (items.getItemUnitPrice() != null && items.getExtendedPrice() != null) {
255 items.setItemSurcharge(items.getExtendedPrice().bigDecimalValue()
256 .subtract(newUnitPrice.multiply(items.getItemQuantity().bigDecimalValue()))
257 .setScale(4, RoundingMode.HALF_UP));
258 } else {
259 items.setItemSurcharge(BigDecimal.ZERO);
260 }
261 }
262 }
263 if (this.getVendorDetail().getCurrencyType()!=null){
264 if(this.getVendorDetail().getCurrencyType().getCurrencyType().equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME)){
265 currencyTypeIndicator=true;
266 }
267 else{
268 currencyTypeIndicator=false;
269 }
270 }
271
272 if (this.getVendorDetail() != null && (!currencyTypeIndicator)) {
273 Long currencyTypeId = this.getVendorDetail().getCurrencyType().getCurrencyTypeId();
274 Map documentNumberMap = new HashMap();
275 documentNumberMap.put(OleSelectConstant.CURRENCY_TYPE_ID, currencyTypeId);
276 List<OleExchangeRate> exchangeRateList = (List) getBusinessObjectService().findMatchingOrderBy(OleExchangeRate.class, documentNumberMap, OleSelectConstant.EXCHANGE_RATE_DATE, false);
277 Iterator iterator = exchangeRateList.iterator();
278 for (OleCreditMemoItem items : item) {
279 iterator = exchangeRateList.iterator();
280 if (iterator.hasNext()) {
281 OleExchangeRate tempOleExchangeRate = (OleExchangeRate) iterator.next();
282 items.setItemExchangeRate(new KualiDecimal(tempOleExchangeRate.getExchangeRate()));
283 }
284 }
285
286 }
287
288 List<OleCreditMemoItem> items = this.getItems();
289
290
291 String itemDescription = "";
292
293 for (OleCreditMemoItem singleItem : items) {
294 if (LOG.isDebugEnabled()) {
295 LOG.debug("Title id while retriving ------>" + singleItem.getItemTitleId());
296 }
297
298
299 if (singleItem.getItemTitleId() != null) {
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315 Bib bib = new BibMarc();
316 DocstoreClientLocator docstoreClientLocator=new DocstoreClientLocator();
317 if(singleItem.getItemTitleId()!=null && singleItem.getItemTitleId()!=""){
318 bib= docstoreClientLocator.getDocstoreClient().retrieveBib(singleItem.getItemTitleId());
319 singleItem.setBibUUID(bib.getId());
320 singleItem.setDocFormat(DocumentUniqueIDPrefix.getBibFormatType(singleItem.getItemTitleId()));
321 }
322
323 itemDescription = ((bib.getTitle() != null && !bib.getTitle().isEmpty()) ? bib.getTitle() + "," : "") +
324 ((bib.getAuthor() != null && !bib.getAuthor().isEmpty()) ? bib.getAuthor() + "," : "") +
325 ((bib.getPublisher() != null && !bib.getPublisher().isEmpty()) ? bib.getPublisher() + "," : "") +
326 ((bib.getIsbn() != null && !bib.getIsbn().isEmpty()) ? bib.getIsbn() + "," : "");
327 if (!itemDescription.isEmpty()) {
328 itemDescription = itemDescription.lastIndexOf(",") < 0 ? itemDescription :
329 itemDescription.substring(0, itemDescription.lastIndexOf(","));
330 }
331 StringEscapeUtils stringEscapeUtils = new StringEscapeUtils();
332 itemDescription = stringEscapeUtils.unescapeXml(itemDescription);
333 singleItem.setItemDescription(itemDescription);
334 }
335 }
336 if (this.getProrateBy() != null) {
337 this.setProrateQty(this.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY));
338 this.setProrateManual(this.getProrateBy().equals(OLEConstants.MANUAL_PRORATE));
339 this.setProrateDollar(this.getProrateBy().equals(OLEConstants.PRORATE_BY_DOLLAR));
340 this.setNoProrate(this.getProrateBy().equals(OLEConstants.NO_PRORATE));
341 }
342
343 } catch (Exception e) {
344 LOG.error("Exception in OleVendorCreditMemoDocument:processAfterRetrieve " + e);
345 throw new RuntimeException(e);
346 }
347 }
348
349 public void creditMemoCalculation(OleCreditMemoItem item) {
350 item.setItemUnitPrice(item.getPoUnitPrice());
351 BigDecimal calcExtendedPrice = item.getItemUnitPrice().multiply(item.getItemQuantity().bigDecimalValue());
352 KualiDecimal extendedPrice = new KualiDecimal(calcExtendedPrice.setScale(KualiDecimal.SCALE, KualiDecimal.ROUND_BEHAVIOR));
353 item.setPoTotalAmount(extendedPrice);
354 }
355
356 private void setItemDescription(OleCreditMemoItem singleItem) throws Exception {
357 if (singleItem.getOleOrderRecord() != null) {
358 Map<String, ?> bibAssociatedFieldValuesMap = singleItem.getOleOrderRecord().getOleBibRecord().getBibAssociatedFieldsValueMap();
359 List titleList = (List) bibAssociatedFieldValuesMap.get("Title_search");
360 String title = titleList != null && !titleList.isEmpty() ? (String) (titleList).get(0) : null;
361 List authorList = (List) bibAssociatedFieldValuesMap.get("Author_search");
362 String author = authorList != null && !authorList.isEmpty() ? (String) (authorList).get(0) : null;
363 List publisherList = (List) bibAssociatedFieldValuesMap.get("Publisher_search");
364 String publisher = publisherList != null && !publisherList.isEmpty() ? (String) (publisherList).get(0) : null;
365 List isbnList = (List) bibAssociatedFieldValuesMap.get("020a");
366 String isbn = isbnList != null && !isbnList.isEmpty() ? (String) (isbnList).get(0) : null;
367
368 singleItem.setBibUUID(singleItem.getOleOrderRecord().getOleBibRecord().getBibUUID());
369 singleItem.setItemDescription(title + "," + author + "," + publisher + "," + isbn);
370 }
371 }
372
373 @Override
374 public OlePaymentRequestDocument getPaymentRequestDocument() {
375 if ((ObjectUtils.isNull(paymentRequestDocument)) && (ObjectUtils.isNotNull(getPaymentRequestIdentifier()))) {
376 setPaymentRequestDocument(SpringContext.getBean(PaymentRequestService.class).getPaymentRequestById(getPaymentRequestIdentifier()));
377 }
378 return (OlePaymentRequestDocument) this.paymentRequestDocument;
379 }
380
381
382
383
384
385
386
387
388 @Override
389 public boolean answerSplitNodeQuestion(String nodeName) throws UnsupportedOperationException {
390 if (nodeName.equals(OLEConstants.OlePaymentRequest.HAS_INVOICE_TYPE)) {
391 return hasInvoiceType();
392 }
393 if (nodeName.equals(OLEConstants.OlePaymentRequest.HAS_PREPAID_INVOICE_TYPE)) {
394 return hasPrepaidInvoiceType();
395 }
396 if (nodeName.equals(PurapWorkflowConstants.REQUIRES_IMAGE_ATTACHMENT)) {
397 return requiresAccountsPayableReviewRouting();
398 }
399 throw new UnsupportedOperationException("Cannot answer split question for this node you call \"" + nodeName + "\"");
400 }
401
402
403 private boolean hasInvoiceType() {
404 if (this.getInvoiceTypeId() != null) {
405 return true;
406 }
407 return false;
408 }
409
410 private boolean hasPrepaidInvoiceType() {
411 if (this.getInvoiceTypeId() != null) {
412 Map<String, String> invoiceMap = new HashMap<String, String>();
413 invoiceMap.put("invoiceTypeId", this.getInvoiceTypeId().toString());
414 OleInvoiceType invoiceType = this.getBusinessObjectService().findByPrimaryKey(OleInvoiceType.class, invoiceMap);
415 if (invoiceType != null &&
416 invoiceType.getInvoiceType().equals("Prepay") ||
417 invoiceType.getInvoiceType().equals("Deposit")) {
418 return true;
419 }
420 }
421 return false;
422 }
423
424 @Override
425 public KualiDecimal getGrandTotal() {
426 KualiDecimal grandTotal = KualiDecimal.ZERO;
427
428 if ((this.prorateBy != null) && (this.prorateBy.equals(OLEConstants.PRORATE_BY_QTY) || this.prorateBy.equals(OLEConstants.PRORATE_BY_DOLLAR) || this.prorateBy.equals(OLEConstants.MANUAL_PRORATE))) {
429
430
431 for (CreditMemoItem item : (List<CreditMemoItem>) getItems()) {
432 item.refreshReferenceObject(PurapPropertyConstants.ITEM_TYPE);
433
434 if (item.getTotalAmount() != null) {
435
436 if (!StringUtils.equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_RESTCK_FEE_CODE, item.getItemTypeCode()) && !StringUtils.equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_MISC_CRDT_CODE, item.getItemTypeCode())) {
437 if (item.getExtendedPrice() != null) {
438 grandTotal = grandTotal.add(item.getExtendedPrice());
439 }
440 }
441 }
442 }
443 } else {
444
445 for (CreditMemoItem item : (List<CreditMemoItem>) getItems()) {
446 if (!StringUtils.equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_RESTCK_FEE_CODE, item.getItemTypeCode()) && !StringUtils.equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_MISC_CRDT_CODE, item.getItemTypeCode())) {
447 if (item.getExtendedPrice() != null) {
448 grandTotal = grandTotal.add(item.getExtendedPrice());
449 }
450 }
451 }
452 }
453
454 return grandTotal;
455 }
456
457 @Override
458 public KualiDecimal getTotalDollarAmountAllItems(String[] excludedTypes) {
459 if ((this.prorateBy != null) && (this.prorateBy.equals(OLEConstants.PRORATE_BY_QTY) || this.prorateBy.equals(OLEConstants.PRORATE_BY_DOLLAR) || this.prorateBy.equals(OLEConstants.MANUAL_PRORATE))) {
460 return getTotalDollarAmountWithExclusions(excludedTypes, false);
461 } else {
462 return getTotalDollarAmountWithExclusions(excludedTypes, true);
463 }
464 }
465
466 @Override
467 public KualiDecimal getTotalPreTaxDollarAmountAllItems(String[] excludedTypes) {
468 if ((this.prorateBy != null) && (this.prorateBy.equals(OLEConstants.PRORATE_BY_QTY) || this.prorateBy.equals(OLEConstants.PRORATE_BY_DOLLAR) || this.prorateBy.equals(OLEConstants.MANUAL_PRORATE))) {
469 return getTotalPreTaxDollarAmountWithExclusions(excludedTypes, false);
470 } else {
471 return getTotalPreTaxDollarAmountWithExclusions(excludedTypes, true);
472 }
473 }
474
475 @Override
476 public KualiDecimal getTotalPreTaxDollarAmountAboveLineItems() {
477 if ((this.prorateBy != null) && (this.prorateBy.equals(OLEConstants.PRORATE_BY_QTY) || this.prorateBy.equals(OLEConstants.PRORATE_BY_DOLLAR) || this.prorateBy.equals(OLEConstants.MANUAL_PRORATE))) {
478 KualiDecimal addChargeItem = KualiDecimal.ZERO;
479 KualiDecimal lineItemPreTaxTotal = KualiDecimal.ZERO;
480 KualiDecimal prorateSurcharge = KualiDecimal.ZERO;
481 List<OleCreditMemoItem> item = this.getItems();
482 for (OleCreditMemoItem items : item) {
483 if (items.getItemType().isQuantityBasedGeneralLedgerIndicator() && items.getExtendedPrice() != null && items.getExtendedPrice().compareTo(KualiDecimal.ZERO) != 0) {
484 if (items.getItemSurcharge() != null) {
485 prorateSurcharge = new KualiDecimal(items.getItemSurcharge());
486 }
487 addChargeItem = addChargeItem.add(items.getExtendedPrice().subtract(prorateSurcharge.multiply(items.getItemQuantity())));
488 }
489 }
490 lineItemPreTaxTotal = addChargeItem;
491 return lineItemPreTaxTotal;
492 } else {
493 return super.getTotalPreTaxDollarAmountAboveLineItems();
494 }
495 }
496
497 public Integer getInvoiceIdentifier() {
498 return invoiceIdentifier;
499 }
500
501 public void setInvoiceIdentifier(Integer invoiceIdentifier) {
502 this.invoiceIdentifier = invoiceIdentifier;
503 }
504
505 private void populateVendorAliasName() {
506 Map vendorDetailMap = new HashMap();
507 vendorDetailMap.put(OLEConstants.VENDOR_HEADER_IDENTIFIER, this.getVendorHeaderGeneratedIdentifier());
508 vendorDetailMap.put(OLEConstants.VENDOR_DETAIL_IDENTIFIER, this.getVendorDetailAssignedIdentifier());
509 List<VendorAlias> vendorDetailList = (List) getBusinessObjectService().findMatching(VendorAlias.class, vendorDetailMap);
510 if (vendorDetailList != null && vendorDetailList.size() > 0) {
511 this.setVendorAliasName(vendorDetailList.get(0).getVendorAliasName());
512 }
513 }
514 }