www.pudn.com > code_source_compiere_erp_crm_logiciel_java.zip > MPaySelectionCheck.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 Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.model;
import java.sql.*;
import java.math.*;
import java.util.*;
import java.text.*;
import java.io.*;
import org.apache.log4j.Logger;
import org.compiere.model.*;
import org.compiere.util.*;
/**
* Payment Print/Export model.
*
* @author Jorg Janke
* @version $Id: MPaySelectionCheck.java,v 1.9 2003/04/22 05:32:19 jjanke Exp $
*/
public final class MPaySelectionCheck extends PO
implements Serializable
{
/**
* Constructor
* @param ctx context
* @param C_PaySelectionCheck_ID C_PaySelectionCheck_ID
*/
public MPaySelectionCheck (Properties ctx, int C_PaySelectionCheck_ID)
{
super(ctx, C_PaySelectionCheck_ID);
} // MPaySelectionCheck
/**
* Constructor
* @param ctx context
* @param rs result set
*/
public MPaySelectionCheck(Properties ctx, ResultSet rs)
{
super(ctx, rs);
} // MPaySelectionCheck
static private Logger s_log = Logger.getLogger (MPaySelectionCheck.class);
/**
* Initialize and return PO_Info
* @param ctx context
* @return POInfo
*/
protected POInfo initPO (Properties ctx)
{
int AD_Table_ID = 525;
POInfo poi = POInfo.getPOInfo (ctx, AD_Table_ID);
return poi;
} // initPO
public static final String PAYRULE_CASH = "B";
public static final String PAYRULE_CREDITCARD = "K";
public static final String PAYRULE_CHECK = "S";
public static final String PAYRULE_ACH = "T";
/*************************************************************************/
/**
* Pay Selection
* @return C_PaySelection_ID
*/
public int getC_PaySelection_ID()
{
Integer ii = (Integer)getValue("C_PaySelection_ID");
if (ii == null)
return 0;
return ii.intValue();
}
/**
* Business Partner
* @return C_BPartner_ID or 0
*/
public int getC_BPartner_ID()
{
Integer ii = (Integer)getValue("C_BPartner_ID");
if (ii == null)
return 0;
return ii.intValue();
} // getC_BPartner_ID
/**
* Payment Amount
* @return PayAmt or 0
*/
public BigDecimal getPayAmt()
{
BigDecimal bd = (BigDecimal)getValue("PayAmt");
if (bd == null)
return Env.ZERO;
return bd;
} // getPayAmt
/**
* Number of Invoices
* @return Qty or 0
*/
public int getQty()
{
Integer ii = (Integer)getValue("Qty");
if (ii == null)
return 0;
return ii.intValue();
} // getQty
/**
* Payment Rule
* @return PaymentRule
*/
public String getPaymentRule()
{
return (String)getValue("PaymentRule");
} // getPaymentRule
/**
* Payment Document No
* @return DocumentNo
*/
public String getDocumentNo()
{
return (String)getValue("DocumentNo");
} // getDocumentNo
/**
* Payment Document No
* @param DocumentNo Document No
*/
public void setDocumentNo (String DocumentNo)
{
setValueNoCheck("DocumentNo", DocumentNo);
} // setDocumentNo
/**
* Payment
* @return C_Payment_ID or 0
*/
public int getC_Payment_ID()
{
Integer ii = (Integer)getValue("C_Payment_ID");
if (ii == null)
return 0;
return ii.intValue();
} // getC_Payment_ID
/**
* Payment
* @param C_Payment_ID payment
*/
public void setC_Payment_ID (int C_Payment_ID)
{
setValueNoCheck("C_Payment_ID", new Integer (C_Payment_ID));
} // setC_Payment_ID
/**
* Is Printed
* @return printed
*/
public boolean isPrinted()
{
Boolean bb = (Boolean)getValue("IsPrinted");
if (bb == null)
return false;
return bb.booleanValue();
} // isPrinted
/**
* Set Printed
* @param printed printed
*/
public void setPrinted (boolean printed)
{
setValueNoCheck("IsPrinted", new Boolean(printed));
} // setPrinted
/*************************************************************************/
/**
* Bank Account
* @return C_BankAccount_ID
*/
public int getC_BankAccount_ID()
{
if (m_C_BankAccount_ID == -1)
setPaySelectionInfo();
return m_C_BankAccount_ID;
} // getC_BankAccount_ID
/**
* Payment Date
* @return PayDate
*/
public Timestamp getPayDate()
{
if (m_PayDate == null)
setPaySelectionInfo();
return m_PayDate;
} // getPayDate
/**
* Payment Currency
* @return C_Currency_ID
*/
public int getC_Currency_ID()
{
if (m_C_Currency_ID == -1)
setPaySelectionInfo();
return m_C_Currency_ID;
} // getC_Currency_ID
/**
* String Representation
* @return info
*/
public String toString()
{
StringBuffer sb = new StringBuffer("MPaymentCheck[");
sb.append(getID()).append("-").append(getDocumentNo())
.append("-").append(getPayAmt()).append("-").append(getPayDate())
.append(",PaymetRule=").append(getPaymentRule())
.append(",Qty=").append(getQty())
.append("]");
return sb.toString();
} // toString
private int m_C_BankAccount_ID = -1;
private int m_C_Currency_ID = -1;
private Timestamp m_PayDate = null;
/**
* Get PaySelection Info
*/
private void setPaySelectionInfo()
{
String sql = "SELECT ps.C_BankAccount_ID, ps.PayDate, ba.C_Currency_ID "
+ "FROM C_PaySelection ps"
+ " INNER JOIN C_BankAccount ba ON (ps.C_BankAccount_ID=ba.C_BankAccount_ID) "
+ "WHERE ps.C_PaySelection_ID=?";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, getC_PaySelection_ID());
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
m_C_BankAccount_ID = rs.getInt(1);
m_PayDate = rs.getTimestamp(2);
m_C_Currency_ID = rs.getInt(3);
}
else
log.error("setPaySelectionInfo - Not found for ID=" + getC_PaySelection_ID());
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.error("setPaySelectionInfo", e);
}
} // setPaySelectionInfo
/*************************************************************************/
/**
* Get Invoice IDs
* @return array of C_Invoice_ID
*/
protected PaySelectionLine[] getPaySelectionLines ()
{
ArrayList list = new ArrayList();
String sql = "SELECT psl.C_Invoice_ID, psl.PayAmt, psl.DifferenceAmt,"
+ " i.DocumentNo, i.GrandTotal, i.C_Currency_ID, i.C_BPartner_ID "
+ "FROM C_PaySelectionLine psl"
+ " INNER JOIN C_Invoice i ON (psl.C_Invoice_ID=i.C_Invoice_ID) "
+ "WHERE C_PaySelectionCheck_ID=?";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql);
pstmt.setInt(1, getID());
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
int C_Invoice_ID = rs.getInt(1);
BigDecimal PayAmt = rs.getBigDecimal(2);
BigDecimal DifferenceAmt = rs.getBigDecimal(3);
String DocumentNo = rs.getString(4);
BigDecimal GrandTotal = rs.getBigDecimal(5);
int C_Currency_ID = rs.getInt(6);
int C_BPartner_ID = rs.getInt(7);
//
PaySelectionLine psl = new PaySelectionLine(C_Invoice_ID, DocumentNo,
GrandTotal, PayAmt, DifferenceAmt, C_Currency_ID, C_BPartner_ID);
list.add(psl);
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.error("getPaySelectionLines", e);
}
//
PaySelectionLine[] retValue = new PaySelectionLine[list.size()];
list.toArray(retValue);
return retValue;
} // getInvoiceInfo
/**
* Payment Selection Line Info (Value Object)
*/
class PaySelectionLine
{
public PaySelectionLine (int C_Invoice_ID, String DocumentNo,
BigDecimal GrandTotal, BigDecimal PayAmt, BigDecimal DifferenceAmt,
int C_Currency_ID, int C_BPartner_ID)
{
this.C_Invoice_ID = C_Invoice_ID;
this.DocumentNo = DocumentNo;
if (GrandTotal != null)
this.GrandTotal = GrandTotal;
if (PayAmt != null)
this.PayAmt = PayAmt;
if (DifferenceAmt != null)
this.DifferenceAmt = DifferenceAmt;
this.C_Currency_ID = C_Currency_ID;
this.C_BPartner_ID = C_BPartner_ID;
}
public int C_Invoice_ID;
public String DocumentNo;
public BigDecimal GrandTotal = Env.ZERO;
public BigDecimal PayAmt = Env.ZERO;
public BigDecimal DifferenceAmt = Env.ZERO;
public int C_Currency_ID;
public int C_BPartner_ID;
/**
* String Representation
* @return info
*/
public String toString()
{
StringBuffer sb = new StringBuffer("PaySelectionLine[");
sb.append(DocumentNo).append(",GrandTotal=").append(GrandTotal)
.append(",Difference=").append(DifferenceAmt)
.append("]");
return sb.toString();
} // toString
} // PaySelectionLine
/*************************************************************************/
/**
* Get Checks of Payment Selection
*
* @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