📄 allocationauto.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.process;
import java.math.*;
import java.sql.*;
import java.util.*;
import org.compiere.model.*;
import java.util.logging.*;
import org.compiere.util.*;
/**
* Automatic Allocation Process
*
* @author Jorg Janke
* @version $Id: AllocationAuto.java,v 1.12 2005/11/28 03:36:02 jjanke Exp $
*/
public class AllocationAuto extends SvrProcess
{
/** BP Group */
private int p_C_BP_Group_ID = 0;
/** BPartner */
private int p_C_BPartner_ID = 0;
/** Allocate Oldest Setting */
private boolean p_AllocateOldest = true;
/** Only AP/AR Transactions */
private String p_APAR = "A";
private static String ONLY_AP = "P";
private static String ONLY_AR = "R";
/** Payments */
private MPayment[] m_payments = null;
/** Invoices */
private MInvoice[] m_invoices = null;
/** Allocation */
private MAllocationHdr m_allocation = null;
/**
* Prepare - e.g., get Parameters.
*/
protected void prepare()
{
ProcessInfoParameter[] para = getParameter();
for (int i = 0; i < para.length; i++)
{
String name = para[i].getParameterName();
if (para[i].getParameter() == null)
;
else if (name.equals("C_BP_Group_ID"))
p_C_BP_Group_ID = para[i].getParameterAsInt();
else if (name.equals("C_BPartner_ID"))
p_C_BPartner_ID = para[i].getParameterAsInt();
else if (name.equals("AllocateOldest"))
p_AllocateOldest = "Y".equals(para[i].getParameter());
else if (name.equals("APAR"))
p_APAR = (String)para[i].getParameter();
else
log.log(Level.SEVERE, "Unknown Parameter: " + name);
}
} // prepare
/**
* Process
* @return message
* @throws Exception
*/
protected String doIt() throws Exception
{
log.info ("C_BP_Group_ID=" + p_C_BP_Group_ID
+ ", C_BPartner_ID=" + p_C_BPartner_ID + ", Oldest=" + p_AllocateOldest);
int countBP = 0;
int countAlloc = 0;
if (p_C_BPartner_ID != 0)
{
countAlloc = allocateBP (p_C_BPartner_ID);
if (countAlloc > 0)
countBP++;
}
else if (p_C_BP_Group_ID != 0)
{
String sql = "SELECT C_BPartner_ID FROM C_BPartner WHERE C_BP_Group_ID=? ORDER BY Value";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, get_TrxName());
pstmt.setInt (1, p_C_BP_Group_ID);
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
{
int C_BPartner_ID = rs.getInt(1);
int count = allocateBP (C_BPartner_ID);
if (count > 0)
{
countBP++;
countAlloc += count;
commit();
}
}
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
}
else
{
String sql = "SELECT C_BPartner_ID FROM C_BPartner WHERE AD_Client_ID=? ORDER BY Value";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, get_TrxName());
pstmt.setInt (1, Env.getAD_Client_ID(getCtx()));
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
{
int C_BPartner_ID = rs.getInt(1);
int count = allocateBP (C_BPartner_ID);
if (count > 0)
{
countBP++;
countAlloc += count;
commit();
}
}
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
}
//
return "@Created@ #" + countBP + "/" + countAlloc;
} // doIt
/**
* Allocate BP
* @param C_BPartner_ID
* @return number of allocations
*/
private int allocateBP (int C_BPartner_ID) throws Exception
{
getPayments(C_BPartner_ID);
getInvoices(C_BPartner_ID);
log.info ("(1) - C_BPartner_ID=" + C_BPartner_ID
+ " - #Payments=" + m_payments.length + ", #Invoices=" + m_invoices.length);
if (m_payments.length + m_invoices.length < 2)
return 0;
// Payment Info - Invoice or Pay Selection
int count = allocateBPPaymentWithInfo();
if (count != 0)
{
getPayments(C_BPartner_ID); // for next
getInvoices(C_BPartner_ID);
log.info ("(2) - C_BPartner_ID=" + C_BPartner_ID
+ " - #Payments=" + m_payments.length + ", #Invoices=" + m_invoices.length);
if (m_payments.length + m_invoices.length < 2)
return count;
}
// All
int newCount = allocateBPartnerAll();
if (newCount != 0)
{
count += newCount;
getPayments(C_BPartner_ID); // for next
getInvoices(C_BPartner_ID);
processAllocation();
log.info ("(3) - C_BPartner_ID=" + C_BPartner_ID
+ " - #Payments=" + m_payments.length + ", #Invoices=" + m_invoices.length);
if (m_payments.length + m_invoices.length < 2)
return count;
}
// One:One
newCount = allocateBPOneToOne();
if (newCount != 0)
{
count += newCount;
getPayments(C_BPartner_ID); // for next
getInvoices(C_BPartner_ID);
processAllocation();
log.info ("(4) - C_BPartner_ID=" + C_BPartner_ID
+ " - #Payments=" + m_payments.length + ", #Invoices=" + m_invoices.length);
if (m_payments.length + m_invoices.length < 2)
return count;
}
// Oldest First
if (p_AllocateOldest)
{
newCount = allocateBPOldestFirst();
if (newCount != 0)
{
count += newCount;
getPayments(C_BPartner_ID); // for next
getInvoices(C_BPartner_ID);
processAllocation();
log.info ("(5) - C_BPartner_ID=" + C_BPartner_ID
+ " - #Payments=" + m_payments.length + ", #Invoices=" + m_invoices.length);
if (m_payments.length + m_invoices.length < 2)
return count;
}
}
// Other, e.g.
// Allocation if "close" % and $
return count;
} // alloc
/**
* Get Payments of BP
* @param C_BPartner_ID id
* @return unallocated payments
*/
private MPayment[] getPayments (int C_BPartner_ID)
{
ArrayList<MPayment> list = new ArrayList<MPayment>();
String sql = "SELECT * FROM C_Payment "
+ "WHERE IsAllocated='N' AND Processed='Y' AND C_BPartner_ID=?"
+ " AND IsPrepayment='N' AND C_Charge_ID IS NULL ";
if (ONLY_AP.equals(p_APAR))
sql += "AND IsReceipt='N' ";
else if (ONLY_AR.equals(p_APAR))
sql += "AND IsReceipt='Y' ";
sql += "ORDER BY DateTrx";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, get_TrxName());
pstmt.setInt (1, C_BPartner_ID);
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
{
MPayment payment = new MPayment (getCtx(), rs, get_TrxName());
BigDecimal allocated = payment.getAllocatedAmt();
if (allocated != null && allocated.compareTo(payment.getPayAmt()) == 0)
{
payment.setIsAllocated(true);
payment.save();
}
else
list.add (payment);
}
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
log.log(Level.SEVERE, "getPayments", e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
m_payments = new MPayment[list.size ()];
list.toArray (m_payments);
return m_payments;
} // getPayments
/**
* Get Invoices of BP
* @param C_BPartner_ID id
* @return unallocated Invoices
*/
private MInvoice[] getInvoices (int C_BPartner_ID)
{
ArrayList<MInvoice> list = new ArrayList<MInvoice>();
String sql = "SELECT * FROM C_Invoice "
+ "WHERE IsPaid='N' AND Processed='Y' AND C_BPartner_ID=? ";
if (ONLY_AP.equals(p_APAR))
sql += "AND IsSOTrx='N' ";
else if (ONLY_AR.equals(p_APAR))
sql += "AND IsSOTrx='Y' ";
sql += "ORDER BY DateInvoiced";;
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, get_TrxName());
pstmt.setInt (1, C_BPartner_ID);
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
{
MInvoice invoice = new MInvoice (getCtx(), rs, get_TrxName());
if (invoice.getOpenAmt(false, null).signum() == 0)
{
invoice.setIsPaid(true);
invoice.save();
}
else
list.add (invoice);
}
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
log.log(Level.SEVERE, "getInvoicess", e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
m_invoices = new MInvoice[list.size ()];
list.toArray (m_invoices);
return m_invoices;
} // getInvoices
/**************************************************************************
* Allocate Individual Payments with payment references
* @return number of allocations
*/
private int allocateBPPaymentWithInfo ()
{
int count = 0;
//**** See if there is a direct link (Invoice or Pay Selection)
for (int p = 0; p < m_payments.length; p++)
{
MPayment payment = m_payments[p];
if (payment.isAllocated())
continue;
BigDecimal allocatedAmt = payment.getAllocatedAmt();
log.info("allocatePaymentWithInfo - " + payment + ", Allocated=" + allocatedAmt);
if (allocatedAmt != null && allocatedAmt.signum() != 0)
continue;
BigDecimal availableAmt = payment.getPayAmt()
.add(payment.getDiscountAmt())
.add(payment.getWriteOffAmt())
.add(payment.getOverUnderAmt());
if (!payment.isReceipt())
availableAmt = availableAmt.negate();
log.fine("allocatePaymentWithInfo - Available=" + availableAmt);
//
if (payment.getC_Invoice_ID() != 0)
{
for (int i = 0; i < m_invoices.length; i++)
{
MInvoice invoice = m_invoices[i];
if (invoice.isPaid())
continue;
// log.fine("allocateIndividualPayments - " + invoice);
if (payment.getC_Invoice_ID() == invoice.getC_Invoice_ID())
{
if (payment.getC_Currency_ID() == invoice.getC_Currency_ID())
{
BigDecimal openAmt = invoice.getOpenAmt(true, null);
if (!invoice.isSOTrx())
openAmt = openAmt.negate();
log.fine("allocatePaymentWithInfo - " + invoice + ", Open=" + openAmt);
// With Discount, etc.
if (availableAmt.compareTo(openAmt) == 0)
{
if (payment.allocateIt())
{
addLog(0, payment.getDateAcct(), openAmt, payment.getDocumentNo() + " [1]");
count++;
}
break;
}
}
else // Mixed Currency
{
}
} // invoice found
} // for all invoices
} // payment has invoice
else // No direct invoice
{
MPaySelectionCheck psCheck = MPaySelectionCheck.getOfPayment(getCtx(), payment.getC_Payment_ID(), get_TrxName());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -