1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.select.document.web.struts;
17
18 import org.apache.commons.lang.StringEscapeUtils;
19 import org.apache.struts.action.ActionForm;
20 import org.apache.struts.action.ActionForward;
21 import org.apache.struts.action.ActionMapping;
22 import org.kuali.ole.module.purap.PurapConstants;
23 import org.kuali.ole.module.purap.PurapPropertyConstants;
24 import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
25 import org.kuali.ole.module.purap.businessobject.PurApItem;
26 import org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocument;
27 import org.kuali.ole.module.purap.document.service.OlePurapService;
28 import org.kuali.ole.module.purap.document.validation.event.AttributedCalculateAccountsPayableEvent;
29 import org.kuali.ole.module.purap.document.validation.event.AttributedContinuePurapEvent;
30 import org.kuali.ole.module.purap.document.web.struts.PurchasingAccountsPayableFormBase;
31 import org.kuali.ole.module.purap.document.web.struts.PurchasingFormBase;
32 import org.kuali.ole.module.purap.document.web.struts.VendorCreditMemoAction;
33 import org.kuali.ole.pojo.OleBibRecord;
34 import org.kuali.ole.pojo.OleEditorResponse;
35 import org.kuali.ole.select.OleSelectConstant;
36 import org.kuali.ole.select.businessobject.BibInfoBean;
37 import org.kuali.ole.select.businessobject.OleCreditMemoItem;
38 import org.kuali.ole.select.businessobject.OleDocstoreResponse;
39 import org.kuali.ole.select.document.OleVendorCreditMemoDocument;
40 import org.kuali.ole.select.document.service.OleCreditMemoService;
41 import org.kuali.ole.select.document.validation.event.OleCreditMemoDescEvent;
42 import org.kuali.ole.select.document.validation.event.OleForeignCurrencyCreditMemoEvent;
43 import org.kuali.ole.select.service.BibInfoWrapperService;
44 import org.kuali.ole.select.service.FileProcessingService;
45 import org.kuali.ole.select.service.impl.BibInfoWrapperServiceImpl;
46 import org.kuali.ole.sys.OLEConstants;
47 import org.kuali.ole.sys.OLEPropertyConstants;
48 import org.kuali.ole.sys.context.SpringContext;
49 import org.kuali.ole.sys.document.validation.event.AddAccountingLineEvent;
50 import org.kuali.ole.vnd.businessobject.OleExchangeRate;
51 import org.kuali.rice.core.api.util.type.KualiDecimal;
52 import org.kuali.rice.krad.service.BusinessObjectService;
53 import org.kuali.rice.krad.service.KualiRuleService;
54 import org.kuali.rice.krad.service.PersistenceService;
55 import org.kuali.rice.krad.util.GlobalVariables;
56 import org.kuali.rice.krad.util.ObjectUtils;
57
58 import javax.servlet.http.HttpServletRequest;
59 import javax.servlet.http.HttpServletResponse;
60 import java.math.BigDecimal;
61 import java.math.RoundingMode;
62 import java.util.HashMap;
63 import java.util.Iterator;
64 import java.util.List;
65 import java.util.Map;
66
67
68 public class OleVendorCreditMemoAction extends VendorCreditMemoAction {
69
70 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleVendorCreditMemoAction.class);
71
72
73
74
75 @Override
76 public ActionForward calculate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
77
78 OleVendorCreditMemoForm creditForm = (OleVendorCreditMemoForm) form;
79 OleVendorCreditMemoDocument creditDoc = (OleVendorCreditMemoDocument) creditForm.getDocument();
80 List<OleCreditMemoItem> item = creditDoc.getItems();
81
82 if (!(creditDoc.getVendorDetail() == null || (creditDoc.getVendorDetail() != null && creditDoc
83 .getVendorDetail().getVendorHeader().getVendorForeignIndicator() != true))) {
84
85 LOG.debug("###########Foreign Currency Field Calculation###########");
86 for (int i = 0; item.size() > i; i++) {
87 OleCreditMemoItem items = (OleCreditMemoItem) creditDoc.getItem(i);
88 Long id = creditDoc.getVendorDetail().getCurrencyType().getCurrencyTypeId();
89 Map documentNumberMap = new HashMap();
90 documentNumberMap.put(OleSelectConstant.CURRENCY_TYPE_ID, id);
91 BusinessObjectService businessObjectService = SpringContext.getBean(BusinessObjectService.class);
92 List<OleExchangeRate> exchangeRateList = (List) businessObjectService.findMatchingOrderBy(OleExchangeRate.class, documentNumberMap, OleSelectConstant.EXCHANGE_RATE_DATE, false);
93 Iterator iterator = exchangeRateList.iterator();
94 if (iterator.hasNext()) {
95 OleExchangeRate tempOleExchangeRate = (OleExchangeRate) iterator.next();
96 items.setItemExchangeRate(new KualiDecimal(tempOleExchangeRate.getExchangeRate()));
97
98 }
99 if ((items.getItemType().isQuantityBasedGeneralLedgerIndicator())) {
100 boolean rulePassed = getKualiRuleService().applyRules(new OleForeignCurrencyCreditMemoEvent(creditDoc, items));
101 if (rulePassed) {
102 SpringContext.getBean(OlePurapService.class).calculateForeignCurrency(items);
103 if (items.getItemExchangeRate() != null && items.getItemForeignUnitCost() != null) {
104 items.setItemUnitCostUSD(new KualiDecimal(items.getItemForeignUnitCost().bigDecimalValue().divide(items.getItemExchangeRate().bigDecimalValue(), 4, RoundingMode.HALF_UP)));
105 items.setItemUnitPrice(items.getItemUnitCostUSD().bigDecimalValue());
106 }
107 }
108 } else {
109 if (items.getItemExchangeRate() != null && items.getForeignCurrencyExtendedPrice() != null) {
110
111 if (items.isAdditionalChargeUsd()) {
112 items.setItemUnitPrice(items.getForeignCurrencyExtendedPrice().bigDecimalValue());
113 } else {
114 items.setItemUnitPrice(items.getForeignCurrencyExtendedPrice().bigDecimalValue().divide(items.getItemExchangeRate().bigDecimalValue(), 4, RoundingMode.HALF_UP));
115
116 }
117 }
118 }
119 }
120 }
121 creditDoc.setProrateBy(creditDoc.isProrateQty() ? OLEConstants.PRORATE_BY_QTY : creditDoc.isProrateManual() ? OLEConstants.MANUAL_PRORATE : creditDoc.isProrateDollar() ? OLEConstants.PRORATE_BY_DOLLAR : null);
122 if ((creditDoc.getProrateBy() != null) && (creditDoc.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY) || creditDoc.getProrateBy().equals(OLEConstants.PRORATE_BY_DOLLAR) || creditDoc.getProrateBy().equals(OLEConstants.MANUAL_PRORATE))) {
123 LOG.debug("Calculation for ProrateItemSurcharge");
124 getOleCreditMemoService().calculateCreditMemo(creditDoc);
125 } else {
126 for (OleCreditMemoItem items : item) {
127 if (items.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
128 items.setItemSurcharge(BigDecimal.ZERO);
129 }
130 }
131 }
132
133 return super.calculate(mapping, form, request, response);
134 }
135
136
137
138
139
140
141
142
143
144
145
146 public ActionForward addItem(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
147 OleVendorCreditMemoForm purchasingForm = (OleVendorCreditMemoForm) form;
148 OleCreditMemoItem item = (OleCreditMemoItem) purchasingForm.getNewPurchasingItemLine();
149
150 OleVendorCreditMemoDocument document = (OleVendorCreditMemoDocument) purchasingForm.getDocument();
151 BibInfoWrapperService docStore = SpringContext.getBean(BibInfoWrapperServiceImpl.class);
152 FileProcessingService fileProcessingService = SpringContext.getBean(FileProcessingService.class);
153 String titleId = null;
154 boolean isBibFileExist = false;
155 Iterator itemIterator = document.getItems().iterator();
156 int itemCounter = 0;
157 while (itemIterator.hasNext()) {
158 OleCreditMemoItem tempItem = (OleCreditMemoItem) itemIterator.next();
159 if (tempItem.getItemTypeCode().equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_ITEM_CODE) || tempItem.getItemTypeCode().equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE)) {
160 itemCounter++;
161 }
162 }
163 String itemNo = String.valueOf(itemCounter);
164
165 HashMap<String, String> dataMap = new HashMap<String, String>();
166 item.setBibInfoBean(new BibInfoBean());
167 if (item.getBibInfoBean().getDocStoreOperation() == null) {
168 item.getBibInfoBean().setDocStoreOperation(OleSelectConstant.DOCSTORE_OPERATION_STAFF);
169 }
170 String fileName = document.getDocumentNumber() + "_" + itemNo;
171
172 setItemDescription(item, fileName);
173
174
175 boolean ruleFlag = getKualiRuleService().applyRules(new OleCreditMemoDescEvent(document, item));
176
177 if (ruleFlag) {
178 if (item.getItemDescription() == null || item.getItemDescription().isEmpty()) {
179 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, OleSelectConstant.ERROR_REQUIRED, new String[]{"Line Item"});
180 } else if (item.getPoUnitPrice() == null || item.getPoUnitPrice().equals(BigDecimal.ZERO)) {
181 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, OleSelectConstant.ERROR_UNIT_PRICE_REQUIRED);
182 } else {
183 if ((document.getVendorDetail() == null) || (document.getVendorDetail().getVendorName() != null && !document.getVendorDetail().getVendorHeader().getVendorForeignIndicator())) {
184
185 item = (OleCreditMemoItem) purchasingForm.getAndResetNewPurchasingItemLine();
186 document.creditMemoCalculation(item);
187 document.addItem(item);
188 } else {
189 boolean rulePassed = getKualiRuleService().applyRules(new OleForeignCurrencyCreditMemoEvent(document, item));
190 if (rulePassed) {
191 LOG.debug("###########Foreign Currency Field Calculation for Payment Request ###########");
192
193 Long id = document.getVendorDetail().getCurrencyType().getCurrencyTypeId();
194 Map documentNumberMap = new HashMap();
195 documentNumberMap.put(OleSelectConstant.CURRENCY_TYPE_ID, id);
196 BusinessObjectService businessObjectService = SpringContext
197 .getBean(BusinessObjectService.class);
198 List<OleExchangeRate> exchangeRateList = (List) businessObjectService.findMatchingOrderBy(OleExchangeRate.class, documentNumberMap, OleSelectConstant.EXCHANGE_RATE_DATE, false);
199 Iterator iterator = exchangeRateList.iterator();
200 if (iterator.hasNext()) {
201 OleExchangeRate tempOleExchangeRate = (OleExchangeRate) iterator.next();
202 item.setItemExchangeRate(new KualiDecimal(tempOleExchangeRate.getExchangeRate()));
203 }
204 if (item.getItemExchangeRate() != null && item.getItemForeignUnitCost() != null) {
205 item.setItemUnitCostUSD(new KualiDecimal(item.getItemForeignUnitCost().bigDecimalValue().divide(item.getItemExchangeRate().bigDecimalValue(), 4, RoundingMode.HALF_UP)));
206 item.setItemUnitPrice(item.getItemUnitCostUSD().bigDecimalValue());
207
208 }
209 item = (OleCreditMemoItem) purchasingForm.getAndResetNewPurchasingItemLine();
210 document.addItem(item);
211 }
212 }
213 }
214 }
215 return mapping.findForward(OLEConstants.MAPPING_BASIC);
216 }
217
218
219
220
221
222
223
224
225
226
227
228
229 public ActionForward deleteItem(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
230 OleVendorCreditMemoForm creditMemoForm = (OleVendorCreditMemoForm) form;
231 OleVendorCreditMemoDocument creditMemoDocument = (OleVendorCreditMemoDocument) creditMemoForm.getDocument();
232 creditMemoDocument.deleteItem(getSelectedLine(request));
233 return mapping.findForward(OLEConstants.MAPPING_BASIC);
234 }
235
236 @Override
237 public ActionForward continueCreditMemo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
238 OleVendorCreditMemoForm rqForm = (OleVendorCreditMemoForm) form;
239 OleVendorCreditMemoDocument document = (OleVendorCreditMemoDocument) rqForm.getDocument();
240 boolean rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedContinuePurapEvent(document));
241 if (!rulePassed) {
242 return super.continueCreditMemo(mapping, form, request, response);
243 }
244 ActionForward forward = super.continueCreditMemo(mapping, form, request, response);
245 List<OleCreditMemoItem> items = document.getItems();
246 OleCreditMemoItem newLineItem = (OleCreditMemoItem) rqForm.getNewPurchasingItemLine();
247 document.setProrateQty(true);
248 if (document.getVendorDetail() != null && document.getVendorDetail().getVendorHeader().getVendorForeignIndicator()) {
249 Long currencyTypeId = document.getVendorDetail().getCurrencyType().getCurrencyTypeId();
250 Map documentNumberMap = new HashMap();
251 documentNumberMap.put(OleSelectConstant.CURRENCY_TYPE_ID, currencyTypeId);
252 BusinessObjectService businessObjectService = SpringContext.getBean(BusinessObjectService.class);
253 List<OleExchangeRate> exchangeRateList = (List) businessObjectService.findMatchingOrderBy(OleExchangeRate.class, documentNumberMap, OleSelectConstant.EXCHANGE_RATE_DATE, false);
254 Iterator iterator = exchangeRateList.iterator();
255 for (OleCreditMemoItem item : items) {
256 iterator = exchangeRateList.iterator();
257 if (iterator.hasNext()) {
258 OleExchangeRate tempOleExchangeRate = (OleExchangeRate) iterator.next();
259 item.setItemExchangeRate(new KualiDecimal(tempOleExchangeRate.getExchangeRate()));
260 }
261 }
262 iterator = exchangeRateList.iterator();
263 if (iterator.hasNext()) {
264 OleExchangeRate tempOleExchangeRate = (OleExchangeRate) iterator.next();
265 newLineItem.setItemExchangeRate(new KualiDecimal(tempOleExchangeRate.getExchangeRate()));
266 }
267 }
268 rqForm.getAndResetNewPurchasingItemLine();
269
270 return forward;
271 }
272
273 @Override
274 public ActionForward route(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
275 ActionForward forward = super.route(mapping, form, request, response);
276 this.calculate(mapping, form, request, response);
277 return forward;
278 }
279
280
281
282
283
284
285 @Override
286 public ActionForward insertSourceLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
287
288 PurchasingAccountsPayableFormBase purapForm = (PurchasingAccountsPayableFormBase) form;
289
290
291 int itemIndex = getSelectedLine(request);
292 PurApItem item = null;
293
294
295 if (processCustomInsertAccountingLine(purapForm, request) == false) {
296 String errorPrefix = null;
297 PurApAccountingLine line = null;
298
299 boolean rulePassed = false;
300 if (itemIndex >= 0) {
301 item = ((PurchasingAccountsPayableDocument) purapForm.getDocument()).getItem((itemIndex));
302
303 PurApAccountingLine lineItem = item.getNewSourceLine();
304 if (lineItem.getAccountLinePercent() != null) {
305 BigDecimal percent = lineItem.getAccountLinePercent().divide(new BigDecimal(100));
306 lineItem.setAmount((item.getTotalAmount().multiply(new KualiDecimal(percent))));
307 } else if (lineItem.getAmount() != null && lineItem.getAccountLinePercent() == null) {
308 KualiDecimal dollar = lineItem.getAmount().multiply(new KualiDecimal(100));
309 BigDecimal dollarToPercent = dollar.bigDecimalValue().divide((item.getTotalAmount().bigDecimalValue()), 0, RoundingMode.FLOOR);
310 lineItem.setAccountLinePercent(dollarToPercent);
311 }
312 line = (PurApAccountingLine) ObjectUtils.deepCopy(lineItem);
313
314
315 errorPrefix = OLEPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.ITEM + "[" + Integer.toString(itemIndex) + "]." + OLEConstants.NEW_SOURCE_ACCT_LINE_PROPERTY_NAME;
316 rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new AddAccountingLineEvent(errorPrefix, purapForm.getDocument(), line));
317 } else if (itemIndex == -2) {
318
319
320 line = ((PurchasingFormBase) purapForm).getAccountDistributionnewSourceLine();
321
322 errorPrefix = PurapPropertyConstants.ACCOUNT_DISTRIBUTION_NEW_SRC_LINE;
323 rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new AddAccountingLineEvent(errorPrefix, purapForm.getDocument(), line));
324 }
325
326 if (rulePassed) {
327
328 SpringContext.getBean(PersistenceService.class).retrieveNonKeyFields(line);
329 if (itemIndex >= 0) {
330 insertAccountingLine(purapForm, item, line);
331
332 item.resetAccount();
333 } else if (itemIndex == -2) {
334
335 ((PurchasingFormBase) purapForm).addAccountDistributionsourceAccountingLine(line);
336 }
337 }
338 }
339
340 return mapping.findForward(OLEConstants.MAPPING_BASIC);
341 }
342
343 private OleCreditMemoService getOleCreditMemoService() {
344 return SpringContext.getBean(OleCreditMemoService.class);
345 }
346
347
348 private void setItemDescription(OleCreditMemoItem item, String fileName) {
349
350 if (OleDocstoreResponse.getInstance().getDocstoreResponse() != null) {
351 HashMap<String, OleEditorResponse> oleEditorResponses = OleDocstoreResponse.getInstance().getDocstoreResponse();
352 OleEditorResponse oleEditorResponse = oleEditorResponses.get(fileName);
353 OleBibRecord oleBibRecord = oleEditorResponse != null ? oleEditorResponse.getOleBibRecord() : null;
354 StringEscapeUtils stringEscapeUtils = new StringEscapeUtils();
355 Map<String, ?> bibAssociatedFieldValuesMap = oleBibRecord != null ? oleBibRecord.getBibAssociatedFieldsValueMap() : null;
356 if (bibAssociatedFieldValuesMap != null && bibAssociatedFieldValuesMap.size() > 0) {
357 List titleList = (List) bibAssociatedFieldValuesMap.get("Title_search");
358 String title = titleList != null && !titleList.isEmpty() ? (String) (titleList).get(0) + ", " : "";
359 title = stringEscapeUtils.unescapeXml(title);
360 List authorList = (List) bibAssociatedFieldValuesMap.get("Author_search");
361 String author = (authorList != null && !authorList.isEmpty() ? (String) (authorList).get(0) + ", " : "");
362 author = stringEscapeUtils.unescapeXml(author);
363 List publisherList = (List) bibAssociatedFieldValuesMap.get("Publisher_display");
364 String publisher = (publisherList != null && !publisherList.isEmpty() ? (String) (publisherList).get(0)
365 + ", " : "");
366 publisher = stringEscapeUtils.unescapeXml(publisher);
367 List isbnList = (List) bibAssociatedFieldValuesMap.get("020a");
368 String isbn = (isbnList != null && !isbnList.isEmpty() ? (String) (isbnList).get(0) + "," : "");
369 String description = title + author + publisher + isbn;
370 item.setItemDescription(description.substring(0, (description.lastIndexOf(","))));
371 }
372 if (oleBibRecord != null) {
373 item.setBibUUID(oleBibRecord.getBibUUID());
374 item.setItemTitleId(oleBibRecord.getLinkedInstanceId());
375 }
376 OleDocstoreResponse.getInstance().getDocstoreResponse().remove(oleEditorResponse);
377 }
378 }
379
380
381
382
383
384
385 @Override
386 protected void customCalculate(PurchasingAccountsPayableDocument apDoc) {
387 OleVendorCreditMemoDocument cmDocument = (OleVendorCreditMemoDocument) apDoc;
388
389
390 SpringContext.getBean(OleCreditMemoService.class).calculateCreditMemo(cmDocument);
391
392
393 SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedCalculateAccountsPayableEvent(cmDocument));
394
395 }
396
397 }