1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.sys.document;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.ole.gl.service.SufficientFundsService;
23  import org.kuali.ole.sys.OLEConstants;
24  import org.kuali.ole.sys.OLEKeyConstants;
25  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
26  import org.kuali.ole.sys.businessobject.SufficientFundsItem;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.ole.sys.service.GeneralLedgerPendingEntryService;
29  import org.kuali.rice.kew.api.exception.WorkflowException;
30  import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
31  import org.kuali.rice.krad.exception.ValidationException;
32  import org.kuali.rice.krad.rules.rule.event.ApproveDocumentEvent;
33  import org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent;
34  import org.kuali.rice.krad.rules.rule.event.RouteDocumentEvent;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  
37  
38  
39  
40  public class GeneralLedgerPostingDocumentBase extends LedgerPostingDocumentBase implements GeneralLedgerPostingDocument {
41      protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(GeneralLedgerPostingDocumentBase.class);
42  
43      protected List<GeneralLedgerPendingEntry> generalLedgerPendingEntries;
44  
45      
46  
47  
48      public GeneralLedgerPostingDocumentBase() {
49          super();
50          setGeneralLedgerPendingEntries(new ArrayList<GeneralLedgerPendingEntry>());
51      }
52  
53      
54  
55  
56      public List<GeneralLedgerPendingEntry> getGeneralLedgerPendingEntries() {
57          return generalLedgerPendingEntries;
58      }
59  
60      
61  
62  
63      public GeneralLedgerPendingEntry getGeneralLedgerPendingEntry(int index) {
64          while (generalLedgerPendingEntries.size() <= index) {
65              generalLedgerPendingEntries.add(new GeneralLedgerPendingEntry());
66          }
67          return generalLedgerPendingEntries.get(index);
68      }
69  
70      
71  
72  
73      public void setGeneralLedgerPendingEntries(List<GeneralLedgerPendingEntry> generalLedgerPendingEntries) {
74          this.generalLedgerPendingEntries = generalLedgerPendingEntries;
75      }
76  
77      
78  
79  
80      public List<SufficientFundsItem> checkSufficientFunds() {
81          LOG.debug("checkSufficientFunds() started");
82  
83          if (documentPerformsSufficientFundsCheck()) {
84              SufficientFundsService sufficientFundsService = SpringContext.getBean(SufficientFundsService.class);
85              return sufficientFundsService.checkSufficientFunds(this);
86          }
87          else {
88              return new ArrayList<SufficientFundsItem>();
89          }
90      }
91  
92      
93  
94  
95  
96  
97  
98  
99      public boolean documentPerformsSufficientFundsCheck() {
100         
101         return StringUtils.isBlank(this.getFinancialSystemDocumentHeader().getFinancialDocumentInErrorNumber());
102     }
103 
104     
105 
106 
107     public List<GeneralLedgerPendingEntry> getPendingLedgerEntriesForSufficientFundsChecking() {
108         return getGeneralLedgerPendingEntries();
109     }
110 
111     
112 
113 
114 
115 
116     @Override
117     public void doRouteStatusChange(DocumentRouteStatusChange statusChangeEvent) {
118         super.doRouteStatusChange(statusChangeEvent);
119         if (getDocumentHeader().getWorkflowDocument().isProcessed()) {
120             changeGeneralLedgerPendingEntriesApprovedStatusCode(); 
121         }
122         else if (getDocumentHeader().getWorkflowDocument().isCanceled() || getDocumentHeader().getWorkflowDocument().isDisapproved()) {
123             removeGeneralLedgerPendingEntries();
124             if (this instanceof ElectronicPaymentClaiming) {
125                 ((ElectronicPaymentClaiming)this).declaimElectronicPaymentClaims();
126             }
127         }
128     }
129 
130     
131 
132 
133     protected void changeGeneralLedgerPendingEntriesApprovedStatusCode() {
134         for (GeneralLedgerPendingEntry glpe : getGeneralLedgerPendingEntries()) {
135             glpe.setFinancialDocumentApprovedCode(OLEConstants.DocumentStatusCodes.APPROVED);
136         }
137     }
138 
139     
140 
141 
142     protected void removeGeneralLedgerPendingEntries() {
143         GeneralLedgerPendingEntryService glpeService = SpringContext.getBean(GeneralLedgerPendingEntryService.class);
144         glpeService.delete(getDocumentHeader().getDocumentNumber());
145     }
146 
147     
148 
149 
150     @Override
151     public void toCopy() throws WorkflowException {
152         super.toCopy();
153         getGeneralLedgerPendingEntries().clear();
154     }
155 
156     
157 
158 
159     @Override
160     public void toErrorCorrection() throws WorkflowException {
161         super.toErrorCorrection();
162         getGeneralLedgerPendingEntries().clear();
163     }
164 
165     @Override
166     public void prepareForSave(KualiDocumentEvent event) {
167         super.prepareForSave(event);
168         
169         if (event instanceof RouteDocumentEvent || event instanceof ApproveDocumentEvent) {
170             
171             List<SufficientFundsItem> sfItems = checkSufficientFunds();
172             if (!sfItems.isEmpty()) {
173                 for (SufficientFundsItem sfItem : sfItems) {
174                     GlobalVariables.getMessageMap().putError(OLEConstants.ACCOUNTING_LINE_ERRORS, OLEKeyConstants.SufficientFunds.ERROR_INSUFFICIENT_FUNDS, new String[] { sfItem.getAccount().getChartOfAccountsCode(), sfItem.getAccount().getAccountNumber(), StringUtils.isNotBlank(sfItem.getSufficientFundsObjectCode()) ? sfItem.getSufficientFundsObjectCode() : OLEConstants.NOT_AVAILABLE_STRING, sfItem.getAccountSufficientFundsCode() });
175                 }
176                 throw new ValidationException("Insufficient Funds on this Document:");
177             }
178         }
179     }
180     
181     
182 
183 
184 
185     public void addPendingEntry(GeneralLedgerPendingEntry pendingEntry) {
186         pendingEntry.refreshReferenceObject("financialObject");
187         generalLedgerPendingEntries.add(pendingEntry);
188     }
189     
190     
191 
192 
193     public void clearAnyGeneralLedgerPendingEntries() {
194         generalLedgerPendingEntries = new ArrayList<GeneralLedgerPendingEntry>();
195     }
196 }