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
19 import org.apache.commons.lang.StringEscapeUtils;
20 import org.apache.struts.action.ActionForm;
21 import org.apache.struts.action.ActionForward;
22 import org.apache.struts.action.ActionMapping;
23 import org.kuali.ole.coa.businessobject.Account;
24 import org.kuali.ole.module.purap.PurapConstants;
25 import org.kuali.ole.module.purap.PurapConstants.PaymentRequestStatuses;
26 import org.kuali.ole.module.purap.PurapPropertyConstants;
27 import org.kuali.ole.module.purap.businessobject.PaymentRequestAccount;
28 import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
29 import org.kuali.ole.module.purap.businessobject.PurApItem;
30 import org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocument;
31 import org.kuali.ole.module.purap.document.service.OlePurapService;
32 import org.kuali.ole.module.purap.document.service.PaymentRequestService;
33 import org.kuali.ole.module.purap.document.validation.event.AttributedCalculateAccountsPayableEvent;
34 import org.kuali.ole.module.purap.document.validation.event.AttributedContinuePurapEvent;
35 import org.kuali.ole.module.purap.document.web.struts.PaymentRequestAction;
36 import org.kuali.ole.module.purap.document.web.struts.PurchasingAccountsPayableFormBase;
37 import org.kuali.ole.module.purap.document.web.struts.PurchasingFormBase;
38 import org.kuali.ole.pojo.OleBibRecord;
39 import org.kuali.ole.pojo.OleEditorResponse;
40 import org.kuali.ole.select.OleSelectConstant;
41 import org.kuali.ole.select.businessobject.*;
42 import org.kuali.ole.select.constants.OleSelectPropertyConstants;
43 import org.kuali.ole.select.document.OleInvoiceDocument;
44 import org.kuali.ole.select.document.OlePaymentRequestDocument;
45 import org.kuali.ole.select.document.service.OlePaymentRequestService;
46 import org.kuali.ole.select.document.validation.event.OleDiscountPaymentRequestEvent;
47 import org.kuali.ole.select.document.validation.event.OleForeignCurrencyPaymentRequestEvent;
48 import org.kuali.ole.select.document.validation.event.OlePaymentRequestDescEvent;
49 import org.kuali.ole.select.service.BibInfoWrapperService;
50 import org.kuali.ole.select.service.FileProcessingService;
51 import org.kuali.ole.select.service.impl.BibInfoWrapperServiceImpl;
52 import org.kuali.ole.sys.OLEConstants;
53 import org.kuali.ole.sys.OLEPropertyConstants;
54 import org.kuali.ole.sys.businessobject.AccountingLineBase;
55 import org.kuali.ole.sys.context.SpringContext;
56 import org.kuali.ole.sys.document.validation.event.AddAccountingLineEvent;
57 import org.kuali.ole.vnd.businessobject.OleExchangeRate;
58 import org.kuali.ole.vnd.businessobject.VendorAddress;
59 import org.kuali.ole.vnd.businessobject.VendorAlias;
60 import org.kuali.ole.vnd.businessobject.VendorDetail;
61 import org.kuali.rice.core.api.util.type.KualiDecimal;
62 import org.kuali.rice.krad.service.*;
63 import org.kuali.rice.krad.util.GlobalVariables;
64 import org.kuali.rice.krad.util.ObjectUtils;
65
66 import javax.servlet.http.HttpServletRequest;
67 import javax.servlet.http.HttpServletResponse;
68 import java.math.BigDecimal;
69 import java.math.RoundingMode;
70 import java.util.*;
71
72
73
74
75
76 public class OlePaymentRequestAction extends PaymentRequestAction {
77
78 protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OlePaymentRequestAction.class);
79
80
81
82
83 @Override
84 public ActionForward calculate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
85
86
87 PurchasingAccountsPayableFormBase purchasingForm = (PurchasingAccountsPayableFormBase) form;
88 List<PurApItem> purApItems = ((PurchasingAccountsPayableDocument) purchasingForm.getDocument()).getItems();
89 for(PurApItem purApItem:purApItems){
90 List<KualiDecimal> existingAmount=new ArrayList<>();
91 for(PurApAccountingLine oldSourceAccountingLine:purApItem.getSourceAccountingLines()) {
92 if(oldSourceAccountingLine instanceof PaymentRequestAccount) {
93 if(((PaymentRequestAccount)oldSourceAccountingLine).getExistingAmount()!=null){
94 existingAmount.add(((PaymentRequestAccount)oldSourceAccountingLine).getExistingAmount());
95 }
96 }
97 }
98 int count=0;
99 for(PurApAccountingLine account:purApItem.getSourceAccountingLines()){
100
101 if (ObjectUtils.isNotNull(account.getAccountLinePercent()) || ObjectUtils.isNotNull(account.getAmount())) {
102 if (account.getAmount()!=null&&count<existingAmount.size()&&existingAmount.size() != 0 && !existingAmount.get(count).toString().equals(account.getAmount().toString())) {
103 KualiDecimal calculatedPercent = new KualiDecimal(account.getAmount().multiply(new KualiDecimal(100)).divide(purApItem.getTotalAmount()).toString());
104 account.setAccountLinePercent(calculatedPercent.bigDecimalValue().setScale(OLEConstants.BIG_DECIMAL_SCALE));
105 }
106
107 else {
108 if(account.getAccountLinePercent().intValue()==100&&(account.getAmount()==null||account.getAccount()!=null)){
109 KualiDecimal calculatedAmount = new KualiDecimal(account.getAccountLinePercent().multiply(purApItem.getTotalAmount().bigDecimalValue()).divide(new BigDecimal(100)).toString());
110 account.setAmount(calculatedAmount);
111 }
112 else{
113 KualiDecimal calculatedAmount = new KualiDecimal(account.getAccountLinePercent().multiply(purApItem.getTotalAmount().bigDecimalValue()).divide(new BigDecimal(100)).toString());
114 account.setAmount(calculatedAmount);
115 }
116 }
117 }
118 count++;
119 }
120 for(PurApAccountingLine oldSourceAccountingLine:purApItem.getSourceAccountingLines()) {
121 if(oldSourceAccountingLine instanceof PaymentRequestAccount) {
122 ((PaymentRequestAccount)oldSourceAccountingLine).setExistingAmount(oldSourceAccountingLine.getAmount());
123 }
124 }
125 }
126 OlePaymentRequestForm paymentForm = (OlePaymentRequestForm) form;
127 OlePaymentRequestDocument payDoc = (OlePaymentRequestDocument) paymentForm.getDocument();
128 Map invMap = new HashMap();
129 if (payDoc.getInvoiceIdentifier() != null) {
130 invMap.put(PurapConstants.PRQSDocumentsStrings.INV_ID, payDoc.getInvoiceIdentifier());
131 payDoc.getInvoiceIdentifier();
132 OleInvoiceDocument oleInvoice = SpringContext
133 .getBean(org.kuali.rice.krad.service.BusinessObjectService.class).findByPrimaryKey(OleInvoiceDocument.class, invMap);
134 payDoc.setProrateBy(oleInvoice.isProrateQty() ? OLEConstants.PRORATE_BY_QTY : oleInvoice.isProrateManual() ? OLEConstants.MANUAL_PRORATE : oleInvoice.isProrateDollar() ? OLEConstants.PRORATE_BY_DOLLAR : OLEConstants.DEFAULT_PRORATE_BY_INVOICE);
135
136 List<OleInvoiceItem> item = oleInvoice.getItems();
137 for (int i = 0; item.size() > i; i++) {
138 OleInvoiceItem items = (OleInvoiceItem) oleInvoice.getItem(i);
139 if (items.getItemTypeCode().equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_FREIGHT_CODE)) {
140 log.info("Item Type Code >>>>>>>>>>>" + items.getItemTypeCode());
141 log.info("items.getItemUnitPrice() >>>>>>>>>" + items.getItemUnitPrice());
142 log.info("items.getTotalAmount() >>>>>>>>" + items.getTotalAmount());
143
144 }
145 }
146
147 } else {
148 payDoc.setProrateBy(payDoc.isProrateQty() ? OLEConstants.PRORATE_BY_QTY : payDoc.isProrateManual() ? OLEConstants.MANUAL_PRORATE : payDoc.isProrateDollar() ? OLEConstants.PRORATE_BY_DOLLAR : null);
149 }
150 boolean manualProrateValidFlag = true;
151 if ((payDoc.getProrateBy() != null) && (payDoc.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY) || payDoc.getProrateBy().equals(OLEConstants.PRORATE_BY_DOLLAR) || payDoc.getProrateBy().equals(OLEConstants.MANUAL_PRORATE))) {
152 if (payDoc.getProrateBy() != null && payDoc.getProrateBy().equals(OLEConstants.MANUAL_PRORATE)) {
153
154 manualProrateValidFlag = getOlePaymentRequestService().validateProratedSurcharge(payDoc);
155 }
156 if (manualProrateValidFlag) {
157
158 List<OlePaymentRequestItem> item = payDoc.getItems();
159 if (payDoc.getVendorDetail() == null || (payDoc.getVendorDetail() != null && payDoc.getVendorDetail().getVendorHeader().getVendorForeignIndicator() != true)) {
160 for (int i = 0; item.size() > i; i++) {
161 OlePaymentRequestItem items = (OlePaymentRequestItem) payDoc.getItem(i);
162 if (items.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
163 boolean rulePassed = getKualiRuleService().applyRules(
164 new OleDiscountPaymentRequestEvent(payDoc, items));
165 if (rulePassed) {
166 items.setItemUnitPrice(SpringContext.getBean(OlePurapService.class).calculateDiscount(items));
167 }
168 }
169 }
170 } else {
171
172 LOG.debug("###########Foreign Currency Field Calculation###########");
173 for (int i = 0; item.size() > i; i++) {
174 OlePaymentRequestItem items = (OlePaymentRequestItem) payDoc.getItem(i);
175 Long id = payDoc.getVendorDetail().getCurrencyType().getCurrencyTypeId();
176 Map documentNumberMap = new HashMap();
177 documentNumberMap.put(OleSelectConstant.CURRENCY_TYPE_ID, id);
178 org.kuali.rice.krad.service.BusinessObjectService businessObjectService = SpringContext
179 .getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
180 List<OleExchangeRate> exchangeRateList = (List) businessObjectService.findMatchingOrderBy(
181 OleExchangeRate.class, documentNumberMap, OleSelectConstant.EXCHANGE_RATE_DATE, false);
182 Iterator iterator = exchangeRateList.iterator();
183 if (iterator.hasNext()) {
184 OleExchangeRate tempOleExchangeRate = (OleExchangeRate) iterator.next();
185 items.setItemExchangeRate(new KualiDecimal(tempOleExchangeRate.getExchangeRate()));
186 payDoc.setForeignVendorInvoiceAmount(payDoc.getVendorInvoiceAmount().bigDecimalValue()
187 .multiply(tempOleExchangeRate.getExchangeRate()));
188 }
189 if ((items.getItemType().isQuantityBasedGeneralLedgerIndicator())) {
190 boolean rulePassed = getKualiRuleService().applyRules(
191 new OleForeignCurrencyPaymentRequestEvent(payDoc, items));
192 if (rulePassed) {
193 SpringContext.getBean(OlePurapService.class).calculateForeignCurrency(items);
194 if (items.getItemExchangeRate() != null && items.getItemForeignUnitCost() != null) {
195 items.setItemUnitCostUSD(new KualiDecimal(items.getItemForeignUnitCost().bigDecimalValue().divide(items.getItemExchangeRate().bigDecimalValue(), 4, RoundingMode.HALF_UP)));
196 items.setItemUnitPrice(items.getItemUnitCostUSD().bigDecimalValue());
197 items.setItemListPrice(items.getItemUnitCostUSD());
198 items.setPurchaseOrderItemUnitPrice(items.getItemUnitPrice());
199 }
200 }
201
202 } else {
203 if (items.getItemExchangeRate() != null && items.getForeignCurrencyExtendedPrice() != null) {
204
205 if (items.isAdditionalChargeUsd()) {
206 items.setItemUnitPrice(items.getForeignCurrencyExtendedPrice().bigDecimalValue());
207 } else {
208 items.setItemUnitPrice(items.getForeignCurrencyExtendedPrice().bigDecimalValue().divide(items.getItemExchangeRate().bigDecimalValue(), 4, RoundingMode.HALF_UP));
209 }
210 }
211 }
212 }
213 }
214 getOlePaymentRequestService().calculateProrateItemSurcharge(payDoc);
215 }
216 }
217
218 if (payDoc.getProrateBy() == null && manualProrateValidFlag) {
219 getOlePaymentRequestService().calculateWithoutProrates(payDoc);
220 }
221 List<PurApItem> newpurApItems = ((PurchasingAccountsPayableDocument) paymentForm.getDocument()).getItems();
222 for(PurApItem purApItem:newpurApItems){
223 for(PurApAccountingLine account:purApItem.getSourceAccountingLines()){
224 KualiDecimal calculatedAmount = new KualiDecimal(account.getAccountLinePercent().multiply(purApItem.getTotalAmount().bigDecimalValue()).divide(new BigDecimal(100)).toString());
225 account.setAmount(calculatedAmount);
226 }
227 }
228 return super.calculate(mapping, form, request, response);
229 }
230
231
232
233
234
235
236
237
238
239
240
241
242 public ActionForward addNote(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
243 LOG.debug("Inside addNote Method of OlePaymentRequestAction");
244 OlePaymentRequestForm paymentForm = (OlePaymentRequestForm) form;
245 OlePaymentRequestDocument paymentDocument = (OlePaymentRequestDocument) paymentForm.getDocument();
246 int line = this.getSelectedLine(request);
247 OlePaymentRequestItem item = (OlePaymentRequestItem) ((PurchasingAccountsPayableDocument) paymentForm.getDocument()).getItem(line);
248 OlePaymentRequestNote note = new OlePaymentRequestNote();
249 note.setNote(item.getNote());
250
251
252 item.getNotes().add(note);
253 LOG.debug("Adding InvoiceNote to the PaymentRequestItem");
254 item.setNote(null);
255 LOG.debug("Leaving addNote Method of OlePaymentRequestAction");
256
257 return mapping.findForward(OLEConstants.MAPPING_BASIC);
258 }
259
260
261
262
263
264
265
266
267
268
269
270 public ActionForward deleteNote(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
271 LOG.debug("Inside deleteNote Method of OlePaymentRequestAction");
272 OlePaymentRequestForm paymentForm = (OlePaymentRequestForm) form;
273
274 String[] indexes = getSelectedLineForAccounts(request);
275 int itemIndex = Integer.parseInt(indexes[0]);
276 int noteIndex = Integer.parseInt(indexes[1]);
277 OlePaymentRequestItem item = (OlePaymentRequestItem) ((PurchasingAccountsPayableDocument) paymentForm.getDocument()).getItem((itemIndex));
278 item.getNotes().remove(noteIndex);
279 LOG.debug("Note deleted for the selected Item");
280 LOG.debug("Leaving deleteNote Method of OlePaymentRequestAction");
281 return mapping.findForward(OLEConstants.MAPPING_BASIC);
282 }
283
284
285
286
287
288
289
290
291
292
293
294 public ActionForward addItem(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
295 OlePaymentRequestForm purchasingForm = (OlePaymentRequestForm) form;
296 OlePaymentRequestItem item = (OlePaymentRequestItem) purchasingForm.getNewPurchasingItemLine();
297 item.getNewSourceLine().setAccountLinePercent(new BigDecimal(100));
298
299
300
301
302
303 OlePaymentRequestDocument document = (OlePaymentRequestDocument) purchasingForm.getDocument();
304 BibInfoWrapperService docStore = SpringContext.getBean(BibInfoWrapperServiceImpl.class);
305 FileProcessingService fileProcessingService = SpringContext.getBean(FileProcessingService.class);
306 String titleId = null;
307 boolean isBibFileExist = false;
308 Iterator itemIterator = document.getItems().iterator();
309 int itemCounter = 0;
310 while (itemIterator.hasNext()) {
311 OlePaymentRequestItem tempItem = (OlePaymentRequestItem) itemIterator.next();
312 if (tempItem.getItemTypeCode().equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_ITEM_CODE) || tempItem.getItemTypeCode().equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE)) {
313 itemCounter++;
314 }
315 }
316 String itemNo = String.valueOf(itemCounter);
317
318 HashMap<String, String> dataMap = new HashMap<String, String>();
319 item.setBibInfoBean(new BibInfoBean());
320 if (item.getBibInfoBean().getDocStoreOperation() == null) {
321 item.getBibInfoBean().setDocStoreOperation(OleSelectConstant.DOCSTORE_OPERATION_STAFF);
322 }
323 String fileName = document.getDocumentNumber() + "_" + itemNo;
324
325 setItemDescription(item, fileName);
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351 boolean ruleFlag = getKualiRuleService().applyRules(new OlePaymentRequestDescEvent(document, item));
352 if (ruleFlag) {
353 if ((document.getVendorDetail() == null) || (document.getVendorDetail().getVendorName() != null && !document.getVendorDetail().getVendorHeader().getVendorForeignIndicator())) {
354 boolean rulePassed = getKualiRuleService().applyRules(new OleDiscountPaymentRequestEvent(document, item));
355 if (rulePassed) {
356 purchasingForm.getNewPurchasingItemLine().setItemUnitPrice(SpringContext.getBean(OlePurapService.class).calculateDiscount(item));
357 item = (OlePaymentRequestItem) purchasingForm.getAndResetNewPurchasingItemLine();
358 document.addItem(item);
359 }
360 } else {
361 boolean rulePassed = getKualiRuleService().applyRules(new OleForeignCurrencyPaymentRequestEvent(document, item));
362 if (rulePassed) {
363 LOG.debug("###########Foreign Currency Field Calculation for Payment Request ###########");
364 SpringContext.getBean(OlePurapService.class).calculateForeignCurrency(item);
365 Long id = document.getVendorDetail().getCurrencyType().getCurrencyTypeId();
366 Map documentNumberMap = new HashMap();
367 documentNumberMap.put(OleSelectConstant.CURRENCY_TYPE_ID, id);
368 org.kuali.rice.krad.service.BusinessObjectService businessObjectService = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
369 List<OleExchangeRate> exchangeRateList = (List) businessObjectService.findMatchingOrderBy(OleExchangeRate.class, documentNumberMap, OleSelectConstant.EXCHANGE_RATE_DATE, false);
370 Iterator iterator = exchangeRateList.iterator();
371 if (iterator.hasNext()) {
372 OleExchangeRate tempOleExchangeRate = (OleExchangeRate) iterator.next();
373 item.setItemExchangeRate(new KualiDecimal(tempOleExchangeRate.getExchangeRate()));
374 }
375 if (item.getItemExchangeRate() != null && item.getItemForeignUnitCost() != null) {
376 item.setItemUnitCostUSD(new KualiDecimal(item.getItemForeignUnitCost().bigDecimalValue().divide(item.getItemExchangeRate().bigDecimalValue(), 4, RoundingMode.HALF_UP)));
377 item.setItemUnitPrice(item.getItemUnitCostUSD().bigDecimalValue());
378 item.setItemListPrice(item.getItemUnitCostUSD());
379 }
380 item = (OlePaymentRequestItem) purchasingForm.getAndResetNewPurchasingItemLine();
381 document.addItem(item);
382 }
383 }
384 }
385 return mapping.findForward(OLEConstants.MAPPING_BASIC);
386 }
387
388
389
390
391
392
393
394
395
396
397
398 public ActionForward deleteItem(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
399 OlePaymentRequestForm purchasingForm = (OlePaymentRequestForm) form;
400 OlePaymentRequestDocument purDocument = (OlePaymentRequestDocument) purchasingForm.getDocument();
401 purDocument.deleteItem(getSelectedLine(request));
402 return mapping.findForward(OLEConstants.MAPPING_BASIC);
403 }
404
405 @Override
406 public ActionForward continuePREQ(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
407 OlePaymentRequestForm rqForm = (OlePaymentRequestForm) form;
408 OlePaymentRequestDocument document = (OlePaymentRequestDocument) rqForm.getDocument();
409 boolean rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedContinuePurapEvent(document));
410 if (!rulePassed) {
411 return super.continuePREQ(mapping, form, request, response);
412 }
413 ActionForward forward = super.continuePREQ(mapping, form, request, response);
414 List<OlePaymentRequestItem> items = document.getItems();
415 OlePaymentRequestItem newLineItem = (OlePaymentRequestItem) rqForm.getNewPurchasingItemLine();
416 if (document.getVendorDetail() != null && document.getVendorDetail().getVendorHeader().getVendorForeignIndicator()) {
417 Long currencyTypeId = document.getVendorDetail().getCurrencyType().getCurrencyTypeId();
418 Map documentNumberMap = new HashMap();
419 documentNumberMap.put(OleSelectConstant.CURRENCY_TYPE_ID, currencyTypeId);
420 org.kuali.rice.krad.service.BusinessObjectService businessObjectService = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
421 List<OleExchangeRate> exchangeRateList = (List) businessObjectService.findMatchingOrderBy(OleExchangeRate.class, documentNumberMap, OleSelectConstant.EXCHANGE_RATE_DATE, false);
422 Iterator iterator = exchangeRateList.iterator();
423 for (OlePaymentRequestItem item : items) {
424 iterator = exchangeRateList.iterator();
425 if (iterator.hasNext()) {
426 OleExchangeRate tempOleExchangeRate = (OleExchangeRate) iterator.next();
427 item.setItemExchangeRate(new KualiDecimal(tempOleExchangeRate.getExchangeRate()));
428 }
429 }
430 iterator = exchangeRateList.iterator();
431 if (iterator.hasNext()) {
432 OleExchangeRate tempOleExchangeRate = (OleExchangeRate) iterator.next();
433 newLineItem.setItemExchangeRate(new KualiDecimal(tempOleExchangeRate.getExchangeRate()));
434 }
435 }
436 rqForm.getAndResetNewPurchasingItemLine();
437
438 return forward;
439 }
440
441 @Override
442 public ActionForward route(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
443 this.calculate(mapping, form, request, response);
444 ActionForward forward = super.route(mapping, form, request, response);
445 OlePaymentRequestDocument document = (OlePaymentRequestDocument) ((OlePaymentRequestForm) form).getDocument();
446 Iterator itemIterator = document.getItems().iterator();
447 int itemCounter = 0;
448 boolean rulePassed = true;
449 while (itemIterator.hasNext()) {
450 OlePaymentRequestItem tempItem = (OlePaymentRequestItem) itemIterator.next();
451 if (tempItem.getItemTypeCode().equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_ITEM_CODE)
452 || tempItem.getItemTypeCode().equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE)) {
453 List<PurApAccountingLine> accountingLineBase = tempItem.getSourceAccountingLines();
454 if (accountingLineBase != null) {
455 for (int accountingLine = 0; accountingLine < accountingLineBase.size(); accountingLine++) {
456 String accountNumber = accountingLineBase.get(accountingLine).getAccountNumber();
457 String chartOfAccountsCode = accountingLineBase.get(accountingLine).getChartOfAccountsCode();
458 Map<String, String> criteria = new HashMap<String, String>();
459 criteria.put(OleSelectConstant.ACCOUNT_NUMBER, accountNumber);
460 criteria.put(OleSelectConstant.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
461 Account account = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(
462 Account.class, criteria);
463 rulePassed = checkForValidAccount(account);
464 if (!rulePassed) {
465 return mapping.findForward(OLEConstants.MAPPING_BASIC);
466 }
467 }
468 }
469 }
470 }
471
472
473
474 return forward;
475 }
476
477
478
479
480
481 @Override
482 public ActionForward insertSourceLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
483
484 PurchasingAccountsPayableFormBase purapForm = (PurchasingAccountsPayableFormBase) form;
485
486
487 int itemIndex = getSelectedLine(request);
488 PurApItem item = null;
489
490
491 if (processCustomInsertAccountingLine(purapForm, request) == false) {
492 String errorPrefix = null;
493 PurApAccountingLine line = null;
494
495 boolean rulePassed = false;
496 if (itemIndex >= 0) {
497 item = ((PurchasingAccountsPayableDocument) purapForm.getDocument()).getItem((itemIndex));
498
499 PurApAccountingLine lineItem = item.getNewSourceLine();
500 if (lineItem.getAccountLinePercent() != null) {
501 BigDecimal percent = lineItem.getAccountLinePercent().divide(new BigDecimal(100));
502 lineItem.setAmount((item.getTotalAmount().multiply(new KualiDecimal(percent))));
503 } else if (lineItem.getAmount() != null && lineItem.getAccountLinePercent() == null) {
504 KualiDecimal dollar = lineItem.getAmount().multiply(new KualiDecimal(100));
505 BigDecimal dollarToPercent = dollar.bigDecimalValue().divide((item.getTotalAmount().bigDecimalValue()), 0, RoundingMode.FLOOR);
506 lineItem.setAccountLinePercent(dollarToPercent);
507 }
508 line = (PurApAccountingLine) ObjectUtils.deepCopy(lineItem);
509
510
511 errorPrefix = OLEPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.ITEM + "[" + Integer.toString(itemIndex) + "]." + OLEConstants.NEW_SOURCE_ACCT_LINE_PROPERTY_NAME;
512 rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new AddAccountingLineEvent(errorPrefix, purapForm.getDocument(), line));
513 } else if (itemIndex == -2) {
514
515
516 line = ((PurchasingFormBase) purapForm).getAccountDistributionnewSourceLine();
517
518 errorPrefix = PurapPropertyConstants.ACCOUNT_DISTRIBUTION_NEW_SRC_LINE;
519 rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new AddAccountingLineEvent(errorPrefix, purapForm.getDocument(), line));
520 }
521 AccountingLineBase accountingLineBase = (AccountingLineBase) item.getNewSourceLine();
522 if (accountingLineBase != null) {
523 String accountNumber = accountingLineBase.getAccountNumber();
524 String chartOfAccountsCode = accountingLineBase.getChartOfAccountsCode();
525 Map<String, String> criteria = new HashMap<String, String>();
526 criteria.put(OleSelectConstant.ACCOUNT_NUMBER, accountNumber);
527 criteria.put(OleSelectConstant.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
528 Account account = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(Account.class,
529 criteria);
530 rulePassed = checkForValidAccount(account);
531 }
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546 if (rulePassed) {
547
548 SpringContext.getBean(PersistenceService.class).retrieveNonKeyFields(line);
549
550 PurApAccountingLine newSourceLine = item.getNewSourceLine();
551 List<PurApAccountingLine> existingSourceLine = item.getSourceAccountingLines();
552
553 BigDecimal initialValue = new BigDecimal(0);
554
555 for (PurApAccountingLine accountLine : existingSourceLine) {
556 initialValue = initialValue.add(accountLine.getAccountLinePercent());
557 }
558 if (itemIndex >= 0) {
559
560 if ((newSourceLine.getAccountLinePercent().intValue() <= OleSelectConstant.ACCOUNTINGLINE_PERCENT_HUNDRED && newSourceLine.getAccountLinePercent().intValue() <= OleSelectConstant.MAX_PERCENT.subtract(initialValue).intValue()) && newSourceLine.getAccountLinePercent().intValue() > OleSelectConstant.ZERO) {
561 if (OleSelectConstant.MAX_PERCENT.subtract(initialValue).intValue() != OleSelectConstant.ZERO) {
562 insertAccountingLine(purapForm, item, line);
563 }
564 }else {
565 checkAccountingLinePercent(newSourceLine);
566
567 }
568 for(PurApAccountingLine oldSourceAccountingLine:item.getSourceAccountingLines()) {
569 if(oldSourceAccountingLine instanceof PaymentRequestAccount) {
570 ((PaymentRequestAccount)oldSourceAccountingLine).setExistingAmount(oldSourceAccountingLine.getAmount());
571 }
572 }
573 List<PurApAccountingLine> existingAccountingLine = item.getSourceAccountingLines();
574 BigDecimal totalPercent = new BigDecimal(100);
575 BigDecimal initialPercent = new BigDecimal(0);
576 for (PurApAccountingLine purApAccountingLine : existingAccountingLine) {
577 initialPercent = initialPercent.add(purApAccountingLine.getAccountLinePercent());
578
579 }
580 initialPercent = totalPercent.subtract(initialPercent);
581 BigDecimal maxPercent = initialPercent.max(OleSelectConstant.ZERO_PERCENT);
582 if (maxPercent.intValue() == OleSelectConstant.ZERO) {
583 item.resetAccount(OleSelectConstant.ZERO_PERCENT);
584
585 } else {
586 item.resetAccount(initialPercent);
587
588 }
589 } else if (itemIndex == -2) {
590
591 ((PurchasingFormBase) purapForm).addAccountDistributionsourceAccountingLine(line);
592 }
593 }
594 }
595
596
597 return mapping.findForward(OLEConstants.MAPPING_BASIC);
598 }
599
600 private void checkAccountingLinePercent(PurApAccountingLine newSourceLine) {
601 if (newSourceLine.getAccountLinePercent().intValue() >= OleSelectConstant.ACCOUNTINGLINE_PERCENT_HUNDRED) {
602 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
603 OleSelectPropertyConstants.ERROR_PERCENT_SHOULD_GREATER, OleSelectConstant.PERCENT);
604 } else if (newSourceLine.getAccountLinePercent().intValue() == OleSelectConstant.ZERO) {
605 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
606 OleSelectPropertyConstants.ERROR_PERCENT_ZERO, OleSelectConstant.PERCENT);
607 } else {
608
609 }
610
611 }
612
613 private boolean checkForValidAccount(Account account) {
614 boolean result = true;
615 if (account != null) {
616 String subFundGroupParameter = getParameterService().getParameterValueAsString(Account.class,
617 OleSelectConstant.SUB_FUND_GRP_CD);
618 if (account.getSubFundGroupCode().equalsIgnoreCase(subFundGroupParameter)) {
619 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
620 OleSelectPropertyConstants.ERROR_ACCOUNT_NUMBER,
621 new String[]{OleSelectConstant.PAYMENT_REQUEST});
622 result = false;
623 }
624 }
625 return result;
626 }
627
628 private void setItemDescription(OlePaymentRequestItem item, String fileName) {
629
630 if (OleDocstoreResponse.getInstance().getDocstoreResponse() != null) {
631 HashMap<String, OleEditorResponse> oleEditorResponses = OleDocstoreResponse.getInstance().getDocstoreResponse();
632 OleEditorResponse oleEditorResponse = oleEditorResponses.get(fileName);
633 OleBibRecord oleBibRecord = oleEditorResponse != null ? oleEditorResponse.getOleBibRecord() : null;
634 StringEscapeUtils stringEscapeUtils = new StringEscapeUtils();
635 Map<String, ?> bibAssociatedFieldValuesMap = oleBibRecord != null ? oleBibRecord.getBibAssociatedFieldsValueMap() : null;
636 if (bibAssociatedFieldValuesMap != null && bibAssociatedFieldValuesMap.size() > 0) {
637 List titleList = (List) bibAssociatedFieldValuesMap.get("Title_search");
638 String title = titleList != null && !titleList.isEmpty() ? (String) (titleList).get(0) + ", " : "";
639 title = stringEscapeUtils.unescapeXml(title);
640 List authorList = (List) bibAssociatedFieldValuesMap.get("Author_search");
641 String author = (authorList != null && !authorList.isEmpty() ? (String) (authorList).get(0) + ", " : "");
642 author = stringEscapeUtils.unescapeXml(author);
643 List publisherList = (List) bibAssociatedFieldValuesMap.get("Publisher_display");
644 String publisher = (publisherList != null && !publisherList.isEmpty() ? (String) (publisherList).get(0)
645 + ", " : "");
646 publisher = stringEscapeUtils.unescapeXml(publisher);
647 List isbnList = (List) bibAssociatedFieldValuesMap.get("020a");
648 String isbn = (isbnList != null && !isbnList.isEmpty() ? (String) (isbnList).get(0) + ", " : "");
649 String description = title + author + publisher + isbn;
650 item.setItemDescription(description.substring(0, (description.lastIndexOf(","))));
651 }
652 if (oleBibRecord != null) {
653 item.setBibUUID(oleBibRecord.getBibUUID());
654 item.setItemTitleId(oleBibRecord.getBibUUID());
655 }
656 OleDocstoreResponse.getInstance().getDocstoreResponse().remove(oleEditorResponse);
657 }
658 }
659
660 private OlePaymentRequestService getOlePaymentRequestService() {
661 return SpringContext.getBean(OlePaymentRequestService.class);
662 }
663
664 @Override
665 protected void customCalculate(PurchasingAccountsPayableDocument apDoc) {
666 OlePaymentRequestDocument preqDoc = (OlePaymentRequestDocument) apDoc;
667 if ((preqDoc.getProrateBy() != null) && (preqDoc.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY) || preqDoc.getProrateBy().equals(OLEConstants.PRORATE_BY_DOLLAR) || preqDoc.getProrateBy().equals(OLEConstants.MANUAL_PRORATE))) {
668
669 preqDoc.updateExtendedPriceOnItems();
670
671
672
673 if (preqDoc.getApplicationDocumentStatus().equals(PaymentRequestStatuses.APPDOC_AWAITING_TAX_REVIEW)) {
674 SpringContext.getBean(PaymentRequestService.class).calculateTaxArea(preqDoc);
675 return;
676 }
677
678
679
680 SpringContext.getBean(OlePaymentRequestService.class).calculatePaymentRequest(preqDoc, true);
681 SpringContext.getBean(KualiRuleService.class).applyRules(
682 new AttributedCalculateAccountsPayableEvent(preqDoc));
683 } else {
684 super.customCalculate(preqDoc);
685 }
686 }
687
688 @Override
689 public ActionForward blanketApprove(ActionMapping mapping, ActionForm form, HttpServletRequest request,
690 HttpServletResponse response) throws Exception {
691 this.calculate(mapping, form, request, response);
692 ActionForward forward = super.blanketApprove(mapping, form, request, response);
693 OlePaymentRequestDocument document = (OlePaymentRequestDocument) ((OlePaymentRequestForm) form).getDocument();
694 Iterator itemIterator = document.getItems().iterator();
695 int itemCounter = 0;
696 boolean rulePassed = true;
697 while (itemIterator.hasNext()) {
698 OlePaymentRequestItem tempItem = (OlePaymentRequestItem) itemIterator.next();
699 if (tempItem.getItemTypeCode().equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_ITEM_CODE)
700 || tempItem.getItemTypeCode().equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE)) {
701 List<PurApAccountingLine> accountingLineBase = tempItem.getSourceAccountingLines();
702 if (accountingLineBase != null) {
703 for (int accountingLine = 0; accountingLine < accountingLineBase.size(); accountingLine++) {
704 String accountNumber = accountingLineBase.get(accountingLine).getAccountNumber();
705 String chartOfAccountsCode = accountingLineBase.get(accountingLine).getChartOfAccountsCode();
706 Map<String, String> criteria = new HashMap<String, String>();
707 criteria.put(OleSelectConstant.ACCOUNT_NUMBER, accountNumber);
708 criteria.put(OleSelectConstant.CHART_OF_ACCOUNTS_CODE, chartOfAccountsCode);
709 Account account = SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(
710 Account.class, criteria);
711 rulePassed = checkForValidAccount(account);
712 if (!rulePassed) {
713 return mapping.findForward(OLEConstants.MAPPING_BASIC);
714 }
715 }
716 }
717 }
718 }
719 return forward;
720 }
721
722 public ActionForward selectVendor(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
723
724 OlePaymentRequestForm preqForm = (OlePaymentRequestForm) form;
725 OlePaymentRequestDocument document = (OlePaymentRequestDocument) preqForm.getDocument();
726 if (document.getVendorAliasName() != null && document.getVendorAliasName().length() > 0) {
727
728 Map vendorAliasMap = new HashMap();
729 vendorAliasMap.put(OLEConstants.VENDOR_ALIAS_NAME, document.getVendorAliasName());
730 org.kuali.rice.krad.service.BusinessObjectService businessObject = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
731 List<VendorAlias> vendorAliasList = (List<VendorAlias>) getLookupService().findCollectionBySearchHelper(VendorAlias.class, vendorAliasMap, true);
732 if (vendorAliasList != null && vendorAliasList.size() > 0 && vendorAliasList.get(0) != null) {
733 Map vendorDetailMap = new HashMap();
734 vendorDetailMap.put(OLEConstants.VENDOR_HEADER_IDENTIFIER, vendorAliasList.get(0).getVendorHeaderGeneratedIdentifier());
735 vendorDetailMap.put(OLEConstants.VENDOR_DETAIL_IDENTIFIER, vendorAliasList.get(0).getVendorDetailAssignedIdentifier());
736 VendorDetail vendorDetail = businessObject.findByPrimaryKey(VendorDetail.class, vendorDetailMap);
737 document.setVendorDetail(vendorDetail);
738 document.setVendorHeaderGeneratedIdentifier(vendorAliasList.get(0).getVendorHeaderGeneratedIdentifier());
739 document.setVendorDetailAssignedIdentifier(vendorAliasList.get(0).getVendorDetailAssignedIdentifier());
740 document.setVendorName(vendorDetail.getVendorName());
741 VendorAddress vendorAddress = businessObject.findByPrimaryKey(VendorAddress.class, vendorDetailMap);
742 if (vendorAddress != null) {
743 document.setVendorLine1Address(vendorAddress.getVendorLine1Address());
744 document.setVendorLine2Address(vendorAddress.getVendorLine2Address());
745 document.setVendorCityName(vendorAddress.getVendorCityName());
746 document.setVendorStateCode(vendorAddress.getVendorStateCode());
747 document.setVendorPostalCode(vendorAddress.getVendorZipCode());
748 document.setVendorCountryCode(vendorAddress.getVendorCountryCode());
749 document.setVendorAddressInternationalProvinceName(vendorAddress.getVendorAddressInternationalProvinceName());
750 }
751 refresh(mapping, form, request, response);
752 } else {
753 GlobalVariables.getMessageMap().putError(PurapConstants.VENDOR_ERRORS, OLEConstants.VENDOR_NOT_FOUND);
754 }
755 }
756 return mapping.findForward(OLEConstants.MAPPING_BASIC);
757 }
758
759 private LookupService getLookupService() {
760 return KRADServiceLocatorWeb.getLookupService();
761 }
762
763 }