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.pdp.batch.service.impl;
017    
018    import java.sql.Date;
019    import java.util.ArrayList;
020    import java.util.Iterator;
021    import java.util.List;
022    
023    import org.kuali.kfs.fp.batch.service.DisbursementVoucherExtractService;
024    import org.kuali.kfs.fp.document.DisbursementVoucherConstants;
025    import org.kuali.kfs.fp.document.DisbursementVoucherDocument;
026    import org.kuali.kfs.integration.purap.PurchasingAccountsPayableModuleService;
027    import org.kuali.kfs.pdp.PdpConstants;
028    import org.kuali.kfs.pdp.batch.service.ProcessPdpCancelPaidService;
029    import org.kuali.kfs.pdp.businessobject.PaymentDetail;
030    import org.kuali.kfs.pdp.service.PaymentDetailService;
031    import org.kuali.kfs.pdp.service.PaymentGroupService;
032    import org.kuali.kfs.sys.KFSParameterKeyConstants;
033    import org.kuali.kfs.sys.service.impl.KfsParameterConstants;
034    import org.kuali.rice.kns.service.DateTimeService;
035    import org.kuali.rice.kns.service.ParameterService;
036    import org.springframework.transaction.annotation.Transactional;
037    
038    /**
039     * Implementation of ProcessPdpCancelPaidService
040     */
041    @Transactional
042    public class ProcessPdpCancelPaidServiceImpl implements ProcessPdpCancelPaidService {
043        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProcessPdpCancelPaidServiceImpl.class);
044    
045        private PaymentGroupService paymentGroupService;
046        private PaymentDetailService paymentDetailService;
047        private ParameterService parameterService;
048        private DateTimeService dateTimeService;
049        private PurchasingAccountsPayableModuleService purchasingAccountsPayableModuleService;
050        private DisbursementVoucherExtractService dvExtractService;
051    
052        /**
053         * @see org.kuali.kfs.module.purap.service.ProcessPdpCancelPaidService#processPdpCancels()
054         */
055        public void processPdpCancels() {
056            LOG.debug("processPdpCancels() started");
057    
058            Date processDate = dateTimeService.getCurrentSqlDate();
059    
060            String organization = parameterService.getParameterValue(KfsParameterConstants.PURCHASING_BATCH.class, KFSParameterKeyConstants.PurapPdpParameterConstants.PURAP_PDP_ORG_CODE);
061            String purapSubUnit = parameterService.getParameterValue(KfsParameterConstants.PURCHASING_BATCH.class, KFSParameterKeyConstants.PurapPdpParameterConstants.PURAP_PDP_SUB_UNIT_CODE);
062            String dvSubUnit = parameterService.getParameterValue(DisbursementVoucherDocument.class, DisbursementVoucherConstants.DvPdpExtractGroup.DV_PDP_SBUNT_CODE);
063    
064            List<String> subUnits = new ArrayList<String>();
065            subUnits.add(purapSubUnit);
066            subUnits.add(dvSubUnit);
067    
068            Iterator<PaymentDetail> details = paymentDetailService.getUnprocessedCancelledDetails(organization, subUnits);
069            while (details.hasNext()) {
070                PaymentDetail paymentDetail = details.next();
071    
072                String documentTypeCode = paymentDetail.getFinancialDocumentTypeCode();
073                String documentNumber = paymentDetail.getCustPaymentDocNbr();
074    
075                boolean primaryCancel = paymentDetail.getPrimaryCancelledPayment();
076                boolean disbursedPayment = PdpConstants.PaymentStatusCodes.CANCEL_PAYMENT.equals(paymentDetail.getPaymentGroup().getPaymentStatusCode());
077    
078                if(purchasingAccountsPayableModuleService.isPurchasingBatchDocument(documentTypeCode)) {
079                    purchasingAccountsPayableModuleService.handlePurchasingBatchCancels(documentNumber, documentTypeCode, primaryCancel, disbursedPayment);
080                }
081                else if (DisbursementVoucherConstants.DOCUMENT_TYPE_CHECKACH.equals(documentTypeCode)) {
082                    DisbursementVoucherDocument dv = dvExtractService.getDocumentById(documentNumber);
083                    if (dv != null) {
084                        if (disbursedPayment || primaryCancel) {
085                            dvExtractService.cancelExtractedDisbursementVoucher(dv, processDate);
086                        } else {
087                            dvExtractService.resetExtractedDisbursementVoucher(dv, processDate);
088                        }
089                    }
090                }
091                else {
092                    LOG.warn("processPdpCancels() Unknown document type (" + documentTypeCode + ") for document ID: " + documentNumber);
093                    continue;
094                }
095    
096                paymentGroupService.processCancelledGroup(paymentDetail.getPaymentGroup(), processDate);
097            }
098        }
099    
100        /**
101         * @see org.kuali.kfs.module.purap.service.ProcessPdpCancelPaidService#processPdpPaids()
102         */
103        public void processPdpPaids() {
104            LOG.debug("processPdpPaids() started");
105    
106            Date processDate = dateTimeService.getCurrentSqlDate();
107    
108            String organization = parameterService.getParameterValue(KfsParameterConstants.PURCHASING_BATCH.class, KFSParameterKeyConstants.PurapPdpParameterConstants.PURAP_PDP_ORG_CODE);
109            String purapSubUnit = parameterService.getParameterValue(KfsParameterConstants.PURCHASING_BATCH.class, KFSParameterKeyConstants.PurapPdpParameterConstants.PURAP_PDP_SUB_UNIT_CODE);
110            String dvSubUnit = parameterService.getParameterValue(DisbursementVoucherDocument.class, DisbursementVoucherConstants.DvPdpExtractGroup.DV_PDP_SBUNT_CODE);
111    
112            List<String> subUnits = new ArrayList<String>();
113            subUnits.add(purapSubUnit);
114            subUnits.add(dvSubUnit);
115    
116            Iterator<PaymentDetail> details = paymentDetailService.getUnprocessedPaidDetails(organization, subUnits);
117            while (details.hasNext()) {
118                PaymentDetail paymentDetail = details.next();
119    
120                String documentTypeCode = paymentDetail.getFinancialDocumentTypeCode();
121                String documentNumber = paymentDetail.getCustPaymentDocNbr();
122    
123                if(purchasingAccountsPayableModuleService.isPurchasingBatchDocument(documentTypeCode)) {
124                    purchasingAccountsPayableModuleService.handlePurchasingBatchPaids(documentNumber, documentTypeCode, processDate);
125                }
126                else if (documentTypeCode.equals(DisbursementVoucherConstants.DOCUMENT_TYPE_CHECKACH)) {
127                    DisbursementVoucherDocument dv = dvExtractService.getDocumentById(documentNumber);
128                    dvExtractService.markDisbursementVoucherAsPaid(dv, processDate);
129                }
130                else {
131                    LOG.warn("processPdpPaids() Unknown document type (" + documentTypeCode + ") for document ID: " + documentNumber);
132                    continue;
133                }
134    
135                paymentGroupService.processPaidGroup(paymentDetail.getPaymentGroup(), processDate);
136            }
137        }
138    
139        /**
140         * @see org.kuali.kfs.module.purap.service.ProcessPdpCancelPaidService#processPdpCancelsAndPaids()
141         */
142        public void processPdpCancelsAndPaids() {
143            LOG.debug("processPdpCancelsAndPaids() started");
144    
145            processPdpCancels();
146            processPdpPaids();
147        }
148    
149        public void setPaymentDetailService(PaymentDetailService paymentDetailService) {
150            this.paymentDetailService = paymentDetailService;
151        }
152    
153        public void setPaymentGroupService(PaymentGroupService paymentGroupService) {
154            this.paymentGroupService = paymentGroupService;
155        }
156    
157        public void setParameterService(ParameterService parameterService) {
158            this.parameterService = parameterService;
159        }
160    
161        public void setPurchasingAccountsPayableModuleService(PurchasingAccountsPayableModuleService purchasingAccountsPayableModuleService) {
162            this.purchasingAccountsPayableModuleService = purchasingAccountsPayableModuleService;
163        }
164    
165        public void setDateTimeService(DateTimeService dts) {
166            this.dateTimeService = dts;
167        }
168    
169        /**
170         * Sets the dvExtractService attribute value.
171         * @param dvExtractService The dvExtractService to set.
172         */
173        public void setDvExtractService(DisbursementVoucherExtractService dvExtractService) {
174            this.dvExtractService = dvExtractService;
175        }
176        
177    }