📄 mpayselectioncheck.java
字号:
* @param C_PaySelection_ID Payment Selection
* @param PaymentRule Payment Rule
* @param startDocumentNo start document no
* @return array of checks
*/
static public MPaySelectionCheck[] get (int C_PaySelection_ID,
String PaymentRule, int startDocumentNo)
{
s_log.debug("get - C_PaySelection_ID=" + C_PaySelection_ID
+ ", PaymentRule=" + PaymentRule + ", startDocumentNo=" + startDocumentNo);
ArrayList list = new ArrayList();
int docNo = startDocumentNo;
String sql = "SELECT * FROM C_PaySelectionCheck "
+ "WHERE C_PaySelection_ID=? AND PaymentRule=?";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, C_PaySelection_ID);
pstmt.setString(2, PaymentRule);
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
MPaySelectionCheck c = new MPaySelectionCheck (Env.getCtx(), rs);
c.setDocumentNo(String.valueOf(docNo++)); // set Check Document No
list.add(c);
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
s_log.error("get", e);
}
// convert to Array
MPaySelectionCheck[] retValue = new MPaySelectionCheck[list.size()];
list.toArray(retValue);
return retValue;
} // createPayments
/*************************************************************************/
/**
* Export to File
* @param checks array of checks
* @param file file to export checks
* @return number of lines
*/
public static int exportToFile (MPaySelectionCheck[] checks, File file)
{
if (checks == null || checks.length == 0)
return 0;
// Must be a file
if (file.isDirectory())
{
s_log.error("exportToFile - file is directory - " + file.getAbsolutePath());
return 0;
}
// delete if exists
try
{
if (file.exists())
file.delete();
}
catch (Exception e)
{
s_log.error("exportToFile - could not delete - " + file.getAbsolutePath(), e);
}
char x = '"'; // ease
int noLines = 0;
StringBuffer line = null;
try
{
FileWriter fw = new FileWriter(file);
// write header
line = new StringBuffer();
line.append(x).append("Value").append(x).append(",")
.append(x).append("Name").append(x).append(",")
.append(x).append("Contact").append(x).append(",")
.append(x).append("Addr1").append(x).append(",")
.append(x).append("Addr2").append(x).append(",")
.append(x).append("City").append(x).append(",")
.append(x).append("State").append(x).append(",")
.append(x).append("ZIP").append(x).append(",")
.append(x).append("Country").append(x).append(",")
.append(x).append("ReferenceNo").append(x).append(",")
.append(x).append("DocumentNo").append(x).append(",")
.append(x).append("PayDate").append(x).append(",")
.append(x).append("Currency").append(x).append(",")
.append(x).append("PayAmount").append(x).append(",")
.append(x).append("Comment").append(x)
.append(Env.NL);
fw.write(line.toString());
noLines++;
// write lines
for (int i = 0; i < checks.length; i++)
{
MPaySelectionCheck mpp = checks[i];
if (mpp == null)
continue;
// BPartner Info
String bp[] = getBPartnerInfo(mpp.getC_BPartner_ID());
// Comment - list of invoice document no
StringBuffer comment = new StringBuffer();
PaySelectionLine[] psls = mpp.getPaySelectionLines();
for (int l = 0; l < psls.length; l++)
{
if (l > 0)
comment.append("_");
comment.append(psls[l].DocumentNo);
}
line = new StringBuffer();
line.append(x).append(bp[BP_VALUE]).append(x).append(",") // Value
.append(x).append(bp[BP_NAME]).append(x).append(",") // Name
.append(x).append(bp[BP_CONTACT]).append(x).append(",") // Contact
.append(x).append(bp[BP_ADDR1]).append(x).append(",") // Addr1
.append(x).append(bp[BP_ADDR2]).append(x).append(",") // Addr2
.append(x).append(bp[BP_CITY]).append(x).append(",") // City
.append(x).append(bp[BP_REGION]).append(x).append(",") // State
.append(x).append(bp[BP_POSTAL]).append(x).append(",") // ZIP
.append(x).append(bp[BP_COUNTRY]).append(x).append(",") // Country
.append(x).append(bp[BP_REFNO]).append(x).append(",") // ReferenceNo
// Payment Info
.append(x).append(mpp.getDocumentNo()).append(x).append(",") // DocumentNo
.append(mpp.getPayDate()).append(",") // PayDate
.append(x).append(getCurrencyInfo(mpp.getC_Currency_ID())).append(x).append(",") // Currency
.append(mpp.getPayAmt()).append(",") // PayAmount
.append(x).append(comment.toString()).append(x) // Comment
.append(Env.NL);
fw.write(line.toString());
noLines++;
} // write line
fw.flush();
fw.close();
}
catch (Exception e)
{
s_log.error("exportToFile", e);
}
return noLines;
} // exportToFile
/** Currency Cache */
private static HashMap s_currencies = new HashMap();
/**
* Get Currency Description
* @param C_Currency_ID currency
* @return currency
*/
public static String getCurrencyInfo(int C_Currency_ID)
{
Integer key = new Integer(C_Currency_ID);
String retValue = (String)s_currencies.get(key);
if (retValue != null)
return retValue;
// Read it
String sql = "SELECT ISO_Code FROM C_Currency WHERE C_Currency_ID=?";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, C_Currency_ID);
ResultSet rs = pstmt.executeQuery();
//
if (rs.next())
retValue = rs.getString(1);
rs.close();
pstmt.close();
}
catch (SQLException e)
{
s_log.error("MPaySelectionCheck.getCurrency", e);
}
// Currency not found
if (retValue == null)
{
retValue = "<" + C_Currency_ID + ">";
s_log.error("getCurrency - NOT found ID=" + C_Currency_ID);
}
s_currencies.put(key, retValue);
return retValue;
} // getCurrencyInfo
/** BPartner Info Index for Value */
private static final int BP_VALUE = 0;
/** BPartner Info Index for Name */
private static final int BP_NAME = 1;
/** BPartner Info Index for Contact Name */
private static final int BP_CONTACT = 2;
/** BPartner Info Index for Address 1 */
private static final int BP_ADDR1 = 3;
/** BPartner Info Index for Address 2 */
private static final int BP_ADDR2 = 4;
/** BPartner Info Index for City */
private static final int BP_CITY = 5;
/** BPartner Info Index for Region */
private static final int BP_REGION = 6;
/** BPartner Info Index for Postal Code */
private static final int BP_POSTAL = 7;
/** BPartner Info Index for Country */
private static final int BP_COUNTRY = 8;
/** BPartner Info Index for Reference No */
private static final int BP_REFNO = 9;
/**
* Get Customer/Vendor Info.
* Based on BP_ static variables
* @param C_BPartner_ID BPartner
* @return info array
*/
private static String[] getBPartnerInfo (int C_BPartner_ID)
{
String[] bp = new String[10];
String sql = "SELECT bp.Value, bp.Name, c.Name AS Contact, "
+ "a.Address1, a.Address2, a.City, r.Name AS Region, a.Postal, "
+ "cc.Name AS Country, bp.ReferenceNo "
+ "FROM C_BPartner bp, C_BPartner_Contact c, C_BPartner_Location l, C_Location a, C_Region r, C_Country cc "
+ "WHERE bp.C_BPartner_ID=?" // #1
+ " AND bp.C_BPartner_ID=c.C_BPartner_ID(+)"
+ " AND bp.C_BPartner_ID=l.C_BPartner_ID"
+ " AND l.C_Location_ID=a.C_Location_ID"
+ " AND a.C_Region_ID=r.C_Region_ID(+)"
+ " AND a.C_Country_ID=cc.C_Country_ID "
+ "ORDER BY l.IsBillTo DESC";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, C_BPartner_ID);
ResultSet rs = pstmt.executeQuery();
//
if (rs.next())
{
bp[BP_VALUE] = rs.getString(1);
if (bp[BP_VALUE] == null)
bp[BP_VALUE] = "";
bp[BP_NAME] = rs.getString(2);
if (bp[BP_NAME] == null)
bp[BP_NAME] = "";
bp[BP_CONTACT] = rs.getString(3);
if (bp[BP_CONTACT] == null)
bp[BP_CONTACT] = "";
bp[BP_ADDR1] = rs.getString(4);
if (bp[BP_ADDR1] == null)
bp[BP_ADDR1] = "";
bp[BP_ADDR2] = rs.getString(5);
if (bp[BP_ADDR2] == null)
bp[BP_ADDR2] = "";
bp[BP_CITY] = rs.getString(6);
if (bp[BP_CITY] == null)
bp[BP_CITY] = "";
bp[BP_REGION] = rs.getString(7);
if (bp[BP_REGION] == null)
bp[BP_REGION] = "";
bp[BP_POSTAL] = rs.getString(8);
if (bp[BP_POSTAL] == null)
bp[BP_POSTAL] = "";
bp[BP_COUNTRY] = rs.getString(9);
if (bp[BP_COUNTRY] == null)
bp[BP_COUNTRY] = "";
bp[BP_REFNO] = rs.getString(10);
if (bp[BP_REFNO] == null)
bp[BP_REFNO] = "";
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
s_log.error("getBPartnerInfo", e);
}
return bp;
} // getBPartnerInfo
/*************************************************************************/
/**
* Confirm Print
* @param checks checks
* @return last Document number or 0 if nothing printed
*/
public static int confirmPrint (MPaySelectionCheck[] checks)
{
int lastDocumentNo = 0;
for (int i = 0; i < checks.length; i++)
{
MPaySelectionCheck check = checks[i];
MPayment payment = new MPayment(check.getCtx(), check.getC_Payment_ID());
if (check.getC_Payment_ID() != 0)
{
// save dcheck (new doc no)
check.save();
// Update check number
payment.setBankCheckNo(check.getDocumentNo());
payment.save();
}
else // New Payment
{
payment = new MPayment(check.getCtx(), 0);
//
if (check.getPaymentRule().equals(PAYRULE_CHECK))
payment.setBankCheck (check.getC_BankAccount_ID(), false, check.getDocumentNo());
else if (check.getPaymentRule().equals(PAYRULE_CREDITCARD))
payment.setTenderType(MPayment.TENDER_CREDITCARD);
else if (check.getPaymentRule().equals(PAYRULE_ACH))
payment.setBankACH(check.getC_BankAccount_ID(), false);
else
{
s_log.error("confirmPrint - Unsupported Payment Rule=" + check.getPaymentRule());
continue;
}
payment.setTrxType(MPayment.TRX_CREDIT);
payment.setAmount(check.getC_Currency_ID(), check.getPayAmt());
payment.setDateTrx(check.getPayDate());
payment.setDocumentNo();
payment.setC_BPartner_ID(check.getC_BPartner_ID());
// Link to Invoice
PaySelectionLine[] psls = check.getPaySelectionLines();
s_log.debug("confirmPrint - " + check + " (#SelectionLines=" + psls.length + ")");
if (check.getQty() == 1 && psls != null && psls.length == 1)
{
PaySelectionLine psl = psls[0];
s_log.debug("confirmPrint - map to Invoice " + psl);
//
payment.setC_Invoice_ID (psl.C_Invoice_ID);
payment.setDiscountAmt (psl.DifferenceAmt);
}
else
payment.setDiscountAmt(Env.ZERO);
payment.setWriteOffAmt(Env.ZERO);
payment.save();
//
int C_Payment_ID = payment.getID();
if (C_Payment_ID < 1)
s_log.error("confirmPrint - Payment not created=" + check);
else
{
check.setC_Payment_ID (C_Payment_ID);
check.setPrinted(true);
check.save ();
payment.post();
}
} // new Payment
// Get Check Document No
try
{
int no = Integer.parseInt(check.getDocumentNo());
if (lastDocumentNo < no)
lastDocumentNo = no;
}
catch (NumberFormatException ex)
{
s_log.error("confirmPrint - DocumentNo=" + check.getDocumentNo(), ex);
}
} // all checks
s_log.debug("confirmPrint - Last Document No = " + lastDocumentNo);
return lastDocumentNo;
} // confirmPrint
} // MPaySelectionCheck
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -