View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.ar.document.service.impl;
20  
21  import java.util.List;
22  import java.util.Properties;
23  
24  import org.kuali.kfs.module.ar.ArConstants;
25  import org.kuali.kfs.module.ar.ArKeyConstants;
26  import org.kuali.kfs.module.ar.businessobject.AccountsReceivableDocumentHeader;
27  import org.kuali.kfs.module.ar.businessobject.CashControlDetail;
28  import org.kuali.kfs.module.ar.document.CashControlDocument;
29  import org.kuali.kfs.module.ar.document.service.AccountsReceivableDocumentHeaderService;
30  import org.kuali.kfs.module.ar.document.service.CashControlDocumentService;
31  import org.kuali.kfs.sys.KFSConstants;
32  import org.kuali.kfs.sys.businessobject.ChartOrgHolder;
33  import org.kuali.kfs.sys.businessobject.ElectronicPaymentClaim;
34  import org.kuali.kfs.sys.service.ElectronicPaymentClaimingDocumentGenerationStrategy;
35  import org.kuali.kfs.sys.service.ElectronicPaymentClaimingService;
36  import org.kuali.kfs.sys.service.FinancialSystemUserService;
37  import org.kuali.rice.core.api.config.property.ConfigurationService;
38  import org.kuali.rice.kew.api.KewApiConstants;
39  import org.kuali.rice.kew.api.KewApiServiceLocator;
40  import org.kuali.rice.kew.api.doctype.DocumentTypeService;
41  import org.kuali.rice.kew.api.exception.WorkflowException;
42  import org.kuali.rice.kim.api.identity.Person;
43  import org.kuali.rice.kns.service.DataDictionaryService;
44  import org.kuali.rice.krad.bo.Note;
45  import org.kuali.rice.krad.service.DocumentService;
46  import org.kuali.rice.krad.util.GlobalVariables;
47  import org.kuali.rice.krad.util.UrlFactory;
48  
49  public class CashControlElectronicPaymentClaimingHelperImpl implements ElectronicPaymentClaimingDocumentGenerationStrategy {
50      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CashControlElectronicPaymentClaimingHelperImpl.class);
51  
52      protected DataDictionaryService dataDictionaryService;
53      protected DocumentService documentService;
54      protected ElectronicPaymentClaimingService electronicPaymentClaimingService;
55      protected CashControlDocumentService cashControlDocumentService;
56      protected ConfigurationService kualiConfigurationService;
57      protected FinancialSystemUserService financialSystemUserService;
58      private static DocumentTypeService documentTypeService;
59      private AccountsReceivableDocumentHeaderService accountsReceivableDocumentHeaderService;
60  
61      /**
62       * @see org.kuali.kfs.sys.service.ElectronicPaymentClaimingDocumentGenerationStrategy#createDocumentFromElectronicPayments(java.util.List,
63       *      org.kuali.rice.kim.api.identity.Person)
64       */
65      @Override
66      public String createDocumentFromElectronicPayments(List<ElectronicPaymentClaim> electronicPayments, Person user) {
67          CashControlDocument document = null;
68          try {
69              document = (CashControlDocument) documentService.getNewDocument(getClaimingDocumentWorkflowDocumentType());
70              document.setCustomerPaymentMediumCode(ArConstants.PaymentMediumCode.WIRE_TRANSFER);
71  
72              //create and set AccountsReceivableDocumentHeader
73              ChartOrgHolder currentUser = financialSystemUserService.getPrimaryOrganization(GlobalVariables.getUserSession().getPerson(), ArConstants.AR_NAMESPACE_CODE);
74              AccountsReceivableDocumentHeader accountsReceivableDocumentHeader = accountsReceivableDocumentHeaderService.getNewAccountsReceivableDocumentHeaderForCurrentUser();
75              accountsReceivableDocumentHeader.setDocumentNumber(document.getDocumentNumber());
76              document.setAccountsReceivableDocumentHeader(accountsReceivableDocumentHeader);
77  
78              addDescriptionToDocument(document);
79              addNotesToDocument(document, electronicPayments, user);
80              addCashControlDetailsToDocument(document, electronicPayments);
81              documentService.saveDocument(document);
82              electronicPaymentClaimingService.claimElectronicPayments(electronicPayments, document.getDocumentNumber());
83          }
84          catch (WorkflowException we) {
85              throw new RuntimeException("WorkflowException while creating a CashControlDocument to claim ElectronicPaymentClaim records.", we);
86          }
87  
88          return getURLForDocument(document);
89      }
90  
91      /**
92       * This method add a description to the cash control document
93       *
94       * @param document the cash control document
95       */
96      protected void addDescriptionToDocument(CashControlDocument document) {
97          document.getDocumentHeader().setDocumentDescription(kualiConfigurationService.getPropertyValueAsString(ArKeyConstants.ELECTRONIC_PAYMENT_CLAIM));
98      }
99  
100     /**
101      * This method adds notes to the cash control document
102      *
103      * @param claimingDoc the cash control document
104      * @param claims the list of electronic payments being claimed
105      * @param user the current user
106      */
107     protected void addNotesToDocument(CashControlDocument claimingDoc, List<ElectronicPaymentClaim> claims, Person user) {
108         for (String noteText : electronicPaymentClaimingService.constructNoteTextsForClaims(claims)) {
109             Note note = documentService.createNoteFromDocument(claimingDoc, noteText);
110             claimingDoc.addNote(note);
111             documentService.saveDocumentNotes(claimingDoc);
112         }
113     }
114 
115     /**
116      * This method adds new cash control details to the cash control document based on the list of electronic payments.
117      *
118      * @param document cash control document
119      * @param electronicPayments the electronic payments to be claimed
120      * @throws WorkflowException workflow exception
121      */
122     protected void addCashControlDetailsToDocument(CashControlDocument document, List<ElectronicPaymentClaim> electronicPayments) throws WorkflowException {
123         for (ElectronicPaymentClaim electronicPaymentClaim : electronicPayments) {
124             CashControlDetail newCashControlDetail = new CashControlDetail();
125             newCashControlDetail.setCashControlDocument(document);
126             newCashControlDetail.setDocumentNumber(document.getDocumentNumber());
127             newCashControlDetail.setFinancialDocumentLineAmount(electronicPaymentClaim.getGeneratingAccountingLine().getAmount());
128             newCashControlDetail.setCustomerPaymentDescription(electronicPaymentClaim.getGeneratingAccountingLine().getFinancialDocumentLineDescription());
129             cashControlDocumentService.addNewCashControlDetail(kualiConfigurationService.getPropertyValueAsString(ArKeyConstants.CREATED_BY_CASH_CTRL_DOC), document, newCashControlDetail);
130         }
131 
132     }
133 
134     /**
135      * Builds the URL that can be used to redirect to the correct document
136      *
137      * @param doc the document to build the URL for
138      * @return the relative URL to redirect to
139      */
140     protected String getURLForDocument(CashControlDocument doc) {
141         Properties parameters = new Properties();
142         parameters.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.DOC_HANDLER_METHOD);
143         parameters.put(KFSConstants.PARAMETER_COMMAND, KewApiConstants.ACTIONLIST_COMMAND);
144         parameters.put(KFSConstants.PARAMETER_DOC_ID, doc.getDocumentNumber());
145 
146         return UrlFactory.parameterizeUrl(ArConstants.UrlActions.CASH_CONTROL_DOCUMENT, parameters);
147     }
148 
149     /**
150      * @see org.kuali.kfs.sys.service.ElectronicPaymentClaimingDocumentGenerationStrategy#getClaimingDocumentWorkflowDocumentType()
151      *
152      * @return the name CashControlDocument workflow document type
153      */
154     @Override
155     public String getClaimingDocumentWorkflowDocumentType() {
156         return KFSConstants.FinancialDocumentTypeCodes.CASH_CONTROL;
157     }
158 
159     /**
160      * @see org.kuali.kfs.sys.service.ElectronicPaymentClaimingDocumentGenerationStrategy#getDocumentLabel()
161      */
162     @Override
163     public String getDocumentLabel() {
164         return getDocumentTypeService().getDocumentTypeByName(getClaimingDocumentWorkflowDocumentType()).getLabel();
165     }
166 
167     /**
168      * @see org.kuali.kfs.sys.service.ElectronicPaymentClaimingDocumentGenerationStrategy#isDocumentReferenceValid(java.lang.String)
169      */
170     @Override
171     public boolean isDocumentReferenceValid(String referenceDocumentNumber) {
172         boolean isValid = false;
173         try {
174             long docNumberAsLong = Long.parseLong(referenceDocumentNumber);
175             if (docNumberAsLong > 0L) {
176                 isValid = documentService.documentExists(referenceDocumentNumber);
177             }
178         }
179         catch (NumberFormatException nfe) {
180             isValid = false;
181         }
182         return isValid;
183     }
184 
185     /**
186      * @see org.kuali.kfs.sys.service.ElectronicPaymentClaimingDocumentGenerationStrategy#userMayUseToClaim(org.kuali.rice.kim.api.identity.Person)
187      */
188     @Override
189     public boolean userMayUseToClaim(Person claimingUser) {
190         final String documentTypeName = this.getClaimingDocumentWorkflowDocumentType();
191 
192         final boolean canClaim = electronicPaymentClaimingService.isAuthorizedForClaimingElectronicPayment(claimingUser, documentTypeName) || electronicPaymentClaimingService.isAuthorizedForClaimingElectronicPayment(claimingUser, null);
193 
194         return canClaim;
195     }
196 
197     public void setCashControlDocumentService(CashControlDocumentService cashControlDocumentService) {
198         this.cashControlDocumentService = cashControlDocumentService;
199     }
200 
201     public void setDocumentService(DocumentService documentService) {
202         this.documentService = documentService;
203     }
204 
205     public void setElectronicPaymentClaimingService(ElectronicPaymentClaimingService electronicPaymentClaimingService) {
206         this.electronicPaymentClaimingService = electronicPaymentClaimingService;
207     }
208 
209     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
210         this.dataDictionaryService = dataDictionaryService;
211     }
212 
213     public void setKualiConfigurationService(ConfigurationService kualiConfigurationService) {
214         this.kualiConfigurationService = kualiConfigurationService;
215     }
216 
217     public DocumentTypeService getDocumentTypeService() {
218         if (documentTypeService == null) {
219             documentTypeService = KewApiServiceLocator.getDocumentTypeService();
220         }
221         return documentTypeService;
222     }
223 
224     /**
225      * Sets the accountsReceivableDocumentHeaderService attribute value.
226      *
227      * @param accountsReceivableDocumentHeaderService The accountsReceivableDocumentHeaderService to set.
228      */
229     public void setAccountsReceivableDocumentHeaderService(AccountsReceivableDocumentHeaderService accountsReceivableDocumentHeaderService) {
230         this.accountsReceivableDocumentHeaderService = accountsReceivableDocumentHeaderService;
231     }
232 
233     public FinancialSystemUserService getFinancialSystemUserService() {
234         return financialSystemUserService;
235     }
236 
237     public void setFinancialSystemUserService(FinancialSystemUserService financialSystemUserService) {
238         this.financialSystemUserService = financialSystemUserService;
239     }
240 }
241