📄 mpayment.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Smart Business Solution. The Initial
* Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
* are Copyright (C) 1999-2005 Jorg Janke.
* All parts are Copyright (C) 1999-2005 ComPiere, Inc. All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.model;
import java.io.*;
import java.math.*;
import java.rmi.*;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
import org.compiere.db.*;
import org.compiere.interfaces.*;
import org.compiere.process.*;
import org.compiere.util.*;
/**
* Payment Model.
* - retrieve and create payments for invoice
* <pre>
* Event chain
* - Payment inserted
* C_Payment_Trg fires
* update DocumentNo with payment summary
* - Payment posted (C_Payment_Post)
* create allocation line
* C_Allocation_Trg fires
* Update C_BPartner Open Item Amount
* update invoice (IsPaid)
* link invoice-payment if batch
*
* Lifeline:
* - Created by VPayment or directly
* - When changed in VPayment
* - old payment is reversed
* - new payment created
*
* When Payment is posed, the Allocation is made
* </pre>
* @author Jorg Janke
* @version $Id: MPayment.java,v 1.88 2006/01/09 19:38:03 jjanke Exp $
*/
public final class MPayment extends X_C_Payment
implements DocAction, ProcessCall
{
/**
* Get Payments Of BPartner
* @param ctx context
* @param C_BPartner_ID id
* @return array
*/
public static MPayment[] getOfBPartner (Properties ctx, int C_BPartner_ID, String trxName)
{
ArrayList<MPayment> list = new ArrayList<MPayment>();
String sql = "SELECT * FROM C_Payment WHERE C_BPartner_ID=?";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement(sql, trxName);
pstmt.setInt(1, C_BPartner_ID);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
list.add(new MPayment(ctx,rs, trxName));
rs.close();
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
s_log.log(Level.SEVERE, sql, e);
}
try
{
if (pstmt != null)
pstmt.close();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
//
MPayment[] retValue = new MPayment[list.size()];
list.toArray(retValue);
return retValue;
} // getOfBPartner
/**************************************************************************
* Default Constructor
* @param ctx context
* @param C_Payment_ID payment to load, (0 create new payment)
* @param trxName trx name
*/
public MPayment (Properties ctx, int C_Payment_ID, String trxName)
{
super (ctx, C_Payment_ID, trxName);
// New
if (C_Payment_ID == 0)
{
setDocAction(DOCACTION_Complete);
setDocStatus(DOCSTATUS_Drafted);
setTrxType(TRXTYPE_Sales);
//
setR_AvsAddr (R_AVSZIP_Unavailable);
setR_AvsZip (R_AVSZIP_Unavailable);
//
setIsReceipt (true);
setIsApproved (false);
setIsReconciled (false);
setIsAllocated(false);
setIsOnline (false);
setIsSelfService(false);
setIsDelayedCapture (false);
setIsPrepayment(false);
setProcessed(false);
setProcessing(false);
setPosted (false);
//
setPayAmt(Env.ZERO);
setDiscountAmt(Env.ZERO);
setTaxAmt(Env.ZERO);
setWriteOffAmt(Env.ZERO);
setIsOverUnderPayment (false);
setOverUnderAmt(Env.ZERO);
//
setDateTrx (new Timestamp(System.currentTimeMillis()));
setDateAcct (getDateTrx());
setTenderType(TENDERTYPE_Check);
}
} // MPayment
/**
* Load Constructor
* @param ctx context
* @param rs result set record
*/
public MPayment (Properties ctx, ResultSet rs, String trxName)
{
super(ctx, rs, trxName);
} // MPayment
/** Temporary Payment Processors */
private MPaymentProcessor[] m_mPaymentProcessors = null;
/** Temporary Payment Processor */
private MPaymentProcessor m_mPaymentProcessor = null;
/** Logger */
private static CLogger s_log = CLogger.getCLogger (MPayment.class);
/** Error Message */
private String m_errorMessage = null;
/** Reversal Indicator */
public static String REVERSE_INDICATOR = "^";
/**
* Reset Payment to new status
*/
public void resetNew()
{
setC_Payment_ID(0); // forces new Record
set_ValueNoCheck ("DocumentNo", null);
setDocAction(DOCACTION_Prepare);
setDocStatus(DOCSTATUS_Drafted);
setProcessed(false);
setPosted (false);
setIsReconciled (false);
setIsAllocated(false);
setIsOnline(false);
setIsDelayedCapture (false);
// setC_BPartner_ID(0);
setC_Invoice_ID(0);
setC_Order_ID(0);
setC_Charge_ID(0);
setC_Project_ID(0);
setIsPrepayment(false);
} // resetNew
/**
* Is Cashbook Transfer Trx
* @return true if Cash Trx
*/
public boolean isCashTrx()
{
return "X".equals(getTenderType());
} // isCashTrx
/**************************************************************************
* Set Credit Card.
* Need to set PatmentProcessor after Amount/Currency Set
*
* @param TrxType Transaction Type see TRX_
* @param creditCardType CC type
* @param creditCardNumber CC number
* @param creditCardVV CC verification
* @param creditCardExpMM CC Exp MM
* @param creditCardExpYY CC Exp YY
* @return true if valid
*/
public boolean setCreditCard (String TrxType, String creditCardType, String creditCardNumber,
String creditCardVV, int creditCardExpMM, int creditCardExpYY)
{
setTenderType(TENDERTYPE_CreditCard);
setTrxType(TrxType);
//
setCreditCardType (creditCardType);
setCreditCardNumber (creditCardNumber);
setCreditCardVV (creditCardVV);
setCreditCardExpMM (creditCardExpMM);
setCreditCardExpYY (creditCardExpYY);
//
int check = MPaymentValidate.validateCreditCardNumber(creditCardNumber, creditCardType).length()
+ MPaymentValidate.validateCreditCardExp(creditCardExpMM, creditCardExpYY).length();
if (creditCardVV.length() > 0)
check += MPaymentValidate.validateCreditCardVV(creditCardVV, creditCardType).length();
return check == 0;
} // setCreditCard
/**
* Set Credit Card - Exp.
* Need to set PatmentProcessor after Amount/Currency Set
*
* @param TrxType Transaction Type see TRX_
* @param creditCardType CC type
* @param creditCardNumber CC number
* @param creditCardVV CC verification
* @param creditCardExp CC Exp
* @return true if valid
*/
public boolean setCreditCard (String TrxType, String creditCardType, String creditCardNumber,
String creditCardVV, String creditCardExp)
{
return setCreditCard(TrxType, creditCardType, creditCardNumber,
creditCardVV, MPaymentValidate.getCreditCardExpMM(creditCardExp),
MPaymentValidate.getCreditCardExpYY(creditCardExp));
} // setCreditCard
/**
* Set ACH BankAccount Info
*
* @param C_BankAccount_ID bank account
* @param isReceipt true if receipt
* @return true if valid
*/
public boolean setBankACH (int C_BankAccount_ID, boolean isReceipt)
{
setBankAccountDetails(C_BankAccount_ID);
setIsReceipt (isReceipt);
//
int check = MPaymentValidate.validateRoutingNo(getRoutingNo()).length()
+ MPaymentValidate.validateAccountNo(getAccountNo()).length();
return check == 0;
} // setBankACH
/**
* Set ACH BankAccount Info
*
* @param C_BankAccount_ID bank account
* @param isReceipt true if receipt
* @param tenderType - Direct Debit or Direct Deposit
* @param routingNo routing
* @param accountNo account
* @return true if valid
*/
public boolean setBankACH (int C_BankAccount_ID, boolean isReceipt, String tenderType,
String routingNo, String accountNo)
{
setTenderType (tenderType);
setIsReceipt (isReceipt);
//
if (C_BankAccount_ID > 0
&& (routingNo == null || routingNo.length() == 0 || accountNo == null || accountNo.length() == 0))
setBankAccountDetails(C_BankAccount_ID);
else
{
setC_BankAccount_ID(C_BankAccount_ID);
setRoutingNo (routingNo);
setAccountNo (accountNo);
}
setCheckNo ("");
//
int check = MPaymentValidate.validateRoutingNo(routingNo).length()
+ MPaymentValidate.validateAccountNo(accountNo).length();
return check == 0;
} // setBankACH
/**
* Set Check BankAccount Info
*
* @param C_BankAccount_ID bank account
* @param isReceipt true if receipt
* @param checkNo chack no
* @return true if valid
*/
public boolean setBankCheck (int C_BankAccount_ID, boolean isReceipt, String checkNo)
{
return setBankCheck (C_BankAccount_ID, isReceipt, null, null, checkNo);
} // setBankCheck
/**
* Set Check BankAccount Info
*
* @param C_BankAccount_ID bank account
* @param isReceipt true if receipt
* @param routingNo routing no
* @param accountNo account no
* @param checkNo chack no
* @return true if valid
*/
public boolean setBankCheck (int C_BankAccount_ID, boolean isReceipt,
String routingNo, String accountNo, String checkNo)
{
setTenderType (TENDERTYPE_Check);
setIsReceipt (isReceipt);
//
if (C_BankAccount_ID > 0
&& (routingNo == null || routingNo.length() == 0
|| accountNo == null || accountNo.length() == 0))
setBankAccountDetails(C_BankAccount_ID);
else
{
setC_BankAccount_ID(C_BankAccount_ID);
setRoutingNo (routingNo);
setAccountNo (accountNo);
}
setCheckNo (checkNo);
//
int check = MPaymentValidate.validateRoutingNo(routingNo).length()
+ MPaymentValidate.validateAccountNo(accountNo).length()
+ MPaymentValidate.validateCheckNo(checkNo).length();
return check == 0; // no error message
} // setBankCheck
/**
* Set Bank Account Details.
* Look up Routing No & Bank Acct No
* @param C_BankAccount_ID bank account
*/
public void setBankAccountDetails (int C_BankAccount_ID)
{
if (C_BankAccount_ID == 0)
return;
setC_BankAccount_ID(C_BankAccount_ID);
//
String sql = "SELECT b.RoutingNo, ba.AccountNo "
+ "FROM C_BankAccount ba"
+ " INNER JOIN C_Bank b ON (ba.C_Bank_ID=b.C_Bank_ID) "
+ "WHERE C_BankAccount_ID=?";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, get_TrxName());
pstmt.setInt(1, C_BankAccount_ID);
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
setRoutingNo (rs.getString(1));
setAccountNo (rs.getString(2));
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.log(Level.SEVERE, "setsetBankAccountDetails", e);
}
} // setBankAccountDetails
/**
* Set Account Address
*
* @param name name
* @param street street
* @param city city
* @param state state
* @param zip zip
* @param country country
*/
public void setAccountAddress (String name, String street,
String city, String state, String zip, String country)
{
setA_Name (name);
setA_Street (street);
setA_City (city);
setA_State (state);
setA_Zip (zip);
setA_Country(country);
} // setAccountAddress
/**************************************************************************
* Process Payment
* @return true if approved
*/
public boolean processOnline()
{
log.info ("Amt=" + getPayAmt());
//
setIsOnline(true);
setErrorMessage(null);
// prevent charging twice
if (isApproved())
{
log.info("Already processed - " + getR_Result() + " - " + getR_RespMsg());
setErrorMessage("Payment already Processed");
return true;
}
if (m_mPaymentProcessor == null)
setPaymentProcessor();
if (m_mPaymentProcessor == null)
{
log.log(Level.SEVERE, "No Payment Processor Model");
setErrorMessage("No Payment Processor Model");
return false;
}
boolean approved = false;
/** Process Payment on Server */
if (DB.isRemoteObjects())
{
Server server = CConnection.get().getServer();
try
{
if (server != null)
{ // See ServerBean
String trxName = null; // unconditionally save
save(trxName); // server reads from disk
approved = server.paymentOnline (getCtx(), getC_Payment_ID(),
m_mPaymentProcessor.getC_PaymentProcessor_ID(), trxName);
if (CLogMgt.isLevelFinest())
s_log.fine("processOnline - server => " + approved);
load(trxName); // server saves to disk
setIsApproved(approved);
return approved;
}
log.log(Level.SEVERE, "processOnline - AppsServer not found");
}
catch (RemoteException ex)
{
log.log(Level.SEVERE, "processOnline - AppsServer error", ex);
}
}
/** **/
// Try locally
try
{
PaymentProcessor pp = PaymentProcessor.create(m_mPaymentProcessor, this);
if (pp == null)
setErrorMessage("No Payment Processor");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -