001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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: /*
017: * Created on Jun 28, 2004
018: *
019: */
020: package org.kuali.module.pdp.xml.impl;
021:
022: import java.math.BigDecimal;
023: import java.sql.Timestamp;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import org.kuali.kfs.context.SpringContext;
029: import org.kuali.module.pdp.bo.CustomerProfile;
030: import org.kuali.module.pdp.bo.PdpUser;
031: import org.kuali.module.pdp.dao.CustomerProfileDao;
032: import org.kuali.module.pdp.dao.PaymentFileLoadDao;
033: import org.kuali.module.pdp.xml.PdpFileHandler;
034: import org.kuali.module.pdp.xml.XmlDetail;
035: import org.kuali.module.pdp.xml.XmlGroup;
036: import org.kuali.module.pdp.xml.XmlHeader;
037: import org.kuali.module.pdp.xml.XmlTrailer;
038:
039: /**
040: * @author jsissom
041: */
042: public class HardEditHandler implements PdpFileHandler {
043: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
044: .getLogger(HardEditHandler.class);
045:
046: private CustomerProfileDao customerDao;
047: private PaymentFileLoadDao loadDao;
048: private CustomerProfile customer;
049:
050: private XmlHeader header;
051: private XmlTrailer trailer;
052:
053: private int maxNoteLines;
054:
055: private int actualGroupCount = 0;
056: private int actualPaymentCount = 0;
057: private BigDecimal calculatedPaymentTotalAmount = new BigDecimal(
058: 0.00);
059: private List errorMessageList = new ArrayList();
060:
061: public HardEditHandler() {
062: customerDao = SpringContext.getBean(CustomerProfileDao.class);
063: loadDao = SpringContext.getBean(PaymentFileLoadDao.class);
064: }
065:
066: public void clear() {
067: header = null;
068: trailer = null;
069: actualPaymentCount = 0;
070: calculatedPaymentTotalAmount = new BigDecimal(0.00);
071: errorMessageList.clear();
072: }
073:
074: public void setMaxNoteLines(int lines) {
075: maxNoteLines = lines;
076: }
077:
078: public XmlHeader getHeader() {
079: return header;
080: }
081:
082: public XmlTrailer getTrailer() {
083: return trailer;
084: }
085:
086: public void setHeader(XmlHeader header) {
087: this .header = header;
088:
089: // See if this customer is valid & active
090: customer = customerDao.get(header.getChart(), header.getOrg(),
091: header.getSubUnit());
092: if (customer == null) {
093: setErrorMessage("Invalid Customer: " + header.getChart()
094: + "/" + header.getOrg() + "/" + header.getSubUnit());
095: } else {
096: if (!convert2boolean(customer.getCustomerActive())) {
097: setErrorMessage("Customer not active: "
098: + header.getChart() + "/" + header.getOrg()
099: + "/" + header.getSubUnit());
100: }
101: }
102: }
103:
104: private boolean convert2boolean(Boolean b) {
105: if (b == null) {
106: return false;
107: }
108: return b.booleanValue();
109: }
110:
111: public void setTrailer(XmlTrailer trailer) {
112: this .trailer = trailer;
113:
114: // Only attempt this stuff if there aren't any errors
115: if (errorMessageList.size() == 0) {
116: // If this doesn't match the number of detail segments, add
117: // an error message
118: if (actualPaymentCount != trailer.getPaymentCount()) {
119: setErrorMessage("Detail Count in trailer ("
120: + trailer.getPaymentCount()
121: + ") does not match number of detail segments ("
122: + actualPaymentCount + ")");
123: }
124:
125: // If this doesn't match the amount in the detail segments, add
126: // an error message
127: if (trailer.getPaymentTotalAmount().compareTo(
128: getCalculatedPaymentTotalAmount()) != 0) {
129: setErrorMessage("Detail Total Amount in trailer ("
130: + trailer.getPaymentTotalAmount()
131: + ") does not match total of detail segments ("
132: + getCalculatedPaymentTotalAmount() + ")");
133: } else {
134: // Check to see if this is a duplicate batch
135: Timestamp now = new Timestamp(header.getCreationDate()
136: .getTime());
137:
138: if (loadDao.isDuplicateBatch(customer, new Integer(
139: trailer.getPaymentCount()), trailer
140: .getPaymentTotalAmount(), now)) {
141: LOG
142: .error("setPaymentTotalAmount() duplicate batch uploaded");
143: setErrorMessage("Duplicate Batch");
144: }
145: }
146: }
147: }
148:
149: public int getActualPaymentCount() {
150: return actualPaymentCount;
151: }
152:
153: public BigDecimal getCalculatedPaymentTotalAmount() {
154: return calculatedPaymentTotalAmount;
155: }
156:
157: public void setGroup(XmlGroup item) {
158: BigDecimal groupTotal = new BigDecimal(0);
159:
160: int noteLineCount = 0;
161: actualGroupCount++;
162: for (Iterator iter = item.getDetail().iterator(); iter
163: .hasNext();) {
164: XmlDetail detail = (XmlDetail) iter.next();
165:
166: noteLineCount++; // Add a line to print the invoice number
167: noteLineCount = noteLineCount
168: + detail.getPayment_text().size();
169:
170: // Add up the payment count & total payment amount
171: actualPaymentCount++;
172: calculatedPaymentTotalAmount = calculatedPaymentTotalAmount
173: .add(detail.getAccountTotal());
174:
175: LOG.debug("setGroup() actualPaymentCount: "
176: + actualPaymentCount + " Accounts: "
177: + detail.getAccounting().size());
178:
179: if ((detail.getNet_payment_amt() == null)
180: && (!detail.isDetailAmountProvided())) {
181: // set net amount to accounting segments
182: detail.setNet_payment_amt(detail.getAccountTotal());
183: } else if ((detail.getNet_payment_amt() == null)
184: && (detail.isDetailAmountProvided())) {
185: // set net amount to calculated detail amount
186: detail.setNet_payment_amt(detail
187: .getCalculatedPaymentAmount());
188:
189: // compare net to accounting segments
190: if (detail.getAccountTotal().compareTo(
191: detail.getNet_payment_amt()) != 0) {
192: setErrorMessage("Detail " + actualPaymentCount
193: + " account total "
194: + detail.getAccountTotal()
195: + " is not equal to net payment "
196: + detail.getNet_payment_amt());
197: }
198: } else if ((detail.getNet_payment_amt() != null)
199: && (!detail.isDetailAmountProvided())) {
200: // compare net to accounting segments
201: if (detail.getAccountTotal().compareTo(
202: detail.getNet_payment_amt()) != 0) {
203: setErrorMessage("Detail " + actualPaymentCount
204: + " account total "
205: + detail.getAccountTotal()
206: + " is not equal to net payment "
207: + detail.getNet_payment_amt());
208: }
209: } else {
210: // compare net to accounting segments
211: if (detail.getAccountTotal().compareTo(
212: detail.getNet_payment_amt()) != 0) {
213: setErrorMessage("Detail " + actualPaymentCount
214: + " account total "
215: + detail.getAccountTotal()
216: + " is not equal to net payment "
217: + detail.getNet_payment_amt());
218: }
219:
220: // THIS WAS REMOVED FOR EPIC
221: // The PREQ section was sending original invoice amounts that might or might not work with this validation check
222: // compare calculation to net
223: // if ( detail.getCalculatedPaymentAmount().compareTo(detail.getNet_payment_amt()) != 0 ) {
224: // setErrorMessage("Detail " + actualPaymentCount + " calculated total " + detail.getCalculatedPaymentAmount() + "
225: // is not equal to net payment " + detail.getNet_payment_amt());
226: // }
227: }
228: groupTotal = groupTotal.add(detail.getNet_payment_amt());
229: }
230:
231: if (groupTotal.doubleValue() < 0) {
232: setErrorMessage("Group #" + actualGroupCount
233: + " total is less than 0");
234: }
235:
236: // Now check that the number of detail items and note lines will fit on a check stub
237: if (noteLineCount > maxNoteLines) {
238: setErrorMessage("Group #" + actualGroupCount
239: + " total note lines required is " + noteLineCount
240: + " which is greater than " + maxNoteLines);
241: }
242: }
243:
244: public void setErrorMessage(String message) {
245: errorMessageList.add(message);
246: }
247:
248: public List getErrorMessages() {
249: return errorMessageList;
250: }
251:
252: public void setFilename(String filename) {
253: // Unneeded
254: }
255:
256: public void setUser(PdpUser u) {
257: // Unneeded
258: }
259:
260: public void setPaymentFileLoadDao(PaymentFileLoadDao pfld) {
261: this .loadDao = pfld;
262: }
263:
264: public void setCustomerProfileDao(CustomerProfileDao cpd) {
265: this.customerDao = cpd;
266: }
267: }
|