1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.fp.document.web.struts;
17  
18  import java.io.FileNotFoundException;
19  import java.io.IOException;
20  import java.util.ArrayList;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.struts.action.ActionForm;
27  import org.apache.struts.action.ActionForward;
28  import org.apache.struts.action.ActionMapping;
29  import org.kuali.ole.fp.businessobject.VoucherAccountingLineHelper;
30  import org.kuali.ole.fp.businessobject.VoucherAccountingLineHelperBase;
31  import org.kuali.ole.fp.document.VoucherDocument;
32  import org.kuali.ole.sys.OLEConstants;
33  import org.kuali.ole.sys.OLEKeyConstants;
34  import org.kuali.ole.sys.businessobject.SourceAccountingLine;
35  import org.kuali.ole.sys.context.SpringContext;
36  import org.kuali.ole.sys.document.AmountTotaling;
37  import org.kuali.ole.sys.service.UniversityDateService;
38  import org.kuali.ole.sys.web.struts.KualiAccountingDocumentActionBase;
39  import org.kuali.rice.core.api.config.property.ConfigurationService;
40  import org.kuali.rice.core.api.util.type.KualiDecimal;
41  import org.kuali.rice.core.web.format.CurrencyFormatter;
42  import org.kuali.rice.kew.api.exception.WorkflowException;
43  import org.kuali.rice.kns.question.ConfirmationQuestion;
44  import org.kuali.rice.kns.util.KNSGlobalVariables;
45  import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
46  import org.kuali.rice.krad.util.GlobalVariables;
47  
48  
49  
50  
51  
52  
53  public class VoucherAction extends KualiAccountingDocumentActionBase {
54      
55      
56      
57  
58      
59  
60  
61  
62  
63  
64      @Override
65      public ActionForward correct(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
66          ActionForward actionForward = super.correct(mapping, form, request, response);
67  
68          VoucherForm vForm = (VoucherForm) form;
69  
70          
71          populateAllVoucherAccountingLineHelpers(vForm);
72  
73          return actionForward;
74      }
75  
76      
77  
78  
79  
80  
81  
82  
83      @Override
84      public ActionForward insertSourceLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
85          
86          VoucherForm voucherForm = (VoucherForm) form;
87  
88          
89          ActionForward actionForward = super.insertSourceLine(mapping, form, request, response);
90  
91          if (GlobalVariables.getMessageMap().getErrorCount() == 0) {
92              
93              
94              VoucherAccountingLineHelper helperLine = populateNewVoucherAccountingLineHelper(voucherForm);
95              voucherForm.getVoucherLineHelpers().add(helperLine);
96  
97              
98              voucherForm.setNewSourceLineDebit(KualiDecimal.ZERO);
99              voucherForm.setNewSourceLineCredit(KualiDecimal.ZERO);
100         }
101 
102         return actionForward;
103     }
104 
105     
106 
107 
108 
109 
110 
111     @Override
112     public ActionForward deleteSourceLine(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
113         
114         VoucherForm voucherForm = (VoucherForm) form;
115 
116         
117         ActionForward actionForward = super.deleteSourceLine(mapping, voucherForm, request, response);
118 
119         
120         int index = getLineToDelete(request);
121         if (voucherForm.getVoucherLineHelpers() != null && voucherForm.getVoucherLineHelpers().size() > index) {
122             voucherForm.getVoucherLineHelpers().remove(getLineToDelete(request));
123         }
124 
125         return actionForward;
126     }
127 
128     
129 
130 
131 
132 
133 
134     @Override
135     protected void loadDocument(KualiDocumentFormBase kualiDocumentFormBase) throws WorkflowException {
136         super.loadDocument(kualiDocumentFormBase);
137         VoucherForm voucherForm = (VoucherForm) kualiDocumentFormBase;
138 
139         populateAllVoucherAccountingLineHelpers(voucherForm);
140         voucherForm.setNewSourceLineCredit(KualiDecimal.ZERO);
141         voucherForm.setNewSourceLineDebit(KualiDecimal.ZERO);
142 
143         
144         voucherForm.setNewSourceLine(null);
145 
146         
147         populateSelectedAccountingPeriod(voucherForm.getVoucherDocument(), voucherForm);
148     }
149 
150     
151 
152 
153 
154 
155 
156 
157     protected void populateSelectedAccountingPeriod(VoucherDocument voucherDocument, VoucherForm voucherForm) {
158         if (StringUtils.isNotBlank(voucherDocument.getPostingPeriodCode())) {
159             String selectedAccountingPeriod = voucherDocument.getPostingPeriodCode();
160             if (null != voucherDocument.getPostingYear()) {
161                 selectedAccountingPeriod += voucherDocument.getPostingYear().toString();
162             }
163             else {
164                 selectedAccountingPeriod += SpringContext.getBean(UniversityDateService.class).getCurrentFiscalYear().toString();
165             }
166             voucherForm.setSelectedAccountingPeriod(selectedAccountingPeriod);
167         }
168     }
169 
170     
171 
172 
173 
174 
175 
176 
177     protected VoucherAccountingLineHelper populateVoucherAccountingLineHelper(VoucherForm voucherForm) {
178         VoucherAccountingLineHelper helperLine = new VoucherAccountingLineHelperBase();
179 
180         KualiDecimal debitAmount = voucherForm.getNewSourceLineDebit();
181         if (debitAmount != null && StringUtils.isNotBlank(debitAmount.toString())) {
182             helperLine.setDebit(debitAmount);
183         }
184 
185         KualiDecimal creditAmount = voucherForm.getNewSourceLineCredit();
186         if (creditAmount != null && StringUtils.isNotBlank(creditAmount.toString())) {
187             helperLine.setCredit(creditAmount);
188         }
189 
190         return helperLine;
191     }
192 
193     
194 
195 
196 
197 
198 
199 
200 
201     protected void populateAllVoucherAccountingLineHelpers(VoucherForm voucherForm) {
202         
203         ArrayList voucherLineHelpers = (ArrayList) voucherForm.getVoucherLineHelpers();
204 
205         
206         VoucherDocument vDoc = (VoucherDocument) voucherForm.getTransactionalDocument();
207         int size = vDoc.getSourceAccountingLines().size();
208         voucherLineHelpers.ensureCapacity(size);
209 
210         
211         for (int i = 0; i < size; i++) {
212             
213             SourceAccountingLine sourceAccountingLine = vDoc.getSourceAccountingLine(i);
214 
215             
216             VoucherAccountingLineHelper avAcctLineHelperForm = voucherForm.getVoucherLineHelper(i);
217 
218             
219             if (StringUtils.isNotBlank(sourceAccountingLine.getDebitCreditCode())) {
220                 if (sourceAccountingLine.getDebitCreditCode().equals(OLEConstants.GL_DEBIT_CODE)) {
221                     avAcctLineHelperForm.setDebit(sourceAccountingLine.getAmount());
222                     avAcctLineHelperForm.setCredit(KualiDecimal.ZERO);
223                 }
224                 else if (sourceAccountingLine.getDebitCreditCode().equals(OLEConstants.GL_CREDIT_CODE)) {
225                     avAcctLineHelperForm.setCredit(sourceAccountingLine.getAmount());
226                     avAcctLineHelperForm.setDebit(KualiDecimal.ZERO);
227                 }
228             }
229         }
230     }
231 
232 
233     
234 
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
245 
246 
247 
248 
249     protected ActionForward processRouteOutOfBalanceDocumentConfirmationQuestion(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
250         VoucherForm vForm = (VoucherForm) form;
251         VoucherDocument avDoc = vForm.getVoucherDocument();
252 
253         String question = request.getParameter(OLEConstants.QUESTION_INST_ATTRIBUTE_NAME);
254         ConfigurationService kualiConfiguration = SpringContext.getBean(ConfigurationService.class);
255 
256         if (question == null) { 
257             String currencyFormattedDebitTotal = (String) new CurrencyFormatter().format(avDoc.getDebitTotal());
258             String currencyFormattedCreditTotal = (String) new CurrencyFormatter().format(avDoc.getCreditTotal());
259             String currencyFormattedTotal = (String) new CurrencyFormatter().format(((AmountTotaling) avDoc).getTotalDollarAmount());
260             String message = "";
261             message = StringUtils.replace(kualiConfiguration.getPropertyValueAsString(OLEKeyConstants.QUESTION_ROUTE_OUT_OF_BALANCE_JV_DOC), "{0}", currencyFormattedDebitTotal);
262             message = StringUtils.replace(message, "{1}", currencyFormattedCreditTotal);
263 
264             
265             return this.performQuestionWithoutInput(mapping, form, request, response, OLEConstants.JOURNAL_VOUCHER_ROUTE_OUT_OF_BALANCE_DOCUMENT_QUESTION, message, OLEConstants.CONFIRMATION_QUESTION, OLEConstants.ROUTE_METHOD, "");
266         }
267         else {
268             String buttonClicked = request.getParameter(OLEConstants.QUESTION_CLICKED_BUTTON);
269             if ((OLEConstants.JOURNAL_VOUCHER_ROUTE_OUT_OF_BALANCE_DOCUMENT_QUESTION.equals(question)) && ConfirmationQuestion.NO.equals(buttonClicked)) {
270                 KNSGlobalVariables.getMessageList().add(OLEKeyConstants.MESSAGE_JV_CANCELLED_ROUTE);
271                 return mapping.findForward(OLEConstants.MAPPING_BASIC);
272             }
273         }
274         return null;
275     }
276 
277     
278 
279 
280 
281 
282 
283 
284     protected VoucherAccountingLineHelper populateNewVoucherAccountingLineHelper(VoucherForm voucherForm) {
285         VoucherAccountingLineHelper helperLine = new VoucherAccountingLineHelperBase();
286 
287         KualiDecimal debitAmount = voucherForm.getNewSourceLineDebit();
288         if (debitAmount != null && StringUtils.isNotBlank(debitAmount.toString())) {
289             helperLine.setDebit(debitAmount);
290         }
291 
292         KualiDecimal creditAmount = voucherForm.getNewSourceLineCredit();
293         if (creditAmount != null && StringUtils.isNotBlank(creditAmount.toString())) {
294             helperLine.setCredit(creditAmount);
295         }
296 
297         return helperLine;
298     }
299 
300     
301 
302 
303 
304 
305 
306 
307 
308 
309 
310 
311 
312     @Override
313     public ActionForward uploadSourceLines(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException {
314         
315         uploadAccountingLines(true, form);
316 
317         return mapping.findForward(OLEConstants.MAPPING_BASIC);
318     }
319 
320     
321 
322 
323 
324 
325 
326 
327 
328 
329     @Override
330     protected void uploadAccountingLines(boolean isSource, ActionForm form) throws FileNotFoundException, IOException {
331         super.uploadAccountingLines(isSource, form);
332 
333         populateAllVoucherAccountingLineHelpers((VoucherForm) form);
334     }
335 
336     
337 
338 
339 
340     @Override
341     public ActionForward copy(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
342         ActionForward forward = super.copy(mapping, form, request, response);
343         VoucherForm voucherForm = (VoucherForm)form;
344         voucherForm.populateAccountingPeriodListForRendering();
345         voucherForm.populateDefaultSelectedAccountingPeriod();
346         return forward;
347     }
348 }