IDEMPIERE-5476 Refactoring of WRequest and MRequest (#1560)
This commit is contained in:
parent
be7fb240cb
commit
da587235c7
|
@ -17,13 +17,16 @@
|
|||
package org.compiere.model;
|
||||
|
||||
import java.io.File;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.adempiere.exceptions.AdempiereException;
|
||||
import org.compiere.util.CLogger;
|
||||
import org.compiere.util.DB;
|
||||
import org.compiere.util.Env;
|
||||
import org.compiere.util.TimeUtil;
|
||||
|
||||
|
@ -914,4 +917,122 @@ public class MRequest extends X_R_Request
|
|||
this.m_changed = changed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get number of active and inactive request
|
||||
* @param AD_Table_ID
|
||||
* @param Record_ID
|
||||
* @param whereClause
|
||||
* @param trxName
|
||||
* @return int[], [0] = inactive request count and [1] = active request count
|
||||
*/
|
||||
public static int[] getRequestCount(int AD_Table_ID, int Record_ID, StringBuilder whereClause, String trxName) {
|
||||
int[] counts = new int[] {0, 0};
|
||||
|
||||
whereClause.append("(AD_Table_ID=").append(AD_Table_ID)
|
||||
.append(" AND Record_ID=").append(Record_ID)
|
||||
.append(")");
|
||||
//
|
||||
if (AD_Table_ID == MUser.Table_ID)
|
||||
whereClause.append(" OR AD_User_ID=").append(Record_ID)
|
||||
.append(" OR SalesRep_ID=").append(Record_ID);
|
||||
else if (AD_Table_ID == MBPartner.Table_ID)
|
||||
whereClause.append(" OR C_BPartner_ID=").append(Record_ID);
|
||||
else if (AD_Table_ID == MOrder.Table_ID)
|
||||
whereClause.append(" OR C_Order_ID=").append(Record_ID);
|
||||
else if (AD_Table_ID == MInvoice.Table_ID)
|
||||
whereClause.append(" OR C_Invoice_ID=").append(Record_ID);
|
||||
else if (AD_Table_ID == MPayment.Table_ID)
|
||||
whereClause.append(" OR C_Payment_ID=").append(Record_ID);
|
||||
else if (AD_Table_ID == MProduct.Table_ID)
|
||||
whereClause.append(" OR M_Product_ID=").append(Record_ID);
|
||||
else if (AD_Table_ID == MProject.Table_ID)
|
||||
whereClause.append(" OR C_Project_ID=").append(Record_ID);
|
||||
else if (AD_Table_ID == MCampaign.Table_ID)
|
||||
whereClause.append(" OR C_Campaign_ID=").append(Record_ID);
|
||||
else if (AD_Table_ID == MAsset.Table_ID)
|
||||
whereClause.append(" OR A_Asset_ID=").append(Record_ID);
|
||||
//
|
||||
String sql = "SELECT Processed, COUNT(*) "
|
||||
+ "FROM R_Request WHERE " + whereClause
|
||||
+ " GROUP BY Processed "
|
||||
+ "ORDER BY Processed DESC";
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
try
|
||||
{
|
||||
pstmt = DB.prepareStatement (sql, trxName);
|
||||
rs = pstmt.executeQuery ();
|
||||
while (rs.next ())
|
||||
{
|
||||
if ("Y".equals(rs.getString(1)))
|
||||
counts[0] += rs.getInt(2);
|
||||
else
|
||||
counts[1] += rs.getInt(2);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new AdempiereException(e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DB.close(rs, pstmt);
|
||||
}
|
||||
|
||||
return counts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new request
|
||||
* @param tab Grid Tab for request
|
||||
* @param AD_Table_ID
|
||||
* @param Record_ID
|
||||
* @param C_BPartner_ID
|
||||
*/
|
||||
public static void newRequest(GridTab tab, int AD_Table_ID, int Record_ID, int C_BPartner_ID) {
|
||||
tab.dataNew (false);
|
||||
tab.setValue("AD_Table_ID", Integer.valueOf(AD_Table_ID));
|
||||
tab.setValue("Record_ID", Integer.valueOf(Record_ID));
|
||||
//
|
||||
if (C_BPartner_ID != 0)
|
||||
tab.setValue("C_BPartner_ID", Integer.valueOf(C_BPartner_ID));
|
||||
//
|
||||
if (AD_Table_ID == MBPartner.Table_ID)
|
||||
tab.setValue("C_BPartner_ID", Integer.valueOf(Record_ID));
|
||||
else if (AD_Table_ID == MUser.Table_ID)
|
||||
tab.setValue("AD_User_ID", Integer.valueOf(Record_ID));
|
||||
//
|
||||
else if (AD_Table_ID == MProject.Table_ID)
|
||||
tab.setValue("C_Project_ID", Integer.valueOf(Record_ID));
|
||||
else if (AD_Table_ID == MAsset.Table_ID)
|
||||
tab.setValue("A_Asset_ID", Integer.valueOf(Record_ID));
|
||||
//
|
||||
else if (AD_Table_ID == MOrder.Table_ID)
|
||||
tab.setValue("C_Order_ID", Integer.valueOf(Record_ID));
|
||||
else if (AD_Table_ID == MInvoice.Table_ID)
|
||||
tab.setValue("C_Invoice_ID", Integer.valueOf(Record_ID));
|
||||
//
|
||||
else if (AD_Table_ID == MProduct.Table_ID)
|
||||
tab.setValue("M_Product_ID", Integer.valueOf(Record_ID));
|
||||
else if (AD_Table_ID == MPayment.Table_ID)
|
||||
tab.setValue("C_Payment_ID", Integer.valueOf(Record_ID));
|
||||
//
|
||||
else if (AD_Table_ID == MInOut.Table_ID)
|
||||
tab.setValue("M_InOut_ID", Integer.valueOf(Record_ID));
|
||||
else if (AD_Table_ID == MRMA.Table_ID)
|
||||
tab.setValue("M_RMA_ID", Integer.valueOf(Record_ID));
|
||||
//
|
||||
else if (AD_Table_ID == MCampaign.Table_ID)
|
||||
tab.setValue("C_Campaign_ID", Integer.valueOf(Record_ID));
|
||||
//
|
||||
else if (AD_Table_ID == MRequest.Table_ID)
|
||||
tab.setValue(MRequest.COLUMNNAME_R_RequestRelated_ID, Integer.valueOf(Record_ID));
|
||||
// FR [2842165] - Order Ref link from SO line creating new request
|
||||
else if (AD_Table_ID == MOrderLine.Table_ID) {
|
||||
MOrderLine oLine = new MOrderLine(Env.getCtx(), Record_ID, null);
|
||||
if (oLine != null) {
|
||||
tab.setValue(MOrderLine.COLUMNNAME_C_Order_ID, Integer.valueOf(oLine.getC_Order_ID()));
|
||||
}
|
||||
}
|
||||
}
|
||||
} // MRequest
|
||||
|
|
|
@ -14,8 +14,6 @@
|
|||
|
||||
package org.adempiere.webui;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.adempiere.util.Callback;
|
||||
|
@ -23,23 +21,10 @@ import org.adempiere.webui.adwindow.ADWindow;
|
|||
import org.adempiere.webui.session.SessionManager;
|
||||
import org.adempiere.webui.theme.ThemeManager;
|
||||
import org.compiere.model.GridTab;
|
||||
import org.compiere.model.MAsset;
|
||||
import org.compiere.model.MBPartner;
|
||||
import org.compiere.model.MCampaign;
|
||||
import org.compiere.model.MInOut;
|
||||
import org.compiere.model.MInvoice;
|
||||
import org.compiere.model.MOrder;
|
||||
import org.compiere.model.MOrderLine;
|
||||
import org.compiere.model.MPayment;
|
||||
import org.compiere.model.MProduct;
|
||||
import org.compiere.model.MProject;
|
||||
import org.compiere.model.MQuery;
|
||||
import org.compiere.model.MRMA;
|
||||
import org.compiere.model.MRequest;
|
||||
import org.compiere.model.MUser;
|
||||
import static org.compiere.model.SystemIDs.*;
|
||||
import org.compiere.util.CLogger;
|
||||
import org.compiere.util.DB;
|
||||
import org.compiere.util.Env;
|
||||
import org.compiere.util.Msg;
|
||||
import org.zkoss.zk.ui.Component;
|
||||
|
@ -92,7 +77,7 @@ public class WRequest implements EventListener<Event>
|
|||
private Menuitem m_active = null;
|
||||
private Menuitem m_all = null;
|
||||
/** Where Clause */
|
||||
StringBuffer m_where = null;
|
||||
protected StringBuilder m_where = null;
|
||||
|
||||
/** Logger */
|
||||
private static final CLogger log = CLogger.getCLogger (WRequest.class);
|
||||
|
@ -111,62 +96,10 @@ public class WRequest implements EventListener<Event>
|
|||
m_new.addEventListener(Events.ON_CLICK, this);
|
||||
m_popup.appendChild(m_new);
|
||||
//
|
||||
int activeCount = 0;
|
||||
int inactiveCount = 0;
|
||||
m_where = new StringBuffer();
|
||||
m_where.append("(AD_Table_ID=").append(m_AD_Table_ID)
|
||||
.append(" AND Record_ID=").append(m_Record_ID)
|
||||
.append(")");
|
||||
//
|
||||
if (m_AD_Table_ID == MUser.Table_ID)
|
||||
m_where.append(" OR AD_User_ID=").append(m_Record_ID)
|
||||
.append(" OR SalesRep_ID=").append(m_Record_ID);
|
||||
else if (m_AD_Table_ID == MBPartner.Table_ID)
|
||||
m_where.append(" OR C_BPartner_ID=").append(m_Record_ID);
|
||||
else if (m_AD_Table_ID == MOrder.Table_ID)
|
||||
m_where.append(" OR C_Order_ID=").append(m_Record_ID);
|
||||
else if (m_AD_Table_ID == MInvoice.Table_ID)
|
||||
m_where.append(" OR C_Invoice_ID=").append(m_Record_ID);
|
||||
else if (m_AD_Table_ID == MPayment.Table_ID)
|
||||
m_where.append(" OR C_Payment_ID=").append(m_Record_ID);
|
||||
else if (m_AD_Table_ID == MProduct.Table_ID)
|
||||
m_where.append(" OR M_Product_ID=").append(m_Record_ID);
|
||||
else if (m_AD_Table_ID == MProject.Table_ID)
|
||||
m_where.append(" OR C_Project_ID=").append(m_Record_ID);
|
||||
else if (m_AD_Table_ID == MCampaign.Table_ID)
|
||||
m_where.append(" OR C_Campaign_ID=").append(m_Record_ID);
|
||||
else if (m_AD_Table_ID == MAsset.Table_ID)
|
||||
m_where.append(" OR A_Asset_ID=").append(m_Record_ID);
|
||||
//
|
||||
String sql = "SELECT Processed, COUNT(*) "
|
||||
+ "FROM R_Request WHERE " + m_where
|
||||
+ " GROUP BY Processed "
|
||||
+ "ORDER BY Processed DESC";
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
try
|
||||
{
|
||||
pstmt = DB.prepareStatement (sql, null);
|
||||
rs = pstmt.executeQuery ();
|
||||
while (rs.next ())
|
||||
{
|
||||
if ("Y".equals(rs.getString(1)))
|
||||
inactiveCount = rs.getInt(2);
|
||||
else
|
||||
activeCount += rs.getInt(2);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
log.log(Level.SEVERE, sql, e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DB.close(rs, pstmt);
|
||||
rs = null;
|
||||
pstmt = null;
|
||||
}
|
||||
//
|
||||
m_where = new StringBuilder();
|
||||
int[] counts = MRequest.getRequestCount(m_AD_Table_ID, m_Record_ID, m_where, null);
|
||||
int activeCount = counts[1];
|
||||
int inactiveCount = counts[0];
|
||||
if (activeCount > 0)
|
||||
{
|
||||
m_active = new Menuitem(Msg.getMsg(Env.getCtx(), "RequestActive")
|
||||
|
@ -193,8 +126,9 @@ public class WRequest implements EventListener<Event>
|
|||
LayoutUtils.autoDetachOnClose(m_popup);
|
||||
}
|
||||
m_popup.open(invoker, "after_start");
|
||||
} // getZoomTargets
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEvent(final Event e) throws Exception
|
||||
{
|
||||
if (e.getTarget() instanceof Menuitem)
|
||||
|
@ -242,50 +176,7 @@ public class WRequest implements EventListener<Event>
|
|||
if (e.getTarget() == m_new)
|
||||
{
|
||||
GridTab tab = frame.getADWindowContent().getActiveGridTab();
|
||||
tab.dataNew (false);
|
||||
tab.setValue("AD_Table_ID", Integer.valueOf(m_AD_Table_ID));
|
||||
tab.setValue("Record_ID", Integer.valueOf(m_Record_ID));
|
||||
//
|
||||
if (m_C_BPartner_ID != 0)
|
||||
tab.setValue("C_BPartner_ID", Integer.valueOf(m_C_BPartner_ID));
|
||||
//
|
||||
if (m_AD_Table_ID == MBPartner.Table_ID)
|
||||
tab.setValue("C_BPartner_ID", Integer.valueOf(m_Record_ID));
|
||||
else if (m_AD_Table_ID == MUser.Table_ID)
|
||||
tab.setValue("AD_User_ID", Integer.valueOf(m_Record_ID));
|
||||
//
|
||||
else if (m_AD_Table_ID == MProject.Table_ID)
|
||||
tab.setValue("C_Project_ID", Integer.valueOf(m_Record_ID));
|
||||
else if (m_AD_Table_ID == MAsset.Table_ID)
|
||||
tab.setValue("A_Asset_ID", Integer.valueOf(m_Record_ID));
|
||||
//
|
||||
else if (m_AD_Table_ID == MOrder.Table_ID)
|
||||
tab.setValue("C_Order_ID", Integer.valueOf(m_Record_ID));
|
||||
else if (m_AD_Table_ID == MInvoice.Table_ID)
|
||||
tab.setValue("C_Invoice_ID", Integer.valueOf(m_Record_ID));
|
||||
//
|
||||
else if (m_AD_Table_ID == MProduct.Table_ID)
|
||||
tab.setValue("M_Product_ID", Integer.valueOf(m_Record_ID));
|
||||
else if (m_AD_Table_ID == MPayment.Table_ID)
|
||||
tab.setValue("C_Payment_ID", Integer.valueOf(m_Record_ID));
|
||||
//
|
||||
else if (m_AD_Table_ID == MInOut.Table_ID)
|
||||
tab.setValue("M_InOut_ID", Integer.valueOf(m_Record_ID));
|
||||
else if (m_AD_Table_ID == MRMA.Table_ID)
|
||||
tab.setValue("M_RMA_ID", Integer.valueOf(m_Record_ID));
|
||||
//
|
||||
else if (m_AD_Table_ID == MCampaign.Table_ID)
|
||||
tab.setValue("C_Campaign_ID", Integer.valueOf(m_Record_ID));
|
||||
//
|
||||
else if (m_AD_Table_ID == MRequest.Table_ID)
|
||||
tab.setValue(MRequest.COLUMNNAME_R_RequestRelated_ID, Integer.valueOf(m_Record_ID));
|
||||
// FR [2842165] - Order Ref link from SO line creating new request
|
||||
else if (m_AD_Table_ID == MOrderLine.Table_ID) {
|
||||
MOrderLine oLine = new MOrderLine(Env.getCtx(), m_Record_ID, null);
|
||||
if (oLine != null) {
|
||||
tab.setValue(MOrderLine.COLUMNNAME_C_Order_ID, Integer.valueOf(oLine.getC_Order_ID()));
|
||||
}
|
||||
}
|
||||
MRequest.newRequest(tab, m_AD_Table_ID, m_Record_ID, m_C_BPartner_ID);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -495,4 +495,16 @@ public final class DictionaryIDs {
|
|||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
||||
public enum R_RequestType {
|
||||
REQUEST_FOR_QUOTATION(100),
|
||||
SERVICE_REQUEST(101),
|
||||
WARRANTY(102);
|
||||
|
||||
public final int id;
|
||||
|
||||
private R_RequestType(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,89 @@
|
|||
/***********************************************************************
|
||||
* This file is part of iDempiere ERP Open Source *
|
||||
* http://www.idempiere.org *
|
||||
* *
|
||||
* Copyright (C) Contributors *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or *
|
||||
* modify it under the terms of the GNU General Public License *
|
||||
* as published by the Free Software Foundation; either version 2 *
|
||||
* of the License, or (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program; if not, write to the Free Software *
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, *
|
||||
* MA 02110-1301, USA. *
|
||||
* *
|
||||
* Contributors: *
|
||||
* - hengsin *
|
||||
**********************************************************************/
|
||||
package org.idempiere.test.model;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.compiere.model.GridTab;
|
||||
import org.compiere.model.GridWindow;
|
||||
import org.compiere.model.MBPartner;
|
||||
import org.compiere.model.MQuery;
|
||||
import org.compiere.model.MRequest;
|
||||
import org.compiere.model.SystemIDs;
|
||||
import org.compiere.util.Env;
|
||||
import org.idempiere.test.AbstractTestCase;
|
||||
import org.idempiere.test.DictionaryIDs;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class MRequestTest extends AbstractTestCase {
|
||||
|
||||
public MRequestTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRequestCount() {
|
||||
int[] counts = MRequest.getRequestCount(MBPartner.Table_ID, DictionaryIDs.C_BPartner.PATIO.id, new StringBuilder(), getTrxName());
|
||||
|
||||
MRequest req = new MRequest(Env.getCtx(), 0, getTrxName());
|
||||
req.setC_BPartner_ID(DictionaryIDs.C_BPartner.PATIO.id);
|
||||
req.setAD_Table_ID(MBPartner.Table_ID);
|
||||
req.setRecord_ID(req.getC_BPartner_ID());
|
||||
req.setProcessed(false);
|
||||
req.setR_RequestType_ID(DictionaryIDs.R_RequestType.SERVICE_REQUEST.id);
|
||||
req.setSummary("testRequestCount");
|
||||
req.setSalesRep_ID(getAD_User_ID());
|
||||
req.saveEx();
|
||||
|
||||
int[] counts1 = MRequest.getRequestCount(MBPartner.Table_ID, DictionaryIDs.C_BPartner.PATIO.id, new StringBuilder(), getTrxName());
|
||||
assertEquals(counts[0], counts1[0], "Unexpected processed request count");
|
||||
assertEquals(counts[1]+1, counts1[1], "Unexpected not processed request count");
|
||||
|
||||
req.setProcessed(true);
|
||||
req.saveEx();
|
||||
|
||||
counts1 = MRequest.getRequestCount(MBPartner.Table_ID, DictionaryIDs.C_BPartner.PATIO.id, new StringBuilder(), getTrxName());
|
||||
assertEquals(counts[0]+1, counts1[0], "Unexpected processed request count");
|
||||
assertEquals(counts[1], counts1[1], "Unexpected not processed request count");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGridTabNewRequest() {
|
||||
GridWindow gridWindow = GridWindow.get(Env.getCtx(), 1, SystemIDs.WINDOW_REQUESTS_ALL);
|
||||
assertNotNull(gridWindow, "Failed to load grid window of Request(All)");
|
||||
gridWindow.initTab(0);
|
||||
GridTab gridTab = gridWindow.getTab(0);
|
||||
MQuery query = new MQuery(MRequest.Table_ID);
|
||||
query.addRestriction("1=2");
|
||||
gridTab.setQuery(query);
|
||||
gridTab.query(false);
|
||||
MRequest.newRequest(gridTab, MBPartner.Table_ID, DictionaryIDs.C_BPartner.PATIO.id, DictionaryIDs.C_BPartner.PATIO.id);
|
||||
assertTrue(gridTab.isNew(), "Current row of Grid Tab is not a new row");
|
||||
assertEquals(MBPartner.Table_ID, gridTab.getValue("AD_Table_ID"));
|
||||
assertEquals(DictionaryIDs.C_BPartner.PATIO.id, gridTab.getValue("Record_ID"), "Unexpected Record_ID value");
|
||||
assertEquals(DictionaryIDs.C_BPartner.PATIO.id, gridTab.getValue("C_BPartner_ID"), "Unexpected C_BPartner_ID value");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue