1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.module.purap.document.web.struts;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.struts.action.ActionForm;
20  import org.apache.struts.action.ActionForward;
21  import org.apache.struts.action.ActionMapping;
22  import org.apache.struts.upload.FormFile;
23  import org.kuali.ole.module.purap.PurapConstants;
24  import org.kuali.ole.module.purap.PurapPropertyConstants;
25  import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
26  import org.kuali.ole.module.purap.businessobject.PurApAccountingLineParser;
27  import org.kuali.ole.module.purap.businessobject.PurApItem;
28  import org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocument;
29  import org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocumentBase;
30  import org.kuali.ole.module.purap.document.service.PurapService;
31  import org.kuali.ole.module.purap.service.PurapAccountingService;
32  import org.kuali.ole.sys.OLEConstants;
33  import org.kuali.ole.sys.OLEPropertyConstants;
34  import org.kuali.ole.sys.businessobject.AccountingLine;
35  import org.kuali.ole.sys.businessobject.SourceAccountingLine;
36  import org.kuali.ole.sys.context.SpringContext;
37  import org.kuali.ole.sys.document.validation.event.AddAccountingLineEvent;
38  import org.kuali.ole.sys.exception.AccountingLineParserException;
39  import org.kuali.ole.sys.web.struts.KualiAccountingDocumentActionBase;
40  import org.kuali.ole.sys.web.struts.KualiAccountingDocumentFormBase;
41  import org.kuali.rice.core.api.util.RiceConstants;
42  import org.kuali.rice.kew.api.WorkflowDocument;
43  import org.kuali.rice.kew.api.exception.WorkflowException;
44  import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
45  import org.kuali.rice.kns.web.struts.form.KualiForm;
46  import org.kuali.rice.krad.service.DocumentService;
47  import org.kuali.rice.krad.service.KualiRuleService;
48  import org.kuali.rice.krad.service.PersistenceService;
49  import org.kuali.rice.krad.util.GlobalVariables;
50  import org.kuali.rice.krad.util.ObjectUtils;
51  
52  import javax.servlet.http.HttpServletRequest;
53  import javax.servlet.http.HttpServletResponse;
54  import java.io.FileNotFoundException;
55  import java.io.IOException;
56  import java.math.BigDecimal;
57  import java.util.HashMap;
58  import java.util.Iterator;
59  import java.util.List;
60  import java.util.Map;
61  import java.util.Map.Entry;
62  
63  
64  
65  
66  public class PurchasingAccountsPayableActionBase extends KualiAccountingDocumentActionBase {
67  
68      
69  
70  
71      @Override
72      public ActionForward copy(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
73          ActionForward actionForward = super.copy(mapping, form, request, response);
74  
75  
76          KualiDocumentFormBase kualiDocumentFormBase = (KualiDocumentFormBase) form;
77          PurchasingAccountsPayableDocument purapDocument = (PurchasingAccountsPayableDocument) kualiDocumentFormBase.getDocument();
78  
79          
80          List<PurApItem> items = purapDocument.getItems();
81  
82          for (PurApItem item : items) {
83              item.getNewSourceLine().setAccountLinePercent(new BigDecimal(0));
84              
85              for (PurApAccountingLine account : item.getSourceAccountingLines()) {
86                  account.setSequenceNumber(0);
87              }
88          }
89  
90          return actionForward;
91      }
92  
93      
94  
95  
96      @Override
97      protected void loadDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
98          super.loadDocument(kualiDocumentFormBase);
99          PurchasingAccountsPayableFormBase purapForm = (PurchasingAccountsPayableFormBase) kualiDocumentFormBase;
100         PurchasingAccountsPayableDocument document = (PurchasingAccountsPayableDocument) purapForm.getDocument();
101 
102         
103         purapForm.refreshAccountSummmary();
104 
105         for (org.kuali.rice.krad.bo.Note note : (java.util.List<org.kuali.rice.krad.bo.Note>) document.getNotes()) {
106             note.refreshReferenceObject("attachment");
107         }
108 
109         
110         SpringContext.getBean(PurapService.class).sortBelowTheLine(document);
111 
112         updateBaseline(document, (PurchasingAccountsPayableFormBase) kualiDocumentFormBase);
113     }
114 
115     
116 
117 
118 
119 
120     protected <T extends PurchasingAccountsPayableDocument, V extends KualiAccountingDocumentFormBase> void updateBaseline(T document, V form) {
121         
122         for (PurApItem item : document.getItems()) {
123             
124             item.getBaselineSourceAccountingLines().clear();
125 
126             for (PurApAccountingLine sourceAccount : item.getSourceAccountingLines()) {
127                 
128                 
129                 
130                 item.getBaselineSourceAccountingLines().add(sourceAccount);
131             }
132         }
133     }
134 
135     
136 
137 
138 
139 
140 
141 
142 
143 
144 
145     public ActionForward refreshAccountSummary(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
146         PurchasingAccountsPayableFormBase purapForm = (PurchasingAccountsPayableFormBase) form;
147         PurchasingAccountsPayableDocument document = (PurchasingAccountsPayableDocument) purapForm.getDocument();
148         SpringContext.getBean(PurapAccountingService.class).updateAccountAmounts(document);
149         purapForm.refreshAccountSummmary();
150         return mapping.findForward(OLEConstants.MAPPING_BASIC);
151     }
152 
153     
154 
155 
156     @Override
157     protected void uploadAccountingLines(boolean isSource, ActionForm form) throws FileNotFoundException, IOException {
158         PurchasingAccountsPayableFormBase purapForm = (PurchasingAccountsPayableFormBase) form;
159         PurchasingAccountsPayableDocumentBase purapDocument = (PurchasingAccountsPayableDocumentBase) purapForm.getFinancialDocument();
160         PurApAccountingLineParser accountingLineParser = (PurApAccountingLineParser) purapDocument.getAccountingLineParser();
161         List importedLines = null;
162         String errorPathPrefix = PurapConstants.ACCOUNT_DISTRIBUTION_ERROR_KEY;
163         
164 
165         
166         try {
167             FormFile sourceFile = purapForm.getSourceFile();
168             checkUploadFile(sourceFile);
169             GlobalVariables.getMessageMap().clearErrorPath();
170             GlobalVariables.getMessageMap().addToErrorPath(errorPathPrefix);
171             importedLines = accountingLineParser.importSourceAccountingLines(sourceFile.getFileName(), sourceFile.getInputStream(), purapDocument);
172             GlobalVariables.getMessageMap().removeFromErrorPath(errorPathPrefix);
173         } catch (AccountingLineParserException e) {
174             GlobalVariables.getMessageMap().putError(errorPathPrefix, e.getErrorKey(), e.getErrorParameters());
175         }
176 
177         
178         if (importedLines != null) {
179             for (Iterator iter = importedLines.iterator(); iter.hasNext(); ) {
180                 PurApAccountingLine importedLine = (PurApAccountingLine) iter.next();
181                 
182                 
183                 
184                 SpringContext.getBean(PersistenceService.class).retrieveNonKeyFields(importedLine);
185                 ((PurchasingFormBase) purapForm).addAccountDistributionsourceAccountingLine(importedLine);
186                 
187             }
188         }
189     }
190 
191     
192 
193 
194 
195     @Override
196     public ActionForward insertSourceLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
197         
198         PurchasingAccountsPayableFormBase purapForm = (PurchasingAccountsPayableFormBase) form;
199 
200         
201         int itemIndex = getSelectedLine(request);
202         PurApItem item = null;
203 
204         
205         if (processCustomInsertAccountingLine(purapForm, request) == false) {
206             String errorPrefix = null;
207             PurApAccountingLine line = null;
208             boolean rulePassed = false;
209             if (itemIndex >= 0) {
210                 item = (PurApItem) ((PurchasingAccountsPayableDocument) purapForm.getDocument()).getItem((itemIndex));
211                 line = (PurApAccountingLine) ObjectUtils.deepCopy(item.getNewSourceLine());
212                 
213                 errorPrefix = OLEPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.ITEM + "[" + Integer.toString(itemIndex) + "]." + OLEConstants.NEW_SOURCE_ACCT_LINE_PROPERTY_NAME;
214                 rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new AddAccountingLineEvent(errorPrefix, purapForm.getDocument(), (AccountingLine) line));
215             } else if (itemIndex == -2) {
216                 
217                 
218                 line = ((PurchasingFormBase) purapForm).getAccountDistributionnewSourceLine();
219                 
220                 errorPrefix = PurapPropertyConstants.ACCOUNT_DISTRIBUTION_NEW_SRC_LINE;
221                 rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new AddAccountingLineEvent(errorPrefix, purapForm.getDocument(), (AccountingLine) line));
222             }
223 
224             if (rulePassed) {
225                 
226                 SpringContext.getBean(PersistenceService.class).retrieveNonKeyFields(line);
227                 if (itemIndex >= 0) {
228                     insertAccountingLine(purapForm, item, line);
229                     
230                     item.resetAccount();
231                 } else if (itemIndex == -2) {
232                     
233                     ((PurchasingFormBase) purapForm).addAccountDistributionsourceAccountingLine(line);
234                 }
235             }
236         }
237 
238         return mapping.findForward(OLEConstants.MAPPING_BASIC);
239     }
240 
241     
242 
243 
244 
245 
246 
247 
248     protected void insertAccountingLine(PurchasingAccountsPayableFormBase financialDocumentForm, PurApItem item, PurApAccountingLine line) {
249         PurchasingAccountsPayableDocument preq = (PurchasingAccountsPayableDocument) financialDocumentForm.getDocument();
250 
251         Integer index = item.getSourceAccountingLines().size() + 1;
252         line.setSequenceNumber(index);
253         
254         item.getSourceAccountingLines().add(line);
255     }
256 
257     
258 
259 
260 
261 
262 
263 
264 
265     public boolean processCustomInsertAccountingLine(PurchasingAccountsPayableFormBase purapForm, HttpServletRequest request) {
266         return false;
267     }
268 
269     
270 
271 
272 
273 
274 
275 
276     protected void insertAccountingLine(KualiAccountingDocumentFormBase financialDocumentForm, PurApItem item, PurApAccountingLine line) {
277         
278         item.getSourceAccountingLines().add(line);
279     }
280 
281     
282 
283 
284 
285     @Override
286     public ActionForward deleteSourceLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
287         PurchasingAccountsPayableFormBase purapForm = (PurchasingAccountsPayableFormBase) form;
288 
289         String[] indexes = getSelectedLineForAccounts(request);
290         int itemIndex = Integer.parseInt(indexes[0]);
291         int accountIndex = Integer.parseInt(indexes[1]);
292 
293         PurApItem item = (PurApItem) ((PurchasingAccountsPayableDocument) purapForm.getDocument()).getItem((itemIndex));
294         if (itemIndex == -2) {
295             item.getSourceAccountingLines().remove(accountIndex);
296         } else {
297             item.getSourceAccountingLines().remove(accountIndex);
298             List<PurApAccountingLine> purApAccountingLineList = item.getSourceAccountingLines();
299             BigDecimal initialPercent = new BigDecimal(0);
300             for (PurApAccountingLine purApAccountingLine : purApAccountingLineList) {
301                 initialPercent = initialPercent.add(purApAccountingLine.getAccountLinePercent());
302             }
303             initialPercent = new BigDecimal(100).subtract(initialPercent);
304             if(initialPercent.intValue()>0){
305                 item.resetAccount(initialPercent);
306             }
307             else{
308                 item.resetAccount(new BigDecimal(0));
309             }
310         }
311         return mapping.findForward(OLEConstants.MAPPING_BASIC);
312     }
313 
314     
315 
316 
317 
318     @Override
319     public SourceAccountingLine getSourceAccountingLine(ActionForm form, HttpServletRequest request) {
320         String[] indexes = getSelectedLineForAccounts(request);
321         int itemIndex = Integer.parseInt(indexes[0]);
322         int accountIndex = Integer.parseInt(indexes[1]);
323         PurchasingAccountsPayableFormBase purchasingAccountsPayableForm = (PurchasingAccountsPayableFormBase) form;
324         SourceAccountingLine line;
325         if (itemIndex == -2) {
326             line = customAccountRetrieval(accountIndex, purchasingAccountsPayableForm);
327         } else {
328             PurApItem item = (PurApItem) ((PurchasingAccountsPayableDocument) purchasingAccountsPayableForm.getDocument()).getItem((itemIndex));
329             line = (SourceAccountingLine) ObjectUtils.deepCopy(item.getSourceAccountingLines().get(accountIndex));
330         }
331         return line;
332     }
333 
334     
335 
336 
337 
338 
339 
340 
341     protected SourceAccountingLine customAccountRetrieval(int accountIndex, PurchasingAccountsPayableFormBase purchasingAccountsPayableForm) {
342         
343         return null;
344     }
345 
346     
347 
348 
349 
350 
351 
352 
353 
354     protected String[] getSelectedLineForAccounts(HttpServletRequest request) {
355         String accountString = new String();
356         String parameterName = (String) request.getAttribute(OLEConstants.METHOD_TO_CALL_ATTRIBUTE);
357         if (StringUtils.isNotBlank(parameterName)) {
358             accountString = StringUtils.substringBetween(parameterName, ".line", ".");
359         }
360         String[] result = StringUtils.split(accountString, ":");
361 
362         return result;
363     }
364 
365     
366 
367 
368 
369     @Override
370     public ActionForward downloadBOAttachment(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
371         PurchasingAccountsPayableDocument document = (PurchasingAccountsPayableDocument) ((PurchasingAccountsPayableFormBase) form).getDocument();
372 
373         for (org.kuali.rice.krad.bo.Note note : (java.util.List<org.kuali.rice.krad.bo.Note>) document.getNotes()) {
374             note.refreshReferenceObject("attachment");
375         }
376 
377         return super.downloadBOAttachment(mapping, form, request, response);
378     }
379 
380     @Override
381     protected void processAccountingLineOverrides(List accountingLines) {
382         
383     }
384 
385     
386 
387 
388 
389 
390 
391 
392 
393 
394     public ActionForward calculate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
395         return mapping.findForward(OLEConstants.MAPPING_BASIC);
396     }
397 
398     public ActionForward clearAllTaxes(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
399         return mapping.findForward(OLEConstants.MAPPING_BASIC);
400     }
401 
402     protected void customCalculate(PurchasingAccountsPayableDocument purapDoc) {
403         
404     }
405 
406     
407 
408 
409 
410 
411 
412 
413 
414 
415 
416     public ActionForward showAllAccounts(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
417         KualiForm kualiForm = (KualiForm) form;
418         String accountingLineTab = "AccountingLines";
419         String value = null;
420 
421         Map<String, String> tabStates = kualiForm.getTabStates();
422         Map<String, String> newTabStates = new HashMap<String, String>();
423         for (Entry<String, String> tabEntry : tabStates.entrySet()) {
424             if (tabEntry.getKey().startsWith(accountingLineTab)) {
425                 newTabStates.put(tabEntry.getKey(), "OPEN");
426             } else {
427                 if (tabEntry.getValue() instanceof String) {
428                     value = tabEntry.getValue();
429                 } else {
430                     
431                     
432                     Object result = tabEntry.getValue();
433                     result.getClass();
434                     value = ((String[]) result)[0];
435                 }
436                 newTabStates.put(tabEntry.getKey(), value);
437             }
438         }
439         kualiForm.setTabStates(newTabStates);
440         return mapping.findForward(RiceConstants.MAPPING_BASIC);
441     }
442 
443     
444 
445 
446 
447 
448 
449 
450 
451 
452 
453     public ActionForward hideAllAccounts(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
454         KualiForm kualiForm = (KualiForm) form;
455         String accountingLineTab = "AccountingLines";
456         String value = null;
457 
458         Map<String, String> tabStates = kualiForm.getTabStates();
459         Map<String, String> newTabStates = new HashMap<String, String>();
460         for (Entry<String, String> tabEntry : tabStates.entrySet()) {
461             if (tabEntry.getKey().startsWith(accountingLineTab)) {
462                 newTabStates.put(tabEntry.getKey(), "CLOSE");
463             } else {
464                 if (tabEntry.getValue() instanceof String) {
465                     value = tabEntry.getValue();
466                 } else {
467                     
468                     
469                     Object result = tabEntry.getValue();
470                     result.getClass();
471                     value = ((String[]) result)[0];
472                 }
473                 newTabStates.put(tabEntry.getKey(), value);
474             }
475         }
476         kualiForm.setTabStates(newTabStates);
477         return mapping.findForward(RiceConstants.MAPPING_BASIC);
478     }
479 
480     
481 
482 
483 
484 
485 
486 
487     @Override
488     public ActionForward insertBONote(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
489         PurchasingAccountsPayableDocument document = (PurchasingAccountsPayableDocument) ((PurchasingAccountsPayableFormBase) form).getDocument();
490         WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
491 
492         if (workflowDocument.isInitiated()) {
493             SpringContext.getBean(DocumentService.class).saveDocument(document);
494         }
495 
496         return super.insertBONote(mapping, form, request, response);
497     }
498 
499 }