IDEMPIERE-6009 AllocationReset Not Working Correctly (#2204)

- fix wrong usage of TRIM in postgresql, must use TRUNC
- refactor to use safer methods throwing error on DB exceptions
This commit is contained in:
Carlos Ruiz 2024-01-27 13:32:46 +01:00
parent 1ec37be854
commit f56452e6d7
1 changed files with 36 additions and 55 deletions

View File

@ -16,17 +16,17 @@
*****************************************************************************/ *****************************************************************************/
package org.compiere.process; package org.compiere.process;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import org.adempiere.exceptions.AdempiereException; import org.adempiere.exceptions.AdempiereException;
import org.compiere.model.MAllocationHdr; import org.compiere.model.MAllocationHdr;
import org.compiere.model.MProcessPara; import org.compiere.model.MProcessPara;
import org.compiere.model.POResultSet;
import org.compiere.model.Query;
import org.compiere.util.AdempiereUserError; import org.compiere.util.AdempiereUserError;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg; import org.compiere.util.Msg;
import org.compiere.util.Trx; import org.compiere.util.Trx;
@ -124,63 +124,44 @@ public class AllocationReset extends SvrProcess
return msgreturn.toString(); return msgreturn.toString();
} }
// Selection List<Object> params = new ArrayList<Object>();
StringBuilder sql = new StringBuilder("SELECT * FROM C_AllocationHdr ah ") StringBuilder where = new StringBuilder("EXISTS (SELECT * FROM C_AllocationLine al WHERE C_AllocationHdr.C_AllocationHdr_ID=al.C_AllocationHdr_ID");
.append("WHERE EXISTS (SELECT * FROM C_AllocationLine al ") if (p_C_BPartner_ID != 0) {
.append("WHERE ah.C_AllocationHdr_ID=al.C_AllocationHdr_ID"); where.append(" AND al.C_BPartner_ID=?");
if (p_C_BPartner_ID != 0) params.add(p_C_BPartner_ID);
sql.append(" AND al.C_BPartner_ID=?"); } else if (p_C_BP_Group_ID != 0) {
else if (p_C_BP_Group_ID != 0) where.append(" AND EXISTS (SELECT * FROM C_BPartner bp WHERE bp.C_BPartner_ID=al.C_BPartner_ID AND bp.C_BP_Group_ID=?)");
sql.append(" AND EXISTS (SELECT * FROM C_BPartner bp ") params.add(p_C_BP_Group_ID);
.append("WHERE bp.C_BPartner_ID=al.C_BPartner_ID AND bp.C_BP_Group_ID=?)"); } else {
else where.append(" AND AD_Client_ID=?");
sql.append(" AND AD_Client_ID=?"); params.add(getAD_Client_ID());
if (p_DateAcct_From != null) }
sql.append(" AND TRIM(ah.DateAcct) >= ?"); if (p_DateAcct_From != null) {
if (p_DateAcct_To != null) where.append(" AND TRUNC(C_AllocationHdr.DateAcct) >= ?");
sql.append(" AND TRIM(ah.DateAcct) <= ?"); params.add(p_DateAcct_From);
}
if (p_DateAcct_To != null) {
where.append(" AND TRUNC(C_AllocationHdr.DateAcct) <= ?");
params.add(p_DateAcct_To);
}
// Do not delete Cash Trx // Do not delete Cash Trx
sql.append(" AND al.C_CashLine_ID IS NULL)"); where.append(" AND al.C_CashLine_ID IS NULL)");
// Open Period // Open Period
sql.append(" AND EXISTS (SELECT * FROM C_Period p") where.append(" AND EXISTS (SELECT * FROM C_Period p")
.append(" INNER JOIN C_PeriodControl pc ON (p.C_Period_ID=pc.C_Period_ID AND pc.DocBaseType='CMA') ") .append(" INNER JOIN C_PeriodControl pc ON (p.C_Period_ID=pc.C_Period_ID AND pc.DocBaseType='CMA') ")
.append("WHERE ah.DateAcct BETWEEN p.StartDate AND p.EndDate)"); .append("WHERE C_AllocationHdr.DateAcct BETWEEN p.StartDate AND p.EndDate)");
//
PreparedStatement pstmt = null; try (POResultSet<MAllocationHdr> pors = new Query(getCtx(), MAllocationHdr.Table_Name, where.toString(), get_TrxName())
ResultSet rs = null; .setClient_ID()
try .setParameters(params)
{ .scroll()) {
pstmt = DB.prepareStatement (sql.toString(), m_trx.getTrxName()); while (pors.hasNext()) {
int index = 1; MAllocationHdr hdr = pors.next();
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)) if (delete(hdr))
count++; 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();
}
StringBuilder msgreturn = new StringBuilder("@Deleted@ #").append(count); StringBuilder msgreturn = new StringBuilder("@Deleted@ #").append(count);
return msgreturn.toString(); return msgreturn.toString();
} // doIt } // doIt