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