1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.sys.web.struts;
17  
18  import static org.kuali.ole.sys.OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_SALES_TAX_INVALID_ACCOUNT;
19  import static org.kuali.ole.sys.OLEKeyConstants.ERROR_DOCUMENT_ACCOUNTING_LINE_SALES_TAX_REQUIRED;
20  import static org.kuali.ole.sys.OLEKeyConstants.ERROR_REQUIRED;
21  
22  import java.io.FileNotFoundException;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.HashSet;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Properties;
30  import java.util.Set;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  
35  import org.apache.commons.lang.StringUtils;
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionForward;
38  import org.apache.struts.action.ActionMapping;
39  import org.apache.struts.upload.FormFile;
40  import org.kuali.ole.fp.businessobject.SalesTax;
41  import org.kuali.ole.sys.OLEConstants;
42  import org.kuali.ole.sys.OLEKeyConstants;
43  import org.kuali.ole.sys.OLEPropertyConstants;
44  import org.kuali.ole.sys.businessobject.AccountingLine;
45  import org.kuali.ole.sys.businessobject.AccountingLineOverride;
46  import org.kuali.ole.sys.businessobject.AccountingLineParser;
47  import org.kuali.ole.sys.businessobject.FinancialSystemDocumentHeader;
48  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
49  import org.kuali.ole.sys.businessobject.SourceAccountingLine;
50  import org.kuali.ole.sys.businessobject.TargetAccountingLine;
51  import org.kuali.ole.sys.context.SpringContext;
52  import org.kuali.ole.sys.document.AccountingDocument;
53  import org.kuali.ole.sys.document.AmountTotaling;
54  import org.kuali.ole.sys.document.validation.event.AddAccountingLineEvent;
55  import org.kuali.ole.sys.document.validation.event.DeleteAccountingLineEvent;
56  import org.kuali.ole.sys.document.validation.impl.AccountingDocumentRuleBaseConstants.APPLICATION_PARAMETER;
57  import org.kuali.ole.sys.document.web.struts.FinancialSystemTransactionalDocumentActionBase;
58  import org.kuali.ole.sys.exception.AccountingLineParserException;
59  import org.kuali.ole.sys.service.impl.OleParameterConstants;
60  import org.kuali.rice.core.api.parameter.ParameterEvaluator;
61  import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
62  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
63  import org.kuali.rice.kew.api.exception.WorkflowException;
64  import org.kuali.rice.kns.service.DataDictionaryService;
65  import org.kuali.rice.kns.service.DictionaryValidationService;
66  import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
67  import org.kuali.rice.krad.service.BusinessObjectService;
68  import org.kuali.rice.krad.service.KualiRuleService;
69  import org.kuali.rice.krad.service.PersistenceService;
70  import org.kuali.rice.krad.util.GlobalVariables;
71  import org.kuali.rice.krad.util.KRADConstants;
72  import org.kuali.rice.krad.util.ObjectUtils;
73  import org.kuali.rice.krad.util.UrlFactory;
74  
75  
76  
77  
78  public class KualiAccountingDocumentActionBase extends FinancialSystemTransactionalDocumentActionBase {
79      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(KualiAccountingDocumentActionBase.class);
80  
81      
82  
83  
84  
85  
86  
87      @Override
88      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
89          KualiAccountingDocumentFormBase transForm = (KualiAccountingDocumentFormBase) form;
90  
91          
92          if (transForm.hasDocumentId()) {
93              AccountingDocument financialDocument = (AccountingDocument) transForm.getDocument();
94  
95              processAccountingLines(financialDocument, transForm, OLEConstants.SOURCE);
96              processAccountingLines(financialDocument, transForm, OLEConstants.TARGET);
97          }
98  
99          
100         processAccountingLineOverrides(transForm);
101 
102         
103         ActionForward result = super.execute(mapping, form, request, response);
104         return result;
105     }
106 
107     
108 
109 
110 
111 
112     @Override
113     protected void loadDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
114         super.loadDocument(kualiDocumentFormBase);
115 
116 
117         KualiAccountingDocumentFormBase tform = (KualiAccountingDocumentFormBase) kualiDocumentFormBase;
118 
119         
120         tform.setNewSourceLine(null);
121         tform.setNewTargetLine(null);
122 
123         processAccountingLineOverrides(tform);
124     }
125 
126     
127 
128 
129 
130 
131 
132     @Override
133     public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
134         super.refresh(mapping, form, request, response);
135         refreshSalesTaxInfo(form);
136 
137         return mapping.findForward(OLEConstants.MAPPING_BASIC);
138     }
139 
140     
141 
142 
143 
144 
145 
146     @Override
147     public ActionForward toggleTab(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
148         super.toggleTab(mapping, form, request, response);
149         refreshSalesTaxInfo(form);
150 
151         return mapping.findForward(OLEConstants.MAPPING_BASIC);
152     }
153 
154 
155     
156     protected static final Set UPDATE_EVENT_ACTIONS;
157     static {
158         String[] updateEventActions = { OLEConstants.SAVE_METHOD, OLEConstants.ROUTE_METHOD, OLEConstants.APPROVE_METHOD, OLEConstants.BLANKET_APPROVE_METHOD };
159         UPDATE_EVENT_ACTIONS = new HashSet();
160         for (int i = 0; i < updateEventActions.length; ++i) {
161             UPDATE_EVENT_ACTIONS.add(updateEventActions[i]);
162         }
163     }
164 
165     
166 
167 
168     protected void processAccountingLineOverrides(KualiAccountingDocumentFormBase transForm) {
169         processAccountingLineOverrides(transForm.getNewSourceLine());
170         processAccountingLineOverrides(transForm.getNewTargetLine());
171         if (transForm.hasDocumentId()) {
172             AccountingDocument financialDocument = (AccountingDocument) transForm.getDocument();
173 
174             processAccountingLineOverrides(financialDocument,financialDocument.getSourceAccountingLines());
175             processAccountingLineOverrides(financialDocument,financialDocument.getTargetAccountingLines());
176         }
177     }
178 
179     
180 
181 
182     protected void processAccountingLineOverrides(AccountingLine line) {
183         processAccountingLineOverrides(Arrays.asList(new AccountingLine[] { line }));
184     }
185 
186     protected void processAccountingLineOverrides(List accountingLines) {
187         processAccountingLineOverrides(null,accountingLines);
188     }
189     
190 
191 
192     protected void processAccountingLineOverrides(AccountingDocument financialDocument ,List accountingLines) {
193         if (!accountingLines.isEmpty()) {
194             
195 
196             for (Iterator i = accountingLines.iterator(); i.hasNext();) {
197                 AccountingLine line = (AccountingLine) i.next();
198                
199                 SpringContext.getBean(PersistenceService.class).retrieveReferenceObjects(line, AccountingLineOverride.REFRESH_FIELDS);
200                 AccountingLineOverride.processForOutput(financialDocument,line);
201             }
202         }
203     }
204 
205     
206 
207 
208 
209 
210     protected void processAccountingLines(AccountingDocument transDoc, KualiAccountingDocumentFormBase transForm, String lineSet) {
211         
212         List formLines;
213         String pathPrefix;
214         boolean source;
215         if (lineSet.equals(OLEConstants.SOURCE)) {
216             formLines = transDoc.getSourceAccountingLines();
217             pathPrefix = OLEConstants.DOCUMENT_PROPERTY_NAME + "." + OLEConstants.EXISTING_SOURCE_ACCT_LINE_PROPERTY_NAME;
218             source = true;
219         }
220         else {
221             formLines = transDoc.getTargetAccountingLines();
222             pathPrefix = OLEConstants.DOCUMENT_PROPERTY_NAME + "." + OLEConstants.EXISTING_TARGET_ACCT_LINE_PROPERTY_NAME;
223             source = false;
224         }
225 
226         
227         int index = 0;
228         for (Iterator i = formLines.iterator(); i.hasNext(); index++) {
229             AccountingLine formLine = (AccountingLine) i.next();
230 
231             
232             
233             checkSalesTax(transDoc, formLine, source, false, index);
234         }
235     }
236 
237    
238 
239     
240 
241 
242 
243 
244 
245 
246 
247 
248 
249 
250     public ActionForward deleteTargetLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
251         KualiAccountingDocumentFormBase financialDocumentForm = (KualiAccountingDocumentFormBase) form;
252 
253         int deleteIndex = getLineToDelete(request);
254         String errorPath = OLEConstants.DOCUMENT_PROPERTY_NAME + "." + OLEConstants.EXISTING_TARGET_ACCT_LINE_PROPERTY_NAME + "[" + deleteIndex + "]";
255         boolean rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new DeleteAccountingLineEvent(errorPath, financialDocumentForm.getDocument(), ((AccountingDocument) financialDocumentForm.getDocument()).getTargetAccountingLine(deleteIndex), false));
256 
257         
258         if (rulePassed) {
259             deleteAccountingLine(false, financialDocumentForm, deleteIndex);
260         }
261         else {
262             String[] errorParams = new String[] { "target", Integer.toString(deleteIndex + 1) };
263             GlobalVariables.getMessageMap().putError(errorPath, OLEKeyConstants.ERROR_ACCOUNTINGLINE_DELETERULE_INVALIDACCOUNT, errorParams);
264         }
265 
266         return mapping.findForward(OLEConstants.MAPPING_BASIC);
267     }
268 
269     
270 
271 
272 
273 
274 
275 
276 
277 
278 
279 
280     public ActionForward deleteSourceLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
281         KualiAccountingDocumentFormBase financialDocumentForm = (KualiAccountingDocumentFormBase) form;
282 
283         int deleteIndex = getLineToDelete(request);
284         String errorPath = OLEConstants.DOCUMENT_PROPERTY_NAME + "." + OLEConstants.EXISTING_SOURCE_ACCT_LINE_PROPERTY_NAME + "[" + deleteIndex + "]";
285         boolean rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new DeleteAccountingLineEvent(errorPath, financialDocumentForm.getDocument(), ((AccountingDocument) financialDocumentForm.getDocument()).getSourceAccountingLine(deleteIndex), false));
286 
287         
288         if (rulePassed) {
289             deleteAccountingLine(true, financialDocumentForm, deleteIndex);
290         }
291         else {
292             String[] errorParams = new String[] { "source", Integer.toString(deleteIndex + 1) };
293             GlobalVariables.getMessageMap().putError(errorPath, OLEKeyConstants.ERROR_ACCOUNTINGLINE_DELETERULE_INVALIDACCOUNT, errorParams);
294         }
295 
296         return mapping.findForward(OLEConstants.MAPPING_BASIC);
297     }
298 
299 
300     
301 
302 
303 
304 
305 
306 
307 
308     protected void deleteAccountingLine(boolean isSource, KualiAccountingDocumentFormBase financialDocumentForm, int deleteIndex) {
309         if (isSource) {
310             
311             financialDocumentForm.getFinancialDocument().getSourceAccountingLines().remove(deleteIndex);
312 
313         }
314         else {
315             
316             financialDocumentForm.getFinancialDocument().getTargetAccountingLines().remove(deleteIndex);
317         }
318         
319         AccountingDocument tdoc = (AccountingDocument) financialDocumentForm.getDocument();
320         if (tdoc instanceof AmountTotaling) {
321             ((FinancialSystemDocumentHeader) financialDocumentForm.getDocument().getDocumentHeader()).setFinancialDocumentTotalAmount(((AmountTotaling) tdoc).getTotalDollarAmount());
322         }
323 
324     }
325 
326 
327     
328 
329 
330 
331 
332 
333 
334 
335 
336 
337 
338     public ActionForward uploadTargetLines(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
339 
340         
341         uploadAccountingLines(false, form);
342 
343         return mapping.findForward(OLEConstants.MAPPING_BASIC);
344     }
345 
346 
347     
348 
349 
350 
351 
352 
353 
354 
355 
356 
357 
358 
359     public ActionForward uploadSourceLines(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException {
360         LOG.info("Uploading source accounting lines");
361         
362         uploadAccountingLines(true, form);
363 
364         return mapping.findForward(OLEConstants.MAPPING_BASIC);
365     }
366 
367     
368 
369 
370 
371 
372 
373 
374 
375 
376     protected void uploadAccountingLines(boolean isSource, ActionForm form) throws FileNotFoundException, IOException {
377         KualiAccountingDocumentFormBase tmpForm = (KualiAccountingDocumentFormBase) form;
378 
379         List importedLines = null;
380 
381         AccountingDocument financialDocument = tmpForm.getFinancialDocument();
382         AccountingLineParser accountingLineParser = financialDocument.getAccountingLineParser();
383 
384         
385         String errorPathPrefix = null;
386         try {
387             if (isSource) {
388                 errorPathPrefix = OLEConstants.DOCUMENT_PROPERTY_NAME + "." + OLEConstants.SOURCE_ACCOUNTING_LINE_ERRORS;
389                 FormFile sourceFile = tmpForm.getSourceFile();
390                 checkUploadFile(sourceFile);
391                 importedLines = accountingLineParser.importSourceAccountingLines(sourceFile.getFileName(), sourceFile.getInputStream(), financialDocument);
392             }
393             else {
394                 errorPathPrefix = OLEConstants.DOCUMENT_PROPERTY_NAME + "." + OLEConstants.TARGET_ACCOUNTING_LINE_ERRORS;
395                 FormFile targetFile = tmpForm.getTargetFile();
396                 checkUploadFile(targetFile);
397                 importedLines = accountingLineParser.importTargetAccountingLines(targetFile.getFileName(), targetFile.getInputStream(), financialDocument);
398             }
399         }
400         catch (AccountingLineParserException e) {
401             GlobalVariables.getMessageMap().putError(errorPathPrefix, e.getErrorKey(), e.getErrorParameters());
402         }
403 
404         
405         if (importedLines != null) {
406             for (Iterator i = importedLines.iterator(); i.hasNext();) {
407                 AccountingLine importedLine = (AccountingLine) i.next();
408                 insertAccountingLine(isSource, tmpForm, importedLine);
409             }
410         }
411     }
412 
413     protected void checkUploadFile(FormFile file) {
414         if (file == null) {
415             throw new AccountingLineParserException("invalid (null) upload file", OLEKeyConstants.ERROR_UPLOADFILE_NULL);
416         }
417     }
418 
419     
420 
421 
422 
423 
424 
425 
426 
427 
428 
429 
430 
431     public ActionForward insertTargetLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
432         KualiAccountingDocumentFormBase financialDocumentForm = (KualiAccountingDocumentFormBase) form;
433         TargetAccountingLine line = financialDocumentForm.getNewTargetLine();
434         
435         
436         
437         
438         boolean rulePassed = true;
439         
440         
441         
442         rulePassed &= checkSalesTax((AccountingDocument) financialDocumentForm.getDocument(), line, false, true, 0);
443 
444         
445         rulePassed &= SpringContext.getBean(KualiRuleService.class).applyRules(new AddAccountingLineEvent(OLEConstants.NEW_TARGET_ACCT_LINE_PROPERTY_NAME, financialDocumentForm.getDocument(), line));
446 
447         
448         if (rulePassed) {
449             
450             SpringContext.getBean(PersistenceService.class).refreshAllNonUpdatingReferences(line);
451             insertAccountingLine(false, financialDocumentForm, line);
452 
453             
454             financialDocumentForm.setNewTargetLine(null);
455         }
456 
457         return mapping.findForward(OLEConstants.MAPPING_BASIC);
458     }
459 
460 
461     
462 
463 
464 
465 
466 
467 
468 
469 
470 
471 
472     public ActionForward insertSourceLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
473         KualiAccountingDocumentFormBase financialDocumentForm = (KualiAccountingDocumentFormBase) form;       
474         SourceAccountingLine line = financialDocumentForm.getNewSourceLine();
475         
476         
477         
478         
479         boolean rulePassed = true;
480         
481         
482         
483         rulePassed &= checkSalesTax((AccountingDocument) financialDocumentForm.getDocument(), line, true, true, 0);
484         
485         rulePassed &= SpringContext.getBean(KualiRuleService.class).applyRules(new AddAccountingLineEvent(OLEConstants.NEW_SOURCE_ACCT_LINE_PROPERTY_NAME, financialDocumentForm.getDocument(), line));
486 
487         if (rulePassed) {
488             
489             SpringContext.getBean(PersistenceService.class).refreshAllNonUpdatingReferences(line);
490             insertAccountingLine(true, financialDocumentForm, line);
491 
492             
493             financialDocumentForm.setNewSourceLine(null);
494         }
495 
496         return mapping.findForward(OLEConstants.MAPPING_BASIC);
497     }
498 
499     
500 
501 
502 
503 
504 
505 
506     protected void insertAccountingLine(boolean isSource, KualiAccountingDocumentFormBase financialDocumentForm, AccountingLine line) {
507         AccountingDocument tdoc = financialDocumentForm.getFinancialDocument();
508         if (isSource) {
509             
510             tdoc.addSourceAccountingLine((SourceAccountingLine) line);
511 
512             
513             if (line.isSalesTaxRequired()) {
514                 populateSalesTax(line);
515             }
516 
517             
518             if (tdoc instanceof AmountTotaling)
519                 ((FinancialSystemDocumentHeader) financialDocumentForm.getDocument().getDocumentHeader()).setFinancialDocumentTotalAmount(((AmountTotaling) tdoc).getTotalDollarAmount());
520         }
521         else {
522             
523             tdoc.addTargetAccountingLine((TargetAccountingLine) line);
524 
525             
526             if (line.isSalesTaxRequired()) {
527                 populateSalesTax(line);
528             }
529         }
530     }
531 
532     
533 
534 
535     protected List deepCopyAccountingLinesList(List originals) {
536         if (originals == null) {
537             return null;
538         }
539         List copiedLines = new ArrayList();
540         for (int i = 0; i < originals.size(); i++) {
541             copiedLines.add(ObjectUtils.deepCopy((AccountingLine) originals.get(i)));
542         }
543         return copiedLines;
544     }
545 
546     
547 
548 
549 
550 
551 
552 
553 
554 
555 
556 
557     public ActionForward showDetails(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
558         KualiAccountingDocumentFormBase tmpForm = (KualiAccountingDocumentFormBase) form;
559         tmpForm.setHideDetails(false);
560         return mapping.findForward(OLEConstants.MAPPING_BASIC);
561     }
562 
563     
564 
565 
566 
567 
568 
569 
570 
571 
572 
573 
574     public ActionForward hideDetails(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
575         KualiAccountingDocumentFormBase tmpForm = (KualiAccountingDocumentFormBase) form;
576         tmpForm.setHideDetails(true);
577         return mapping.findForward(OLEConstants.MAPPING_BASIC);
578     }
579 
580     
581 
582 
583 
584 
585 
586 
587 
588 
589 
590 
591     public ActionForward performBalanceInquiryForSourceLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
592         SourceAccountingLine line = this.getSourceAccountingLine(form, request);
593         return performBalanceInquiryForAccountingLine(mapping, form, request, line);
594     }
595 
596     
597 
598 
599 
600 
601 
602 
603 
604 
605 
606 
607     public ActionForward performBalanceInquiryForTargetLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
608         int lineIndex = getSelectedLine(request);
609 
610         TargetAccountingLine line = this.getTargetAccountingLine(form, request);
611 
612         return performBalanceInquiryForAccountingLine(mapping, form, request, line);
613     }
614 
615     
616 
617 
618 
619 
620 
621 
622 
623 
624 
625 
626     protected SourceAccountingLine getSourceAccountingLine(ActionForm form, HttpServletRequest request) {
627         int lineIndex = getSelectedLine(request);
628         SourceAccountingLine line = (SourceAccountingLine) ObjectUtils.deepCopy(((KualiAccountingDocumentFormBase) form).getFinancialDocument().getSourceAccountingLine(lineIndex));
629         return line;
630     }
631 
632     protected TargetAccountingLine getTargetAccountingLine(ActionForm form, HttpServletRequest request) {
633         int lineIndex = getSelectedLine(request);
634         TargetAccountingLine line = (TargetAccountingLine) ((KualiAccountingDocumentFormBase) form).getFinancialDocument().getTargetAccountingLine(lineIndex);
635 
636         return line;
637     }
638 
639     
640 
641 
642 
643 
644 
645 
646 
647 
648 
649     protected ActionForward performBalanceInquiryForAccountingLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, AccountingLine line) {
650         
651         String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
652 
653         
654         String callerDocFormKey = GlobalVariables.getUserSession().addObjectWithGeneratedKey(form);
655 
656         
657         Properties parameters = new Properties();
658         parameters.put(OLEConstants.DISPATCH_REQUEST_PARAMETER, OLEConstants.START_METHOD);
659         
660         
661         parameters.put(OLEConstants.BALANCE_INQUIRY_REPORT_MENU_CALLER_DOC_FORM_KEY, callerDocFormKey);
662         parameters.put(OLEConstants.DOC_FORM_KEY, callerDocFormKey);
663         parameters.put(OLEConstants.BACK_LOCATION, basePath + mapping.getPath() + ".do");
664 
665         if (line.getPostingYear() != null) {
666             parameters.put(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, line.getPostingYear().toString());
667         }
668         if (StringUtils.isNotBlank(line.getReferenceOriginCode())) {
669             parameters.put("referenceOriginCode", line.getReferenceOriginCode());
670         }
671         if (StringUtils.isNotBlank(line.getReferenceNumber())) {
672             parameters.put("referenceNumber", line.getReferenceNumber());
673         }
674         if (StringUtils.isNotBlank(line.getReferenceTypeCode())) {
675             parameters.put("referenceTypeCode", line.getReferenceTypeCode());
676         }
677         if (StringUtils.isNotBlank(line.getDebitCreditCode())) {
678             parameters.put("debitCreditCode", line.getDebitCreditCode());
679         }
680         if (StringUtils.isNotBlank(line.getChartOfAccountsCode())) {
681             parameters.put("chartOfAccountsCode", line.getChartOfAccountsCode());
682         }
683         if (StringUtils.isNotBlank(line.getAccountNumber())) {
684             parameters.put("accountNumber", line.getAccountNumber());
685         }
686         if (StringUtils.isNotBlank(line.getFinancialObjectCode())) {
687             parameters.put("financialObjectCode", line.getFinancialObjectCode());
688         }
689         if (StringUtils.isNotBlank(line.getSubAccountNumber())) {
690             parameters.put("subAccountNumber", line.getSubAccountNumber());
691         }
692         if (StringUtils.isNotBlank(line.getFinancialSubObjectCode())) {
693             parameters.put("financialSubObjectCode", line.getFinancialSubObjectCode());
694         }
695         if (StringUtils.isNotBlank(line.getProjectCode())) {
696             parameters.put("projectCode", line.getProjectCode());
697         }
698         if (StringUtils.isNotBlank(getObjectTypeCodeFromLine(line))) {
699             if (!StringUtils.isBlank(line.getObjectTypeCode())) {
700                 parameters.put("objectTypeCode", line.getObjectTypeCode());
701             }
702             else {
703                 line.refreshReferenceObject("objectCode");
704                 parameters.put("objectTypeCode", line.getObjectCode().getFinancialObjectTypeCode());
705             }
706         }
707 
708         String lookupUrl = UrlFactory.parameterizeUrl(basePath + "/" + OLEConstants.BALANCE_INQUIRY_REPORT_MENU_ACTION, parameters);
709 
710         
711         ((KualiAccountingDocumentFormBase) form).registerEditableProperty(KRADConstants.DISPATCH_REQUEST_PARAMETER);
712 
713         return new ActionForward(lookupUrl, true);
714     }
715 
716     
717 
718 
719 
720 
721 
722 
723     protected String getObjectTypeCodeFromLine(AccountingLine line) {
724         line.refreshReferenceObject("objectCode");
725         return line.getObjectCode().getFinancialObjectTypeCode();
726     }
727 
728     @Override
729     public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
730         KualiAccountingDocumentFormBase tmpForm = (KualiAccountingDocumentFormBase) form;
731 
732         ActionForward forward = super.save(mapping, form, request, response);
733 
734         
735         checkSalesTaxRequiredAllLines(tmpForm, tmpForm.getFinancialDocument().getSourceAccountingLines());
736         checkSalesTaxRequiredAllLines(tmpForm, tmpForm.getFinancialDocument().getTargetAccountingLines());
737         return forward;
738     }
739 
740     @Override
741     public ActionForward approve(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
742         KualiAccountingDocumentFormBase tmpForm = (KualiAccountingDocumentFormBase) form;
743 
744         ActionForward forward = super.approve(mapping, form, request, response);
745 
746         
747         checkSalesTaxRequiredAllLines(tmpForm, tmpForm.getFinancialDocument().getSourceAccountingLines());
748         checkSalesTaxRequiredAllLines(tmpForm, tmpForm.getFinancialDocument().getTargetAccountingLines());
749 
750         return forward;
751     }
752 
753     @Override
754     public ActionForward route(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
755         KualiAccountingDocumentFormBase tmpForm = (KualiAccountingDocumentFormBase) form;
756      
757 
758         ActionForward forward = super.route(mapping, form, request, response);
759 
760         checkSalesTaxRequiredAllLines(tmpForm, tmpForm.getFinancialDocument().getSourceAccountingLines());
761         checkSalesTaxRequiredAllLines(tmpForm, tmpForm.getFinancialDocument().getTargetAccountingLines());
762 
763         return forward;
764     }
765 
766     @Override
767     public ActionForward blanketApprove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
768         KualiAccountingDocumentFormBase tmpForm = (KualiAccountingDocumentFormBase) form;
769 
770         checkSalesTaxRequiredAllLines(tmpForm, tmpForm.getFinancialDocument().getSourceAccountingLines());
771         checkSalesTaxRequiredAllLines(tmpForm, tmpForm.getFinancialDocument().getTargetAccountingLines());
772 
773         ActionForward forward = super.blanketApprove(mapping, form, request, response);
774 
775         return forward;
776     }
777 
778     
779 
780 
781 
782 
783 
784 
785     protected boolean checkSalesTax(AccountingDocument document, AccountingLine line, boolean source, boolean newLine, int index) {
786         boolean passed = true;
787         if (isSalesTaxRequired(document, line)) {
788             
789             line.setSalesTaxRequired(true);
790             populateSalesTax(line);
791             
792             passed &= isValidSalesTaxEntered(line, source, newLine, index);
793         } else {
794             
795             line.setSalesTax(null);
796         }
797         return passed;
798     }
799 
800     
801 
802 
803 
804 
805 
806 
807 
808 
809     protected boolean isSalesTaxRequired(AccountingDocument financialDocument, AccountingLine accountingLine) {
810         boolean required = false;
811         String docType = SpringContext.getBean(DataDictionaryService.class).getDocumentTypeNameByClass(financialDocument.getClass());
812         
813         ParameterService parameterService = SpringContext.getBean(ParameterService.class);
814         
815         ParameterEvaluator docTypeSalesTaxCheckEvaluator = SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(OleParameterConstants.FINANCIAL_PROCESSING_DOCUMENT.class, APPLICATION_PARAMETER.DOCTYPE_SALES_TAX_CHECK, docType);
816         if (docTypeSalesTaxCheckEvaluator.evaluationSucceeds()) {
817             required = true;
818         }
819 
820         
821         if (required) {
822             
823             String objCd = accountingLine.getFinancialObjectCode();
824             String account = accountingLine.getAccountNumber();
825             if (!StringUtils.isEmpty(objCd) && !StringUtils.isEmpty(account)) {
826                 String compare = account + ":" + objCd;
827                 ParameterEvaluator salesTaxApplicableAcctAndObjectEvaluator = SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(OleParameterConstants.FINANCIAL_PROCESSING_DOCUMENT.class, APPLICATION_PARAMETER.SALES_TAX_APPLICABLE_ACCOUNTS_AND_OBJECT_CODES, compare);
828                 if (!salesTaxApplicableAcctAndObjectEvaluator.evaluationSucceeds()) {
829                     required = false;
830                 }
831             }
832             else {
833                 
834                 required = false;
835             }
836         }
837         return required;
838     }
839 
840     
841 
842 
843 
844 
845 
846     protected boolean isValidSalesTaxEntered(AccountingLine accountingLine, boolean source, boolean newLine, int index) {
847         boolean valid = true;
848         DictionaryValidationService dictionaryValidationService = SpringContext.getBean(DictionaryValidationService.class);
849         BusinessObjectService boService = SpringContext.getBean(BusinessObjectService.class);
850         String objCd = accountingLine.getFinancialObjectCode();
851         String account = accountingLine.getAccountNumber();
852         SalesTax salesTax = accountingLine.getSalesTax();
853         String pathPrefix = "";
854         if (source && !newLine) {
855             pathPrefix = "document." + OLEConstants.EXISTING_SOURCE_ACCT_LINE_PROPERTY_NAME + "[" + index + "]";
856         }
857         else if (!source && !newLine) {
858             pathPrefix = "document." + OLEConstants.EXISTING_TARGET_ACCT_LINE_PROPERTY_NAME + "[" + index + "]";
859         }
860         else if (source && newLine) {
861             pathPrefix = OLEConstants.NEW_SOURCE_ACCT_LINE_PROPERTY_NAME;
862         }
863         else if (!source && newLine) {
864             pathPrefix = OLEConstants.NEW_TARGET_ACCT_LINE_PROPERTY_NAME;
865         }
866         GlobalVariables.getMessageMap().addToErrorPath(pathPrefix);
867         if (ObjectUtils.isNull(salesTax)) {
868             valid &= false;
869             GlobalVariables.getMessageMap().putError("salesTax.chartOfAccountsCode", ERROR_DOCUMENT_ACCOUNTING_LINE_SALES_TAX_REQUIRED, account, objCd);
870         }
871         else {
872 
873             if (StringUtils.isBlank(salesTax.getChartOfAccountsCode())) {
874                 valid &= false;
875                 GlobalVariables.getMessageMap().putError("salesTax.chartOfAccountsCode", ERROR_REQUIRED, "Chart of Accounts");
876             }
877             if (StringUtils.isBlank(salesTax.getAccountNumber())) {
878                 valid &= false;
879                 GlobalVariables.getMessageMap().putError("salesTax.accountNumber", ERROR_REQUIRED, "Account Number");
880             }
881             if (salesTax.getFinancialDocumentGrossSalesAmount() == null) {
882                 valid &= false;
883                 GlobalVariables.getMessageMap().putError("salesTax.financialDocumentGrossSalesAmount", ERROR_REQUIRED, "Gross Sales Amount");
884             }
885             if (salesTax.getFinancialDocumentTaxableSalesAmount() == null) {
886                 valid &= false;
887                 GlobalVariables.getMessageMap().putError("salesTax.financialDocumentTaxableSalesAmount", ERROR_REQUIRED, "Taxable Sales Amount");
888             }
889             if (salesTax.getFinancialDocumentSaleDate() == null) {
890                 valid &= false;
891                 GlobalVariables.getMessageMap().putError("salesTax.financialDocumentSaleDate", ERROR_REQUIRED, "Sale Date");
892             }
893             if (StringUtils.isNotBlank(salesTax.getChartOfAccountsCode()) && StringUtils.isNotBlank(salesTax.getAccountNumber())) {
894 
895                 if (boService.getReferenceIfExists(salesTax, "account") == null) {
896                     valid &= false;
897                     GlobalVariables.getMessageMap().putError("salesTax.accountNumber", ERROR_DOCUMENT_ACCOUNTING_LINE_SALES_TAX_INVALID_ACCOUNT, salesTax.getChartOfAccountsCode(), salesTax.getAccountNumber());
898 
899                 }
900             }
901             if (!valid) {
902                 GlobalVariables.getMessageMap().putError("salesTax.chartOfAccountsCode", ERROR_DOCUMENT_ACCOUNTING_LINE_SALES_TAX_REQUIRED, account, objCd);
903             }
904         }
905         GlobalVariables.getMessageMap().removeFromErrorPath(pathPrefix);
906         return valid;
907     }
908 
909     
910 
911 
912 
913 
914     protected void removeSalesTax(AccountingLine accountingLine) {
915         SalesTax salesTax = accountingLine.getSalesTax();
916         if (ObjectUtils.isNotNull(salesTax)) {
917             accountingLine.setSalesTax(null);
918         }
919     }
920 
921 
922     
923 
924 
925 
926 
927 
928 
929 
930 
931 
932     protected void handleSalesTaxRequired(AccountingDocument transDoc, AccountingLine formLine, boolean source, boolean newLine, int index) {
933         boolean salesTaxRequired = isSalesTaxRequired(transDoc, formLine);
934         if (salesTaxRequired) {
935             formLine.setSalesTaxRequired(true);
936             populateSalesTax(formLine);
937         }
938         else if (!salesTaxRequired && hasSalesTaxBeenEntered(formLine, source, newLine, index)) {
939             
940             removeSalesTax(formLine);
941             formLine.setSalesTax(null);
942         }
943         
944         if (!salesTaxRequired) {
945             formLine.setSalesTax(null);
946         }
947     }
948 
949     protected boolean hasSalesTaxBeenEntered(AccountingLine accountingLine, boolean source, boolean newLine, int index) {
950         boolean entered = true;
951         String objCd = accountingLine.getFinancialObjectCode();
952         String account = accountingLine.getAccountNumber();
953         SalesTax salesTax = accountingLine.getSalesTax();
954         if (ObjectUtils.isNull(salesTax)) {
955             return false;
956         }
957         if (StringUtils.isBlank(salesTax.getChartOfAccountsCode())) {
958             entered &= false;
959         }
960         if (StringUtils.isBlank(salesTax.getAccountNumber())) {
961             entered &= false;
962         }
963         if (salesTax.getFinancialDocumentGrossSalesAmount() == null) {
964             entered &= false;
965         }
966         if (salesTax.getFinancialDocumentTaxableSalesAmount() == null) {
967             entered &= false;
968         }
969         if (salesTax.getFinancialDocumentSaleDate() == null) {
970             entered &= false;
971         }
972         return entered;
973     }
974 
975     
976 
977 
978 
979 
980 
981 
982     protected void handleSalesTaxRequiredAllLines(KualiDocumentFormBase kualiDocumentFormBase, List<AccountingLine> baselineAcctingLines) {
983         AccountingDocument accoutingDocument = (AccountingDocument) kualiDocumentFormBase.getDocument();
984         int index = 0;
985         for (AccountingLine accountingLine : baselineAcctingLines) {
986             boolean source = false;
987             if (accountingLine.isSourceAccountingLine()) {
988                 source = true;
989             }
990             handleSalesTaxRequired(accoutingDocument, accountingLine, source, false, index);
991             index++;
992         }
993 
994     }
995 
996     protected boolean checkSalesTaxRequiredAllLines(KualiDocumentFormBase kualiDocumentFormBase, List<AccountingLine> baselineAcctingLines) {
997         AccountingDocument accoutingDocument = (AccountingDocument) kualiDocumentFormBase.getDocument();
998         boolean passed = true;
999         int index = 0;
1000         for (AccountingLine accountingLine : baselineAcctingLines) {
1001             boolean source = false;
1002             if (accountingLine.isSourceAccountingLine()) {
1003                 source = true;
1004             }
1005             passed &= checkSalesTax(accoutingDocument, accountingLine, source, false, index);
1006             index++;
1007         }
1008         return passed;
1009     }
1010 
1011     
1012 
1013 
1014 
1015 
1016 
1017     protected void refreshSalesTaxInfo(ActionForm form) {
1018         KualiAccountingDocumentFormBase accountingForm = (KualiAccountingDocumentFormBase) form;
1019         AccountingDocument document = (AccountingDocument) accountingForm.getDocument();
1020         List sourceLines = document.getSourceAccountingLines();
1021         List targetLines = document.getTargetAccountingLines();
1022         handleSalesTaxRequiredAllLines(accountingForm, sourceLines);
1023         handleSalesTaxRequiredAllLines(accountingForm, targetLines);
1024 
1025         AccountingLine newTargetLine = accountingForm.getNewTargetLine();
1026         AccountingLine newSourceLine = accountingForm.getNewSourceLine();
1027         if (newTargetLine != null) {
1028             handleSalesTaxRequired(document, newTargetLine, false, true, 0);
1029         }
1030         if (newSourceLine != null) {
1031             handleSalesTaxRequired(document, newSourceLine, true, true, 0);
1032         }
1033     }
1034 
1035     
1036 
1037 
1038 
1039 
1040 
1041     protected void populateSalesTax(AccountingLine line) {
1042         SalesTax salesTax = line.getSalesTax();
1043 
1044         if (ObjectUtils.isNotNull(salesTax)) {
1045             salesTax.setDocumentNumber(line.getDocumentNumber());
1046             salesTax.setFinancialDocumentLineTypeCode(line.getFinancialDocumentLineTypeCode());
1047             salesTax.setFinancialDocumentLineNumber(line.getSequenceNumber());
1048         }
1049     }
1050 
1051     @Override
1052     public ActionForward performLookup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
1053         
1054         String fullParameter = (String) request.getAttribute(OLEConstants.METHOD_TO_CALL_ATTRIBUTE);
1055         String boClassName = StringUtils.substringBetween(fullParameter, OLEConstants.METHOD_TO_CALL_BOPARM_LEFT_DEL, OLEConstants.METHOD_TO_CALL_BOPARM_RIGHT_DEL);
1056 
1057         if (!StringUtils.equals(boClassName, GeneralLedgerPendingEntry.class.getName())) {
1058             return super.performLookup(mapping, form, request, response);
1059         }
1060 
1061         String path = super.performLookup(mapping, form, request, response).getPath();
1062         path = path.replaceFirst(OLEConstants.LOOKUP_ACTION, OLEConstants.GL_MODIFIED_INQUIRY_ACTION);
1063 
1064         return new ActionForward(path, true);
1065     }
1066     
1067 }