package com.gpc.tams.eod.task; // Java Imports import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Hashtable; import org.apache.log4j.Logger; import com.gpc.backofficecommon.constants.schema.Sch_ARPayment; import com.gpc.backofficecommon.constants.schema.Sch_ARTransaction; import com.gpc.backofficecommon.constants.schema.Sch_CustomerAR; import com.gpc.backofficecommon.constants.schema.Sch_Tables; import com.gpc.common.ApplicationContext; import com.gpc.common.Profile; import com.gpc.common.TsoConstant; import com.gpc.common.multistore.MSHelper; import com.gpc.server.paymentcredit.PaymentCredit; import com.gpc.server.util.JDBCUtil; import com.gpc.server.util.ServerUtil; import com.gpc.tams.eod.shared.EndOfDay; import com.gpc.valueobjects.endofday.EndOfDayMonitorVO; import com.gpc.valueobjects.profile.ARProfileVO; /** *

Title: ApplyPayments

*

Description: CLASS DESCRIPTION GOES HERE

* @author Copyright © 1999-2003, Genuine Parts Company, All Rights Reserved */ public class ApplyPayments implements EndOfDay { private static final Logger logger = Logger.getLogger(ApplyPayments.class); //Bug #30701: Declared a new global variable �APPLYPAYMENTS� to store the Program Source name. private final String APPLYPAYMENTS = "ApplyPayments"; public ApplyPayments() { } public java.util.Hashtable startEndOfDay(java.util.Hashtable params) { logger.debug("startEndOfDay()"); Hashtable returnValue = new Hashtable(); returnValue.put(KEY_OUTCOME, OUTCOME_FAILURE); Integer loc = (Integer)params.get(TsoConstant.KEY_LOC); Integer empId = (Integer) params.get(TsoConstant.KEY_EMPLOYEE_ID); String ipAddress = (String) params.get(TsoConstant.KEY_IP_ADDRESS); //Bug #31474, get EOD_STARTED_DATE from params EndOfDayMonitorVO eodMonitorVO = (EndOfDayMonitorVO) params.get(KEY_MONITOR); //Bug #30701: As in the REF_FILE_ACTIVITY_PROGRAM_SOURCE table the program source name is �Apply Payments�, //the code is modified to send the same program source name. Timestamp lastModifiedDate = ServerUtil.getTransactionTimestamp(loc, empId, ipAddress, APPLYPAYMENTS); Integer paymentLoc = null; Integer paymentId = null; Integer creditId = null; Integer creditLoc = null; Integer customerId = null; Integer customerLoc = null; String applyResult = null; int failureCount = 0; try { if(shouldApplyPayments(loc)) { ArrayList payments = getPayments(loc); ArrayList credits = getCredits(loc); Timestamp eodStartedDate = new Timestamp((eodMonitorVO.getEODStartedDate()).getTime()); if (credits != null) { for (int i = 0; i < credits.size(); i++) { Hashtable credit = (Hashtable) credits.get(i); creditId = (Integer) credit.get("CREDIT_ID"); creditLoc = (Integer) credit.get("CREDIT_LOC"); customerId = (Integer) credit.get("CUSTOMER_ID"); PaymentCredit pc = new PaymentCredit(); pc.setEODStartDate(eodStartedDate); applyResult = pc.applyCreditAuto(creditId, loc, customerId, creditLoc, lastModifiedDate, empId, true,ipAddress,APPLYPAYMENTS); if (applyResult != null && applyResult.equals(TsoConstant.KEY_FAILURE) ) { failureCount ++; } } } if(payments != null) { for (int i = 0; i < payments.size(); i++) { Hashtable payment = (Hashtable) payments.get(i); paymentId = (Integer)payment.get("PAYMENT_ID"); paymentLoc = (Integer)payment.get("PAYMENT_LOC"); customerId = (Integer)payment.get("CUSTOMER_ID"); PaymentCredit pc = new PaymentCredit(); pc.setEODStartDate(eodStartedDate); applyResult = pc.applyPaymentAuto(paymentId, loc, customerId, paymentLoc, lastModifiedDate, empId, true , ipAddress,APPLYPAYMENTS); if (applyResult != null && applyResult.equals(TsoConstant.KEY_FAILURE) ) { failureCount ++; } } } } // Bug No. 30700 the EndOfDay.KEY_UPDATE_LOG is set to false only when the store is a branch store and/or does not prints statements. else { returnValue.put(EndOfDay.KEY_UPDATE_LOG, Boolean.FALSE); } returnValue.put(KEY_OUTCOME, OUTCOME_SUCCESS); } catch (SQLException e) { returnValue.put(KEY_ERROR_TEXT, e.toString()); } finally { logger.debug("returnValue = " + returnValue.toString()); if (failureCount != 0) { logger.warn(failureCount +" numbers of payments or credits were failed to apply!"); } return returnValue; } } // EOD_007 1.10.5: // This process is only executed if the store is printing statements and not a branch store private boolean shouldApplyPayments(Integer loc) { ARProfileVO arProfile = ApplicationContext.getInstance().getProfile(Profile.SERVER, loc.intValue()).getARProfile(); boolean bPrint = arProfile.getPrintStatements().booleanValue(); boolean isMainOrStandalone = !MSHelper.getInstance().isArBranchType(Profile.SERVER, loc.intValue()); return bPrint && isMainOrStandalone; } //Check customers having balance forward account private ArrayList getPayments(Integer loc) throws SQLException{ logger.warn("\n inside getPayments method---->"); Connection conn = null; ResultSet rs = null; Statement stmt = null; try { conn = ServerUtil.getConnection(); stmt = conn.createStatement(); // if CUSTOMER_AR.REF_STATEMENT_TYPE_ID IN (1,11) then it is balance forward account StringBuffer sb = new StringBuffer(); sb.append("SELECT ").append(Sch_Tables.AR_PAYMENT).append('.'); sb.append(Sch_ARPayment.ID).append(" AS PAYMENT_ID, "); sb.append(Sch_Tables.AR_PAYMENT).append('.').append(Sch_ARPayment.LOC); sb.append(" AS PAYMENT_LOC, ").append(Sch_Tables.CUSTOMER_AR).append('.'); sb.append(Sch_CustomerAR.CUSTOMER_ID).append(" AS CUSTOMER_ID "); sb.append("FROM ").append(Sch_Tables.AR_PAYMENT).append(','); sb.append(Sch_Tables.CUSTOMER_AR).append(" WHERE "); sb.append(Sch_Tables.CUSTOMER_AR).append('.').append(Sch_CustomerAR.CUSTOMER_ID); sb.append('=').append(Sch_Tables.AR_PAYMENT).append('.'); sb.append(Sch_CustomerAR.CUSTOMER_ID).append(" AND ").append(Sch_Tables.CUSTOMER_AR); sb.append('.').append(Sch_CustomerAR.LOC).append('=').append(Sch_Tables.AR_PAYMENT); sb.append('.').append(Sch_ARPayment.LOC).append(" AND "); sb.append(Sch_Tables.CUSTOMER_AR).append('.').append(Sch_CustomerAR.LOC); sb.append('=').append(loc).append(" AND ").append(Sch_Tables.CUSTOMER_AR).append('.'); sb.append(Sch_CustomerAR.REF_STATEMENT_TYPE_ID).append(" IN (1, 11) AND "); sb.append(Sch_Tables.AR_PAYMENT).append('.').append(Sch_ARPayment.UNAPPLIED_AMOUNT); sb.append(" > 0 AND ").append(Sch_Tables.AR_PAYMENT).append('.'); sb.append(Sch_ARPayment.HISTORICAL).append(" = 0 "); logger.debug("getPayments SQL string = " + sb.toString()); logger.warn("\n inside getPayments method---->"); logger.warn("\napplication getPayments method"+ "EOD"); /*Integer employeeID= ApplicationContext.getInstance().getProfile(Profile.SERVER, loc).getEndOfDayProfile().getAutostartEmployeeID();*/ //logger.warn("\nuser"+ employeeID); Calendar dtTime = Calendar.getInstance(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); SimpleDateFormat timeFormat = new SimpleDateFormat("HHmmss"); logger.warn("\ndateandtime"+ dateFormat.format(dtTime.getTime())+"-"+timeFormat.format(dtTime.getTime() )); rs = stmt.executeQuery(sb.toString()); ArrayList results = new ArrayList(); while(rs.next()) { Hashtable result = new Hashtable(); result.put("PAYMENT_ID", new Integer(rs.getInt(1))); result.put("PAYMENT_LOC", new Integer(rs.getInt(2))); result.put("CUSTOMER_ID", new Integer(rs.getInt(3))); results.add(result); } return results; } finally { JDBCUtil.closeResultSet(rs); JDBCUtil.closeStatement(stmt); if(conn != null) { ServerUtil.releaseConnection(conn); } } } private ArrayList getCredits(Integer loc) throws SQLException { Connection conn = null; ResultSet rs = null; Statement stmt = null; try { conn = ServerUtil.getConnection(); stmt = conn.createStatement(); StringBuffer sb = new StringBuffer(); sb.append("SELECT ").append(Sch_Tables.AR_TRANSACTION).append('.'); sb.append(Sch_ARTransaction.ID).append(" AS CREDIT_ID,"); sb.append(Sch_Tables.AR_TRANSACTION).append('.').append(Sch_ARTransaction.LOC); sb.append(" AS CREDIT_LOC,").append(Sch_Tables.CUSTOMER_AR).append('.'); sb.append(Sch_CustomerAR.CUSTOMER_ID).append(" AS CUSTOMER_ID "); sb.append("FROM ").append(Sch_Tables.AR_TRANSACTION).append(','); sb.append(Sch_Tables.CUSTOMER_AR); sb.append(" WHERE ").append(Sch_Tables.CUSTOMER_AR); sb.append('.').append(Sch_CustomerAR.CUSTOMER_ID).append('='); sb.append(Sch_Tables.AR_TRANSACTION).append('.').append(Sch_ARTransaction.CUSTOMER_ID); sb.append(" AND ").append(Sch_Tables.CUSTOMER_AR).append('.').append(Sch_CustomerAR.LOC); sb.append('=').append(Sch_Tables.AR_TRANSACTION).append('.'); sb.append(Sch_ARTransaction.LOC).append(" AND ").append(Sch_Tables.CUSTOMER_AR); sb.append('.').append(Sch_CustomerAR.LOC).append('=').append(loc); sb.append(" AND ").append(Sch_Tables.AR_TRANSACTION).append('.'); sb.append(Sch_ARTransaction.GROSS_AMOUNT).append(" < 0 AND "); sb.append(Sch_Tables.AR_TRANSACTION).append('.'); sb.append(Sch_ARTransaction.CURRENT_BALANCE).append(" <> 0"); sb.append(" AND ").append(Sch_Tables.AR_TRANSACTION).append('.'); sb.append(Sch_ARTransaction.HISTORICAL).append(" =0 AND "); sb.append(Sch_Tables.AR_TRANSACTION).append('.'); sb.append(Sch_ARTransaction.REF_AR_TRANSACTION_TYPE_ID).append(" IN (1,2,3,4,5,6,7) "); sb.append(" AND ").append(Sch_Tables.CUSTOMER_AR).append('.').append(Sch_CustomerAR.REF_STATEMENT_TYPE_ID); sb.append(" IN (1,11)"); sb.append(" AND ").append(Sch_Tables.AR_TRANSACTION).append('.'); sb.append(Sch_ARTransaction.NUMBER_OF_STMT_APPEARANCES).append(" > 0"); rs = stmt.executeQuery(sb.toString()); ArrayList results = new ArrayList(); while(rs.next()) { Hashtable result = new Hashtable(); result.put("CREDIT_ID", new Integer(rs.getInt(1))); result.put("CREDIT_LOC", new Integer(rs.getInt(2))); result.put("CUSTOMER_ID", new Integer(rs.getInt(3))); results.add(result); } return results; } finally { JDBCUtil.closeResultSet(rs); JDBCUtil.closeStatement(stmt); if(conn != null) { ServerUtil.releaseConnection(conn); } } } }