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