📄 vallocation.java
字号:
}
}
m_calculating = false;
calculate();
} // tableChanged
/**
* Calculate Allocation info
*/
private void calculate ()
{
Log.trace(Log.l3_Util, "VAllocation.calculate");
//
DecimalFormat format = DisplayType.getNumberFormat(DisplayType.Amount);
// Payment
TableModel payment = paymentTable.getModel();
BigDecimal totalPay = new BigDecimal(0.0);
int rows = payment.getRowCount();
m_noPayments = 0;
for (int i = 0; i < rows; i++)
{
if (((Boolean)payment.getValueAt(i, 0)).booleanValue())
{
BigDecimal bd = (BigDecimal)payment.getValueAt(i, i_payment);
totalPay = totalPay.add(bd); // Applied Pay
m_noPayments++;
Log.trace(Log.l6_Database, "Payment_" + i + " = " + bd + " - Total=" + totalPay);
}
}
paymentInfo.setText(String.valueOf(m_noPayments) + " - "
+ Msg.getMsg(Env.getCtx(), "Sum") + " " + format.format(totalPay) + " ");
// Invoices
TableModel invoice = invoiceTable.getModel();
BigDecimal totalInv = new BigDecimal(0.0);
rows = invoice.getRowCount();
m_noInvoices = 0;
for (int i = 0; i < rows; i++)
{
if (((Boolean)invoice.getValueAt(i, 0)).booleanValue())
{
BigDecimal bd = (BigDecimal)invoice.getValueAt(i, i_applied);
totalInv = totalInv.add(bd); // Applied Inv
m_noInvoices++;
Log.trace(Log.l6_Database, "Invoice_" + i + " = " + bd + " - Total=" + totalPay);
}
}
invoiceInfo.setText(String.valueOf(m_noInvoices) + " - "
+ Msg.getMsg(Env.getCtx(), "Sum") + " " + format.format(totalInv) + " ");
// Set Allocation Currency
allocCurrencyLabel.setText(currencyPick.getDisplay());
// Difference
BigDecimal difference = totalPay.subtract(totalInv);
differenceField.setText(format.format(difference));
if (difference.compareTo(new BigDecimal(0.0)) == 0)
allocateButton.setEnabled(true);
else
allocateButton.setEnabled(false);
} // calculate
/**
* Vetoable Change Listener.
* - Business Partner
* - Discount
* @param e event
*/
public void vetoableChange (PropertyChangeEvent e)
{
String name = e.getPropertyName();
Object value = e.getNewValue();
Log.trace(Log.l3_Util, "VAllocation.vetoableChange - " + name + "=" + value);
if (value == null)
return;
// BPartner / Currency
if (name.equals("C_BPartner_ID"))
{
bpartnerSearch.setValue(value);
m_C_BPartner_ID = ((Integer)value).intValue();
loadBPartner();
}
else if (name.equals("C_Currency_ID"))
{
m_C_Currency_ID = ((Integer)value).intValue();
loadBPartner();
}
else if (name.equals("Date"))
loadBPartner();
} // vetoableChange
/*************************************************************************/
/**
* Save Data
*/
private void saveData()
{
if (m_noInvoices + m_noPayments == 0)
return;
Log.trace(Log.l3_Util, "VAllocation.saveData");
// fixed fields
int AD_Client_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "AD_Client_ID");
int AD_Org_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNo, "AD_Org_ID");
int C_BPartner_ID = m_C_BPartner_ID;
int C_Order_ID = 0;
int C_CashLine_ID = 0;
Timestamp DateTrx = (Timestamp)dateField.getValue();
int C_Currency_ID = m_C_Currency_ID;
int AllocationNo = 0; // C_Allocation_Seq.NextVal
try
{
CallableStatement cstmt = DB.prepareCall("{? = call C_Allocation_No(?)}");
cstmt.registerOutParameter(1, Types.INTEGER);
cstmt.setInt(2, AD_Client_ID);
cstmt.executeUpdate();
AllocationNo = cstmt.getInt(1);
cstmt.close();
}
catch (SQLException e)
{
Log.error ("VAllocation.saveData - AllocationNo", e);
}
Log.trace(Log.l4_Data, "Client=" + AD_Client_ID + ", Org=" + AD_Org_ID
+ ", BPartner=" + C_BPartner_ID + ", AllocationNo=" + AllocationNo + ", Date=" + DateTrx);
/**
* Generation of allocations: amount/discount/writeOff
* - if there is one payment -- one line per invoice is generated
* with both the Invoice and Payment reference
* Pay=80 Inv=100 Disc=10 WOff=10 => 80/10/10 Pay#1 Inv#1
* or
* Pay=160 Inv=100 Disc=10 WOff=10 => 80/10/10 Pay#1 Inv#1
* Pay=160 Inv=100 Disc=10 WOff=10 => 80/10/10 Pay#1 Inv#2
*
* - if there are multiple payment lines -- the amounts are allocated
* starting with the first payment and payment
* Pay=60 Inv=100 Disc=10 WOff=10 => 60/10/10 Pay#1 Inv#1
* Pay=100 Inv=100 Disc=10 WOff=10 => 20/0/0 Pay#2 Inv#1
* Pay=100 Inv=100 Disc=10 WOff=10 => 80/10/10 Pay#2 Inv#2
*
* - if you apply a credit memo to an invoice
* Inv=10 Disc=0 WOff=0 => 10/0/0 Inv#1
* Inv=-10 Disc=0 WOff=0 => -10/0/0 Inv#2
*
* - if you want to write off a (partial) invoice without applying,
* enter zero in applied
* Inv=10 Disc=1 WOff=9 => 0/1/9 Inv#1
* Issues
* - you cannot write-off a payment
*/
// Payment - Loop and add them to paymentList/amountList
int rows = paymentTable.getRowCount();
TableModel payment = paymentTable.getModel();
ArrayList paymentList = new ArrayList(rows);
ArrayList amountList = new ArrayList(rows);
ArrayList multiplierList = new ArrayList(rows);
for (int i = 0; i < rows; i++)
{
// Payment line is selected
if (((Boolean)payment.getValueAt(i, 0)).booleanValue())
{
KeyNamePair pp = (KeyNamePair)payment.getValueAt(i, 2); // Value
// Payment variables
int C_Payment_ID = pp.getKey();
paymentList.add(new Integer(C_Payment_ID));
//
BigDecimal PaymentAmt = (BigDecimal)payment.getValueAt(i, i_payment); // Applied Payment
amountList.add(PaymentAmt);
BigDecimal Multiplier = (BigDecimal)payment.getValueAt(i, i_payment+1);
multiplierList.add(Multiplier);
Log.trace(Log.l6_Database, "Payment - ID=" + C_Payment_ID + " - PaymentAmt=" + PaymentAmt + " " + Multiplier);
}
}
Log.trace(Log.l4_Data, "Number of Payments=" + paymentList.size());
// Invoices - Loop and generate alloctions
rows = invoiceTable.getRowCount();
TableModel invoice = invoiceTable.getModel();
BigDecimal TotalAppliedAmt = Env.ZERO;
for (int i = 0; i < rows; i++)
{
// Invoice line is selected
if (((Boolean)invoice.getValueAt(i, 0)).booleanValue())
{
KeyNamePair pp = (KeyNamePair)invoice.getValueAt(i, 2); // Value
// Invoice variables
int C_Invoice_ID = pp.getKey();
BigDecimal AppliedAmt = (BigDecimal)invoice.getValueAt(i, i_applied);
BigDecimal AppliedMultiplier = (BigDecimal)invoice.getValueAt(i, i_multiplier);
BigDecimal AppliedAbs = AppliedAmt.multiply(AppliedMultiplier);
// semi-fixed fields (reset after first invoice)
BigDecimal DiscountAmt = (BigDecimal)invoice.getValueAt(i, i_discount);
DiscountAmt = DiscountAmt.multiply(AppliedMultiplier);
BigDecimal WriteOffAmt = (BigDecimal)invoice.getValueAt(i, i_writeOff);
WriteOffAmt = WriteOffAmt.multiply(AppliedMultiplier);
Log.trace(Log.l4_Data, "Invoice #" + i + " - AppliedAmt=" + AppliedAmt + " -> " + AppliedAbs);
// loop through all payments until invoice applied
int noPayments = 0;
for (int j = 0; j < paymentList.size() && AppliedAmt.compareTo(Env.ZERO) != 0; j++)
{
int C_Payment_ID = ((Integer)paymentList.get(j)).intValue();
BigDecimal PaymentAmt = (BigDecimal)amountList.get(j);
BigDecimal PaymentMultiplier = (BigDecimal)multiplierList.get(j);
BigDecimal PaymentAbs = PaymentAmt.multiply(PaymentMultiplier);
if (PaymentAbs.compareTo(Env.ZERO) != 0)
{
Log.trace(Log.l4_Data, ".. with payment #" + j + ", Amt=" + PaymentAmt + " -> " + PaymentAbs);
noPayments++;
// use the lesser of Payment Amt and Invoice Applied Amt
BigDecimal amount = AppliedAbs.min(PaymentAbs);
if (PaymentAbs.compareTo(Env.ZERO) < 0)
amount = AppliedAbs;
//
Log.trace(Log.l5_DData, "Payment=" + C_Payment_ID + ", Invoice=" + C_Invoice_ID
+ ", Amount=" + amount + ", Discount=" + DiscountAmt + ", WriteOff=" + WriteOffAmt);
int C_Allocation_ID = MAllocation.createAllocation(Env.getCtx(), m_WindowNo,
AD_Client_ID, AD_Org_ID, C_BPartner_ID,
0, C_Invoice_ID, C_Payment_ID, 0,
AllocationNo, C_Currency_ID, DateTrx, true,
amount, DiscountAmt, WriteOffAmt);
// Apply Discounts and WriteOff only first time
DiscountAmt = Env.ZERO;
WriteOffAmt = Env.ZERO;
// subtract amount from Payment/Invoice
AppliedAmt = AppliedAmt.subtract(amount.multiply(PaymentMultiplier));
PaymentAmt = PaymentAmt.subtract(amount.multiply(PaymentMultiplier));
Log.trace(Log.l6_Database, "Allocation Amount=" + amount + " - Remaining Applied=" + AppliedAmt + ", Payment=" + PaymentAmt);
amountList.set(j, PaymentAmt); // update
}
}
// No Payments allocated
if (noPayments == 0)
{
int C_Payment_ID = 0;
Log.trace(Log.l4_Data, " ... no payment - TotalApplied=" + TotalAppliedAmt);
// Create Allocation
Log.trace(Log.l5_DData, "Payment=" + C_Payment_ID + ", Invoice=" + C_Invoice_ID
+ ", Amount=" + AppliedAbs + ", Discount=" + DiscountAmt + ", WriteOff=" + WriteOffAmt);
int C_Allocation_ID = MAllocation.createAllocation(Env.getCtx(), m_WindowNo,
AD_Client_ID, AD_Org_ID, C_BPartner_ID,
0, C_Invoice_ID, C_Payment_ID, 0,
AllocationNo, C_Currency_ID, DateTrx, true,
AppliedAbs, DiscountAmt, WriteOffAmt);
if (C_Allocation_ID == 0)
Log.error("VAllocation.saveData - Allocation not written - Invoice=" + C_Invoice_ID);
Log.trace(Log.l6_Database, "Allocation Amount=" + AppliedAbs);
}
TotalAppliedAmt = TotalAppliedAmt.add(AppliedAmt);
Log.trace(Log.l4_Data, "TotalRemaining=" + TotalAppliedAmt);
// Test/Set IsPaid for Invoice
String sql = "UPDATE C_Invoice SET IsPaid='Y' "
+ "WHERE C_Invoice_Paid(C_Invoice_ID, C_Currency_ID, 1)=GrandTotal"
+ " AND C_Invoice_ID=" + C_Invoice_ID;
int no = DB.executeUpdate(sql);
Log.trace(Log.l4_Data, "Invoice #" + i + (no==0 ? " not" : " is") + " paid");
} // invoice selected
} // invoice loop
if (TotalAppliedAmt.compareTo(Env.ZERO) != 0)
Log.error("VAllocation.saveData - Remaining TotalAppliedAmt=" + TotalAppliedAmt);
// Test/Set Payment is fully allocated
for (int i = 0; i < paymentList.size(); i++)
{
String sql = "UPDATE C_Payment SET IsAllocated='Y' "
+ "WHERE C_Payment_Allocated(C_Payment_ID, C_Currency_ID)=PayAmt"
+ " AND C_Payment_ID=" + ((Integer)paymentList.get(i)).intValue();
int no = DB.executeUpdate(sql);
Log.trace(Log.l4_Data, "Payment #" + i + (no==0 ? " not" : " is") + " fully allocated");
}
paymentList.clear();
amountList.clear();
} // saveData
} // VAllocation
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -