use of org.compiere.model.MAllocationLine in project lar_361 by comitsrl.
the class PosOrderModel method completeIt.
// processOrder
@Override
public String completeIt() {
// Se ejecuta el completeIt() estándar de ADempiere
if (super.completeIt().equals(DocAction.STATUS_Invalid))
return DocAction.STATUS_Invalid;
// con la cabecera de cobros creada por la orden de venta PDV
if (!isPaidFromAccount && !m_pos.get_ValueAsBoolean("IsShipment")) {
// Se crean las imputaciones para cada cobro de la orden
final String desc = Msg.translate(Env.getCtx(), "C_Order_ID") + ": " + getDocumentNo();
final Timestamp today = Env.getContextAsDate(Env.getCtx(), "#Date");
final MAllocationHdr alloc = new MAllocationHdr(p_ctx, false, today, getC_Currency_ID(), desc, get_TrxName());
alloc.setAD_Org_ID(Env.getAD_Org_ID(Env.getCtx()));
alloc.setDateAcct(today);
alloc.saveEx();
// y crear la imputación de pago de los mismos.
for (final MPayment payment : getPayments()) {
// Asignación de la factura al cobro
payment.setC_Invoice_ID(getC_Invoice_ID());
payment.save(get_TrxName());
// Imputación del cobro
final MAllocationLine line = new MAllocationLine(alloc, payment.getPayAmt(), payment.getDiscountAmt(), payment.getWriteOffAmt(), payment.getOverUnderAmt());
line.setDocInfo(payment.getC_BPartner_ID(), getC_Order_ID(), getC_Invoice_ID());
line.setC_Payment_ID(payment.getC_Payment_ID());
line.saveEx(get_TrxName());
}
// Se completa la imputación de pago
alloc.processIt(DocAction.ACTION_Complete);
alloc.saveEx(get_TrxName());
// Se relaciona la factura generada con la cabecera de cobro
paymentHeader.setC_Invoice_ID(getC_Invoice_ID());
paymentHeader.saveEx(get_TrxName());
}
return DocAction.STATUS_Completed;
}
use of org.compiere.model.MAllocationLine 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.MAllocationLine 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();
}
}
use of org.compiere.model.MAllocationLine 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.MAllocationLine in project lar_361 by comitsrl.
the class LAR_Validator method accountingForWithholdingOnPayment.
// voidWarehouseOrder
/**
* Process acounting for withholding on sales payment
*/
private String accountingForWithholdingOnPayment(final MAllocationHdr ah) {
final Doc doc = ah.getDoc();
final List<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[] allocLines = ah.getLines(false);
for (int j = 0; j < allocLines.length; j++) {
MAllocationLine al = allocLines[j];
// TODO is this line necesary?
doc.setC_BPartner_ID(al.getC_BPartner_ID());
int c_Payment_ID = al.getC_Payment_ID();
if (c_Payment_ID <= 0)
continue;
MPayment payment = new MPayment(ah.getCtx(), c_Payment_ID, ah.get_TrxName());
if (payment == null || payment.getC_Payment_ID() == 0)
continue;
// Se determina si se procesan cobros o pagos
if (payment.isReceipt()) {
// //////////////// PROCESA COBROS //////////////////
// Determine if is an withholding or not
int c_TaxWithholding_ID = payment.get_ValueAsInt("C_TaxWithholding_ID");
if (c_TaxWithholding_ID <= 0)
continue;
if (payment.getWriteOffAmt().compareTo(Env.ZERO) <= 0)
continue;
// Iterates over factlines, searching one with writeoff account
// in order to change it to the retrieved from processed payment
final FactLine[] factlines = fact.getLines();
for (int ifl = 0; ifl < factlines.length; ifl++) {
final FactLine fl = factlines[ifl];
// if factline account is WriteOff, change it
if (fl.getAccount().equals(doc.getAccount(Doc.ACCTTYPE_WriteOff, as))) {
// Creates factline with proper account (using c_taxwithholding_id from processed payment)
final BigDecimal withholdingAmt = payment.getWriteOffAmt();
final MTax tw = new MTax(ah.getCtx(), c_TaxWithholding_ID, ah.get_TrxName());
final DocTax taxLine = new DocTax(c_TaxWithholding_ID, tw.getName(), tw.getRate(), Env.ZERO, withholdingAmt, tw.isSalesTax());
final FactLine newFactLine = fact.createLine(null, taxLine.getAccount(DocTax.ACCTTYPE_TaxCredit, as), as.getC_Currency_ID(), withholdingAmt, null);
if (newFactLine != null)
newFactLine.setC_Tax_ID(c_TaxWithholding_ID);
// Removes factline with writeoff account from fact
log.info(String.format("Replace factline: %s -> %s", fl, newFactLine));
fact.remove(fl);
}
}
}
}
}
return null;
}
Aggregations