001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.kfs.module.ar.document.service.impl;
017
018 import java.util.List;
019
020 import org.kuali.kfs.module.ar.ArConstants;
021 import org.kuali.kfs.module.ar.ArKeyConstants;
022 import org.kuali.kfs.module.ar.businessobject.AccountsReceivableDocumentHeader;
023 import org.kuali.kfs.module.ar.businessobject.CashControlDetail;
024 import org.kuali.kfs.module.ar.document.CashControlDocument;
025 import org.kuali.kfs.module.ar.document.service.AccountsReceivableDocumentHeaderService;
026 import org.kuali.kfs.module.ar.document.service.CashControlDocumentService;
027 import org.kuali.kfs.sys.KFSConstants;
028 import org.kuali.kfs.sys.businessobject.ChartOrgHolder;
029 import org.kuali.kfs.sys.businessobject.ElectronicPaymentClaim;
030 import org.kuali.kfs.sys.context.SpringContext;
031 import org.kuali.kfs.sys.service.ElectronicPaymentClaimingDocumentGenerationStrategy;
032 import org.kuali.kfs.sys.service.ElectronicPaymentClaimingService;
033 import org.kuali.kfs.sys.service.FinancialSystemUserService;
034 import org.kuali.rice.kew.exception.WorkflowException;
035 import org.kuali.rice.kew.util.KEWConstants;
036 import org.kuali.rice.kim.bo.Person;
037 import org.kuali.rice.kns.bo.Note;
038 import org.kuali.rice.kns.service.DataDictionaryService;
039 import org.kuali.rice.kns.service.DocumentService;
040 import org.kuali.rice.kns.service.KNSServiceLocator;
041 import org.kuali.rice.kns.service.KualiConfigurationService;
042 import org.kuali.rice.kns.util.GlobalVariables;
043 import org.kuali.rice.kns.workflow.service.KualiWorkflowInfo;
044 import org.kuali.rice.kns.workflow.service.WorkflowInfoService;
045
046 public class CashControlElectronicPaymentClaimingHelperImpl implements ElectronicPaymentClaimingDocumentGenerationStrategy {
047 private org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CashControlElectronicPaymentClaimingHelperImpl.class);
048
049 private DataDictionaryService dataDictionaryService;
050 private DocumentService documentService;
051 private ElectronicPaymentClaimingService electronicPaymentClaimingService;
052 private CashControlDocumentService cashControlDocumentService;
053 private KualiConfigurationService kualiConfigurationService;
054
055 protected final static String CC_WORKFLOW_DOCUMENT_TYPE = "CTRL";
056 protected final static String URL_PREFIX = "ar";
057 protected final static String URL_MIDDLE = "Document.do?methodToCall=docHandler&command=";
058 protected final static String URL_SUFFIX = "&docId=";
059 protected final static String URL_DOC_TYPE = "CashControl";
060
061 /**
062 * @see org.kuali.kfs.sys.service.ElectronicPaymentClaimingDocumentGenerationStrategy#createDocumentFromElectronicPayments(java.util.List,
063 * org.kuali.rice.kim.bo.Person)
064 */
065 public String createDocumentFromElectronicPayments(List<ElectronicPaymentClaim> electronicPayments, Person user) {
066 CashControlDocument document = null;
067 try {
068 document = (CashControlDocument) documentService.getNewDocument(getClaimingDocumentWorkflowDocumentType());
069 document.setCustomerPaymentMediumCode(ArConstants.PaymentMediumCode.WIRE_TRANSFER);
070
071 //create and set AccountsReceivableDocumentHeader
072 ChartOrgHolder currentUser = SpringContext.getBean(FinancialSystemUserService.class).getPrimaryOrganization(GlobalVariables.getUserSession().getPerson(), ArConstants.AR_NAMESPACE_CODE);
073 AccountsReceivableDocumentHeaderService accountsReceivableDocumentHeaderService = SpringContext.getBean(AccountsReceivableDocumentHeaderService.class);
074 AccountsReceivableDocumentHeader accountsReceivableDocumentHeader = accountsReceivableDocumentHeaderService.getNewAccountsReceivableDocumentHeaderForCurrentUser();
075 accountsReceivableDocumentHeader.setDocumentNumber(document.getDocumentNumber());
076 document.setAccountsReceivableDocumentHeader(accountsReceivableDocumentHeader);
077
078 addDescriptionToDocument(document);
079 addNotesToDocument(document, electronicPayments, user);
080 addCashControlDetailsToDocument(document, electronicPayments);
081 documentService.saveDocument(document);
082 electronicPaymentClaimingService.claimElectronicPayments(electronicPayments, document.getDocumentNumber());
083 }
084 catch (WorkflowException we) {
085 throw new RuntimeException("WorkflowException while creating a CashControlDocument to claim ElectronicPaymentClaim records.", we);
086 }
087
088 return getURLForDocument(document);
089 }
090
091 /**
092 * This method add a description to the cash control document
093 *
094 * @param document the cash control document
095 */
096 protected void addDescriptionToDocument(CashControlDocument document) {
097 document.getDocumentHeader().setDocumentDescription(kualiConfigurationService.getPropertyString(ArKeyConstants.ELECTRONIC_PAYMENT_CLAIM));
098 }
099
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 try {
110 Note note = documentService.createNoteFromDocument(claimingDoc, noteText);
111 documentService.addNoteToDocument(claimingDoc, note);
112 }
113 catch (Exception e) {
114 LOG.error("Exception while attempting to create or add note: " + e);
115 }
116 }
117 }
118
119 /**
120 * This method adds new cash control details to the cash control document based on the list of electronic payments.
121 *
122 * @param document cash control document
123 * @param electronicPayments the electronic payments to be claimed
124 * @throws WorkflowException workflow exception
125 */
126 protected void addCashControlDetailsToDocument(CashControlDocument document, List<ElectronicPaymentClaim> electronicPayments) throws WorkflowException {
127 for (ElectronicPaymentClaim electronicPaymentClaim : electronicPayments) {
128 CashControlDetail newCashControlDetail = new CashControlDetail();
129 newCashControlDetail.setCashControlDocument(document);
130 newCashControlDetail.setDocumentNumber(document.getDocumentNumber());
131 newCashControlDetail.setFinancialDocumentLineAmount(electronicPaymentClaim.getGeneratingAccountingLine().getAmount());
132 newCashControlDetail.setCustomerPaymentDescription(electronicPaymentClaim.getGeneratingAccountingLine().getFinancialDocumentLineDescription());
133 cashControlDocumentService.addNewCashControlDetail(kualiConfigurationService.getPropertyString(ArKeyConstants.CREATED_BY_CASH_CTRL_DOC), document, newCashControlDetail);
134 }
135
136 }
137
138 /**
139 * Builds the URL that can be used to redirect to the correct document
140 *
141 * @param doc the document to build the URL for
142 * @return the relative URL to redirect to
143 */
144 protected String getURLForDocument(CashControlDocument doc) {
145 StringBuilder url = new StringBuilder();
146 url.append(URL_PREFIX);
147 url.append(URL_DOC_TYPE);
148 url.append(URL_MIDDLE);
149 url.append(KEWConstants.ACTIONLIST_COMMAND);
150 url.append(URL_SUFFIX);
151 url.append(doc.getDocumentNumber());
152 return url.toString();
153 }
154
155 /**
156 * @see org.kuali.kfs.sys.service.ElectronicPaymentClaimingDocumentGenerationStrategy#getClaimingDocumentWorkflowDocumentType()
157 *
158 * @return the name CashControlDocument workflow document type
159 */
160 public String getClaimingDocumentWorkflowDocumentType() {
161 return CashControlElectronicPaymentClaimingHelperImpl.CC_WORKFLOW_DOCUMENT_TYPE;
162 }
163
164 /**
165 * @see org.kuali.kfs.sys.service.ElectronicPaymentClaimingDocumentGenerationStrategy#getDocumentLabel()
166 */
167 public String getDocumentLabel() {
168 try {
169 KualiWorkflowInfo workflowInfo = KNSServiceLocator.getWorkflowInfoService();;
170
171 return workflowInfo.getDocType(getClaimingDocumentWorkflowDocumentType()).getDocTypeLabel();
172 }
173 catch (WorkflowException e) {
174 throw new RuntimeException("Caught Exception trying to get Workflow Document Type", e);
175 }
176 }
177
178 /**
179 * @see org.kuali.kfs.sys.service.ElectronicPaymentClaimingDocumentGenerationStrategy#isDocumentReferenceValid(java.lang.String)
180 */
181 public boolean isDocumentReferenceValid(String referenceDocumentNumber) {
182 boolean valid = false;
183 try {
184 long docNumberAsLong = Long.parseLong(referenceDocumentNumber);
185 if (docNumberAsLong > 0L) {
186 valid = documentService.documentExists(referenceDocumentNumber);
187 }
188 }
189 catch (NumberFormatException nfe) {
190 valid = false;
191 }
192 return valid;
193 }
194
195 /**
196 * @see org.kuali.kfs.sys.service.ElectronicPaymentClaimingDocumentGenerationStrategy#userMayUseToClaim(org.kuali.rice.kim.bo.Person)
197 */
198 public boolean userMayUseToClaim(Person claimingUser) {
199 final String documentTypeName = this.getClaimingDocumentWorkflowDocumentType();
200
201 final boolean canClaim = electronicPaymentClaimingService.isAuthorizedForClaimingElectronicPayment(claimingUser, documentTypeName) || electronicPaymentClaimingService.isAuthorizedForClaimingElectronicPayment(claimingUser, null);
202
203 return canClaim;
204 }
205
206 /**
207 * This method sets cashControlDocumentService value
208 *
209 * @param cashControlDocumentService
210 */
211 public void setCashControlDocumentService(CashControlDocumentService cashControlDocumentService) {
212 this.cashControlDocumentService = cashControlDocumentService;
213 }
214
215 /**
216 * This method sets document service value
217 *
218 * @param documentService
219 */
220 public void setDocumentService(DocumentService documentService) {
221 this.documentService = documentService;
222 }
223
224 /**
225 * This method sets electronicPaymentClaimingService value
226 *
227 * @param electronicPaymentClaimingService
228 */
229 public void setElectronicPaymentClaimingService(ElectronicPaymentClaimingService electronicPaymentClaimingService) {
230 this.electronicPaymentClaimingService = electronicPaymentClaimingService;
231 }
232
233 /**
234 * This method sets dataDictionaryService value
235 *
236 * @param dataDictionaryService
237 */
238 public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
239 this.dataDictionaryService = dataDictionaryService;
240 }
241
242 /**
243 * This method sets kualiConfigurationService
244 * @param kualiConfigurationService
245 */
246 public void setKualiConfigurationService(KualiConfigurationService kualiConfigurationService) {
247 this.kualiConfigurationService = kualiConfigurationService;
248 }
249 }
250