1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.select.document;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.fp.businessobject.DisbursementVoucherPayeeDetail;
20  import org.kuali.ole.fp.document.DisbursementVoucherConstants;
21  import org.kuali.ole.fp.document.DisbursementVoucherDocument;
22  import org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService;
23  import org.kuali.ole.module.purap.PurapConstants;
24  import org.kuali.ole.select.businessobject.OleDisbursementVoucherAccountingLine;
25  import org.kuali.ole.select.businessobject.OleDisbursementVoucherPayeeDetail;
26  import org.kuali.ole.select.businessobject.OlePaymentMethod;
27  import org.kuali.ole.sys.OLEConstants;
28  import org.kuali.ole.sys.OLEConstants.AdHocPaymentIndicator;
29  import org.kuali.ole.sys.businessobject.Bank;
30  import org.kuali.ole.sys.businessobject.ChartOrgHolder;
31  import org.kuali.ole.sys.businessobject.SourceAccountingLine;
32  import org.kuali.ole.sys.context.SpringContext;
33  import org.kuali.ole.sys.document.AmountTotaling;
34  import org.kuali.ole.sys.service.BankService;
35  import org.kuali.ole.vnd.VendorConstants;
36  import org.kuali.ole.vnd.businessobject.VendorAddress;
37  import org.kuali.ole.vnd.businessobject.VendorAlias;
38  import org.kuali.ole.vnd.businessobject.VendorDetail;
39  import org.kuali.rice.kim.api.identity.Person;
40  import org.kuali.rice.krad.service.BusinessObjectService;
41  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
42  import org.kuali.rice.krad.service.LookupService;
43  import org.kuali.rice.krad.util.GlobalVariables;
44  import org.kuali.rice.krad.util.ObjectUtils;
45  
46  import java.sql.Date;
47  import java.text.MessageFormat;
48  import java.util.Calendar;
49  import java.util.HashMap;
50  import java.util.List;
51  import java.util.Map;
52  
53  
54  public class OleDisbursementVoucherDocument extends DisbursementVoucherDocument {
55  
56      private String vendorAliasName;
57      private String invoiceNumber;
58  
59      public String getVendorAliasName() {
60          return vendorAliasName;
61      }
62  
63      public void setVendorAliasName(String vendorAliasName) {
64          this.vendorAliasName = vendorAliasName;
65      }
66  
67      public String getInvoiceNumber() {
68          return invoiceNumber;
69      }
70  
71      public void setInvoiceNumber(String invoiceNumber) {
72          this.invoiceNumber = invoiceNumber;
73      }
74  
75      protected static final String HAS_CLEARING_ACCOUNT_TYPE_SPLIT = "HasClearingAccountType";
76  
77      protected OleDisbursementVoucherPayeeDetail dvPayeeDetail;
78  
79      
80  
81  
82      public OleDisbursementVoucherDocument() {
83          super();
84          dvPayeeDetail = new OleDisbursementVoucherPayeeDetail();
85      }
86  
87      
88  
89  
90      @Override
91      public OleDisbursementVoucherPayeeDetail getDvPayeeDetail() {
92          return dvPayeeDetail;
93      }
94  
95      
96  
97  
98      @Override
99      public void setDvPayeeDetail(DisbursementVoucherPayeeDetail dvPayeeDetail) {
100         OleDisbursementVoucherPayeeDetail dvDetail = (OleDisbursementVoucherPayeeDetail) dvPayeeDetail;
101         this.dvPayeeDetail = dvDetail;
102     }
103 
104     
105 
106 
107     @Override
108     public void prepareForSave() {
109         if (this instanceof AmountTotaling) {
110             getFinancialSystemDocumentHeader().setFinancialDocumentTotalAmount(
111                     ((AmountTotaling) this).getTotalDollarAmount());
112         }
113 
114         if (dvWireTransfer != null) {
115             dvWireTransfer.setDocumentNumber(this.documentNumber);
116         }
117 
118         if (dvNonResidentAlienTax != null) {
119             dvNonResidentAlienTax.setDocumentNumber(this.documentNumber);
120         }
121 
122         dvPayeeDetail.setDocumentNumber(this.documentNumber);
123 
124         if (dvNonEmployeeTravel != null) {
125             dvNonEmployeeTravel.setDocumentNumber(this.documentNumber);
126             dvNonEmployeeTravel.setTotalTravelAmount(dvNonEmployeeTravel.getTotalTravelAmount());
127         }
128 
129         if (dvPreConferenceDetail != null) {
130             dvPreConferenceDetail.setDocumentNumber(this.documentNumber);
131             dvPreConferenceDetail.setDisbVchrConferenceTotalAmt(dvPreConferenceDetail.getDisbVchrConferenceTotalAmt());
132         }
133 
134         if (shouldClearSpecialHandling()) {
135             clearSpecialHandling();
136         }
137         if (dvPayeeDetail.getVendorAliasName() != null && dvPayeeDetail.getVendorAliasName().length() > 0) {
138             checkVendorUsingVendorAliasName();
139         }
140         if(this.getPaymentMethodId() != null)  {
141             OlePaymentMethod olePaymentMethod = new OlePaymentMethod();
142                  olePaymentMethod = SpringContext.getBean(BusinessObjectService.class).findBySinglePrimaryKey(
143                         OlePaymentMethod.class, this.getPaymentMethodId());
144            if(olePaymentMethod != null)  {
145                if(olePaymentMethod.getPaymentMethod().equalsIgnoreCase("CHECK")) {
146                    this.setDisbVchrPaymentMethodCode(DisbursementVoucherConstants.PAYMENT_METHOD_CHECK);
147                }
148                else if(olePaymentMethod.getPaymentMethod().equalsIgnoreCase("Wire Transfer")) {
149                    this.setDisbVchrPaymentMethodCode(DisbursementVoucherConstants.PAYMENT_METHOD_WIRE);
150                }
151                else if(olePaymentMethod.getPaymentMethod().equalsIgnoreCase("Foreign Draft")) {
152                    this.setDisbVchrPaymentMethodCode(DisbursementVoucherConstants.PAYMENT_METHOD_DRAFT);
153                }
154 
155            }
156         }
157     }
158 
159     private void checkVendorUsingVendorAliasName() {
160         Map vendorAliasMap = new HashMap();
161         vendorAliasMap.put(OLEConstants.VENDOR_ALIAS_NAME, dvPayeeDetail.getVendorAliasName());
162         org.kuali.rice.krad.service.BusinessObjectService businessObject = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
163         List<VendorAlias> vendorAliasList = (List<VendorAlias>) getLookupService().findCollectionBySearchHelper(VendorAlias.class, vendorAliasMap, true);
164         if (vendorAliasList != null && vendorAliasList.size() > 0 && vendorAliasList.get(0) != null) {
165             if (this.getDvPayeeDetail().getDisbVchrPayeeIdNumber() == null) {
166                 GlobalVariables.getMessageMap().putError(PurapConstants.VENDOR_ERRORS, OLEConstants.VENDOR_SELECT);
167             } else {
168                 String vendorDetailAssignedIdentifier = vendorAliasList.get(0).getVendorDetailAssignedIdentifier().toString();
169                 String vendorHeaderGeneratedIdentifier = vendorAliasList.get(0).getVendorHeaderGeneratedIdentifier().toString();
170                 String AliasPayeeId = vendorHeaderGeneratedIdentifier + "-" + vendorDetailAssignedIdentifier;
171                 if (this.getDvPayeeDetail().getDisbVchrPayeeIdNumber().equalsIgnoreCase(AliasPayeeId)) {
172                     this.getDvPayeeDetail().setDisbVchrPaymentReasonCode(DisbursementVoucherConstants.DV_PAYEE_TYPE_EMPLOYEE);
173                     LOG.debug("###########vendors are allowed to save###########");
174                 } else {
175                     GlobalVariables.getMessageMap().putError(PurapConstants.VENDOR_ERRORS, OLEConstants.VENDOR_NOT_SAME);
176                 }
177             }
178         } else {
179             GlobalVariables.getMessageMap().putError(PurapConstants.VENDOR_ERRORS, OLEConstants.VENDOR_NOT_FOUND);
180         }
181     }
182 
183     
184 
185 
186     @Override
187     protected void clearSpecialHandling() {
188         OleDisbursementVoucherPayeeDetail payeeDetail = getDvPayeeDetail();
189 
190         if (!StringUtils.isBlank(payeeDetail.getDisbVchrSpecialHandlingPersonName())) {
191             payeeDetail.setDisbVchrSpecialHandlingPersonName(null);
192         }
193         if (!StringUtils.isBlank(payeeDetail.getDisbVchrSpecialHandlingLine1Addr())) {
194             payeeDetail.setDisbVchrSpecialHandlingLine1Addr(null);
195         }
196         if (!StringUtils.isBlank(payeeDetail.getDisbVchrSpecialHandlingLine2Addr())) {
197             payeeDetail.setDisbVchrSpecialHandlingLine2Addr(null);
198         }
199         if (!StringUtils.isBlank(payeeDetail.getDisbVchrSpecialHandlingCityName())) {
200             payeeDetail.setDisbVchrSpecialHandlingCityName(null);
201         }
202         if (!StringUtils.isBlank(payeeDetail.getDisbVchrSpecialHandlingStateCode())) {
203             payeeDetail.setDisbVchrSpecialHandlingStateCode(null);
204         }
205         if (!StringUtils.isBlank(payeeDetail.getDisbVchrSpecialHandlingZipCode())) {
206             payeeDetail.setDisbVchrSpecialHandlingZipCode(null);
207         }
208         if (!StringUtils.isBlank(payeeDetail.getDisbVchrSpecialHandlingCountryCode())) {
209             payeeDetail.setDisbVchrSpecialHandlingCountryCode(null);
210         }
211     }
212 
213     
214 
215 
216 
217     @Override
218     protected void cleanDocumentData() {
219         
220         if (!DisbursementVoucherConstants.PAYMENT_METHOD_WIRE.equals(this.getDisbVchrPaymentMethodCode()) && !DisbursementVoucherConstants.PAYMENT_METHOD_DRAFT.equals(this.getDisbVchrPaymentMethodCode())) {
221             getBusinessObjectService().delete(dvWireTransfer);
222             dvWireTransfer = null;
223         }
224 
225         if (!dvPayeeDetail.isDisbVchrAlienPaymentCode()) {
226             getBusinessObjectService().delete(dvNonResidentAlienTax);
227             dvNonResidentAlienTax = null;
228         }
229 
230         DisbursementVoucherPaymentReasonService paymentReasonService = SpringContext.getBean(DisbursementVoucherPaymentReasonService.class);
231         String paymentReasonCode = this.getDvPayeeDetail().getDisbVchrPaymentReasonCode();
232         
233         
234 
235 
236 
237 
238 
239 
240 
241 
242     }
243 
244     
245 
246 
247 
248 
249 
250     @Override
251     protected String buildDocumentTitle(String title) {
252         OleDisbursementVoucherPayeeDetail payee = getDvPayeeDetail();
253         if (payee == null) {
254             return title;
255         }
256 
257         Object[] indicators = new String[2];
258         indicators[0] = payee.isEmployee() ? AdHocPaymentIndicator.EMPLOYEE_PAYEE : AdHocPaymentIndicator.OTHER;
259         indicators[1] = payee.isDisbVchrAlienPaymentCode() ? AdHocPaymentIndicator.ALIEN_PAYEE : AdHocPaymentIndicator.OTHER;
260 
261         DisbursementVoucherPaymentReasonService paymentReasonService = SpringContext.getBean(DisbursementVoucherPaymentReasonService.class);
262         
263         
264 
265 
266         for (Object indicator : indicators) {
267             if (!AdHocPaymentIndicator.OTHER.equals(indicator)) {
268                 String titlePattern = title + " [{0}:{1}]";
269                 return MessageFormat.format(titlePattern, indicators);
270             }
271         }
272 
273         return title;
274     }
275 
276     
277 
278 
279 
280 
281     @Override
282     public void templateVendor(VendorDetail vendor, VendorAddress vendorAddress) {
283         if (vendor == null) {
284             return;
285         }
286         if (this.getDocumentHeader().getDocumentTemplateNumber() == null) {
287             this.setPaymentMethodId(vendor.getPaymentMethodId());
288         }
289         this.getDvPayeeDetail().setDisbursementVoucherPayeeTypeCode(DisbursementVoucherConstants.DV_PAYEE_TYPE_VENDOR);
290         this.getDvPayeeDetail().setDisbVchrPayeeIdNumber(vendor.getVendorNumber());
291         this.getDvPayeeDetail().setDisbVchrPayeePersonName(vendor.getVendorName());
292 
293         this.getDvPayeeDetail().setDisbVchrAlienPaymentCode(vendor.getVendorHeader().getVendorForeignIndicator());
294 
295         if (ObjectUtils.isNull(vendorAddress) || ObjectUtils.isNull(vendorAddress.getVendorAddressGeneratedIdentifier())) {
296             for (VendorAddress addr : vendor.getVendorAddresses()) {
297                 if (addr.isVendorDefaultAddressIndicator()) {
298                     vendorAddress = addr;
299                     break;
300                 }
301             }
302         }
303 
304         if (ObjectUtils.isNotNull(vendorAddress) && ObjectUtils.isNotNull(vendorAddress.getVendorAddressGeneratedIdentifier())) {
305             this.getDvPayeeDetail().setDisbVchrVendorAddressIdNumber(vendorAddress.getVendorAddressGeneratedIdentifier().toString());
306             this.getDvPayeeDetail().setDisbVchrPayeeLine1Addr(vendorAddress.getVendorLine1Address());
307             this.getDvPayeeDetail().setDisbVchrPayeeLine2Addr(vendorAddress.getVendorLine2Address());
308             this.getDvPayeeDetail().setDisbVchrPayeeCityName(vendorAddress.getVendorCityName());
309             this.getDvPayeeDetail().setDisbVchrPayeeStateCode(vendorAddress.getVendorStateCode());
310             this.getDvPayeeDetail().setDisbVchrPayeeZipCode(vendorAddress.getVendorZipCode());
311             this.getDvPayeeDetail().setDisbVchrPayeeCountryCode(vendorAddress.getVendorCountryCode());
312         } else {
313             this.getDvPayeeDetail().setDisbVchrVendorAddressIdNumber("");
314             this.getDvPayeeDetail().setDisbVchrPayeeLine1Addr("");
315             this.getDvPayeeDetail().setDisbVchrPayeeLine2Addr("");
316             this.getDvPayeeDetail().setDisbVchrPayeeCityName("");
317             this.getDvPayeeDetail().setDisbVchrPayeeStateCode("");
318             this.getDvPayeeDetail().setDisbVchrPayeeZipCode("");
319             this.getDvPayeeDetail().setDisbVchrPayeeCountryCode("");
320         }
321 
322 
323         this.getDvPayeeDetail().setDvPayeeSubjectPaymentCode(VendorConstants.VendorTypes.SUBJECT_PAYMENT.equals(vendor.getVendorHeader().getVendorTypeCode()));
324         this.getDvPayeeDetail().setDisbVchrEmployeePaidOutsidePayrollCode(getVendorService().isVendorInstitutionEmployee(vendor.getVendorHeaderGeneratedIdentifier()));
325 
326         this.getDvPayeeDetail().setHasMultipleVendorAddresses(1 < vendor.getVendorAddresses().size());
327 
328         boolean w9AndW8Checked = false;
329         if ((ObjectUtils.isNotNull(vendor.getVendorHeader().getVendorW9ReceivedIndicator()) && vendor.getVendorHeader().getVendorW9ReceivedIndicator() == true) ||
330                 (ObjectUtils.isNotNull(vendor.getVendorHeader().getVendorW8BenReceivedIndicator()) && vendor.getVendorHeader().getVendorW8BenReceivedIndicator() == true)) {
331 
332             w9AndW8Checked = true;
333         }
334 
335         
336         this.disbVchrPayeeW9CompleteCode = w9AndW8Checked;
337 
338         Date vendorFederalWithholdingTaxBeginDate = vendor.getVendorHeader().getVendorFederalWithholdingTaxBeginningDate();
339         Date vendorFederalWithholdingTaxEndDate = vendor.getVendorHeader().getVendorFederalWithholdingTaxEndDate();
340         java.util.Date today = getDateTimeService().getCurrentDate();
341         if ((vendorFederalWithholdingTaxBeginDate != null && vendorFederalWithholdingTaxBeginDate.before(today)) && (vendorFederalWithholdingTaxEndDate == null || vendorFederalWithholdingTaxEndDate.after(today))) {
342             this.disbVchrPayeeTaxControlCode = DisbursementVoucherConstants.TAX_CONTROL_CODE_BEGIN_WITHHOLDING;
343         }
344 
345         
346         if (getVendorService().isVendorForeign(vendor.getVendorHeaderGeneratedIdentifier())) {
347             getDvPayeeDetail().setDisbVchrAlienPaymentCode(true);
348         }
349     }
350 
351     
352 
353 
354 
355 
356 
357 
358     @Override
359     public void populateDocumentForRouting() {
360         OleDisbursementVoucherPayeeDetail payeeDetail = getDvPayeeDetail();
361 
362         if (payeeDetail.isVendor()) {
363             payeeDetail.setDisbVchrPayeeEmployeeCode(getVendorService().isVendorInstitutionEmployee(payeeDetail.getDisbVchrVendorHeaderIdNumberAsInteger()));
364             payeeDetail.setDvPayeeSubjectPaymentCode(getVendorService().isSubjectPaymentVendor(payeeDetail.getDisbVchrVendorHeaderIdNumberAsInteger()));
365         } else if (payeeDetail.isEmployee()) {
366             
367             
368             
369 
370 
371 
372 
373 
374 
375 
376 
377 
378 
379 
380 
381         }
382         if (this.getPaymentMethodId() != null) {
383             OlePaymentMethod olePaymentMethod = SpringContext.getBean(BusinessObjectService.class).findBySinglePrimaryKey(OlePaymentMethod.class, this.getPaymentMethodId());
384             this.setPaymentMethod(olePaymentMethod);
385         }
386         super.populateDocumentForRouting(); 
387     }
388 
389     
390 
391 
392     @Override
393     public void initiateDocument() {
394         Person currentUser = GlobalVariables.getUserSession().getPerson();
395         setDisbVchrContactPersonName(currentUser.getName());
396         setDisbVchrContactPhoneNumber(currentUser.getPhoneNumber());
397         setDisbVchrContactEmailId(currentUser.getEmailAddress());
398         ChartOrgHolder chartOrg = SpringContext.getBean(org.kuali.ole.sys.service.FinancialSystemUserService.class).getPrimaryOrganization(currentUser, OLEConstants.ParameterNamespaces.FINANCIAL);
399 
400         
401         
402         if (chartOrg != null && chartOrg.getOrganization() != null) {
403             setCampusCode(chartOrg.getOrganization().getOrganizationPhysicalCampusCode());
404         }
405         
406         
407         else {
408             String affiliatedCampusCode = currentUser.getCampusCode();
409             setCampusCode(affiliatedCampusCode);
410         }
411 
412         
413         Calendar calendar = getDateTimeService().getCurrentCalendar();
414         calendar.add(Calendar.DAY_OF_MONTH, 1);
415         setDisbursementVoucherDueDate(new Date(calendar.getTimeInMillis()));
416 
417         
418         if (StringUtils.isBlank(getDisbursementVoucherDocumentationLocationCode())) {
419             setDisbursementVoucherDocumentationLocationCode(getParameterService().getParameterValueAsString(
420                     DisbursementVoucherDocument.class, DisbursementVoucherConstants.DEFAULT_DOC_LOCATION_PARM_NM));
421         }
422 
423         
424         Bank defaultBank = SpringContext.getBean(BankService.class).getDefaultBankByDocType(this.getClass());
425         if (defaultBank != null) {
426             this.disbVchrBankCode = defaultBank.getBankCode();
427             this.bank = defaultBank;
428         }
429         OlePaymentMethod olePaymentMethod = new OlePaymentMethod();
430         if (this.getPaymentMethodId() != null) {
431             olePaymentMethod = SpringContext.getBean(BusinessObjectService.class).findBySinglePrimaryKey(
432                     OlePaymentMethod.class, this.getPaymentMethodId());
433         }
434 
435         this.setPaymentMethod(olePaymentMethod);
436     }
437 
438     
439 
440 
441 
442 
443 
444 
445     @Override
446     public void addSourceAccountingLine(SourceAccountingLine line) {
447         OleDisbursementVoucherAccountingLine oleDisbursementVoucherAccountingLine = (OleDisbursementVoucherAccountingLine) line;
448         oleDisbursementVoucherAccountingLine.setSequenceNumber(this.getNextSourceLineNumber());
449         this.sourceAccountingLines.add(oleDisbursementVoucherAccountingLine);
450         this.nextSourceLineNumber = new Integer(this.getNextSourceLineNumber().intValue() + 1);
451     }
452 
453     
454 
455 
456 
457 
458 
459 
460 
461 
462     @Override
463     public OleDisbursementVoucherAccountingLine getSourceAccountingLine(int index) {
464         while (getSourceAccountingLines().size() <= index) {
465             try {
466                 getSourceAccountingLines().add((OleDisbursementVoucherAccountingLine) getSourceAccountingLineClass().newInstance());
467             } catch (InstantiationException e) {
468                 throw new RuntimeException("Unable to get class");
469             } catch (IllegalAccessException e) {
470                 throw new RuntimeException("Unable to get class");
471             }
472         }
473         return getSourceAccountingLines().get(index);
474     }
475 
476     
477 
478 
479 
480 
481     @Override
482     public Class getSourceAccountingLineClass() {
483         if (sourceAccountingLineClass == null) {
484             sourceAccountingLineClass = (getDataDictionaryEntry().getAccountingLineGroups() != null && getDataDictionaryEntry().getAccountingLineGroups().containsKey("source") && getDataDictionaryEntry().getAccountingLineGroups().get("source").getAccountingLineClass() != null) ? getDataDictionaryEntry().getAccountingLineGroups().get("source").getAccountingLineClass() : OleDisbursementVoucherAccountingLine.class;
485         }
486         return sourceAccountingLineClass;
487     }
488 
489     
490 
491 
492     @Override
493     public List<OleDisbursementVoucherAccountingLine> getSourceAccountingLines() {
494         return this.sourceAccountingLines;
495     }
496 
497     
498 
499 
500     @Override
501     public void setSourceAccountingLines(List sourceLines) {
502         this.sourceAccountingLines = sourceLines;
503     }
504 
505     
506 
507 
508 
509 
510 
511 
512 
513 
514     @Override
515     public boolean answerSplitNodeQuestion(String nodeName) throws UnsupportedOperationException {
516         if (nodeName.equals(OleDisbursementVoucherDocument.HAS_CLEARING_ACCOUNT_TYPE_SPLIT)) {
517             return hasClearingAccountType();
518         }
519         if (nodeName.equals(DisbursementVoucherDocument.PAYEE_IS_PURCHASE_ORDER_VENDOR_SPLIT)) {
520             return isPayeePurchaseOrderVendor();
521         }
522         if (nodeName.equals(DisbursementVoucherDocument.DOCUMENT_REQUIRES_TAX_REVIEW_SPLIT)) {
523             return isTaxReviewRequired();
524         }
525         if (nodeName.equals(DisbursementVoucherDocument.DOCUMENT_REQUIRES_TRAVEL_REVIEW_SPLIT)) {
526             return true;
527         }
528         if (nodeName.equals(DOCUMENT_REQUIRES_SEPARATION_OF_DUTIES)) {
529             return isSeparationOfDutiesReviewRequired();
530         }
531         throw new UnsupportedOperationException("Cannot answer split question for this node you call \"" + nodeName + "\"");
532     }
533 
534     private boolean hasClearingAccountType() {
535         boolean hasAccountType = false;
536         List<OleDisbursementVoucherAccountingLine> sourceAccounts = this.getSourceAccountingLines();
537         for (OleDisbursementVoucherAccountingLine sourceAccount : sourceAccounts) {
538             if (sourceAccount.getAccount().getSubFundGroupCode().equalsIgnoreCase(OLEConstants.CLEARING_ACCOUNT_CODE)) {
539                 hasAccountType = true;
540             }
541         }
542 
543 
544 
545 
546 
547 
548 
549         return hasAccountType;
550     }
551 
552     private LookupService getLookupService() {
553         return KRADServiceLocatorWeb.getLookupService();
554     }
555 
556 }