001/*
002 * Copyright 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.service.impl;
017
018import org.kuali.ole.integration.purap.PurchasingAccountsPayableModuleService;
019import org.kuali.ole.integration.purap.PurchasingAccountsPayableSensitiveData;
020import org.kuali.ole.module.purap.PurapConstants;
021import org.kuali.ole.module.purap.PurapParameterConstants;
022import org.kuali.ole.module.purap.businessobject.SensitiveData;
023import org.kuali.ole.module.purap.document.PaymentRequestDocument;
024import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
025import org.kuali.ole.module.purap.document.VendorCreditMemoDocument;
026import org.kuali.ole.module.purap.document.service.CreditMemoService;
027import org.kuali.ole.module.purap.document.service.PaymentRequestService;
028import org.kuali.ole.module.purap.document.service.PurapService;
029import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
030import org.kuali.ole.sys.context.SpringContext;
031import org.kuali.rice.coreservice.framework.parameter.ParameterService;
032import org.kuali.rice.krad.bo.Note;
033import org.kuali.rice.krad.service.BusinessObjectService;
034import org.kuali.rice.krad.service.DocumentService;
035import org.kuali.rice.krad.service.NoteService;
036import org.kuali.rice.krad.util.ObjectUtils;
037
038import java.sql.Date;
039import java.util.*;
040
041public class PurchasingAccountsPayableModuleServiceImpl implements PurchasingAccountsPayableModuleService {
042    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PurchasingAccountsPayableModuleServiceImpl.class);
043
044    private PurchaseOrderService purchaseOrderService;
045    private PurapService purapService;
046    private DocumentService documentService;
047
048    /**
049     * @see org.kuali.ole.integration.service.PurchasingAccountsPayableModuleService#addAssignedAssetNumbers(java.lang.Integer,
050     *      java.util.List)
051     */
052    public void addAssignedAssetNumbers(Integer purchaseOrderNumber, String principalId, String noteText) {
053        PurchaseOrderDocument document = purchaseOrderService.getCurrentPurchaseOrder(purchaseOrderNumber);
054
055        try {
056            Note assetNote = SpringContext.getBean(DocumentService.class).createNoteFromDocument(document, noteText);
057            // set the initiator user info to the new note
058            assetNote.setAuthorUniversalIdentifier(principalId);
059            document.addNote(assetNote);
060            SpringContext.getBean(NoteService.class).save(assetNote);
061        } catch (Exception e) {
062            throw new RuntimeException(e);
063        }
064    }
065
066    /**
067     * @see org.kuali.ole.integration.service.PurchasingAccountsPayableModuleService#getPurchaseOrderInquiryUrl(java.lang.Integer)
068     */
069    public String getPurchaseOrderInquiryUrl(Integer purchaseOrderNumber) {
070        PurchaseOrderDocument po = purchaseOrderService.getCurrentPurchaseOrder(purchaseOrderNumber);
071        if (ObjectUtils.isNotNull(po)) {
072            return "purapPurchaseOrder.do?methodToCall=docHandler&docId=" + po.getDocumentNumber() + "&command=displayDocSearchView";
073        } else {
074            return "";
075        }
076    }
077
078    /**
079     * @see org.kuali.ole.integration.service.PurchasingAccountsPayableModuleService#getAllSensitiveDatas()
080     */
081    public List<PurchasingAccountsPayableSensitiveData> getAllSensitiveDatas() {
082        List<PurchasingAccountsPayableSensitiveData> sensitiveDatas = new ArrayList<PurchasingAccountsPayableSensitiveData>();
083        Collection sensitiveDatasAsObjects = SpringContext.getBean(BusinessObjectService.class).findAll(SensitiveData.class);
084        for (Object rm : sensitiveDatasAsObjects) {
085            sensitiveDatas.add((PurchasingAccountsPayableSensitiveData) rm);
086        }
087        return sensitiveDatas;
088    }
089
090    /**
091     * @see org.kuali.ole.integration.service.PurchasingAccountsPayableModuleService#getSensitiveDataByCode(java.lang.String)
092     */
093    public PurchasingAccountsPayableSensitiveData getSensitiveDataByCode(String sensitiveDataCode) {
094        Map primaryKeys = new HashMap();
095        primaryKeys.put("sensitiveDataCode", sensitiveDataCode);
096        return (PurchasingAccountsPayableSensitiveData) SpringContext.getBean(BusinessObjectService.class).findByPrimaryKey(SensitiveData.class, primaryKeys);
097    }
098
099    /**
100     * @see org.kuali.ole.integration.service.PurchasingAccountsPayableModuleService#isPurchasingBatchDocument(java.lang.String)
101     */
102    public boolean isPurchasingBatchDocument(String documentTypeCode) {
103        if (PurapConstants.PurapDocTypeCodes.PAYMENT_REQUEST_DOCUMENT.equals(documentTypeCode) || PurapConstants.PurapDocTypeCodes.CREDIT_MEMO_DOCUMENT.equals(documentTypeCode)) {
104            return true;
105        }
106        return false;
107    }
108
109    /**
110     * @see org.kuali.ole.integration.service.PurchasingAccountsPayableModuleService#handlePurchasingBatchCancels(java.lang.String)
111     */
112    public void handlePurchasingBatchCancels(String documentNumber, String documentTypeCode, boolean primaryCancel, boolean disbursedPayment) {
113        ParameterService parameterService = SpringContext.getBean(ParameterService.class);
114        PaymentRequestService paymentRequestService = SpringContext.getBean(PaymentRequestService.class);
115        CreditMemoService creditMemoService = SpringContext.getBean(CreditMemoService.class);
116
117        String preqCancelNote = parameterService.getParameterValueAsString(PaymentRequestDocument.class, PurapParameterConstants.PURAP_PDP_PREQ_CANCEL_NOTE);
118        String preqResetNote = parameterService.getParameterValueAsString(PaymentRequestDocument.class, PurapParameterConstants.PURAP_PDP_PREQ_RESET_NOTE);
119        String cmCancelNote = parameterService.getParameterValueAsString(VendorCreditMemoDocument.class, PurapParameterConstants.PURAP_PDP_CM_CANCEL_NOTE);
120        String cmResetNote = parameterService.getParameterValueAsString(VendorCreditMemoDocument.class, PurapParameterConstants.PURAP_PDP_CM_RESET_NOTE);
121
122        if (PurapConstants.PurapDocTypeCodes.PAYMENT_REQUEST_DOCUMENT.equals(documentTypeCode)) {
123            PaymentRequestDocument pr = paymentRequestService.getPaymentRequestByDocumentNumber(documentNumber);
124            if (pr != null) {
125                if (disbursedPayment || primaryCancel) {
126                    paymentRequestService.cancelExtractedPaymentRequest(pr, preqCancelNote);
127                } else {
128                    paymentRequestService.resetExtractedPaymentRequest(pr, preqResetNote);
129                }
130            } else {
131                LOG.error("processPdpCancels() DOES NOT EXIST, CANNOT PROCESS - Payment Request with doc type of " + documentTypeCode + " with id " + documentNumber);
132            }
133        } else if (PurapConstants.PurapDocTypeCodes.CREDIT_MEMO_DOCUMENT.equals(documentTypeCode)) {
134            VendorCreditMemoDocument cm = creditMemoService.getCreditMemoByDocumentNumber(documentNumber);
135            if (cm != null) {
136                if (disbursedPayment || primaryCancel) {
137                    creditMemoService.cancelExtractedCreditMemo(cm, cmCancelNote);
138                } else {
139                    creditMemoService.resetExtractedCreditMemo(cm, cmResetNote);
140                }
141            } else {
142                LOG.error("processPdpCancels() DOES NOT EXIST, CANNOT PROCESS - Credit Memo with doc type of " + documentTypeCode + " with id " + documentNumber);
143            }
144        }
145    }
146
147    /**
148     * @see org.kuali.ole.integration.service.PurchasingAccountsPayableModuleService#handlePurchasingBatchPaids(java.lang.String)
149     */
150    public void handlePurchasingBatchPaids(String documentNumber, String documentTypeCode, Date processDate) {
151        ParameterService parameterService = SpringContext.getBean(ParameterService.class);
152        PaymentRequestService paymentRequestService = SpringContext.getBean(PaymentRequestService.class);
153        CreditMemoService creditMemoService = SpringContext.getBean(CreditMemoService.class);
154
155        if (PurapConstants.PurapDocTypeCodes.PAYMENT_REQUEST_DOCUMENT.equals(documentTypeCode)) {
156            PaymentRequestDocument pr = paymentRequestService.getPaymentRequestByDocumentNumber(documentNumber);
157            if (pr != null) {
158                paymentRequestService.markPaid(pr, processDate);
159            } else {
160                LOG.error("processPdpPaids() DOES NOT EXIST, CANNOT MARK - Payment Request with doc type of " + documentTypeCode + " with id " + documentNumber);
161            }
162        } else if (PurapConstants.PurapDocTypeCodes.CREDIT_MEMO_DOCUMENT.equals(documentTypeCode)) {
163            VendorCreditMemoDocument cm = creditMemoService.getCreditMemoByDocumentNumber(documentNumber);
164            if (cm != null) {
165                creditMemoService.markPaid(cm, processDate);
166            } else {
167                LOG.error("processPdpPaids() DOES NOT EXIST, CANNOT PROCESS - Credit Memo with doc type of " + documentTypeCode + " with id " + documentNumber);
168            }
169        }
170
171    }
172
173    public String getB2BUrlString() {
174        return PurapConstants.B2B_URL_STRING;
175    }
176
177    public void setPurchaseOrderService(PurchaseOrderService purchaseOrderService) {
178        this.purchaseOrderService = purchaseOrderService;
179    }
180
181    public void setDocumentService(DocumentService documentService) {
182        this.documentService = documentService;
183    }
184
185    public void setPurapService(PurapService purapService) {
186        this.purapService = purapService;
187    }
188
189}
190