From 21cad08cbba2c0c1fd1400be75315bee0c577599 Mon Sep 17 00:00:00 2001 From: hengsin Date: Mon, 30 Mar 2020 17:21:29 +0800 Subject: [PATCH 1/3] IDEMPIERE-4200 AR Payment Allocation: Incorrect Currency Balancing Postings --- .../org/compiere/acct/Doc_AllocationHdr.java | 42 +++++++++++++------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/acct/Doc_AllocationHdr.java b/org.adempiere.base/src/org/compiere/acct/Doc_AllocationHdr.java index 9b656af07c..5a346ab36a 100644 --- a/org.adempiere.base/src/org/compiere/acct/Doc_AllocationHdr.java +++ b/org.adempiere.base/src/org/compiere/acct/Doc_AllocationHdr.java @@ -845,7 +845,10 @@ public class Doc_AllocationHdr extends Doc if (valuesInv != null) { if (invoice.getReversal_ID() == 0 || invoice.get_ID() < invoice.getReversal_ID()) { - if ((invoice.isSOTrx() && !invoice.isCreditMemo()) || (!invoice.isSOTrx() && invoice.isCreditMemo())) { + if ((invoice.isSOTrx() && invoice.getGrandTotal().signum() >= 0 && !invoice.isCreditMemo()) + || (invoice.isSOTrx() && invoice.getGrandTotal().signum() < 0 && invoice.isCreditMemo()) + || (!invoice.isSOTrx() && invoice.getGrandTotal().signum() >= 0 && invoice.isCreditMemo()) + || (!invoice.isSOTrx() && invoice.getGrandTotal().signum() < 0 && !invoice.isCreditMemo())) { invoiceSource = (BigDecimal) valuesInv.get(0); // AmtSourceDr invoiceAccounted = (BigDecimal) valuesInv.get(1); // AmtAcctDr } else { @@ -855,7 +858,10 @@ public class Doc_AllocationHdr extends Doc } else { - if ((invoice.isSOTrx() && !invoice.isCreditMemo()) || (!invoice.isSOTrx() && invoice.isCreditMemo())) { + if ((invoice.isSOTrx() && invoice.getGrandTotal().signum() >= 0 && !invoice.isCreditMemo()) + || (invoice.isSOTrx() && invoice.getGrandTotal().signum() < 0 && invoice.isCreditMemo()) + || (!invoice.isSOTrx() && invoice.getGrandTotal().signum() >= 0 && invoice.isCreditMemo()) + || (!invoice.isSOTrx() && invoice.getGrandTotal().signum() < 0 && !invoice.isCreditMemo())) { invoiceSource = (BigDecimal) valuesInv.get(2); // AmtSourceCr invoiceAccounted = (BigDecimal) valuesInv.get(3); // AmtAcctCr } else { @@ -912,7 +918,10 @@ public class Doc_AllocationHdr extends Doc MAccount loss = MAccount.get (as.getCtx(), as.getAcctSchemaDefault().getRealizedLoss_Acct()); // - if ((invoice.isSOTrx() && !invoice.isCreditMemo()) || (!invoice.isSOTrx() && invoice.isCreditMemo())) + if ((invoice.isSOTrx() && invoice.getGrandTotal().signum() >= 0 && !invoice.isCreditMemo()) + || (invoice.isSOTrx() && invoice.getGrandTotal().signum() < 0 && invoice.isCreditMemo()) + || (!invoice.isSOTrx() && invoice.getGrandTotal().signum() >= 0 && invoice.isCreditMemo()) + || (!invoice.isSOTrx() && invoice.getGrandTotal().signum() < 0 && !invoice.isCreditMemo())) { FactLine fl = fact.createLine (line, loss, gain, as.getC_Currency_ID(), acctDifference); fl.setDescription(description.toString()); @@ -1080,7 +1089,10 @@ public class Doc_AllocationHdr extends Doc BigDecimal invoiceAccounted = null; if (invoice.getReversal_ID() == 0 || invoice.get_ID() < invoice.getReversal_ID()) { - if ((invoice.isSOTrx() && !invoice.isCreditMemo()) || (!invoice.isSOTrx() && invoice.isCreditMemo())) { + if ((invoice.isSOTrx() && invoice.getGrandTotal().signum() >= 0 && !invoice.isCreditMemo()) + || (invoice.isSOTrx() && invoice.getGrandTotal().signum() < 0 && invoice.isCreditMemo()) + || (!invoice.isSOTrx() && invoice.getGrandTotal().signum() >= 0 && invoice.isCreditMemo()) + || (!invoice.isSOTrx() && invoice.getGrandTotal().signum() < 0 && !invoice.isCreditMemo())) { invoiceSource = (BigDecimal) valuesInv.get(0); // AmtSourceDr invoiceAccounted = (BigDecimal) valuesInv.get(1); // AmtAcctDr } else { @@ -1090,7 +1102,10 @@ public class Doc_AllocationHdr extends Doc } else { - if ((invoice.isSOTrx() && !invoice.isCreditMemo()) || (!invoice.isSOTrx() && invoice.isCreditMemo())) { + if ((invoice.isSOTrx() && invoice.getGrandTotal().signum() >= 0 && !invoice.isCreditMemo()) + || (invoice.isSOTrx() && invoice.getGrandTotal().signum() < 0 && invoice.isCreditMemo()) + || (!invoice.isSOTrx() && invoice.getGrandTotal().signum() >= 0 && invoice.isCreditMemo()) + || (!invoice.isSOTrx() && invoice.getGrandTotal().signum() < 0 && !invoice.isCreditMemo())) { invoiceSource = (BigDecimal) valuesInv.get(2); // AmtSourceCr invoiceAccounted = (BigDecimal) valuesInv.get(3); // AmtAcctCr } else { @@ -1199,12 +1214,12 @@ public class Doc_AllocationHdr extends Doc { if (totalAmtAcctDr.compareTo(totalAmtAcctCr) > 0) { - allocateSource = allocateSource.add(totalAmtSourceDr); + allocateSource = allocateSource.add(totalAmtSourceDr).subtract(totalAmtSourceCr); allocateAccounted = allocateAccounted.add(totalAmtAcctDr).subtract(totalAmtAcctCr); } else { - allocateSource = allocateSource.add(totalAmtSourceCr); + allocateSource = allocateSource.add(totalAmtSourceCr).subtract(totalAmtSourceDr); allocateAccounted = allocateAccounted.add(totalAmtAcctCr).subtract(totalAmtAcctDr); } } @@ -1335,12 +1350,15 @@ public class Doc_AllocationHdr extends Doc if (acctDifference == null || acctDifference.signum() == 0) { log.fine("No Difference"); - return null; + continue; } // Integer C_AllocationLine_ID = htInvAllocLine.get(invoice.getC_Invoice_ID()); - if ((invoice.isSOTrx() && !invoice.isCreditMemo()) || (!invoice.isSOTrx() && invoice.isCreditMemo())) + if ((invoice.isSOTrx() && invoice.getGrandTotal().signum() >= 0 && !invoice.isCreditMemo()) + || (invoice.isSOTrx() && invoice.getGrandTotal().signum() < 0 && invoice.isCreditMemo()) + || (!invoice.isSOTrx() && invoice.getGrandTotal().signum() >= 0 && invoice.isCreditMemo()) + || (!invoice.isSOTrx() && invoice.getGrandTotal().signum() < 0 && !invoice.isCreditMemo())) { FactLine fl = fact.createLine (null, acct, as.getC_Currency_ID(), acctDifference); fl.setDescription(description.toString()); @@ -1525,12 +1543,12 @@ public class Doc_AllocationHdr extends Doc { if (totalAmtAcctDr.compareTo(totalAmtAcctCr) > 0) { - allocateSource = allocateSource.add(totalAmtSourceDr); + allocateSource = allocateSource.add(totalAmtSourceDr).subtract(totalAmtSourceCr); allocateAccounted = allocateAccounted.add(totalAmtAcctDr).subtract(totalAmtAcctCr); } else { - allocateSource = allocateSource.add(totalAmtSourceCr); + allocateSource = allocateSource.add(totalAmtSourceCr).subtract(totalAmtSourceDr); allocateAccounted = allocateAccounted.add(totalAmtAcctCr).subtract(totalAmtAcctDr); } } @@ -1665,7 +1683,7 @@ public class Doc_AllocationHdr extends Doc if (acctDifference == null || acctDifference.signum() == 0) { log.fine("No Difference"); - return null; + continue; } // From 6e606799f911b08fc4b3a499af2b03c528a9dd25 Mon Sep 17 00:00:00 2001 From: hengsin Date: Wed, 1 Apr 2020 17:12:51 +0800 Subject: [PATCH 2/3] IDEMPIERE-4225 Do not cache MSession that's not thread safe --- .../src/org/compiere/model/MSession.java | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/MSession.java b/org.adempiere.base/src/org/compiere/model/MSession.java index 76f245307e..93315b759c 100644 --- a/org.adempiere.base/src/org/compiere/model/MSession.java +++ b/org.adempiere.base/src/org/compiere/model/MSession.java @@ -20,12 +20,11 @@ import java.net.InetAddress; import java.net.UnknownHostException; import java.sql.ResultSet; import java.util.Properties; +import java.util.Vector; import java.util.logging.Level; import org.compiere.Adempiere; -import org.compiere.util.CCache; import org.compiere.util.Env; -import org.compiere.util.Ini; import org.compiere.util.TimeUtil; import org.compiere.util.WebUtil; @@ -58,16 +57,15 @@ public class MSession extends X_AD_Session { int AD_Session_ID = Env.getContextAsInt(ctx, "#AD_Session_ID"); MSession session = null; - if (AD_Session_ID > 0) - session = (MSession)s_sessions.get(Integer.valueOf(AD_Session_ID)); // Try to load - if (session == null && AD_Session_ID > 0) + if (AD_Session_ID > 0 && s_sessions.contains(AD_Session_ID)) { session = new MSession(ctx, AD_Session_ID, null); - if (session.get_ID() != AD_Session_ID) { - Env.setContext (ctx, "#AD_Session_ID", AD_Session_ID); + if (session.get_ID() != AD_Session_ID) + { + session = null; + s_sessions.remove(AD_Session_ID); } - s_sessions.put(AD_Session_ID, session); } // Create New if (session == null && createNew) @@ -76,7 +74,7 @@ public class MSession extends X_AD_Session session.saveEx(); AD_Session_ID = session.getAD_Session_ID(); Env.setContext (ctx, "#AD_Session_ID", AD_Session_ID); - s_sessions.put (Integer.valueOf(AD_Session_ID), session); + s_sessions.add (Integer.valueOf(AD_Session_ID)); } return session; } // get @@ -93,23 +91,29 @@ public class MSession extends X_AD_Session { int AD_Session_ID = Env.getContextAsInt(ctx, "#AD_Session_ID"); MSession session = null; - if (AD_Session_ID > 0) - session = (MSession)s_sessions.get(Integer.valueOf(AD_Session_ID)); + // Try to load + if (AD_Session_ID > 0 && s_sessions.contains(AD_Session_ID)) + { + session = new MSession(ctx, AD_Session_ID, null); + if (session.get_ID() != AD_Session_ID) + { + session = null; + s_sessions.remove(AD_Session_ID); + } + } if (session == null) { session = new MSession (ctx, Remote_Addr, Remote_Host, WebSession, null); // remote session session.saveEx(); AD_Session_ID = session.getAD_Session_ID(); Env.setContext(ctx, "#AD_Session_ID", AD_Session_ID); - s_sessions.put(Integer.valueOf(AD_Session_ID), session); - } + s_sessions.add(Integer.valueOf(AD_Session_ID)); + } return session; } // get /** Sessions */ - private static CCache s_sessions = Ini.isClient() - ? new CCache(null, "AD_Session_ID", 1, 0, false) // one client session - : new CCache(null, "AD_Session_ID", 30, 0, false); // no time-out + private static Vector s_sessions = new Vector<>(); /************************************************************************** From bf8f5ea8c1102433c67ebdcadbe2e6171ebde61a Mon Sep 17 00:00:00 2001 From: hengsin Date: Wed, 1 Apr 2020 17:19:18 +0800 Subject: [PATCH 3/3] IDEMPIERE-4206 Session timeout shown sometimes when changing roles --- .../WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java | 8 ++++++-- .../adempiere/webui/session/SessionContextListener.java | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java index 6899059656..a841868c40 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java @@ -81,6 +81,8 @@ import org.zkoss.zul.Window; */ public class AdempiereWebUI extends Window implements EventListener, IWebClient { + public static final String DESKTOP_SESSION_INVALIDATED_ATTR = "DesktopSessionInvalidated"; + /** * */ @@ -569,9 +571,11 @@ public class AdempiereWebUI extends Window implements EventListener, IWeb //redirect must happens before removeDesktop below, otherwise you get NPE Executions.getCurrent().sendRedirect("index.zul"); - //remove old desktop - if (desktopCache != null) + //remove old desktop + if (desktopCache != null) { + desktop.setAttribute(DESKTOP_SESSION_INVALIDATED_ATTR, Boolean.TRUE); desktopCache.removeDesktop(desktop); + } } @Override diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/SessionContextListener.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/SessionContextListener.java index 6efd564e81..c09c9933e0 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/SessionContextListener.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/SessionContextListener.java @@ -237,6 +237,11 @@ public class SessionContextListener implements ExecutionInit, return; } + Object sessionInvalidated = desktop.getAttribute(AdempiereWebUI.DESKTOP_SESSION_INVALIDATED_ATTR); + if (sessionInvalidated != null) { + return; + } + if (ServerContext.getCurrentInstance().isEmpty() || !isContextValid()) { setupExecutionContextFromSession(Executions.getCurrent());