1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
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  
63  
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              
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  
93  
94  
95  
96      protected void addDescriptionToDocument(CashControlDocument document) {
97          document.getDocumentHeader().setDocumentDescription(kualiConfigurationService.getPropertyValueAsString(ArKeyConstants.ELECTRONIC_PAYMENT_CLAIM));
98      }
99  
100     
101 
102 
103 
104 
105 
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 
117 
118 
119 
120 
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 
136 
137 
138 
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 
151 
152 
153 
154     @Override
155     public String getClaimingDocumentWorkflowDocumentType() {
156         return KFSConstants.FinancialDocumentTypeCodes.CASH_CONTROL;
157     }
158 
159     
160 
161 
162     @Override
163     public String getDocumentLabel() {
164         return getDocumentTypeService().getDocumentTypeByName(getClaimingDocumentWorkflowDocumentType()).getLabel();
165     }
166 
167     
168 
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 
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 
226 
227 
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