View Javadoc
1   /*
2    * Copyright 2006-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.document;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.log4j.Logger;
20  import org.kuali.ole.module.purap.PurapConstants;
21  import org.kuali.ole.module.purap.document.service.BulkReceivingService;
22  import org.kuali.ole.module.purap.document.service.RequisitionService;
23  import org.kuali.ole.module.purap.document.validation.event.AttributedContinuePurapEvent;
24  import org.kuali.ole.module.purap.util.PurapSearchUtils;
25  import org.kuali.ole.sys.context.SpringContext;
26  import org.kuali.ole.vnd.VendorConstants;
27  import org.kuali.ole.vnd.businessobject.VendorAddress;
28  import org.kuali.ole.vnd.businessobject.VendorDetail;
29  import org.kuali.ole.vnd.document.service.VendorService;
30  import org.kuali.rice.core.api.datetime.DateTimeService;
31  import org.kuali.rice.kim.api.identity.Person;
32  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
33  import org.kuali.rice.kns.service.DataDictionaryService;
34  import org.kuali.rice.krad.bo.DocumentHeader;
35  import org.kuali.rice.krad.rules.rule.event.DocumentEvent;
36  import org.kuali.rice.krad.rules.rule.event.DocumentEventBase;
37  import org.kuali.rice.krad.util.GlobalVariables;
38  import org.kuali.rice.krad.util.KRADPropertyConstants;
39  import org.kuali.rice.location.api.country.Country;
40  import org.kuali.rice.location.api.country.CountryService;
41  
42  import java.util.LinkedHashMap;
43  import java.util.List;
44  
45  public class BulkReceivingDocument extends ReceivingDocumentBase {
46  
47      protected static final Logger LOG = Logger.getLogger(BulkReceivingDocument.class);
48  
49      protected String shipmentWeight;
50      protected Integer noOfCartons;
51      protected String trackingNumber;
52      protected String vendorAddressInternationalProvinceName;
53      protected String vendorNoteText;
54  
55      /**
56       * Goods delivered vendor
57       */
58      protected Integer goodsDeliveredVendorHeaderGeneratedIdentifier;
59      protected Integer goodsDeliveredVendorDetailAssignedIdentifier;
60      protected String goodsDeliveredVendorNumber;
61      protected String deliveryAdditionalInstructionText;
62  
63      protected String requestorPersonName;
64      protected String requestorPersonPhoneNumber;
65      protected String requestorPersonEmailAddress;
66  
67      protected String preparerPersonName;
68      protected String preparerPersonPhoneNumber;
69  
70      protected String deliveryCampusName;
71      protected String institutionContactName;
72      protected String institutionContactPhoneNumber;
73      protected String institutionContactEmailAddress;
74  
75      protected VendorDetail alternateVendorDetail;
76  
77      /**
78       * Not persisted in DB
79       */
80      protected String goodsDeliveredVendorName;
81      protected String vendorContact;
82  
83      public BulkReceivingDocument() {
84          super();
85      }
86  
87      @Override
88      public void initiateDocument() {
89          setShipmentReceivedDate(SpringContext.getBean(DateTimeService.class).getCurrentSqlDate());
90      }
91  
92      public void populateBulkReceivingFromPurchaseOrder(PurchaseOrderDocument po) {
93  
94          setPurchaseOrderIdentifier(po.getPurapDocumentIdentifier());
95          getDocumentHeader().setOrganizationDocumentNumber(po.getDocumentHeader().getOrganizationDocumentNumber());
96          setAccountsPayablePurchasingDocumentLinkIdentifier(po.getAccountsPayablePurchasingDocumentLinkIdentifier());
97  
98          //copy vendor
99          setVendorHeaderGeneratedIdentifier(po.getVendorHeaderGeneratedIdentifier());
100         setVendorDetailAssignedIdentifier(po.getVendorDetailAssignedIdentifier());
101         setVendorName(po.getVendorName());
102         setVendorNumber(po.getVendorNumber());
103         setVendorLine1Address(po.getVendorLine1Address());
104         setVendorLine2Address(po.getVendorLine2Address());
105         setVendorCityName(po.getVendorCityName());
106         setVendorStateCode(po.getVendorStateCode());
107         setVendorPostalCode(po.getVendorPostalCode());
108         setVendorCountryCode(po.getVendorCountryCode());
109         setVendorDetail(po.getVendorDetail());
110         setVendorNumber(po.getVendorNumber());
111         setVendorAddressInternationalProvinceName(po.getVendorAddressInternationalProvinceName());
112         setVendorNoteText(po.getVendorNoteText());
113         setVendorAddressGeneratedIdentifier(po.getVendorAddressGeneratedIdentifier());
114 
115         //copy alternate vendor
116         setAlternateVendorName(po.getAlternateVendorName());
117         setAlternateVendorNumber(StringUtils.isEmpty(po.getAlternateVendorNumber()) ? null : po.getAlternateVendorNumber());
118         setAlternateVendorDetailAssignedIdentifier(po.getAlternateVendorDetailAssignedIdentifier());
119         setAlternateVendorHeaderGeneratedIdentifier(po.getAlternateVendorHeaderGeneratedIdentifier());
120 
121         //copy delivery
122         setDeliveryBuildingCode(po.getDeliveryBuildingCode());
123         setDeliveryBuildingLine1Address(po.getDeliveryBuildingLine1Address());
124         setDeliveryBuildingLine2Address(po.getDeliveryBuildingLine2Address());
125         setDeliveryBuildingName(po.getDeliveryBuildingName());
126         setDeliveryBuildingRoomNumber(po.getDeliveryBuildingRoomNumber());
127         setDeliveryCampusCode(po.getDeliveryCampusCode());
128         setDeliveryCityName(po.getDeliveryCityName());
129         setDeliveryCountryCode(po.getDeliveryCountryCode());
130         setDeliveryInstructionText(po.getDeliveryInstructionText());
131         setDeliveryPostalCode(po.getDeliveryPostalCode());
132         setDeliveryStateCode(po.getDeliveryStateCode());
133         setDeliveryToEmailAddress(po.getDeliveryToEmailAddress());
134         setDeliveryToName(po.getDeliveryToName());
135         setDeliveryToPhoneNumber(po.getDeliveryToPhoneNumber());
136         setInstitutionContactName(po.getInstitutionContactName());
137         setInstitutionContactPhoneNumber(po.getInstitutionContactPhoneNumber());
138         setInstitutionContactEmailAddress(po.getInstitutionContactEmailAddress());
139 
140         //Requestor and Requisition preparer
141         setRequestorPersonName(po.getRequestorPersonName());
142         setRequestorPersonPhoneNumber(po.getRequestorPersonPhoneNumber());
143         setRequestorPersonEmailAddress(po.getRequestorPersonEmailAddress());
144 
145         RequisitionDocument reqDoc = SpringContext.getBean(RequisitionService.class).getRequisitionById(po.getRequisitionIdentifier());
146         if (reqDoc != null) { // reqDoc is null when called from unit test
147             String requisitionPreparer = reqDoc.getFinancialSystemDocumentHeader().getWorkflowDocument().getInitiatorPrincipalId();
148             /**
149              * This is to get the user name for display
150              */
151             Person initiatorUser = KimApiServiceLocator.getPersonService().getPerson(requisitionPreparer);
152             setPreparerPersonName(initiatorUser.getName());
153         }
154 
155         if (getVendorNumber() != null) {
156             setGoodsDeliveredVendorHeaderGeneratedIdentifier(getVendorHeaderGeneratedIdentifier());
157             setGoodsDeliveredVendorDetailAssignedIdentifier(getVendorDetailAssignedIdentifier());
158             setGoodsDeliveredVendorNumber(getVendorNumber());
159             setGoodsDeliveredVendorName(getVendorName());
160         }
161 
162         populateVendorDetails();
163         populateDocumentDescription(po);
164 
165     }
166 
167     protected void populateVendorDetails() {
168 
169         if (getVendorHeaderGeneratedIdentifier() != null &&
170                 getVendorDetailAssignedIdentifier() != null) {
171             VendorDetail tempVendor = new VendorDetail();
172             tempVendor.setVendorHeaderGeneratedIdentifier(getVendorHeaderGeneratedIdentifier());
173             tempVendor.setVendorDetailAssignedIdentifier(getVendorDetailAssignedIdentifier());
174             setVendorNumber(tempVendor.getVendorNumber());
175         }
176 
177         if (getAlternateVendorHeaderGeneratedIdentifier() != null &&
178                 getAlternateVendorDetailAssignedIdentifier() != null) {
179 
180             VendorDetail vendorDetail = SpringContext.getBean(VendorService.class).getVendorDetail(getAlternateVendorHeaderGeneratedIdentifier(),
181                     getAlternateVendorDetailAssignedIdentifier());
182             //copied from creditmemocreateserviceimpl.populatedocumentfromvendor
183             String userCampus = GlobalVariables.getUserSession().getPerson().getCampusCode();
184             VendorAddress vendorAddress = SpringContext.getBean(VendorService.class).getVendorDefaultAddress(getAlternateVendorHeaderGeneratedIdentifier(),
185                     getAlternateVendorDetailAssignedIdentifier(),
186                     VendorConstants.AddressTypes.REMIT,
187                     userCampus);
188             if (vendorAddress == null) {
189                 // pick up the default vendor po address type
190                 vendorAddress = SpringContext.getBean(VendorService.class).getVendorDefaultAddress(getAlternateVendorHeaderGeneratedIdentifier(),
191                         getAlternateVendorDetailAssignedIdentifier(),
192                         VendorConstants.AddressTypes.PURCHASE_ORDER,
193                         userCampus);
194             }
195 
196             if (vendorAddress != null) {
197                 setAlternateVendorName(vendorDetail.getVendorName());
198                 setAlternateVendorNumber(vendorDetail.getVendorNumber());
199                 vendorDetail.setDefaultAddressLine1(vendorAddress.getVendorLine1Address());
200                 vendorDetail.setDefaultAddressLine2(vendorAddress.getVendorLine2Address());
201                 vendorDetail.setDefaultAddressCity(vendorAddress.getVendorCityName());
202                 vendorDetail.setDefaultAddressCountryCode(vendorAddress.getVendorCountryCode());
203                 vendorDetail.setDefaultAddressPostalCode(vendorAddress.getVendorZipCode());
204                 vendorDetail.setDefaultAddressStateCode(vendorAddress.getVendorStateCode());
205                 vendorDetail.setDefaultAddressInternationalProvince(vendorAddress.getVendorAddressInternationalProvinceName());
206             }
207 
208             setAlternateVendorDetail(vendorDetail);
209         }
210 
211         if (getGoodsDeliveredVendorHeaderGeneratedIdentifier() != null &&
212                 getGoodsDeliveredVendorDetailAssignedIdentifier() != null) {
213             VendorDetail tempVendor = new VendorDetail();
214             tempVendor.setVendorHeaderGeneratedIdentifier(getGoodsDeliveredVendorHeaderGeneratedIdentifier());
215             tempVendor.setVendorDetailAssignedIdentifier(getGoodsDeliveredVendorDetailAssignedIdentifier());
216             setGoodsDeliveredVendorNumber(tempVendor.getVendorNumber());
217             if (StringUtils.equals(getVendorNumber(), getGoodsDeliveredVendorNumber())) {
218                 setGoodsDeliveredVendorName(getVendorName());
219             } else {
220                 setGoodsDeliveredVendorName(getAlternateVendorName());
221             }
222         }
223     }
224 
225     /**
226      * Perform logic needed to clear the initial fields on a Receiving Line Document
227      */
228     public void clearInitFields() {
229         // Clearing document overview fields
230         getDocumentHeader().setDocumentDescription(null);
231         getDocumentHeader().setExplanation(null);
232         getFinancialSystemDocumentHeader().setFinancialDocumentTotalAmount(null);
233         getDocumentHeader().setOrganizationDocumentNumber(null);
234 
235         setPurchaseOrderIdentifier(null);
236 
237         setShipmentReceivedDate(SpringContext.getBean(DateTimeService.class).getCurrentSqlDate());
238         setShipmentPackingSlipNumber(null);
239         setShipmentBillOfLadingNumber(null);
240         setCarrierCode(null);
241     }
242 
243 
244     @Override
245     public void processAfterRetrieve() {
246         super.processAfterRetrieve();
247         refreshNonUpdateableReferences();
248         populateVendorDetails();
249     }
250 
251     @Override
252     public void prepareForSave(DocumentEvent event) {
253 
254         if (event instanceof AttributedContinuePurapEvent) {
255             SpringContext.getBean(BulkReceivingService.class).populateBulkReceivingFromPurchaseOrder(this);
256             if (getPurchaseOrderIdentifier() == null) {
257                 getDocumentHeader().setDocumentDescription(PurapConstants.BulkReceivingDocumentStrings.MESSAGE_BULK_RECEIVING_DEFAULT_DOC_DESCRIPTION);
258                 ;
259             }
260         } else {
261             if (getGoodsDeliveredVendorNumber() != null) {
262                 VendorDetail tempVendor = new VendorDetail();
263                 tempVendor.setVendorNumber(getGoodsDeliveredVendorNumber());
264                 setGoodsDeliveredVendorHeaderGeneratedIdentifier(tempVendor.getVendorHeaderGeneratedIdentifier());
265                 setGoodsDeliveredVendorDetailAssignedIdentifier(tempVendor.getVendorDetailAssignedIdentifier());
266             }
267         }
268 
269         super.prepareForSave(event);
270     }
271 
272     protected void populateDocumentDescription(PurchaseOrderDocument poDocument) {
273         String description = "PO: " + poDocument.getPurapDocumentIdentifier() + " Vendor: " + poDocument.getVendorName();
274         int noteTextMaxLength = SpringContext.getBean(DataDictionaryService.class).getAttributeMaxLength(DocumentHeader.class, KRADPropertyConstants.DOCUMENT_DESCRIPTION).intValue();
275         if (noteTextMaxLength < description.length()) {
276             description = description.substring(0, noteTextMaxLength);
277         }
278         getDocumentHeader().setDocumentDescription(description);
279     }
280 
281     @Override
282     public String getDeliveryCountryName() {
283         if (StringUtils.isNotBlank(getDeliveryCountryCode())) {
284             Country country = SpringContext.getBean(CountryService.class).getCountry(getDeliveryCountryCode());
285             if (country != null) {
286                 return country.getName();
287             }
288         }
289         return null;
290     }
291 
292     public String getGoodsDeliveredVendorNumber() {
293         return goodsDeliveredVendorNumber;
294     }
295 
296     public void setGoodsDeliveredVendorNumber(String goodsDeliveredVendorNumber) {
297         this.goodsDeliveredVendorNumber = goodsDeliveredVendorNumber;
298     }
299 
300     public Integer getNoOfCartons() {
301         return noOfCartons;
302     }
303 
304     public void setNoOfCartons(Integer noOfCartons) {
305         this.noOfCartons = noOfCartons;
306     }
307 
308     public String getVendorContact() {
309         return vendorContact;
310     }
311 
312     public void setVendorContact(String vendorContact) {
313         this.vendorContact = vendorContact;
314     }
315 
316     public VendorDetail getAlternateVendorDetail() {
317         return alternateVendorDetail;
318     }
319 
320     public void setAlternateVendorDetail(VendorDetail alternateVendorDetail) {
321         this.alternateVendorDetail = alternateVendorDetail;
322     }
323 
324     public String getShipmentWeight() {
325         return shipmentWeight;
326     }
327 
328     public void setShipmentWeight(String shipmentWeight) {
329         this.shipmentWeight = shipmentWeight;
330     }
331 
332     public String getGoodsDeliveredVendorName() {
333         return goodsDeliveredVendorName;
334     }
335 
336     public void setGoodsDeliveredVendorName(String goodsDeliveredVendorName) {
337         this.goodsDeliveredVendorName = goodsDeliveredVendorName;
338     }
339 
340     public String getVendorAddressInternationalProvinceName() {
341         return vendorAddressInternationalProvinceName;
342     }
343 
344     public void setVendorAddressInternationalProvinceName(String vendorAddressInternationalProvinceName) {
345         this.vendorAddressInternationalProvinceName = vendorAddressInternationalProvinceName;
346     }
347 
348     public String getDeliveryCampusName() {
349         return deliveryCampusName;
350     }
351 
352     public void setDeliveryCampusName(String deliveryCampusName) {
353         this.deliveryCampusName = deliveryCampusName;
354     }
355 
356     public String getRequestorPersonName() {
357         return requestorPersonName;
358     }
359 
360     public void setRequestorPersonName(String requestorPersonName) {
361         this.requestorPersonName = requestorPersonName;
362     }
363 
364     public String getRequestorPersonPhoneNumber() {
365         return requestorPersonPhoneNumber;
366     }
367 
368     public void setRequestorPersonPhoneNumber(String requestorPersonPhoneNumber) {
369         this.requestorPersonPhoneNumber = requestorPersonPhoneNumber;
370     }
371 
372     public String getPreparerPersonName() {
373         return preparerPersonName;
374     }
375 
376     public void setPreparerPersonName(String preparerPersonName) {
377         this.preparerPersonName = preparerPersonName;
378     }
379 
380     public String getPreparerPersonPhoneNumber() {
381         return preparerPersonPhoneNumber;
382     }
383 
384     public void setPreparerPersonPhoneNumber(String preparerPersonPhoneNumber) {
385         this.preparerPersonPhoneNumber = preparerPersonPhoneNumber;
386     }
387 
388     public String getTrackingNumber() {
389         return trackingNumber;
390     }
391 
392     public void setTrackingNumber(String trackingNumber) {
393         this.trackingNumber = trackingNumber;
394     }
395 
396     public String getVendorNoteText() {
397         return vendorNoteText;
398     }
399 
400     public void setVendorNoteText(String vendorNoteText) {
401         this.vendorNoteText = vendorNoteText;
402     }
403 
404     public String getRequestorPersonEmailAddress() {
405         return requestorPersonEmailAddress;
406     }
407 
408     public void setRequestorPersonEmailAddress(String requestorPersonEmailAddress) {
409         this.requestorPersonEmailAddress = requestorPersonEmailAddress;
410     }
411 
412     public String getInstitutionContactEmailAddress() {
413         return institutionContactEmailAddress;
414     }
415 
416     public void setInstitutionContactEmailAddress(String institutionContactEmailAddress) {
417         this.institutionContactEmailAddress = institutionContactEmailAddress;
418     }
419 
420     public String getInstitutionContactName() {
421         return institutionContactName;
422     }
423 
424     public void setInstitutionContactName(String institutionContactName) {
425         this.institutionContactName = institutionContactName;
426     }
427 
428     public String getInstitutionContactPhoneNumber() {
429         return institutionContactPhoneNumber;
430     }
431 
432     public void setInstitutionContactPhoneNumber(String institutionContactPhoneNumber) {
433         this.institutionContactPhoneNumber = institutionContactPhoneNumber;
434     }
435 
436     public String getDeliveryAdditionalInstructionText() {
437         return deliveryAdditionalInstructionText;
438     }
439 
440     public void setDeliveryAdditionalInstructionText(String deliveryAdditionalInstructionText) {
441         this.deliveryAdditionalInstructionText = deliveryAdditionalInstructionText;
442     }
443 
444     public Integer getGoodsDeliveredVendorDetailAssignedIdentifier() {
445         return goodsDeliveredVendorDetailAssignedIdentifier;
446     }
447 
448     public void setGoodsDeliveredVendorDetailAssignedIdentifier(Integer goodsDeliveredVendorDetailAssignedIdentifier) {
449         this.goodsDeliveredVendorDetailAssignedIdentifier = goodsDeliveredVendorDetailAssignedIdentifier;
450     }
451 
452     public Integer getGoodsDeliveredVendorHeaderGeneratedIdentifier() {
453         return goodsDeliveredVendorHeaderGeneratedIdentifier;
454     }
455 
456     public void setGoodsDeliveredVendorHeaderGeneratedIdentifier(Integer goodsDeliveredVendorHeaderGeneratedIdentifier) {
457         this.goodsDeliveredVendorHeaderGeneratedIdentifier = goodsDeliveredVendorHeaderGeneratedIdentifier;
458     }
459 
460     @Override
461     public boolean isBoNotesSupport() {
462         return true;
463     }
464 
465     protected LinkedHashMap toStringMapper() {
466         LinkedHashMap m = new LinkedHashMap();
467         m.put("documentNumber", this.documentNumber);
468         m.put("PO", getPurchaseOrderIdentifier());
469         return m;
470     }
471 
472     @Override
473     public String getWorkflowStatusForResult() {
474         return PurapSearchUtils.getWorkFlowStatusString(getDocumentHeader());
475     }
476 
477     /**
478      * It's not needed to implement this method in this class since bulk receiving doesn't support items.
479      * Calling this method will throw an UnsupportedOperationException
480      *
481      * @throws UnsupportedOperationException
482      */
483     @Override
484     public Class getItemClass() {
485         throw new UnsupportedOperationException("Items are not being handled in Bulk Receiving document");
486     }
487 
488     /**
489      * It's not needed to implement PurapItemOperations methods in this class since bulk receiving doesn't support items.
490      * Calling this method will throw an UnsupportedOperationException
491      *
492      * @throws UnsupportedOperationException
493      */
494     @Override
495     public <T> T getItem(int pos) {
496         throw new UnsupportedOperationException("Items are not being handled in Bulk Receiving document");
497     }
498 
499     /**
500      * It's not needed to implement PurapItemOperations methods in this class since bulk receiving doesn't support items.
501      * Calling this method will throw an UnsupportedOperationException
502      *
503      * @throws UnsupportedOperationException
504      */
505     @Override
506     public List getItems() {
507         throw new UnsupportedOperationException("Items are not being handled in Bulk Receiving document");
508     }
509 
510     /**
511      * It's not needed to implement PurapItemOperations methods in this class since bulk receiving doesn't support items.
512      * Calling this method will throw an UnsupportedOperationException
513      *
514      * @throws UnsupportedOperationException
515      */
516     @Override
517     public void setItems(List items) {
518         throw new UnsupportedOperationException("Items are not being handled in Bulk Receiving document");
519     }
520 
521 }