use of org.compiere.model.MAllocationHdr in project adempiere by adempiere.
the class AllocationReset method doIt.
// 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 + ", DateAcct= " + p_DateAcct_From + " - " + p_DateAcct_To + ", C_AllocationHdr_ID=" + p_C_AllocationHdr_ID + ", AllAllocations=" + p_AllAllocations);
if (p_C_AllocationHdr_ID == 0 && !p_AllAllocations)
throw new AdempiereUserError(Msg.parseTranslation(getCtx(), "@Mandatory@: @C_AllocationHdr_ID@"));
m_trx = Trx.get(Trx.createTrxName("AllocReset"), true);
int count = 0;
if (p_C_AllocationHdr_ID != 0) {
MAllocationHdr hdr = new MAllocationHdr(getCtx(), p_C_AllocationHdr_ID, m_trx.getTrxName());
if (delete(hdr))
count++;
m_trx.close();
return "@Deleted@ #" + count;
}
// Selection
StringBuffer sql = new StringBuffer("SELECT * FROM C_AllocationHdr ah " + "WHERE EXISTS (SELECT * FROM C_AllocationLine al " + "WHERE ah.C_AllocationHdr_ID=al.C_AllocationHdr_ID");
if (p_C_BPartner_ID != 0)
sql.append(" AND al.C_BPartner_ID=?");
else if (p_C_BP_Group_ID != 0)
sql.append(" AND EXISTS (SELECT * FROM C_BPartner bp " + "WHERE bp.C_BPartner_ID=al.C_BPartner_ID AND bp.C_BP_Group_ID=?)");
else
sql.append(" AND AD_Client_ID=?");
if (p_DateAcct_From != null)
sql.append(" AND TRIM(ah.DateAcct) >= ?");
if (p_DateAcct_To != null)
sql.append(" AND TRIM(ah.DateAcct) <= ?");
// Do not delete Cash Trx
sql.append(" AND al.C_CashLine_ID IS NULL)");
// Open Period
sql.append(" AND EXISTS (SELECT * FROM C_Period p" + " INNER JOIN C_PeriodControl pc ON (p.C_Period_ID=pc.C_Period_ID AND pc.DocBaseType='CMA') " + "WHERE ah.DateAcct BETWEEN p.StartDate AND p.EndDate)");
//
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql.toString(), m_trx.getTrxName());
int index = 1;
if (p_C_BPartner_ID != 0)
pstmt.setInt(index++, p_C_BPartner_ID);
else if (p_C_BP_Group_ID != 0)
pstmt.setInt(index++, p_C_BP_Group_ID);
else
pstmt.setInt(index++, Env.getAD_Client_ID(getCtx()));
if (p_DateAcct_From != null)
pstmt.setTimestamp(index++, p_DateAcct_From);
if (p_DateAcct_To != null)
pstmt.setTimestamp(index++, p_DateAcct_To);
rs = pstmt.executeQuery();
while (rs.next()) {
MAllocationHdr hdr = new MAllocationHdr(getCtx(), rs, m_trx.getTrxName());
if (delete(hdr))
count++;
}
} catch (Exception e) {
log.log(Level.SEVERE, sql.toString(), e);
m_trx.rollback();
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
m_trx.close();
return "@Deleted@ #" + count;
}
use of org.compiere.model.MAllocationHdr in project adempiere by adempiere.
the class Doc_AllocationTax method loadDocumentDetails.
/**
* Load Specific Document Details
* @return error message or null
*/
protected String loadDocumentDetails() {
MAllocationHdr alloc = (MAllocationHdr) getPO();
setDateDoc(alloc.getDateTrx());
// Contained Objects
p_lines = loadLines(alloc);
return null;
}
use of org.compiere.model.MAllocationHdr in project adempiere by adempiere.
the class InvoiceWriteOff method writeOff.
// doIt
/**
* Write Off
* @param invoiceId invoice
* @param documentNo doc no
* @param dateInvoiced date
* @param currencyId currency
* @param openAmt open amt
* @return true if written off
*/
private boolean writeOff(int invoiceId, String documentNo, Timestamp dateInvoiced, int currencyId, BigDecimal openAmt) {
// Nothing to do
if (openAmt == null || openAmt.signum() == 0)
return false;
if (openAmt.abs().compareTo(getMaximumWriteOffperInvoice()) >= 0)
return false;
//
if (isSimulation()) {
addLog("@IsSimulation@");
addLog(invoiceId, dateInvoiced, openAmt, documentNo);
return true;
}
// Invoice
MInvoice invoice = new MInvoice(getCtx(), invoiceId, get_TrxName());
if (!invoice.isSOTrx())
openAmt = openAmt.negate();
// Allocation
if (allocation == null || currencyId != allocation.getC_Currency_ID()) {
processAllocation();
allocation = new MAllocationHdr(getCtx(), true, getAccountDate(), currencyId, getProcessInfo().getTitle() + " #" + getAD_PInstance_ID(), get_TrxName());
allocation.setAD_Org_ID(invoice.getAD_Org_ID());
if (!allocation.save()) {
log.log(Level.SEVERE, "Cannot create allocation header");
return false;
}
}
// Payment
if (isCreatePayment() && (payment == null || invoice.getC_BPartner_ID() != payment.getC_BPartner_ID() || currencyId != payment.getC_Currency_ID())) {
processPayment();
payment = new MPayment(getCtx(), 0, get_TrxName());
payment.setAD_Org_ID(invoice.getAD_Org_ID());
payment.setC_BankAccount_ID(getBankAccountId());
payment.setTenderType(MPayment.TENDERTYPE_Check);
payment.setDateTrx(getAccountDate());
payment.setDateAcct(getAccountDate());
payment.setDescription(getProcessInfo().getTitle() + " #" + getAD_PInstance_ID());
payment.setC_BPartner_ID(invoice.getC_BPartner_ID());
// payments are negative
payment.setIsReceipt(true);
payment.setC_Currency_ID(currencyId);
if (!payment.save()) {
log.log(Level.SEVERE, "Cannot create payment");
return false;
}
}
// Line
MAllocationLine allocationLine = null;
if (isCreatePayment()) {
allocationLine = new MAllocationLine(allocation, openAmt, Env.ZERO, Env.ZERO, Env.ZERO);
payment.setPayAmt(payment.getPayAmt().add(openAmt));
allocationLine.setC_Payment_ID(payment.getC_Payment_ID());
} else
allocationLine = new MAllocationLine(allocation, Env.ZERO, Env.ZERO, openAmt, Env.ZERO);
allocationLine.setC_Invoice_ID(invoiceId);
if (allocationLine.save()) {
addLog(invoiceId, dateInvoiced, openAmt, documentNo);
return true;
}
// Error
log.log(Level.SEVERE, "Cannot create allocation line for C_Invoice_ID=" + invoiceId);
return false;
}
use of org.compiere.model.MAllocationHdr 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.MAllocationHdr 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