use of org.compiere.model.MPayment in project adempiere by adempiere.
the class Allocation method saveData.
/**************************************************************************
* Save Data
*/
public String saveData(int m_WindowNo, Object date, IMiniTable payment, IMiniTable invoice, String trxName) {
if (m_noInvoices + m_noPayments == 0)
return "";
// 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) date;
// the allocation currency
int C_Currency_ID = m_C_Currency_ID;
//
if (AD_Org_ID == 0) {
//ADialog.error(m_WindowNo, this, "Org0NotAllowed", null);
throw new AdempiereException("@Org0NotAllowed@");
}
//
log.config("Client=" + AD_Client_ID + ", Org=" + AD_Org_ID + ", BPartner=" + C_BPartner_ID + ", Date=" + DateTrx);
// Payment - Loop and add them to paymentList/amountList
int pRows = payment.getRowCount();
ArrayList<Integer> paymentList = new ArrayList<Integer>(pRows);
ArrayList<BigDecimal> amountList = new ArrayList<BigDecimal>(pRows);
BigDecimal paymentAppliedAmt = Env.ZERO;
paymentList = payment.getSelectedKeys();
// Sum up the payment and applied amounts.
for (int i = 0; i < pRows; i++) {
if (payment.isRowChecked(i)) {
// Applied Payment
BigDecimal PaymentAmt = (BigDecimal) payment.getValueAt(i, i_payment);
amountList.add(PaymentAmt);
//
paymentAppliedAmt = paymentAppliedAmt.add(PaymentAmt);
//
log.fine("C_Payment_ID=" + payment.getRowKey(i) + " - PaymentAmt=" + // + " * " + Multiplier + " = " + PaymentAmtAbs);
PaymentAmt);
}
}
log.config("Number of Payments=" + paymentList.size() + " - Total=" + paymentAppliedAmt);
// Invoices - Loop and generate allocations
int iRows = invoice.getRowCount();
// Create Allocation
MAllocationHdr alloc = new // manual
MAllocationHdr(// manual
Env.getCtx(), // manual
true, DateTrx, C_Currency_ID, Env.getContext(Env.getCtx(), "#AD_User_Name"), trxName);
alloc.setAD_Org_ID(AD_Org_ID);
// Set Description
if (!Util.isEmpty(m_description))
alloc.setDescription(m_description);
alloc.saveEx();
// For all invoices
BigDecimal unmatchedApplied = Env.ZERO;
for (int i = 0; i < iRows; i++) {
// Invoice line is selected
if (invoice.isRowChecked(i)) {
// Invoice variables
int C_Invoice_ID = ((IDColumn) invoice.getValueAt(i, invoice.getKeyColumnIndex())).getRecord_ID();
BigDecimal AppliedAmt = (BigDecimal) invoice.getValueAt(i, i_applied);
// semi-fixed fields (reset after first invoice)
BigDecimal DiscountAmt = (BigDecimal) invoice.getValueAt(i, i_discount);
BigDecimal WriteOffAmt = (BigDecimal) invoice.getValueAt(i, i_writeOff);
// OverUnderAmt needs to be in Allocation Currency
BigDecimal OverUnderAmt = ((BigDecimal) invoice.getValueAt(i, i_open)).subtract(AppliedAmt).subtract(DiscountAmt).subtract(WriteOffAmt);
// + " -> " + AppliedAbs);
log.config("Invoice #" + i + " - AppliedAmt=" + AppliedAmt);
for (int j = 0; j < paymentList.size() && AppliedAmt.signum() != 0; j++) {
int C_Payment_ID = ((Integer) paymentList.get(j)).intValue();
BigDecimal PaymentAmt = (BigDecimal) amountList.get(j);
if (// only match same sign (otherwise appliedAmt increases)
PaymentAmt.signum() == AppliedAmt.signum()) {
// and not zero (appliedAmt was checked earlier)
log.config(".. with payment #" + j + ", Amt=" + PaymentAmt);
BigDecimal amount = AppliedAmt;
if (// if there's more open on the invoice
amount.abs().compareTo(PaymentAmt.abs()) > 0)
// than left in the payment
amount = PaymentAmt;
// Allocation Line
MAllocationLine aLine = new MAllocationLine(alloc, amount, DiscountAmt, WriteOffAmt, OverUnderAmt);
aLine.setDocInfo(C_BPartner_ID, C_Order_ID, C_Invoice_ID);
aLine.setPaymentInfo(C_Payment_ID, C_CashLine_ID);
aLine.saveEx();
// Apply Discounts and WriteOff only first time
DiscountAmt = Env.ZERO;
WriteOffAmt = Env.ZERO;
// subtract amount from Payment/Invoice
AppliedAmt = AppliedAmt.subtract(amount);
PaymentAmt = PaymentAmt.subtract(amount);
log.fine("Allocation Amount=" + amount + " - Remaining Applied=" + AppliedAmt + ", Payment=" + PaymentAmt);
// update
amountList.set(j, PaymentAmt);
}
// for all applied amounts
}
if (AppliedAmt.signum() == 0 && DiscountAmt.signum() == 0 && WriteOffAmt.signum() == 0)
continue;
else {
// remainder will need to match against other invoices
int C_Payment_ID = 0;
// Allocation Line
MAllocationLine aLine = new MAllocationLine(alloc, AppliedAmt, DiscountAmt, WriteOffAmt, OverUnderAmt);
aLine.setDocInfo(C_BPartner_ID, C_Order_ID, C_Invoice_ID);
aLine.setPaymentInfo(C_Payment_ID, C_CashLine_ID);
aLine.saveEx();
log.fine("Allocation Amount=" + AppliedAmt);
unmatchedApplied = unmatchedApplied.add(AppliedAmt);
}
}
// invoice selected
}
// check for unapplied payment amounts (eg from payment reversals)
for (int i = 0; i < paymentList.size(); i++) {
BigDecimal payAmt = (BigDecimal) amountList.get(i);
if (payAmt.signum() == 0)
continue;
int C_Payment_ID = ((Integer) paymentList.get(i)).intValue();
log.fine("Payment=" + C_Payment_ID + ", Amount=" + payAmt);
// Allocation Line
MAllocationLine aLine = new MAllocationLine(alloc, payAmt, Env.ZERO, Env.ZERO, Env.ZERO);
aLine.setDocInfo(C_BPartner_ID, 0, 0);
aLine.setPaymentInfo(C_Payment_ID, 0);
aLine.saveEx();
unmatchedApplied = unmatchedApplied.subtract(payAmt);
}
// check for charge amount
if (m_C_Charge_ID > 0 && unmatchedApplied.compareTo(Env.ZERO) != 0) {
BigDecimal chargeAmt = totalDiff;
// Allocation Line
MAllocationLine aLine = new MAllocationLine(alloc, chargeAmt.negate(), Env.ZERO, Env.ZERO, Env.ZERO);
aLine.set_CustomColumn("C_Charge_ID", m_C_Charge_ID);
aLine.setC_BPartner_ID(m_C_BPartner_ID);
aLine.saveEx(trxName);
unmatchedApplied = unmatchedApplied.add(chargeAmt);
}
if (unmatchedApplied.signum() != 0)
log.log(Level.SEVERE, "Allocation not balanced -- out by " + unmatchedApplied);
// Should start WF
if (alloc.get_ID() != 0) {
if (//@Trifon
!alloc.processIt(DocAction.ACTION_Complete))
//@Trifon
throw new AdempiereException("@ProcessFailed@: " + alloc.getProcessMsg());
alloc.saveEx();
}
// Test/Set IsPaid for Invoice - requires that allocation is posted
for (int i = 0; i < iRows; i++) {
// Invoice line is selected
if (invoice.isRowChecked(i)) {
// Invoice variables
int C_Invoice_ID = ((IDColumn) invoice.getValueAt(i, invoice.getKeyColumnIndex())).getRecord_ID();
String sql = "SELECT invoiceOpen(C_Invoice_ID, 0) " + "FROM C_Invoice WHERE C_Invoice_ID=?";
BigDecimal open = DB.getSQLValueBD(trxName, sql, C_Invoice_ID);
if (open != null && open.signum() == 0) {
sql = "UPDATE C_Invoice SET IsPaid='Y' " + "WHERE C_Invoice_ID=" + C_Invoice_ID;
int no = DB.executeUpdate(sql, trxName);
log.config("Invoice #" + i + " is paid - updated=" + no);
} else
log.config("Invoice #" + i + " is not paid - " + open);
}
}
// Test/Set Payment is fully allocated
for (int i = 0; i < paymentList.size(); i++) {
int C_Payment_ID = ((Integer) paymentList.get(i)).intValue();
MPayment pay = new MPayment(Env.getCtx(), C_Payment_ID, trxName);
if (pay.testAllocation())
pay.saveEx();
log.config("Payment #" + i + (pay.isAllocated() ? " not" : " is") + " fully allocated");
}
paymentList.clear();
amountList.clear();
return alloc.getDocumentNo();
}
use of org.compiere.model.MPayment in project adempiere by adempiere.
the class InfoPayment method initInfo.
// statInit
/**
* General Init
* @return true, if success
*/
protected void initInfo(int record_id, String value) {
//
if (!(record_id == 0) && value != null && value.length() > 0) {
log.severe("Received both a record_id and a value: " + record_id + " - " + value);
}
// Set values
if (// A record is defined
!(record_id == 0)) {
fieldID = record_id;
String trxName = Trx.createTrxName();
MPayment p = new MPayment(Env.getCtx(), record_id, trxName);
fcheckReceipt.setSelected(p.isReceipt());
fcheckPayment.setSelected(!p.isReceipt());
p = null;
Trx.get(trxName, false).close();
} else // Try to find other criteria in the context
{
String id;
// C_BPartner_ID
id = Env.getContext(Env.getCtx(), p_WindowNo, p_TabNo, "C_BPartner_ID", true);
if (id != null && id.length() != 0 && (new Integer(id).intValue() > 0))
fBPartner_ID.setValue(new Integer(id));
// The value passed in from the field
if (value != null && value.length() > 0) {
fDocumentNo.setValue(value);
} else {
// C_Payment_ID
id = Env.getContext(Env.getCtx(), p_WindowNo, p_TabNo, "C_Payment_ID", true);
if (id != null && id.length() != 0 && (new Integer(id).intValue() > 0)) {
fieldID = new Integer(id).intValue();
String trxName = Trx.createTrxName();
MPayment p = new MPayment(Env.getCtx(), record_id, trxName);
fcheckReceipt.setSelected(p.isReceipt());
fcheckPayment.setSelected(!p.isReceipt());
p = null;
Trx.get(trxName, false).close();
}
// C_BankAccount_ID
id = Env.getContext(Env.getCtx(), p_WindowNo, p_TabNo, "C_BankAccount_ID", true);
if (id != null && id.length() != 0 && (new Integer(id).intValue() > 0))
fBankAccount_ID.setValue(new Integer(id));
}
}
//
return;
}
use of org.compiere.model.MPayment in project adempiere by adempiere.
the class AllocationAuto method allocateBPPaymentWithInfo.
// 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(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("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(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
} else // payment has invoice
// No direct invoice
{
MPaySelectionCheck psCheck = MPaySelectionCheck.getOfPayment(getCtx(), payment.getC_Payment_ID(), get_TrxName());
if (psCheck == null)
continue;
//
BigDecimal totalInvoice = Env.ZERO;
List<MPaySelectionLine> paySelectionLines = psCheck.getPaySelectionLinesAsList(false);
//for (int i = 0; i < psLines.length; i++)
for (MPaySelectionLine paySelectionLine : paySelectionLines) {
MInvoice invoice = paySelectionLine.getInvoice();
if (payment.getC_Currency_ID() == invoice.getC_Currency_ID()) {
BigDecimal invoiceAmt = invoice.getOpenAmt(true, null);
BigDecimal overUnder = paySelectionLine.getOpenAmt().subtract(paySelectionLine.getPayAmt()).subtract(paySelectionLine.getDiscountAmt()).subtract(paySelectionLine.getDifferenceAmt());
invoiceAmt = invoiceAmt.subtract(paySelectionLine.getDiscountAmt()).subtract(paySelectionLine.getDifferenceAmt()).subtract(overUnder);
if (!invoice.isSOTrx())
invoiceAmt = invoiceAmt.negate();
log.fine(invoice + ", Invoice=" + invoiceAmt);
totalInvoice = totalInvoice.add(invoiceAmt);
} else // Multi-Currency
{
}
}
if (availableAmt.compareTo(totalInvoice) == 0) {
if (payment.allocateIt()) {
addLog(0, payment.getDateAcct(), availableAmt, payment.getDocumentNo() + " [n]");
count++;
}
}
}
// No direct invoice
}
return count;
}
use of org.compiere.model.MPayment in project adempiere by adempiere.
the class StatementCreateFrom method doIt.
@Override
protected String doIt() throws Exception {
// fixed values
if (getRecord_ID() <= 0)
throw new AdempiereException("@C_BankStatement_ID@ @NotFound@");
MBankStatement bankStatement = new MBankStatement(Env.getCtx(), getRecord_ID(), get_TrxName());
// Get Bank Account
MBankAccount bankAccount = bankStatement.getBankAccount();
// Created
AtomicInteger created = new AtomicInteger(0);
// Total Amount
AtomicReference<BigDecimal> totalAmt = new AtomicReference<>(BigDecimal.ZERO);
log.config(bankStatement.toString());
// Lines
getSelectionKeys().stream().forEach(key -> {
Timestamp dateTransaction = getSelectionAsTimestamp(key, "P_DateTrx");
int paymentId = getSelectionAsInt(key, "P_C_Payment_ID");
int currencyId = getSelectionAsInt(key, "P_C_Currency_ID");
BigDecimal transactionAmount = getSelectionAsBigDecimal(key, "P_ConvertedAmount");
log.fine("Line Date=" + dateTransaction + ", Payment=" + paymentId + ", Currency=" + currencyId + ", Amt=" + transactionAmount);
MBankStatementLine bankStatementLine = new MBankStatementLine(bankStatement);
bankStatementLine.setDateAcct(bankStatement.getStatementDate());
bankStatementLine.setStatementLineDate(bankStatement.getStatementDate());
bankStatementLine.setValutaDate(dateTransaction);
MPayment payment = new MPayment(Env.getCtx(), paymentId, get_TrxName());
bankStatementLine.setPayment(payment);
bankStatementLine.setTrxAmt(transactionAmount);
bankStatementLine.setStmtAmt(transactionAmount);
bankStatementLine.setC_Currency_ID(bankAccount.getC_Currency_ID());
bankStatementLine.saveEx();
created.updateAndGet(createNo -> createNo + 1);
totalAmt.updateAndGet(amt -> amt.add(payment.isReceipt() ? payment.getPayAmt() : payment.getPayAmt().negate()));
});
// Return Created
return "@Created@ = " + created.get() + " - @PayAmt@ = " + DisplayType.getNumberFormat(DisplayType.Amount).format(totalAmt.get());
}
use of org.compiere.model.MPayment in project adempiere by adempiere.
the class CreateOrderBasedOnAnother method createAllocations.
/**
* Create Allocations for new order
* @param targetOrder
*/
private void createAllocations(MOrder targetOrder) {
List<MPayment> payments = MPayment.getOfOrder(targetOrder);
MInvoice[] invoices = targetOrder.getInvoices();
BigDecimal totalPay = BigDecimal.ZERO;
BigDecimal totalInvoiced = BigDecimal.ZERO;
for (MPayment payment : payments) totalPay = totalPay.add(payment.getPayAmt());
for (MInvoice invoice : invoices) {
totalInvoiced = totalInvoiced.add(invoice.getGrandTotal());
}
if (totalInvoiced.signum() != 0 && totalPay.signum() != 0 && totalInvoiced.compareTo(totalPay) == 0) {
MAllocationHdr allocation = new MAllocationHdr(getCtx(), true, today, targetOrder.getC_Currency_ID(), targetOrder.getDescription(), get_TrxName());
allocation.setDocStatus(org.compiere.process.DocAction.STATUS_Drafted);
allocation.setDocAction(org.compiere.process.DocAction.ACTION_Complete);
allocation.saveEx();
addLog(allocation.getDocumentInfo());
for (MInvoice invoice : invoices) {
MAllocationLine allocationLine = new MAllocationLine(allocation);
allocationLine.setDocInfo(targetOrder.getC_BPartner_ID(), targetOrder.getC_Order_ID(), invoice.getC_Invoice_ID());
allocationLine.setAmount(invoice.getGrandTotal());
allocationLine.saveEx();
}
for (MPayment payment : payments) {
MAllocationLine allocationLine = new MAllocationLine(allocation);
allocationLine.setPaymentInfo(payment.get_ID(), 0);
allocationLine.setAmount(payment.getPayAmt());
allocationLine.saveEx();
}
allocation.processIt(org.compiere.process.DocAction.ACTION_Complete);
allocation.saveEx();
}
}
Aggregations