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.purap.document;
017    
018    import java.util.LinkedHashMap;
019    import java.util.List;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.apache.log4j.Logger;
023    import org.kuali.kfs.module.purap.PurapConstants;
024    import org.kuali.kfs.module.purap.document.service.BulkReceivingService;
025    import org.kuali.kfs.module.purap.document.service.RequisitionService;
026    import org.kuali.kfs.module.purap.document.validation.event.AttributedContinuePurapEvent;
027    import org.kuali.kfs.module.purap.util.PurapSearchUtils;
028    import org.kuali.kfs.sys.context.SpringContext;
029    import org.kuali.kfs.vnd.VendorConstants;
030    import org.kuali.kfs.vnd.businessobject.VendorAddress;
031    import org.kuali.kfs.vnd.businessobject.VendorDetail;
032    import org.kuali.kfs.vnd.document.service.VendorService;
033    import org.kuali.rice.kim.bo.Person;
034    import org.kuali.rice.kim.service.PersonService;
035    import org.kuali.rice.kns.bo.Country;
036    import org.kuali.rice.kns.bo.DocumentHeader;
037    import org.kuali.rice.kns.rule.event.KualiDocumentEvent;
038    import org.kuali.rice.kns.service.CountryService;
039    import org.kuali.rice.kns.service.DataDictionaryService;
040    import org.kuali.rice.kns.service.DateTimeService;
041    import org.kuali.rice.kns.util.GlobalVariables;
042    import org.kuali.rice.kns.util.KNSPropertyConstants;
043    
044    public class BulkReceivingDocument extends ReceivingDocumentBase{
045    
046        protected static final Logger LOG = Logger.getLogger(BulkReceivingDocument.class);
047        
048        protected String shipmentWeight;
049        protected Integer noOfCartons;
050        protected String trackingNumber;
051        protected String vendorAddressInternationalProvinceName;
052        protected String vendorNoteText;
053    
054        /**
055         * Goods delivered vendor
056         */
057        protected Integer goodsDeliveredVendorHeaderGeneratedIdentifier;
058        protected Integer goodsDeliveredVendorDetailAssignedIdentifier;
059        protected String goodsDeliveredVendorNumber;
060        protected String deliveryAdditionalInstructionText;
061    
062        protected String requestorPersonName;
063        protected String requestorPersonPhoneNumber;
064        protected String requestorPersonEmailAddress;
065        
066        protected String preparerPersonName;
067        protected String preparerPersonPhoneNumber;
068        
069        protected String deliveryCampusName;
070        protected String institutionContactName;
071        protected String institutionContactPhoneNumber;
072        protected String institutionContactEmailAddress;
073    
074        protected VendorDetail alternateVendorDetail;
075        
076        /**
077         * Not persisted in DB
078         */
079        protected String goodsDeliveredVendorName;
080        protected String vendorContact;
081        
082        public BulkReceivingDocument() {
083            super();
084        }
085    
086        public void initiateDocument(){
087            setShipmentReceivedDate(SpringContext.getBean(DateTimeService.class).getCurrentSqlDate());
088        }
089        
090        public void populateBulkReceivingFromPurchaseOrder(PurchaseOrderDocument po){
091            
092            setPurchaseOrderIdentifier( po.getPurapDocumentIdentifier() );
093            getDocumentHeader().setOrganizationDocumentNumber( po.getDocumentHeader().getOrganizationDocumentNumber() );
094            setAccountsPayablePurchasingDocumentLinkIdentifier( po.getAccountsPayablePurchasingDocumentLinkIdentifier() );
095            
096            //copy vendor
097            setVendorHeaderGeneratedIdentifier( po.getVendorHeaderGeneratedIdentifier() );
098            setVendorDetailAssignedIdentifier( po.getVendorDetailAssignedIdentifier() );        
099            setVendorName( po.getVendorName() );
100            setVendorNumber( po.getVendorNumber() );
101            setVendorLine1Address( po.getVendorLine1Address() );
102            setVendorLine2Address( po.getVendorLine2Address() );
103            setVendorCityName( po.getVendorCityName() );
104            setVendorStateCode( po.getVendorStateCode() );
105            setVendorPostalCode( po.getVendorPostalCode() );
106            setVendorCountryCode( po.getVendorCountryCode() );
107            setVendorDetail(po.getVendorDetail());
108            setVendorNumber(po.getVendorNumber());
109            setVendorAddressInternationalProvinceName(po.getVendorAddressInternationalProvinceName());
110            setVendorNoteText(po.getVendorNoteText());
111            setVendorAddressGeneratedIdentifier(po.getVendorAddressGeneratedIdentifier());
112            
113            //copy alternate vendor
114            setAlternateVendorName( po.getAlternateVendorName() );
115            setAlternateVendorNumber( StringUtils.isEmpty(po.getAlternateVendorNumber()) ? null : po.getAlternateVendorNumber());
116            setAlternateVendorDetailAssignedIdentifier( po.getAlternateVendorDetailAssignedIdentifier() );
117            setAlternateVendorHeaderGeneratedIdentifier( po.getAlternateVendorHeaderGeneratedIdentifier() );
118            
119            //copy delivery
120            setDeliveryBuildingCode( po.getDeliveryBuildingCode() );
121            setDeliveryBuildingLine1Address( po.getDeliveryBuildingLine1Address() );
122            setDeliveryBuildingLine2Address( po.getDeliveryBuildingLine2Address() );
123            setDeliveryBuildingName( po.getDeliveryBuildingName() );        
124            setDeliveryBuildingRoomNumber( po.getDeliveryBuildingRoomNumber() );
125            setDeliveryCampusCode( po.getDeliveryCampusCode() );
126            setDeliveryCityName( po.getDeliveryCityName() );
127            setDeliveryCountryCode( po.getDeliveryCountryCode() );
128            setDeliveryInstructionText( po.getDeliveryInstructionText() );
129            setDeliveryPostalCode( po.getDeliveryPostalCode() );
130            setDeliveryStateCode( po.getDeliveryStateCode() );
131            setDeliveryToEmailAddress( po.getDeliveryToEmailAddress() );
132            setDeliveryToName( po.getDeliveryToName() );
133            setDeliveryToPhoneNumber( po.getDeliveryToPhoneNumber() );
134            setInstitutionContactName(po.getInstitutionContactName());
135            setInstitutionContactPhoneNumber(po.getInstitutionContactPhoneNumber());
136            setInstitutionContactEmailAddress(po.getInstitutionContactEmailAddress());
137    
138            //Requestor and Requisition preparer
139            setRequestorPersonName(po.getRequestorPersonName());
140            setRequestorPersonPhoneNumber(po.getRequestorPersonPhoneNumber());
141            setRequestorPersonEmailAddress(po.getRequestorPersonEmailAddress());
142            
143            RequisitionDocument reqDoc = SpringContext.getBean(RequisitionService.class).getRequisitionById(po.getRequisitionIdentifier());
144            if (reqDoc != null){ // reqDoc is null when called from unit test
145                String requisitionPreparer = reqDoc.getDocumentHeader().getWorkflowDocument().getInitiatorNetworkId();
146                /**
147                 * This is to get the user name for display
148                 */
149                Person initiatorUser = SpringContext.getBean(PersonService.class).getPersonByPrincipalName(requisitionPreparer);
150                setPreparerPersonName(initiatorUser.getName());
151            }
152            
153            if (getVendorNumber() != null){
154                setGoodsDeliveredVendorHeaderGeneratedIdentifier(getVendorHeaderGeneratedIdentifier());
155                setGoodsDeliveredVendorDetailAssignedIdentifier(getVendorDetailAssignedIdentifier());
156                setGoodsDeliveredVendorNumber(getVendorNumber());
157                setGoodsDeliveredVendorName(getVendorName());
158            }
159            
160            populateVendorDetails();
161            populateDocumentDescription(po);
162            
163        }
164         
165        protected void populateVendorDetails(){
166            
167            if (getVendorHeaderGeneratedIdentifier() != null &&
168                getVendorDetailAssignedIdentifier() != null){
169                VendorDetail tempVendor = new VendorDetail();
170                tempVendor.setVendorHeaderGeneratedIdentifier(getVendorHeaderGeneratedIdentifier());
171                tempVendor.setVendorDetailAssignedIdentifier(getVendorDetailAssignedIdentifier());
172                setVendorNumber(tempVendor.getVendorNumber());
173            }
174            
175            if (getAlternateVendorHeaderGeneratedIdentifier() != null &&
176                getAlternateVendorDetailAssignedIdentifier() != null){
177                
178                VendorDetail vendorDetail = SpringContext.getBean(VendorService.class).getVendorDetail(getAlternateVendorHeaderGeneratedIdentifier(), 
179                                                                                                       getAlternateVendorDetailAssignedIdentifier());
180                //copied from creditmemocreateserviceimpl.populatedocumentfromvendor
181                String userCampus = GlobalVariables.getUserSession().getPerson().getCampusCode();
182                VendorAddress vendorAddress = SpringContext.getBean(VendorService.class).getVendorDefaultAddress(getAlternateVendorHeaderGeneratedIdentifier(), 
183                                                                                                                 getAlternateVendorDetailAssignedIdentifier(), 
184                                                                                                                 VendorConstants.AddressTypes.REMIT, 
185                                                                                                                 userCampus);
186                if (vendorAddress == null) {
187                // pick up the default vendor po address type
188                    vendorAddress = SpringContext.getBean(VendorService.class).getVendorDefaultAddress(getAlternateVendorHeaderGeneratedIdentifier(), 
189                                                                                                       getAlternateVendorDetailAssignedIdentifier(), 
190                                                                                                       VendorConstants.AddressTypes.PURCHASE_ORDER, 
191                                                                                                       userCampus);
192                }
193    
194                if (vendorAddress != null){
195                    setAlternateVendorName(vendorDetail.getVendorName());
196                    setAlternateVendorNumber(vendorDetail.getVendorNumber());
197                    vendorDetail.setDefaultAddressLine1(vendorAddress.getVendorLine1Address());
198                    vendorDetail.setDefaultAddressLine2(vendorAddress.getVendorLine2Address());
199                    vendorDetail.setDefaultAddressCity(vendorAddress.getVendorCityName());
200                    vendorDetail.setDefaultAddressCountryCode(vendorAddress.getVendorCountryCode());
201                    vendorDetail.setDefaultAddressPostalCode(vendorAddress.getVendorZipCode());
202                    vendorDetail.setDefaultAddressStateCode(vendorAddress.getVendorStateCode());
203                    vendorDetail.setDefaultAddressInternationalProvince(vendorAddress.getVendorAddressInternationalProvinceName());
204                }
205    
206                setAlternateVendorDetail(vendorDetail);
207            }
208            
209            if (getGoodsDeliveredVendorHeaderGeneratedIdentifier() != null &&
210                getGoodsDeliveredVendorDetailAssignedIdentifier() != null){
211                VendorDetail tempVendor = new VendorDetail();
212                tempVendor.setVendorHeaderGeneratedIdentifier(getGoodsDeliveredVendorHeaderGeneratedIdentifier());
213                tempVendor.setVendorDetailAssignedIdentifier(getGoodsDeliveredVendorDetailAssignedIdentifier());
214                setGoodsDeliveredVendorNumber(tempVendor.getVendorNumber());
215                if (StringUtils.equals(getVendorNumber(),getGoodsDeliveredVendorNumber())){
216                    setGoodsDeliveredVendorName(getVendorName());
217                }else {
218                    setGoodsDeliveredVendorName(getAlternateVendorName());
219                }
220            }
221        }
222        
223        /**
224         * Perform logic needed to clear the initial fields on a Receiving Line Document
225         */
226        public void clearInitFields() {
227            // Clearing document overview fields
228            getDocumentHeader().setDocumentDescription(null);
229            getDocumentHeader().setExplanation(null);
230            getDocumentHeader().setFinancialDocumentTotalAmount(null);
231            getDocumentHeader().setOrganizationDocumentNumber(null);
232    
233            setPurchaseOrderIdentifier(null);
234            
235            setShipmentReceivedDate(SpringContext.getBean(DateTimeService.class).getCurrentSqlDate());
236            setShipmentPackingSlipNumber(null);
237            setShipmentBillOfLadingNumber(null);
238            setCarrierCode(null);        
239        }
240    
241        @Override
242        public void processAfterRetrieve() {
243            super.processAfterRetrieve();
244            refreshNonUpdateableReferences();
245            populateVendorDetails();
246        }
247        
248        @Override
249        public void prepareForSave(KualiDocumentEvent event) {
250            
251            if (event instanceof AttributedContinuePurapEvent) {
252                SpringContext.getBean(BulkReceivingService.class).populateBulkReceivingFromPurchaseOrder(this);
253                if (getPurchaseOrderIdentifier() == null){
254                    getDocumentHeader().setDocumentDescription(PurapConstants.BulkReceivingDocumentStrings.MESSAGE_BULK_RECEIVING_DEFAULT_DOC_DESCRIPTION);;
255                }
256            }else{
257                if (getGoodsDeliveredVendorNumber() != null){
258                    VendorDetail tempVendor = new VendorDetail();
259                    tempVendor.setVendorNumber(getGoodsDeliveredVendorNumber());
260                    setGoodsDeliveredVendorHeaderGeneratedIdentifier(tempVendor.getVendorHeaderGeneratedIdentifier());
261                    setGoodsDeliveredVendorDetailAssignedIdentifier(tempVendor.getVendorDetailAssignedIdentifier());
262                }
263            }
264            
265            super.prepareForSave(event);
266        }
267        
268        protected void populateDocumentDescription(PurchaseOrderDocument poDocument) {
269            String description = "PO: " + poDocument.getPurapDocumentIdentifier() + " Vendor: " + poDocument.getVendorName();
270            int noteTextMaxLength = SpringContext.getBean(DataDictionaryService.class).getAttributeMaxLength(DocumentHeader.class, KNSPropertyConstants.DOCUMENT_DESCRIPTION).intValue();
271            if (noteTextMaxLength < description.length()) {
272                description = description.substring(0, noteTextMaxLength);
273            }
274            getDocumentHeader().setDocumentDescription(description);
275        }
276    
277        public String getDeliveryCountryName() {
278            Country country = SpringContext.getBean(CountryService.class).getByPrimaryId(getDeliveryCountryCode());
279            if (country != null)
280                return country.getPostalCountryName();
281            return null;
282        }
283    
284        public String getGoodsDeliveredVendorNumber() {
285            return goodsDeliveredVendorNumber;
286        }
287    
288        public void setGoodsDeliveredVendorNumber(String goodsDeliveredVendorNumber) {
289            this.goodsDeliveredVendorNumber = goodsDeliveredVendorNumber;
290        }
291    
292        public Integer getNoOfCartons() {
293            return noOfCartons;
294        }
295    
296        public void setNoOfCartons(Integer noOfCartons) {
297            this.noOfCartons = noOfCartons;
298        }
299    
300        public String getVendorContact() {
301            return vendorContact;
302        }
303    
304        public void setVendorContact(String vendorContact) {
305            this.vendorContact = vendorContact;
306        }
307     
308        public VendorDetail getAlternateVendorDetail() {
309            return alternateVendorDetail;
310        }
311    
312        public void setAlternateVendorDetail(VendorDetail alternateVendorDetail) {
313            this.alternateVendorDetail = alternateVendorDetail;
314        }
315    
316        public String getShipmentWeight() {
317            return shipmentWeight;
318        }
319    
320        public void setShipmentWeight(String shipmentWeight) {
321            this.shipmentWeight = shipmentWeight;
322        }
323    
324        public String getGoodsDeliveredVendorName() {
325            return goodsDeliveredVendorName;
326        }
327        
328        public void setGoodsDeliveredVendorName(String goodsDeliveredVendorName) {
329            this.goodsDeliveredVendorName = goodsDeliveredVendorName;
330        }
331        
332        public String getVendorAddressInternationalProvinceName() {
333            return vendorAddressInternationalProvinceName;
334        }
335    
336        public void setVendorAddressInternationalProvinceName(String vendorAddressInternationalProvinceName) {
337            this.vendorAddressInternationalProvinceName = vendorAddressInternationalProvinceName;
338        }
339    
340        public String getDeliveryCampusName() {
341            return deliveryCampusName;
342        }
343    
344        public void setDeliveryCampusName(String deliveryCampusName) {
345            this.deliveryCampusName = deliveryCampusName;
346        }
347    
348        public String getRequestorPersonName() {
349            return requestorPersonName;
350        }
351    
352        public void setRequestorPersonName(String requestorPersonName) {
353            this.requestorPersonName = requestorPersonName;
354        }
355    
356        public String getRequestorPersonPhoneNumber() {
357            return requestorPersonPhoneNumber;
358        }
359    
360        public void setRequestorPersonPhoneNumber(String requestorPersonPhoneNumber) {
361            this.requestorPersonPhoneNumber = requestorPersonPhoneNumber;
362        }
363    
364        public String getPreparerPersonName() {
365            return preparerPersonName;
366        }
367    
368        public void setPreparerPersonName(String preparerPersonName) {
369            this.preparerPersonName = preparerPersonName;
370        }
371    
372        public String getPreparerPersonPhoneNumber() {
373            return preparerPersonPhoneNumber;
374        }
375    
376        public void setPreparerPersonPhoneNumber(String preparerPersonPhoneNumber) {
377            this.preparerPersonPhoneNumber = preparerPersonPhoneNumber;
378        }
379    
380        public String getTrackingNumber() {
381            return trackingNumber;
382        }
383    
384        public void setTrackingNumber(String trackingNumber) {
385            this.trackingNumber = trackingNumber;
386        }
387    
388        public String getVendorNoteText() {
389            return vendorNoteText;
390        }
391    
392        public void setVendorNoteText(String vendorNoteText) {
393            this.vendorNoteText = vendorNoteText;
394        }
395    
396        public String getRequestorPersonEmailAddress() {
397            return requestorPersonEmailAddress;
398        }
399    
400        public void setRequestorPersonEmailAddress(String requestorPersonEmailAddress) {
401            this.requestorPersonEmailAddress = requestorPersonEmailAddress;
402        }
403    
404        public String getInstitutionContactEmailAddress() {
405            return institutionContactEmailAddress;
406        }
407    
408        public void setInstitutionContactEmailAddress(String institutionContactEmailAddress) {
409            this.institutionContactEmailAddress = institutionContactEmailAddress;
410        }
411    
412        public String getInstitutionContactName() {
413            return institutionContactName;
414        }
415    
416        public void setInstitutionContactName(String institutionContactName) {
417            this.institutionContactName = institutionContactName;
418        }
419    
420        public String getInstitutionContactPhoneNumber() {
421            return institutionContactPhoneNumber;
422        }
423    
424        public void setInstitutionContactPhoneNumber(String institutionContactPhoneNumber) {
425            this.institutionContactPhoneNumber = institutionContactPhoneNumber;
426        }
427    
428        public String getDeliveryAdditionalInstructionText() {
429            return deliveryAdditionalInstructionText;
430        }
431    
432        public void setDeliveryAdditionalInstructionText(String deliveryAdditionalInstructionText) {
433            this.deliveryAdditionalInstructionText = deliveryAdditionalInstructionText;
434        }
435    
436        public Integer getGoodsDeliveredVendorDetailAssignedIdentifier() {
437            return goodsDeliveredVendorDetailAssignedIdentifier;
438        }
439    
440        public void setGoodsDeliveredVendorDetailAssignedIdentifier(Integer goodsDeliveredVendorDetailAssignedIdentifier) {
441            this.goodsDeliveredVendorDetailAssignedIdentifier = goodsDeliveredVendorDetailAssignedIdentifier;
442        }
443    
444        public Integer getGoodsDeliveredVendorHeaderGeneratedIdentifier() {
445            return goodsDeliveredVendorHeaderGeneratedIdentifier;
446        }
447    
448        public void setGoodsDeliveredVendorHeaderGeneratedIdentifier(Integer goodsDeliveredVendorHeaderGeneratedIdentifier) {
449            this.goodsDeliveredVendorHeaderGeneratedIdentifier = goodsDeliveredVendorHeaderGeneratedIdentifier;
450        }
451    
452        @Override
453        public boolean isBoNotesSupport() {
454            return true;
455        }
456        
457        protected LinkedHashMap toStringMapper() {
458            LinkedHashMap m = new LinkedHashMap();      
459            m.put("documentNumber", this.documentNumber);
460            m.put("PO", getPurchaseOrderIdentifier());
461            return m;
462        }
463        
464        public String getWorkflowStatusForResult(){
465            return PurapSearchUtils.getWorkFlowStatusString(getDocumentHeader());
466        }
467    
468        /**
469         * It's not needed to implement this method in this class since bulk receiving doesn't support items.
470         * Calling this method will throw an UnsupportedOperationException
471         * @throws UnsupportedOperationException
472         */
473        @Override
474        public Class getItemClass() {
475            throw new UnsupportedOperationException("Items are not being handled in Bulk Receiving document");
476        }
477    
478        /**
479         * It's not needed to implement PurapItemOperations methods in this class since bulk receiving doesn't support items. 
480         * Calling this method will throw an UnsupportedOperationException
481         * @throws UnsupportedOperationException
482         */
483        public <T> T getItem(int pos) {
484            throw new UnsupportedOperationException("Items are not being handled in Bulk Receiving document");
485        }
486    
487        /**
488         * It's not needed to implement PurapItemOperations methods in this class since bulk receiving doesn't support items. 
489         * Calling this method will throw an UnsupportedOperationException
490         * @throws UnsupportedOperationException
491         */
492        public List getItems() {
493            throw new UnsupportedOperationException("Items are not being handled in Bulk Receiving document");
494        }
495    
496        /**
497         * It's not needed to implement PurapItemOperations methods in this class since bulk receiving doesn't support items. 
498         * Calling this method will throw an UnsupportedOperationException
499         * @throws UnsupportedOperationException
500         */
501        public void setItems(List items) {
502            throw new UnsupportedOperationException("Items are not being handled in Bulk Receiving document");
503        }
504    
505    }