📄 mgoal.java
字号:
/**
* Base Constructor
* @param ctx context
* @param Name Name
* @param Description Decsription
* @param MeasureTarget target
* @param trxName trx
*/
public MGoal (Properties ctx, String Name, String Description,
BigDecimal MeasureTarget, String trxName)
{
super (ctx, 0, trxName);
setName(Name);
setDescription(Description);
setMeasureTarget(MeasureTarget);
} // MGoal
/** Restrictions */
private MGoalRestriction[] m_restrictions = null;
/** Performance Color */
private Color m_color = null;
/**
* Get Restriction Lines
* @param reload reload data
* @return array of lines
*/
public MGoalRestriction[] getRestrictions (boolean reload)
{
if (m_restrictions != null && !reload)
return m_restrictions;
ArrayList<MGoalRestriction> list = new ArrayList<MGoalRestriction>();
//
String sql = "SELECT * FROM PA_GoalRestriction "
+ "WHERE PA_Goal_ID=? AND IsActive='Y' "
+ "ORDER BY Org_ID, C_BPartner_ID, M_Product_ID";
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, get_TrxName());
pstmt.setInt (1, getPA_Goal_ID());
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
list.add (new MGoalRestriction (getCtx(), rs, get_TrxName()));
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
log.log (Level.SEVERE, sql, e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
//
m_restrictions = new MGoalRestriction[list.size ()];
list.toArray (m_restrictions);
return m_restrictions;
} // getRestrictions
/**
* Get Measure
* @return measure or null
*/
public MMeasure getMeasure()
{
if (getPA_Measure_ID() != 0)
return MMeasure.get(getCtx(), getPA_Measure_ID());
return null;
} // getMeasure
/**************************************************************************
* Update/save Goals for the same measure
* @param force force to update goal (default once per day)
* @return true if updated
*/
public boolean updateGoal(boolean force)
{
log.config("Force=" + force);
MMeasure measure = MMeasure.get(getCtx(), getPA_Measure_ID());
if (force
|| getDateLastRun() == null
|| !TimeUtil.isSameHour(getDateLastRun(), null))
{
if (measure.updateGoals()) // saves
{
load(get_ID(), get_TrxName());
return true;
}
}
return false;
} // updateGoal
/**
* Set Measure Actual
* @param MeasureActual actual
*/
public void setMeasureActual (BigDecimal MeasureActual)
{
if (MeasureActual == null)
return;
super.setMeasureActual (MeasureActual);
setDateLastRun(new Timestamp(System.currentTimeMillis()));
setGoalPerformance();
} // setMeasureActual
/**
* Calculate Performance Goal as multiplier
*/
public void setGoalPerformance ()
{
BigDecimal MeasureTarget = getMeasureTarget();
BigDecimal MeasureActual = getMeasureActual();
BigDecimal GoalPerformance = Env.ZERO;
if (MeasureTarget.signum() != 0)
GoalPerformance = MeasureActual.divide(MeasureTarget, 6, BigDecimal.ROUND_HALF_UP);
super.setGoalPerformance (GoalPerformance);
m_color = null;
} // setGoalPerformance
/**
* Get Goal Performance as Double
* @return performance as multipier
*/
public double getGoalPerformanceDouble()
{
BigDecimal bd = getGoalPerformance();
return bd.doubleValue();
} // getGoalPerformanceDouble
/**
* Get Goal Performance in Percent
* @return performance in percent
*/
public int getPercent()
{
BigDecimal bd = getGoalPerformance().multiply(Env.ONEHUNDRED);
return bd.intValue();
} // getPercent
/**
* Get Color
* @return color - white if no target
*/
public Color getColor()
{
if (m_color == null)
{
if (getMeasureTarget().signum() == 0)
m_color = Color.white;
else
m_color = MColorSchema.getColor(getCtx(), getPA_ColorSchema_ID(), getPercent());
}
return m_color;
} // getColor
/**
* Get Measure Display
* @return Measure Display
*/
public String getMeasureDisplay ()
{
String s = super.getMeasureDisplay ();
if (s == null)
{
if (MEASURESCOPE_Week.equals(getMeasureScope()))
s = MEASUREDISPLAY_Week;
else if (MEASURESCOPE_Day.equals(getMeasureScope()))
s = MEASUREDISPLAY_Day;
else
s = MEASUREDISPLAY_Month;
}
return s;
} // getMeasureDisplay
/**
* Get Measure Display Text
* @return Measure Display Text
*/
public String getXAxisText ()
{
MMeasure measure = getMeasure();
if (measure != null
&& MMeasure.MEASUREDATATYPE_StatusQtyAmount.equals(measure.getMeasureDataType()))
{
if (MMeasure.MEASURETYPE_Request.equals(measure.getMeasureType()))
return Msg.getElement(getCtx(), "R_Status_ID");
if (MMeasure.MEASURETYPE_Project.equals(measure.getMeasureType()))
return Msg.getElement(getCtx(), "C_Phase_ID");
}
String value = getMeasureDisplay();
String display = MRefList.getListName(getCtx(), MEASUREDISPLAY_AD_Reference_ID, value);
return display==null ? value : display;
} // getMeasureDisplayText
/**
* Goal has Target
* @return true if target
*/
public boolean isTarget()
{
return getMeasureTarget().signum() != 0;
} // isTarget
/**
* String Representation
* @return info
*/
public String toString ()
{
StringBuffer sb = new StringBuffer ("MGoal[");
sb.append (get_ID ())
.append ("-").append (getName())
.append(",").append(getGoalPerformance())
.append ("]");
return sb.toString ();
} // toString
/**
* Before Save
* @param newRecord new
* @return true
*/
protected boolean beforeSave (boolean newRecord)
{
// if (getMultiplier(this) == null) // error
// setMeasureDisplay(getMeasureScope());
// Measure required if nor Summary
if (!isSummary() && getPA_Measure_ID() == 0)
{
log.saveError("FillMandatory", Msg.getElement(getCtx(), "PA_Measure_ID"));
return false;
}
if (isSummary() && getPA_Measure_ID() != 0)
setPA_Measure_ID(0);
// User/Role Check
if ((newRecord || is_ValueChanged("AD_User_ID") || is_ValueChanged("AD_Role_ID"))
&& getAD_User_ID() != 0)
{
MUser user = MUser.get(getCtx(), getAD_User_ID());
MRole[] roles = user.getRoles(getAD_Org_ID());
if (roles.length == 0) // No Role
setAD_Role_ID(0);
else if (roles.length == 1) // One
setAD_Role_ID(roles[0].getAD_Role_ID());
else
{
int AD_Role_ID = getAD_Role_ID();
if (AD_Role_ID != 0) // validate
{
boolean found = false;
for (int i = 0; i < roles.length; i++)
{
if (AD_Role_ID == roles[i].getAD_Role_ID())
{
found = true;
break;
}
}
if (!found)
AD_Role_ID = 0;
}
if (AD_Role_ID == 0) // set to first one
setAD_Role_ID(roles[0].getAD_Role_ID());
} // multiple roles
} // user check
return true;
} // beforeSave
/**
* After Save
* @param newRecord new
* @param success success
* @return true
*/
protected boolean afterSave (boolean newRecord, boolean success)
{
if (!success)
return success;
// Update Goal if Target / Scope Changed
if (newRecord
|| is_ValueChanged("MeasureTarget")
|| is_ValueChanged("MeasureScope"))
updateGoal(true);
return success;
}
} // MGoal
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -