use of org.compiere.model.MAllocationLine in project adempiere by adempiere.
the class AllocationAuto method createAllocation.
// allocateOldestFirst
/**********************************************************************************************
* Create Allocation allocation
* @param C_Currency_ID currency
* @param description decription
* @param Amount amount
* @param DiscountAmt discount
* @param WriteOffAmt write off
* @param OverUnderAmt over under
* @param C_BPartner_ID partner
* @param C_Payment_ID payment
* @param C_Invoice_ID invoice
* @return true if created
*/
private boolean createAllocation(int C_Currency_ID, String description, Timestamp dateAcct, BigDecimal Amount, BigDecimal DiscountAmt, BigDecimal WriteOffAmt, BigDecimal OverUnderAmt, int C_BPartner_ID, int C_Payment_ID, int C_Invoice_ID, int AD_Org_ID) {
// Process old Allocation
if (m_allocation != null && m_allocation.getC_Currency_ID() != C_Currency_ID)
processAllocation();
// New Allocation
if (m_allocation == null) {
m_allocation = new // automatic
MAllocationHdr(// automatic
getCtx(), // automatic
false, // automatic
dateAcct, C_Currency_ID, "Auto " + description, get_TrxName());
m_allocation.setAD_Org_ID(AD_Org_ID);
if (!m_allocation.save())
return false;
}
// New Allocation Line
MAllocationLine aLine = new MAllocationLine(m_allocation, Amount, DiscountAmt, WriteOffAmt, OverUnderAmt);
aLine.setC_BPartner_ID(C_BPartner_ID);
aLine.setC_Payment_ID(C_Payment_ID);
aLine.setC_Invoice_ID(C_Invoice_ID);
return aLine.save();
}
use of org.compiere.model.MAllocationLine in project adempiere by adempiere.
the class Doc_AllocationTax method loadLines.
// loadDocumentDetails
/**
* Load Invoice Line
* @param alloc header
* @return DocLine Array
*/
private DocLine[] loadLines(MAllocationHdr alloc) {
ArrayList<DocLine> list = new ArrayList<DocLine>();
MAllocationLine[] lines = alloc.getLines(false);
for (int i = 0; i < lines.length; i++) {
MAllocationLine line = lines[i];
DocLine_Allocation docLine = new DocLine_Allocation(line, this);
// Get Payment Conversion Rate
if (line.getC_Payment_ID() != 0) {
MPayment payment = new MPayment(getCtx(), line.getC_Payment_ID(), getTrxName());
int C_ConversionType_ID = payment.getC_ConversionType_ID();
docLine.setC_ConversionType_ID(C_ConversionType_ID);
}
//
log.fine(docLine.toString());
list.add(docLine);
}
// Return Array
DocLine[] dls = new DocLine[list.size()];
list.toArray(dls);
return dls;
}
use of org.compiere.model.MAllocationLine in project lar_361 by comitsrl.
the class LCO_Validator method completePaymentWithholdings.
private String completePaymentWithholdings(MAllocationHdr ah) {
MAllocationLine[] als = ah.getLines(true);
for (int i = 0; i < als.length; i++) {
MAllocationLine al = als[i];
if (al.getC_Invoice_ID() > 0) {
String sql = "SELECT LCO_InvoiceWithholding_ID " + "FROM LCO_InvoiceWithholding " + "WHERE C_Invoice_ID = ? AND " + "IsActive = 'Y' AND " + "IsCalcOnPayment = 'Y' AND " + "Processed = 'N' AND " + "C_AllocationLine_ID IS NULL";
PreparedStatement pstmt = DB.prepareStatement(sql, ah.get_TrxName());
ResultSet rs = null;
try {
pstmt.setInt(1, al.getC_Invoice_ID());
rs = pstmt.executeQuery();
while (rs.next()) {
int iwhid = rs.getInt(1);
MLCOInvoiceWithholding iwh = new MLCOInvoiceWithholding(ah.getCtx(), iwhid, ah.get_TrxName());
iwh.setC_AllocationLine_ID(al.getC_AllocationLine_ID());
iwh.setDateAcct(ah.getDateAcct());
iwh.setDateTrx(ah.getDateTrx());
iwh.setProcessed(true);
if (!iwh.save())
return "Error saving LCO_InvoiceWithholding completePaymentWithholdings";
}
} catch (SQLException e) {
e.printStackTrace();
return e.getLocalizedMessage();
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
}
}
return null;
}
use of org.compiere.model.MAllocationLine in project lar_361 by comitsrl.
the class LCO_Validator method accountingForInvoiceWithholdingOnPayment.
private String accountingForInvoiceWithholdingOnPayment(MAllocationHdr ah) {
// Accounting like Doc_Allocation
// (Write off) vs (invoice withholding where iscalconpayment=Y)
// 20070807 - globalqss - instead of adding a new WriteOff post, find the
// current WriteOff and subtract from the posting
Doc doc = ah.getDoc();
ArrayList<Fact> facts = doc.getFacts();
// one fact per acctschema
for (int i = 0; i < facts.size(); i++) {
Fact fact = facts.get(i);
MAcctSchema as = fact.getAcctSchema();
MAllocationLine[] alloc_lines = ah.getLines(false);
for (int j = 0; j < alloc_lines.length; j++) {
BigDecimal tottax = new BigDecimal(0);
MAllocationLine alloc_line = alloc_lines[j];
doc.setC_BPartner_ID(alloc_line.getC_BPartner_ID());
int inv_id = alloc_line.getC_Invoice_ID();
if (inv_id <= 0)
continue;
MInvoice invoice = null;
invoice = new MInvoice(ah.getCtx(), alloc_line.getC_Invoice_ID(), ah.get_TrxName());
if (invoice == null || invoice.getC_Invoice_ID() == 0)
continue;
String sql = "SELECT i.C_Tax_ID, NVL(SUM(i.TaxBaseAmt),0) AS TaxBaseAmt, NVL(SUM(i.TaxAmt),0) AS TaxAmt, t.Name, t.Rate, t.IsSalesTax " + " FROM LCO_InvoiceWithholding i, C_Tax t " + " WHERE i.C_Invoice_ID = ? AND " + "i.IsCalcOnPayment = 'Y' AND " + "i.IsActive = 'Y' AND " + "i.Processed = 'Y' AND " + "i.C_AllocationLine_ID = ? AND " + "i.C_Tax_ID = t.C_Tax_ID " + "GROUP BY i.C_Tax_ID, t.Name, t.Rate, t.IsSalesTax";
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql, ah.get_TrxName());
pstmt.setInt(1, invoice.getC_Invoice_ID());
pstmt.setInt(2, alloc_line.getC_AllocationLine_ID());
rs = pstmt.executeQuery();
while (rs.next()) {
int tax_ID = rs.getInt(1);
BigDecimal taxBaseAmt = rs.getBigDecimal(2);
BigDecimal amount = rs.getBigDecimal(3);
String name = rs.getString(4);
BigDecimal rate = rs.getBigDecimal(5);
boolean salesTax = rs.getString(6).equals("Y") ? true : false;
DocTax taxLine = new DocTax(tax_ID, name, rate, taxBaseAmt, amount, salesTax);
if (amount != null && amount.signum() != 0) {
FactLine tl = null;
if (invoice.isSOTrx()) {
tl = fact.createLine(null, taxLine.getAccount(DocTax.ACCTTYPE_TaxDue, as), as.getC_Currency_ID(), amount, null);
} else {
tl = fact.createLine(null, taxLine.getAccount(taxLine.getAPTaxType(), as), as.getC_Currency_ID(), null, amount);
}
if (tl != null)
tl.setC_Tax_ID(taxLine.getC_Tax_ID());
tottax = tottax.add(amount);
}
}
} catch (Exception e) {
log.log(Level.SEVERE, sql, e);
return "Error posting C_InvoiceTax from LCO_InvoiceWithholding";
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
// Write off DR
if (Env.ZERO.compareTo(tottax) != 0) {
// First try to find the WriteOff posting record
FactLine[] factlines = fact.getLines();
boolean foundflwriteoff = false;
for (int ifl = 0; ifl < factlines.length; ifl++) {
FactLine fl = factlines[ifl];
if (fl.getAccount().equals(doc.getAccount(Doc.ACCTTYPE_WriteOff, as))) {
foundflwriteoff = true;
// old balance = DB - CR
BigDecimal balamt = fl.getAmtSourceDr().subtract(fl.getAmtSourceCr());
// new balance = old balance +/- tottax
BigDecimal newbalamt = Env.ZERO;
if (invoice.isSOTrx())
newbalamt = balamt.subtract(tottax);
else
newbalamt = balamt.add(tottax);
if (Env.ZERO.compareTo(newbalamt) == 0) {
// both zeros, remove the line
fact.remove(fl);
} else if (Env.ZERO.compareTo(newbalamt) > 0) {
fl.setAmtAcct(fl.getC_Currency_ID(), Env.ZERO, newbalamt);
fl.setAmtSource(fl.getC_Currency_ID(), Env.ZERO, newbalamt);
} else {
fl.setAmtAcct(fl.getC_Currency_ID(), newbalamt, Env.ZERO);
fl.setAmtSource(fl.getC_Currency_ID(), newbalamt, Env.ZERO);
}
break;
}
}
if (!foundflwriteoff) {
// Create a new line
DocLine line = new DocLine(alloc_line, doc);
FactLine fl = null;
if (invoice.isSOTrx()) {
fl = fact.createLine(line, doc.getAccount(Doc.ACCTTYPE_WriteOff, as), as.getC_Currency_ID(), null, tottax);
} else {
fl = fact.createLine(line, doc.getAccount(Doc.ACCTTYPE_WriteOff, as), as.getC_Currency_ID(), tottax, null);
}
if (fl != null)
fl.setAD_Org_ID(ah.getAD_Org_ID());
}
}
}
}
return null;
}
use of org.compiere.model.MAllocationLine in project lar_361 by comitsrl.
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;
for (int i = 0; i < pRows; i++) {
// Payment line is selected
if (((Boolean) payment.getValueAt(i, 0)).booleanValue()) {
// Value
KeyNamePair pp = (KeyNamePair) payment.getValueAt(i, 2);
// Payment variables
int C_Payment_ID = pp.getKey();
paymentList.add(new Integer(C_Payment_ID));
//
// Applied Payment
BigDecimal PaymentAmt = (BigDecimal) payment.getValueAt(i, i_payment);
amountList.add(PaymentAmt);
//
paymentAppliedAmt = paymentAppliedAmt.add(PaymentAmt);
//
log.fine("C_Payment_ID=" + C_Payment_ID + " - 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);
alloc.saveEx();
// For all invoices
int invoiceLines = 0;
BigDecimal unmatchedApplied = Env.ZERO;
for (int i = 0; i < iRows; i++) {
// Invoice line is selected
if (((Boolean) invoice.getValueAt(i, 0)).booleanValue()) {
invoiceLines++;
// Value
KeyNamePair pp = (KeyNamePair) invoice.getValueAt(i, 2);
// Invoice variables
int C_Invoice_ID = pp.getKey();
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);
// C_Invoice_ID en la columna NotaCredito_ID.
if (aLine.getC_Invoice().getC_DocType().getDocBaseType().equals(MDocType.DOCBASETYPE_ARCreditMemo) || aLine.getC_Invoice().getC_DocType().getDocBaseType().equals(MDocType.DOCBASETYPE_APCreditMemo))
aLine.set_ValueOfColumn("NotaCredito_ID", C_Invoice_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);
// C_Invoice_ID en la columna NotaCredito_ID.
if (aLine.getC_Invoice().getC_DocType().getDocBaseType().equals(MDocType.DOCBASETYPE_ARCreditMemo) || aLine.getC_Invoice().getC_DocType().getDocBaseType().equals(MDocType.DOCBASETYPE_APCreditMemo))
aLine.set_ValueOfColumn("NotaCredito_ID", C_Invoice_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);
}
if (unmatchedApplied.signum() != 0)
log.log(Level.SEVERE, "Allocation not balanced -- out by " + unmatchedApplied);
// Should start WF
if (alloc.get_ID() != 0) {
alloc.processIt(DocAction.ACTION_Complete);
alloc.saveEx();
}
// Test/Set IsPaid for Invoice - requires that allocation is posted
for (int i = 0; i < iRows; i++) {
// Invoice line is selected
if (((Boolean) invoice.getValueAt(i, 0)).booleanValue()) {
// Value
KeyNamePair pp = (KeyNamePair) invoice.getValueAt(i, 2);
// Invoice variables
int C_Invoice_ID = pp.getKey();
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();
}
Aggregations