From 56237276adca5a7cd5607e51080b490a168e237c Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Thu, 26 Nov 2020 03:10:31 +0100 Subject: [PATCH] IDEMPIERE-4557 Order document by DateTrx and Created on Auto Allocation process (#395) - change the order by for payments from "DateTrx" to "DateTrx, Created, C_Payment_ID" - change the order by for invoices from "DateInvoiced" to "DateInvoiced, Created, C_Invoice_ID" - refactor four JDBC managed queries to use the Query function --- .../org/compiere/process/AllocationAuto.java | 177 ++++++------------ 1 file changed, 55 insertions(+), 122 deletions(-) diff --git a/org.adempiere.base.process/src/org/compiere/process/AllocationAuto.java b/org.adempiere.base.process/src/org/compiere/process/AllocationAuto.java index b0b947750e..4118a20368 100644 --- a/org.adempiere.base.process/src/org/compiere/process/AllocationAuto.java +++ b/org.adempiere.base.process/src/org/compiere/process/AllocationAuto.java @@ -17,21 +17,21 @@ package org.compiere.process; import java.math.BigDecimal; -import java.sql.PreparedStatement; -import java.sql.ResultSet; import java.sql.Timestamp; import java.util.ArrayList; +import java.util.List; import java.util.logging.Level; import org.compiere.model.MAllocationHdr; import org.compiere.model.MAllocationLine; +import org.compiere.model.MBPartner; import org.compiere.model.MClient; import org.compiere.model.MInvoice; import org.compiere.model.MPaySelectionCheck; import org.compiere.model.MPaySelectionLine; import org.compiere.model.MPayment; +import org.compiere.model.Query; import org.compiere.util.AdempiereSystemError; -import org.compiere.util.DB; import org.compiere.util.Env; import org.compiere.util.Msg; @@ -106,68 +106,33 @@ public class AllocationAuto extends SvrProcess if (countAlloc > 0) countBP++; } - else if (p_C_BP_Group_ID != 0) - { - String sql = "SELECT C_BPartner_ID FROM C_BPartner WHERE C_BP_Group_ID=? ORDER BY Value"; - PreparedStatement pstmt = null; - ResultSet rs = null; - try - { - pstmt = DB.prepareStatement (sql, get_TrxName()); - pstmt.setInt (1, p_C_BP_Group_ID); - rs = pstmt.executeQuery (); - while (rs.next ()) - { - int C_BPartner_ID = rs.getInt(1); - int count = allocateBP (C_BPartner_ID); - if (count > 0) - { - countBP++; - countAlloc += count; - commitEx(); - } - } - } - catch (Exception e) - { - log.log(Level.SEVERE, sql, e); - } - finally - { - DB.close(rs, pstmt); - rs = null; pstmt = null; - } - } else { - String sql = "SELECT C_BPartner_ID FROM C_BPartner WHERE AD_Client_ID=? ORDER BY Value"; - PreparedStatement pstmt = null; - ResultSet rs = null; - try + String where; + int parameter; + if (p_C_BP_Group_ID != 0) { - pstmt = DB.prepareStatement (sql, get_TrxName()); - pstmt.setInt (1, Env.getAD_Client_ID(getCtx())); - rs = pstmt.executeQuery (); - while (rs.next ()) - { - int C_BPartner_ID = rs.getInt(1); - int count = allocateBP (C_BPartner_ID); - if (count > 0) - { - countBP++; - countAlloc += count; - commitEx(); - } - } - } - catch (Exception e) - { - log.log(Level.SEVERE, sql, e); + where = "C_BP_Group_ID=?"; + parameter = p_C_BP_Group_ID; } - finally + else { - DB.close(rs, pstmt); - rs = null; pstmt = null; + where = "AD_Client_ID=?"; + parameter = Env.getAD_Client_ID(getCtx()); + } + int[] ids = new Query(getCtx(), MBPartner.Table_Name, where, get_TrxName()) + .setOrderBy("Value") + .setParameters(parameter) + .getIDs(); + for (int C_BPartner_ID : ids) + { + int count = allocateBP (C_BPartner_ID); + if (count > 0) + { + countBP++; + countAlloc += count; + commitEx(); + } } } // @@ -259,43 +224,27 @@ public class AllocationAuto extends SvrProcess */ private MPayment[] getPayments (int C_BPartner_ID) { - ArrayList list = new ArrayList(); - StringBuilder sql = new StringBuilder("SELECT * FROM C_Payment ") - .append("WHERE IsAllocated='N' AND Processed='Y' AND C_BPartner_ID=?") - .append(" AND IsPrepayment='N' AND C_Charge_ID IS NULL "); + StringBuilder where = new StringBuilder("IsAllocated='N' AND Processed='Y' AND C_BPartner_ID=?" + + " AND IsPrepayment='N' AND C_Charge_ID IS NULL "); if (ONLY_AP.equals(p_APAR)) - sql.append("AND IsReceipt='N' "); + where.append("AND IsReceipt='N'"); else if (ONLY_AR.equals(p_APAR)) - sql.append("AND IsReceipt='Y' "); - sql.append("ORDER BY DateTrx"); - PreparedStatement pstmt = null; - ResultSet rs = null; - try + where.append("AND IsReceipt='Y'"); + List queryList = new Query(getCtx(), MPayment.Table_Name, where.toString(), get_TrxName()) + .setOrderBy("DateTrx, Created, C_Payment_ID") + .setParameters(C_BPartner_ID) + .list(); + ArrayList list = new ArrayList(); + for (MPayment payment : queryList) { - pstmt = DB.prepareStatement (sql.toString(), get_TrxName()); - pstmt.setInt (1, C_BPartner_ID); - rs = pstmt.executeQuery (); - while (rs.next ()) + BigDecimal allocated = payment.getAllocatedAmt(); + if (allocated != null && allocated.compareTo(payment.getPayAmt()) == 0) { - MPayment payment = new MPayment (getCtx(), rs, get_TrxName()); - BigDecimal allocated = payment.getAllocatedAmt(); - if (allocated != null && allocated.compareTo(payment.getPayAmt()) == 0) - { - payment.setIsAllocated(true); - payment.saveEx(); - } - else - list.add (payment); + payment.setIsAllocated(true); + payment.saveEx(); } - } - catch (Exception e) - { - log.log(Level.SEVERE, sql.toString(), e); - } - finally - { - DB.close(rs, pstmt); - rs = null; pstmt = null; + else + list.add (payment); } m_payments = new MPayment[list.size ()]; list.toArray (m_payments); @@ -309,41 +258,25 @@ public class AllocationAuto extends SvrProcess */ private MInvoice[] getInvoices (int C_BPartner_ID) { - ArrayList list = new ArrayList(); - StringBuilder sql = new StringBuilder("SELECT * FROM C_Invoice ") - .append("WHERE IsPaid='N' AND Processed='Y' AND C_BPartner_ID=? "); + StringBuilder where = new StringBuilder("IsPaid='N' AND Processed='Y' AND C_BPartner_ID=? "); if (ONLY_AP.equals(p_APAR)) - sql.append("AND IsSOTrx='N' "); + where.append("AND IsSOTrx='N' "); else if (ONLY_AR.equals(p_APAR)) - sql.append("AND IsSOTrx='Y' "); - sql.append("ORDER BY DateInvoiced"); - PreparedStatement pstmt = null; - ResultSet rs = null; - try + where.append("AND IsSOTrx='Y' "); + List queryList = new Query(getCtx(), MInvoice.Table_Name, where.toString(), get_TrxName()) + .setOrderBy("DateInvoiced, Created, C_Invoice_ID") + .setParameters(C_BPartner_ID) + .list(); + ArrayList list = new ArrayList(); + for (MInvoice invoice : queryList) { - pstmt = DB.prepareStatement (sql.toString(), get_TrxName()); - pstmt.setInt (1, C_BPartner_ID); - rs = pstmt.executeQuery (); - while (rs.next ()) + if (invoice.getOpenAmt(false, null).signum() == 0) { - MInvoice invoice = new MInvoice (getCtx(), rs, get_TrxName()); - if (invoice.getOpenAmt(false, null).signum() == 0) - { - invoice.setIsPaid(true); - invoice.saveEx(); - } - else - list.add (invoice); + invoice.setIsPaid(true); + invoice.saveEx(); } - } - catch (Exception e) - { - log.log(Level.SEVERE, sql.toString(), e); - } - finally - { - DB.close(rs, pstmt); - rs = null; pstmt = null; + else + list.add (invoice); } m_invoices = new MInvoice[list.size ()]; list.toArray (m_invoices);